-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
395 lines (319 loc) · 8.33 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#define SDL_MAIN_HANDLED 1
#define BOARD_SIZE 1500
#define NUM_IMAGES 9
#define IMAGE_SIZE 4
#define TIME_BTWN_FRAMES 16
#define COLOR_LIMIT 150
#define RENDER_OFFSET_X IMAGE_SIZE - (BOARD_SIZE * IMAGE_SIZE / 2) + (SCREEN_WIDTH / 2)
#define RENDER_OFFSET_Y IMAGE_SIZE - (BOARD_SIZE * IMAGE_SIZE / 2) + (SCREEN_HEIGHT / 2)
//#define LIMIT_FRAMERATE true
//#define CELL_LIMIT 30000
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 1024
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <sstream>
#include <math.h>
#include <numeric>
#include <random>
#include <vector>
#include "SDL.h"
#include "SDL_ttf.h"
#include <SDL_image.h>
#include <chrono>
using namespace std;
SDL_Surface** texStorage;
class Cell
{
public:
Cell(SDL_Renderer* render)
{
texture = SDL_CreateTextureFromSurface(render, texStorage[rand() % NUM_IMAGES]);
SDL_SetTextureColorMod(texture, rand() % COLOR_LIMIT, rand() % COLOR_LIMIT, rand() % COLOR_LIMIT);
construct(render);
}
Cell(SDL_Renderer* render, SDL_Texture* text)
{
texture = text;
construct(render);
}
~Cell()
{
//SDL_DestroyTexture(texture);
}
void construct(SDL_Renderer* render)
{
ren = render;
rect.x = rect.y = 0;
rect.w = rect.h = IMAGE_SIZE;
// 15 10
timeToDeath = (rand() % 6) + 7;
resetRepro();
}
void resetRepro()
{
// 10 10
// 15 7
timeToRepro = (rand() % 7) + 5;
}
void setPos(int x, int y)
{
rect.x = x;
rect.y = y;
}
/*
int* getPos()
{
int Pos[2] = {rect.x, rect.y};
return Pos;
}
*/
void setTexture(SDL_Texture* text)
{
SDL_DestroyTexture(texture);
texture = text;
}
SDL_Texture* getTexture()
{
return texture;
}
bool isDead()
{
return timeToDeath <= 0;
}
void timeStep()
{
timeToDeath--;
timeToRepro--;
}
void kill(bool** board)
{
// I found out why there was a diagonal...
// board[rect.x][rect.y] = false;
board[rect.y][rect.x] = false;
}
void reproduce(vector<Cell*>* cells, bool** board)
{
/*
0 = up
1 = right
2 = down
3 = left
*/
// If not time to repro, then skip this
if (timeToRepro > 0) return;
# ifdef CELL_LIMIT
if (cells->size() > CELL_LIMIT)
{
resetRepro();
return;
}
# endif
for (int i = 0; i < 4; i++)
{
if ((rand() % 2) == 1)
{
int newX = rect.x, newY = rect.y;
switch(i)
{
case 0:
newY -= 1;
break;
case 1:
newX += 1;
break;
case 2:
newY += 1;
break;
case 3:
newX -= 1;
break;
}
SDL_Rect tempRect;
tempRect.x = newX * RENDER_OFFSET_X;
tempRect.y = newY * RENDER_OFFSET_Y;
if ( tempRect.x > 0 && tempRect.x < SCREEN_WIDTH &&
tempRect.y > 0 && tempRect.y < SCREEN_HEIGHT &&
!board[newY][newX])
{
// Successful reproduction!
Cell* newCell;
// Set texture to parent's if it failed to speciate
if ( (rand() % 1000) > 2 )
newCell = new Cell(ren, texture);
else
newCell = new Cell(ren);
board[newY][newX] = true;
newCell->setPos(newX, newY);
cells->push_back(newCell);
}
}
}
resetRepro();
}
void render()
{
SDL_Rect tempRect;
tempRect.x = rect.x * RENDER_OFFSET_X;
tempRect.y = rect.y * RENDER_OFFSET_Y;
tempRect.w = tempRect.h = IMAGE_SIZE;
// if ( tempRect.x > 0 && tempRect.x < SCREEN_WIDTH &&
// tempRect.y > 0 && tempRect.y < SCREEN_HEIGHT )
SDL_RenderCopy(ren, texture, NULL, &tempRect);
}
private:
SDL_Rect rect;
SDL_Texture* texture;
SDL_Renderer* ren;
int timeToDeath;
int timeToRepro;
};
int main (int len, char** args)
{
srand((unsigned) time(NULL));
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window* win = SDL_CreateWindow("CDoodle", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (win == nullptr)
{
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
#ifdef LIMIT_FRAMERATE
int flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
#else
int flags = SDL_RENDERER_ACCELERATED;
#endif
SDL_Renderer* ren = SDL_CreateRenderer(win, -1, flags);
if (ren == nullptr)
{
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
return 1;
}
//Initialize PNG loading
if( (IMG_Init( IMG_INIT_PNG ) & IMG_INIT_PNG) != IMG_INIT_PNG )
{
std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
return 1;
}
// initialize TTF fonts
if (TTF_Init() == -1)
{
std::cout << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << std::endl;
return 1;
}
TTF_Font* glacialFont = TTF_OpenFont("./TTF/GlacialIndifference-Regular.ttf", 14);
if (glacialFont == NULL)
{
std::cout << "SDL_ttf could not initialize GlacialIndifference-Regular.ttf! SDL_ttf Error: " << TTF_GetError() << std::endl;
return 1;
}
// Load textures for cells
SDL_Surface* texs[9];
for (int j = 1; j <= NUM_IMAGES; j++)
{
ostringstream str;
str << "./images/cell" << j << ".png";
texs[j-1] = IMG_Load(str.str().c_str() );
// int ran = rand() % 255;
// SDL_SetTextureColorMod(texs[j-1], rand() % 255, rand() % 255, rand() % 255);
// SDL_SetTextureAlphaMod(texs[j-1], 255);
// SDL_SetTextureBlendMode(texs[j-1], SDL_BLENDMODE_BLEND);
}
// Save pointer to textures in global
texStorage = texs;
// Initialize variables for board and cells
SDL_Event event;
bool** board;
board = (bool**)calloc(sizeof(bool), BOARD_SIZE);
for (int i = 0; i < BOARD_SIZE; i++)
{
//cout << i << endl;
board[i] = (bool*)calloc(sizeof(bool), BOARD_SIZE);
}
vector<Cell*> cells;
// Make the first cell
const int firstCellLoc = BOARD_SIZE/2;
board[firstCellLoc][firstCellLoc] = true;
Cell* firstCell = new Cell(ren);
firstCell->setPos(firstCellLoc, firstCellLoc);
cells.push_back(firstCell);
SDL_Texture* firstColTex = firstCell->getTexture();
Uint8 r,g,b;
SDL_GetTextureColorMod(firstColTex, &r, &g, &b);
int flasher = 0;
int flasherDown = 1;
chrono::high_resolution_clock::time_point frameStart;
chrono::microseconds timeOnFrame (0);
const SDL_Color white = {255,255,255,255};
const SDL_Color transparent = {0,0,0,0};
SDL_Rect tempRect = {0, 0, 50, 20};
string fpsString("FPS: ");
int tickCounter = 0;
SDL_Surface* fpsTextSurf = TTF_RenderText_Solid(glacialFont, "0", white);
SDL_Texture* fpsTextText = SDL_CreateTextureFromSurface(ren, fpsTextSurf);
int fpsCounter[5] = {0};
int flasherSpeed = (1000 / TIME_BTWN_FRAMES) / 11;
while (true)
{
frameStart = chrono::high_resolution_clock::now();
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
SDL_Quit();
return 1;
}
}
//First clear the renderer
SDL_RenderClear(ren);
// Handle first colony flashing
flasher += flasherSpeed * flasherDown;
if (flasher >= 255 - flasherSpeed)
flasherDown = -1;
else if (flasher <= flasherSpeed)
flasherDown = 1;
SDL_SetTextureColorMod(firstColTex, (int)fmax(r, flasher), (int)fmax(g, flasher), (int)fmax(b, flasher));
// Take a time step!
for (unsigned i = 0; i < cells.size(); i++)
{
cells[i]->timeStep();
// Be a little more forgiving in the beginning
if (cells[i]->isDead() && cells.size() > 3)
{
cells[i]->kill(board);
delete cells[i];
cells.erase(cells.begin() + i--);
continue;
}
cells[i]->reproduce(&cells, board);
cells[i]->render();
}
// Write FPS of previous frametime to screen
if (tickCounter == 100)
{
SDL_FreeSurface(fpsTextSurf);
SDL_DestroyTexture(fpsTextText);
int accum = 1000000 / (accumulate(fpsCounter,fpsCounter + 5,0) / 5.0);
string fpsConcat = fpsString + to_string(accum);
fpsTextSurf = TTF_RenderText_LCD(glacialFont, fpsConcat.c_str(), white, transparent);
fpsTextText = SDL_CreateTextureFromSurface(ren, fpsTextSurf);
tickCounter = 0;
}
SDL_RenderCopy(ren, fpsTextText, NULL, &tempRect);
//Update the screen
SDL_RenderPresent(ren);
timeOnFrame = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - frameStart);
fpsCounter[tickCounter%5] = timeOnFrame.count();
tickCounter++;
//std::cout << "Time spent on frame: " << timeOnFrame.count() << endl;
//SDL_Delay(TIME_BTWN_FRAMES - (int)timeOnFrame.count());
}
// system("pause");
return 0;
}