Remove programs / folder, add windows drawing to screen example
This commit is contained in:
116
examples/drawing_to_screen_using_windows_api.kl
Normal file
116
examples/drawing_to_screen_using_windows_api.kl
Normal file
@@ -0,0 +1,116 @@
|
||||
#import "base.kl"
|
||||
#import "os_windows.kl"
|
||||
#import "kernel32.kl"
|
||||
#import "gdi32.kl"
|
||||
#import "user32.kl"
|
||||
#import "winmm.kl"
|
||||
|
||||
Vec2I :: struct;; x: S64; y: S64
|
||||
Vec2 :: struct;; x: F32; y: F32
|
||||
|
||||
Windows_Bitmap :: struct
|
||||
size: Vec2I
|
||||
data: *U32
|
||||
hdc: HDC
|
||||
dib: HBITMAP
|
||||
|
||||
Bitmap :: struct
|
||||
size: Vec2I
|
||||
data: *U32
|
||||
|
||||
create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap
|
||||
result: Windows_Bitmap = {size = size}
|
||||
if bottom_up == false
|
||||
result.size.y = -result.size.y
|
||||
|
||||
bminfo := BITMAPINFO{
|
||||
BITMAPINFOHEADER{
|
||||
biSize = 40, // @todo!!! size_of(BITMAPINFOHEADER),
|
||||
biWidth = size.x->LONG,
|
||||
biHeight = size.y->LONG,
|
||||
biPlanes = 1,
|
||||
biBitCount = 32,
|
||||
biCompression = BI_RGB,
|
||||
biXPelsPerMeter = 1,
|
||||
biYPelsPerMeter = 1,
|
||||
}
|
||||
}
|
||||
|
||||
hdc := GetDC(0)
|
||||
result.dib = CreateDIBSection(hdc, &bminfo, DIB_RGB_COLORS, (&result.data)->**void, 0, 0)
|
||||
result.hdc = CreateCompatibleDC(hdc)
|
||||
return result
|
||||
|
||||
|
||||
app_is_running := true
|
||||
window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
|
||||
if msg == WM_DESTROY
|
||||
PostQuitMessage(0)
|
||||
app_is_running = false
|
||||
return 0
|
||||
else;; return DefWindowProcW(hwnd, msg, wparam, lparam)
|
||||
|
||||
WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int
|
||||
if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR
|
||||
good_scheduling = true
|
||||
|
||||
arena: Arena
|
||||
|
||||
window_name := string_to_string16(&arena, "Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ")
|
||||
w := WNDCLASSW{
|
||||
lpfnWndProc = window_procedure,
|
||||
hInstance = hInstance,
|
||||
lpszClassName = window_name.str,
|
||||
}
|
||||
assert(RegisterClassW(&w) != 0)
|
||||
|
||||
screen_size: Vec2I = {1280, 720}
|
||||
window := CreateWindowExW(
|
||||
dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0,
|
||||
X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = screen_size.x->int, nHeight = screen_size.y->int,
|
||||
lpClassName = window_name.str,
|
||||
lpWindowName = window_name.str,
|
||||
dwStyle = WS_OVERLAPPEDWINDOW,
|
||||
hInstance = hInstance
|
||||
)
|
||||
assert(window != 0)
|
||||
ShowWindow(window, nShowCmd)
|
||||
|
||||
window_dc := GetDC(window)
|
||||
bitmap := create_bitmap(screen_size)
|
||||
|
||||
requested_time_per_frame := 1.0 / 60.0
|
||||
frame_start_time := time()
|
||||
frame_number: S64
|
||||
total_time: F64
|
||||
for app_is_running
|
||||
msg: MSG
|
||||
for PeekMessageW(&msg, window, 0, 0, PM_REMOVE) > 0
|
||||
TranslateMessage(&msg)
|
||||
DispatchMessageW(&msg)
|
||||
|
||||
for y := 0, y < bitmap.size.y, y+=1
|
||||
for x := 0, x < bitmap.size.x, x+=1
|
||||
bitmap.data[x + y*bitmap.size.x] = 0xFFFF0000
|
||||
|
||||
SelectObject(bitmap.hdc, bitmap.dib)
|
||||
BitBlt(window_dc, 0, 0, (bitmap.size.x)->int, (bitmap.size.y)->int, bitmap.hdc, 0, 0, SRCCOPY)
|
||||
|
||||
|
||||
frame_time := time() - frame_start_time
|
||||
print_float(frame_time)
|
||||
if frame_time < requested_time_per_frame
|
||||
if good_scheduling
|
||||
time_to_sleep := (requested_time_per_frame - 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 < requested_time_per_frame
|
||||
new_frame_time = time() - frame_start_time
|
||||
|
||||
frame_time = new_frame_time
|
||||
frame_number += 1
|
||||
total_time += frame_time
|
||||
Reference in New Issue
Block a user