-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
150 lines (121 loc) · 4.29 KB
/
main.c
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
/**************************
* Includes
*
**************************/
#include <stdio.h>
#include <windows.h>
#include <gl/glu.h>
#include "utils/utils.h"
#include "gfx/gfx.h"
#include "window/window.h"
#include "game/game.h"
#include <stdlib.h>
#include <time.h>
const u32 WIDTH = 1920;
const u32 HEIGTH = 1080;
/**************************
* Function Declarations
*
**************************/
void process_camera_input(vx_Camera*, VX_T(i32, vx_Vec2), f32);
typedef struct {
gm_World world;
} State;
void init(vx_Window* window, State* state, vx_Renderer* renderer) {
ShowCursor(false);
srand(time(NULL));
VX_DEFAULT(vx_Position);
/* The renderer has a default camera, but it's suggested to push a new camera */
vx_Camera camera = vx_camera_new(120.0f, WIDTH / (f32)HEIGTH, 0.1f, 250.0f);
camera.position = VX_POSITION_NEW(25.0f, 20.0f, 25.0f);
vx_renderer_push_camera(renderer, camera);
glViewport(0, 0, WIDTH, HEIGTH);
/* Set renderer flags */
vx_renderer_set_cull(renderer, true);
vx_renderer_set_depth_test(renderer, true);
renderer->flags.clear_color = VX_VEC4_NEW(f32, 0.2f, 0.7f, 0.9f, 1.0f);
/* Create a new world and register the standard blocks, then generate the world */
state->world = gm_world_new(10);
gm_block_manager_push_standard_color_blocks(&(state->world.block_manager));
gm_world_gen_chunks(&state->world);
gm_world_gen_all_meshes(&state->world);
}
void logic(vx_Window* window, State* state, f32 delta, vx_Renderer* renderer) {
/* The renderer has a default camera. We don't need to push one... */
process_camera_input(vx_renderer_get_current_camera(renderer), window->mouse_offset, delta);
if (GetAsyncKeyState(VK_TAB) & VX_KEY_JUST_PRESSED){
window->grab_cursor = !window->grab_cursor;
ShowCursor(!window->grab_cursor);
}
/* Debug keys just for fun! */
if (GetAsyncKeyState('Z') & VX_KEY_JUST_PRESSED){
vx_renderer_set_wireframe(renderer, !renderer->flags.wireframe);
}
if (GetAsyncKeyState('X')) {
(state->world.chunks[GM_WORLD_POS_TO_INDEX(state->world.render_distance, 2, 2, 2)].blocks)[7][7][7] = 1;
gm_world_regen_chunk_mesh(&state->world, 2, 2, 2);
}
}
void draw(State* state, vx_Renderer* renderer) {
gm_world_draw(&state->world);
GL_CHECK_ERRORS();
}
void close(vx_Window* window, State* state, vx_Renderer* renderer) {
gm_world_free(&state->world);
ShowCursor(true);
}
void process_camera_input(vx_Camera* camera, VX_T(i32, vx_Vec2) mouse_offset, f32 delta) {
/* LOOK */
if (GetAsyncKeyState(VK_LEFT)) {
vx_camera_rotate_x(camera, -5.0f * delta);
} else if (GetAsyncKeyState(VK_RIGHT)) {
vx_camera_rotate_x(camera, 5.0f * delta);
}
if (GetAsyncKeyState(VK_DOWN)) {
vx_camera_rotate_y(camera, 5.0f * delta);
} else if (GetAsyncKeyState(VK_UP)) {
vx_camera_rotate_y(camera, -5.0f * delta);
}
if (GetAsyncKeyState('R')) {
camera->fov += 1.0f * delta;
} else if (GetAsyncKeyState('T')) {
camera->fov -= 1.0f * delta;
}
/* MOUSE LOOK */
vx_camera_rotate_x(camera, mouse_offset.x / 2.5f);
vx_camera_rotate_y(camera, mouse_offset.y / 2.5f);
/* MOVEMENT */
if (GetAsyncKeyState('W')) {
vx_camera_move_forward(camera, 0.5f * delta);
} else if (GetAsyncKeyState('S')) {
vx_camera_move_backward(camera, 0.5f * delta);
}
if (GetAsyncKeyState('D')) {
vx_camera_move_right(camera, 0.5f * delta);
} else if (GetAsyncKeyState('A')) {
vx_camera_move_left(camera, 0.5f * delta);
}
if (GetAsyncKeyState(VK_SPACE)) {
camera->position.y += 0.5f * delta;
} else if (GetAsyncKeyState(VK_CONTROL)) {
camera->position.y -= 0.5f * delta;
}
}
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
freopen("log.txt", "w", stdout);
vx_Window window = vx_window_new(hInstance, "OpenGL", WIDTH, HEIGTH, true);
window.init = init;
window.logic = logic;
window.draw = draw;
window.close = close;
State state;
vx_Window_run(&window, &state);
}