-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
204 lines (182 loc) · 4.16 KB
/
main.c
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
#include <stdio.h>
#include <stdint.h>
#include <SDL2/SDL.h>
#include "world.h"
#include "render.h"
int main()
{
World ourWorld;
InitializeWorld(&ourWorld, 64, 64);
GameRenderer ren;
InitGameRenderer(&ren, &ourWorld, 16, "Conway's Game of Life", 640, 480);
//Mainloop here.
bool quit = false;
bool paused = true;
unsigned int generation = 0;
SDL_Event event;
SetCell(&ourWorld, 3, 2, true);
SetCell(&ourWorld, 4, 2, true);
SetCell(&ourWorld, 3, 3, true);
SetCell(&ourWorld, 3, 4, true);
//Glider
SetCell(&ourWorld, 9, 4, true);
SetCell(&ourWorld, 10, 5, true);
SetCell(&ourWorld, 10, 6, true);
SetCell(&ourWorld, 9, 6, true);
SetCell(&ourWorld, 8, 6, true);
FlipBuffers(&ourWorld);
bool dragMode = false;
bool displayNew = false;
int mousex = 0;
int mousey = 0;
uint32_t lastUpdate = 0;
uint32_t tickDelay = 80;
ResizeWorld(&ourWorld, 128, 128, 32, 32);
bool ctrlHeld = false;
int resizeAmount = 1;
while(!quit)
{
//World logic
if(!paused)
{
uint32_t currentTime = SDL_GetTicks();
if((currentTime -lastUpdate) >= tickDelay)
{
Update(&ourWorld);
++generation;
lastUpdate = currentTime;
}
}
//Input
while( SDL_PollEvent(&event) != 0)
{
//Closed window or somesuch
if(event.type == SDL_QUIT)
{
quit = true;
}
else if( event.type == SDL_MOUSEWHEEL )
{
if(event.wheel.y > 0)
{
ZoomIn(&ren);
}
else if(event.wheel.y < 0)
{
ZoomOut(&ren);
}
}
else if( event.type == SDL_MOUSEMOTION )
{
if(dragMode == true)
{
MoveCamera(&ren, event.motion.xrel, event.motion.yrel);
}
mousex = event.motion.x;
mousey = event.motion.y;
}
else if( event.type == SDL_MOUSEBUTTONDOWN )
{
if(event.button.button == SDL_BUTTON_MIDDLE)
{
dragMode = true;
}
}
else if( event.type == SDL_MOUSEBUTTONUP)
{
if(event.button.button == SDL_BUTTON_MIDDLE)
{
dragMode = false;
}
}
else if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
quit=true;
break;
case SDLK_SPACE:
paused = !paused;
break;
case SDLK_n:
Update(&ourWorld);
++generation;
break;
case SDLK_LCTRL:
ctrlHeld = true;
resizeAmount = -1;
break;
//World resizing
case SDLK_RIGHT:
ResizeWorld(&ourWorld, ourWorld.width+resizeAmount, ourWorld.height, 0, 0);
break;
case SDLK_DOWN:
ResizeWorld(&ourWorld, ourWorld.width, ourWorld.height+resizeAmount, 0, 0);
break;
case SDLK_LEFT:
ResizeWorld(&ourWorld, ourWorld.width+resizeAmount, ourWorld.height, resizeAmount, 0);
break;
case SDLK_UP:
ResizeWorld(&ourWorld, ourWorld.width, ourWorld.height+resizeAmount, 0, resizeAmount);
break;
//Slow down
case SDLK_PAGEDOWN:
if(tickDelay < 1000)
{
tickDelay += 20;
}
break;
//Speed up
case SDLK_PAGEUP:
if(tickDelay > 10)
{
tickDelay -= 10;
}
break;
default:
break;
}
}
else if( event.type == SDL_KEYUP )
{
switch( event.key.keysym.sym )
{
case SDLK_LCTRL:
ctrlHeld = false;
resizeAmount = 1;
break;
default:
break;
}
}
}
if (SDL_GetMouseState(NULL, NULL)
& SDL_BUTTON(SDL_BUTTON_LEFT))
{
WorldCoord wc =
ScreenToWorldCoord(&ren, mousex, mousey);
SetCell(&ourWorld, wc.x, wc.y, true);
displayNew = true;
}
else if (SDL_GetMouseState(NULL, NULL)
& SDL_BUTTON(SDL_BUTTON_RIGHT))
{
WorldCoord wc =
ScreenToWorldCoord(&ren, mousex, mousey);
SetCell(&ourWorld, wc.x, wc.y, false);
displayNew = true;
}
//Flip the buffers so you can see any cells you've clicked in
if(displayNew)
{
FlipBuffers(&ourWorld);
displayNew = false;
}
//Rendering happens here:
DrawGame(&ren, &ourWorld);
}
DestroyWorld(&ourWorld);
CleanupRendering(&ren);
return 0;
}