I am working with the win32 API, and I wrote a simple program to draw 100 random pixels ten times a second. However, when I compile and run the code, the screen only refreshes just around once a second. I would debug it but I am working on a locked down device and my IDE doesn't have debugging. I have changed all variables I can find referencing the timer function and nothing is working. I am compiling using gcc. Here is the code:
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
const int ID_TIMER = 1;
void DrawRandom(HDC hdc);
//Double buffering implementation
static void Paint(HDC hdc, RECT* prc){
//Declares our varibles
HDC hdcMem;
HBITMAP hbmMem, hbmOld;
HBRUSH hbrBkGnd;
POINT Border;
//Creates creates compatible device context
hdcMem = CreateCompatibleDC(hdc);
//Create bitmap big enough for our display
hbmMem = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
//Select the bitmap into the offscreen DC
hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
//Erase the background
hbrBkGnd = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
FillRect(hdcMem, prc, hbrBkGnd);
DeleteObject(hbrBkGnd);
//Gets the size of our border
Border.x = prc->right;
Border.y = prc->bottom;
//Draws random pixels
DrawRandom(hdcMem);
//BLT the changes to the screen dc
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcMem, 0, 0, SRCCOPY);
//Memory manegment
SelectObject(hdcMem, hbmOld);
DeleteObject(hbmMem);
DeleteDC(hdcMem);
}
//Sets up our window
int WINAPI
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nShowCmd) {
MSG msg;
WNDCLASSW wc = {0};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = L"Cube!";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClassW(&wc);
CreateWindowW(wc.lpszClassName, L"Cube",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 300, 250, NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//Calls our buffer and initilizes timer
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
switch(msg) {
//Initilizes timer
case WM_CREATE:
{
UINT ret;
ret = SetTimer(hwnd, ID_TIMER, 50, NULL);
}
break;
case WM_ERASEBKGND:
//Disables auto clear
return (LRESULT)1;
break;
//Makes sure the window closes
case WM_CLOSE:
DestroyWindow(hwnd);
break;
//This is what initilizes the contents of the window
case WM_PAINT:
{
RECT rcClient;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rcClient);
Paint(hdc, &rcClient);
EndPaint(hwnd, &ps);
}
break;
//This is where the main update loop is
case WM_TIMER:
{
RECT rcClient;
HDC hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClient);
Paint(hdc, &rcClient);
}
break;
//Kills the timer
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
break;
//Make sures the program closes
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void DrawRandom(HDC hdc){
srand(time(NULL));
for(int i = 0;i < 100;i++){
int x = rand() % 400;
int y = rand() % 400;
SetPixel(hdc, x, y, RGB(0,0,0));
}
}