-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
196 lines (169 loc) · 5.38 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
// (c) 2018 Zachary Wells
// This code is licensed under MIT license (see LICENSE for details)
// This is an example main file for using the chip8 interpreter.
// It is not required if you wish to use the interpreter somewhere else.
// This is the only source file which has dependencies.
#include <stdio.h>
#include <GLFW/glfw3.h>
#include <AL/al.h>
#include <AL/alc.h>
#include "chip8.h"
static chip8_t emulator;
void load_rom(const char *fileName) {
FILE *file = fopen(fileName, "rb");
if (file != NULL) {
fseek(file, 0, SEEK_END);
unsigned long length = ftell(file);
rewind(file);
uint16_t rom[0xFFF];
fread(rom, 1, length, file);
chip8_load_rom(&emulator, rom, length);
fclose(file);
} else {
uint16_t no_rom[] = {
0x0012
};
chip8_load_rom(&emulator, no_rom, sizeof(no_rom));
}
}
void resize(GLFWwindow *window, int w, int h) {
glViewport(0, 0, w, h);
}
void key(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (action == GLFW_REPEAT) {
return;
}
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
//quit
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
switch (key) {
case GLFW_KEY_1: {
emulator.keypad.K1 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_2: {
emulator.keypad.K2 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_3: {
emulator.keypad.K3 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_4: {
emulator.keypad.KC = (action == GLFW_PRESS);
} return;
case GLFW_KEY_Q: {
emulator.keypad.K4 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_W: {
emulator.keypad.K5 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_E: {
emulator.keypad.K6 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_R: {
emulator.keypad.KD = (action == GLFW_PRESS);
} return;
case GLFW_KEY_A: {
emulator.keypad.K7 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_S: {
emulator.keypad.K8 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_D: {
emulator.keypad.K9 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_F: {
emulator.keypad.KE = (action == GLFW_PRESS);
} return;
case GLFW_KEY_Z: {
emulator.keypad.KA = (action == GLFW_PRESS);
} return;
case GLFW_KEY_X: {
emulator.keypad.K0 = (action == GLFW_PRESS);
} return;
case GLFW_KEY_C: {
emulator.keypad.KB = (action == GLFW_PRESS);
} return;
case GLFW_KEY_V: {
emulator.keypad.KF = (action == GLFW_PRESS);
} return;
}
}
void dragonDrop(GLFWwindow *window, int count, const char **paths) {
if (count >= 1) {
load_rom(paths[0]);
}
}
int main(int argc, char **argv) {
glfwInit();
GLFWwindow *window = glfwCreateWindow(CHIP8_WIDTH * 8, CHIP8_HEIGHT * 9, "chip8", NULL, NULL);
glfwSetWindowSizeCallback(window, resize);
glfwSetDropCallback(window, dragonDrop);
glfwSetKeyCallback(window, key);
glfwMakeContextCurrent(window);
glfwSwapInterval(GLFW_TRUE);
glEnable(GL_TEXTURE_2D);
float index[] = {0.0, 1.0};
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 2, index);
glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 2, index);
glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 2, index);
glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 2, index);
GLuint screen;
glGenTextures(1, &screen);
glBindTexture(GL_TEXTURE_2D, screen);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, CHIP8_WIDTH, CHIP8_HEIGHT, 0, GL_COLOR_INDEX, GL_BITMAP, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
ALCdevice *al_device = alcOpenDevice(NULL);
ALCcontext *al_context = alcCreateContext(al_device, NULL);
alcMakeContextCurrent(al_context);
ALuint audio_source;
alGenSources(1, &audio_source);
alSourcef(audio_source, AL_PITCH, 2);
alSourcef(audio_source, AL_GAIN, 0.2f);
alSource3f(audio_source, AL_POSITION, 0, 0, 0);
alSource3f(audio_source, AL_VELOCITY, 0, 0, 0);
alSourcei(audio_source, AL_LOOPING, AL_FALSE);
ALuint audio_buffer;
alGenBuffers(1, &audio_buffer);
alBufferData(audio_buffer, AL_FORMAT_MONO8, (uint8_t[]){
0, 0, 0, 255, 255, 0, 0, 0
}, 8, 1600);
alSourcei(audio_source, AL_BUFFER, audio_buffer);
if (argc >= 2) {
load_rom(argv[1]);
} else {
load_rom(NULL);
}
glfwSetTime(0.0);
double curTime, lastTime = glfwGetTime();
while (glfwWindowShouldClose(window) != GLFW_TRUE) {
glfwPollEvents();
curTime = glfwGetTime();
chip8_update(&emulator, curTime - lastTime);
lastTime = curTime;
if (emulator.sound) {
alSourcei(audio_source, AL_LOOPING, AL_TRUE);
alSourcePlay(audio_source);
} else {
alSourcei(audio_source, AL_LOOPING, AL_FALSE);
}
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, CHIP8_WIDTH, CHIP8_HEIGHT, GL_COLOR_INDEX, GL_BITMAP, emulator.vram);
//immediate mode because I can't be bothered to make buffers and shaders for something so dead simple ;)
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex2i(-1, -1);
glTexCoord2i(1, 1); glVertex2i(1, -1);
glTexCoord2i(1, 0); glVertex2i(1, 1);
glTexCoord2i(0, 0); glVertex2i(-1, 1);
glEnd();
glfwSwapBuffers(window);
}
alDeleteSources(1, &audio_source);
alDeleteBuffers(1, &audio_buffer);
alcMakeContextCurrent(NULL);
alcDestroyContext(al_context);
alcCloseDevice(al_device);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}