Day 3 complete

This commit is contained in:
Edward R. Gonzalez 2023-09-09 00:01:53 -04:00
parent e3a4686e2b
commit 4c832f3353
5 changed files with 209 additions and 69 deletions

@ -1 +1 @@
Subproject commit 6b43c05d4800c498eb3c9b24e976f9a7eddf3301 Subproject commit b67ab2734f4146a2a4bb9c3894d075cf1fe2c2cb

View File

@ -12,7 +12,7 @@ int gen_main()
log_fmt("Generating code for Handmade Hero\n"); log_fmt("Generating code for Handmade Hero\n");
log_fmt("Generaton finished for Handmade Hero\n"); log_fmt("Generaton finished for Handmade Hero\n");
gen::deinit(); // gen::deinit();
return 0; return 0;
} }
#endif #endif

View File

@ -13,11 +13,66 @@
#include "types.h" #include "types.h"
#include "win32.h" #include "win32.h"
// using namespace win32; NS_WIN32_BEGIN
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nc-winuser-wndproc // TODO(Ed) : This is a global for now.
// https://learn.microsoft.com/en-us/windows/win32/winmsg/about-messages-and-message-queues#system-defined-messages global bool Running;
LRESULT CALLBACK MainWindowCallback(
global BITMAPINFO ScreenBitmapInfo;
global void* ScreenBitmapMemory; // Lets use directly mess with the "pixel's memory buffer"
global HBITMAP ScreenBitmapHandle;
global HDC ScreenDeviceContext;
internal void
resize_dib_section( u32 width, u32 height )
{
// TODO(Ed) : Bulletproof memory handling here for the bitmap memory
// TODO(Ed) : Free DIB section
if ( ScreenBitmapHandle )
{
DeleteObject( ScreenBitmapHandle );
}
if ( ! ScreenDeviceContext )
{
ScreenDeviceContext = CreateCompatibleDC( 0 );
}
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;
ScreenBitmapHandle = CreateDIBSection( ScreenDeviceContext, & ScreenBitmapInfo
, DIB_ColorTable_RGB, & ScreenBitmapMemory
// Ignoring these last two
, 0, 0 );
// ReleaseContext( 0, ScreenDeviceContext );
}
internal void
update_window( HDC device_context
, u32 x, u32 y
, u32 width, u32 height )
{
StretchDIBits( device_context
, x, y, width, height
, x, y, width, height
, ScreenBitmapMemory, & ScreenBitmapInfo
, DIB_ColorTable_RGB, RO_Source_To_Dest );
}
LRESULT CALLBACK
main_window_callback(
HWND handle, HWND handle,
UINT system_messages, UINT system_messages,
WPARAM w_param, WPARAM w_param,
@ -26,7 +81,6 @@ LRESULT CALLBACK MainWindowCallback(
{ {
LRESULT result; LRESULT result;
// https://learn.microsoft.com/en-us/windows/win32/winmsg/window-notifications
switch ( system_messages ) switch ( system_messages )
{ {
case WM_ACTIVATEAPP: case WM_ACTIVATEAPP:
@ -37,13 +91,15 @@ LRESULT CALLBACK MainWindowCallback(
case WM_CLOSE: case WM_CLOSE:
{ {
OutputDebugStringA( "WM_CLOSE\n" ); // TODO(Ed) : Handle with a message to the user
Running = false;
} }
break; break;
case WM_DESTROY: case WM_DESTROY:
{ {
OutputDebugStringA( "WM_DESTROY\n" ); // TODO(Ed) : Handle with as an error and recreate the window
Running = false;
} }
break; break;
@ -52,21 +108,15 @@ LRESULT CALLBACK MainWindowCallback(
PAINTSTRUCT info; PAINTSTRUCT info;
HDC device_context = BeginPaint( handle, & info ); HDC device_context = BeginPaint( handle, & info );
u32 x = info.rcPaint.left; u32 x = info.rcPaint.left;
u32 y = info.rcPaint.top; u32 y = info.rcPaint.top;
u32 height = info.rcPaint.bottom - info.rcPaint.top; u32 height = info.rcPaint.bottom - info.rcPaint.top;
u32 width = info.rcPaint.right - info.rcPaint.left; u32 width = info.rcPaint.right - info.rcPaint.left;
global DWORD operation = RO_Whiteness; update_window( handle
PatBlt( device_context
, x, y , x, y
, width, height , width, height );
, operation );
operation == RO_Whiteness ?
operation = RO_Blackness
: operation = RO_Whiteness;
EndPaint( handle, & info ); EndPaint( handle, & info );
} }
@ -74,6 +124,13 @@ LRESULT CALLBACK MainWindowCallback(
case WM_SIZE: case WM_SIZE:
{ {
RECT client_rect;
GetClientRect( handle, & client_rect );
u32 width = client_rect.right - client_rect.left;
u32 height = client_rect.bottom - client_rect.top;
resize_dib_section( width, height );
OutputDebugStringA( "WM_SIZE\n" ); OutputDebugStringA( "WM_SIZE\n" );
} }
break; break;
@ -87,23 +144,22 @@ LRESULT CALLBACK MainWindowCallback(
return result; return result;
} }
NS_WIN32_END
int CALLBACK WinMain( int CALLBACK
WinMain(
HINSTANCE instance, HINSTANCE instance,
HINSTANCE prev_instance, HINSTANCE prev_instance,
LPSTR commandline, LPSTR commandline,
int show_command int show_command
) )
{ {
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox 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 );
// https://en.wikibooks.org/wiki/Windows_Programming/Window_Creation
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassa
WNDCLASS window_class {}; WNDCLASS window_class {};
// window_class.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
window_class.style = CS_Own_Device_Context | CS_Horizontal_Redraw | CS_Vertical_Redraw; window_class.style = CS_Own_Device_Context | CS_Horizontal_Redraw | CS_Vertical_Redraw;
window_class.lpfnWndProc = MainWindowCallback; window_class.lpfnWndProc = main_window_callback;
// window_class.cbClsExtra = ; // window_class.cbClsExtra = ;
// window_class.cbWndExtra = ; // window_class.cbWndExtra = ;
window_class.hInstance = instance; window_class.hInstance = instance;
@ -115,13 +171,10 @@ int CALLBACK WinMain(
if ( RegisterClassW( &window_class ) ) if ( RegisterClassW( &window_class ) )
{ {
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexa
// https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
HWND window_handle = CreateWindowExW( HWND window_handle = CreateWindowExW(
0, 0,
window_class.lpszClassName, window_class.lpszClassName,
L"Handmade Hero", L"Handmade Hero",
// WS_OVERLAPPEDWINDOW | WS_VISIBLE,
WS_Overlapped_Window | WS_Initially_Visible, WS_Overlapped_Window | WS_Initially_Visible,
CW_USEDEFAULT, CW_USEDEFAULT, // x, y CW_USEDEFAULT, CW_USEDEFAULT, // x, y
CW_USEDEFAULT, CW_USEDEFAULT, // width, height CW_USEDEFAULT, CW_USEDEFAULT, // width, height
@ -131,25 +184,16 @@ int CALLBACK WinMain(
if ( window_handle ) if ( window_handle )
{ {
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow Running = true;
// ShowWindow( window_handle, show_command );
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-updatewindow
// UpdateWindow( window_handle );
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessage
MSG message; MSG message;
for (;;) while( Running )
{ {
BOOL msg_result = GetMessage( & message, 0, 0, 0 ); BOOL msg_result = GetMessage( & message, 0, 0, 0 );
if ( msg_result > 0 ) if ( msg_result > 0 )
{ {
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-translatemessage
TranslateMessage( & message ); TranslateMessage( & message );
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dispatchmessage
DispatchMessage( & message ); DispatchMessage( & message );
// break;
} }
else else
{ {

View File

@ -3,3 +3,5 @@
#define global static // Global variables #define global static // Global variables
#define internal static // Internal linkage #define internal static // Internal linkage
#define local_persist static // Local Persisting variables #define local_persist static // Local Persisting variables
#define api_c extern "C"

View File

@ -10,19 +10,104 @@ Alternative header for windows.h
// #endif // #endif
#if Build_DLL #if Build_DLL
# define WIN_LIBRARY_API extern "C" __declspec(dllexport) # define WIN_LIBRARY_API_START extern "C" __declspec(dllexport)
# define WIN_LIBRARY_API_END
#else #else
# define WIN_LIBRARY_API extern "C" # define WIN_LIBRARY_API_START extern "C" {
# define WIN_LIBRARY_API_END }
#endif #endif
#ifndef CONST #ifndef CONST
# define CONST const # define CONST const
#endif #endif
#define WM_ACTIVATEAPP 0x001C #define NS_WIN32_BEGIN namespace win32 {
#define NS_WIN32_END }
NS_WIN32_BEGIN
WIN_LIBRARY_API_START
// namespace win32 { #pragma region Gdi32
#define _SAL_nop_impl_ X
#define _Deref_post2_impl_(p1,p2)
#define _SAL2_Source_(Name, args, annotes) _SA_annotes3(SAL_name, #Name, "", "2") _Group_(annotes _SAL_nop_impl_)
#define _Outptr_result_bytebuffer_(size) _SAL2_Source_(_Outptr_result_bytebuffer_, (size), _Out_impl_ _Deref_post2_impl_(__notnull_impl_notref, __bytecap_impl(size)))
DECLARE_HANDLE(HBITMAP);
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO, *LPBITMAPINFO, *PBITMAPINFO;
#define GDI_DIBSIZE(bi) ((bi).biHeight < 0 ? (-1)*(GDI__DIBSIZE(bi)) : GDI__DIBSIZE(bi))
HDC WINAPI CreateCompatibleDC( HDC hdc);
HBITMAP WINAPI
CreateDIBSection(
HDC hdc,
CONST BITMAPINFO *pbmi,
UINT usage,
_When_((pbmi->bmiHeader.biBitCount != 0), _Outptr_result_bytebuffer_(_Inexpressible_(GDI_DIBSIZE((pbmi->bmiHeader)))))
_When_((pbmi->bmiHeader.biBitCount == 0), _Outptr_result_bytebuffer_((pbmi->bmiHeader).biSizeImage))
VOID **ppvBits,
HANDLE hSection,
DWORD offset
);
typedef HANDLE HGDIOBJ;
BOOL WINAPI DeleteObject( HGDIOBJ ho);
int WINAPI StretchDIBits( HDC hdc
, int xDest, int yDest, int DestWidth, int DestHeight
, int xSrc, int ySrc, int SrcWidth, int SrcHeight,
CONST VOID* lpBits, CONST BITMAPINFO* lpbmi
, UINT iUsage, DWORD rop );
typedef struct tagPAINTSTRUCT {
HDC hdc;
BOOL fErase;
RECT rcPaint;
BOOL fRestore;
BOOL fIncUpdate;
BYTE rgbReserved[32];
} PAINTSTRUCT, *PPAINTSTRUCT, *NPPAINTSTRUCT, *LPPAINTSTRUCT;
HDC WINAPI
BeginPaint( HWND hWnd, LPPAINTSTRUCT lpPaint );
BOOL WINAPI
EndPaint( HWND hWnd, CONST PAINTSTRUCT *lpPaint );
BOOL WINAPI
PatBlt( HDC hdc
, int x, int y
, int w, int h
, DWORD rop );
#pragma endregion Gdi32
#ifdef UNICODE #ifdef UNICODE
constexpr auto CreateWindowEx = CreateWindowExW; constexpr auto CreateWindowEx = CreateWindowExW;
@ -45,12 +130,14 @@ Alternative header for windows.h
#pragma region Message Function Templates #pragma region Message Function Templates
// From WinUser.h, modular headers lib doesn't have. // From WinUser.h, modular headers lib doesn't have.
WIN_LIBRARY_API BOOL WINAPI GetMessageA( BOOL WINAPI
GetMessageA(
LPMSG lpMsg, LPMSG lpMsg,
HWND hWnd, HWND hWnd,
UINT wMsgFilterMin, UINT wMsgFilterMin,
UINT wMsgFilterMax); UINT wMsgFilterMax);
WIN_LIBRARY_API BOOL WINAPI GetMessageW( BOOL WINAPI
GetMessageW(
LPMSG lpMsg, LPMSG lpMsg,
HWND hWnd, HWND hWnd,
UINT wMsgFilterMin, UINT wMsgFilterMin,
@ -74,29 +161,17 @@ WIN_LIBRARY_API BOOL WINAPI GetMessageW(
#endif // !UNICODE #endif // !UNICODE
#pragma endregion Message Function Templates #pragma endregion Message Function Templates
#pragma region Window PAINT
typedef struct tagPAINTSTRUCT {
HDC hdc;
BOOL fErase;
RECT rcPaint;
BOOL fRestore;
BOOL fIncUpdate;
BYTE rgbReserved[32];
} PAINTSTRUCT, *PPAINTSTRUCT, *NPPAINTSTRUCT, *LPPAINTSTRUCT;
WIN_LIBRARY_API HDC WINAPI
BeginPaint( HWND hWnd, LPPAINTSTRUCT lpPaint );
WIN_LIBRARY_API BOOL WINAPI
EndPaint( HWND hWnd, CONST PAINTSTRUCT *lpPaint );
WIN_LIBRARY_API BOOL WINAPI PatBlt( HDC hdc, int x, int y, int w, int h, DWORD rop );
#pragma endregion Window PAINT
// Class Style Constants // Class Style Constants
// https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes // https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes
// https://learn.microsoft.com/en-us/windows/win32/winmsg/window-class-styles // https://learn.microsoft.com/en-us/windows/win32/winmsg/window-class-styles
enum BI : DWORD
{
BI_RGB_Uncompressed = 0L,
BI_RunLength_Encoded_8bpp = 1L,
BI_RunLength_Encoded_4bpp = 2L,
};
enum CS : UINT enum CS : UINT
{ {
CS_Own_Device_Context = CS_OWNDC, CS_Own_Device_Context = CS_OWNDC,
@ -104,12 +179,21 @@ enum CS : UINT
CS_Vertical_Redraw = CS_VREDRAW, CS_Vertical_Redraw = CS_VREDRAW,
}; };
enum DIB : UINT
{
DIB_ColorTable_RGB = 0,
DIB_ColorTable_Palette = 1
};
enum MB : UINT enum MB : UINT
{ {
MB_Ok_Btn = MB_OK, MB_Ok_Btn = MB_OK,
MB_Icon_Information = MB_ICONINFORMATION, MB_Icon_Information = MB_ICONINFORMATION,
}; };
#define WM_ACTIVATEAPP 0x001C
enum WS : UINT enum WS : UINT
{ {
WS_Overlapped_Window = WS_OVERLAPPEDWINDOW, WS_Overlapped_Window = WS_OVERLAPPEDWINDOW,
@ -118,8 +202,18 @@ enum WS : UINT
enum RasterOps : DWORD enum RasterOps : DWORD
{ {
RO_Source_To_Dest = (DWORD)0x00CC0020,
RO_Blackness = (DWORD)0x00000042, RO_Blackness = (DWORD)0x00000042,
RO_Whiteness = (DWORD)0x00FF0062, RO_Whiteness = (DWORD)0x00FF0062,
}; };
// } WIN_LIBRARY_API_END
NS_WIN32_END
#undef _SAL_nop_impl_
#undef _SAL2_Source_
#undef _Deref_post2_impl_
#undef _Outptr_result_bytebuffer_
#undef _At_
#undef _When_
#undef GDI_DIBSIZE