-
Notifications
You must be signed in to change notification settings - Fork 0
/
Display.cpp
239 lines (203 loc) · 5.27 KB
/
Display.cpp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "Display.h"
static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL;
static uint32_t* color_buffer = NULL;
static float* z_buffer = NULL;
static SDL_Texture* color_buffer_texture = NULL;
static int window_width = 320;
static int window_height = 200;
static int render_method = 0;
static int cull_method = 0;
int get_window_width(void)
{
return window_width;
}
int get_window_height(void)
{
return window_height;
}
bool initialize_window(void)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
fprintf(stderr, "Error initializing SDL.\n");
return false;
}
// Set width and height of the SDL window with the max screen resolution
SDL_DisplayMode display_mode;
SDL_GetCurrentDisplayMode(0, &display_mode);
//window_width = display_mode.w;
//window_height = display_mode.h;
int fullscreen_width = display_mode.w;
int fullscreen_height = display_mode.h;
window_width = fullscreen_width / 3;
window_height = fullscreen_height / 3;
// Create a SDL Window
window = SDL_CreateWindow(
NULL,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
fullscreen_width,
fullscreen_height,
SDL_WINDOW_BORDERLESS
);
if (!window)
{
fprintf(stderr, "Error creating SDL window.\n");
return false;
}
// Create a SDL renderer
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer)
{
fprintf(stderr, "Error creating SDL renderer.\n");
return false;
}
// Allocate the required memory in bytes to hold the color buffer and the z-buffer
color_buffer = (uint32_t*)malloc(sizeof(uint32_t) * window_width * window_height);
z_buffer = (float*)malloc(sizeof(float) * (window_width + 1) * window_height);
// Creating a SDL texture that is used to display the color buffer
color_buffer_texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_STREAMING,
window_width,
window_height
);
return true;
}
void set_render_method(int method)
{
render_method = method;
}
void set_cull_method(int method)
{
cull_method = method;
}
bool should_cull_backface(void)
{
return cull_method == CULL_BACKFACE;
}
bool should_render_textured_triangles(void)
{
return (
render_method == RENDER_TEXTURED ||
render_method == RENDER_TEXTURED_WIRE
);
}
bool should_render_wireframe(void)
{
return (
render_method == RENDER_WIRE ||
render_method == RENDER_WIRE_VERTEX ||
render_method == RENDER_FILL_TRIANGLE_WIRE ||
render_method == RENDER_TEXTURED_WIRE
);
}
bool should_render_filled_triangles(void)
{
return (
render_method == RENDER_FILL_TRIANGLE ||
render_method == RENDER_FILL_TRIANGLE_WIRE
);
}
bool should_render_wire_vertex(void)
{
return (
render_method == RENDER_WIRE_VERTEX
);
}
void draw_grid(void)
{
for (int y = 0; y < window_height; y += 10)
{
for (int x = 0; x < window_width; x += 10)
{
color_buffer[(window_width * y) + x] = 0xFF444444;
}
}
}
void draw_pixel(int x, int y, uint32_t color)
{
if (x < 0 || x >= window_width || y < 0 || y >= window_height)
{
return;
}
color_buffer[(window_width * y) + x] = color;
}
void draw_line(int x0, int y0, int x1, int y1, uint32_t color)
{
int delta_x = (x1 - x0);
int delta_y = (y1 - y0);
int longest_side_length = (abs(delta_x) >= abs(delta_y)) ? abs(delta_x) : abs(delta_y);
float x_inc = (float)delta_x / (float)longest_side_length;
float y_inc = (float)delta_y / (float)longest_side_length;
float current_x = x0;
float current_y = y0;
for (int i = 0; i <= longest_side_length; i++)
{
draw_pixel(round(current_x), round(current_y), color);
current_x += x_inc;
current_y += y_inc;
}
}
void draw_rect(int x, int y, int width, int height, uint32_t color)
{
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
int current_x = x + i;
int current_y = y + j;
draw_pixel(current_x, current_y, color);
}
}
}
void render_color_buffer(void)
{
SDL_UpdateTexture(
color_buffer_texture,
NULL,
color_buffer,
(int)(window_width * sizeof(uint32_t))
);
SDL_RenderCopy(renderer, color_buffer_texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
void clear_color_buffer(uint32_t color)
{
for (int i = 0; i < window_width * window_height; i++)
{
color_buffer[i] = color;
}
}
void clear_z_buffer(void) {
for (int i = 0; i < (window_width + 1) * window_height; i++)
{
z_buffer[i] = 1.0;
}
}
float get_zbuffer_at(int x, int y)
{
if (x < 0 || x >= window_width || y < 0 || y >= window_height)
{
return 1.0;
}
return z_buffer[(window_width * y) + x];
}
void update_zbuffer_at(int x, int y, float value)
{
if (x < 0 || x >= window_width || y < 0 || y >= window_height)
{
return;
}
z_buffer[(window_width * y) + x] = value;
}
void destroy_window(void)
{
free(color_buffer);
free(z_buffer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}