-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
333 lines (271 loc) · 6.77 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <fstream>
#include <vector>
#include <thread>
#include <stdlib.h>
#include <atomic>
using namespace std;
//Screen dimension constants
const int SCREEN_WIDTH = 512;
const int SCREEN_HEIGHT = 512;
int IMAGE_HEIGHT = 7;
int IMAGE_WIDTH = 7;
vector<string> program;
SDL_Window* window;
SDL_Renderer* renderer;
struct icolor
{
int r, g, b;
icolor(int _r, int _g, int _b)
{
r = _r, g = _g, b = _b;
}
};
struct ivec2
{
int x, y;
ivec2(int _x, int _y)
{
x = _x, y = _y;
}
};
void init()
{
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return ;
// Create the window where we will draw.
window = SDL_CreateWindow("SDL_RenderClear",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
0);
// We must call SDL_CreateRenderer in order for draw calls to affect this window.
renderer = SDL_CreateRenderer(window, -1, 0);
}
#define NUM_COLORS 2
int current_index = 0;
icolor colors[NUM_COLORS] = {icolor(0, 0, 0),icolor(0, 60, 36)};
void next_color()
{
current_index++;
if(current_index >= NUM_COLORS)
{
current_index = 0;
}
SDL_SetRenderDrawColor(renderer, colors[current_index].r, colors[current_index].g, colors[current_index].b, 255);
}
void DrawTile(ivec2 pos)
{
ivec2 tile_middle = ivec2(
pos.x * (SCREEN_WIDTH / IMAGE_WIDTH),
pos.y * (SCREEN_HEIGHT / IMAGE_HEIGHT)
);
for(int x = 0; x < (SCREEN_WIDTH / IMAGE_WIDTH); x++)
{
for(int y = 0; y < (SCREEN_HEIGHT / IMAGE_HEIGHT); y++)
{
SDL_RenderDrawPoint(renderer, x + tile_middle.x, y + tile_middle.y);
}
}
SDL_RenderPresent(renderer);
}
ivec2 current_pos(0, 0);
void parse_program_line(string line)
{
if(!line.compare("^"))
{
current_pos.y --;
return;
}
if(!line.compare("v"))
{
current_pos.y ++;
return;
}
if(!line.compare("<"))
{
current_pos.x --;
return;
}
if(!line.compare(">"))
{
current_pos.x ++;
return;
}
if(!line.compare("nc"))
{
next_color();
return;
}
if(!line.compare("f"))
{
DrawTile(current_pos);
return;
}
}
void run()
{
for(unsigned int i = 0;i < program.size(); i++)
{
if(!program[i].compare(0, 1, "("))
{
char * c_times = new char;
int times;
char* p_text = new char[program[i].length() - 3];
program[i].copy(p_text, program[i].length() - 3, 1);
program[i].copy(c_times, 1, program[i].length() - 2);
times = atoi(c_times);
for(int t = 0;t < times;t++)
{
for(int ic = 0; ic < 1 + (sizeof(p_text)/sizeof(char)); ic++)
{
string instruction = string(&p_text[ic]);
instruction.resize(1);
parse_program_line(instruction);
}
}
}
else
{
parse_program_line(program[i]);
}
}
}
void load_file(string path)
{
program.clear();
string line;
ifstream savefile(path, ios::in);
if (savefile.is_open())
{
while ( getline (savefile,line) )
{
program.push_back(line);
//program.push_back("\n");
}
savefile.close();
}
}
void save_file(string path)
{
remove(path.c_str());
ofstream savefile(path, ios::out);
for(unsigned int i = 0;i < program.size(); i++)
{
savefile << program[i] << endl;
}
}
void parse_command(string cmd)
{
if(!cmd.compare("#"))
{
return;
}
if(!cmd.compare("list"))
{
for(unsigned int i = 0;i < program.size(); i++)
{
cout << program[i] << endl;
}
return;
}
if(!cmd.compare("clear"))
{
program.clear();
// Select the color for drawing. It is set to red here.
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);
current_index = 0;
next_color();
next_color();
current_pos = ivec2(0, 0);
SDL_RenderPresent(renderer);
return;
}
if(!cmd.compare("save"))
{
string path;
cin >> path;
save_file(path);
return;
}
if(!cmd.compare("load"))
{
string path;
cin >> path;
load_file(path);
return;
}
if(!cmd.compare("run"))
{
// Select the color for drawing. It is set to red here.
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);
current_index = 0;
next_color();
next_color();
current_pos = ivec2(0, 0);
run();
return;
}
program.push_back(cmd);
}
void whait_command()
{
string in_cmd;
while(in_cmd.compare("exit"))
{
cout << "$:" ;
cin >> in_cmd;
parse_command(in_cmd);
SDL_RenderPresent(renderer);
}
}
int main(int argc, char* argv[])
{
cout << "inserisci larghezza e lunghezza:";
cin >> IMAGE_WIDTH >> IMAGE_HEIGHT;
init();
// Select the color for drawing. It is set to red here.
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);
next_color();
next_color();
/*string in_cmd;
while(in_cmd.compare("exit"))
{
cout << "$:" ;
cin >> in_cmd;
parse_command(in_cmd);
SDL_RenderPresent(renderer);
}*/
thread input_thread(whait_command);
SDL_Event evt;
bool programrunning = true;
while(programrunning)
{
SDL_WaitEvent(&evt);
if(evt.type == SDL_QUIT)
programrunning = false;
}
input_thread.join();
//DrawTile(ivec2(0, 3));
// Up until now everything was drawn behind the scenes.
// This will show the new, red contents of the window.
SDL_RenderPresent(renderer);
// Give us time to see the window.
//SDL_Delay(5000);
// Always be sure to clean up
SDL_Quit();
return 0;
}