From 6f7374c2fa58c8692b51018864b802e6b876d305 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 23 Nov 2024 21:52:24 +0100 Subject: A new start --- src/main.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ce94b9c --- /dev/null +++ b/src/main.c @@ -0,0 +1,182 @@ +#define ASSET_FONT_COUNT 25 +#define ASSET_WORKER_COUNT 4 +#define ASSET_QUEUE_COUNT 100 +#define ASSET_IMAGE_COUNT 70 +#define ASSET_SOUND_COUNT 30 +#define NUM_AUDIO_CHANNELS 8 +#define GAME_VERSION "0.1" + +#include "../project-base/src/project_base.h" + +platform_window* main_window; + +vec4 area; +float scale = 1.0f; +float zoom = 1.0f; +float camera_x = 0.0f; +float camera_y = 0.0f; +font* fnt_rd8; +font* fnt_rd12; +font* fnt_rd16; +font* fnt_rd20; +font* fnt_rd24; +font* fnt_rd28; +font* fnt_rd32; +font* fnt_rd36; +font* fnt_rd40; +font* fnt_rd44; +font* fnt_rd48; + +#include "include/settings.h" +#include "include/ui/colors.h" +#include "include/ui/animation.h" +#include "include/world.h" +#include "include/ui/portrait.h" +#include "include/scenery.h" +#include "include/data.h" +#include "include/game.h" +#include "include/tooltip.h" +#include "include/ui/panel.h" +#include "include/ui/button.h" +#include "include/ui/selectors.h" +#include "include/scenes/menu_scene.h" +#include "include/scenes/loading_scene.h" +#include "include/scenes/save_state_select.h" +#include "include/scenes/world_map.h" +#include "include/scenes/loading_world_scene.h" +#include "include/scenes/error_scene.h" +#include "include/scenes/place_detail.h" +#include "include/scenes/settings_scene.h" + +#include "music.c" +#include "world.c" +#include "data.c" +#include "game.c" +#include "scenery.c" +#include "ui/panel.c" +#include "ui/button.c" +#include "ui/animation.c" +#include "ui/portrait.c" +#include "tooltip.c" +#include "scenes/menu_scene.c" +#include "scenes/loading_scene.c" +#include "scenes/save_state_select.c" +#include "scenes/world_map.c" +#include "scenes/loading_world_scene.c" +#include "scenes/error_scene.c" +#include "scenes/place_detail.c" +#include "scenes/settings_scene.c" +#include "ui/selectors.c" + +#define CONFIG_DIRECTORY "trucker_x" + +static void draw_debug_overlay(platform_window* window) +{ + static bool enabled = false; + if (keyboard_is_key_pressed(KEY_F1)) enabled = !enabled; + if (!enabled) return; + + renderer->set_render_depth(20); + + renderer->render_rectangle(0,0,200*scale,200*scale,rgb(70,70,70)); + + font* fnt = FONT_REGULAR(SIZE_RD(area.w, 24)); + + { + char deltabuf[20]; + sprintf(deltabuf, "Frame: %.5f", frame_delta); + renderer->render_text(fnt, 10, 10, deltabuf, rgb(255,0,0)); + } + + { + char deltabuf[20]; + sprintf(deltabuf, "Game: %.5f", update_delta); + renderer->render_text(fnt, 10, 10+(fnt->px_h+2)*1, deltabuf, rgb(255,0,0)); + } + + { + char deltabuf[20]; + sprintf(deltabuf, "FPS: %.0f", 1.0f/frame_delta); + renderer->render_text(fnt, 10, 10+(fnt->px_h+2)*2, deltabuf, rgb(255,0,0)); + } + + renderer->set_render_depth(19); +} + +void update_render_game(platform_window* window) +{ + area = camera_get_target_rectangle(window); + scale = UI_SCALE(area.w); + + fnt_rd8 = FONT_REGULAR(SIZE_RDF(scale, 8)); + fnt_rd12 = FONT_REGULAR(SIZE_RDF(scale, 12)); + fnt_rd16 = FONT_REGULAR(SIZE_RDF(scale, 16)); + fnt_rd20 = FONT_REGULAR(SIZE_RDF(scale, 20)); + fnt_rd24 = FONT_REGULAR(SIZE_RDF(scale, 24)); + fnt_rd28 = FONT_REGULAR(SIZE_RDF(scale, 28)); + fnt_rd32 = FONT_REGULAR(SIZE_RDF(scale, 32)); + fnt_rd36 = FONT_REGULAR(SIZE_RDF(scale, 36)); + fnt_rd40 = FONT_REGULAR(SIZE_RDF(scale, 40)); + fnt_rd44 = FONT_REGULAR(SIZE_RDF(scale, 44)); + fnt_rd48 = FONT_REGULAR(SIZE_RDF(scale, 48)); + + #ifdef MODE_DEBUG + draw_debug_overlay(window); + #endif + + game_update(window); + game_render(window); + + update_music(); +} + +int main(int argc, char** argv) +{ + platform_init(argc, argv, CONFIG_DIRECTORY); + + #define VALIDATE_VOLUME(_vol) if (_vol < 0.0f) _vol = 0.0f; else if (_vol > 1.0f) _vol = 1.0f; + volume_global = settings_get_number_or_default("v_global", 100) / 100.0f; + volume_music = settings_get_number_or_default("v_music", 100) / 100.0f; + volume_sfx = settings_get_number_or_default("v_sfx", 100) / 100.0f; + option_vsync = settings_get_number_or_default("vsync", 1); + option_fullscreen = settings_get_number_or_default("fullscreen", 1); + VALIDATE_VOLUME(volume_global); + VALIDATE_VOLUME(volume_music); + VALIDATE_VOLUME(volume_sfx); + + s32 window_w = settings_get_number_or_default("window_w", 1280); + s32 window_h = settings_get_number_or_default("window_h", 720); + + main_window = platform_open_window("TruckerX", + window_w, window_h, + 9999, 9999, + 960, 540 + platform_get_titlebar_height(), + update_render_game, 0); + + platform_toggle_vsync(main_window, option_vsync); + if (option_fullscreen) platform_toggle_fullscreen(main_window, option_fullscreen); + + data_load(); + game_set_active_scene(GAME_STATE_LOADING); + + audio_set_mixer_volume(AUDIO_CHANNEL_SFX_1, volume_sfx*volume_global); + audio_set_mixer_volume(AUDIO_CHANNEL_SFX_2, volume_sfx*volume_global); + + while(platform_keep_running(main_window)) { + main_window->do_draw = true; + platform_handle_events(); + } + + settings_set_number("window_w", main_window->width); + settings_set_number("window_h", main_window->height + platform_get_titlebar_height()); + settings_set_number("v_global", volume_global*100); + settings_set_number("v_music", volume_music*100); + settings_set_number("v_sfx", volume_sfx*100); + settings_set_number("vsync", option_vsync); + settings_set_number("fullscreen", option_fullscreen); + + settings_write_to_file(); + platform_destroy(); + + return 0; +} \ No newline at end of file -- cgit v1.2.3-70-g09d2