From: Brendan Hansen Date: Thu, 10 Feb 2022 01:10:49 +0000 (-0600) Subject: added glScissor functionality; cleanup X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=000bffce5ee62f550ee27a1c8359d7e9d2006880;p=bar-game.git added glScissor functionality; cleanup --- diff --git a/src/entity/editor.onyx b/src/entity/editor.onyx index 3cdb998..0450ae2 100644 --- a/src/entity/editor.onyx +++ b/src/entity/editor.onyx @@ -30,7 +30,7 @@ editor_toggle :: () { editor_update :: (dt: f32) { move_towards(^editor_openness, editor_target_openness, dt * 6); - + if is_key_just_up(GLFW_KEY_1) do clicked_tab = .Create; if is_key_just_up(GLFW_KEY_2) do clicked_tab = .Edit; @@ -64,7 +64,7 @@ editor_update :: (dt: f32) { #local handle_entity_selction_and_dragging :: (dt: f32) { mouse_pos := mouse_get_position_vector(); - if mouse_pos.x < ~~window_width - sidebar_width * 2 && mouse_pos.y > menubar_height { + if mouse_pos.x < ~~window_width - sidebar_width && mouse_pos.y > menubar_height { if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) || is_button_just_down(GLFW_MOUSE_BUTTON_RIGHT) { if active_tab == .Edit do active_index = -1; selected_entity_id = Entity_Nothing; @@ -78,7 +78,7 @@ editor_update :: (dt: f32) { break; } } - + dragging = is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) && selected_entity_id != Entity_Nothing; resizing = is_button_just_down(GLFW_MOUSE_BUTTON_RIGHT) && selected_entity_id != Entity_Nothing; } @@ -154,7 +154,7 @@ editor_draw :: () { { // Draw sidebar, if necessary sidebar_width = editor_openness * 400.0f; - w := sidebar_width; + w := sidebar_width; x := ~~ window_width - w; y := 40.0f; h := ~~ window_height - y; @@ -199,7 +199,7 @@ editor_draw :: () { test << #char "t"; } - draw_textbox(.{x, y + 72.0f, w, 36.0f}, ^test); + draw_textbox(.{x, y + 72.0f, w / 2.0f, 36.0f}, ^test); #persist dumb := false; @@ -213,11 +213,12 @@ editor_draw :: () { font_print(editor_big_font, x + 2, y + 24, info.name); + if active_index >= 0 do sidebar_width += w; render_struct_fields(any.{~~entity, entity.type}, 0, x, y + 4, w, h); } #local render_struct_fields :: (v: any, i: i32, x, y, w, h: f32, depth := 0) -> (new_y: f32, new_i: i32) { - Field_Height :: 22.0f + Field_Height :: 22.0f; info := cast(^type_info.Type_Info_Struct) type_info.get_type_info(v.type); for^ info.members { @@ -228,19 +229,20 @@ editor_draw :: () { if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) { 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; } } if active_index + 1 == i { - immediate_set_color(.{.4,.4,.2}); - immediate_rectangle(x, y + 2, w, Field_Height + 2); + immediate_set_color(.{.4,.4,.2}); + immediate_rectangle(x, y + 2, w, Field_Height + 2); - render_field_editor(member_any, y, it.name); + render_field_editor(member_any, y, it.name); } elseif i % 2 == 0 { - immediate_set_color(.{.3,.3,.3}); - immediate_rectangle(x, y + 2, w, Field_Height + 2); + immediate_set_color(.{.3,.3,.3}); + immediate_rectangle(x, y + 2, w, Field_Height + 2); } font_print(editor_font, x + 2, y + Field_Height, it.name); @@ -275,9 +277,9 @@ editor_draw :: () { } #local render_field_editor :: (v: any, y: f32, field_name: str) { - w := sidebar_width; + w := sidebar_width / 2; h := 200.0f; - x := ~~ window_width - sidebar_width * 2; + x := ~~ window_width - sidebar_width; y = math.min(y, ~~ window_height - h); immediate_set_color(.{.3,.3,.3}); immediate_rectangle(x, y, w, h); diff --git a/src/entity/player.onyx b/src/entity/player.onyx index c59d482..9592a30 100644 --- a/src/entity/player.onyx +++ b/src/entity/player.onyx @@ -76,10 +76,10 @@ Player :: struct { speed :: 128.0f; delta: Vector2; - if is_key_down(controls.left) { delta.x -= speed * dt; facing = .Left; } - if is_key_down(controls.right) { delta.x += speed * dt; facing = .Right; } - if is_key_down(controls.up) { delta.y -= speed * dt; facing = .Up; } - if is_key_down(controls.down) { delta.y += speed * dt; facing = .Down; } + if is_key_down(controls.left) { delta.x -= speed * dt; facing = .Left; } + if is_key_down(controls.right) { delta.x += speed * dt; facing = .Right; } + if is_key_down(controls.up) { delta.y -= speed * dt; facing = .Up; } + if is_key_down(controls.down) { delta.y += speed * dt; facing = .Down; } dist := math.max(size.x, size.y) * 2; walls := scene->query_by_flags(.{pos.x - dist, pos.y - dist, dist * 2, dist * 2}, .Solid); @@ -298,4 +298,3 @@ move_towards :: (v: ^$T, target: T, diff: T) { if *v < target do *v += diff; if *v > target do *v -= diff; } - diff --git a/src/gfx/immediate.onyx b/src/gfx/immediate.onyx index b15825a..231b97d 100644 --- a/src/gfx/immediate.onyx +++ b/src/gfx/immediate.onyx @@ -112,6 +112,28 @@ immediate_subimage :: (image: ^Texture, x, y, w, h: f32, tx, ty, tw, th: f32) { immediate_ellipse :: () {} +immediate_push_scissor :: (x, y, w, h: f32) { + // Assuming that x, y, w, and h are in screen (window) coordinates. + + scissors << .{x, y, w, h}; + glEnable(GL_SCISSOR_TEST); + glScissor(~~x, window_height - ~~(y + h), ~~w, ~~h); +} + +immediate_pop_scissor :: () { + if scissors.count > 0 { + array.pop(^scissors); + } + + 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); + } else { + glDisable(GL_SCISSOR_TEST); + } +} + Color :: struct { r, g, b : f32; a := 1.0f; @@ -141,6 +163,11 @@ Immediate_Vertex :: struct { } rendering_type := Rendering_Type.Plain; + Scissor :: struct { + x, y, w, h: f32; + } + scissors: [..] Scissor; + set_rendering_type :: (new_type: typeof rendering_type) { if rendering_type != new_type { immediate_flush(); diff --git a/src/gfx/ui.onyx b/src/gfx/ui.onyx index 62871bd..112706b 100644 --- a/src/gfx/ui.onyx +++ b/src/gfx/ui.onyx @@ -147,7 +147,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th border_width := theme.border_width; text_color := theme.text_color; text := str.{text_buffer.data, text_buffer.count}; - if text.count == 0 && placeholder.count >0 { + if text.count == 0 && placeholder.count > 0 { text = placeholder; text_color = theme.placeholder_text_color; } @@ -205,7 +205,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th } case GLFW_KEY_DELETE { - array.delete(text_buffer, 0); + array.delete(text_buffer, textbox_editing_state.cursor_position); } case #default { @@ -230,7 +230,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th move_towards(^animation_state.hover_time, 0.0f, theme.hover_speed); } - // immediate_push_scissor(x, y, w, h); + immediate_push_scissor(x, y, w, h); immediate_set_color(theme.border_color); immediate_rectangle(x, y, w, h); @@ -244,7 +244,7 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th move_towards(^animation_state.click_time, 0.0f, theme.click_decay_speed); - // immediate_pop_scissor(); + immediate_pop_scissor(); return result; } @@ -436,4 +436,4 @@ draw_checkbox :: (use r: Rect, value: ^bool, text: str, theme := ^default_checkb b = c1.b * (1 - t) + c2.b * t, a = c1.a * (1 - t) + c2.a * t, }; -} \ No newline at end of file +} diff --git a/src/main.onyx b/src/main.onyx index e7a80db..ad85974 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -94,7 +94,7 @@ draw :: () { defer input_post_update(); defer { immediate_flush(); - + #if DEBUG && false { font_set_color(.{1,0,0}); font_print(debug_font, 0, 16, "FPS: {}", game_fps);