From: Brendan Hansen Date: Tue, 2 Mar 2021 22:22:17 +0000 (-0600) Subject: testing capabilities of immediate mode X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c83e3afda421593f6de738026563a1029de57bb9;p=onyx-imgui.git testing capabilities of immediate mode --- diff --git a/lib/gl/gl_utils.onyx b/lib/gl/gl_utils.onyx index 0ad5359..34bae0e 100644 --- a/lib/gl/gl_utils.onyx +++ b/lib/gl/gl_utils.onyx @@ -23,16 +23,17 @@ Shader :: struct { } init_from_source :: (use shader: ^Shader, vertex_source: str, fragment_source: str) { - // @Robustness: Errors in compiling the shaders are ignored right now. - vertex_shader, _ := compile_shader(vertex_source, gl.VERTEX_SHADER); - fragment_shader, _ := compile_shader(fragment_source, gl.FRAGMENT_SHADER); + vertex_shader, vertex_compiled := compile_shader(vertex_source, gl.VERTEX_SHADER); + fragment_shader, fragment_compiled := compile_shader(fragment_source, gl.FRAGMENT_SHADER); + assert(vertex_compiled, "Vertex shader failed to compile"); + assert(fragment_compiled, "Fragment shader failed to compile"); defer { gl.deleteShader(vertex_shader); gl.deleteShader(fragment_shader); } - // @Robustness: Errors in linking the program are ignored right now. - shader_program, _ := link_program(vertex_shader, fragment_shader); + shader_program, program_linked := link_program(vertex_shader, fragment_shader); + assert(program_linked, "Program failed to link"); program = shader_program; position_loc = gl.getAttribLocation(program, "a_position"); diff --git a/lib/immediate_renderer.onyx b/lib/immediate_renderer.onyx index 5551edb..67aa113 100644 --- a/lib/immediate_renderer.onyx +++ b/lib/immediate_renderer.onyx @@ -12,7 +12,8 @@ Vector2 :: struct { } Color4 :: struct { - r, g, b, a: f32; + r, g, b: f32; + a: f32 = 1; } Immediate_Vertex :: struct { @@ -115,16 +116,18 @@ Immediate_Renderer :: struct { }, (use ir: ^Immediate_Renderer, position: Vector2, color: Color4) { + if vertex_count >= verticies.count do ir->flush(); + vertex_ptr := ^verticies[vertex_count]; defer vertex_count += 1; vertex_ptr.position = position; vertex_ptr.color = color; - vertex_ptr.texture = Vector2.{ 0, 0 }; + vertex_ptr.texture = .{ 0, 0 }; }, } - quad :: (use ir: ^Immediate_Renderer, position: Vector2, size: Vector2, color: Color4 = .{1,1,1,1}) { + quad :: (use ir: ^Immediate_Renderer, position: Vector2, size: Vector2, color: Color4 = .{1,1,1}) { push_vertex(ir, .{ position.x, position.y }, color); push_vertex(ir, .{ position.x + size.x, position.y }); push_vertex(ir, .{ position.x + size.x, position.y + size.y }); @@ -160,7 +163,7 @@ immediate_vertex :: proc { (position: Vector2, color: Color4) { immediate_renderer->push_vertex(position, color); }, } -immediate_quad :: (position: Vector2, size: Vector2, color: Color4 = .{1,1,1,1}) { +immediate_quad :: (position: Vector2, size: Vector2, color: Color4 = .{1,1,1}) { immediate_renderer->quad(position, size, color); } diff --git a/test/basic.onyx b/test/basic.onyx index 06a62f9..786bd92 100644 --- a/test/basic.onyx +++ b/test/basic.onyx @@ -12,8 +12,21 @@ init :: () { gl.init("imgui-canvas"); imgui.immediate_renderer_init(); + + array.init(^squares); +} + +width := 0 +height := 0 + +squares : [..] struct { + x, y : f32; + size : f32 = 0.05f; + + color: imgui.Color4 = .{1,0,0}; } + poll_events :: () { use events.DomEventKind; @@ -22,13 +35,20 @@ poll_events :: () { case Resize { println("The window was resized!"); + width = event.resize.width; + height = event.resize.height; + gl.setSize(event.resize.width, event.resize.height); gl.viewport(0, 0, event.resize.width, event.resize.height); } case MouseDown { - println("The mouse was pressed!"); - printf("Specifically, the %i button.\n", event.mouse.button); + if event.mouse.button == ~~0 { + array.push(^squares, .{ + x = (cast(f32) event.mouse.pos_x / ~~width), + y = (cast(f32) event.mouse.pos_y / ~~height), + }); + } } } } @@ -37,6 +57,10 @@ t := 0.0f update :: () { t += 1.0 / 60.0; + + if random.between(0, 100) >= 90 { + array.push(^squares, .{ x = t / 100, y = 0, size = 0.02f, color = .{0,0,1} }); + } } draw :: () { @@ -44,22 +68,27 @@ draw :: () { gl.clear(gl.COLOR_BUFFER_BIT); use imgui; - defer immediate_flush(); x := math.cos(t); y := math.sin(t); - immediate_vertex(.{ 0, 0 }, color=.{ 1, 1, 1, 1 }); + immediate_vertex(.{ 0, 0 }, color=.{ 1, 1, 1 }); immediate_vertex(.{ x, y }); immediate_vertex(.{ -y, x }); if cast(u32) t % 2 == 0 { - immediate_vertex(.{ 0, 0 }, color=.{ 1, 0, 0, 1 }); - immediate_vertex(.{ -1.0f, 0 }, color=.{ 0, 1, 0, 1 }); - immediate_vertex(.{ 0, -1.0f }, color=.{ 0, 0, 1, 1 }); + immediate_vertex(.{ 0, 0 }, color=.{ 1, 0, 0 }); + immediate_vertex(.{ -1.0f, 0 }, color=.{ 0, 1, 0 }); + immediate_vertex(.{ 0, -1.0f }, color=.{ 0, 0, 1 }); + } + + immediate_quad(.{0, 0}, .{.1, .1}, .{1, 0, 1}); + + for ^square: squares { + immediate_quad(.{ square.x, square.y }, .{ square.size, square.size }, square.color); } - immediate_quad(.{0, 0}, .{.1, .1}, .{1, 0, 1, 1}); + immediate_flush(); } loop :: () -> void #export {