summaryrefslogtreecommitdiff
path: root/src/game/game.c
blob: 47d70eb5ab9b32fe2fda6baa0807032c8c7bf0db (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
void init(State *state)
{
	U32	nmeshes;
	Mesh	*meshes;

	state->parena = arena_alloc(Kilobytes(256));

	state->camera = camera_init(v3(0.0f, 0.0f, 3.0f),
				    90.0f, 0.1f, 100.0f,
				    0.0f, 0.0f, 0.0f);
	state->camera_dp = V3_ZERO;

	state->mesh_shader = load_shader("shaders/default.vert", 0, "shaders/default.frag");
	state->ui_shader = load_shader("shaders/ui.vert", 0, "shaders/ui.frag");

	state->texture = load_texture(state->parena, str8lit("data/textures/grid.png"), 0);

	nmeshes = 2;
	meshes = arena_push(state->parena, nmeshes*sizeof(Mesh));
	meshes[0] = *mesh_gen_circle(state->parena, v3(0.0f, 0.0f, 2.0f), V3_ZERO, 3.0f, 6);
	meshes[1] = *mesh_gen_quad(state->parena, V3_ZERO, v3(0.0f, 90.0f, 0.0f), 2.0f, 2.0f);

	mesh_add_texture(&meshes[0], state->texture);

	state->model = model_init(state->parena, V3_ZERO, V3_ZERO, meshes, nmeshes);
}

void update_camera_first_person(State *state, Input *input, F32 speed, F32 mouse_sens)
{
	V3	ddp, r, u, f;

	camera_get_vectors_first_person(&state->camera, &r, &u, &f);
	ddp = V3_ZERO;
	if (key_is_pressed(input->move_forward))
		ddp = v3add(ddp, v3inv(f));
	if (key_is_pressed(input->move_backward))
		ddp = v3add(ddp, f);
	if (key_is_pressed(input->move_right))
		ddp = v3add(ddp, r);
	if (key_is_pressed(input->move_left))
		ddp = v3add(ddp, v3inv(r));
	if (key_is_pressed(input->move_up))
		ddp = v3add(ddp, u);
	if (key_is_pressed(input->move_down))
		ddp = v3add(ddp, v3inv(u));
	ddp = v3norm(ddp);

	state->camera.pos = v3add(state->camera.pos, state->camera_dp);
	state->camera_dp = v3scalef(v3add(state->camera_dp, v3scalef(ddp, input->dt*speed)), 0.8f);

	/* NOTE(pryazha): Camera angles */
	if (input->capture_mouse) {
		state->camera.yaw += input->mouse_offset.x*mouse_sens;
		state->camera.pitch += input->mouse_offset.y*mouse_sens;
		if (state->camera.pitch > 89.0f)
			state->camera.pitch = 89.0f;
		if (state->camera.pitch < -89.0f)
			state->camera.pitch = -89.0f;
	}
}

void update_and_render(Window *wnd, State *state, Input *input)
{
	/* NOTE(pryazha): Update */
	if (key_first_press(input->exit)) {
		if (input->capture_mouse) {
			input->capture_mouse = 0;
		} else {
			input->is_running = 0;
		}
	}

	/* NOTE(pryazha): Camera movement */
	update_camera_first_person(state, input, 1.0f, 0.3f);

	state->model->rotate.z += 20.0f*input->dt;
	state->model->rotate.x += 20.0f*input->dt;

	/* NOTE(pryazha): Render */
	MAT4	proj, view;
	F32	ar;
	Arena	*ui_arena;

	glViewport(0, 0, wnd->width, wnd->height);

	glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	ar = (F32)wnd->width/(F32)wnd->height;
	proj = camera_persp(state->camera, ar);
	view = camera_get_view_matrix_first_person(&state->camera);

	model_draw(state->mesh_shader, &proj, &view, state->model);

	glUseProgram(state->ui_shader);
	proj = ortho(0.0f, (F32)wnd->width, 0.0f, (F32)wnd->height, -1.0f, 1.0f);
	shader_set_mat4fv(state->ui_shader, "proj", proj);
	ui_arena = arena_alloc(0);
	glDisable(GL_DEPTH_TEST);

	if (button(wnd, state->ui_shader, input, ui_arena, str8lit("some name"),
		   v2(wnd->width/8.0f, wnd->height/8.0f), 50.0f, 30.0f))
	{
		input->capture_mouse = 1;
	}

	glEnable(GL_DEPTH_TEST);
	glUseProgram(0);
	arena_release(ui_arena);

	state->t += input->dt;
}

void clear(State *state)
{
	arena_release(state->parena);
}