2023-10-15 08:07:33 +00:00
|
|
|
#include <state.h>
|
|
|
|
#include <utility/math.h>
|
2023-10-10 18:01:15 +00:00
|
|
|
|
2023-10-10 10:43:34 +00:00
|
|
|
#include <glad/glad.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <linmath.h>
|
|
|
|
|
2023-10-14 14:38:38 +00:00
|
|
|
void tc_move_viewer_to(float x, float y, float z)
|
|
|
|
{
|
|
|
|
if(tc_game_state_g.viewer == NULL) return;
|
|
|
|
|
|
|
|
tc_set_float_for_entity(tc_game_state_g.viewer, "pos_x", x);
|
|
|
|
tc_set_float_for_entity(tc_game_state_g.viewer, "pos_y", y);
|
|
|
|
tc_set_float_for_entity(tc_game_state_g.viewer, "pos_z", z);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_rotate_viewer_to(float x, float y, float z)
|
|
|
|
{
|
|
|
|
if(tc_game_state_g.viewer == NULL) return;
|
|
|
|
|
|
|
|
tc_set_float_for_entity(tc_game_state_g.viewer, "rot_x", x);
|
|
|
|
tc_set_float_for_entity(tc_game_state_g.viewer, "rot_y", y);
|
|
|
|
tc_set_float_for_entity(tc_game_state_g.viewer, "rot_z", z);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_get_viewer_position(float *x, float *y, float *z)
|
|
|
|
{
|
|
|
|
if(tc_game_state_g.viewer == NULL) return;
|
|
|
|
|
|
|
|
if(x != NULL) (*x) = tc_get_float_from_entity(tc_game_state_g.viewer, "pos_x");
|
|
|
|
if(y != NULL) (*y) = tc_get_float_from_entity(tc_game_state_g.viewer, "pos_y");
|
|
|
|
if(z != NULL) (*z) = tc_get_float_from_entity(tc_game_state_g.viewer, "pos_z");
|
|
|
|
}
|
|
|
|
|
|
|
|
void tc_get_viewer_rotation(float *x, float *y, float *z)
|
|
|
|
{
|
|
|
|
if(tc_game_state_g.viewer == NULL) return;
|
|
|
|
|
|
|
|
if(x != NULL) (*x) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_x");
|
|
|
|
if(y != NULL) (*y) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_y");
|
|
|
|
if(z != NULL) (*z) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_z");
|
|
|
|
}
|
2023-10-10 15:06:01 +00:00
|
|
|
|
2023-10-10 10:43:34 +00:00
|
|
|
void render()
|
|
|
|
{
|
2023-10-12 16:52:05 +00:00
|
|
|
float color[4] = { 0.12f, 0.5f, 0.8f, 1.0f };
|
2023-10-10 10:43:34 +00:00
|
|
|
glClearBufferfv(GL_COLOR, 0, color);
|
2023-10-15 08:07:33 +00:00
|
|
|
float depth = 1000.0f;
|
2023-10-10 18:01:15 +00:00
|
|
|
glClearBufferfv(GL_DEPTH, 0, &depth);
|
2023-10-10 10:43:34 +00:00
|
|
|
|
2023-10-13 12:00:49 +00:00
|
|
|
int fb_width = 0;
|
|
|
|
int fb_height = 0;
|
|
|
|
SDL_GetWindowSize(tc_game_state_g.renderer.window, &fb_width, &fb_height);
|
|
|
|
|
|
|
|
glViewport(0, 0, fb_width, fb_height);
|
2023-10-11 10:38:23 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_vec3f_s camera_rotation;
|
|
|
|
tc_vec3f_s camera_position;
|
2023-10-14 14:38:38 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z);
|
2023-10-14 14:38:38 +00:00
|
|
|
tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
|
2023-10-13 12:37:02 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_vec3f_s camera_offset;
|
|
|
|
camera_offset.z = camera_position.z;
|
|
|
|
camera_offset.y = -camera_position.y;
|
|
|
|
camera_offset.x = -camera_position.x;
|
2023-10-10 15:06:01 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_mat4f_s view_matrix = tc_mat4f_identity();
|
|
|
|
tc_mat4f_s projection_matrix =
|
|
|
|
tc_mat4f_perspective(120,
|
|
|
|
((float) fb_width) / ((float) fb_height),
|
|
|
|
0.0001f, 1000.0f
|
|
|
|
);
|
2023-10-11 10:38:23 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
view_matrix = tc_mat4f_translate(view_matrix, camera_offset);
|
|
|
|
|
|
|
|
view_matrix = tc_mat4_rotate_z(view_matrix, tc_deg_to_rad(camera_rotation.z));
|
|
|
|
view_matrix = tc_mat4_rotate_y(view_matrix, tc_deg_to_rad(camera_rotation.y));
|
|
|
|
view_matrix = tc_mat4_rotate_x(view_matrix, tc_deg_to_rad(camera_rotation.x));
|
2023-10-10 15:06:01 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
glUseProgram(tc_game_state_g.renderer.draw_shader.program_id);
|
2023-10-10 11:35:29 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "projection_matrix", projection_matrix);
|
|
|
|
tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "view_matrix", view_matrix);
|
2023-10-10 15:06:01 +00:00
|
|
|
|
2023-10-17 20:13:54 +00:00
|
|
|
// tc_draw_world(tc_game_state_g.main_world);
|
2023-10-10 10:43:34 +00:00
|
|
|
|
2023-10-10 18:01:15 +00:00
|
|
|
SDL_GL_SwapWindow(tc_game_state_g.renderer.window);
|
2023-10-10 10:43:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
bool shift_pressed = false;
|
|
|
|
bool go_up = false;
|
|
|
|
bool go_left = false;
|
|
|
|
bool go_right = false;
|
|
|
|
bool go_forward = false;
|
|
|
|
bool go_backwards = false;
|
2023-10-11 08:41:58 +00:00
|
|
|
bool rotate_left = false;
|
|
|
|
bool rotate_right = false;
|
2023-10-10 15:06:01 +00:00
|
|
|
|
2023-10-14 19:17:30 +00:00
|
|
|
uint64_t tc_last_update = 0;
|
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
bool update()
|
|
|
|
{
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_vec3f_s camera_rotation;
|
2023-10-14 14:38:38 +00:00
|
|
|
tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z);
|
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
SDL_Event event;
|
|
|
|
while(SDL_PollEvent(&event))
|
|
|
|
{
|
|
|
|
switch(event.type)
|
|
|
|
{
|
|
|
|
case SDL_QUIT:
|
2023-10-10 18:01:15 +00:00
|
|
|
SDL_HideWindow(tc_game_state_g.renderer.window);
|
2023-10-10 15:06:01 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
{
|
|
|
|
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
|
|
|
|
{
|
2023-10-13 12:00:49 +00:00
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
|
2023-10-11 08:41:58 +00:00
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_Q)
|
|
|
|
{
|
|
|
|
rotate_left = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_E)
|
|
|
|
{
|
|
|
|
rotate_right = true;
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_W)
|
|
|
|
{
|
|
|
|
go_forward = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_S)
|
|
|
|
{
|
|
|
|
go_backwards = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_A)
|
|
|
|
{
|
|
|
|
go_left = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_D)
|
|
|
|
{
|
|
|
|
go_right = true;;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_SPACE)
|
|
|
|
{
|
|
|
|
go_up = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_LSHIFT)
|
|
|
|
{
|
|
|
|
shift_pressed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_KEYUP:
|
|
|
|
|
2023-10-11 08:41:58 +00:00
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_Q)
|
|
|
|
{
|
|
|
|
rotate_left = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_E)
|
|
|
|
{
|
|
|
|
rotate_right = false;
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_W)
|
|
|
|
{
|
|
|
|
go_forward = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_S)
|
|
|
|
{
|
|
|
|
go_backwards = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_A)
|
|
|
|
{
|
|
|
|
go_left = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_D)
|
|
|
|
{
|
|
|
|
go_right = false;;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_SPACE)
|
|
|
|
{
|
|
|
|
go_up = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.key.keysym.scancode == SDL_SCANCODE_LSHIFT)
|
|
|
|
{
|
|
|
|
shift_pressed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2023-10-13 12:37:02 +00:00
|
|
|
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
{
|
|
|
|
if(!(event.motion.state & SDL_BUTTON_LMASK)) break;
|
2023-10-15 12:44:51 +00:00
|
|
|
camera_rotation.y += event.motion.xrel / 5.0f;
|
|
|
|
camera_rotation.x += event.motion.yrel / 5.0f;
|
2023-10-13 12:37:02 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
if(camera_rotation.x > 90)
|
|
|
|
camera_rotation.x = 90;
|
2023-10-13 12:37:02 +00:00
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
if(camera_rotation.x < -90)
|
|
|
|
camera_rotation.x = -90;
|
2023-10-13 12:37:02 +00:00
|
|
|
|
|
|
|
} break;
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_vec3f_s camera_position;
|
2023-10-14 14:38:38 +00:00
|
|
|
tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
|
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
if(shift_pressed)
|
|
|
|
{
|
2023-10-15 12:44:02 +00:00
|
|
|
camera_position.y -= 0.75;
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(go_up)
|
|
|
|
{
|
2023-10-15 12:44:02 +00:00
|
|
|
camera_position.y += 0.75;
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 12:37:02 +00:00
|
|
|
|
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
tc_vec3f_s difference_in_perspective;
|
2023-10-13 12:37:02 +00:00
|
|
|
difference_in_perspective.x = 0.0f;
|
|
|
|
difference_in_perspective.y = 0.0f;
|
|
|
|
difference_in_perspective.z = 0.0f;
|
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
if(go_left)
|
|
|
|
{
|
2023-10-17 11:25:02 +00:00
|
|
|
difference_in_perspective.x -= 2.0f;
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(go_right)
|
|
|
|
{
|
2023-10-17 11:25:02 +00:00
|
|
|
difference_in_perspective.x += 2.0f;
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(go_forward)
|
|
|
|
{
|
2023-10-17 11:25:02 +00:00
|
|
|
difference_in_perspective.z += 2.5f;
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(go_backwards)
|
|
|
|
{
|
2023-10-17 11:25:02 +00:00
|
|
|
difference_in_perspective.z -= 2.5f;
|
2023-10-10 15:06:01 +00:00
|
|
|
}
|
2023-10-11 08:41:58 +00:00
|
|
|
|
2023-10-13 12:37:02 +00:00
|
|
|
mat4x4 position_matrix;
|
|
|
|
mat4x4_identity(position_matrix);
|
2023-10-11 08:41:58 +00:00
|
|
|
|
2023-10-13 12:37:02 +00:00
|
|
|
mat4x4_translate(position_matrix, difference_in_perspective.x, 0.0f, difference_in_perspective.z);
|
|
|
|
|
2023-10-15 08:07:33 +00:00
|
|
|
mat4x4_rotate_Z(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.z));
|
|
|
|
mat4x4_rotate_Y(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.y));
|
|
|
|
mat4x4_rotate_X(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.x));
|
2023-10-13 12:37:02 +00:00
|
|
|
|
|
|
|
vec4 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z };
|
|
|
|
vec4 raw_difference = { 0.0f, 0.0f, 0.0f };
|
|
|
|
mat4x4_mul_vec4(raw_difference, position_matrix, viewed_difference);
|
|
|
|
|
2023-10-14 14:38:38 +00:00
|
|
|
camera_position.x += raw_difference[0];
|
|
|
|
camera_position.z += raw_difference[2];
|
|
|
|
|
|
|
|
tc_move_viewer_to(camera_position.x, camera_position.y, camera_position.z);
|
|
|
|
tc_rotate_viewer_to(camera_rotation.x, camera_rotation.y, camera_rotation.z);
|
2023-10-11 08:41:58 +00:00
|
|
|
|
2023-10-17 20:13:54 +00:00
|
|
|
// tc_game_state_g.main_world->spawn_loader->center.grid_coords.x = ((int32_t) camera_position.x) / 32 - UPDATE_DISTANCE;
|
|
|
|
// tc_game_state_g.main_world->spawn_loader->center.grid_coords.y = ((int32_t) camera_position.y) / 32 - UPDATE_DISTANCE;
|
|
|
|
// tc_game_state_g.main_world->spawn_loader->center.grid_coords.z = - ((int32_t) camera_position.z) / 32 - UPDATE_DISTANCE;
|
2023-10-12 16:52:05 +00:00
|
|
|
|
2023-10-17 20:13:54 +00:00
|
|
|
// tc_update_world(tc_game_state_g.main_world);
|
2023-10-12 16:52:05 +00:00
|
|
|
|
2023-10-10 15:06:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-10 10:43:34 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2023-10-12 06:18:43 +00:00
|
|
|
tc_init();
|
2023-10-10 10:43:34 +00:00
|
|
|
|
|
|
|
int64_t frame_index = 1;
|
|
|
|
while(frame_index > 0)
|
|
|
|
{
|
2023-10-10 15:06:01 +00:00
|
|
|
if(!update())
|
2023-10-10 10:43:34 +00:00
|
|
|
{
|
2023-10-10 15:06:01 +00:00
|
|
|
frame_index = -1;
|
2023-10-10 10:43:34 +00:00
|
|
|
}
|
2023-10-14 19:17:30 +00:00
|
|
|
tc_tick_intervals();
|
|
|
|
|
2023-10-10 10:43:34 +00:00
|
|
|
render();
|
|
|
|
|
|
|
|
if(frame_index == 1)
|
|
|
|
{
|
2023-10-10 18:01:15 +00:00
|
|
|
SDL_ShowWindow(tc_game_state_g.renderer.window);
|
2023-10-10 10:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++frame_index;
|
2023-10-14 19:17:30 +00:00
|
|
|
SDL_Delay(2);
|
2023-10-10 10:43:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 18:01:15 +00:00
|
|
|
tc_cleanup();
|
2023-10-10 10:43:34 +00:00
|
|
|
SDL_Quit();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|