From aa4349df91fc34567725bb16b5aacc322ba6a4da Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 26 Oct 2022 11:31:58 -0500 Subject: [PATCH] began refactoring graphics out of this project --- .vscode/launch.json | 6 +- run_tree/run.sh | 2 +- src/build.onyx | 2 +- src/entity/components/background.onyx | 1 + src/entity/components/collision_mask.onyx | 39 +++++++------ src/entity/components/dispenser.onyx | 5 +- src/entity/components/entryway.onyx | 1 + src/entity/components/money.onyx | 3 +- src/entity/components/movement.onyx | 1 + src/entity/components/patron.onyx | 5 +- src/entity/components/player.onyx | 3 +- src/entity/editor.onyx | 16 +++--- src/entity/entities.onyx | 21 ++----- src/entity/items.onyx | 6 +- src/entity/scene.onyx | 17 +++--- src/entity/schematics/background.onyx | 2 +- src/entity/schematics/entryway.onyx | 2 +- src/entity/schematics/furniture.onyx | 3 +- src/entity/schematics/patron.onyx | 5 +- src/entity/schematics/player.onyx | 7 ++- src/entity/schematics/tap.onyx | 3 +- src/game.onyx | 11 ++-- src/main.onyx | 56 +++---------------- src/{gfx => ogre}/canvas.onyx | 5 +- src/{utils => ogre}/colors.onyx | 3 +- src/{gfx => ogre}/font.onyx | 1 + src/{gfx => ogre}/immediate.onyx | 18 +++++- src/{utils => ogre}/input.onyx | 31 +++++------ src/{gfx => ogre}/mesh.onyx | 2 + src/{gfx => ogre}/shader.onyx | 1 + src/{gfx => ogre}/texture.onyx | 5 +- src/{gfx => ogre}/ui.onyx | 9 ++- src/{utils => ogre}/vecmath.onyx | 45 +++++++-------- src/ogre/window.onyx | 67 +++++++++++++++++++++++ src/sfx/audio_manager.onyx | 14 ++--- src/utils/asset_loader.onyx | 3 +- 36 files changed, 239 insertions(+), 182 deletions(-) rename src/{gfx => ogre}/canvas.onyx (93%) rename src/{utils => ogre}/colors.onyx (98%) rename src/{gfx => ogre}/font.onyx (99%) rename src/{gfx => ogre}/immediate.onyx (94%) rename src/{utils => ogre}/input.onyx (84%) rename src/{gfx => ogre}/mesh.onyx (99%) rename src/{gfx => ogre}/shader.onyx (99%) rename src/{gfx => ogre}/texture.onyx (94%) rename src/{gfx => ogre}/ui.onyx (99%) rename src/{utils => ogre}/vecmath.onyx (77%) create mode 100644 src/ogre/window.onyx diff --git a/.vscode/launch.json b/.vscode/launch.json index ca18c20..66ac8cd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,10 +12,10 @@ "type": "onyx", "request": "launch", "name": "Launch", - "wasmFile": "game.wasm", + "onyxFiles": ["../src/build"], "workingDir": "${workspaceFolder}/run_tree", - "stopOnEntry": true, - "preLaunchTask": "Build debug game" + "stopOnEntry": true + // "preLaunchTask": "Build debug game" } ] } diff --git a/run_tree/run.sh b/run_tree/run.sh index e9050d4..27fb7ad 100755 --- a/run_tree/run.sh +++ b/run_tree/run.sh @@ -6,4 +6,4 @@ case "$1" in run) onyx-run $dest ;; debug) onyx-run --debug $dest ;; *) onyx run -V -I ../src build $@ ;; -esac \ No newline at end of file +esac diff --git a/src/build.onyx b/src/build.onyx index a03806a..9246fe2 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -17,7 +17,6 @@ MINOR_VERSION :: 1 #load_all "./entity" #load_all "./entity/components" #load_all "./entity/schematics" -#load_all "./gfx" #load_all "./sfx" #load_all "./utils" @@ -26,3 +25,4 @@ MINOR_VERSION :: 1 #load "./../lib/openal/module" #load "./../lib/stb_truetype/module" #load "./../lib/stb_image/module" +#load_all "./ogre" diff --git a/src/entity/components/background.onyx b/src/entity/components/background.onyx index 3bce989..241071c 100644 --- a/src/entity/components/background.onyx +++ b/src/entity/components/background.onyx @@ -1,5 +1,6 @@ use core +use ogre BackgroundComponent :: struct { use component: Component; diff --git a/src/entity/components/collision_mask.onyx b/src/entity/components/collision_mask.onyx index c9d52b5..707e9c7 100644 --- a/src/entity/components/collision_mask.onyx +++ b/src/entity/components/collision_mask.onyx @@ -1,5 +1,6 @@ use core +use ogre CollisionMaskComponent :: struct { use component: Component; @@ -8,12 +9,16 @@ CollisionMaskComponent :: struct { width: i32 = 50; height: i32 = 40; - #tag Entity_Store.Skip, Editor_Hidden + @Entity_Store.Skip @Editor_Hidden mask: [] bool; - #tag Entity_Store.Skip + @Entity_Store.Skip should_render := false; + init :: (use this: ^CollisionMaskComponent) { + mask = make([] bool, width * height); + } + added :: (use this: ^CollisionMaskComponent, entity: ^Entity) { scene->modify_component(entity, RenderComponent) { comp.func = render; @@ -21,21 +26,21 @@ CollisionMaskComponent :: struct { } update :: (use this: ^CollisionMaskComponent, entity: ^Entity, dt: f32) { - if mask.data == null { - memory.alloc_slice(^mask, width * height); - - for y: height { - for x: width { - mask[y * width + x] = false; - area := Rect.{ ~~(x * grid_size), ~~(y * grid_size), ~~grid_size, ~~grid_size }; - - for scene.entities { - if it.flags & .Solid { - if Rect.intersects(Entity.get_rect(it), area) { - mask[y * width + x] = true; - continue continue; - } - } + memory.set(mask.data, 0, width * height * sizeof bool); + + for scene.entities { + if it.flags & .Solid { + r := it->get_rect(); + + g := cast(f32) grid_size; + x0 := cast(i32) math.floor(r.x / g); + y0 := cast(i32) math.floor(r.y / g); + x1 := cast(i32) math.ceil((r.x + r.w) / g); + y1 := cast(i32) math.ceil((r.y + r.h) / g); + + for y: y0 .. y1 { + for x: x0 .. x1 { + mask[y * width + x] = true; } } } diff --git a/src/entity/components/dispenser.onyx b/src/entity/components/dispenser.onyx index ecc60b0..649c825 100644 --- a/src/entity/components/dispenser.onyx +++ b/src/entity/components/dispenser.onyx @@ -1,5 +1,6 @@ use core +use ogre // // Currently, DispenserComponents only dispense Item_Entity's, and no @@ -9,12 +10,12 @@ use core DispenserComponent :: struct { use base: Component; - #tag Editor_Custom_Field.{render_item_picker} + @Editor_Custom_Field.{render_item_picker} item: str; max_timeout := 2.0f; draw_item := true; - #tag Entity_Store.Skip + @Entity_Store.Skip timeout := 0.0f; added :: (use this: ^DispenserComponent, entity: ^Entity) { diff --git a/src/entity/components/entryway.onyx b/src/entity/components/entryway.onyx index c180189..d313e78 100644 --- a/src/entity/components/entryway.onyx +++ b/src/entity/components/entryway.onyx @@ -1,5 +1,6 @@ use core +use ogre EntrywayComponent :: struct { use component: Component; diff --git a/src/entity/components/money.onyx b/src/entity/components/money.onyx index a4e1689..78a2921 100644 --- a/src/entity/components/money.onyx +++ b/src/entity/components/money.onyx @@ -1,5 +1,6 @@ use core +use ogre #local Transaction :: struct { amount: i32; @@ -12,7 +13,7 @@ MoneyComponent :: struct { money: i32 = 100; money_sprite: Sprite; - #tag Editor_Hidden, Entity_Store.Skip + @Editor_Hidden, Entity_Store.Skip transactions: [..] Transaction; added :: (use this: ^MoneyComponent, entity: ^Entity) { diff --git a/src/entity/components/movement.onyx b/src/entity/components/movement.onyx index ae7f97f..81f3816 100644 --- a/src/entity/components/movement.onyx +++ b/src/entity/components/movement.onyx @@ -1,6 +1,7 @@ use core use glfw3 +use ogre Facing :: enum { diff --git a/src/entity/components/patron.onyx b/src/entity/components/patron.onyx index 1879bc6..bc8ac42 100644 --- a/src/entity/components/patron.onyx +++ b/src/entity/components/patron.onyx @@ -1,5 +1,6 @@ use core +use ogre #local Patron_State :: enum { Walking_To_Seat; @@ -21,9 +22,9 @@ PatronComponent :: struct { annoy_timeout: f32; walk_speed: f32 = 100.0f; - #tag Editor_Hidden, Entity_Store.Skip + @Editor_Hidden, Entity_Store.Skip path: [..] Vector2; - #tag Editor_Hidden, Entity_Store.Skip + @Editor_Hidden, Entity_Store.Skip path_pos: i32; init :: (use this: ^PatronComponent) { diff --git a/src/entity/components/player.onyx b/src/entity/components/player.onyx index 450972b..bb99537 100644 --- a/src/entity/components/player.onyx +++ b/src/entity/components/player.onyx @@ -1,13 +1,14 @@ use core use glfw3 +use ogre PlayerComponent :: struct { use base: Component; holding : Entity_ID; - #tag Editor_Hidden, Entity_Store.Skip + @Editor_Hidden, Entity_Store.Skip nearby_holding, nearby_interact : Entity_ID; update :: (player: ^PlayerComponent, use this: ^Entity, dt: f32) { diff --git a/src/entity/editor.onyx b/src/entity/editor.onyx index 69b02b3..dd45a70 100644 --- a/src/entity/editor.onyx +++ b/src/entity/editor.onyx @@ -10,7 +10,9 @@ use core use opengles use glfw3 -#local type_info :: runtime.info +use ogre +use ogre.ui +use runtime {type_info :: info} Editor_Range :: struct {min, max: f32;} Editor_Disabled :: struct {} @@ -66,7 +68,7 @@ editor_update :: (dt: f32) { mouse_raw := mouse_get_position_vector(); mouse_pos := mouse_raw - scene_render_offset; - if is_button_just_up(GLFW_MOUSE_BUTTON_LEFT) && mouse_raw.x < ~~window_width - sidebar_width && mouse_raw.x >= 0 { + if is_button_just_up(GLFW_MOUSE_BUTTON_LEFT) && mouse_raw.x < ~~window.width - sidebar_width && mouse_raw.x >= 0 { entity: ^Entity; if active_index == 0 { entity = scene->make(); @@ -87,7 +89,7 @@ editor_update :: (dt: f32) { mouse_raw := mouse_get_position_vector(); mouse_pos := mouse_raw - scene_render_offset; - if mouse_raw.x < ~~window_width - sidebar_width && mouse_pos.x >= 0 { + if mouse_raw.x < ~~window.width - sidebar_width && mouse_pos.x >= 0 { if !dragging && !resizing && (is_button_just_up(GLFW_MOUSE_BUTTON_LEFT) || is_button_just_up(GLFW_MOUSE_BUTTON_RIGHT)) { if active_tab == .Edit do active_index = -1; @@ -200,7 +202,7 @@ editor_draw :: () { { // Draw menu bar x := 0.0f; - w := cast(f32) window_width; + w := cast(f32) window.width; h := menubar_height; y := h * (editor_openness - 1); @@ -229,9 +231,9 @@ editor_draw :: () { { // Draw sidebar, if necessary sidebar_width = editor_openness * 400.0f; w := sidebar_width; - x := ~~ window_width - w; + x := ~~ window.width - w; y := 40.0f; - h := ~~ window_height - y; + h := ~~ window.height - y; immediate_set_color(background_color); immediate_rectangle(x, y, w, h); @@ -471,7 +473,7 @@ editor_draw :: () { w := sidebar_width / 2; h := 200.0f; - x := ~~ window_width - sidebar_width; + x := ~~ window.width - sidebar_width; render := render_field_editor; if custom_editors[it.type] != null_proc { diff --git a/src/entity/entities.onyx b/src/entity/entities.onyx index 8ee2960..7891c70 100644 --- a/src/entity/entities.onyx +++ b/src/entity/entities.onyx @@ -1,9 +1,10 @@ use core use glfw3 +use ogre -#tag Entity_Schematic.{ - (scene) => wall_create(scene, .{0,0}, .{0,0}) +@Entity_Schematic.{ + (scene: ^Scene) => wall_create(scene, .{0,0}, .{0,0}) } #local Wall :: struct { render :: (use this: ^Entity) { @@ -25,9 +26,9 @@ wall_create :: (scene: ^Scene, pos, size: Vector2) -> ^Entity { return this; } -#tag Entity_Schematic.{create} +@Entity_Schematic.{create} #local Door :: struct { - create :: (scene) => door_create(scene, .Zero, .Zero); + create :: (scene: ^Scene) => door_create(scene, .Zero, .Zero); render :: (use this: ^Entity) { immediate_set_color(.{0.7, 0.7, 0.1}); @@ -85,15 +86,3 @@ door_create :: (scene: ^Scene, pos, size: Vector2) -> ^Entity { return this; } - - -// @Relocate -move_towards :: (v: ^$T, target: T, diff: T) { - if math.abs(target - *v) <= diff { - *v = target; - return; - } - - if *v < target do *v += diff; - if *v > target do *v -= diff; -} diff --git a/src/entity/items.onyx b/src/entity/items.onyx index f084e94..a9afb9a 100644 --- a/src/entity/items.onyx +++ b/src/entity/items.onyx @@ -1,5 +1,7 @@ use core +use ogre +use ogre.ui Item :: struct { @@ -87,7 +89,7 @@ item_store_get_item :: (use this: ^Item_Store, id: str) -> ^Item { return items[id]; } -#tag Entity_Schematic.{create} +@Entity_Schematic.{create} #local Item_Entity :: struct { create :: (scene) => { @@ -118,7 +120,7 @@ item_store_get_item :: (use this: ^Item_Store, id: str) -> ^Item { ItemComponent :: struct { use base: Component; - #tag Editor_Custom_Field.{render_item_picker} + @Editor_Custom_Field.{render_item_picker} item: str; get_info :: (use this: ^ItemComponent) -> ^Item { diff --git a/src/entity/scene.onyx b/src/entity/scene.onyx index 5eb877c..af11f37 100644 --- a/src/entity/scene.onyx +++ b/src/entity/scene.onyx @@ -1,6 +1,7 @@ use core -use glfw3 +use ogre +use ogre.ui Entity_Nothing :: cast(Entity_ID) 0 Entity_ID :: #distinct u32 @@ -43,10 +44,10 @@ Entity_ID :: #distinct u32 } Component :: struct { - #tag Editor_Hidden, Entity_Store.Skip + @Editor_Hidden, Entity_Store.Skip use vtable: ^Component_Vtable; - #tag Editor_Hidden, Entity_Store.Skip + @Editor_Hidden, Entity_Store.Skip type: type_expr; } @@ -57,7 +58,7 @@ IsComponent :: interface (c: $C) { RenderComponent :: struct { use base: Component; - #tag Editor_Hidden, Entity_Store.Skip + @Editor_Hidden, Entity_Store.Skip func : (e: ^Entity) -> void; layer := 0; @@ -82,13 +83,13 @@ SpriteRenderComponent :: struct { } } -#tag Entity_Store.Skip +@Entity_Store.Skip SizeComponent :: struct { use base: Component; func : (e: ^Entity) -> Rect; } -#tag Entity_Store.Skip +@Entity_Store.Skip InteractableComponent :: struct { use base: Component; interact: (e: ^Entity, interactor: ^Entity) -> void; @@ -98,7 +99,7 @@ Entity :: struct { id: Entity_ID; flags: Entity_Flags; - #tag Entity_Store.Skip + @Entity_Store.Skip schematic: type_expr; nickname: str; @@ -113,7 +114,7 @@ Entity :: struct { return Rect.{ pos.x - size.x / 2, pos.y - size.y / 2, size.x, size.y }; }; - #tag Entity_Store.Skip, Editor_Hidden + @Entity_Store.Skip, Editor_Hidden components: Map(type_expr, ^Component); has :: (use this: ^Entity, component_type: type_expr) => components->has(component_type); diff --git a/src/entity/schematics/background.onyx b/src/entity/schematics/background.onyx index 8e87669..735bb54 100644 --- a/src/entity/schematics/background.onyx +++ b/src/entity/schematics/background.onyx @@ -3,7 +3,7 @@ use core // #local background_texture: Texture; -#tag Entity_Schematic.{ Background.create } +@Entity_Schematic.{ Background.create } Background :: struct { create :: (scene) => { this := scene->make(); diff --git a/src/entity/schematics/entryway.onyx b/src/entity/schematics/entryway.onyx index 7329eab..9b898d1 100644 --- a/src/entity/schematics/entryway.onyx +++ b/src/entity/schematics/entryway.onyx @@ -1,7 +1,7 @@ use core -#tag Entity_Schematic.{ Entryway.create } +@Entity_Schematic.{ Entryway.create } Entryway :: struct { create :: (scene) => { this := scene->make(); diff --git a/src/entity/schematics/furniture.onyx b/src/entity/schematics/furniture.onyx index 5362952..7fce57a 100644 --- a/src/entity/schematics/furniture.onyx +++ b/src/entity/schematics/furniture.onyx @@ -1,7 +1,8 @@ use core +use ogre -#tag Entity_Schematic.{ +@Entity_Schematic.{ (scene) => Furniture.create(scene, .{0, 0}) } Furniture :: struct { diff --git a/src/entity/schematics/patron.onyx b/src/entity/schematics/patron.onyx index 17d60bd..b6b1a63 100644 --- a/src/entity/schematics/patron.onyx +++ b/src/entity/schematics/patron.onyx @@ -1,8 +1,9 @@ use core +use ogre -#tag Entity_Schematic.{ - (scene) => Patron.create(scene, .{0,0}) +@Entity_Schematic.{ + (scene: ^Scene) => Patron.create(scene, .{0,0}) } Patron :: struct { create :: (scene: ^Scene, pos: Vector2) -> ^Entity { diff --git a/src/entity/schematics/player.onyx b/src/entity/schematics/player.onyx index b13432b..c71e367 100644 --- a/src/entity/schematics/player.onyx +++ b/src/entity/schematics/player.onyx @@ -1,6 +1,7 @@ use core use glfw3 +use ogre Player_Controls :: struct { up : i32; @@ -30,7 +31,7 @@ player_2_controls :: Player_Controls.{ } -#tag Entity_Schematic.{ +@Entity_Schematic.{ (scene: ^Scene) => Player.create(scene, .{0,0}) } Player :: struct { @@ -61,8 +62,8 @@ Player :: struct { } #persist assets: struct { - #tag "assets/images/player.png" - #tag Texture_Wrap.Clamp + @"assets/images/player.png" + @Texture_Wrap.Clamp texture: Texture; } } diff --git a/src/entity/schematics/tap.onyx b/src/entity/schematics/tap.onyx index 60baef6..95e619a 100644 --- a/src/entity/schematics/tap.onyx +++ b/src/entity/schematics/tap.onyx @@ -1,5 +1,6 @@ +use ogre -#tag Entity_Schematic.{ +@Entity_Schematic.{ (scene) => Tap.create(scene, .{0,0}, .{0,0}) } Tap :: struct { diff --git a/src/game.onyx b/src/game.onyx index 427df1c..f798106 100644 --- a/src/game.onyx +++ b/src/game.onyx @@ -2,6 +2,7 @@ use core use glfw3 use opengles +use ogre // // Game Global Variables @@ -88,23 +89,23 @@ game_draw :: () { texture_wrap(^texture, .Clamp); view_rect: Rect; if !editor_shown() { - view_rect = Rect.{0, 0, ~~window_width, ~~window_height}; + view_rect = Rect.{0, 0, ~~window.width, ~~window.height}; } else { // view_rect.x = math.lerp(editor_open_percent(), cast(f32) ((window_width - scene_canvas.width) / 2), 0); - view_rect.x = math.lerp(editor_open_percent(), cast(f32) ((window_width - scene_canvas.width) / 2), cast(f32) ((window_width - scene_canvas.width - 400) / 2)); - view_rect.y = ~~ ((window_height - scene_canvas.height) / 2); + view_rect.x = math.lerp(editor_open_percent(), cast(f32) ((window.width - scene_canvas.width) / 2), cast(f32) ((window.width - scene_canvas.width - 400) / 2)); + view_rect.y = ~~ ((window.height - scene_canvas.height) / 2); view_rect.w = ~~ scene_canvas.width; view_rect.h = ~~ scene_canvas.height; } scene_render_offset = Rect.top_left(view_rect); glDisable(GL_CULL_FACE); - immediate_image(^texture, view_rect.x, ~~window_height - view_rect.y, view_rect.w, -view_rect.h); + immediate_image(^texture, view_rect.x, ~~window.height - view_rect.y, view_rect.w, -view_rect.h); if distortion_enabled { shader_use(distortion_shader); shader_set_uniform(distortion_shader, #cstr "u_texture", 0); shader_set_uniform(distortion_shader, #cstr "u_tex_size", Vector2.{800, 608}); - shader_set_uniform(distortion_shader, #cstr "u_output_size", Vector2.{~~window_width, ~~window_height}); + shader_set_uniform(distortion_shader, #cstr "u_output_size", Vector2.{~~window.width, ~~window.height}); immediate_flush(false); } else { immediate_flush(); diff --git a/src/main.onyx b/src/main.onyx index d538d19..05c80aa 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -2,29 +2,17 @@ use core use opengles use glfw3 +use ogre DEBUG :: #defined(runtime.vars.DEBUG) -// @GlobalVariable -window: GLFWwindow_p - -// @GlobalVariable -window_width: u32 -window_height: u32 +window: Window #if DEBUG { debug_font: Font; } init :: () { - window = create_window(); - input_bind_glfw_events(window); - glInit(glfwGetLoadProcAddress()); - - glEnable(GL_TEXTURE); - glEnable(GL_CULL_FACE); - glFrontFace(GL_CW); - glCullFace(GL_BACK); - glEnable(GL_BLEND); - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + window = window_create(1200, 900, #cstr "Bar Simulator"); + window_use(^window); shaders_init(); fonts_init(); @@ -32,9 +20,6 @@ init :: () { editor_init(); game_init(); - glfwGetWindowSize(window, ^window_width, ^window_height); - update_view_matrix(window_width, window_height); - #if DEBUG { debug_font = font_lookup(.{"./assets/fonts/calibri.ttf", 16}); } } @@ -46,7 +31,7 @@ update :: (dt: f32) { input_update(); if is_key_just_up(GLFW_KEY_ESCAPE) { - glfwSetWindowShouldClose(window, true); + glfwSetWindowShouldClose(window.glfw_window, true); return; } @@ -59,7 +44,7 @@ update :: (dt: f32) { draw :: () { immediate_clear(.{0.15, 0.15, 0.2}); - defer ui_end_frame(); + defer ui.ui_end_frame(); defer input_post_update(); defer { immediate_flush(); @@ -73,10 +58,10 @@ draw :: () { version_buf : [32] u8; version_str := conv.format(version_buf, "Version: {}.{}", runtime.vars.MAJOR_VERSION, runtime.vars.MINOR_VERSION); - font_print(debug_font, ~~window_width - font_get_width(debug_font, version_str), 16, version_str); + font_print(debug_font, ~~window.width - font_get_width(debug_font, version_str), 16, version_str); } - glfwSwapBuffers(window); + glfwSwapBuffers(window.glfw_window); } game_draw(); @@ -100,7 +85,7 @@ run :: () { seconds := 0.0; frame_count := 0; - while !glfwWindowShouldClose(window) { + while !glfwWindowShouldClose(window.glfw_window) { glfwPollEvents(); now = glfwGetTime(); @@ -121,29 +106,6 @@ run :: () { } } -create_window :: () => { - #if runtime.compiler_os == .Linux { - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); - glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); - } else { - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); - } - window := glfwCreateWindow(1200, 900, #cstr "Bar simulator"); - - glfwMakeContextCurrent(window); - glfwSwapInterval(1); - glfwSetWindowSizeCallback(window, "on_resize"); - return window; -} - -#export "on_resize" (window: GLFWwindow_p, width, height: u32) { - window_width = width; - window_height = height; - update_view_matrix(width, height); -} - main :: () { random.set_seed(os.time()); diff --git a/src/gfx/canvas.onyx b/src/ogre/canvas.onyx similarity index 93% rename from src/gfx/canvas.onyx rename to src/ogre/canvas.onyx index c8743d3..7e85764 100644 --- a/src/gfx/canvas.onyx +++ b/src/ogre/canvas.onyx @@ -1,3 +1,4 @@ +package ogre use core use opengles @@ -34,7 +35,7 @@ canvas_make :: (width, height: i32) -> Canvas { glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, canvas.depth_stencil_buffer); if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE { - debug_log(.Error, "Framebuffer is not complete!"); + printf("[ERROR] Framebuffer is not complete!\n"); } glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -50,7 +51,7 @@ canvas_free :: (use canvas: ^Canvas) { canvas_use :: (use canvas: ^Canvas) { if canvas == null { glBindFramebuffer(GL_FRAMEBUFFER, 0); - update_view_matrix(window_width, window_height); + update_view_matrix(global_window.width, global_window.height); } else { glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); update_view_matrix(width, height); diff --git a/src/utils/colors.onyx b/src/ogre/colors.onyx similarity index 98% rename from src/utils/colors.onyx rename to src/ogre/colors.onyx index ab6231d..9a7cceb 100644 --- a/src/utils/colors.onyx +++ b/src/ogre/colors.onyx @@ -1,5 +1,6 @@ +package ogre -use package core +use core Color :: struct { r, g, b : f32; diff --git a/src/gfx/font.onyx b/src/ogre/font.onyx similarity index 99% rename from src/gfx/font.onyx rename to src/ogre/font.onyx index 1ece709..9a9f090 100644 --- a/src/gfx/font.onyx +++ b/src/ogre/font.onyx @@ -1,3 +1,4 @@ +package ogre use core use stb_truetype diff --git a/src/gfx/immediate.onyx b/src/ogre/immediate.onyx similarity index 94% rename from src/gfx/immediate.onyx rename to src/ogre/immediate.onyx index 8a88eca..0cd0505 100644 --- a/src/gfx/immediate.onyx +++ b/src/ogre/immediate.onyx @@ -1,3 +1,4 @@ +package ogre use core use opengles @@ -140,7 +141,7 @@ immediate_push_scissor :: (x, y, w, h: f32) { scissors << .{xi, yi, wi, hi}; glEnable(GL_SCISSOR_TEST); - glScissor(xi, window_height - yi - hi, wi, hi); + glScissor(xi, global_window.height - yi - hi, wi, hi); } immediate_pop_scissor :: () { @@ -151,7 +152,7 @@ immediate_pop_scissor :: () { if scissors.count > 0 { glEnable(GL_SCISSOR_TEST); s := scissors[scissors.count - 1]; - glScissor(s.x, window_height - s.y - s.h, s.w, s.h); + glScissor(s.x, global_window.height - s.y - s.h, s.w, s.h); } else { glDisable(GL_SCISSOR_TEST); } @@ -203,3 +204,16 @@ Immediate_Vertex :: struct { } } } + + + +// @Relocate +move_towards :: (v: ^$T, target: T, diff: T) { + if math.abs(target - *v) <= diff { + *v = target; + return; + } + + if *v < target do *v += diff; + if *v > target do *v -= diff; +} diff --git a/src/utils/input.onyx b/src/ogre/input.onyx similarity index 84% rename from src/utils/input.onyx rename to src/ogre/input.onyx index 31a9022..38d656e 100644 --- a/src/utils/input.onyx +++ b/src/ogre/input.onyx @@ -1,3 +1,5 @@ +package ogre + use package core use package glfw3 @@ -37,7 +39,7 @@ Key_Descriptor :: struct { #operator == (x, y: Key_Descriptor) => x.key == y.key; input_update :: () { - glfwGetCursorPos(window, ^mouse_x, ^mouse_y); + glfwGetCursorPos(global_window.glfw_window, ^mouse_x, ^mouse_y); } input_post_update :: () { @@ -139,20 +141,14 @@ mouse_get_scroll_vector :: () -> Vector2 { } input_bind_glfw_events :: (window: GLFWwindow_p) { - glfwSetKeyCallback(window, INPUT_KEY_EVENT); - glfwSetCharCallback(window, INPUT_CHAR_EVENT); - glfwSetMouseButtonCallback(window, INPUT_BUTTON_EVENT); - glfwSetScrollCallback(window, INPUT_SCROLL_EVENT); -} - -#local { - INPUT_BUTTON_EVENT :: "__input_button_event" - INPUT_KEY_EVENT :: "__input_key_event" - INPUT_SCROLL_EVENT :: "__input_scroll_event" - INPUT_CHAR_EVENT :: "__input_char_event" + glfwSetKeyCallback(window, #export_name input_key_event); + glfwSetCharCallback(window, #export_name input_char_event); + glfwSetMouseButtonCallback(window, #export_name input_button_event); + glfwSetScrollCallback(window, #export_name input_scroll_event); } -#export INPUT_BUTTON_EVENT (window: GLFWwindow_p, button, action, mod: u32) { +#local +input_button_event :: (window: GLFWwindow_p, button, action, mod: u32) { if action == GLFW_PRESS { buttons_this_frame[button] = true; } @@ -162,7 +158,8 @@ input_bind_glfw_events :: (window: GLFWwindow_p) { } } -#export INPUT_KEY_EVENT (window: GLFWwindow_p, key, scancode, action, mod: u32) { +#local +input_key_event :: (window: GLFWwindow_p, key, scancode, action, mod: u32) { if action == GLFW_PRESS { keys_this_frame << .{ key, scancode, mod }; } @@ -176,12 +173,14 @@ input_bind_glfw_events :: (window: GLFWwindow_p) { } } -#export INPUT_CHAR_EVENT (window: GLFWwindow_p, codepoint: u32) { +#local +input_char_event :: (window: GLFWwindow_p, codepoint: u32) { if !character_mode do return; key_codepoints << codepoint; } -#export INPUT_SCROLL_EVENT (window: GLFWwindow_p, xoff, yoff: f64) { +#local +input_scroll_event :: (window: GLFWwindow_p, xoff, yoff: f64) { scroll_x = xoff; scroll_y = yoff; } diff --git a/src/gfx/mesh.onyx b/src/ogre/mesh.onyx similarity index 99% rename from src/gfx/mesh.onyx rename to src/ogre/mesh.onyx index 981ff11..ba926aa 100644 --- a/src/gfx/mesh.onyx +++ b/src/ogre/mesh.onyx @@ -1,3 +1,5 @@ +package ogre + use core use opengles diff --git a/src/gfx/shader.onyx b/src/ogre/shader.onyx similarity index 99% rename from src/gfx/shader.onyx rename to src/ogre/shader.onyx index 711c6a0..671e583 100644 --- a/src/gfx/shader.onyx +++ b/src/ogre/shader.onyx @@ -1,3 +1,4 @@ +package ogre use core use opengles diff --git a/src/gfx/texture.onyx b/src/ogre/texture.onyx similarity index 94% rename from src/gfx/texture.onyx rename to src/ogre/texture.onyx index 5a205d4..64773cc 100644 --- a/src/gfx/texture.onyx +++ b/src/ogre/texture.onyx @@ -1,3 +1,4 @@ +package ogre use core use opengles @@ -32,7 +33,7 @@ texture_lookup :: #match {} tex.filename = filename; pixels := stbi_load(path, ^tex.width, ^tex.height, ^tex.channels, 4); if pixels == null { - debug_log(.Warning, "Failed to load texture: {}", filename); + printf("[WARN ] Failed to load texture: {}\n", filename); return .{}, false; } defer stbi_image_free(pixels); @@ -52,7 +53,7 @@ texture_lookup :: #match {} // This assumes that the filename data is going to be persistant forever. // Not a great assumption to make but is it really worth copying it? texture_cache[filename] = tex; - debug_log(.Info, "Loaded texture: {}", filename); + printf("[INFO ] Loaded texture: {}\n", filename); return tex, true; } diff --git a/src/gfx/ui.onyx b/src/ogre/ui.onyx similarity index 99% rename from src/gfx/ui.onyx rename to src/ogre/ui.onyx index ae31eaa..1242f11 100644 --- a/src/gfx/ui.onyx +++ b/src/ogre/ui.onyx @@ -1,3 +1,5 @@ +package ogre.ui + // // Very simple immediate mode UI // @@ -5,6 +7,7 @@ use core use opengles use glfw3 +use ogre UI_Id :: u32 @@ -586,7 +589,7 @@ draw_radio :: (use r: Rect, value: ^$T, set_to: T, text: str, theme := ^default_ scrolling_region_start :: (r: Rect, max_y_scroll := 10000.0f, site := #callsite, increment := 0) { hash := get_site_hash(site, increment); mx, my := mouse_get_position(); - state := map.get_ptr(^scroll_states, hash); + state := ^scroll_states[hash]; if state == null { animation_states[hash] = .{}; state = ^scroll_states[hash]; @@ -684,7 +687,7 @@ scrolling_region_stop :: () { animation_states : Map(UI_Id, Animation_State); get_animation :: (id: UI_Id) -> ^Animation_State { - retval := map.get_ptr(^animation_states, id); + retval := ^animation_states[id]; if retval == null { animation_states[id] = .{}; retval = ^animation_states[id]; @@ -711,7 +714,7 @@ scrolling_region_stop :: () { scroll_states : Map(UI_Id, Scroll_State); - get_site_hash :: macro (site: CallSite, increment := 0) -> UI_Id { + get_site_hash :: (site: CallSite, increment := 0) -> UI_Id { file_hash := core.hash.to_u32(site.file); line_hash := core.hash.to_u32(site.line); column_hash := core.hash.to_u32(site.column); diff --git a/src/utils/vecmath.onyx b/src/ogre/vecmath.onyx similarity index 77% rename from src/utils/vecmath.onyx rename to src/ogre/vecmath.onyx index 83e177f..583dd2c 100644 --- a/src/utils/vecmath.onyx +++ b/src/ogre/vecmath.onyx @@ -1,15 +1,16 @@ +package ogre -use core {hash, math} +use core {hash, math, conv, string} -#tag conv.Custom_Format.{format_vector2i} -#tag conv.Custom_Parse.{parse_vector2i} +@conv.Custom_Format.{format_vector2i} +@conv.Custom_Parse.{parse_vector2i} Vector2i :: struct { x, y: i32; } -#tag conv.Custom_Format.{format_vector2} -#tag conv.Custom_Parse.{parse_vector2} +@conv.Custom_Format.{format_vector2} +@conv.Custom_Parse.{parse_vector2} Vector2 :: struct { x, y: f32; @@ -26,12 +27,12 @@ Vector2 :: struct { Zero :: Vector2.{0, 0} } -#tag conv.Custom_Format.{format_vector3i} +@conv.Custom_Format.{format_vector3i} Vector3i :: struct { x, y, z: i32; } -#tag conv.Custom_Format.{format_vector3} +@conv.Custom_Format.{format_vector3} Vector3 :: struct { x, y, z: f32; @@ -65,34 +66,32 @@ Vector3 :: struct { } } -#operator + (v1, v2: Vector2i) => Vector2i.{ v1.x + v2.x, v1.y + v2.y }; -#operator - (v1, v2: Vector2i) => Vector2i.{ v1.x - v2.x, v1.y - v2.y }; +#operator + (v1, v2: Vector2i) => Vector2i.{ v1.x + v2.x, v1.y + v2.y }; +#operator - (v1, v2: Vector2i) => Vector2i.{ v1.x - v2.x, v1.y - v2.y }; #operator * (v: Vector2i, s: i32) => Vector2i.{ v.x * s, v.y * s }; -#operator * (v1, v2: Vector2i) => Vector2i.{ v1.x * v2.x, v1.y * v2.y }; -#operator == (v1, v2: Vector2i) => v1.x == v2.x && v1.y == v2.y; -#match hash.to_u32 (v: Vector2i) => 13 * v.x + 17 * v.y; - -#operator + (v1, v2: Vector2) => (typeof v1).{ v1.x + v2.x, v1.y + v2.y }; -#operator - (v1, v2: Vector2) => (typeof v1).{ v1.x - v2.x, v1.y - v2.y }; -#operator * (v: Vector2, s: f32) => (typeof v ).{ v.x * s, v.y * s }; -#operator * (v1, v2: Vector2) => (typeof v1).{ v1.x * v2.x, v1.y * v2.y }; +#operator * (v1, v2: Vector2i) => Vector2i.{ v1.x * v2.x, v1.y * v2.y }; +#operator == (v1, v2: Vector2i) => v1.x == v2.x && v1.y == v2.y; +#match hash.to_u32 (v: Vector2i) => 13 * v.x + 17 * v.y; + +#operator + (v1, v2: Vector2) => Vector2.{ v1.x + v2.x, v1.y + v2.y }; +#operator - (v1, v2: Vector2) => Vector2.{ v1.x - v2.x, v1.y - v2.y }; +#operator * (v: Vector2, s: f32) => Vector2.{ v.x * s, v.y * s }; +#operator * (v1, v2: Vector2) => Vector2.{ v1.x * v2.x, v1.y * v2.y }; #operator == (v1, v2: Vector2) => v1.x == v2.x && v1.y == v2.y; #operator + (v1, v2: Vector3) => Vector3.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; #operator - (v1, v2: Vector3) => Vector3.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; #operator * (v: Vector3, s: f32) => Vector3.{ v.x * s, v.y * s, v.z * s }; -#operator * (v1, v2: Vector3) => (typeof v1).{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; +#operator * (v1, v2: Vector3) => Vector3.{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; #operator == (v1, v2: Vector3) => v1.x == v2.x && v1.y == v2.y && v1.z == v2.z; #operator + (v1, v2: Vector3i) => Vector3i.{ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; #operator - (v1, v2: Vector3i) => Vector3i.{ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; #operator * (v: Vector3i, s: i32) => Vector3i.{ v.x * s, v.y * s, v.z * s }; -#operator * (v1, v2: Vector3i) => (typeof v1).{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; +#operator * (v1, v2: Vector3i) => Vector3i.{ v1.x * v2.x, v1.y * v2.y, v1.z * v2.z }; #operator == (v1, v2: Vector3i) => v1.x == v2.x && v1.y == v2.y && v1.z == v2.z; #local { - conv :: package core.conv - format_vector2i :: (output: ^conv.Format_Output, format: ^conv.Format, v: ^Vector2i) { conv.format(output, "({}, {})", v.x, v.y); } @@ -110,8 +109,6 @@ Vector3 :: struct { } parse_vector2i :: (output: ^Vector2i, line_: str, string_allocator: Allocator) -> bool { - string :: package core.string - line := line_; xs := string.read_until(^line, #char " "); string.advance(^line, 1); @@ -125,8 +122,6 @@ Vector3 :: struct { } parse_vector2 :: (output: ^Vector2, line_: str, string_allocator: Allocator) -> bool { - string :: package core.string - line := line_; xs := string.read_until(^line, #char " "); string.advance(^line, 1); diff --git a/src/ogre/window.onyx b/src/ogre/window.onyx new file mode 100644 index 0000000..486cf37 --- /dev/null +++ b/src/ogre/window.onyx @@ -0,0 +1,67 @@ +package ogre + +use glfw3 +use opengles + +Window :: struct { + glfw_window: GLFWwindow_p; + width, height: i32; +} + +window_create :: (width, height: i32, name: cstr) -> Window { + #if runtime.compiler_os == .Linux { + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); + } else { + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + } + + w := glfwCreateWindow(width, height, name); + + return .{ w, width, height }; +} + +window_use :: (w: ^Window) { + glfwMakeContextCurrent(w.glfw_window); + glfwSwapInterval(1); + + input_bind_glfw_events(w.glfw_window); + + // Provide sensible defaults for OpenGL + glInit(glfwGetLoadProcAddress()); + glEnable(GL_TEXTURE); + glEnable(GL_CULL_FACE); + glFrontFace(GL_CW); + glCullFace(GL_BACK); + glEnable(GL_BLEND); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + + glfwGetWindowSize(w.glfw_window, ^w.width, ^w.height); + update_view_matrix(w.width, w.height); + + glfwSetWindowSizeCallback(w.glfw_window, #export_name (w: GLFWwindow_p, width, height: u32) { + global_window.width = width; + global_window.height = height; + update_view_matrix(width, height); + }); + + global_window = w; +} + +// +// These have to be macros because '#export_name' is used on the argument, +// which requires that they are compile time known functions. +// +// window_size_callback :: macro (w: ^Window, resize: (w: GLFWwindow_p, width, height: u32) -> void) { +// glfw3.glfwSetWindowSizeCallback(w.glfw_window, #export_name resize); +// } + + + + + +#package +global_window: ^Window; + diff --git a/src/sfx/audio_manager.onyx b/src/sfx/audio_manager.onyx index 970ec85..9398137 100644 --- a/src/sfx/audio_manager.onyx +++ b/src/sfx/audio_manager.onyx @@ -55,7 +55,7 @@ Audio_Manager :: struct { } alBufferData(buffer, al_format, wav_file.data.data, wav_file.data.count, wav_file.sample_rate); - memory.free_slice(^wav_file.loaded_file_data); + delete(^wav_file.loaded_file_data); loaded_sounds[path] = .{ buffer }; return .{ buffer }; @@ -78,15 +78,13 @@ Audio_Manager :: struct { } tick :: () { - while i := 0; i < playing_sounds.count { - defer i += 1; - + for it: iter.as_iterator(^playing_sounds) { state: i32; - alGetSourcei(playing_sounds[i], AL_SOURCE_STATE, ^state); + alGetSourcei(it, AL_SOURCE_STATE, ^state); + if state != AL_PLAYING { - alDeleteSources(1, ^playing_sounds[i]); - array.fast_delete(^playing_sounds, i); - i -= 1; + alDeleteSources(1, ^it); + #remove; } } } diff --git a/src/utils/asset_loader.onyx b/src/utils/asset_loader.onyx index 72abc95..ec5f756 100644 --- a/src/utils/asset_loader.onyx +++ b/src/utils/asset_loader.onyx @@ -1,5 +1,6 @@ -use package core +use core +use ogre queue_assets :: (store: ^$T, callsite := #callsite) { assets_to_load << .{ T, store, callsite }; -- 2.25.1