-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
360 lines (302 loc) · 11.2 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <iostream>
#include <chrono>
#include <random>
#include <math.h>
#include <SDL2/SDL.h>
using namespace std;
const int FPS = 60;
const int WIDTH = 1000, HEIGHT = 600;
const int GRID_SIZE = 4;
const int MAX_PLACE_SIZE = 20;
const int GRID_WIDTH = WIDTH / GRID_SIZE, GRID_HEIGHT = HEIGHT / GRID_SIZE;
#include "base_element.h"
#include "grid.h"
Grid grid;
#include "elements.h"
int xMouse, yMouse;
int oldXMouse, oldYMouse;
int placeSize = 2;
int selectedElement = 0;
bool heatmapEnabled = false;
// Pixel to grid square
int p_to_grid(int x){
return x / GRID_SIZE;
}
void place_element(int x0, int y0){
if (!grid.inBounds(x0, y0)){return;} // Only place element in bounds
int dx, dy, x, y;
Element *placedElement;
for (dx=-placeSize; dx<=placeSize; ++dx) // place inside circle, radius ~placeSize
for (dy=-placeSize; dy<=placeSize; ++dy) {
x = x0 + dx;
y = y0 + dy;
if (!grid.inBounds(x, y)) {continue;}
if (dx*dx + dy*dy > placeSize*placeSize + 1) {continue;} // dont place if outside circle around cursor
switch (selectedElement) {
case 0: placedElement = new Stone(x, y); break;
case 1: placedElement = new Sand(x, y); break;
case 2: placedElement = new Water(x, y); break;
case 3: placedElement = new Dirt(x, y); break;
case 4: placedElement = new Coal(x, y); break;
case 5: placedElement = new Wood(x, y); break;
case 6: placedElement = new Smoke(x, y); break;
case 7: placedElement = new Oil(x, y); break;
case 8: placedElement = new Slime(x, y); break;
case 9: placedElement = new Acid(x, y); break;
}
if (grid.isFull(x, y) && grid.getPtr(x, y)->tag == placedElement->tag) {delete placedElement; continue;} // don't overwrite same element
grid.set(x, y, placedElement);
}
}
void placeBetween(int startX, int startY, int endX, int endY){
int yDirection = 1;
if (endY < startY){yDirection = -1;}
if (endX == startX){ // Moving vertically
for (int i = 0; i <= abs(endY - startY); i++){
place_element(startX, startY + i * yDirection);
}
}
else { // Not moving vertically
float gradient = (float)(endY - startY) / (float)(endX - startX);
if (abs(gradient) > 1){
gradient = 1 / gradient;
for (int i = 0; i <= abs(endY - startY); i++){
place_element(startX + round(i * gradient) * yDirection, startY + i * yDirection);
}
}
else{
int xDirection = 1;
if (endX < startX){xDirection = -1;}
for (int i = 0; i <= abs(endX - startX); i++){
place_element(startX + i * xDirection, startY + round(i * gradient) * xDirection);
}
}
}
}
void remove_element() {
int x0 = p_to_grid(xMouse);
int y0 = p_to_grid(yMouse);
int dx, dy, x, y;
for (dx=-placeSize; dx<=placeSize; ++dx)
for (dy=-placeSize; dy<=placeSize; ++dy) {
x = x0 + dx;
y = y0 + dy;
if (!grid.inBounds(x, y)) {continue;} // Only remove element in bounds
if ((dx*dx) + (dy*dy) > placeSize*placeSize + 1) {continue;} // Only remove element inside circle around cursor
grid.reset(x, y);
}
}
void igniteInRange(int x0, int y0) {
int dx, dy, x, y;
for (dx=-placeSize; dx<=placeSize; ++dx)
for (dy=-placeSize; dy<=placeSize; ++dy) {
x = x0 + dx;
y = y0 + dy;
if (!grid.inBounds(x, y)) {continue;} // Only ignite element in bounds
if (grid.isEmpty(x, y)) {continue;}
if ((dx*dx) + (dy*dy) > placeSize*placeSize + 1) {continue;} // Only ignite element inside circle around cursor
if (!grid.getPtr(x, y)->canBeSetOnFire()) {continue;}
grid.getPtr(x, y)->ignite();
}
}
void draw(SDL_Renderer* renderer, double deltaTime){
// Colours background black
SDL_SetRenderDrawColor(renderer, 50, 50, 50, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
// Looping through all elements in the grid array and drawing each element
for (int x = 0; x < GRID_WIDTH; x++){
for (int y = 0; y < GRID_HEIGHT; y++){
if (grid.isFull(x, y)){
grid.getPtr(x, y)->render(renderer, GRID_SIZE, heatmapEnabled);
}
}
}
// Updates screen
SDL_RenderPresent(renderer);
}
void update(double deltaTime){
// grid.copy(); // Ensures elements can only be updated once
// Looping through all elements in the grid array and updating each element
int x, y;
for (y=0; y<GRID_HEIGHT; ++y)
for (x=0; x<GRID_WIDTH; ++x) {
if (grid.isEmpty(x, y)) {continue;}
grid.getPtr(x, y)->updated = false;
}
Element *elem;
bool rand_choice = rand()%2;
int x_iter;
if (rand_choice) {
x = 0;
x_iter = 1;
} else {
x = GRID_WIDTH-1;
x_iter = -1;
}
for (; x >= 0 && x < GRID_WIDTH; x += x_iter) {
for (y = GRID_HEIGHT-1; y >= 0; y--) { // From bottom to top
if (grid.isEmpty(x, y)) {continue;}
elem = grid.getPtr(x, y);
if (elem->updated) {
continue;
}
elem->update(deltaTime);
elem->updated = true;
}
}
// delete elems with health <= 0
for (x=0; x < GRID_WIDTH; ++x)
for (y=0; y < GRID_HEIGHT; ++y) {
if (grid.isEmpty(x, y)) {continue;}
if (grid.getPtr(x, y)->health > 0) {continue;}
grid.reset(x, y);
}
// create smoke, yes this is in the wrong place
// but smoke isnt defined yet in the other place
// because we put our stuff in .h files soooo
// produce smoke
for (x=0; x < GRID_WIDTH; ++x) {
for (y = GRID_HEIGHT-1; y >= 0; y--) { // From bottom to top
if (grid.isEmpty(x, y)) {continue;}
elem = grid.getPtr(x, y);
if (elem->tag == "smoke") {continue;}
if (!elem->canBeSetOnFire()) {continue;}
if (!elem->onFire) {continue;}
if ((float)rand()/RAND_MAX <= elem->fireSmokiness) {
int dx = rand()%2 * 2 - 1; // either -1 or +1
int dy = rand()%2 * 2 - 1; // either -1 or +1
if (!grid.inBounds(x+dx, y+dy)) {continue;}
if (grid.isEmpty(x+dx, y+dy)) {
grid.set(x+dx, y+dy, new Smoke(x+dx, y+dy));
}
}
}
}
}
int main(int argc, char* args[]){
// Initial message & SDL version
SDL_version version;
SDL_GetVersion(&version);
cout << "Initializing Grain (SDL version: " << (int) version.major << "." <<
(int) version.minor << "." << (int) version.patch << ")" << endl;
// Set up SDL
if (SDL_Init(SDL_INIT_VIDEO||SDL_INIT_EVENTS) < 0){
cout << "SDL could not initialize: " << SDL_GetError() << endl;
return 1;
}
SDL_Window *window = SDL_CreateWindow("Grain", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
if (window == NULL){
cout << "Could not create window: " << SDL_GetError() << endl;
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL){
cout << "Could not create renderer: " << SDL_GetError() << endl;
return 1;
}
// Fill grid with null pointers
grid.resetPtr();
// Set up Main loop
double deltaTime = 0;
bool running = true;
SDL_Event event;
// Main loop
while (running){
auto start = chrono::system_clock::now();
update(deltaTime);
draw(renderer, deltaTime);
// Handle events
while (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT){
running = false;
break;
}
else if (event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym){
case SDLK_1:
selectedElement = 0;
break;
case SDLK_2:
selectedElement = 1;
break;
case SDLK_3:
selectedElement = 2;
break;
case SDLK_4:
selectedElement = 3;
break;
case SDLK_5:
selectedElement = 4;
break;
case SDLK_6:
selectedElement = 5;
break;
case SDLK_7:
selectedElement = 6;
break;
case SDLK_8:
selectedElement = 7;
break;
case SDLK_9:
selectedElement = 8;
break;
case SDLK_0:
selectedElement = 9;
break;
case SDLK_h:
heatmapEnabled = !heatmapEnabled;
break;
case SDLK_UP:
if (placeSize < MAX_PLACE_SIZE){placeSize++;}
break;
case SDLK_DOWN:
if (placeSize > 0){placeSize--;}
break;
case SDLK_r:
grid.reset();
break;
case SDLK_p:
SDL_GetMouseState(&xMouse, &yMouse);
place_element(p_to_grid(xMouse), p_to_grid(yMouse));
break;
case SDLK_f:
SDL_GetMouseState(&xMouse, &yMouse);
igniteInRange(p_to_grid(xMouse), p_to_grid(yMouse));
break;
}
}
else if (event.type == SDL_MOUSEWHEEL){
if (event.wheel.y > 0){ // Scroll up
if (placeSize < MAX_PLACE_SIZE){
placeSize++;
}
} else if (event.wheel.y < 0){ // Scroll down
if (placeSize > 0){
placeSize--;
}
}
}
}
// Place element if left click held down, as well as update mouse position
if (SDL_GetMouseState(&xMouse, &yMouse) & SDL_BUTTON_LEFT){
// Places the selected element at the mouse position
placeBetween(p_to_grid(oldXMouse), p_to_grid(oldYMouse), p_to_grid(xMouse), p_to_grid(yMouse));
}
// If right click held down, delete element at position
else if (SDL_GetMouseState(&xMouse, &yMouse) & SDL_BUTTON_X1){
remove_element();
}
oldXMouse = xMouse;
oldYMouse = yMouse;
auto end = chrono::system_clock::now();
chrono::duration<double> dt = end - start;
deltaTime = dt.count();
if (deltaTime < 1.0/FPS){
SDL_Delay(1000 * (1.0/FPS - deltaTime)); // Max framerate is FPS, ensures elements fall slow enough
}
//cout << "FPS: " << (1.0 / deltaTime) << "\n";
}
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}