Simplify drawing to screen example

This commit is contained in:
Krzosa Karol
2022-10-01 09:29:09 +02:00
parent 0646ae9279
commit 02addfb389
5 changed files with 51 additions and 65 deletions

View File

@@ -1,36 +1,41 @@
#import "Base.core"
#import "Arena.core"
#import "Windows.core"
#import "KERNEL32.core"
#import "GDI32.core"
#import "USER32.core"
#import "WINMM.core"
Vec2I :: struct;; x: S64; y: S64
WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int
screen_size_x: int = 1280
screen_size_y: int = 720
AppIsRunning := true
Windows_Bitmap :: struct
size: Vec2I
data: *U32
hdc: HDC
dib: HBITMAP
arena: Arena
window_name := StringToString16(&arena, "Have a wonderful day!")
w := WNDCLASSW{
lpfnWndProc = WindowProc,
hInstance = hInstance,
lpszClassName = window_name.str,
}
Assert(RegisterClassW(&w) != 0)
Bitmap :: struct
size: Vec2I
data: *U32
CreateBitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap
result: Windows_Bitmap = {size = size}
if bottom_up == false
result.size.y = -result.size.y
window := CreateWindowExW(
dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0,
X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = screen_size_x, nHeight = screen_size_y,
lpClassName = window_name.str,
lpWindowName = window_name.str,
dwStyle = WS_OVERLAPPEDWINDOW,
hInstance = hInstance
)
Assert(window != 0)
ShowWindow(window, nShowCmd)
window_dc := GetDC(window)
header_size: U32 = SizeOf(BITMAPINFOHEADER)
Assert(header_size == 40)
bminfo := BITMAPINFO{
BITMAPINFOHEADER{
biSize = header_size,
biWidth = size.x->LONG,
biHeight = size.y->LONG,
biWidth = screen_size_x->LONG,
biHeight = screen_size_y->LONG,
biPlanes = 1,
biBitCount = 32,
biCompression = BI_RGB,
@@ -39,46 +44,9 @@ CreateBitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap
}
}
hdc := GetDC(0)
result.dib = CreateDIBSection(hdc, &bminfo, DIB_RGB_COLORS, (&result.data)->**void, 0, 0)
result.hdc = CreateCompatibleDC(hdc)
return result
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)
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 := StringToString16(&arena, "Have a wonderful day! 你好世界 ")
w := WNDCLASSW{
lpfnWndProc = WindowProc,
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 := CreateBitmap(screen_size)
pixels: *U32
bitmap_dib := CreateDIBSection(window_dc, &bminfo, DIB_RGB_COLORS, (&pixels)->**void, 0, 0)
bitmap_hdc := CreateCompatibleDC(window_dc)
for AppIsRunning
msg: MSG
@@ -86,14 +54,23 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS
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
for y := 0->int, y < screen_size_y, y+=1
for x := 0->int, x < screen_size_x, x+=1
pixels[x + y*screen_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)
SelectObject(bitmap_hdc, bitmap_dib)
BitBlt(window_dc, 0, 0, screen_size_x, screen_size_y, bitmap_hdc, 0, 0, SRCCOPY)
Sleep(100)
if CStringCompare(lpCmdLine, "testing")
return 0
return 0
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)