-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
224 lines (183 loc) · 5.2 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include "chip8.h"
#include <GL/glut.h>
#define SCREEN_WIDTH 64
#define SCREEN_HEIGHT 32
#define MODIFIER 10
struct Chip8 chip8;
// window size
int display_width = SCREEN_WIDTH * MODIFIER;
int display_height = SCREEN_HEIGHT * MODIFIER;
void display();
void reshape_window(GLsizei w, GLsizei h);
void keyboardUp(unsigned char key, int x, int y);
void keyboardDown(unsigned char key, int x, int y);
#define DRAWWITHTEXTURE
typedef uint8_t u8;
u8 screenData[SCREEN_HEIGHT][SCREEN_WIDTH][3];
void setupTexture();
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("need 2 arguments\n");
return 1;
}
// load game
if (!load(argv[1],&chip8)) {
return 1;
}
// setup opengl
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(display_width, display_height);
glutInitWindowPosition(320, 320);
glutCreateWindow("chip8");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape_window);
glutKeyboardFunc(keyboardDown);
glutKeyboardUpFunc(keyboardUp);
#ifdef DRAWWITHTEXTURE
setupTexture();
#endif
glutMainLoop();
return 0;
}
void setupTexture() {
// clear screen
for (int y = 0; y < SCREEN_HEIGHT; ++y)
{
for (int x = 0; x < SCREEN_WIDTH; ++x)
{
screenData[y][x][0] = 0;
screenData[y][x][1] = 0;
screenData[y][x][2] = 0;
}
}
// create a texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)screenData);
// set up the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// enable textures
glEnable(GL_TEXTURE_2D);
}
void updateTexture(struct Chip8* c8) {
// update pixels
for(int y = 0; y < 32; ++y)
for(int x = 0; x < 64; ++x)
if(c8->graphic[(y * 64) + x] == 0) {
screenData[y][x][0] = 0;
screenData[y][x][1] = 0;
screenData[y][x][2] = 0;
}
else {
screenData[y][x][0] = 255;
screenData[y][x][1] = 255;
screenData[y][x][2] = 255;
}
// Update Texture
glTexSubImage2D(GL_TEXTURE_2D, 0 ,0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)screenData);
glBegin( GL_QUADS );
glTexCoord2d(0.0, 0.0); glVertex2d(0.0, 0.0);
glTexCoord2d(1.0, 0.0); glVertex2d(display_width, 0.0);
glTexCoord2d(1.0, 1.0); glVertex2d(display_width, display_height);
glTexCoord2d(0.0, 1.0); glVertex2d(0.0, display_height);
glEnd();
}
// Old gfx code
void drawPixel(int x, int y)
{
glBegin(GL_QUADS);
glVertex3f((x * MODIFIER) + 0.0f, (y * MODIFIER) + 0.0f, 0.0f);
glVertex3f((x * MODIFIER) + 0.0f, (y * MODIFIER) + MODIFIER, 0.0f);
glVertex3f((x * MODIFIER) + MODIFIER, (y * MODIFIER) + MODIFIER, 0.0f);
glVertex3f((x * MODIFIER) + MODIFIER, (y * MODIFIER) + 0.0f, 0.0f);
glEnd();
}
void updateQuads(struct Chip8* c8)
{
// Draw
for(int y = 0; y < 32; ++y)
for(int x = 0; x < 64; ++x)
{
if(c8->graphic[(y*64) + x] == 0)
glColor3f(0.0f,0.0f,0.0f);
else
glColor3f(1.0f,1.0f,1.0f);
drawPixel(x, y);
}
}
void display()
{
cycle(&chip8);
if(chip8.updateScreen)
{
// Clear framebuffer
glClear(GL_COLOR_BUFFER_BIT);
#ifdef DRAWWITHTEXTURE
updateTexture(&chip8);
#else
updateQuads(&chip8);
#endif
// Swap buffers!
glutSwapBuffers();
// Processed frame
chip8.updateScreen = false;
}
}
void reshape_window(GLsizei w, GLsizei h)
{
glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
// Resize quad
display_width = w;
display_height = h;
}
void keyboardDown(unsigned char key, int x, int y)
{
if(key == 27) // esc
exit(0);
if(key == '1') chip8.key[0x1] = 1;
else if(key == '2') chip8.key[0x2] = 1;
else if(key == '3') chip8.key[0x3] = 1;
else if(key == '4') chip8.key[0xC] = 1;
else if(key == 'q') chip8.key[0x4] = 1;
else if(key == 'w') chip8.key[0x5] = 1;
else if(key == 'e') chip8.key[0x6] = 1;
else if(key == 'r') chip8.key[0xD] = 1;
else if(key == 'a') chip8.key[0x7] = 1;
else if(key == 's') chip8.key[0x8] = 1;
else if(key == 'd') chip8.key[0x9] = 1;
else if(key == 'f') chip8.key[0xE] = 1;
else if(key == 'z') chip8.key[0xA] = 1;
else if(key == 'x') chip8.key[0x0] = 1;
else if(key == 'c') chip8.key[0xB] = 1;
else if(key == 'v') chip8.key[0xF] = 1;
//printf("Press key %c\n", key);
}
void keyboardUp(unsigned char key, int x, int y)
{
if(key == '1') chip8.key[0x1] = 0;
else if(key == '2') chip8.key[0x2] = 0;
else if(key == '3') chip8.key[0x3] = 0;
else if(key == '4') chip8.key[0xC] = 0;
else if(key == 'q') chip8.key[0x4] = 0;
else if(key == 'w') chip8.key[0x5] = 0;
else if(key == 'e') chip8.key[0x6] = 0;
else if(key == 'r') chip8.key[0xD] = 0;
else if(key == 'a') chip8.key[0x7] = 0;
else if(key == 's') chip8.key[0x8] = 0;
else if(key == 'd') chip8.key[0x9] = 0;
else if(key == 'f') chip8.key[0xE] = 0;
else if(key == 'z') chip8.key[0xA] = 0;
else if(key == 'x') chip8.key[0x0] = 0;
else if(key == 'c') chip8.key[0xB] = 0;
else if(key == 'v') chip8.key[0xF] = 0;
}