Skip to content

Instantly share code, notes, and snippets.

@jmwright
Created August 1, 2022 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmwright/22b28de8f30764a76e75b9ba1fc7434a to your computer and use it in GitHub Desktop.
Save jmwright/22b28de8f30764a76e75b9ba1fc7434a to your computer and use it in GitHub Desktop.
odin_imgui_example
package main
import "core:log"
import "core:runtime"
import gl "vendor:OpenGL"
// import glfw "shared:odin-glfw"
import "vendor:glfw"
DESIRED_GL_MAJOR_VERSION :: 4;
DESIRED_GL_MINOR_VERSION :: 5;
import imgui "../odin-imgui";
import imgl "../odin-imgui/impl/opengl";
import imglfw "../odin-imgui/impl/glfw";
main :: proc() {
// Make sure we can set up GLFW
if !bool(glfw.Init()) {
desc, err := glfw.GetError();
log.error("GLFW init failed:", err, desc);
return;
}
defer glfw.Terminate();
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, DESIRED_GL_MAJOR_VERSION);
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, DESIRED_GL_MINOR_VERSION);
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE);
glfw.WindowHint(glfw.DOUBLEBUFFER, 1);
glfw.WindowHint(glfw.DEPTH_BITS, 24);
glfw.WindowHint(glfw.STENCIL_BITS, 8);
window := glfw.CreateWindow(1280, 720, "GitBuilding UI", nil, nil);
defer glfw.DestroyWindow(window);
glfw.SetErrorCallback(error_callback);
// glfw.set_key_callback(window, key_callback);
glfw.MakeContextCurrent(window);
glfw.SwapInterval(1);
gl.load_up_to(DESIRED_GL_MAJOR_VERSION, DESIRED_GL_MINOR_VERSION, proc(p: rawptr, name: cstring) { (cast(^rawptr)p)^ = glfw.GetProcAddress(name) });
gl.ClearColor(0.25, 0.25, 0.25, 1);
// This line throws the following error
// /usr/bin/ld: /home/jwright/Downloads/gbui/gbui.o: in function `imgui_impl_opengl.ImGui_ImplOpenGL3_InitPlatformInterface':
// odin_package:(.text+0x17934): undefined reference to `igGetPlatformIO'
// clang: error: linker command failed with exit code 1 (use -v to see invocation)
imgui_state := init_imgui_state(window);
// io := imgui.get_io();
for !glfw.WindowShouldClose(window) {
/*gl.Viewport(0, 0, i32(io.display_size.x), i32(io.display_size.y));
gl.Scissor(0, 0, i32(io.display_size.x), i32(io.display_size.y));
gl.Clear(gl.COLOR_BUFFER_BIT);
imgl.imgui_render(imgui.get_draw_data(), imgui_state.opengl_state);*/
glfw.SwapBuffers(window);
glfw.PollEvents();
}
}
get_logger :: proc() -> log.Logger {
logger_opts := log.Options {
.Level,
.Line,
.Procedure,
};
return log.create_console_logger(opt = logger_opts);
}
error_callback :: proc "c" (error: i32, description: cstring) {
context = runtime.default_context();
context.logger = get_logger();
log.error("GLFW error:", error, description);
}
Imgui_State :: struct {
opengl_state: imgl.OpenGL_State,
}
init_imgui_state :: proc(window: glfw.WindowHandle) -> Imgui_State {
res := Imgui_State{};
imgui.create_context();
imgui.style_colors_dark();
imglfw.setup_state(window, true);
imgl.setup_state(&res.opengl_state);
return res;
}
imgui_new_frame :: proc() {
imglfw.update_display_size();
imglfw.update_mouse();
imglfw.update_dt();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment