From 2a3284f70ee3773e08355e50c5dcb05f2e96233b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 16 Jun 2022 09:51:48 +0200 Subject: [PATCH] Window painting is working! --- programs/Windows.kl | 3 ++- programs/gdi32.kl | 47 +++++++++++++++++++++++++++++++-------------- programs/main.kl | 21 ++++++++++++++------ 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/programs/Windows.kl b/programs/Windows.kl index 5e61067..c32956b 100644 --- a/programs/Windows.kl +++ b/programs/Windows.kl @@ -50,4 +50,5 @@ STD_OUTPUT_HANDLE :: 4294967285//(-11)->DWORD //STD_ERROR_HANDLE :: (-12)->DWORD GetStdHandle :: #foreign (nStdHandle: DWORD): HANDLE WriteConsoleA :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL -WriteConsoleW :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL \ No newline at end of file +WriteConsoleW :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL +GetLastError :: #foreign (): DWORD \ No newline at end of file diff --git a/programs/gdi32.kl b/programs/gdi32.kl index f4302a9..cdf8f09 100644 --- a/programs/gdi32.kl +++ b/programs/gdi32.kl @@ -1,20 +1,39 @@ #load "Windows.kl" -RBGQUAD :: struct;; rgbBlue: BYTE; rgbGreen: BYTE; rgbRed: BYTE; rgbReserved: BYTE -BITMAPINFOHEADER :: struct;; biSize: DWORD; biWidth: LONG; biHeight: LONG; biPlanes: WORD; biBitCount: WORD; biCompression: DWORD; biSizeImage: DWORD; biXPelsPerMeter: LONG; biYPelsPerMeter: LONG; biClrUsed: DWORD; biClrImportant: DWORD -BITMAPINFO :: struct;; bmiHeader: BITMAPINFOHEADER; bmiColors: [1]RBGQUAD +RBGQUAD :: struct;; rgbBlue: BYTE; rgbGreen: BYTE; rgbRed: BYTE; rgbReserved: BYTE +BITMAPINFOHEADER :: struct;; biSize: DWORD; biWidth: LONG; biHeight: LONG; biPlanes: WORD; biBitCount: WORD; biCompression: DWORD; biSizeImage: DWORD; biXPelsPerMeter: LONG; biYPelsPerMeter: LONG; biClrUsed: DWORD; biClrImportant: DWORD +BITMAPINFO :: struct;; bmiHeader: BITMAPINFOHEADER; bmiColors: [1]RBGQUAD +HGDIOBJ :: HANDLE -BI_RGB :: 0x0000 -BI_RLE8 :: 0x0001 -BI_RLE4 :: 0x0002 -BI_BITFIELDS :: 0x0003 -BI_JPEG :: 0x0004 -BI_PNG :: 0x0005 -BI_CMYK :: 0x000B -BI_CMYKRLE8 :: 0x000C -BI_CMYKRLE4 :: 0x000 -DIB_RGB_COLORS :: 0x00 +BI_RGB :: 0x0000 +BI_RLE8 :: 0x0001 +BI_RLE4 :: 0x0002 +BI_BITFIELDS :: 0x0003 +BI_JPEG :: 0x0004 +BI_PNG :: 0x0005 +BI_CMYK :: 0x000B +BI_CMYKRLE8 :: 0x000C +BI_CMYKRLE4 :: 0x000 +DIB_RGB_COLORS :: 0x00 + +SRCCOPY :: 0x00CC0020 /* dest = source */ +SRCPAINT :: 0x00EE0086 /* dest = source OR dest */ +SRCAND :: 0x008800C6 /* dest = source AND dest */ +SRCINVERT :: 0x00660046 /* dest = source XOR dest */ +SRCERASE :: 0x00440328 /* dest = source AND (NOT dest ) */ +NOTSRCCOPY :: 0x00330008 /* dest = (NOT source) */ +NOTSRCERASE :: 0x001100A6 /* dest = (NOT src) AND (NOT dest) */ +MERGECOPY :: 0x00C000CA /* dest = (source AND pattern) */ +MERGEPAINT :: 0x00BB0226 /* dest = (NOT source) OR dest */ +PATCOPY :: 0x00F00021 /* dest = pattern */ +PATPAINT :: 0x00FB0A09 /* dest = DPSnoo */ +PATINVERT :: 0x005A0049 /* dest = pattern XOR dest */ +DSTINVERT :: 0x00550009 /* dest = (NOT dest) */ +BLACKNESS :: 0x00000042 /* dest = BLACK */ +WHITENESS :: 0x00FF0062 /* dest = WHITE */ // #import #foreign "gdi32.lib" @todo -CreateDIBSection :: #foreign (hdc: HDC, pbmi: *BITMAPINFO, usage: UINT, ppvBits: **VOID, hSection: HANDLE, offset: DWORD): HBITMAP +CreateDIBSection :: #foreign (hdc: HDC, pbmi: *BITMAPINFO, usage: UINT, ppvBits: **VOID, hSection: HANDLE, offset: DWORD): HBITMAP CreateCompatibleDC :: #foreign (hdc: HDC): HDC +SelectObject :: #foreign (hdc: HDC, h: HGDIOBJ): HGDIOBJ +BitBlt :: #foreign (hdc: HDC, x: int, y: int, cx: int, cy: int, hdcSrc: HDC, x1: int, y1: int, ro: DWORD): BOOL \ No newline at end of file diff --git a/programs/main.kl b/programs/main.kl index 20178e6..00fb507 100644 --- a/programs/main.kl +++ b/programs/main.kl @@ -100,7 +100,7 @@ string_to_string16 :: (in: String): String16 result.str[result.len] = 0 return result -Vec2I :: struct;; x: S32; y: S32 +Vec2I :: struct;; x: S64; y: S64 Vec2 :: struct;; x: F32; y: F32 Windows_Bitmap :: struct size: Vec2I @@ -115,7 +115,7 @@ create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap bminfo := BITMAPINFO{ BITMAPINFOHEADER{ - biSize = size_of(BITMAPINFOHEADER), + biSize = 40, // @todo!!! size_of(BITMAPINFOHEADER), biWidth = size.x->LONG, biHeight = size.y->LONG, biPlanes = 1, @@ -128,6 +128,7 @@ create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap hdc := GetDC(0) result.dib = CreateDIBSection(hdc, &bminfo, DIB_RGB_COLORS, (&result.data)->**void, 0, 0) + error := GetLastError() result.hdc = CreateCompatibleDC(hdc) return result @@ -135,6 +136,7 @@ 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) @@ -153,9 +155,7 @@ test_unicode :: () assert(result.out_str == 0xF3, "Invalid decode") WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int - bitmap := create_bitmap({1280, 720}) - - window_name := string_to_string16("Have a wonderful day!") + window_name := string_to_string16("Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ") w := WNDCLASSW{ lpfnWndProc = window_procedure, hInstance = hInstance, @@ -165,7 +165,7 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS window := CreateWindowExW( dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0, - X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = CW_USEDEFAULT, nHeight = CW_USEDEFAULT, + X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = 1280, nHeight = 720, lpClassName = window_name.str, lpWindowName = window_name.str, dwStyle = WS_OVERLAPPEDWINDOW, @@ -174,9 +174,18 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS assert(window != 0) ShowWindow(window, nShowCmd) + window_dc := GetDC(window) + bitmap := create_bitmap({1280, 720}) 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) +