Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
158 lines (132 loc) · 4.97 KB
/
Copy pathmain.cpp
File metadata and controls
158 lines (132 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <windows.h>
#include <stdint.h>
#include <stdbool.h>
typedef uint8_t uint8;
typedef uint32_t uint32;
static bool Running = true;
static void* FramebufferMemory;
static int FramebufferWidth = 800;
static int FramebufferHeight = 600;
static BITMAPINFO BitmapInfo;
// 1. Reallocate pixel memory on window resize
static void ResizeFramebuffer(int Width, int Height) {
if (FramebufferMemory) {
VirtualFree(FramebufferMemory, 0, MEM_RELEASE);
}
FramebufferWidth = Width;
FramebufferHeight = Height;
BitmapInfo.bmiHeader.biSize = sizeof(BitmapInfo.bmiHeader);
BitmapInfo.bmiHeader.biWidth = FramebufferWidth;
BitmapInfo.bmiHeader.biHeight = -FramebufferHeight; // Negative top-down orientation
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32; // 0x00RRGGBB format
BitmapInfo.bmiHeader.biCompression = BI_RGB;
int BytesPerPixel = 4;
int MemorySize = (FramebufferWidth * FramebufferHeight) * BytesPerPixel;
FramebufferMemory = VirtualAlloc(0, MemorySize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}
// 2. Manipulate raw pixels in system RAM
static void RenderWeirdGradient(int XOffset, int YOffset) {
uint8* Row = (uint8*)FramebufferMemory;
int Pitch = FramebufferWidth * 4;
for (int Y = 0; Y < FramebufferHeight; ++Y) {
uint32* Pixel = (uint32*)Row;
for (int X = 0; X < FramebufferWidth; ++X) {
uint8 Blue = (uint8)(X + XOffset);
uint8 Green = (uint8)(Y + YOffset);
uint8 Red = (uint8)(X + Y);
*Pixel++ = (Red << 16) | (Green << 8) | Blue;
}
Row += Pitch;
}
}
// 3. Blit memory buffer to display context
static void DisplayBufferInWindow(HDC DeviceContext, int WindowWidth, int WindowHeight) {
StretchDIBits(
DeviceContext,
0, 0, WindowWidth, WindowHeight,
0, 0, FramebufferWidth, FramebufferHeight,
FramebufferMemory,
&BitmapInfo,
DIB_RGB_COLORS,
SRCCOPY
);
}
// 4. Callback procedure
LRESULT CALLBACK MainWindowCallback(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) {
LRESULT Result = 0;
switch (Message) {
case WM_SIZE: {
RECT ClientRect;
GetClientRect(Window, &ClientRect);
int Width = ClientRect.right - ClientRect.left;
int Height = ClientRect.bottom - ClientRect.top;
ResizeFramebuffer(Width, Height);
} break;
case WM_CLOSE:
case WM_DESTROY: {
Running = false;
} break;
case WM_PAINT: {
PAINTSTRUCT Paint;
HDC DeviceContext = BeginPaint(Window, &Paint);
RECT ClientRect;
GetClientRect(Window, &ClientRect);
DisplayBufferInWindow(DeviceContext, ClientRect.right - ClientRect.left, ClientRect.bottom - ClientRect.top);
EndPaint(Window, &Paint);
} break;
default: {
Result = DefWindowProc(Window, Message, wParam, lParam);
} break;
}
return Result;
}
// Win32 Entry Point
int WINAPI WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode) {
WNDCLASS WindowClass = {0};
WindowClass.style = CS_HREDRAW | CS_VREDRAW;
WindowClass.lpfnWndProc = MainWindowCallback;
WindowClass.hInstance = Instance;
WindowClass.lpszClassName = "SoftwareRendererWindowClass";
if (RegisterClass(&WindowClass)) {
// Adjust client rect so rendering area is exact 800x600 inside window borders
RECT WindowRect = { 0, 0, 800, 600 };
AdjustWindowRect(&WindowRect, WS_OVERLAPPEDWINDOW, FALSE);
HWND Window = CreateWindowEx(
0,
WindowClass.lpszClassName,
"Raw Pixel Software Renderer",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
0, 0, Instance, 0
);
if (Window) {
int XOffset = 0;
int YOffset = 0;
// Application Loop
while (Running) {
MSG Message;
while (PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) {
if (Message.message == WM_QUIT) {
Running = false;
}
TranslateMessage(&Message);
DispatchMessage(&Message);
}
RenderWeirdGradient(XOffset, YOffset);
HDC DeviceContext = GetDC(Window);
RECT ClientRect;
GetClientRect(Window, &ClientRect);
DisplayBufferInWindow(DeviceContext, ClientRect.right - ClientRect.left, ClientRect.bottom - ClientRect.top);
ReleaseDC(Window, DeviceContext);
++XOffset;
YOffset += 2;
// Yield CPU slightly to cap framing rate
Sleep(16); // ~60 FPS
}
}
}
return 0;
}