From b119b239a31dcb6628a21cb3668c8bb46ca1e901 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 11 Feb 2022 13:53:49 -0600 Subject: [PATCH] slight refactoring --- docs/todo.txt | 2 +- src/build.onyx | 2 ++ src/entity/editor.onyx | 4 ++- src/entity/items.onyx | 29 +++++++++++++++++++++ src/entity/player.onyx | 27 -------------------- src/game.onyx | 57 ++++++++++++++++++++++++++++++++++++++++++ src/gfx/ui.onyx | 2 ++ src/main.onyx | 54 ++++++--------------------------------- 8 files changed, 101 insertions(+), 76 deletions(-) create mode 100644 src/entity/items.onyx create mode 100644 src/game.onyx diff --git a/docs/todo.txt b/docs/todo.txt index 4c7bb0e..24dbb21 100644 --- a/docs/todo.txt +++ b/docs/todo.txt @@ -17,4 +17,4 @@ Automated asset loader for: - Sounds - Shaders? - +// Draw the cursor on textboxes diff --git a/src/build.onyx b/src/build.onyx index 5fd8821..013b443 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -14,9 +14,11 @@ DEBUG :: false #load "core/std" #load "config" +#load "game" #load "main" #load "entity/editor" +#load "entity/items" #load "entity/manager" #load "entity/player" #load "entity/store" diff --git a/src/entity/editor.onyx b/src/entity/editor.onyx index 0450ae2..7e2ae90 100644 --- a/src/entity/editor.onyx +++ b/src/entity/editor.onyx @@ -228,6 +228,8 @@ editor_draw :: () { member_any := any.{cast(^u8) v.data + it.offset, it.type}; if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) { + @TODO // This should query if this field has Editor_Disabled in its tags. + if Rect.contains(.{x, y + 2, w, Field_Height + 2}, mouse_get_position_vector()) { if active_index < 0 do sidebar_width += w; active_index = i - 1; @@ -329,7 +331,7 @@ editor_draw :: () { } } } else { - + @TODO // Add radio buttons for enum type. } } } diff --git a/src/entity/items.onyx b/src/entity/items.onyx new file mode 100644 index 0000000..e0fa2e2 --- /dev/null +++ b/src/entity/items.onyx @@ -0,0 +1,29 @@ + +use package core + +Item :: struct { + use entity: Entity; + + color: Color; + + init_data :: struct { + pos := Vector2.{0, 0}; + color := Color.{1, 0, 1, 1}; + } + + init :: (use this: ^Item, data: init_data) { + this.pos = data.pos; + this.size = .{16, 16}; + this.color = data.color; + this.flags |= .Carryable; + } + + get_rect :: Entity.get_rect + + draw :: (use this: ^Item) { + immediate_set_color(this.color); + + r := this->get_rect(); + immediate_rectangle(r.x, r.y, r.w, r.h); + } +} diff --git a/src/entity/player.onyx b/src/entity/player.onyx index 9592a30..d6279f1 100644 --- a/src/entity/player.onyx +++ b/src/entity/player.onyx @@ -259,33 +259,6 @@ Door :: struct { } -Item :: struct { - use entity: Entity; - - color: Color; - - init_data :: struct { - pos := Vector2.{0, 0}; - color := Color.{1, 0, 1, 1}; - } - - init :: (use this: ^Item, data: init_data) { - this.pos = data.pos; - this.size = .{16, 16}; - this.color = data.color; - this.flags |= .Carryable; - } - - get_rect :: Entity.get_rect - - draw :: (use this: ^Item) { - immediate_set_color(this.color); - - r := this->get_rect(); - immediate_rectangle(r.x, r.y, r.w, r.h); - } -} - @Relocate diff --git a/src/game.onyx b/src/game.onyx new file mode 100644 index 0000000..5f6c872 --- /dev/null +++ b/src/game.onyx @@ -0,0 +1,57 @@ + +use package core +use package glfw3 + +// +// Game Global Variables +// + +scene: Entity_Manager; + +game_init :: () { + player_texture = texture_make(#cstr "./assets/images/player.png"); + + scene = entity_manager_create(); + scene->register(Player); + scene->register(Wall); + scene->register(Door); + scene->register(Item); + scene->load_from_file("scenes/quick_save.scene"); + + #if DEBUG { + println("Registered Entity types:"); + for scene.entity_types.entries { + info := cast(^type_info.Type_Info_Struct) type_info.get_type_info(it.key); + printf(" {}\n", info.name); + } + } +} + +game_update :: (dt: f32) { + if is_key_just_up(GLFW_KEY_F8) { + scene->save_to_file("scenes/quick_save.scene"); + } + + if is_key_just_up(GLFW_KEY_F9) { + scene->load_from_file("scenes/quick_save.scene"); + } + + if editor_shown() { + editor_update(dt); + return; + } + + scene->update(dt); +} + +game_draw :: () { + update_world_matrix(); + update_model_matrix(.{0,0}); + + if editor_shown() { + editor_draw(); + return; + } + + scene->draw(); +} diff --git a/src/gfx/ui.onyx b/src/gfx/ui.onyx index 112706b..6d74b10 100644 --- a/src/gfx/ui.onyx +++ b/src/gfx/ui.onyx @@ -239,6 +239,8 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th immediate_set_color(surface_color); immediate_rectangle(x + border_width, y + border_width, w - border_width * 2, h - border_width * 2); + // Draw the cursor on textboxes + font_set_color(theme.text_color); font_draw(font, text_x, text_y, text); // This is technically a frame late for updating the text? diff --git a/src/main.onyx b/src/main.onyx index ad85974..c4044b1 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -9,18 +9,14 @@ DEBUG :: #defined(runtime.vars.DEBUG) @GlobalVariable window: GLFWwindow_p -@GlobalVariable -scene: Entity_Manager; - @GlobalVariable window_width: u32 window_height: u32 #if DEBUG { debug_font: Font; } - init :: () { - create_window(); + window = create_window(); input_bind_glfw_events(window); glInit(glfwGetLoadProcAddress()); @@ -35,29 +31,13 @@ init :: () { fonts_init(); immediate_init(); editor_init(); + game_init(); glfwGetWindowSize(window, ^window_width, ^window_height); glViewport(0, 0, window_width, window_height); update_view_matrix(); - player_texture = texture_make(#cstr "./assets/images/player.png"); - #if DEBUG { debug_font = font_lookup(.{"./assets/fonts/calibri.ttf", 16}); } - - scene = entity_manager_create(); - scene->register(Player); - scene->register(Wall); - scene->register(Door); - scene->register(Item); - scene->load_from_file("scenes/quick_save.scene"); - - #if DEBUG { - println("Registered Entity types:"); - for scene.entity_types.entries { - info := cast(^type_info.Type_Info_Struct) type_info.get_type_info(it.key); - printf(" {}\n", info.name); - } - } } update :: (dt: f32) { @@ -72,20 +52,7 @@ update :: (dt: f32) { editor_toggle(); } - if is_key_just_up(GLFW_KEY_F8) { - scene->save_to_file("scenes/quick_save.scene"); - } - - if is_key_just_up(GLFW_KEY_F9) { - scene->load_from_file("scenes/quick_save.scene"); - } - - if editor_shown() { - editor_update(dt); - return; - } - - scene->update(dt); + game_update(dt); } draw :: () { @@ -109,15 +76,7 @@ draw :: () { glfwSwapBuffers(window); } - update_world_matrix(); - update_model_matrix(.{0,0}); - - if editor_shown() { - editor_draw(); - return; - } - - scene->draw(); + game_draw(); #if false { mx, my := mouse_get_position(); @@ -159,7 +118,7 @@ run :: () { } } -create_window :: () { +create_window :: () => { #if runtime.compiler_os == .Linux { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); @@ -168,11 +127,12 @@ create_window :: () { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); } - window = glfwCreateWindow(800, 600, #cstr "Bar simulator"); + window := glfwCreateWindow(800, 600, #cstr "Bar simulator"); glfwMakeContextCurrent(window); glfwSwapInterval(1); glfwSetWindowSizeCallback(window, "on_resize"); + return window; } #export "on_resize" (window: GLFWwindow_p, width, height: u32) { -- 2.25.1