Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace do with {} in header.odin #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ cimgui:
$(CC) $(CIMGUI_FLAGS) /MTd /Zi /Fd:$(EXTERNAL_LIB_DIR)/cimgui_debug.pdb $(CIMGUI_SRC)
$(LINK) /nologo $(CIMGUI_OBJS) /out:$(EXTERNAL_LIB_DIR)/cimgui_debug.lib
rm *.obj

$(CC) $(CIMGUI_FLAGS) /MT /O2 $(CIMGUI_SRC)
$(LINK) /nologo $(CIMGUI_OBJS) /out:$(EXTERNAL_LIB_DIR)/cimgui.lib
rm *.obj
Binary file added external/libcimgui.a
Binary file not shown.
10 changes: 7 additions & 3 deletions foreign.odin
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package imgui;

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

@(default_calling_convention="c")
Expand Down
1,114 changes: 557 additions & 557 deletions header.odin

Large diffs are not rendered by default.

95 changes: 48 additions & 47 deletions impl/glfw/glfw.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package imgui_impl_glfw

import "core:runtime"
import "core:strings"
import glfw "shared:odin-glfw"

import glfw "vendor:glfw"

import imgui "../..";

@private
state: GLFW_State;

GLFW_State :: struct {
window: glfw.Window_Handle,
window: glfw.WindowHandle,
time: f64,
mouse_just_pressed: [imgui.Mouse_Button.Count]bool,
mouse_cursors: [imgui.Mouse_Cursor.Count]glfw.Cursor_Handle,
mouse_cursors: [imgui.Mouse_Cursor.Count]glfw.CursorHandle,
installed_callbacks: bool,
prev_user_callback_mouse_button: glfw.Mouse_Button_Proc,
prev_user_callback_scroll: glfw.Scroll_Proc,
prev_user_callback_key: glfw.Key_Proc,
prev_user_callback_char: glfw.Char_Proc,
prev_user_callback_mouse_button: glfw.MouseButtonProc,
prev_user_callback_scroll: glfw.ScrollProc,
prev_user_callback_key: glfw.KeyProc,
prev_user_callback_char: glfw.CharProc,
}

setup_state :: proc(window: glfw.Window_Handle, install_callbacks: bool) {
setup_state :: proc(window: glfw.WindowHandle, install_callbacks: bool) {
state.window = window;
state.time = 0.0;

Expand Down Expand Up @@ -57,30 +58,30 @@ setup_state :: proc(window: glfw.Window_Handle, install_callbacks: bool) {
io.set_clipboard_text_fn = set_clipboard_text;
io.clipboard_user_data = state.window;

when ODIN_OS == "windows" {
io.ime_window_handle = rawptr(glfw.get_win32_window(state.window));
when ODIN_OS == .Windows {
io.ime_window_handle = rawptr(glfw.GetWin32Window(state.window));
}

prev_error_callback: glfw.Error_Proc = glfw.set_error_callback(nil);
prev_error_callback: glfw.ErrorProc = glfw.SetErrorCallback(nil);

state.mouse_cursors[imgui.Mouse_Cursor.Arrow] = glfw.create_standard_cursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.TextInput] = glfw.create_standard_cursor(glfw.IBEAM_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNs] = glfw.create_standard_cursor(glfw.VRESIZE_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeEw] = glfw.create_standard_cursor(glfw.HRESIZE_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.Hand] = glfw.create_standard_cursor(glfw.HAND_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.Arrow] = glfw.CreateStandardCursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.TextInput] = glfw.CreateStandardCursor(glfw.IBEAM_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNs] = glfw.CreateStandardCursor(glfw.VRESIZE_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeEw] = glfw.CreateStandardCursor(glfw.HRESIZE_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.Hand] = glfw.CreateStandardCursor(glfw.HAND_CURSOR);

/* GLFW 3.4 cursors (not supported by odin-glfw yet)
state.mouse_cursors[imgui.Mouse_Cursor.ResizeAll] = glfw.create_standard_cursor(glfw.RESIZE_ALL_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNESW] = glfw.create_standard_cursor(glfw.RESIZE_NESW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNWSE] = glfw.create_standard_cursor(glfw.RESIZE_NWSE_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.NotAllowed] = glfw.create_standard_cursor(glfw.NOT_ALLOWED_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeAll] = glfw.CreateStandardCursor(glfw.RESIZE_ALL_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNESW] = glfw.CreateStandardCursor(glfw.RESIZE_NESW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNWSE] = glfw.CreateStandardCursor(glfw.RESIZE_NWSE_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.NotAllowed] = glfw.CreateStandardCursor(glfw.NOT_ALLOWED_CURSOR);
*/
state.mouse_cursors[imgui.Mouse_Cursor.ResizeAll] = glfw.create_standard_cursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNesw] = glfw.create_standard_cursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNwse] = glfw.create_standard_cursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.NotAllowed] = glfw.create_standard_cursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeAll] = glfw.CreateStandardCursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNesw] = glfw.CreateStandardCursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.ResizeNwse] = glfw.CreateStandardCursor(glfw.ARROW_CURSOR);
state.mouse_cursors[imgui.Mouse_Cursor.NotAllowed] = glfw.CreateStandardCursor(glfw.ARROW_CURSOR);

glfw.set_error_callback(prev_error_callback);
glfw.SetErrorCallback(prev_error_callback);

state.prev_user_callback_mouse_button = nil;
state.prev_user_callback_scroll = nil;
Expand All @@ -89,63 +90,63 @@ setup_state :: proc(window: glfw.Window_Handle, install_callbacks: bool) {
if (install_callbacks)
{
state.installed_callbacks = true;
state.prev_user_callback_mouse_button = glfw.set_mouse_button_callback(window, mouse_button_callback);
state.prev_user_callback_scroll = glfw.set_scroll_callback(window, scroll_callback);
state.prev_user_callback_key = glfw.set_key_callback(window, key_callback);
state.prev_user_callback_char = glfw.set_char_callback(window, char_callback);
state.prev_user_callback_mouse_button = glfw.SetMouseButtonCallback(window, mouse_button_callback);
state.prev_user_callback_scroll = glfw.SetScrollCallback(window, scroll_callback);
state.prev_user_callback_key = glfw.SetKeyCallback(window, key_callback);
state.prev_user_callback_char = glfw.SetCharCallback(window, char_callback);
}
}

update_mouse :: proc() {
io := imgui.get_io();

for i in 0..<len(io.mouse_down) {
io.mouse_down[i] = state.mouse_just_pressed[i] || glfw.get_mouse_button(state.window, glfw.Mouse_Button(i)) != .RELEASE;
io.mouse_down[i] = state.mouse_just_pressed[i] || glfw.GetMouseButton(state.window, i32(i)) != glfw.RELEASE;
state.mouse_just_pressed[i] = false;
}

mouse_pos_backup := io.mouse_pos;
io.mouse_pos = { min(f32), min(f32) };

if glfw.get_window_attrib(state.window, .FOCUSED) != 0 {
if glfw.GetWindowAttrib(state.window, glfw.FOCUSED) != 0 {
if io.want_set_mouse_pos {
glfw.set_cursor_pos(state.window, f64(mouse_pos_backup.x), f64(mouse_pos_backup.y));
glfw.SetCursorPos(state.window, f64(mouse_pos_backup.x), f64(mouse_pos_backup.y));
} else {
x, y := glfw.get_cursor_pos(state.window);
x, y := glfw.GetCursorPos(state.window);
io.mouse_pos = { f32(x), f32(y) };
}
}

if io.config_flags & .NoMouseCursorChange != .NoMouseCursorChange {
desired_cursor := imgui.get_mouse_cursor();
if(io.mouse_draw_cursor || desired_cursor == .None) {
glfw.set_input_mode(state.window, .CURSOR, int(glfw.CURSOR_HIDDEN));
glfw.SetInputMode(state.window, glfw.CURSOR, glfw.CURSOR_HIDDEN);
} else {
new_cursor: glfw.Cursor_Handle;
new_cursor: glfw.CursorHandle;
if state.mouse_cursors[desired_cursor] != nil {
new_cursor = state.mouse_cursors[desired_cursor];
} else {
new_cursor = state.mouse_cursors[imgui.Mouse_Cursor.Arrow];
}
glfw.set_cursor(state.window, new_cursor);
glfw.set_input_mode(state.window, .CURSOR, int(glfw.CURSOR_NORMAL));
glfw.SetCursor(state.window, new_cursor);
glfw.SetInputMode(state.window, glfw.CURSOR, glfw.CURSOR_NORMAL);
}
}
}

update_display_size :: proc() {
w, h := glfw.get_window_size(state.window);
w, h := glfw.GetWindowSize(state.window);
io := imgui.get_io();
io.display_size = { f32(w), f32(h) };
if w > 0 && h > 0 {
display_w, display_h := glfw.get_framebuffer_size(state.window);
display_w, display_h := glfw.GetFramebufferSize(state.window);
io.display_framebuffer_scale = { f32(display_w) / f32(w), f32(display_h) / f32(h) };
}
}

update_dt :: proc() {
io := imgui.get_io();
now := glfw.get_time();
now := glfw.GetTime();
io.delta_time = state.time > 0.0 ? f32(now - state.time) : f32(1.0/60.0);
state.time = now;
}
Expand All @@ -154,19 +155,19 @@ update_dt :: proc() {
get_clipboard_text :: proc "c" (user_data: rawptr) -> cstring
{
context = runtime.default_context();
s := glfw.get_clipboard_string(glfw.Window_Handle(user_data));
s := glfw.GetClipboardString(glfw.WindowHandle(user_data));
return strings.unsafe_string_to_cstring(s);
}

@private
set_clipboard_text :: proc "c" (user_data: rawptr, text: cstring)
{
context = runtime.default_context();
glfw.set_clipboard_string(glfw.Window_Handle(user_data), string(text));
glfw.SetClipboardString(glfw.WindowHandle(user_data), text);
}

@private
key_callback :: proc "c" (window: glfw.Window_Handle, key, scancode, action, mods: i32) {
key_callback :: proc "c" (window: glfw.WindowHandle, key, scancode, action, mods: i32) {
context = runtime.default_context();

if (state.prev_user_callback_key != nil) {
Expand All @@ -182,15 +183,15 @@ key_callback :: proc "c" (window: glfw.Window_Handle, key, scancode, action, mod
io.key_shift = io.keys_down[glfw.KEY_LEFT_SHIFT] || io.keys_down[glfw.KEY_RIGHT_SHIFT];
io.key_alt = io.keys_down[glfw.KEY_LEFT_ALT] || io.keys_down[glfw.KEY_RIGHT_ALT];

when ODIN_OS == "windows" {
when ODIN_OS == .Windows {
io.key_super = false;
} else {
io.key_super = io.keys_down[glfw.KEY_LEFT_SUPER] || io.keys_down[glfw.KEY_RIGHT_SUPER];
}
}

@private
mouse_button_callback :: proc "c" (window: glfw.Window_Handle, button, action, mods: i32) {
mouse_button_callback :: proc "c" (window: glfw.WindowHandle, button, action, mods: i32) {
context = runtime.default_context();

if (state.prev_user_callback_mouse_button != nil) {
Expand All @@ -203,7 +204,7 @@ mouse_button_callback :: proc "c" (window: glfw.Window_Handle, button, action, m
}

@private
scroll_callback :: proc "c" (window: glfw.Window_Handle, xoffset, yoffset: f64) {
scroll_callback :: proc "c" (window: glfw.WindowHandle, xoffset, yoffset: f64) {
context = runtime.default_context();

if (state.prev_user_callback_scroll != nil) {
Expand All @@ -216,7 +217,7 @@ scroll_callback :: proc "c" (window: glfw.Window_Handle, xoffset, yoffset: f64)
}

@private
char_callback :: proc "c" (window: glfw.Window_Handle, codepoint: rune) {
char_callback :: proc "c" (window: glfw.WindowHandle, codepoint: rune) {
context = runtime.default_context();

if (state.prev_user_callback_char != nil) {
Expand Down
51 changes: 35 additions & 16 deletions impl/opengl/opengl.odin
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "core:mem";
import "core:log";
import "core:strings";

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

import imgui "../..";

Expand Down Expand Up @@ -168,9 +168,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), uintptr(offset_of(imgui.Draw_Vert, pos)));
gl.VertexAttribPointer(u32(state.attrib_vtx_uv), 2, gl.FLOAT, gl.FALSE, size_of(imgui.Draw_Vert), uintptr(offset_of(imgui.Draw_Vert, uv)));
gl.VertexAttribPointer(u32(state.attrib_vtx_color), 4, gl.UNSIGNED_BYTE, gl.TRUE, size_of(imgui.Draw_Vert), uintptr(offset_of(imgui.Draw_Vert, col)));
}

backup_opengl_state :: proc(state: ^OpenGL_Backup_State) {
Expand All @@ -192,10 +192,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);
state.last_enable_cull_face = gl.IsEnabled(gl.CULL_FACE);
state.last_enable_depth_test = gl.IsEnabled(gl.DEPTH_TEST);
state.last_enable_scissor_test = gl.IsEnabled(gl.SCISSOR_TEST);
}

restore_opengl_state :: proc(state: OpenGL_Backup_State) {
Expand All @@ -211,10 +211,29 @@ 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 @@ -225,11 +244,11 @@ restore_opengl_state :: proc(state: OpenGL_Backup_State) {
compile_shader :: proc(kind: u32, shader_src: string) -> u32 {
h := gl.CreateShader(kind);
data := cast(^u8)strings.clone_to_cstring(shader_src, context.temp_allocator);
gl.ShaderSource(h, 1, &data, nil);
gl.ShaderSource(h, 1, cast([^]cstring) &data, nil);
gl.CompileShader(h);
ok: i32;
gl.GetShaderiv(h, gl.COMPILE_STATUS, &ok);
if ok != gl.TRUE {
if ok == 0 {
log.errorf("Unable to compile shader: {}", h);
return 0;
}
Expand All @@ -246,10 +265,10 @@ setup_imgui_shaders :: proc() -> u32 {
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 == 0 {
log.errorf("Error linking program: {}", program_h);
}

Expand Down