Techneck/code/source-c/main.c

340 lines
9.2 KiB
C
Raw Normal View History

2023-10-10 18:01:15 +00:00
#include "state.h"
2023-10-10 10:43:34 +00:00
#include <glad/glad.h>
#include <SDL2/SDL.h>
#include <linmath.h>
2023-10-10 11:35:29 +00:00
mat4x4 projection_matrix;
mat4x4 view_matrix;
2023-10-10 10:43:34 +00:00
2023-10-11 08:10:06 +00:00
float triangle_vertices[108] =
{
2023-10-10 18:01:15 +00:00
// Front
-0.5f, -0.5f, -0.5f,
2023-10-12 06:18:43 +00:00
-0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
2023-10-12 06:18:43 +00:00
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
// Back
2023-10-12 06:18:43 +00:00
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
2023-10-10 18:01:15 +00:00
2023-10-12 06:18:43 +00:00
-0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
2023-10-10 18:01:15 +00:00
// Left
2023-10-12 06:18:43 +00:00
-0.5f, 0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
-0.5f, -0.5f, -0.5f,
2023-10-12 06:18:43 +00:00
-0.5f, -0.5f, 0.5f,
2023-10-10 18:01:15 +00:00
2023-10-12 06:18:43 +00:00
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
// Right
2023-10-12 06:18:43 +00:00
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
2023-10-12 06:18:43 +00:00
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
2023-10-10 18:01:15 +00:00
// Top
2023-10-12 06:18:43 +00:00
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
2023-10-12 06:18:43 +00:00
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
// Bottom
2023-10-12 06:18:43 +00:00
-0.5f, -0.5f, 0.5f,
2023-10-10 18:01:15 +00:00
-0.5f, -0.5f, -0.5f,
2023-10-12 06:18:43 +00:00
0.5f, -0.5f, -0.5f,
2023-10-10 18:01:15 +00:00
2023-10-12 06:18:43 +00:00
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f
2023-10-10 18:01:15 +00:00
};
2023-10-10 10:43:34 +00:00
tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f };
tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f };
2023-10-10 10:43:34 +00:00
tc_block_s block;
void render_block(tc_block_s block)
{
2023-10-10 16:00:43 +00:00
mat4x4 model_matrix;
mat4x4_identity(model_matrix);
mat4x4_rotate_X(view_matrix, model_matrix, block.rotation.x);
mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y);
mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z);
mat4x4_translate_in_place(model_matrix, block.position.x, block.position.y, block.position.z);
2023-10-10 16:00:43 +00:00
2023-10-10 18:01:15 +00:00
int model_matrix_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix");
2023-10-10 16:00:43 +00:00
glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]);
2023-10-10 10:43:34 +00:00
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0);
2023-10-10 18:01:15 +00:00
glDrawArrays(GL_TRIANGLES, 0, 36);
2023-10-10 10:43:34 +00:00
}
tc_block_s tc_new_block_at_3f(float x, float y, float z)
{
tc_block_s block;
block.position.x = x;
block.position.y = y;
block.position.z = z;
block.rotation.x = 0.0f;
block.rotation.y = 0.0f;
block.rotation.z = 0.0f;
2023-10-10 10:43:34 +00:00
glGenBuffers(1, &block.drawing.vbo);
glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo);
2023-10-10 18:01:15 +00:00
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 108, &triangle_vertices[0], GL_STATIC_DRAW);
2023-10-10 10:43:34 +00:00
return block;
}
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-10 18:01:15 +00:00
float depth = 10000000.0f;
glClearBufferfv(GL_DEPTH, 0, &depth);
2023-10-10 10:43:34 +00:00
mat4x4_perspective(projection_matrix, 120, 1200 / 800, 0.0001f, 1000.0f);
2023-10-11 10:38:23 +00:00
mat4x4_identity(view_matrix);
2023-10-11 08:41:58 +00:00
mat4x4_rotate_X(view_matrix, view_matrix, tc_camera_rotation.x);
mat4x4_rotate_Y(view_matrix, view_matrix, tc_camera_rotation.y);
mat4x4_rotate_Z(view_matrix, view_matrix, tc_camera_rotation.z);
2023-10-11 10:38:23 +00:00
mat4x4_translate_in_place(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z);
2023-10-10 18:01:15 +00:00
glUseProgram(tc_game_state_g.renderer.draw_shader.program_id);
2023-10-10 18:01:15 +00:00
int projection_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "projection_matrix");
2023-10-10 11:35:29 +00:00
glUniformMatrix4fv(projection_uniform_location, 1, GL_FALSE, &projection_matrix[0][0]);
2023-10-10 18:01:15 +00:00
int view_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "view_matrix");
glUniformMatrix4fv(view_uniform_location, 1, GL_FALSE, &view_matrix[0][0]);
// printf("%d\n", view_uniform_location);
2023-10-11 08:41:58 +00:00
render_block(block);
2023-10-12 16:52:05 +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
}
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;
bool update()
{
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);
return false;
break;
case SDL_WINDOWEVENT:
{
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
{
puts("RESIZED");
}
} 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;
}
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;
}
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;
}
}
if(shift_pressed)
{
tc_camera_position.y -= 0.25;
}
if(go_up)
{
tc_camera_position.y += 0.25;
}
if(go_left)
{
tc_camera_position.x -= 0.25;
}
if(go_right)
{
tc_camera_position.x += 0.25;
}
if(go_forward)
{
tc_camera_position.z += 0.25;
}
if(go_backwards)
{
tc_camera_position.z -= 0.25;
}
2023-10-11 08:41:58 +00:00
if(rotate_left)
{
2023-10-11 10:38:23 +00:00
tc_camera_rotation.y -= 0.1f / (3.1415 * 2);
2023-10-11 08:41:58 +00:00
}
if(rotate_right)
{
2023-10-11 10:38:23 +00:00
tc_camera_rotation.y += 0.1f / (3.1415 * 2);
2023-10-11 08:41:58 +00:00
}
2023-10-13 06:00:53 +00:00
tc_game_state_g.main_world->loading_center.x = ((int32_t) tc_camera_position.x) / 32;
tc_game_state_g.main_world->loading_center.y = ((int32_t) tc_camera_position.y) / 32;
tc_game_state_g.main_world->loading_center.z = ((int32_t) tc_camera_position.z) / 32;
2023-10-12 16:52:05 +00:00
tc_update_world(tc_game_state_g.main_world);
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-13 06:00:53 +00:00
// tc_new_chunk(tc_game_state_g.main_world, 1.0f, 1.0f, 1.0f);
2023-10-10 10:43:34 +00:00
2023-10-10 10:43:34 +00:00
block = tc_new_block_at_3f(0.0, 0.0, 0.0);
2023-10-12 06:18:43 +00:00
// tc_new_cube_block();
2023-10-10 10:43:34 +00:00
int64_t frame_index = 1;
while(frame_index > 0)
{
if(!update())
2023-10-10 10:43:34 +00:00
{
frame_index = -1;
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
}
2023-10-10 18:01:15 +00:00
block.position.x += 0.005f;
2023-10-10 16:00:43 +00:00
2023-10-10 10:43:34 +00:00
++frame_index;
SDL_Delay(1000/60);
}
2023-10-10 18:01:15 +00:00
tc_cleanup();
2023-10-10 10:43:34 +00:00
SDL_Quit();
return 0;
}