-
Notifications
You must be signed in to change notification settings - Fork 0
/
hot_reload_example.odin
169 lines (137 loc) · 4.83 KB
/
hot_reload_example.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main;
import "core:fmt"
import "core:log";
import sdl "vendor:sdl2";
import gl "vendor:OpenGL";
import imgui "odin-imgui";
import imgl "odin-imgui/impl/opengl";
import imsdl "odin-imgui/impl/sdl";
import hr "hot_reload"
enum_filename :: "enum.odin"
package_name :: "main"
DESIRED_GL_MAJOR_VERSION :: 4;
DESIRED_GL_MINOR_VERSION :: 5;
main :: proc() {
logger_opts := log.Options {
.Level,
.Line,
.Procedure,
};
context.logger = log.create_console_logger(opt = logger_opts);
log.info("Starting SDL Example...");
init_err := sdl.Init({.VIDEO});
defer sdl.Quit();
if init_err != 0 {
log.debugf("Error during SDL init: (%d)%s", init_err, sdl.GetError());
return
}
log.info("Setting up the window...");
window := sdl.CreateWindow("odin-imgui SDL+OpenGL example", 100, 100, 1280, 720, { .OPENGL, .MOUSE_FOCUS, .SHOWN, .RESIZABLE});
if window == nil {
log.debugf("Error during window creation: %s", sdl.GetError());
sdl.Quit();
return;
}
defer sdl.DestroyWindow(window);
log.info("Setting up the OpenGL...");
sdl.GL_SetAttribute(.CONTEXT_MAJOR_VERSION, DESIRED_GL_MAJOR_VERSION);
sdl.GL_SetAttribute(.CONTEXT_MINOR_VERSION, DESIRED_GL_MINOR_VERSION);
sdl.GL_SetAttribute(.CONTEXT_PROFILE_MASK, i32(sdl.GLprofile.CORE));
sdl.GL_SetAttribute(.DOUBLEBUFFER, 1);
sdl.GL_SetAttribute(.DEPTH_SIZE, 24);
sdl.GL_SetAttribute(.STENCIL_SIZE, 8);
gl_ctx := sdl.GL_CreateContext(window);
if gl_ctx == nil {
log.debugf("Error during window creation: %s", sdl.GetError());
return;
}
sdl.GL_MakeCurrent(window, gl_ctx);
defer sdl.GL_DeleteContext(gl_ctx);
if sdl.GL_SetSwapInterval(1) != 0 {
log.debugf("Error during window creation: %s", sdl.GetError());
return;
}
gl.load_up_to(DESIRED_GL_MAJOR_VERSION, DESIRED_GL_MINOR_VERSION, sdl.gl_set_proc_address);
gl.ClearColor(0.25, 0.25, 0.25, 1);
imgui_state := init_imgui_state(window);
running := true;
show_demo_window := false;
e := sdl.Event{};
using hr
gs:GreetStruct
hotlib_init(&gs, "greet")
hotlib_load(&gs)
hotlib_update(&gs)
fmt.printf("GronkQWRQT!")
for running {
for sdl.PollEvent(&e) {
imsdl.process_event(e, &imgui_state.sdl_state);
#partial switch e.type {
case .QUIT:
log.info("Got SDL_QUIT event!");
running = false;
case .KEYDOWN: #partial switch e.key.keysym.sym {
case .ESCAPE: sdl.PushEvent(&sdl.Event{type = .QUIT});
case .TAB: if imgui.get_io().want_capture_keyboard == false do show_demo_window = true;
}
}
}
imgui_new_frame(window, &imgui_state);
imgui.new_frame();
info_overlay();
if show_demo_window do imgui.show_demo_window(&show_demo_window);
hot_reload_window(&gs)
gs.greet()
imgui.render();
io := imgui.get_io();
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);
sdl.GL_SwapWindow(window);
}
log.info("Shutting down...");
}
info_overlay :: proc() {
imgui.set_next_window_pos(imgui.Vec2{10, 10});
imgui.set_next_window_bg_alpha(0.2);
overlay_flags: imgui.Window_Flags = .NoDecoration |
.AlwaysAutoResize |
.NoSavedSettings |
.NoFocusOnAppearing |
.NoNav |
.NoMove;
imgui.begin("Info", nil, overlay_flags);
imgui.text_unformatted("Press Esc to close the application");
imgui.text_unformatted("Press Tab to show demo window");
imgui.end();
}
Imgui_State :: struct {
sdl_state: imsdl.SDL_State,
opengl_state: imgl.OpenGL_State,
}
init_imgui_state :: proc(window: ^sdl.Window) -> Imgui_State {
using res := Imgui_State{};
imgui.create_context();
imgui.style_colors_dark();
imsdl.setup_state(&res.sdl_state);
imgl.setup_state(&res.opengl_state);
return res;
}
imgui_new_frame :: proc(window: ^sdl.Window, state: ^Imgui_State) {
imsdl.update_display_size(window);
imsdl.update_mouse(&state.sdl_state, window);
imsdl.update_dt(&state.sdl_state);
}
hot_reload_window :: proc (gs:^hr.GreetStruct) {
using hr
imgui.begin("Reload the DLL");
imgui.text("Honk")
if imgui.button("Reload DLL!") {
hotlib_unload(gs)
hotlib_compile_dll(gs)
hotlib_load(gs)
hotlib_update(gs)
}
imgui.end()
}