small refactorings; starting tile rendering
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 3 Oct 2020 04:10:52 +0000 (23:10 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 3 Oct 2020 04:10:52 +0000 (23:10 -0500)
src/gfx/quad_renderer.onyx
src/globals.onyx [new file with mode: 0644]
src/input.onyx
src/main.onyx
tags

index 993d1917e63fa99d6b0d3bed6eff9b037d55a55d..258d83d1cd8291cd4577bc21d48f1df8defe2711 100644 (file)
@@ -4,7 +4,7 @@ use package core
 use package gl as gl
 use package gl_utils as gl_utils
 use package vecmath
-use package main { half_window_width, half_window_height }
+use package globals
 
 #private_file
 quad_program : gl.GLProgram = -1;
diff --git a/src/globals.onyx b/src/globals.onyx
new file mode 100644 (file)
index 0000000..2edc82a
--- /dev/null
@@ -0,0 +1,20 @@
+package globals
+
+use package input as input
+use package main
+use package gfx
+use package quad_tree
+
+window_width  := 0
+window_height := 0
+half_window_width  := 0
+half_window_height := 0
+
+renderer      : RenderContext
+input_state   : input.InputState
+atlas         : Atlas
+
+dudes         : [..] Dude
+dude_tree     : QuadTree(Dude)
+
+tilemap       : Tilemap
index 35e72af1457457552ddb1f2919402161de248ce0..5b26b4e3b11c95aeae30f888b5715cb41cf34f8d 100644 (file)
@@ -2,7 +2,7 @@ package input
 
 use package event as event
 use package core { print }
-use package main { half_window_width, half_window_height }
+use package globals
 
 #private_file KEY_COUNT :: 256
 #private_file BUTTON_COUNT :: 3
index 6f0b4a0899fd9968b501996179069013412a86b0..d6ac236feadbc097549a8cafad5ecf30e049dbe1 100644 (file)
@@ -14,6 +14,7 @@ package main
 #include_file "vecmath"
 #include_file "aabb"
 #include_file "quad_tree"
+#include_file "globals"
 
 use package core
 use package gfx
@@ -21,12 +22,12 @@ use package gl as gl
 use package event as event
 use package input as input { Key }
 use package vecmath
-use package ttf_font
+use package globals
 
 use package quad_tree
 use package aabb
 
-NUM_QUADS :: 1 << 14
+NUM_QUADS :: 1 << 17
 
 RenderContext :: struct {
     quad_renderer: ^QuadRenderer;
@@ -100,18 +101,6 @@ render_context_flush :: proc (use rc: ^RenderContext) {
 
 
 
-// @Cleanup
-window_width  := 0
-window_height := 0
-half_window_width  := 0
-half_window_height := 0
-
-renderer      : RenderContext
-input_state   : input.InputState
-atlas         : Atlas
-dudes         : [..] Dude
-dude_tree     : QuadTree(Dude)
-
 poll_events :: proc () {
     use event.DomEventKind;
 
@@ -144,10 +133,10 @@ update :: proc () {
 
     if input.key_just_down(^input_state, Key.Space) do simulating = !simulating;
 
-    if input.key_down(^input_state, Key.ArrowUp)    do renderer.trans_y += 10.0f;
-    if input.key_down(^input_state, Key.ArrowDown)  do renderer.trans_y -= 10.0f;
-    if input.key_down(^input_state, Key.ArrowLeft)  do renderer.trans_x += 10.0f;
-    if input.key_down(^input_state, Key.ArrowRight) do renderer.trans_x -= 10.0f;
+    if input.key_down(^input_state, Key.ArrowUp)    do renderer.trans_y += 15.0f / renderer.scale;
+    if input.key_down(^input_state, Key.ArrowDown)  do renderer.trans_y -= 15.0f / renderer.scale;
+    if input.key_down(^input_state, Key.ArrowLeft)  do renderer.trans_x += 15.0f / renderer.scale;
+    if input.key_down(^input_state, Key.ArrowRight) do renderer.trans_x -= 15.0f / renderer.scale;
 
     if input_state.mouse.wheel_ups > ~~0   do renderer.scale *= 1.125f;
     if input_state.mouse.wheel_downs > ~~0 do renderer.scale /= 1.125f;
@@ -177,7 +166,7 @@ draw :: proc () {
 
     texture_use(^textures.tilemap);
 
-    // draw_quad_tree(^dude_tree);
+    tilemap_draw(^tilemap, ^renderer);
 
     dude_sprite := atlas_lookup(^atlas, 0);
     for ^d: dudes {
@@ -193,28 +182,6 @@ draw :: proc () {
     render_context_flush(^renderer);
 }
 
-draw_quad_tree :: proc (qt: ^QuadTree($T)) {
-    col := cast(f32) (0.0f + ~~ qt.points_count * 0.25f);
-    if col > 1.0f do col = 1.0f;
-
-    color := Color4f32.{ col, col, col, 1.0f };
-
-    quad := Quad.{
-        pos  = V2f.{ qt.region.x, qt.region.y },
-        size = V2f.{ qt.region.w, qt.region.h },
-        color = color,
-
-        tex_pos = V2f.{ -1.0f, -1.0f },
-    };
-    draw_quad(^renderer, ^quad);
-
-    if qt.nw != null {
-        draw_quad_tree(qt.nw);
-        draw_quad_tree(qt.ne);
-        draw_quad_tree(qt.sw);
-        draw_quad_tree(qt.se);
-    }
-}
 
 debugger :: proc () #foreign "dummy" "breakable" ---
 
@@ -256,6 +223,8 @@ main :: proc (args: [] cstring) {
         array_push(^dudes, dude_create_random());
     }
 
+    tilemap_init(^tilemap, 150, 80);
+
     game_launch :: proc () #foreign "game" "launch" ---;
     game_launch();
 }
@@ -341,3 +310,83 @@ dude_try_move :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) {
 dude_get_aabb :: proc (use dude: ^Dude) -> AABB {
     return AABB.{ x = pos.x - size, y = pos.y - size, w = size * 2.0f, h = size * 2.0f };
 }
+
+TILE_SIZE :: 10.0f;
+
+Tilemap :: struct {
+    width  : u32;
+    height : u32;
+
+    tiles : [] Tile;
+}
+
+Tile :: struct {
+    r : u8 = ~~0;
+    g : u8 = ~~0;
+    b : u8 = ~~0;
+}
+
+tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) {
+    width = w;
+    height = h;
+    alloc_slice(^tiles, width * height);
+
+    for ^t: tiles {
+        t.r = ~~random_between(0, 255);
+        t.g = ~~random_between(0, 255);
+        t.b = ~~random_between(0, 255);
+    }
+}
+
+tilemap_free :: proc (use tm: ^Tilemap) {
+    cfree(tiles.data);
+    width = 0;
+    height = 0;
+}
+
+tilemap_draw :: proc (use tm: ^Tilemap, renderer: ^RenderContext) {
+    for y: 0 .. height do for x: 0 .. width {
+        renderer.color = tile_to_color4f32(tiles[y * width + x]);
+        draw_rect(renderer, ~~x * TILE_SIZE, ~~y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
+    }
+}
+
+tilemap_get_tile :: proc (use tm: ^Tilemap, x: u32, y: u32) -> Tile {
+    if x < 0 || y < 0 || x >= width || y >= height do return Tile.{};
+    return tiles[x + y * width];
+}
+
+tile_to_color4f32 :: proc (t: Tile) -> Color4f32 {
+    return Color4f32.{
+        r = ~~cast(u32) t.r / 255.0f,
+        g = ~~cast(u32) t.g / 255.0f,
+        b = ~~cast(u32) t.b / 255.0f,
+        a = 1.0f,
+    };
+}
+
+
+/*
+draw_quad_tree :: proc (qt: ^QuadTree($T)) {
+    col := cast(f32) (0.0f + ~~ qt.points_count * 0.25f);
+    if col > 1.0f do col = 1.0f;
+
+    color := Color4f32.{ col, col, col, 1.0f };
+
+    quad := Quad.{
+        pos  = V2f.{ qt.region.x, qt.region.y },
+        size = V2f.{ qt.region.w, qt.region.h },
+        color = color,
+
+        tex_pos = V2f.{ -1.0f, -1.0f },
+    };
+    draw_quad(^renderer, ^quad);
+
+    if qt.nw != null {
+        draw_quad_tree(qt.nw);
+        draw_quad_tree(qt.ne);
+        draw_quad_tree(qt.sw);
+        draw_quad_tree(qt.se);
+    }
+}
+*/
diff --git a/tags b/tags
index 19bbc0a86c9a7e792b0452aac7f33df7f9493082..bbdf9a0ee27e4463ac33f97c5504b26186059a13 100644 (file)
--- a/tags
+++ b/tags
@@ -313,7 +313,7 @@ NICEST      /usr/share/onyx/core/js/webgl.onyx      /^NICEST                         :: 0x
 NONE   /usr/share/onyx/core/js/webgl.onyx      /^NONE                           :: 0$/
 NOTEQUAL       /usr/share/onyx/core/js/webgl.onyx      /^NOTEQUAL                       :: 0x0205$/
 NO_ERROR       /usr/share/onyx/core/js/webgl.onyx      /^NO_ERROR                       :: 0$/
-NUM_QUADS      src/main.onyx   /^NUM_QUADS :: 1 << 14$/
+NUM_QUADS      src/main.onyx   /^NUM_QUADS :: 1 << 17$/
 OBJECT_TYPE    /usr/share/onyx/core/js/webgl.onyx      /^OBJECT_TYPE                                   :: 0x9112$/
 ONE    /usr/share/onyx/core/js/webgl.onyx      /^ONE                            :: 1$/
 ONE_MINUS_CONSTANT_ALPHA       /usr/share/onyx/core/js/webgl.onyx      /^ONE_MINUS_CONSTANT_ALPHA       :: 0x8004$/
@@ -546,6 +546,7 @@ TEXTURE_MIN_LOD     /usr/share/onyx/core/js/webgl.onyx      /^TEXTURE_MIN_LOD
 TEXTURE_WRAP_R /usr/share/onyx/core/js/webgl.onyx      /^TEXTURE_WRAP_R                                :: 0x8072$/
 TEXTURE_WRAP_S /usr/share/onyx/core/js/webgl.onyx      /^TEXTURE_WRAP_S                 :: 0x2802$/
 TEXTURE_WRAP_T /usr/share/onyx/core/js/webgl.onyx      /^TEXTURE_WRAP_T                 :: 0x2803$/
+TILE_SIZE      src/main.onyx   /^TILE_SIZE :: 10.0f;$/
 TIMEOUT_EXPIRED        /usr/share/onyx/core/js/webgl.onyx      /^TIMEOUT_EXPIRED                               :: 0x911B$/
 TIMEOUT_IGNORED        /usr/share/onyx/core/js/webgl.onyx      /^TIMEOUT_IGNORED                               :: -1$/
 TRANSFORM_FEEDBACK     /usr/share/onyx/core/js/webgl.onyx      /^TRANSFORM_FEEDBACK                            :: 0x8E22$/
@@ -575,6 +576,8 @@ TTSegment   src/font.onyx   /^TTSegment :: struct {$/
 TTTableInfo    src/font.onyx   /^TTTableInfo :: struct {$/
 Texture        src/gfx/texture.onyx    /^Texture :: struct {$/
 TextureDataMode        src/gfx/texture.onyx    /^TextureDataMode :: enum {$/
+Tile   src/main.onyx   /^Tile :: struct {$/
+Tilemap        src/main.onyx   /^Tilemap :: struct {$/
 TrueTypeFont   src/font.onyx   /^TrueTypeFont :: struct {$/
 UNIFORM_ARRAY_STRIDE   /usr/share/onyx/core/js/webgl.onyx      /^UNIFORM_ARRAY_STRIDE                          :: 0x8A3C$/
 UNIFORM_BLOCK_ACTIVE_UNIFORMS  /usr/share/onyx/core/js/webgl.onyx      /^UNIFORM_BLOCK_ACTIVE_UNIFORMS                 :: 0x8A42$/
@@ -668,7 +671,7 @@ array_push  /usr/share/onyx/core/array.onyx /^array_push :: proc (arr: ^[..] $T,
 array_remove   /usr/share/onyx/core/array.onyx /^array_remove :: proc (arr: ^[..] $T, elem: T) {$/
 array_sort     /usr/share/onyx/core/array.onyx /^array_sort :: proc (arr: ^[..] $T, cmp: proc (T, T) -> i32) {$/
 array_to_slice /usr/share/onyx/core/array.onyx /^array_to_slice :: proc (arr: ^[..] $T) -> [] T {$/
-atlas  src/main.onyx   /^atlas         : Atlas$/
+atlas  src/globals.onyx        /^atlas         : Atlas$/
 atlas_apply_to_quad    src/gfx/atlas.onyx      /^atlas_apply_to_quad :: proc (use atlas: ^Atlas, id: i32, quad: ^Quad) {$/
 atlas_create   src/gfx/atlas.onyx      /^atlas_create :: proc (tex: ^Texture) -> Atlas {$/
 atlas_lookup   src/gfx/atlas.onyx      /^atlas_lookup :: proc (use atlas: ^Atlas, id: i32) -> AtlasSprite {$/
@@ -751,15 +754,14 @@ drawArraysInstanced       /usr/share/onyx/core/js/webgl.onyx      /^drawArraysInstanced
 drawElements   /usr/share/onyx/core/js/webgl.onyx      /^drawElements                   :: proc (mode: GLenum, count: GLsizei, type: GLenum, offset: GLint) #foreign "gl" "drawElements" ---$/
 drawElementsInstanced  /usr/share/onyx/core/js/webgl.onyx      /^drawElementsInstanced          :: proc (mode: GLenum, count: GLsizei, type: GLenum, offset: GLint, instanceCount: GLsizei) #foreign "gl" "drawElementsInstanced" ---$/
 draw_quad      src/main.onyx   /^draw_quad :: proc (use rc: ^RenderContext, quad: ^Quad) {$/
-draw_quad_tree src/main.onyx   /^draw_quad_tree :: proc (qt: ^QuadTree($T)) {$/
 draw_rect      src/main.onyx   /^draw_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32) {$/
 draw_textured_rect     src/main.onyx   /^draw_textured_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32, tx: f32, ty: f32, tw: f32, th: f32) {$/
 dude_create_random     src/main.onyx   /^dude_create_random :: proc () -> Dude {$/
 dude_get_aabb  src/main.onyx   /^dude_get_aabb :: proc (use dude: ^Dude) -> AABB {$/
-dude_tree      src/main.onyx   /^dude_tree     : QuadTree(Dude)$/
+dude_tree      src/globals.onyx        /^dude_tree     : QuadTree(Dude)$/
 dude_try_move  src/main.onyx   /^dude_try_move :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) {$/
 dude_update    src/main.onyx   /^dude_update :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) {$/
-dudes  src/main.onyx   /^dudes         : [..] Dude$/
+dudes  src/globals.onyx        /^dudes         : [..] Dude$/
 enable /usr/share/onyx/core/js/webgl.onyx      /^enable                         :: proc (cap: GLenum) #foreign "gl" "enable" ---$/
 enableVertexAttribArray        /usr/share/onyx/core/js/webgl.onyx      /^enableVertexAttribArray        :: proc (index: GLuint) #foreign "gl" "enableVertexAttribArray" ---$/
 f64_to_string  /usr/share/onyx/core/string.onyx        /^f64_to_string :: proc (f: f64, buf: [] u8) -> string {$/
@@ -783,8 +785,8 @@ getProgramParameter /usr/share/onyx/core/js/webgl.onyx      /^getProgramParameter
 getShaderParameter     /usr/share/onyx/core/js/webgl.onyx      /^getShaderParameter             :: proc (shader: GLShader, pname: GLenum) -> GLenum #foreign "gl" "getShaderParameter" ---$/
 getUniformLocation     /usr/share/onyx/core/js/webgl.onyx      /^getUniformLocation             :: proc (program: GLProgram, name: string) -> GLUniformLocation #foreign "gl" "getUniformLocation" ---$/
 getVertexAttribOffset  /usr/share/onyx/core/js/webgl.onyx      /^getVertexAttribOffset          :: proc (index: GLuint, pname: GLenum) #foreign "gl" "getVertexAttribOffset" ---$/
-half_window_height     src/main.onyx   /^half_window_height := 0$/
-half_window_width      src/main.onyx   /^half_window_width  := 0$/
+half_window_height     src/globals.onyx        /^half_window_height := 0$/
+half_window_width      src/globals.onyx        /^half_window_width  := 0$/
 heap_alloc     /usr/share/onyx/core/alloc.onyx /^heap_alloc :: proc (size_: u32, align: u32) -> rawptr {$/
 heap_alloc_proc        /usr/share/onyx/core/alloc.onyx /^heap_alloc_proc :: proc (data: rawptr, aa: AllocAction, size: u32, align: u32, oldptr: rawptr) -> rawptr {$/
 heap_allocator /usr/share/onyx/core/alloc.onyx /^heap_allocator : Allocator;$/
@@ -805,7 +807,7 @@ i64_to_string       /usr/share/onyx/core/string.onyx        /^i64_to_string :: proc (n_: i64,
 init   src/events.onyx /^init :: proc () {$/
 init   /usr/share/onyx/core/js/webgl.onyx      /^init                           :: proc (canvasname: string) -> GLboolean #foreign "gl" "init" ---$/
 init   src/input.onyx  /^init :: proc (use state: ^InputState) {$/
-input_state    src/main.onyx   /^input_state   : input.InputState$/
+input_state    src/globals.onyx        /^input_state   : input.InputState$/
 invalidateFramebuffer  /usr/share/onyx/core/js/webgl.onyx      /^invalidateFramebuffer          :: proc (target: GLenum, attachments: string) #foreign "gl" "invalidateFramebuffer" ---$/
 invalidateSubFramebuffer       /usr/share/onyx/core/js/webgl.onyx      /^invalidateSubFramebuffer       :: proc (target: GLenum, attachments: string, x: GLint, y: GLint, width: GLsizei, height: GLsizei) #foreign "gl" "invalidateSubFramebuffer" ---$/
 isEnabled      /usr/share/onyx/core/js/webgl.onyx      /^isEnabled                      :: proc (cap: GLenum) -> GLboolean #foreign "gl" "isEnabled" ---$/
@@ -891,7 +893,7 @@ readPixels  /usr/share/onyx/core/js/webgl.onyx      /^readPixels                     :
 render_context_flush   src/main.onyx   /^render_context_flush :: proc (use rc: ^RenderContext) {$/
 render_context_init    src/main.onyx   /^render_context_init :: proc (use rc: ^RenderContext, qr: ^QuadRenderer = null) {$/
 renderbufferStorageMultisample /usr/share/onyx/core/js/webgl.onyx      /^renderbufferStorageMultisample :: proc (target: GLenum, samples: GLsizei, internalforamt: GLenum, width: GLsizei, height: GLsizei) #foreign "gl" "renderbufferStorageMultisample" ---$/
-renderer       src/main.onyx   /^renderer      : RenderContext$/
+renderer       src/globals.onyx        /^renderer      : RenderContext$/
 resize /usr/share/onyx/core/builtin.onyx       /^resize :: proc (use a: Allocator, ptr: rawptr, size: u32) -> rawptr {$/
 rotl_i32       /usr/share/onyx/core/intrinsics.onyx    /^rotl_i32     :: proc (lhs: i32, rhs: i32) -> i32 #intrinsic ---$/
 rotl_i64       /usr/share/onyx/core/intrinsics.onyx    /^rotl_i64     :: proc (lhs: i64, rhs: i64) -> i64 #intrinsic ---$/
@@ -956,6 +958,11 @@ texture_prepare    src/gfx/texture.onyx    /^texture_prepare :: proc (use tex: ^Textur
 texture_use    src/gfx/texture.onyx    /^texture_use :: proc (use tex: ^Texture) {$/
 textures       src/gfx/texture.onyx    /^textures : struct {$/
 textures_init  src/gfx/texture.onyx    /^textures_init :: proc () {$/
+tile_to_color4f32      src/main.onyx   /^tile_to_color4f32 :: proc (t: Tile) -> Color4f32 {$/
+tilemap        src/globals.onyx        /^tilemap       : Tilemap$/
+tilemap_draw   src/main.onyx   /^tilemap_draw :: proc (use tm: ^Tilemap, renderer: ^RenderContext) {$/
+tilemap_free   src/main.onyx   /^tilemap_free :: proc (use tm: ^Tilemap) {$/
+tilemap_init   src/main.onyx   /^tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) {$/
 trunc_f32      /usr/share/onyx/core/intrinsics.onyx    /^trunc_f32    :: proc (val: f32) -> f32 #intrinsic ---$/
 trunc_f64      /usr/share/onyx/core/intrinsics.onyx    /^trunc_f64    :: proc (val: f64) -> f64 #intrinsic ---$/
 ttf_create     src/font.onyx   /^ttf_create :: proc (ttf_data: [] u8) -> TrueTypeFont {$/
@@ -1004,7 +1011,7 @@ vertexAttrib4f    /usr/share/onyx/core/js/webgl.onyx      /^vertexAttrib4f
 vertexAttribDivisor    /usr/share/onyx/core/js/webgl.onyx      /^vertexAttribDivisor            :: proc (idx: GLuint, divisor: GLuint) #foreign "gl" "vertexAttribDivisor" ---$/
 vertexAttribPointer    /usr/share/onyx/core/js/webgl.onyx      /^vertexAttribPointer            :: proc (idx: GLuint, size: GLint, type: GLenum, normalized: GLboolean, stride: GLsizei, offset: GLint) #foreign "gl" "vertexAttribPointer" ---$/
 viewport       /usr/share/onyx/core/js/webgl.onyx      /^viewport                       :: proc (x: GLint, y: GLint, width: GLsizei, height: GLsizei) #foreign "gl" "viewport" ---      $/
-window_height  src/main.onyx   /^window_height := 0$/
-window_width   src/main.onyx   /^window_width  := 0$/
+window_height  src/globals.onyx        /^window_height := 0$/
+window_width   src/globals.onyx        /^window_width  := 0$/
 xor_i32        /usr/share/onyx/core/intrinsics.onyx    /^xor_i32      :: proc (lhs: i32, rhs: i32) -> i32 #intrinsic ---$/
 xor_i64        /usr/share/onyx/core/intrinsics.onyx    /^xor_i64      :: proc (lhs: i64, rhs: i64) -> i64 #intrinsic ---$/