-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdl.odin
More file actions
356 lines (280 loc) · 11.3 KB
/
Copy pathsdl.odin
File metadata and controls
356 lines (280 loc) · 11.3 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import sdl "vendor:sdl2"
import "vendor:sdl2/ttf"
import "vendor:sdl2/image"
import "core:math"
import "core:fmt"
Texture :: ^sdl.Texture
Surface :: ^sdl.Surface
Font :: ^ttf.Font
Window :: ^sdl.Window
Renderer :: ^sdl.Renderer
Event :: sdl.Event
Color :: sdl.Color
Key :: sdl.Keycode
start_text_input :: sdl.StartTextInput
stop_text_input :: sdl.StopTextInput
destroy_texture :: sdl.DestroyTexture
sleep :: sdl.Delay
// =======================================================================================
// ================================ WINDOW SETUP & RESIZE ================================
// =======================================================================================
// {{{
// just for performance
frame_start_time : Tick
frame_time_taken : Duration
other_frame_times : Duration
@(private="file")
next_frame_target: u32
take_break :: proc() {
now := sdl.GetTicks()
breaktime := max(next_frame_target - now, 0)
if breaktime < 1000 do sleep(breaktime)
}
EMBEDDED_FONTS: [] [] byte = {
#load("font-regular.ttf"),
#load("font-monospace.ttf")
}
init_graphics :: proc() -> bool {
window.size = { 1280, 720 }
assert( sdl.Init(sdl.INIT_VIDEO) >= 0, "Failed to initialize SDL!" )
assert( sdl.CreateWindowAndRenderer(window.size.x, window.size.y, { .RESIZABLE, .SHOWN, .OPENGL if ODIN_OS != .Darwin else .METAL },
&window.handle, &window.renderer) >= 0, "Failed to start program!" )
assert( ttf.Init() >= 0, "Failed to get True Type Font support" )
sdl.SetWindowTitle(window.handle, "oi")
FONTS[.REGULAR] = load_font(0, CONFIG_FONT_SIZE)
FONTS[.MONO] = load_font(1, CONFIG_FONT_SIZE)
FONTS[.LARGE] = load_font(0, CONFIG_LARGE_FONT_SIZE)
sdl.SetRenderDrawBlendMode(window.renderer, .BLEND)
sdl.SetWindowMinimumSize(window.handle, 600, 200)
setup_window_icon()
return true
}
poll_events :: proc() {
event: sdl.Event
for sdl.PollEvent(&event) {
#partial switch event.type {
case .RENDER_TARGETS_RESET: rerender_all_codeblocks()
case .QUIT: window.should_exit = true
case .WINDOWEVENT: if event.window.event == .RESIZED { handle_resize() }
case .MOUSEWHEEL: window.events.scroll = { -event.wheel.x, event.wheel.y }
case .MOUSEBUTTONDOWN:
window.events.click = auto_cast event.button.button
window.pressed = auto_cast event.button.button
case .MOUSEBUTTONUP:
window.pressed = .NONE
case .KEYDOWN, .TEXTINPUT:
when ODIN_OS == .Darwin {
handle_keypress(event)
if window.active_input != nil {
handle_keyboard_in_text_input(window.active_input, event)
}
}
case:
}
}
sdl.GetMouseState(&window.mouse.x, &window.mouse.y)
window.events.base = event
}
begin_frame :: proc() {
window_w, window_h: i32
drawable_w, drawable_h: i32
sdl.GetWindowSize(window.handle, &window_w, &window_h)
sdl.GL_GetDrawableSize(window.handle, &drawable_w, &drawable_h)
scale_x := f32(drawable_w) / f32(window_w)
scale_y := f32(drawable_h) / f32(window_h)
sdl.SetRenderDrawColor(window.renderer, 0, 0, 0, 255)
sdl.RenderSetScale(window.renderer, scale_x, scale_y)
sdl.RenderClear(window.renderer)
frame_start_time = tick_now()
window.onframe = true
}
end_frame :: proc() {
window.onframe = false
frame_time_taken = tick_diff(frame_start_time, tick_now())
other_frame_times = get_smoothed_frame_time()
debug.box_drawn = 0
sdl.RenderPresent(window.renderer)
take_break()
next_frame_target += (1000 / CONFIG_MAX_FPS)
window.events = {}
window.frames += 1
free_all(context.temp_allocator)
}
handle_resize :: proc() {
prev_size := window.size
sdl.GetRendererOutputSize(window.renderer, &window.size.x, &window.size.y)
delta_size := prev_size - window.size
if math.abs(delta_size.x) > 2 || math.abs(delta_size.y) > 2 {
window.should_relayout = true
}
}
// its a mess, but also, just don't look, i guess
@(private="file") packages_done: int
@(private="file") done_packages: [dynamic] string
recache :: proc() {// {{{
do_async(proc(task: Task) {
cache_everything(&progress_metrics.the_recaching, &done_packages)
})
mid_frame := window.onframe
if mid_frame do end_frame()
Message :: struct { tex: Texture, size: Vector }
message : Message
messages : [dynamic] Message
sum_height :: proc(msgs: [dynamic] Message) -> (height: i32) {
for m in msgs do height += m.size.y; return
}
message.tex, message.size = render_text("CREATING ODIN PACKAGE CACHE:", .LARGE, .FG2)
append(&messages, message)
append(&messages, Message {})
message.tex, message.size = render_text("Please restart 'oi' to see changes...", .MONO, .FG2)
append(&messages, message)
prev_cache_at_frame := window.frames
prev_cache_progress := 0
for !window.should_exit {
poll_events()
begin_frame()
defer end_frame()
if window.should_relayout do update_layout()
draw_window()
for i in 0..<10 { pop_box_from_any_queue() }
progress := progress_metrics.the_recaching
resume: bool
// resume |= window.frames - prev_cache_at_frame > CONFIG_MAX_FPS * 2
resume |= progress[0] >= progress[1]
draw_rectangle({ 0, 0 }, window.size, .BG3)
if messages[1].tex != nil { destroy_texture(messages[1].tex) }
messages[1].tex, messages[1].size = render_text(
fmt.aprintf("progress: %d / %d", progress[0], progress[1], allocator = context.temp_allocator),
.MONO, .FG2)
for i in packages_done..<len(done_packages) {
message.tex, message.size = render_text(
fmt.aprintf("cached %s", done_packages[i], allocator = context.temp_allocator),
.MONO, .FG2)
append(&messages, message)
}
packages_done = len(done_packages)
pos: Vector = { 8, min(8, window.size.y - sum_height(messages)) }
for msg in messages {
draw_texture(pos, msg.size, msg.tex)
pos.y += msg.size.y
}
if resume {
sleep(200)
break
}
if prev_cache_progress != progress[0] do prev_cache_at_frame = window.frames
prev_cache_progress = progress[0]
}
if mid_frame do begin_frame()
for message in messages { destroy_texture(message.tex) }
delete(messages)
progress_metrics.the_recaching = {}
}// }}}
setup_window_icon :: proc() {
icon_data := #load("icon.png")
image_data := sdl.RWFromConstMem(raw_data(icon_data), auto_cast len(icon_data))
icon := image.Load_RW(image_data, false)
sdl.SetWindowIcon(window.handle, icon)
}
load_font :: proc(font_index: int, size: i32) -> Font {
the_font := EMBEDDED_FONTS[font_index]
font_data := sdl.RWFromConstMem(raw_data(the_font), auto_cast len(the_font))
return ttf.OpenFontRW(font_data, false, size)
}
// }}}
// =======================================================================================
// ================================ DRAWING PROCS ================================
// =======================================================================================
draw_rectangle :: proc(pos, size: Vector, color: Palette) {
rgba := COLORS[color]
if color == .TRANSPARENT do return
sdl.SetRenderDrawColor(window.renderer, rgba.r, rgba.g, rgba.b, rgba.a)
sdl.RenderFillRect(window.renderer, &{ pos.x, pos.y, size.x, size.y })
}
draw_rectangle_rgba :: proc(pos, size: Vector, color: Color) {
sdl.SetRenderDrawColor(window.renderer, color.r, color.g, color.b, color.a)
sdl.RenderFillRect(window.renderer, &{ pos.x, pos.y, size.x, size.y })
}
draw_two_lines_rgba :: proc(pos, size: Vector, color: Color) {
v2p :: proc(v: Vector) -> sdl.Point { return transmute(sdl.Point) v }
sdl.SetRenderDrawColor(window.renderer, color.r, color.g, color.b, color.a)
pos, size := pos, size-1
points: [4] sdl.Point = { v2p(pos), { pos.x + size.x, pos.y }, v2p(pos), { pos.x, pos.y + size.y } }
sdl.RenderDrawLines(window.renderer, auto_cast &points, len(points))
}
draw_line_rgba :: proc(pos, size: Vector, color: Color) {
sdl.SetRenderDrawColor(window.renderer, color.r, color.g, color.b, color.a)
sdl.RenderDrawLine(window.renderer, pos.x, pos.y, size.x, size.y)
}
draw_texture :: proc(pos, size: Vector, texture: Texture) {
sdl.RenderCopy(window.renderer, texture, &{ 0, 0, size.x, size.y }, &{ pos.x, pos.y, size.x, size.y })
}
render_text :: proc(text: string, font: FontType, color: Palette) -> (t: Texture, size: Vector) {
if len(text) == 0 do return
rgba := COLORS[color]
font := FONTS[font]
surface := ttf.RenderUTF8_Blended_Wrapped(font, cstr(text), rgba, 0)
defer sdl.FreeSurface(surface)
texture := sdl.CreateTextureFromSurface(window.renderer, surface)
sdl.SetTextureBlendMode(texture, .BLEND)
return texture, { surface.w, surface.h }
}
render_text_onto :: proc(out: Surface, pos: Vector, text: string, font: FontType, color: Palette) -> (size: Vector) {
rgba := COLORS[color]
font := FONTS[font]
surface := ttf.RenderUTF8_Blended_Wrapped(font, cstr(text), rgba, 0)
defer sdl.FreeSurface(surface)
sdl.BlitSurface(surface, &surface.clip_rect, out, &{ pos.x, pos.y, surface.w, surface.h })
return { surface.w, surface.h }
}
measure_rune_advance :: proc(r: rune, font: FontType) -> int {
font := FONTS[font]
minx, maxx, miny, maxy, advance: i32
glyph := ttf.GlyphMetrics32(font, r, &minx, &maxx, &miny, &maxy, &advance)
return int(advance)
}
measure_text :: proc(text: string, font: FontType) -> Vector {
font := FONTS[font]
width, height: i32
glyph := ttf.SizeUTF8(font, cstr(text), &width, &height)
lines: i32 = 1
for i in 0..<len(text) {
if text[i] == '\n' do lines += 1
}
return { width, height * lines }
}
set_clip_area :: proc(pos: Vector, size: Vector) {
sdl.RenderSetClipRect(window.renderer, &{ pos.x, pos.y, size.x, size.y })
}
get_clip_area_size :: proc() -> Vector {
rect: sdl.Rect
sdl.RenderGetClipRect(window.renderer, &rect)
return { rect.w, rect.h }
}
unset_clip_area :: proc() {
sdl.RenderSetClipRect(window.renderer, nil)
}
handle_premultiplied_alpha_compositing :: proc(texture: Texture) {
sdl.SetTextureBlendMode(
texture,
sdl.ComposeCustomBlendMode(.ONE, .ONE_MINUS_SRC_ALPHA, .ADD, .ONE, .ONE_MINUS_SRC_ALPHA, .ADD)
// src rgb -> dest rgb src a -> dest a
)
}
copy :: proc(text: string) {
sdl.SetClipboardText(cstr(text))
}
// =======================================================================================
// ================================= DEBUGGING ================================
// =======================================================================================
print_valid_texture_formats :: proc() {
info: sdl.RendererInfo
sdl.GetRendererInfo(window.renderer, &info)
fmt.println(info)
fmt.println("[DEBUG] Valid SDL Texture Formats: ")
for format in info.texture_formats {
if format == 0 do continue
fmt.println("[DEBUG] ", sdl.GetPixelFormatName(format))
}
}