This application opens a window (no title bar or border) and types 012 based on keyboard input.
For some reason after compiling using g++, it takes 6-7 seconds to run (infact the icon of the executable takes 3-4 seconds to load after compiling, I have never seen that happen). The entire pc lags and the task manager also doesn’t show anything like memory leak or high cpu usage or anything.
include <windows.h>
#include <stdint.h>
#include <stdio.h>
#define glyph_width 5
#define glyph_height 5
int glyph[3][glyph_width * glyph_height] = {{0,1,1,0,0,
1,0,1,1,0,
1,0,0,1,0,
1,1,0,1,0,
0,1,1,0,0},
{0,0,1,0,0,
0,1,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,1,1,1,0},
{0,1,1,0,0,
1,0,0,1,0,
0,0,1,0,0,
0,1,0,0,0,
1,1,1,1,0}};
bool Running;
uint32_t* BitmapBuffer;
int BitmapWidth;
int BitmapHeight;
BITMAPINFO BitmapInfo;
uint32_t color = 0x00ffffff;
int box_x = 0;
int box_y = 0;
LRESULT CALLBACK WindowProcedure(HWND WindowHandle, uint32_t Message, WPARAM WParam, LPARAM LParam){
LRESULT Result = 0;
switch(Message){
case WM_CREATE:{
BitmapWidth = ((CREATESTRUCT*) LParam)->cx;
BitmapHeight = ((CREATESTRUCT*) LParam)->cy;
BitmapBuffer = (uint32_t*) VirtualAlloc(0, BitmapWidth * BitmapHeight * 4, MEM_COMMIT, PAGE_READWRITE);
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
BitmapInfo.bmiHeader.biWidth = BitmapWidth;
BitmapInfo.bmiHeader.biHeight = -BitmapHeight;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
} break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:{
uint16_t VKCode = (uint16_t) WParam;
bool WasKeyDown = (LParam & (1 << 30));
bool IsKeyDown = !(LParam & (1 << 31));
if(IsKeyDown){
for(int y = 0; y < glyph_height; y++){
for(int x = 0; x < glyph_width; x++){
if(glyph[VKCode - 48][y * glyph_width + x] == 1)
BitmapBuffer[(y + box_y * glyph_height) * BitmapWidth + (x + box_x * glyph_width)] = color;
}
}
box_x += 1;
}
} break;
case WM_PAINT:{
SetDIBitsToDevice(GetDC(WindowHandle), 0, 0, BitmapWidth, BitmapHeight, 0, 0, 0, BitmapHeight, BitmapBuffer, &BitmapInfo, DIB_RGB_COLORS);
} break;
default:{
Result = DefWindowProc(WindowHandle, Message, WParam, LParam);
}
}
return Result;
}
int main(){
WNDCLASS WindowClass = {};
WindowClass.lpfnWndProc = WindowProcedure;
WindowClass.lpszClassName = "WindowClassName";
WindowClass.hInstance = (HINSTANCE) GetModuleHandle(0);
RegisterClass(&WindowClass);
AllocConsole();
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
HWND Window = CreateWindow(WindowClass.lpszClassName, "Editor", WS_POPUP, 0, 0, 800, 800, 0, 0, WindowClass.hInstance, 0);
ShowWindow(Window, 1);
Running = true;
while(Running){
MSG Message = {};
while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)){
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
}
I tried commenting peices of code and turns out the inner while(PeekMessage()) loop is the culprit. But without it the app does nothing except for appearing. I am using PeekMessage so I can draw on screen even without any messages. Why does this happen and what can I do to fix it?
Prateek Bana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.