From 01ebcf82dc5deec330e449bd28f9b6e68bf1069a Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 4 Oct 2020 22:40:42 -0500 Subject: [PATCH] basic tilemap coloring works --- src/main.onyx | 62 ++++++++++++++++++++++++++++++++++++++++++--------- tags | 3 ++- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/main.onyx b/src/main.onyx index d6ac236..a62cbb4 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -113,6 +113,8 @@ poll_events :: proc () { half_window_width = window_width / 2; half_window_height = window_height / 2; + tilemap.origin = V2f.{ ~~-half_window_width, ~~-half_window_height }; + gl.canvasSize(window_width, window_height); gl.viewport(0, 0, window_width, window_height); @@ -141,6 +143,16 @@ update :: proc () { if input_state.mouse.wheel_ups > ~~0 do renderer.scale *= 1.125f; if input_state.mouse.wheel_downs > ~~0 do renderer.scale /= 1.125f; + curr_tile := tilemap_screen_coord_to_tile(^tilemap, V2f.{ ~~input_state.mouse.x, ~~input_state.mouse.y }); + if curr_tile != null { + v := cast(i32) curr_tile.r; + if v < 200 do v += 20; + + curr_tile.r = ~~v; + curr_tile.g = ~~v; + curr_tile.b = ~~v; + } + if simulating { quad_scratch : ScratchState; quad_alloc : Allocator; @@ -158,10 +170,16 @@ update :: proc () { if !d.can_move_x && !d.can_move_y do d.couldnt_move_count += ~~1; } } + + for ^tile: tilemap.tiles { + if tile.r >= ~~1 do tile.r -= ~~1; else do tile.r = ~~0; + if tile.g >= ~~1 do tile.g -= ~~1; else do tile.g = ~~0; + if tile.b >= ~~1 do tile.b -= ~~1; else do tile.b = ~~0; + } } draw :: proc () { - gl.clearColor(0.9f, 0.9f, 0.9f, 1.0f); + gl.clearColor(0.1f, 0.1f, 0.1f, 1.0f); gl.clear(gl.COLOR_BUFFER_BIT); texture_use(^textures.tilemap); @@ -214,16 +232,16 @@ main :: proc (args: [] cstring) { event.init(); input.init(^input_state); - Dude_Color_Table[0] = Color4f32.{ 1.0f, 0.5f, 0.5f, 1.0f }; - Dude_Color_Table[1] = Color4f32.{ 0.5f, 1.0f, 0.5f, 1.0f }; - Dude_Color_Table[2] = Color4f32.{ 0.5f, 0.5f, 1.0f, 1.0f }; + Dude_Color_Table[0] = Color4f32.{ 1.0f, 0.2f, 0.2f, 1.0f }; + Dude_Color_Table[1] = Color4f32.{ 0.2f, 1.0f, 0.2f, 1.0f }; + Dude_Color_Table[2] = Color4f32.{ 0.2f, 0.2f, 1.0f, 1.0f }; array_init(^dudes); for i: 0 .. 2000 { array_push(^dudes, dude_create_random()); } - tilemap_init(^tilemap, 150, 80); + tilemap_init(^tilemap, 202, 140); game_launch :: proc () #foreign "game" "launch" ---; game_launch(); @@ -259,6 +277,21 @@ dude_update :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) { couldnt_move_count = ~~0; } + if tile := tilemap_screen_coord_to_tile(^tilemap, pos); tile != null { + target_r := cast(u8) cast(u32) (color.r * 255.0f); + target_g := cast(u8) cast(u32) (color.g * 255.0f); + target_b := cast(u8) cast(u32) (color.b * 255.0f); + + diff_r := cast(i32) (target_r - tile.r) >>> ~~2; + diff_g := cast(i32) (target_g - tile.g) >>> ~~2; + diff_b := cast(i32) (target_b - tile.b) >>> ~~2; + + tile.r += ~~diff_r; + tile.g += ~~diff_g; + tile.b += ~~diff_b; + } + + dude_try_move(dude, other_dudes); } @@ -317,6 +350,8 @@ Tilemap :: struct { width : u32; height : u32; + origin : V2f; + tiles : [] Tile; } @@ -331,11 +366,7 @@ tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) { 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); - } + for ^t: tiles do *t = Tile.{}; } tilemap_free :: proc (use tm: ^Tilemap) { @@ -347,7 +378,7 @@ tilemap_free :: proc (use tm: ^Tilemap) { 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); + draw_rect(renderer, ~~x * TILE_SIZE + origin.x, ~~y * TILE_SIZE + origin.y, TILE_SIZE, TILE_SIZE); } } @@ -356,6 +387,15 @@ tilemap_get_tile :: proc (use tm: ^Tilemap, x: u32, y: u32) -> Tile { return tiles[x + y * width]; } +tilemap_screen_coord_to_tile :: proc (use tm: ^Tilemap, pos: V2f) -> ^Tile { + n := v2_sub(pos, origin); + tx := cast(i32) (n.x / TILE_SIZE); + ty := cast(i32) (n.y / TILE_SIZE); + + if tx < 0 || ty < 0 || tx >= width || ty >= height do return null; + return ^tiles[tx + ty * width]; +} + tile_to_color4f32 :: proc (t: Tile) -> Color4f32 { return Color4f32.{ r = ~~cast(u32) t.r / 255.0f, diff --git a/tags b/tags index bbdf9a0..ce1d00b 100644 --- a/tags +++ b/tags @@ -906,7 +906,6 @@ scissor /usr/share/onyx/core/js/webgl.onyx /^scissor :: p scratch_alloc_init /usr/share/onyx/core/alloc.onyx /^scratch_alloc_init :: proc (a: ^Allocator, ss: ^ScratchState) {$/ scratch_alloc_proc /usr/share/onyx/core/alloc.onyx /^scratch_alloc_proc :: proc (data: rawptr, aa: AllocAction, size: u32, align: u32, oldptr: rawptr) -> rawptr {$/ scratch_state_init /usr/share/onyx/core/alloc.onyx /^scratch_state_init :: proc (use ss: ^ScratchState, buffer: rawptr, length: u32) {$/ -seed /usr/share/onyx/core/random.onyx /^seed := 8675309$/ shaderSource /usr/share/onyx/core/js/webgl.onyx /^shaderSource :: proc (shader: GLShader, source: string) #foreign "gl" "shaderSource" ---$/ shl_i32 /usr/share/onyx/core/intrinsics.onyx /^shl_i32 :: proc (lhs: i32, rhs: i32) -> i32 #intrinsic ---$/ shl_i64 /usr/share/onyx/core/intrinsics.onyx /^shl_i64 :: proc (lhs: i64, rhs: i64) -> i64 #intrinsic ---$/ @@ -962,7 +961,9 @@ tile_to_color4f32 src/main.onyx /^tile_to_color4f32 :: proc (t: Tile) -> Color4f 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_get_tile src/main.onyx /^tilemap_get_tile :: proc (use tm: ^Tilemap, x: u32, y: u32) -> Tile {$/ tilemap_init src/main.onyx /^tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) {$/ +tilemap_screen_coord_to_tile src/main.onyx /^tilemap_screen_coord_to_tile :: proc (use tm: ^Tilemap, pos: V2f) -> ^Tile {$/ 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 {$/ -- 2.25.1