-
Notifications
You must be signed in to change notification settings - Fork 57
/
play.c
181 lines (139 loc) · 3.88 KB
/
play.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
/**
* @file play.c
*
* @author David Matlack
*/
#include "chip8.h"
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include <sys/time.h>
#define PIXEL_SIZE 5
#define CLOCK_HZ 60
#define CLOCK_RATE_MS ((int) ((1.0 / CLOCK_HZ) * 1000 + 0.5))
#define BLACK 0
#define WHITE 255
#define SCREEN_ROWS (GFX_ROWS * PIXEL_SIZE)
#define SCREEN_COLS (GFX_COLS * PIXEL_SIZE)
unsigned char screen[SCREEN_ROWS][SCREEN_COLS][3];
extern uint8_t key[KEY_SIZE];
extern uint8_t gfx[GFX_ROWS][GFX_COLS];
extern bool chip8_draw_flag;
struct timeval clock_prev;
int timediff_ms(struct timeval *end, struct timeval *start) {
int diff = (end->tv_sec - start->tv_sec) * 1000 +
(end->tv_usec - start->tv_usec) / 1000;
//printf("timediff = %d\n", diff);
return diff;
}
void gfx_setup() {
memset(screen, BLACK, sizeof(unsigned char) * SCREEN_ROWS * SCREEN_COLS * 3);
glClear(GL_COLOR_BUFFER_BIT);
}
int keymap(unsigned char k) {
switch (k) {
case '1': return 0x1;
case '2': return 0x2;
case '3': return 0x3;
case '4': return 0xc;
case 'q': return 0x4;
case 'w': return 0x5;
case 'e': return 0x6;
case 'r': return 0xd;
case 'a': return 0x7;
case 's': return 0x8;
case 'd': return 0x9;
case 'f': return 0xe;
case 'z': return 0xa;
case 'x': return 0x0;
case 'c': return 0xb;
case 'v': return 0xf;
default: return -1;
}
}
void keypress(unsigned char k, int x, int y) {
(void) x; (void) y;
int index = keymap(k);
if (index >= 0) { key[index] = 1; }
}
void keyrelease(unsigned char k, int x, int y) {
(void) x; (void) y;
int index = keymap(k);
if (index >= 0) { key[index] = 0; }
}
void paint_pixel(int row, int col, unsigned char color) {
row = SCREEN_ROWS - 1 - row;
screen[row][col][0] = screen[row][col][1] = screen[row][col][2] = color;
}
void paint_cell(int row, int col, unsigned char color) {
int pixel_row = row * PIXEL_SIZE;
int pixel_col = col * PIXEL_SIZE;
int drow, dcol;
for (drow = 0; drow < PIXEL_SIZE; drow++) {
for (dcol = 0; dcol < PIXEL_SIZE; dcol++) {
paint_pixel(pixel_row + drow, pixel_col + dcol, color);
}
}
}
void draw() {
int row, col;
// Clear framebuffer
glClear(GL_COLOR_BUFFER_BIT);
// Draw pixels to the buffer
for (row = 0; row < GFX_ROWS; row++) {
for (col = 0; col < GFX_COLS; col++) {
paint_cell(row, col, gfx[row][col] ? WHITE : BLACK);
}
}
// Update Texture
glDrawPixels(SCREEN_COLS, SCREEN_ROWS, GL_RGB, GL_UNSIGNED_BYTE,
(void *) screen);
glutSwapBuffers();
}
void loop() {
struct timeval clock_now;
gettimeofday(&clock_now, NULL);
chip8_emulatecycle();
if (chip8_draw_flag) {
draw();
chip8_draw_flag = false;
}
if (timediff_ms(&clock_now, &clock_prev) >= CLOCK_RATE_MS) {
chip8_tick();
clock_prev = clock_now;
}
}
void reshape_window(GLsizei w, GLsizei h) {
(void) w; (void) h;
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: ./play <game>\n");
exit(2);
}
// Setup Chip8
chip8_initialize();
chip8_loadgame(argv[1]);
// Setup OpenGL
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(SCREEN_COLS, SCREEN_ROWS);
glutInitWindowPosition(0, 0);
glutCreateWindow("chip8");
glutDisplayFunc(draw);
glutIdleFunc(loop);
glutReshapeFunc(reshape_window);
glutKeyboardFunc(keypress);
glutKeyboardUpFunc(keyrelease);
gfx_setup();
gettimeofday(&clock_prev, NULL);
// Run the emulator
glutMainLoop();
return 0;
}