-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (112 loc) · 4.33 KB
/
main.cpp
File metadata and controls
143 lines (112 loc) · 4.33 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#include <SDL2/SDL.h>
#define MATH_TAU 6.28318530717958647692f
#define TURNS(x) (MATH_TAU*(x))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define UNUSED(x) ((void)(x))
#define ERROR_EXIT(cond, msg, ...) \
do { \
if ((cond)) { \
fprintf(stderr, (msg), __VA_ARGS__); \
exit(1); \
} \
} while(0)
#define internal static
#define global static
#define FPS 60
#define MS_PER_FRAME (1000/FPS)
#define WIDTH 800
#define HEIGHT 600
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#define ARRAY_AT(arr, x, y) ((arr)[(y)*WIDTH + (x)])
global uint32_t frame_buffer[WIDTH*HEIGHT] = {0};
struct Render_Context {
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Texture *buffer_texture;
};
internal void render(float dt);
internal Render_Context create_render_context(unsigned int width, unsigned int height, const char *title)
{
Render_Context render_context = {0};
ERROR_EXIT(SDL_Init(SDL_INIT_VIDEO) != 0, "[ERROR]: Could not initialize SDL2\n");
render_context.window = SDL_CreateWindow(title,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width,
height,
SDL_WINDOW_SHOWN);
ERROR_EXIT(render_context.window == NULL, "[ERROR]: Could not create SDL window\n");
render_context.renderer = SDL_CreateRenderer(render_context.window, -1, SDL_RENDERER_ACCELERATED);
ERROR_EXIT(render_context.renderer == NULL, "[ERROR]: Could not create SDL renderer\n");
render_context.buffer_texture = SDL_CreateTexture(render_context.renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
WIDTH, HEIGHT);
ERROR_EXIT(render_context.buffer_texture == NULL, "[ERROR]: Could not create SDL texture\n");
return(render_context);
}
internal void destroy_render_context(Render_Context *ctx)
{
SDL_DestroyWindow(ctx->window);
SDL_DestroyRenderer(ctx->renderer);
SDL_DestroyTexture(ctx->buffer_texture);
SDL_Quit();
}
int main(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
srand((unsigned int) time(0));
Render_Context ctx = create_render_context(WIDTH, HEIGHT, "A window");
bool should_quit = false;
bool paused = false;
int current_time = 0;
int previous_time = SDL_GetTicks();
while (should_quit != true) {
current_time = SDL_GetTicks();
int time_elapsed = current_time - previous_time;
int time_to_wait = MS_PER_FRAME - time_elapsed;
float dt = time_elapsed / 1000.0f;
previous_time = current_time;
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT: {
should_quit = true;
} break;
case SDL_KEYUP: {
switch (e.key.keysym.sym) {
case SDLK_SPACE: {
paused = !paused;
} break;
case SDLK_q:
case SDLK_ESCAPE: {
should_quit = true;
} break;
}
}
}
}
if (!paused) {
for (size_t i = 0; i < WIDTH*HEIGHT; ++i) {
frame_buffer[i] = 0x181818FF;
}
render(dt);
SDL_UpdateTexture(ctx.buffer_texture, NULL, frame_buffer, sizeof(uint32_t)*WIDTH);
SDL_RenderCopy(ctx.renderer, ctx.buffer_texture, NULL, NULL);
}
SDL_RenderPresent(ctx.renderer);
if (time_to_wait > 0 && time_to_wait < MS_PER_FRAME) {
SDL_Delay(time_to_wait);
}
}
destroy_render_context(&ctx);
return 0;
}