dont want to take you guys time so gonna go straight to the point, i've tried everything i could to try and paint pixels to the window in the day 004 video and failed, i really
dont know what else to do, the window just wont change color no matter what, if anyone could shine a light here i'd be thankful, looking forward to you guys response and thx a lot!
#include <windows.h>
#include <winnt.h>
#include <stdint.h>
#define internalscope static // static variable cant be accessed by other files
#define localscope static // refer to static local variables as localscope
#define globalscope static //refer to static global variables as globalscope
/* typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO, *PBITMAPINFO; */
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
globalscope bool running;
globalscope BITMAPINFO bitmapinfo; // static vars are automatically initialized to zero
globalscope void *bitmapmemory;
globalscope int bitmapwidth;
globalscope int bitmapheight;
internalscope void win32resizeDIBsection(int width, int height) //DIB device independent bitmap
{
if(bitmapmemory){
VirtualFree(bitmapmemory,0 ,MEM_RELEASE);
}
bitmapwidth = width;
bitmapheight= height;
bitmapinfo.bmiHeader.biSize = sizeof(bitmapinfo.bmiHeader);
bitmapinfo.bmiHeader.biWidth = bitmapwidth;
bitmapinfo.bmiHeader.biHeight = -bitmapheight;
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 32;
bitmapinfo.bmiHeader.biCompression = BI_RGB;
bitmapinfo.bmiHeader.biSizeImage = 0;
bitmapinfo.bmiHeader.biXPelsPerMeter = 0;
bitmapinfo.bmiHeader.biYPelsPerMeter = 0;
bitmapinfo.bmiHeader.biClrUsed = 0;
bitmapinfo.bmiHeader.biClrImportant = 0;
int bytesperpixel = 4; // number of bytes per each pixel on the screen
int bitmapmemorysize = (bitmapwidth*bitmapheight) * bytesperpixel;
bitmapmemory = VirtualAlloc(0,bitmapmemorysize, MEM_COMMIT, PAGE_READWRITE);
//TODO:draw pixels to the screen, day 004
int pitch = width*bytesperpixel;
uint8 *row = (uint8 *)bitmapmemory;
for(int y=0; y < bitmapheight;++y){
uint8 *pixel = (uint8 *)row;
for(int x=0; x < bitmapwidth;++x){
*pixel = 255;
++pixel;
*pixel = 0;
++pixel;
*pixel = 0;
++pixel;
*pixel = 0;
++pixel;
}
row += pitch;
}
}
internalscope void win32updatewindow(HDC DeviceContext, RECT *windowrect,
int x, int y, int width, int height)
{
int windowwidth = windowrect -> left - windowrect -> right;
int windowheight = windowrect -> top - windowrect -> bottom;
StretchDIBits(
DeviceContext,
/*x, y, width, height,
x, y, width, height,*/
0, 0 , bitmapwidth, bitmapheight,
0, 0 , windowwidth, windowheight,
bitmapmemory, &bitmapinfo, DIB_RGB_COLORS, SRCCOPY);
}
LRESULT CALLBACK win32mainwindowcallback( //MainWindowProc function <-- main window procedure
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam)
{
LRESULT Result = 0;
switch(uMsg)
{
case WM_ACTIVATEAPP:
{
}
break;
case WM_CLOSE:
{
running = false;
}
break;
case WM_DESTROY:
{
running = false;
}
break;
case WM_SIZE:
{
RECT clientrect;
GetClientRect(hwnd, &clientrect);
int width = clientrect.right - clientrect.left;
int height = clientrect.bottom - clientrect.top;
win32resizeDIBsection(width, height);
}
break;
case WM_PAINT:
{
PAINTSTRUCT Paint;
HDC DeviceContext = BeginPaint(hwnd,&Paint);
int x = Paint.rcPaint.left;
int y = Paint.rcPaint.top;
int width = Paint.rcPaint.right - Paint.rcPaint.left;
int height = Paint.rcPaint.bottom - Paint.rcPaint.top;
RECT clientrect;
GetClientRect(hwnd, &clientrect);
win32updatewindow(DeviceContext, &clientrect, x, y, width, height);
//PatBlt(DeviceContext,x,y,width,height,WHITENESS);
EndPaint(hwnd,&Paint);
}
break;
default:
{
Result = DefWindowProc(hwnd,uMsg,wParam,lParam);//assigns the function DefWindowProc to Result
}
break;
}
return(Result);
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow)
{
WNDCLASS windowclass = {CS_OWNDC|CS_HREDRAW|CS_VREDRAW,win32mainwindowcallback,0,0,hInstance,0,0,0,0,"Handmadeherowindowclass"};
if (RegisterClassA(&windowclass)) { RegisterClass
HWND windowhandle = CreateWindowExA(0,windowclass.lpszClassName,"Handmade Hero",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hInstance,0);
if(windowhandle){
running = true;
while(running){
MSG Message;
BOOL MessageResult = GetMessageA(&Message,0,0,0);
if(MessageResult > 0){
TranslateMessage(&Message);
DispatchMessage(&Message); //sends the message to the Main Window Callback
}
else
{
break;
}
}
}
else
{
// logging
}
}
else
{
//logging
}
return 0;
};