Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

changing to imgui sdl #9

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
736d8a1
changing to imgui sdl
sumofat Dec 8, 2021
5e41a04
Adds linux support. Replaces shared:glfw, and shared:opengl with vend…
Apr 7, 2022
03f0a99
Minor changes.
Apr 7, 2022
bb2697b
Minor changes.
Apr 7, 2022
12d9587
Casts 'ok' to a bool and compares it with gl.TRUE.
Apr 7, 2022
94400ec
Replaces .so with .a.
Apr 7, 2022
c0a254d
aaa
sumofat Apr 16, 2022
ea83356
now compiles and runs ok
Apr 25, 2022
26a53e6
fixes to make this venderized and less bugggy
sumofat Apr 26, 2022
50edfbd
add dylib temp
Apr 26, 2022
9cb44b7
Merge branch 'vendor' of https://github.com/sumofat/odin-imgui into f…
Apr 26, 2022
7ad5b38
fixed super for mac
Apr 26, 2022
97dd1c8
added japanese font support new fonts etc..
sumofat Apr 27, 2022
71a75b0
Fixes syntax error.
Apr 30, 2022
81e574b
Update README.md
NoahR02 Apr 30, 2022
3973940
removed comments
sumofat May 1, 2022
1e91a22
Fixed quite a few errors
sumofat May 1, 2022
18dd69c
Merge pull request #1 from sumofat/fix_vendor
sumofat May 1, 2022
49e5e15
shouldnt put these personal font changes here
sumofat May 1, 2022
7a8824e
Merge remote-tracking branch 'myfork/vendor' into fix_vendor
sumofat May 1, 2022
1f76220
Merge pull request #2 from sumofat/fix_vendor
sumofat May 1, 2022
02979a4
fixed font error other minor changes
sumofat May 1, 2022
451eda2
Merge branch 'vendor' of https://github.com/sumofat/odin-imgui into v…
sumofat May 1, 2022
c1099a7
dont need this comment
sumofat May 1, 2022
f19413c
fixed with minimzied == true pattern for readability
sumofat May 1, 2022
ffd4ccf
elided enum references
sumofat May 1, 2022
ac79b68
Changed to use the Masks instead
sumofat May 1, 2022
aa7508c
Changed to use the mask instead
sumofat May 1, 2022
13071d0
removed unneeded
sumofat May 1, 2022
7cd4ef8
reinserted
sumofat May 1, 2022
c420998
Adds helper function for loading in a font with opengl.
May 8, 2022
20c1f64
Merge pull request #1 from NoahR02/feature/add-odin-font-example
NoahR02 May 8, 2022
54a1296
Merge remote-tracking branch 'noah/master' into vendor
sumofat May 8, 2022
02fccd1
removed fmt and win32 imports and a few others
sumofat May 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ output/*
*.obj

imgui.ini
.DS_Store
18 changes: 9 additions & 9 deletions examples/sdl_opengl/main.odin
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ main :: proc() {

log.info("Starting SDL Example...");
init_err := sdl.init(.Video);
defer sdl.quit();
//defer sdl.quit();
if init_err == 0 {
log.info("Setting up the window...");
window := sdl.create_window("odin-imgui SDL+OpenGL example", 100, 100, 1280, 720, .Open_GL|.Mouse_Focus|.Shown|.Resizable);
if window == nil {
log.debugf("Error during window creation: %s", sdl.get_error());
sdl.quit();
//log.debugf("Error during window creation: %s", sdl.get_error());
//sdl.quit();
return;
}
defer sdl.destroy_window(window);
Expand Down Expand Up @@ -68,14 +68,14 @@ main :: proc() {
imsdl.process_event(e, &imgui_state.sdl_state);
#partial switch e.type {
case .Quit:
log.info("Got SDL_QUIT event!");
running = false;
// log.info("Got SDL_QUIT event!");
// running = false;

case .Key_Down:
if is_key_down(e, .Escape) {
qe := sdl.Event{};
qe.type = .Quit;
sdl.push_event(&qe);
// if is_key_down(e, .Escape) {
// qe := sdl.Event{};
// qe.type = .Quit;
// sdl.push_event(&qe);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line needs to be commented out too, otherwise it introduces syntax errors.

(while I also not sure that this "commenting out" actually should be included in the PR, perhaps these are some forgotten "debug" changes.

if is_key_down(e, .Tab) {
io := imgui.get_io();
Expand Down
Binary file added external/cimgui.dylib
Binary file not shown.
14 changes: 12 additions & 2 deletions foreign.odin
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package imgui;

when ODIN_DEBUG {
foreign import cimgui "external/cimgui_debug.lib";
when ODIN_OS == .Windows{
foreign import cimgui "external/cimgui_debug.lib";
}
when ODIN_OS == .Darwin{
foreign import cimgui "external/cimgui_debug.dylib";
}
} else {
foreign import cimgui "external/cimgui.lib";
when ODIN_OS == .Windows{
foreign import cimgui "external/cimgui.lib";
}
when ODIN_OS == .Darwin{
foreign import cimgui "external/cimgui.dylib";
}
}

@(default_calling_convention="c")
Expand Down
71 changes: 44 additions & 27 deletions impl/opengl/opengl.odin
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import "core:mem";
import "core:log";
import "core:strings";

import gl "shared:odin-gl";
import gl "vendor:OpenGL";

import imgui "../..";
import fmt "core:fmt"
import "core:c"

OpenGL_State :: struct {
shader_program: u32,
Expand Down Expand Up @@ -63,13 +65,12 @@ setup_state :: proc(using state: ^OpenGL_State) {

gl.GenBuffers(1, &vbo_handle);
gl.GenBuffers(1, &elements_handle);

//////////////////////
// Font stuff
// Font stuff
pixels: ^u8;
width, height: i32;
imgui.font_atlas_get_tex_data_as_rgba32(io.fonts, &pixels, &width, &height);
font_tex_h: u32;
imgui.font_atlas_get_tex_data_as_rgba32(io.fonts, &pixels, &width, &height);
// Font stuff
gl.GenTextures(1, &font_tex_h);
gl.BindTexture(gl.TEXTURE_2D, font_tex_h);
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
Expand Down Expand Up @@ -125,7 +126,11 @@ imgui_render :: proc(data: ^imgui.Draw_Data, state: OpenGL_State) {
i32(clip_rect.w - clip_rect.y));
gl.BindTexture(gl.TEXTURE_2D, u32(uintptr(cmd.texture_id)));
//glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset);
gl.DrawElementsBaseVertex(gl.TRIANGLES, i32(cmd.elem_count), gl.UNSIGNED_SHORT, rawptr(uintptr(cmd.idx_offset * size_of(imgui.Draw_Idx))), i32(cmd.vtx_offset));
gl.DrawElementsBaseVertex(gl.TRIANGLES,
i32(cmd.elem_count),
gl.UNSIGNED_SHORT,
rawptr(uintptr(cmd.idx_offset * size_of(imgui.Draw_Idx))),
i32(cmd.vtx_offset));
gl.DrawElements(gl.TRIANGLES,
i32(cmd.elem_count),
gl.UNSIGNED_SHORT,
Expand Down Expand Up @@ -168,9 +173,9 @@ imgui_setup_render_state :: proc(data: ^imgui.Draw_Data, state: OpenGL_State) {
gl.EnableVertexAttribArray(u32(state.attrib_vtx_pos));
gl.EnableVertexAttribArray(u32(state.attrib_vtx_uv));
gl.EnableVertexAttribArray(u32(state.attrib_vtx_color));
gl.VertexAttribPointer(u32(state.attrib_vtx_pos), 2, gl.FLOAT, gl.FALSE, size_of(imgui.Draw_Vert), rawptr(offset_of(imgui.Draw_Vert, pos)));
gl.VertexAttribPointer(u32(state.attrib_vtx_uv), 2, gl.FLOAT, gl.FALSE, size_of(imgui.Draw_Vert), rawptr(offset_of(imgui.Draw_Vert, uv)));
gl.VertexAttribPointer(u32(state.attrib_vtx_color), 4, gl.UNSIGNED_BYTE, gl.TRUE, size_of(imgui.Draw_Vert), rawptr(offset_of(imgui.Draw_Vert, col)));
gl.VertexAttribPointer(u32(state.attrib_vtx_pos), 2, gl.FLOAT, gl.FALSE, size_of(imgui.Draw_Vert), offset_of(imgui.Draw_Vert, pos));
gl.VertexAttribPointer(u32(state.attrib_vtx_uv), 2, gl.FLOAT, gl.FALSE, size_of(imgui.Draw_Vert), offset_of(imgui.Draw_Vert, uv));
gl.VertexAttribPointer(u32(state.attrib_vtx_color), 4, gl.UNSIGNED_BYTE, gl.TRUE, size_of(imgui.Draw_Vert), offset_of(imgui.Draw_Vert, col));
}

backup_opengl_state :: proc(state: ^OpenGL_Backup_State) {
Expand All @@ -192,10 +197,10 @@ backup_opengl_state :: proc(state: ^OpenGL_Backup_State) {
gl.GetIntegerv(gl.BLEND_EQUATION_RGB, &state.last_blend_equation_rgb);
gl.GetIntegerv(gl.BLEND_EQUATION_ALPHA, &state.last_blend_equation_alpha);

state.last_enabled_blend = gl.IsEnabled(gl.BLEND) != 0;
state.last_enable_cull_face = gl.IsEnabled(gl.CULL_FACE) != 0;
state.last_enable_depth_test = gl.IsEnabled(gl.DEPTH_TEST) != 0;
state.last_enable_scissor_test = gl.IsEnabled(gl.SCISSOR_TEST) != 0;
state.last_enabled_blend = gl.IsEnabled(gl.BLEND) != false;
state.last_enable_cull_face = gl.IsEnabled(gl.CULL_FACE) != false;
state.last_enable_depth_test = gl.IsEnabled(gl.DEPTH_TEST) != false;
state.last_enable_scissor_test = gl.IsEnabled(gl.SCISSOR_TEST) != false;
}

restore_opengl_state :: proc(state: OpenGL_Backup_State) {
Expand All @@ -211,10 +216,10 @@ restore_opengl_state :: proc(state: OpenGL_Backup_State) {
u32(state.last_blend_src_alpha),
u32(state.last_blend_dst_alpha));

if state.last_enabled_blend do gl.Enable(gl.BLEND) else do gl.Disable(gl.BLEND);
if state.last_enable_cull_face do gl.Enable(gl.CULL_FACE) else do gl.Disable(gl.CULL_FACE);
if state.last_enable_depth_test do gl.Enable(gl.DEPTH_TEST) else do gl.Disable(gl.DEPTH_TEST);
if state.last_enable_scissor_test do gl.Enable(gl.SCISSOR_TEST) else do gl.Disable(gl.SCISSOR_TEST);
if state.last_enabled_blend{ gl.Enable(gl.BLEND)} else{ gl.Disable(gl.BLEND)}
if state.last_enable_cull_face {gl.Enable(gl.CULL_FACE)} else {gl.Disable(gl.CULL_FACE)}
if state.last_enable_depth_test {gl.Enable(gl.DEPTH_TEST) }else {gl.Disable(gl.DEPTH_TEST)}
if state.last_enable_scissor_test {gl.Enable(gl.SCISSOR_TEST)} else {gl.Disable(gl.SCISSOR_TEST)}

gl.PolygonMode(gl.FRONT_AND_BACK, u32(state.last_polygon_mode[0]));
gl.Viewport(state.last_viewport[0], state.last_viewport[1], state.last_viewport[2], state.last_viewport[3]);
Expand All @@ -223,16 +228,23 @@ restore_opengl_state :: proc(state: OpenGL_Backup_State) {

@(private="package")
compile_shader :: proc(kind: u32, shader_src: string) -> u32 {
fmt.println(shader_src)
h := gl.CreateShader(kind);
data := cast(^u8)strings.clone_to_cstring(shader_src, context.temp_allocator);
data := strings.clone_to_cstring(shader_src, context.temp_allocator);
gl.ShaderSource(h, 1, &data, nil);
gl.CompileShader(h);
ok: i32;
gl.GetShaderiv(h, gl.COMPILE_STATUS, &ok);
if ok != gl.TRUE {
log.errorf("Unable to compile shader: {}", h);
return 0;
}
ok: c.int;
gl.GetShaderiv(h, gl.COMPILE_STATUS,&ok);

if ok != 1{
log_length : c.int
gl.GetShaderiv(h, gl.INFO_LOG_LENGTH, &log_length)
log_bytes := make([]byte, log_length); defer delete(log_bytes)
gl.GetShaderInfoLog(h, log_length, &log_length, raw_data(log_bytes))
fmt.println(string(log_bytes))
log.errorf("Unable to compile shader: {}", h);
return 0;
}

return h;
}
Expand All @@ -243,13 +255,18 @@ setup_imgui_shaders :: proc() -> u32 {
frag_h := compile_shader(gl.FRAGMENT_SHADER, frag_shader_src);

program_h := gl.CreateProgram();
fmt.println(program_h)
fmt.println(frag_h)
fmt.println(vert_h)
gl.AttachShader(program_h, frag_h);

gl.AttachShader(program_h, vert_h);
gl.LinkProgram(program_h);

ok: i32;
gl.GetProgramiv(program_h, gl.LINK_STATUS, &ok);
if ok != gl.TRUE {

if ok != 1{
log.errorf("Error linking program: {}", program_h);
}

Expand All @@ -258,7 +275,7 @@ setup_imgui_shaders :: proc() -> u32 {

@(private="package")
frag_shader_src :: `
#version 450
#version 410
in vec2 Frag_UV;
in vec4 Frag_Color;
uniform sampler2D Texture;
Expand All @@ -271,7 +288,7 @@ void main()

@(private="package")
vert_shader_src :: `
#version 450
#version 410
layout (location = 0) in vec2 Position;
layout (location = 1) in vec2 UV;
layout (location = 2) in vec4 Color;
Expand Down
Loading