diff --git a/project/dependencies/windows/windows_base.h b/project/dependencies/windows/windows_base.h index 6f78714..443a979 100644 --- a/project/dependencies/windows/windows_base.h +++ b/project/dependencies/windows/windows_base.h @@ -214,6 +214,8 @@ typedef double LONGLONG; typedef double ULONGLONG; #endif +#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name + typedef void VOID; typedef void * PVOID; typedef void * LPVOID; @@ -225,17 +227,15 @@ typedef LONG * LPLONG; typedef DWORD * PDWORD; typedef LPVOID HANDLE; -typedef HANDLE HINSTANCE; -typedef HANDLE HWND; +DECLARE_HANDLE(HINSTANCE); +DECLARE_HANDLE(HWND); typedef HINSTANCE HMODULE; -typedef HANDLE HDC; -typedef HANDLE HGLRC; -typedef HANDLE HMENU; +DECLARE_HANDLE(HDC); +DECLARE_HANDLE(HGLRC); +DECLARE_HANDLE(HMENU); typedef HANDLE * PHANDLE; typedef HANDLE * LPHANDLE; -#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name - typedef WCHAR * PWSTR; typedef BYTE * LPBYTE; typedef long * LPLONG; diff --git a/project/handmade_win32.cpp b/project/handmade_win32.cpp index eb7e4d4..a9880c7 100644 --- a/project/handmade_win32.cpp +++ b/project/handmade_win32.cpp @@ -12,6 +12,8 @@ #include "macros.h" #include "types.h" +#include + #include "win32.h" NS_WIN32_BEGIN @@ -20,8 +22,63 @@ global bool Running; global BITMAPINFO ScreenBitmapInfo; global void* ScreenBitmapMemory; // Lets use directly mess with the "pixel's memory buffer" -global HBITMAP ScreenBitmapHandle; -global HDC ScreenDeviceContext; +global u32 BitmapWidth; +global u32 BitmapHeight; + +constexpr u32 BytesPerPixel = 4; + +internal void +render_weird_graident( u32 x_offset, u32 y_offset ) +{ + struct Pixel { + u8 Blue; + u8 Green; + u8 Red; + u8 Alpha; + }; + + u32 pitch = BitmapWidth * BytesPerPixel; + u8* row = rcast( u8*, ScreenBitmapMemory); + local_persist float wildcard = 0; + for ( u32 y = 0; y < BitmapHeight; ++ y ) + { + // u8* pixel = rcast(u8*, row); + // Pixel* pixel = rcast( Pixel*, row ); + u32* pixel = rcast(u32*, row); + for ( u32 x = 0; x < BitmapWidth; ++ x ) + { + /* Pixel in memory: + ----------------------------------------------- + Pixel + 0 Pixel + 1 Pixel + 2 Pixel + 3 + RR GG GG XX + ----------------------------------------------- + x86-64 : Little Endian Arch + 0x XX BB GG RR + */ + // pixel[0] = scast(u8, x + x_offset); + // pixel[1] = scast(u8, y + y_offset); + // pixel[2] = 0; + // pixel[3] = 0; + + // pixel->Blue = scast(u8, x + x_offset * u8(wildcard) % 256); + // pixel->Green = scast(u8, y + y_offset - u8(wildcard) % 128); + // pixel->Red = scast(u8, wildcard) % 256 - x * 0.1f; + // pixel->Alpha = 0; + + // ++pixel; + // pixel += BytesPerPixel; + + u8 blue = scast(u8, x + x_offset * u8(wildcard) % 256); + u8 green = scast(u8, y + y_offset - u8(wildcard) % 128); + u8 red = scast(u8, wildcard) % 256 - x * 0.4f; + + *pixel++ = (red << 16) | (green << 8) | blue; + } + // wildcard += 0.001f; + wildcard += .25f * 0.5; + row += pitch; + } +} internal void resize_dib_section( u32 width, u32 height ) @@ -30,45 +87,71 @@ resize_dib_section( u32 width, u32 height ) // TODO(Ed) : Free DIB section - if ( ScreenBitmapHandle ) + if ( ScreenBitmapMemory ) { - DeleteObject( ScreenBitmapHandle ); - } - if ( ! ScreenDeviceContext ) - { - ScreenDeviceContext = CreateCompatibleDC( 0 ); + VirtualFree( ScreenBitmapMemory, 0, MEM_RELEASE ); } - constexpr BITMAPINFOHEADER& header = ScreenBitmapInfo.bmiHeader; - header.biSize = sizeof( header ); - header.biWidth = width; - header.biHeight = height; - header.biPlanes = 1; - header.biBitCount = 32; // Need 24, but want 32 ( alignment ) - header.biCompression = BI_RGB_Uncompressed; - header.biSizeImage = 0; - header.biXPelsPerMeter = 0; - header.biYPelsPerMeter = 0; - header.biClrUsed = 0; - header.biClrImportant = 0; + BitmapWidth = width; + BitmapHeight = height; + + // Negative means top-down in the context of the biHeight +# define Top_Down - + constexpr BITMAPINFOHEADER& + header = ScreenBitmapInfo.bmiHeader; + ScreenBitmapInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); + ScreenBitmapInfo.bmiHeader.biWidth = BitmapWidth; + ScreenBitmapInfo.bmiHeader.biHeight = Top_Down BitmapHeight; + ScreenBitmapInfo.bmiHeader.biPlanes = 1; + ScreenBitmapInfo.bmiHeader.biBitCount = 32; // Need 24, but want 32 ( alignment ) + ScreenBitmapInfo.bmiHeader.biCompression = BI_RGB_Uncompressed; + // ScreenBitmapInfo.bmiHeader.biSizeImage = 0; + // ScreenBitmapInfo.bmiHeader.biXPelsPerMeter = 0; + // ScreenBitmapInfo.bmiHeader.biYPelsPerMeter = 0; + // ScreenBitmapInfo.bmiHeader.biClrUsed = 0; + // ScreenBitmapInfo.bmiHeader.biClrImportant = 0; +# undef Top_Down +#if 0 ScreenBitmapHandle = CreateDIBSection( ScreenDeviceContext, & ScreenBitmapInfo , DIB_ColorTable_RGB, & ScreenBitmapMemory // Ignoring these last two , 0, 0 ); +#endif // ReleaseContext( 0, ScreenDeviceContext ); + + // We want to "touch" a pixel on every 4-byte boundary + u32 BitmapMemorySize = (BitmapWidth * BitmapHeight) * BytesPerPixel; + ScreenBitmapMemory = VirtualAlloc( NULL, BitmapMemorySize, MEM_COMMIT, PAGE_READWRITE ); + + // TODO(Ed) : Clear to black } internal void -update_window( HDC device_context +update_window( HDC device_context, RECT* WindowRect , u32 x, u32 y , u32 width, u32 height ) { +#if 0 + BitBlt( device_context + , x, y, width, height + , ScreenDeviceContext + , x, y, RO_Source_To_Dest + ); +#else + u32 window_width = WindowRect->right - WindowRect->left; + u32 window_height = WindowRect->bottom - WindowRect->top; + StretchDIBits( device_context + #if 0 , x, y, width, height , x, y, width, height + #endif + , 0, 0, BitmapWidth, BitmapHeight + , 0, 0, window_width, window_height , ScreenBitmapMemory, & ScreenBitmapInfo , DIB_ColorTable_RGB, RO_Source_To_Dest ); +#endif } LRESULT CALLBACK @@ -107,17 +190,17 @@ main_window_callback( { PAINTSTRUCT info; HDC device_context = BeginPaint( handle, & info ); - - u32 x = info.rcPaint.left; u32 y = info.rcPaint.top; - u32 height = info.rcPaint.bottom - info.rcPaint.top; u32 width = info.rcPaint.right - info.rcPaint.left; + u32 height = info.rcPaint.bottom - info.rcPaint.top; - update_window( handle + RECT client_rect; + GetClientRect( handle, & client_rect ); + + update_window( device_context, & client_rect , x, y , width, height ); - EndPaint( handle, & info ); } break; @@ -131,13 +214,11 @@ main_window_callback( u32 height = client_rect.bottom - client_rect.top; resize_dib_section( width, height ); - OutputDebugStringA( "WM_SIZE\n" ); } break; default: { - OutputDebugStringA( "default\n" ); result = DefWindowProc( handle, system_messages, w_param, l_param ); } } @@ -155,7 +236,7 @@ WinMain( ) { using namespace win32; - MessageBox( 0, L"First message!", L"Handmade Hero", MB_Ok_Btn | MB_Icon_Information ); + // MessageBox( 0, L"First message!", L"Handmade Hero", MB_Ok_Btn | MB_Icon_Information ); WNDCLASS window_class {}; window_class.style = CS_Own_Device_Context | CS_Horizontal_Redraw | CS_Vertical_Redraw; @@ -169,7 +250,7 @@ WinMain( window_class.lpszMenuName = L"Handmade Hero!"; window_class.lpszClassName = L"HandmadeHeroWindowClass"; - if ( RegisterClassW( &window_class ) ) + if ( RegisterClassW( & window_class ) ) { HWND window_handle = CreateWindowExW( 0, @@ -186,20 +267,36 @@ WinMain( { Running = true; - MSG message; + MSG msg_info; + + u32 x_offset = 0; + u32 y_offset = 0; + while( Running ) { - BOOL msg_result = GetMessage( & message, 0, 0, 0 ); - if ( msg_result > 0 ) + if ( PeekMessageW( & msg_info, 0, 0, 0, PM_Remove_Messages_From_Queue ) ) { - TranslateMessage( & message ); - DispatchMessage( & message ); - } - else - { - // TODO(Ed) : Logging - break; + if ( msg_info.message == WM_QUIT ) + { + OutputDebugStringA("WM_QUIT\n"); + Running = false; + } + + TranslateMessage( & msg_info ); + DispatchMessage( & msg_info ); } + + render_weird_graident( x_offset, y_offset ); + + RECT window_rect; + GetClientRect( window_handle, & window_rect ); + HDC device_context = GetDC( window_handle ); + u32 window_width = window_rect.right - window_rect.left; + u32 window_height = window_rect.bottom - window_rect.top; + update_window( device_context, & window_rect, 0, 0, window_width, window_height ); + + ++ x_offset; + ++ y_offset; } } else diff --git a/project/platform/macros.h b/project/platform/macros.h index cf19f3d..4699512 100644 --- a/project/platform/macros.h +++ b/project/platform/macros.h @@ -5,3 +5,10 @@ #define local_persist static // Local Persisting variables #define api_c extern "C" + +// Casting + +#define ccast( Type, Value ) ( * const_cast< Type* >( & (Value) ) ) +#define pcast( Type, Value ) ( * reinterpret_cast< Type* >( & ( Value ) ) ) +#define rcast( Type, Value ) reinterpret_cast< Type >( Value ) +#define scast( Type, Value ) static_cast< Type >( Value ) diff --git a/project/platform/win32.h b/project/platform/win32.h index bce6a66..8c4adc6 100644 --- a/project/platform/win32.h +++ b/project/platform/win32.h @@ -2,9 +2,13 @@ Alternative header for windows.h */ +// #include #include "windows/windows_base.h" #include "windows/window.h" +#include "windows/file.h" +#include "windows/io.h" + // #ifdef Build_Debug # include "windows/dbghelp.h" // #endif @@ -127,8 +131,9 @@ PatBlt( HDC hdc constexpr auto DispatchMessage = DispatchMessageA; #endif // !UNICODE -#pragma region Message Function Templates -// From WinUser.h, modular headers lib doesn't have. +#pragma region WinUser + +HDC WINAPI GetDC( HWND hWnd ); BOOL WINAPI GetMessageA( @@ -159,7 +164,7 @@ GetMessageW( #else constexpr auto RegisterClass = RegisterClassA; #endif // !UNICODE -#pragma endregion Message Function Templates +#pragma endregion WinUser // Class Style Constants // https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes @@ -192,12 +197,21 @@ enum MB : UINT MB_Icon_Information = MB_ICONINFORMATION, }; -#define WM_ACTIVATEAPP 0x001C - -enum WS : UINT +enum Mem : DWORD { - WS_Overlapped_Window = WS_OVERLAPPEDWINDOW, - WS_Initially_Visible = WS_VISIBLE, + Mem_Commit_Zeroed = 0x00001000, + Mem_Reserve = 0x00002000, + Mem_Release = 0x00008000, +}; + +enum Page : DWORD +{ + Page_Read_Write = 0x04, +}; + +enum PM : UINT +{ + PM_Remove_Messages_From_Queue = PM_REMOVE, }; enum RasterOps : DWORD @@ -207,6 +221,14 @@ enum RasterOps : DWORD RO_Whiteness = (DWORD)0x00FF0062, }; +#define WM_ACTIVATEAPP 0x001C + +enum WS : UINT +{ + WS_Overlapped_Window = WS_OVERLAPPEDWINDOW, + WS_Initially_Visible = WS_VISIBLE, +}; + WIN_LIBRARY_API_END NS_WIN32_END diff --git a/scripts/handmade.rdbg b/scripts/handmade.rdbg new file mode 100644 index 0000000..36d0507 Binary files /dev/null and b/scripts/handmade.rdbg differ