Files
corelang/modules/win32_multimedia.core
2022-10-01 17:07:36 +02:00

122 lines
3.2 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)
StartMultimedia :: (x: S64 = 1280, y: S64 = 720, title: String = "Hello people!"): Mu
mu: Mu
if timeBeginPeriod(1) == TIMERR_NOERROR
mu.os.good_scheduling = true
mu.time.start = Time()
hInstance := GetModuleHandleA(0)
window_name := StringToString16(&mu.frame_arena, title)
w := WNDCLASSW{
lpfnWndProc = WindowProc,
hInstance = hInstance,
lpszClassName = window_name.str,
}
Assert(RegisterClassW(&w) != 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)
ShowWindow(mu.os.window, SW_SHOW)
mu.os.window_dc = GetDC(mu.os.window)
mu.os.bitmap = W32.CreateBitmap(mu.os.window_dc, {x,y})
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)