Day 4 complete

Going to have to fork win32 modular headers lib, they didn't do handle declares proper.

So far so good. Made a funny looking gradient
This commit is contained in:
Edward R. Gonzalez 2023-09-09 03:03:03 -04:00
parent 4c832f3353
commit 973936ac82
5 changed files with 181 additions and 55 deletions

View File

@ -214,6 +214,8 @@ typedef double LONGLONG;
typedef double ULONGLONG; typedef double ULONGLONG;
#endif #endif
#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
typedef void VOID; typedef void VOID;
typedef void * PVOID; typedef void * PVOID;
typedef void * LPVOID; typedef void * LPVOID;
@ -225,17 +227,15 @@ typedef LONG * LPLONG;
typedef DWORD * PDWORD; typedef DWORD * PDWORD;
typedef LPVOID HANDLE; typedef LPVOID HANDLE;
typedef HANDLE HINSTANCE; DECLARE_HANDLE(HINSTANCE);
typedef HANDLE HWND; DECLARE_HANDLE(HWND);
typedef HINSTANCE HMODULE; typedef HINSTANCE HMODULE;
typedef HANDLE HDC; DECLARE_HANDLE(HDC);
typedef HANDLE HGLRC; DECLARE_HANDLE(HGLRC);
typedef HANDLE HMENU; DECLARE_HANDLE(HMENU);
typedef HANDLE * PHANDLE; typedef HANDLE * PHANDLE;
typedef HANDLE * LPHANDLE; typedef HANDLE * LPHANDLE;
#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name
typedef WCHAR * PWSTR; typedef WCHAR * PWSTR;
typedef BYTE * LPBYTE; typedef BYTE * LPBYTE;
typedef long * LPLONG; typedef long * LPLONG;

View File

@ -12,6 +12,8 @@
#include "macros.h" #include "macros.h"
#include "types.h" #include "types.h"
#include <stdio.h>
#include "win32.h" #include "win32.h"
NS_WIN32_BEGIN NS_WIN32_BEGIN
@ -20,8 +22,63 @@ global bool Running;
global BITMAPINFO ScreenBitmapInfo; global BITMAPINFO ScreenBitmapInfo;
global void* ScreenBitmapMemory; // Lets use directly mess with the "pixel's memory buffer" global void* ScreenBitmapMemory; // Lets use directly mess with the "pixel's memory buffer"
global HBITMAP ScreenBitmapHandle; global u32 BitmapWidth;
global HDC ScreenDeviceContext; 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 internal void
resize_dib_section( u32 width, u32 height ) resize_dib_section( u32 width, u32 height )
@ -30,45 +87,71 @@ resize_dib_section( u32 width, u32 height )
// TODO(Ed) : Free DIB section // TODO(Ed) : Free DIB section
if ( ScreenBitmapHandle ) if ( ScreenBitmapMemory )
{ {
DeleteObject( ScreenBitmapHandle ); VirtualFree( ScreenBitmapMemory, 0, MEM_RELEASE );
}
if ( ! ScreenDeviceContext )
{
ScreenDeviceContext = CreateCompatibleDC( 0 );
} }
constexpr BITMAPINFOHEADER& header = ScreenBitmapInfo.bmiHeader; BitmapWidth = width;
header.biSize = sizeof( header ); BitmapHeight = height;
header.biWidth = width;
header.biHeight = height; // Negative means top-down in the context of the biHeight
header.biPlanes = 1; # define Top_Down -
header.biBitCount = 32; // Need 24, but want 32 ( alignment ) constexpr BITMAPINFOHEADER&
header.biCompression = BI_RGB_Uncompressed; header = ScreenBitmapInfo.bmiHeader;
header.biSizeImage = 0; ScreenBitmapInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
header.biXPelsPerMeter = 0; ScreenBitmapInfo.bmiHeader.biWidth = BitmapWidth;
header.biYPelsPerMeter = 0; ScreenBitmapInfo.bmiHeader.biHeight = Top_Down BitmapHeight;
header.biClrUsed = 0; ScreenBitmapInfo.bmiHeader.biPlanes = 1;
header.biClrImportant = 0; 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 ScreenBitmapHandle = CreateDIBSection( ScreenDeviceContext, & ScreenBitmapInfo
, DIB_ColorTable_RGB, & ScreenBitmapMemory , DIB_ColorTable_RGB, & ScreenBitmapMemory
// Ignoring these last two // Ignoring these last two
, 0, 0 ); , 0, 0 );
#endif
// ReleaseContext( 0, ScreenDeviceContext ); // 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 internal void
update_window( HDC device_context update_window( HDC device_context, RECT* WindowRect
, u32 x, u32 y , u32 x, u32 y
, u32 width, u32 height ) , 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 StretchDIBits( device_context
#if 0
, x, y, width, height , x, y, width, height
, x, y, width, height , x, y, width, height
#endif
, 0, 0, BitmapWidth, BitmapHeight
, 0, 0, window_width, window_height
, ScreenBitmapMemory, & ScreenBitmapInfo , ScreenBitmapMemory, & ScreenBitmapInfo
, DIB_ColorTable_RGB, RO_Source_To_Dest ); , DIB_ColorTable_RGB, RO_Source_To_Dest );
#endif
} }
LRESULT CALLBACK LRESULT CALLBACK
@ -107,17 +190,17 @@ main_window_callback(
{ {
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 width = info.rcPaint.right - info.rcPaint.left; 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 , x, y
, width, height ); , width, height );
EndPaint( handle, & info ); EndPaint( handle, & info );
} }
break; break;
@ -131,13 +214,11 @@ main_window_callback(
u32 height = client_rect.bottom - client_rect.top; u32 height = client_rect.bottom - client_rect.top;
resize_dib_section( width, height ); resize_dib_section( width, height );
OutputDebugStringA( "WM_SIZE\n" );
} }
break; break;
default: default:
{ {
OutputDebugStringA( "default\n" );
result = DefWindowProc( handle, system_messages, w_param, l_param ); result = DefWindowProc( handle, system_messages, w_param, l_param );
} }
} }
@ -155,7 +236,7 @@ WinMain(
) )
{ {
using namespace win32; 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 {}; WNDCLASS window_class {};
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;
@ -169,7 +250,7 @@ WinMain(
window_class.lpszMenuName = L"Handmade Hero!"; window_class.lpszMenuName = L"Handmade Hero!";
window_class.lpszClassName = L"HandmadeHeroWindowClass"; window_class.lpszClassName = L"HandmadeHeroWindowClass";
if ( RegisterClassW( &window_class ) ) if ( RegisterClassW( & window_class ) )
{ {
HWND window_handle = CreateWindowExW( HWND window_handle = CreateWindowExW(
0, 0,
@ -186,20 +267,36 @@ WinMain(
{ {
Running = true; Running = true;
MSG message; MSG msg_info;
u32 x_offset = 0;
u32 y_offset = 0;
while( Running ) while( Running )
{ {
BOOL msg_result = GetMessage( & message, 0, 0, 0 ); if ( PeekMessageW( & msg_info, 0, 0, 0, PM_Remove_Messages_From_Queue ) )
if ( msg_result > 0 )
{ {
TranslateMessage( & message ); if ( msg_info.message == WM_QUIT )
DispatchMessage( & message ); {
} OutputDebugStringA("WM_QUIT\n");
else Running = false;
{ }
// TODO(Ed) : Logging
break; 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 else

View File

@ -5,3 +5,10 @@
#define local_persist static // Local Persisting variables #define local_persist static // Local Persisting variables
#define api_c extern "C" #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 )

View File

@ -2,9 +2,13 @@
Alternative header for windows.h Alternative header for windows.h
*/ */
// #include <windows.h>
#include "windows/windows_base.h" #include "windows/windows_base.h"
#include "windows/window.h" #include "windows/window.h"
#include "windows/file.h"
#include "windows/io.h"
// #ifdef Build_Debug // #ifdef Build_Debug
# include "windows/dbghelp.h" # include "windows/dbghelp.h"
// #endif // #endif
@ -127,8 +131,9 @@ PatBlt( HDC hdc
constexpr auto DispatchMessage = DispatchMessageA; constexpr auto DispatchMessage = DispatchMessageA;
#endif // !UNICODE #endif // !UNICODE
#pragma region Message Function Templates #pragma region WinUser
// From WinUser.h, modular headers lib doesn't have.
HDC WINAPI GetDC( HWND hWnd );
BOOL WINAPI BOOL WINAPI
GetMessageA( GetMessageA(
@ -159,7 +164,7 @@ GetMessageW(
#else #else
constexpr auto RegisterClass = RegisterClassA; constexpr auto RegisterClass = RegisterClassA;
#endif // !UNICODE #endif // !UNICODE
#pragma endregion Message Function Templates #pragma endregion WinUser
// 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
@ -192,12 +197,21 @@ enum MB : UINT
MB_Icon_Information = MB_ICONINFORMATION, MB_Icon_Information = MB_ICONINFORMATION,
}; };
#define WM_ACTIVATEAPP 0x001C enum Mem : DWORD
enum WS : UINT
{ {
WS_Overlapped_Window = WS_OVERLAPPEDWINDOW, Mem_Commit_Zeroed = 0x00001000,
WS_Initially_Visible = WS_VISIBLE, 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 enum RasterOps : DWORD
@ -207,6 +221,14 @@ enum RasterOps : DWORD
RO_Whiteness = (DWORD)0x00FF0062, 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 WIN_LIBRARY_API_END
NS_WIN32_END NS_WIN32_END

BIN
scripts/handmade.rdbg Normal file

Binary file not shown.