summaryrefslogtreecommitdiff
path: root/src/engine/sdl_linux.c
blob: 806b718b928b43274d812a97cc1534cab410a19a (plain)
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
#include "GL/glew.h"
#include "SDL3/SDL.h"

#include "prge.h"

#include "game.h"
#include "game.c"

void process_mouse_pos(Input *input, SDL_MouseMotionEvent *mouseev)
{
	V2	offset;

	input->mouse_pos = v2((F32)mouseev->x, (F32)mouseev->y);

	if (input->first_mouse)
		input->first_mouse = 0;

	offset = v2((F32)mouseev->xrel, (F32)mouseev->yrel);
	input->mouse_offset = v2add(input->mouse_offset, offset);
}

void process_key(B32 down, Key *key)
{
	key->state = (down ? KeyState_PRESS : KeyState_RELEASE);
}

void process_keyboard(Input *input, SDL_KeyboardEvent *keyev)
{
	switch (keyev->scancode) {
	case SDL_SCANCODE_D:
		process_key(keyev->down, &input->move_right);
		break;
	case SDL_SCANCODE_W:
		process_key(keyev->down, &input->move_forward);
		break;
	case SDL_SCANCODE_A:
		process_key(keyev->down, &input->move_left);
		break;
	case SDL_SCANCODE_S:
		process_key(keyev->down, &input->move_backward);
		break;
	case SDL_SCANCODE_E:
		process_key(keyev->down, &input->move_up);
		break;
	case SDL_SCANCODE_Q:
		process_key(keyev->down, &input->move_down);
		break;
	case SDL_SCANCODE_SPACE:
		process_key(keyev->down, &input->jump);
		break;
	case SDL_SCANCODE_RIGHT:
		process_key(keyev->down, &input->action_right);
		break;
	case SDL_SCANCODE_UP:
		process_key(keyev->down, &input->action_up);
		break;
	case SDL_SCANCODE_LEFT:
		process_key(keyev->down, &input->action_left);
		break;
	case SDL_SCANCODE_DOWN:
		process_key(keyev->down, &input->action_down);
		break;
	case SDL_SCANCODE_ESCAPE:
		process_key(keyev->down, &input->exit);
		break;
	default:
		break;
	}
}

void process_mouse_buttons(Input *input, SDL_MouseButtonEvent *buttonev)
{
	if (buttonev->button == SDL_BUTTON_LEFT)
		process_key(buttonev->down, &input->mouse_left);
	else if (buttonev->button == SDL_BUTTON_RIGHT)
		process_key(buttonev->down, &input->mouse_right);
}

void handle_events(Window *wnd, Input *input)
{
	SDL_Event	event;

	while (SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_EVENT_KEY_DOWN:
		case SDL_EVENT_KEY_UP:
			process_keyboard(input, &event.key);
			break;
		case SDL_EVENT_MOUSE_MOTION:
			process_mouse_pos(input, &event.motion);
			break;
		case SDL_EVENT_MOUSE_BUTTON_DOWN:
		case SDL_EVENT_MOUSE_BUTTON_UP:
			process_mouse_buttons(input, &event.button);
			break;
		case SDL_EVENT_WINDOW_RESIZED:
			wnd->width = event.window.data1;
			wnd->height = event.window.data2;
			break;
		default:
			break;
		}
	}
}

F32 get_elapsed_seconds(U64 cur, U64 last)
{
	F32	res;
	res = ((F32)cur-(F32)last)/1000.0f;
	return res;
}

F32 lock_framerate(U64 last, F32 target)
{
        F32	target_mspf, elapsed, sleep, dt;

	elapsed = get_elapsed_seconds(SDL_GetTicks(), last)*1000.0f;
	target_mspf = 1000.0f/target;
 
	if (elapsed < target_mspf) {
		sleep = target_mspf-elapsed;
		if (sleep > 0.0f)
			SDL_Delay(sleep);
		/*
		do {
			elapsed = get_elapsed_seconds(SDL_GetTicks(), last)*1000.0f;
		} while (elapsed < target_mspf);
		*/
	}

	dt = get_elapsed_seconds(SDL_GetTicks(), last);

	return dt;
}

B32 sdl_init(Window *wnd, SDL_Window **sdl_window, SDL_GLContext *context)
{
	U32	flags;
	GLenum	error;
	Arena	*tmpar;
	char	*wname;

	if (!SDL_Init(SDL_INIT_VIDEO)) {
		SDL_Log("[ERROR] : SDL : %s\n", SDL_GetError());
		return 0;
	}

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

	tmpar = arena_alloc(0);
	wname = str8tocstr(tmpar, wnd->name);

	flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;

	(*sdl_window) = SDL_CreateWindow(wname, wnd->width, wnd->height, flags);
	
	arena_release(tmpar);

	if (!(*sdl_window)) {
		SDL_Log("[ERROR] : SDL : %s\n", SDL_GetError());
		return 0;
	}

	SDL_GetWindowSize(*sdl_window, &wnd->width, &wnd->height);

	(*context) = SDL_GL_CreateContext((*sdl_window));
	if (!(*context)) {
		SDL_Log("[ERROR] : SDL : %s\n", SDL_GetError());
		return 0;
	}

	error = glewInit();
	if (error != GLEW_OK) {
		SDL_Log("[ERROR] : GLEW : %s\n", glewGetErrorString(error));
		return 0;
	}

	return 1;
}

int main(void)
{
	/* NOTE(pryazha): Engine init */
	Input		input;
	Window		wnd;

	input = input_init();
	wnd = window_init(800, 600, str8lit("PRGE example"));

	/* NOTE(pryazha): SDL and OpenGL init */
	SDL_Window	*sdl_window;
	SDL_GLContext	context;
	U64		last;

	if (!sdl_init(&wnd, &sdl_window, &context)) {
		SDL_Quit();
		return 1;
	}

	glEnable(GL_DEPTH_TEST);

	/* NOTE(pryazha): Game init */
	State		state;
	MemoryZero(&state, sizeof(State));
	
	init(&state);

	last = SDL_GetTicks();

	while (input.is_running) {
		SDL_SetWindowRelativeMouseMode(sdl_window, input.capture_mouse);

		handle_events(&wnd, &input);

		input.dt = lock_framerate(last, 60.0f);
		last = SDL_GetTicks();

		update_and_render(&wnd, &state, &input);

		input_update(&input);

		SDL_GL_SwapWindow(sdl_window);
	}

	clear(&state);

	SDL_GL_DestroyContext(context);
	SDL_DestroyWindow(sdl_window);
	SDL_Quit();
	return 0;
}