Skip to content

Instantly share code, notes, and snippets.

@ph4un00b
Created September 17, 2015 04:25
Show Gist options
  • Save ph4un00b/ea6a27c66edd0c5e7a93 to your computer and use it in GitHub Desktop.
Save ph4un00b/ea6a27c66edd0c5e7a93 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <stdint.h>
#define internal static // just internal for the file? -> translation unit
#define local_persist static
#define global_variable static // automatically initialize to 0
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
global_variable bool global_Runnnig; // global flag by now.
struct win32_offscreen_buffer
{
BITMAPINFO info;
void *memory;
int width;
int height;
int pitch;
int bytes_per_pixel;
};
global_variable win32_offscreen_buffer global_back_buffer;
struct win32_window_dimension
{
int width;
int height;
};
win32_window_dimension
win32_get_window_dimension(HWND Window)
{
win32_window_dimension Result;
RECT ClientRect;
GetClientRect(Window, &ClientRect/*out lpRect*/);
Result.height = ClientRect.bottom - ClientRect.top;
Result.width = ClientRect.right - ClientRect.left;
return(Result);
}
internal void
render_weird_gradient(win32_offscreen_buffer Buffer, int XOffset, int YOffset)
{
uint8 *Row = (uint8 *)Buffer.memory;
for (int Y = 0; Y < Buffer.height; ++Y)
{
// uint8 *Pixel = (uint8 *)Row;
uint32 *Pixel = (uint32 *)Row;
for (int X = 0; X < Buffer.width; ++X)
{
// pixel 32 bits
// mem: bb gg rr xx
// reg: xx rr gg bb
// uint8 red = ;
uint8 blue = (X + XOffset);
uint8 green = (Y + YOffset);
*Pixel++ = (green << 8) | blue;
// ++Pixel;
// *Pixel = (uint8)(Y + yOffset);
// ++Pixel;
// *Pixel = 255;
// ++Pixel;
// *Pixel = 0;
// ++Pixel;
}
Row += Buffer.pitch;
}
}
internal void
win32_Resize_DIB_section(win32_offscreen_buffer *Buffer, int Width, int Height)
{
// free memory
if(Buffer->memory)
{
VirtualFree(Buffer->memory, 0, MEM_RELEASE);
}
Buffer->width = Width;
Buffer->height = Height;
Buffer->bytes_per_pixel = 4;
Buffer->info.bmiHeader.biSize = sizeof(Buffer->info.bmiHeader);
Buffer->info.bmiHeader.biWidth = Buffer->width;
Buffer->info.bmiHeader.biHeight = -Buffer->height; // when negative treat this as top-down not bottom-up
Buffer->info.bmiHeader.biPlanes = 1;
Buffer->info.bmiHeader.biBitCount = 32;
Buffer->info.bmiHeader.biCompression = BI_RGB;
// bitmap_info.bmiHeader.biSizeImage = 0; // initialized as stat
// bitmap_info.bmiHeader.biXPelsPerMeter = 0;
// bitmap_info.bmiHeader.biYPelsPerMeter = 0;
// bitmap_info.bmiHeader.biClrUsed = 0;
// bitmap_info.bmiHeader.biClrImportant = 0;
// HDC device_context = GetCompatibleDC(0);
int bitmap_memory_size = (Buffer->width * Buffer->height) * Buffer->bytes_per_pixel;
Buffer->memory = VirtualAlloc(0, bitmap_memory_size, MEM_COMMIT, PAGE_READWRITE);
Buffer->pitch = Width * Buffer->bytes_per_pixel;
}
internal void
win32_copy_buffer_2_window(HDC Device_context,
int Window_width, int Window_height,
win32_offscreen_buffer Buffer,
int X, int Y, int Width, int Height)
{
// correct aspect ratio
StretchDIBits( // rectangle to rectngle copy // buffer to buffer
/*_In_ HDC*/ Device_context,
// destination rectangle
/*_In_ int*/ 0,
/*_In_ int*/ 0,
/*_In_ int*/ Window_width,
/*_In_ int*/ Window_height,
// source rectangle, out windows
/*_In_ int*/ 0,
/*_In_ int*/ 0,
/*_In_ int*/ Buffer.width,
/*_In_ int*/ Buffer.height,
/*_In_ const VOID*/ Buffer.memory,
/*_In_ const BITMAPINFO*/ &Buffer.info,
/*_In_ UINT*/ DIB_RGB_COLORS, // kind of buffer
/*_In_ DWORD*/ SRCCOPY // https://msdn.microsoft.com/en-us/library/windows/desktop/dd183370(v=vs.85).aspx
);
}
// typedef struct {
// UINT style;
// WNDPROC lpfnWndProc;
// int cbClsExtra;
// int cbWndExtra;
// HINSTANCE hInstance;
// HICON hIcon;
// HCURSOR hCursor;
// HBRUSH hbrBackground;
// LPCTSTR lpszMenuName;
// LPCTSTR lpszClassName;
// } WNDCLASS, *PWNDCLASS;
LRESULT CALLBACK
win32_main_windows_callback(
HWND Window,
UINT Message,
WPARAM WParam,
LPARAM LParam
)
{
LRESULT Result = 0;
switch(Message) {
case WM_SIZE:
{
} break;
case WM_DESTROY:
{
global_Runnnig = false;
} break;
case WM_CLOSE:
{
global_Runnnig = false;
} break;
case WM_ACTIVATEAPP:
{
OutputDebugStringA("WM_ACTIVATEAPP\n");
} break;
case WM_PAINT:
{
OutputDebugStringA("wm_PAIN\n");
PAINTSTRUCT paint;
HDC DeviceContext =
BeginPaint(
Window, // HWND hwnd,
&paint //LPPAINTSTRUCT lpPaint
);
int x = paint.rcPaint.left;
int y = paint.rcPaint.top;
int height = paint.rcPaint.bottom - paint.rcPaint.top;
int width = paint.rcPaint.right - paint.rcPaint.left;
win32_window_dimension dimension = win32_get_window_dimension(Window);
win32_copy_buffer_2_window(DeviceContext, dimension.width, dimension.height, global_back_buffer,
x, y, width, height);
// PatBlt(DeviceContext, x, y, width, height, BLACKNESS);
EndPaint(
Window, //HWND hWnd,
&paint //PAINTSTRUCT *lpPaint
);
} break;
default:
{
// OutputDebugStringA("default msg\n");
Result = DefWindowProc(Window, Message, WParam, LParam);
} break;
}
return(Result);
}
int CALLBACK
WinMain(
HINSTANCE Instance,
HINSTANCE PrevInstance,
LPSTR CommandLine,
int ShowCode)
{
// MessageBoxA(0, "this is a window", "title", MB_OK | MB_ICONINFORMATION);
WNDCLASS WindowsClass = {}; // initiliza to 0 all properties;
win32_Resize_DIB_section(&global_back_buffer, 1280, 720);
OutputDebugStringA("wm_size\n");
WindowsClass.style = CS_HREDRAW|CS_VREDRAW; // redraw the entire window
WindowsClass.lpfnWndProc = win32_main_windows_callback;
WindowsClass.hInstance = Instance;
// WindowsClass.hIcon = ;
WindowsClass.lpszClassName = "jamon";
if(RegisterClass(&WindowsClass)) {
HWND window =
CreateWindowEx(
0, // DWORD dwExStyle,
WindowsClass.lpszClassName, // LPCTSTR lpClassName,
"lol jamon", //LPCTSTR lpWindowName,
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // DWORD dwStyle,
CW_USEDEFAULT, // int x,
CW_USEDEFAULT, // int y,
CW_USEDEFAULT, // int nWidth,
CW_USEDEFAULT, // int nHeight,
0, // HWND hWndParent,
0, // HMENU hMenu,
Instance, //HINSTANCE hInstance,
0 //LPVOID lpParam // WM_CREATE
);
if (window)
{
int xOffset = 0;
int yOffset = 0;
global_Runnnig = true;
while (global_Runnnig)
{
MSG message;
while(PeekMessage(
/*_Out_ LPMSG*/ &message,
/*_In_opt_ HWND */ 0,
/*_In_ UINT */ 0,
/*_In_ UINT */ 0,
/*_In_ UINT */ PM_REMOVE
))
{
if(message.message == WM_QUIT)
{
global_Runnnig = false;
}
TranslateMessage(&message);
DispatchMessage(&message);
}
render_weird_gradient(global_back_buffer, xOffset, yOffset);
HDC device_context = GetDC(window);
win32_window_dimension dimension = win32_get_window_dimension(window);
win32_copy_buffer_2_window(device_context, dimension.width, dimension.height,
global_back_buffer,
0, 0, dimension.width, dimension.height);
ReleaseDC(window, device_context);
++xOffset;
}
}
else
{
// todo: login
}
}
else
{
// todo: login
}
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment