-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_windows.odin
243 lines (204 loc) · 5.6 KB
/
platform_windows.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
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
package msc
import "core:fmt"
import "core:runtime"
import "core:strings"
import win32 "core:sys/windows"
import ma "vendor:miniaudio"
platform_window_handle :: win32.HWND
platform_font_handle :: win32.HFONT
platform_cursor_handle :: win32.HCURSOR
platform_ui_context_create :: proc(app: ^App, hdc: win32.HDC = nil) -> Platform_Ui_Context {
using win32
ctx: Platform_Ui_Context
ctx.win_handle = app.win_handle
ctx.hdc = hdc
ctx.last_click_cx = app.last_click_cx
ctx.last_click_cy = app.last_click_cy
ctx.mouse_down = app.mouse_down
ctx.font_height = app.font_height
set_text_align(&ctx, TA_CENTER)
win: RECT
if GetClientRect(ctx.win_handle, &win) {
ctx.width = int(win.right - win.left)
ctx.height = int(win.bottom - win.top)
}
cursor: POINT
if GetCursorPos(&cursor) {
ScreenToClient(ctx.win_handle, &cursor)
ctx.cx = int(cursor.x)
ctx.cy = int(cursor.y)
}
return ctx
}
platform_get_text_size :: proc(ctx: ^Platform_Ui_Context, str: string) -> Rect {
using win32
hdc := ctx.hdc
font_old: HFONT
if ctx.msg != .PAINT {
hdc = GetDC(ctx.win_handle)
font_old = HFONT(SelectObject(hdc, HGDIOBJ(font_default)))
}
length_size: win32.SIZE
str_ := utf8_to_utf16(str)
GetTextExtentPoint32W(hdc, &str_[0], i32(len(str_)), &length_size)
if ctx.msg != .PAINT {
SelectObject(hdc, HGDIOBJ(font_old))
ReleaseDC(ctx.win_handle, hdc)
}
return {0, 0, int(length_size.cx), int(length_size.cy)}
}
platform_rect_to_rect :: proc(r0: win32.RECT) -> Rect {
return {int(r0.left), int(r0.top), int(r0.right - r0.left), int(r0.bottom - r0.top)}
}
platform_open_file_dialog :: proc(
filter: string,
allocator := context.allocator,
) -> (
path: string,
ok: bool,
) {
using win32
context.allocator = allocator
max_length := u32(MAX_PATH_WIDE)
file_buf := make([]u16, max_length)
defer delete(file_buf)
// @note: use normal allocator to prevent overwrite
filter_ := utf8_to_wstring(filter, allocator)
defer free(filter_)
ofn := OPENFILENAMEW {
lStructSize = size_of(OPENFILENAMEW),
hwndOwner = GetActiveWindow(),
lpstrFile = wstring(&file_buf[0]),
nMaxFile = max_length,
lpstrFilter = filter_,
Flags = OFN_FILEMUSTEXIST,
}
ok = bool(win32.GetOpenFileNameW(&ofn))
if !ok do return
file_name, _ := utf16_to_utf8(file_buf[:], allocator)
path = strings.trim_right_null(file_name)
return
}
platform_miniaudio_sound_init :: proc(app: ^App, path: string) -> bool {
return(
ma.sound_init_from_file_w(
&app.engine,
win32.utf8_to_wstring(path),
u32(ma.sound_flags.STREAM | ma.sound_flags.NO_SPATIALIZATION),
nil,
nil,
&app.sound,
) ==
.SUCCESS \
)
}
platform_window_set_text :: proc(app: ^App, text: string) {
text_ := win32.utf8_to_wstring(text, context.allocator)
defer free(text_)
win32.SetWindowTextW(app.win_handle, text_)
}
platform_get_shift_key :: proc() -> bool {
return win32.GetKeyState(win32.VK_SHIFT) < 0
}
platform_redraw :: proc(app: ^App) {
win32.InvalidateRect(app.win_handle, nil, win32.TRUE)
}
platform_get_executable_path :: proc() -> (string, bool) {
executable_path := make([]u16, win32.MAX_PATH_WIDE)
defer delete(executable_path)
win32.GetModuleFileNameW(nil, &executable_path[0], u32(len(executable_path)))
result, err := win32.utf16_to_utf8(executable_path[:], context.allocator)
return result, err == .None
}
platform_timer_start :: proc(app: ^App, timer: Timer, time: u32) {
win32.SetTimer(app.win_handle, uintptr(timer), time, nil)
}
platform_timer_stop :: proc(app: ^App, timer: Timer) {
win32.KillTimer(app.win_handle, uintptr(timer))
}
platform_window_create :: proc() -> win32.HWND {
using win32
instance := HINSTANCE(GetModuleHandleW(nil))
class_name := win32.L("msc") // issue #2007
win_class: WNDCLASSEXW
win_class.cbSize = size_of(win_class)
win_class.lpfnWndProc = win_proc
win_class.hInstance = instance
win_class.lpszClassName = class_name
win_class.style = CS_HREDRAW | CS_VREDRAW
icon := cast(HICON)LoadImageW(
instance,
win32.L("MSC_ICON"),
IMAGE_ICON,
LR_DEFAULTSIZE,
LR_DEFAULTSIZE,
0,
)
win_class.hIcon = icon
win_class.hIconSm = icon
if !b32(RegisterClassExW(&win_class)) do return nil
win_handle := CreateWindowExW(
0,
class_name,
win32.L("msc"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
750,
550,
nil,
nil,
instance,
nil,
)
if win_handle == nil do return nil
// windows dark titlebar
DWMWA_USE_IMMERSIVE_DARK_MODE :: 20
dark_mode: BOOL = TRUE
DwmSetWindowAttribute(
win_handle,
DWMWA_USE_IMMERSIVE_DARK_MODE,
&dark_mode,
size_of(dark_mode),
)
cursor_arrow = LoadCursorA(nil, IDC_ARROW) // LoadCursorW?
cursor_hand = LoadCursorA(nil, IDC_HAND)
return win_handle
}
platform_font_init :: proc(app: ^App) {
using win32
font_name := win32.utf8_to_wstring(app.font_name, context.allocator)
defer free(font_name)
font_default = CreateFontW(
i32(app.font_height),
0,
0,
0,
FW_DONTCARE,
0,
0,
0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
font_name,
)
}
Platform_Ui_Context :: struct {
cx, cy: int,
width, height: int,
scroll: f32,
msg: Ui_Message,
text_align: u32,
font_height: int,
last_click_cx, last_click_cy: int,
mouse_down: b32,
redraw: b32,
redraw_rect: win32.RECT,
next_cursor: Cursor,
hold: runtime.Source_Code_Location, // @analyze: add z and compare the z?
hdc: win32.HDC,
win_handle: platform_window_handle,
}