Files
corelang/modules/win32_multimedia.core
2022-10-01 19:59:45 +02:00

162 lines
4.6 KiB
Core

#import "KERNEL32.core"
#import "GDI32.core"
#import "USER32.core"
#import "WINMM.core"
#import "Windows.core"
OS :: struct
bitmap: Bitmap
window_dc: HDC
window: HWND
good_scheduling: Bool
Bitmap :: struct
size: Vec2I
data: *U32
hdc: HDC
dib: HBITMAP
compatible_dc: HDC
IsValidBitmap :: (b: *Bitmap): Bool
result := b.data != 0
return result
CreateBitmap :: (for_dc: HDC, size: Vec2I, bottom_up: Bool = true): Bitmap
result: Bitmap = {size = size}
if bottom_up == false
result.size.y = -result.size.y
header_size: U32 = SizeOf(BITMAPINFOHEADER)
Assert(header_size == 40)
bminfo := BITMAPINFO{
BITMAPINFOHEADER{
biSize = header_size,
biWidth = size.x->LONG,
biHeight = size.y->LONG,
biPlanes = 1,
biBitCount = 32,
biCompression = BI_RGB,
biXPelsPerMeter = 1,
biYPelsPerMeter = 1,
}
}
result.dib = CreateDIBSection(for_dc, &bminfo, DIB_RGB_COLORS, (&result.data)->**void, 0, 0)
result.hdc = CreateCompatibleDC(for_dc)
result.compatible_dc = for_dc
return result
DestroyBitmap :: (b: *Bitmap)
if IsValidBitmap(b)
DeleteDC(b.hdc)
DeleteObject(b.dib)
ZeroMemory(b, SizeOf(Bitmap))
DrawBitmapInCompatibleDC :: (b: *Bitmap)
if(IsValidBitmap(b))
SelectObject(b.hdc, b.dib)
BitBlt(b.compatible_dc, 0, 0, b.size.x->int, b.size.y->int, b.hdc, 0, 0, SRCCOPY)
GetWindowSize :: (window: HWND): Vec2I
result: Vec2I
window_rect: RECT
GetClientRect(window, &window_rect)
result.x = (window_rect.right - window_rect.left)->S64
result.y = (window_rect.bottom - window_rect.top)->S64
return result
GetWindowPos :: (window: HWND): Vec2I
pos: Point
ClientToScreen(window, &pos)
return {pos.x, pos.y}
AdjustWindowRect :: (window: HWND, style: DWORD, rect: *RECT): void
FALSE :: 0
if window == 0
dpi := GetDpiForWindow(window)
AdjustWindowRectExForDpi(rect, style, FALSE, 0, dpi)
else
AdjustWindowRectEx(rect, style, FALSE, 0)
SetWindowSize :: (window: HWND, style: DWORD, size: Vec2I): void
rect := RECT{ 0, 0, size.x->LONG, size.y->LONG }
AdjustWindowRect(window, style, &rect)
SetWindowPos(window, HWND_TOP, 0, 0, (rect.right - rect.left)->int, (rect.bottom - rect.top)->int, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOZORDER)
SetWindowPosition :: (window: HWND, style: DWORD, pos: Vec2I): void
rect := RECT{ pos.x->LONG, pos.y->LONG, pos.x->LONG, pos.y->LONG }
AdjustWindowRect(window, style, &rect)
SetWindowPos(window, 0, rect.left, rect.top, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE)
StartMultimedia :: (x: S64 = 1280, y: S64 = 720, title: String = "Hello people!"): Mu
mu: Mu
if timeBeginPeriod(1) == TIMERR_NOERROR
mu.os.good_scheduling = true
dpi_aware := SetProcessDPIAware()
Assert(dpi_aware != 0)
mu.time.start = Time()
hInstance := GetModuleHandleA(0)
window_name := StringToString16(&mu.frame_arena, title)
w := WNDCLASSW{
lpfnWndProc = WindowProc,
hInstance = hInstance,
lpszClassName = window_name.str,
// style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW,
}
Assert(RegisterClassW(&w) != 0)
style: DWORD = 0
mu.os.window = CreateWindowExW(
dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0,
X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = x->int, nHeight = y->int,
lpClassName = window_name.str,
lpWindowName = window_name.str,
dwStyle = WS_OVERLAPPEDWINDOW,
hInstance = hInstance
)
Assert(mu.os.window != 0)
SetWindowSize(mu.os.window, style, {x,y})
ShowWindow(mu.os.window, SW_SHOW)
size := GetWindowSize(mu.os.window)
Assert(size.x == x && size.y == y)
mu.os.window_dc = GetDC(mu.os.window)
mu.os.bitmap = W32.CreateBitmap(mu.os.window_dc, size)
mu.scrn = mu.os.bitmap.data
mu.x = mu.os.bitmap.size.x
mu.y = mu.os.bitmap.size.y
return mu
UpdateMultimedia :: (mu: *Mu): Bool
mu.frame_count += 1
frame_time := Time() - mu.time.frame_start
mu.time.total += frame_time
if frame_time < mu.time.delta
if mu.os.good_scheduling
time_to_sleep := (mu.time.delta - frame_time) * 1000
if time_to_sleep > 0
time_to_sleep_dword := time_to_sleep->DWORD
// @check if time_to_sleep_dword truncates down
Sleep(time_to_sleep_dword)
new_frame_time := Time()
for new_frame_time < mu.time.delta
new_frame_time = Time() - mu.time.frame_start
mu.time.frame_start = Time()
return !mu.quit
AppIsRunning := true
WindowProc :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
if msg == WM_DESTROY
PostQuitMessage(0)
AppIsRunning = false
return 0
else;; return DefWindowProcW(hwnd, msg, wparam, lparam)