From: Brendan Hansen Date: Tue, 6 Oct 2020 02:12:40 +0000 (-0500) Subject: added mouse control to ui X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=5da20bb33fbcfaca425e01fd9a26c8525d6b0aba;p=onyx-game.git added mouse control to ui --- diff --git a/res/tilemap.data b/res/tilemap.data index dc86ab1..e53b233 100644 Binary files a/res/tilemap.data and b/res/tilemap.data differ diff --git a/res/tilemap.png b/res/tilemap.png index 22b830c..f165b3d 100644 Binary files a/res/tilemap.png and b/res/tilemap.png differ diff --git a/src/gfx/texture.onyx b/src/gfx/texture.onyx index 2928c3e..6536f63 100644 --- a/src/gfx/texture.onyx +++ b/src/gfx/texture.onyx @@ -60,6 +60,8 @@ texture_prepare :: proc (use tex: ^Texture) { gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, gl.UNSIGNED_BYTE, data); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.bindTexture(gl.TEXTURE_2D, -1); } diff --git a/src/input.onyx b/src/input.onyx index 5b26b4e..ba86a0f 100644 --- a/src/input.onyx +++ b/src/input.onyx @@ -25,6 +25,9 @@ InputState :: struct { x : i32; y : i32; + dx : i32; + dy : i32; + buttons_down : [BUTTON_COUNT] bool; buttons_just_down : [BUTTON_COUNT] bool; @@ -61,6 +64,8 @@ process_event :: proc (use state: ^InputState, ev: ^event.Event) { } case MouseDown { + mouse.dx += (ev.mouse.pos_x - half_window_width) - mouse.x; + mouse.dy += (ev.mouse.pos_y - half_window_height) - mouse.y; mouse.x = ev.mouse.pos_x - half_window_width; mouse.y = ev.mouse.pos_y - half_window_height; @@ -69,6 +74,8 @@ process_event :: proc (use state: ^InputState, ev: ^event.Event) { } case MouseUp { + mouse.dx += (ev.mouse.pos_x - half_window_width) - mouse.x; + mouse.dy += (ev.mouse.pos_y - half_window_height) - mouse.y; mouse.x = ev.mouse.pos_x - half_window_width; mouse.y = ev.mouse.pos_y - half_window_height; mouse.buttons_down[cast(u32) ev.mouse.button] = false; @@ -76,6 +83,8 @@ process_event :: proc (use state: ^InputState, ev: ^event.Event) { } case MouseMove { + mouse.dx += (ev.mouse.pos_x - half_window_width) - mouse.x; + mouse.dy += (ev.mouse.pos_y - half_window_height) - mouse.y; mouse.x = ev.mouse.pos_x - half_window_width; mouse.y = ev.mouse.pos_y - half_window_height; } @@ -100,6 +109,9 @@ postupdate :: proc (use state: ^InputState) { if mouse.wheel_ups > ~~0 do mouse.wheel_ups -= ~~1; if mouse.wheel_downs > ~~0 do mouse.wheel_downs -= ~~1; + + mouse.dx = 0; + mouse.dy = 0; } diff --git a/src/main.onyx b/src/main.onyx index a62cbb4..5016653 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -91,7 +91,15 @@ draw_quad :: proc (use rc: ^RenderContext, quad: ^Quad) { curr_quad_idx += 1; } -render_context_flush :: proc (use rc: ^RenderContext) { +render_context_ui :: proc (use rc: ^RenderContext) { + quad_renderer_update_world(quad_renderer, 1.0f, 1.0f, 0.0f, 0.0f); + quad_rebuffer_data(quad_renderer); + quad_renderer_draw(quad_renderer, curr_quad_idx); + + curr_quad_idx = 0; +} + +render_context_transformation :: proc (use rc: ^RenderContext) { quad_renderer_update_world(quad_renderer, scale, scale, trans_x, trans_y); quad_rebuffer_data(quad_renderer); quad_renderer_draw(quad_renderer, curr_quad_idx); @@ -143,14 +151,9 @@ 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 input_state.mouse.buttons_down[0] { + renderer.trans_x += ~~input_state.mouse.dx / renderer.scale; + renderer.trans_y += ~~input_state.mouse.dy / renderer.scale; } if simulating { @@ -172,16 +175,18 @@ update :: proc () { } 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; + if tile.r < ~~255 do tile.r += ~~1; else do tile.r = ~~255; + if tile.g < ~~255 do tile.g += ~~1; else do tile.g = ~~255; + if tile.b < ~~255 do tile.b += ~~1; else do tile.b = ~~255; } } + draw :: proc () { gl.clearColor(0.1f, 0.1f, 0.1f, 1.0f); gl.clear(gl.COLOR_BUFFER_BIT); + // World Rendering texture_use(^textures.tilemap); tilemap_draw(^tilemap, ^renderer); @@ -194,10 +199,12 @@ draw :: proc () { dude_sprite.x, dude_sprite.y, dude_sprite.w, dude_sprite.h); } + render_context_transformation(^renderer); + // UI Rendering renderer.color = Color4f32.{ 0.0f, 0.0f, 0.0f, 1.0f }; draw_rect(^renderer, ~~input_state.mouse.x, ~~input_state.mouse.y, 10f, 10f); - render_context_flush(^renderer); + render_context_ui(^renderer); } @@ -228,6 +235,7 @@ main :: proc (args: [] cstring) { atlas = atlas_create(^textures.tilemap); atlas_map(^atlas, 0, AtlasSprite.{ x = 16.0f, y = 0.0f, w = 16.0f, h = 16.0f }); + atlas_map(^atlas, 1, AtlasSprite.{ x = 32.0f, y = 0.0f, w = 16.0f, h = 16.0f }); event.init(); input.init(^input_state); @@ -237,11 +245,11 @@ main :: proc (args: [] cstring) { Dude_Color_Table[2] = Color4f32.{ 0.2f, 0.2f, 1.0f, 1.0f }; array_init(^dudes); - for i: 0 .. 2000 { + for i: 0 .. 500 { array_push(^dudes, dude_create_random()); } - tilemap_init(^tilemap, 202, 140); + tilemap_init(^tilemap, 250, 140); game_launch :: proc () #foreign "game" "launch" ---; game_launch(); @@ -282,9 +290,9 @@ dude_update :: proc (use dude: ^Dude, other_dudes: ^QuadTree(Dude)) { 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; + 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; @@ -356,9 +364,9 @@ Tilemap :: struct { } Tile :: struct { - r : u8 = ~~0; - g : u8 = ~~0; - b : u8 = ~~0; + r : u8 = ~~255; + g : u8 = ~~255; + b : u8 = ~~255; } tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) { @@ -366,6 +374,8 @@ tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) { height = h; alloc_slice(^tiles, width * height); + printf("The tilemap takes up %i kibibytes.\n", (width * height * sizeof Tile) >> 10); + for ^t: tiles do *t = Tile.{}; } @@ -376,9 +386,22 @@ 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 + origin.x, ~~y * TILE_SIZE + origin.y, TILE_SIZE, TILE_SIZE); + x_pos := origin.x; + y_pos := origin.y; + + for y: 0 .. height { + for x: 0 .. width { + renderer.color = tile_to_color4f32(tiles[y * width + x]); + + // SPEEDUP: Write an optimized version of draw_rect that can take a batch of changes all at once. + // This requires a lot of copying overhead. + draw_rect(renderer, x_pos, y_pos, TILE_SIZE, TILE_SIZE); + + x_pos += ~~TILE_SIZE; + } + + x_pos = origin.x; + y_pos += TILE_SIZE; } } @@ -389,8 +412,8 @@ tilemap_get_tile :: proc (use tm: ^Tilemap, x: u32, y: u32) -> Tile { 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); + 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]; diff --git a/tags b/tags index ce1d00b..4dd576e 100644 --- a/tags +++ b/tags @@ -890,8 +890,9 @@ random_seed /usr/share/onyx/core/random.onyx /^random_seed :: proc (s: u32) do s range /usr/share/onyx/core/builtin.onyx /^range :: struct {$/ readBuffer /usr/share/onyx/core/js/webgl.onyx /^readBuffer :: proc (src: GLenum) #foreign "gl" "readBuffer" ---$/ readPixels /usr/share/onyx/core/js/webgl.onyx /^readPixels :: proc (x: GLint, y: GLint, width: GLsizei, height: GLsizei, format: GLenum, type: GLenum, pixels: string) #foreign "gl" "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) {$/ +render_context_transformation src/main.onyx /^render_context_transformation :: proc (use rc: ^RenderContext) {$/ +render_context_ui src/main.onyx /^render_context_ui :: proc (use rc: ^RenderContext) {$/ 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/globals.onyx /^renderer : RenderContext$/ resize /usr/share/onyx/core/builtin.onyx /^resize :: proc (use a: Allocator, ptr: rawptr, size: u32) -> rawptr {$/