small3dlib uint/magnolia / source / main.c
 1#include <SDL2/SDL.h>
 2
 3#include "SDL_keyboard.h"
 4#include "SDL_timer.h"
 5#include "util.h"
 6#include "platform/platform.h"
 7#include "renderer/renderer.h"
 8#include "renderer/test_scene.h"
 9
10static MGContext ctx = {
11	NULL, {NULL},
12	"Magnolia",
13	0, 0, 1024, 768,
14};
15
16static void input(void)
17{
18	static u32 last;
19
20	const u8* keys = SDL_GetKeyboardState(NULL);
21	MGVec3 move = MG_V3(0.0f, 0.0f, 0.0f);
22	u32 now = SDL_GetTicks();
23	float sensitivity = 0.0025f; /* arbritrary sensitivity */
24	float dt;
25	i32 mx;
26	i32 my;
27
28	if (!last) {
29		last = now;
30		return;
31	}
32
33	dt = (float)(now - last) / 1000.0f;
34	last = now;
35
36	if (keys[SDL_SCANCODE_W])
37		move.z += 1.0f;
38	if (keys[SDL_SCANCODE_A])
39		move.x -= 1.0f;
40	if (keys[SDL_SCANCODE_S])
41		move.z -= 1.0f;
42	if (keys[SDL_SCANCODE_D])
43		move.x += 1.0f;
44	if (keys[SDL_SCANCODE_SPACE])
45		move.y += 1.0f;
46	if (keys[SDL_SCANCODE_LCTRL])
47		move.y -= 1.0f;
48
49	move = MG_MulV3F(move, 4.0f * dt); /* arbritrary movement speed */
50	camera_move(&cam, move);
51	SDL_GetRelativeMouseState(&mx, &my);
52	camera_rotate(&cam, mx * sensitivity, -my*sensitivity);
53}
54
55int main(void)
56{
57	platform_init(&ctx);
58	platform_window_create(&ctx);
59	renderer_create(&ctx);
60	test_scene_create();
61	SDL_SetRelativeMouseMode(SDL_TRUE);
62
63	while (platform_poll(&ctx)) {
64		input();
65		renderer_begin();
66
67		/* ... */
68		test_scene_draw(&ctx);
69
70		renderer_end();
71		platform_present(&ctx);
72		SDL_Delay(1); /* TODO: replace with target fps */
73	}
74
75	test_scene_destroy();
76	renderer_destroy();
77	platform_shutdown(&ctx);
78	return MG_ERR_EXIT_SUCCESS;
79}
80