Techneck/code/source-c/initialization.c

98 lines
3.3 KiB
C
Raw Normal View History

2023-10-10 18:01:15 +00:00
#include "state.h"
2023-10-11 08:10:06 +00:00
#include "world.h"
2023-10-14 13:04:52 +00:00
#include "entity/registration.h"
2023-10-10 18:01:15 +00:00
#include <SDL2/SDL.h>
2023-10-12 06:18:43 +00:00
const char *tc_window_title_g = "Techneck";
techneck_s tc_game_state_g;
2023-10-10 18:01:15 +00:00
void tc_init_renderer(techneck_s *techneck)
{
SDL_Init(SDL_INIT_EVERYTHING);
techneck->renderer.window = SDL_CreateWindow(
tc_window_title_g,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1200,
800,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
2023-10-14 14:38:38 +00:00
techneck->renderer.active_camera = calloc(sizeof(tc_camera_s), 1);
techneck->renderer.active_camera->position.x = 0.0f;
techneck->renderer.active_camera->position.y = 0.0f;
techneck->renderer.active_camera->position.z = 0.0f;
2023-10-11 08:10:06 +00:00
2023-10-10 18:01:15 +00:00
techneck->renderer.gl_context = SDL_GL_CreateContext(techneck->renderer.window);
gladLoadGLLoader(&SDL_GL_GetProcAddress);
gladLoadGL();
techneck->renderer.draw_shader = tc_make_shader_program("vertex_shader.glsl", "fragment_shader.glsl");
glEnable(GL_DEPTH_TEST);
2023-10-13 10:23:26 +00:00
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
2023-10-10 18:01:15 +00:00
}
void tc_load_textures()
{
tc_game_state_g.block_texture_atlas = tc_load_image_from_disk("assets/block_atlas.png");
2023-10-12 06:18:43 +00:00
tc_game_state_g.block_texture_atlas->name = "Atlas";
tc_upload_image(tc_game_state_g.block_texture_atlas);
2023-10-12 06:18:43 +00:00
tc_image_s *dirt =
tc_create_subimage(tc_game_state_g.block_texture_atlas, 0, 0, 8, 8);
dirt->name = "dirt";
2023-10-12 06:18:43 +00:00
tc_image_s *grass =
tc_create_subimage(tc_game_state_g.block_texture_atlas, 8, 0, 8, 8);
grass->name = "grass-side";
tc_image_s *grass_top =
tc_create_subimage(tc_game_state_g.block_texture_atlas, 8, 8, 8, 8);
grass_top->name = "grass-top";
2023-10-12 06:18:43 +00:00
tc_image_s *stone =
tc_create_subimage(tc_game_state_g.block_texture_atlas, 16, 0, 8, 8);
stone->name = "stone";
}
2023-10-12 06:18:43 +00:00
void tc_init()
2023-10-10 18:01:15 +00:00
{
2023-10-12 06:18:43 +00:00
tc_init_renderer(&tc_game_state_g);
2023-10-14 14:38:38 +00:00
tc_game_state_g.asset_storage = tc_init_asset_storage("assets/");
2023-10-12 06:18:43 +00:00
tc_load_textures();
2023-10-14 14:38:38 +00:00
tc_game_state_g.block_registry = tc_init_blocks();
2023-10-12 06:18:43 +00:00
tc_create_blocks();
2023-10-14 14:38:38 +00:00
tc_game_state_g.entity_registry = tc_init_entity_registry();
2023-10-14 13:04:52 +00:00
tc_register_entities();
2023-10-15 12:41:53 +00:00
tc_game_state_g.on_world_create = tc_new_hooklist(16);
2023-10-17 11:25:02 +00:00
tc_add_to_hooklist(&tc_game_state_g.on_world_create, (tc_fn_hook) &tc_create_world_physics, NULL);
2023-10-15 12:41:53 +00:00
2023-10-12 16:52:05 +00:00
tc_init_worlds();
2023-10-14 14:38:38 +00:00
tc_game_state_g.main_world = tc_new_world(&tc_default_terrain_generator_g);
2023-10-15 12:41:53 +00:00
tc_game_state_g.player = tc_create_entity("player", NULL);
tc_game_state_g.viewer = tc_game_state_g.player;
2023-10-17 11:25:02 +00:00
tc_set_float_for_entity(tc_game_state_g.player, "pos_x", 0.0f);
tc_set_float_for_entity(tc_game_state_g.player, "pos_y", 38.0f);
tc_set_float_for_entity(tc_game_state_g.player, "pos_z", 0.0f);
2023-10-14 19:17:30 +00:00
2023-10-15 12:41:53 +00:00
tc_game_state_g.tps = 120;
2023-10-10 18:01:15 +00:00
2023-10-12 06:18:43 +00:00
puts("Finished initializing!");
2023-10-10 18:01:15 +00:00
}
void tc_cleanup()
{
2023-10-17 11:25:02 +00:00
tc_unload_world(tc_game_state_g.main_world);
tc_cleanup_chunk_pool();
2023-10-10 18:01:15 +00:00
SDL_GL_DeleteContext(tc_game_state_g.renderer.gl_context);
SDL_DestroyWindow(tc_game_state_g.renderer.window);
}