-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphics.c
More file actions
183 lines (152 loc) · 6.2 KB
/
graphics.c
File metadata and controls
183 lines (152 loc) · 6.2 KB
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
/******************************************************************************
File: graphics.c
Created: 2019-06-25
Updated: 2019-07-31
Author: Aaron Oman
Notice: Creative Commons Attribution 4.0 International License (CC-BY 4.0)
******************************************************************************/
//! \file graphics.c
#include <string.h> // memset
#include <stdio.h> // fprintf
#include "GL/glew.h"
#include "SDL2/SDL.h"
#include "SDL2/SDL_opengl.h"
#include "system.h"
const unsigned int DISPLAY_WIDTH_WITH_DEBUGGER = 1445;
const unsigned int DISPLAY_HEIGHT_WITH_DEBUGGER = 720;
const unsigned int CHIP8_DISPLAY_WIDTH = 64;
const unsigned int CHIP8_DISPLAY_HEIGHT = 32;
const unsigned int DISPLAY_SCALE = 16;
//! \brief Graphics state
struct graphics {
GLubyte *textureData;
SDL_Window *sdlWindow;
SDL_GLContext *glContext;
unsigned int displayWidth;
unsigned int displayHeight;
int debug;
int glWindowWidth;
int glWindowHeight;
GLuint glTextureName;
};
struct graphics *GraphicsInit(int debug) {
struct graphics *g = (struct graphics *)malloc(sizeof(struct graphics));
memset(g, 0, sizeof(struct graphics));
g->debug = debug;
g->textureData = (GLubyte *)malloc(CHIP8_DISPLAY_WIDTH * CHIP8_DISPLAY_HEIGHT * 3);
if (debug) {
g->displayWidth = DISPLAY_WIDTH_WITH_DEBUGGER;
g->displayHeight = DISPLAY_HEIGHT_WITH_DEBUGGER;
} else {
g->displayWidth = CHIP8_DISPLAY_WIDTH * DISPLAY_SCALE;
g->displayHeight = CHIP8_DISPLAY_HEIGHT * DISPLAY_SCALE;
}
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);
g->sdlWindow = SDL_CreateWindow(
"AaronO's CHIP-8 Emulator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
g->displayWidth, g->displayHeight,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
);
if (g->sdlWindow == NULL) {
fprintf(stderr, "Couldn't open window: %s\n", SDL_GetError());
return NULL;
}
g->glContext = SDL_GL_CreateContext(g->sdlWindow);
if (NULL == g->glContext) {
fprintf(stderr, "%s\n", SDL_GetError());
return NULL;
}
SDL_GetWindowSize(g->sdlWindow, &g->glWindowWidth, &g->glWindowHeight);
glViewport(0, 0, g->glWindowWidth, g->glWindowHeight);
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to setup GLEW\n");
SDL_DestroyWindow(g->sdlWindow);
SDL_Quit();
return NULL;
}
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &g->glTextureName);
glBindTexture(GL_TEXTURE_2D, g->glTextureName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, CHIP8_DISPLAY_WIDTH, CHIP8_DISPLAY_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)g->textureData);
return g;
}
void GraphicsDeinit(struct graphics *g) {
SDL_GL_DeleteContext(g->glContext);
SDL_DestroyWindow(g->sdlWindow);
SDL_Quit();
free(g->textureData);
free(g);
}
//! \brief Graphics rasterization to screen
//!
//! The CHIP-8 uses bitmapped graphics in its internal video memory.
//! This bitmapped memory needs to be interpreted appropriately so the graphics
//! system can render it to the screen.
//!
//! \param[in,out] graphics Graphics state to be updated
//! \param[in] system CHIP-8 system state to be read
void Raster(struct graphics *graphics, struct system *system) {
if (0 != SystemGfxLock(system)) {
fprintf(stderr, "Failed to lock system gfx rwlock");
return;
}
memset(graphics->textureData, 0xFF, CHIP8_DISPLAY_WIDTH * CHIP8_DISPLAY_HEIGHT * 3);
for (int y = CHIP8_DISPLAY_HEIGHT-1, cy = 0; cy < CHIP8_DISPLAY_HEIGHT; cy++, y--) {
for (int x = 0, cx = 0; cx < CHIP8_DISPLAY_WIDTH; cx++, x+=3) {
unsigned int pos = cy * (CHIP8_DISPLAY_WIDTH * 3) + x;
if (system->gfx[y * CHIP8_DISPLAY_WIDTH + cx]) {
// Black (Foreground)
graphics->textureData[pos + 0] = 0;
graphics->textureData[pos + 1] = 0;
graphics->textureData[pos + 2] = 0;
}
}
}
SystemGfxUnlock(system);
}
void GraphicsPresent(struct graphics *g, struct system *s, void (*ui_render_fn)()) {
SDL_GetWindowSize(g->sdlWindow, &g->glWindowWidth, &g->glWindowHeight);
glViewport(0, 0, g->glWindowWidth, g->glWindowHeight);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.10f, 0.18f, 0.24f, 1.0f);
ui_render_fn();
glOrtho(-1, 1, -1, 1, -1, 1);
glColor3f(1, 1, 1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g->glTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Raster(g, s);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, CHIP8_DISPLAY_WIDTH, CHIP8_DISPLAY_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)g->textureData);
float top, bottom, left, right;
if (g->debug) {
top = 0.25;
bottom = -0.75;
left = 0;
right = 1;
} else {
top = 1;
bottom = -1;
left = -1;
right = 1;
}
glBegin(GL_TRIANGLES); {
glTexCoord2f(0, 0);
glVertex3f(left, bottom, 0.5);
glTexCoord2f(0, 1);
glVertex3f(left, top, 0.5);
glTexCoord2f(1, 0);
glVertex3f(right, bottom, 0.5);
glTexCoord2f(0, 1);
glVertex3f(left, top, 0.5);
glTexCoord2f(1, 1);
glVertex3f(right, top, 0.5);
glTexCoord2f(1, 0);
glVertex3f(right, bottom, 0.5);
} glEnd();
SDL_GL_SwapWindow(g->sdlWindow);
}
SDL_Window *GraphicsSDLWindow(struct graphics *g) {
return g->sdlWindow;
}