-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
188 lines (165 loc) · 3.63 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
// Julia Ting
// CS2110 HW11
// Conway's game of Life
#include "mylib.h"
#include <stdlib.h>
#include <stdio.h>
#include "mainScreen.h"
extern const unsigned short mainScreen[19200];
extern const unsigned short mainScreen_palette[256];
int simTick();
int cursorMoves();
void delay(int n);
int cursorMoves() {
int draw = 0;
if (KEY_DOWN_NOW(BUTTON_UP)) {
delay(1);
moveCursor(-1, 0);
draw = 1;
} else if (KEY_DOWN_NOW(BUTTON_DOWN)) {
delay(1);
moveCursor(1, 0);
draw = 1;
} else if (KEY_DOWN_NOW(BUTTON_RIGHT)) {
delay(1);
moveCursor(0, 1);
draw = 1;
} else if (KEY_DOWN_NOW(BUTTON_LEFT)) {
delay(1);
moveCursor(0,-1);
draw = 1;
} else if (KEY_DOWN_NOW(BUTTON_START)) {
delay(1);
toggleCursor();
draw = 1;
while (KEY_DOWN_NOW(BUTTON_START)) {
}
}
return draw;
}
int main() {
REG_DISPCTL = MODE4 | BG2_ENABLE;
// Splash screen here
setPalette(mainScreen_palette);
drawImage4(0, 0, 240, 160, mainScreen);
waitForVBlank();
flipPage();
int screen = 1;
int currentMode = EDITMODE;
const u16 colors[] = {WHITE, BLACK, RED, GREEN};
// alive = black = 1
// dead = white = 0
// alive + cursor = green = 3
// dead + cursor = red = 2
while (1) {
if (screen) {
setPalette(mainScreen_palette);
drawImage4(0, 0, 240, 160, mainScreen);
waitForVBlank();
flipPage();
if (KEY_DOWN_NOW(BUTTON_SELECT)) {
screen = 0;
setPalette(colors);
fillScreen4(DEAD);
flipPage();
copy();
while (KEY_DOWN_NOW(BUTTON_SELECT)) {
}
}
} else {
int draw = 0;
// Select (backspace) = toggle between edit and simmode
if (KEY_DOWN_NOW(BUTTON_SELECT)) {
if (currentMode == EDITMODE) {
currentMode = SIMMODE;
eraseCursor();
} else {
currentMode = EDITMODE;
}
while (KEY_DOWN_NOW(BUTTON_SELECT)) {
// Won't do anything while select button is down
}
}
// SIMMODE = 1
// EDITMODE = 0
// ***************SIM MODE*******************
if (currentMode) {
int paused = 0;
int sim = 1;
while (sim) {
while (~paused & currentMode) {
currentMode = simTick();
if (KEY_DOWN_NOW(BUTTON_SELECT)) {
currentMode = EDITMODE;
sim = 0;
while (KEY_DOWN_NOW(BUTTON_SELECT)) {
}
}
if (KEY_DOWN_NOW(BUTTON_A)) {
paused = 1;
while (KEY_DOWN_NOW(BUTTON_A)) {
}
}
}
//******************PAUSED****************
while (paused) {
// Button B is X
// This toggles next frame
if (KEY_DOWN_NOW(BUTTON_B)) {
simTick();
}
if (KEY_DOWN_NOW(BUTTON_SELECT)) {
currentMode = EDITMODE;
while (KEY_DOWN_NOW(BUTTON_SELECT)) {
}
sim = 0;
paused = 0;
}
if (KEY_DOWN_NOW(BUTTON_A)) {
paused = 0;
while (KEY_DOWN_NOW(BUTTON_A)) {
}
}
} // end of paused
} // end of sim
} // end of currentMode if
// ************EDIT MODE**************
if (~currentMode) {
copy();
drawCursor();
draw = cursorMoves();
if (KEY_DOWN_NOW(BUTTON_L)) {
fillScreen4(DEAD);
flipPage();
copy();
while (KEY_DOWN_NOW(BUTTON_L)) {
}
}
}
drawCursor();
if (draw) {
waitForVBlank();
flipPage();
draw = 0;
}
} // end of else
}
}
void delay(int n){
int i;
volatile int x=0;
for(i = 0; i<n*10000; i++){
x++;
}
}
int simTick() {
if (KEY_DOWN_NOW(BUTTON_SELECT)) {
while (KEY_DOWN_NOW(BUTTON_SELECT)) {
}
return 0;
}
drawScreen();
waitForVBlank();
flipPage();
return 1;
}