webspy 0 Newbie Poster

I'm trying to make a simple game using the Windows GDI, so I'm developing some C++ classes to take care of window class registration, window creation, painting and other common tasks. So far I've finished the OBJECT class (which will be the base object from which the other classes such as BUTTON will derive), which holds a bitmap and paints it. The class works well so far, but I face a problem I can't find the solution. My windows stops updating (painting) itselfs after a while, even if it seems it gets the WM_PAINT message. Here's the code with which I test my class:

#include <windows.h>
#include "engine.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

char szClassName[] = "Win32App";

char* Bitmaps[2] =     {    "C:\\Windows\\ACD Wallpaper.bmp",
                        "C:\\Windows\\Web\\Wallpaper\\Bliss.bmp"
                    };

OBJECT bmp;

HBITMAP Bitmap[2];
int BitmapCount = 2;

int i = 1, j = 0;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPTSTR CmdLine, int CmdShow)
{
    HWND hWnd;
    MSG msg;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(wc));

    wc.hInstance = hInstance;
    wc.lpszClassName = szClassName;
    wc.lpfnWndProc = WndProc;
    wc.style = CS_DBLCLKS;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;

    if(!RegisterClassEx(&wc))
        return 0;

    hWnd = CreateWindowEx(0, szClassName, "SampleApp", WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL,
                            hInstance, NULL);

    Bitmap[0] = (HBITMAP)LoadImage(hInstance, Bitmaps[0], IMAGE_BITMAP,
                    1024, 768, LR_LOADFROMFILE);
    Bitmap[1] = (HBITMAP)LoadImage(hInstance, Bitmaps[1], IMAGE_BITMAP,
                    1024, 768, LR_LOADFROMFILE);

    ShowWindow(hWnd, SW_SHOWMAXIMIZED);
    UpdateWindow(hWnd);

    bmp.SetID(100);
    bmp.Create(Bitmap[0], 0, 0, 1024, 768, hWnd, NULL);
    bmp.Show();

    SetTimer(hWnd, 100, 1, NULL);

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    char s[24];
    wsprintf(s, "painted %d times.", j);
    MessageBox(NULL, s, "", 0);

    return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;

    switch(msg)
    {
        case WM_TIMER:
        {
            if(i >= BitmapCount - 1)
                i = 0;
            else
                i++;
            RECT rect;
            GetClientRect(hWnd, &rect);
            InvalidateRect(hWnd, &rect, FALSE);
            return 0;
        }
        case WM_CREATE:
        {
            i = 1;
            return 0;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }

        case WM_PAINT:
        {
            j++;
            HDC hDC = BeginPaint(hWnd, &ps);
            HDC hDCMem = CreateCompatibleDC(hDC);
            HBITMAP hDCBMP = CreateCompatibleBitmap(hDC, 1024, 768);
            HANDLE hOld = SelectObject(hDCMem, hDCBMP);

            bmp.SetBitmap(Bitmap[i]);
            bmp.Paint(hDCMem);
            TextOut(hDCMem, 10, 10, Bitmaps[i], strlen(Bitmaps[i]));

            BitBlt(hDC, 0, 0, 1024, 768, hDCMem, 0, 0, SRCCOPY);

            SelectObject(hDCMem, hOld);
            DeleteDC(hDCMem);
            EndPaint(hWnd, &ps);

            return 0;
        }

        case WM_LBUTTONUP:
        {
            if(i >= BitmapCount - 1)
                i = 0;
            else
                i++;
            RECT rect;
            GetClientRect(hWnd, &rect);
            InvalidateRect(hWnd, &rect, FALSE);
            return 0;
        }
        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    return 0;
}

I've attached the executable if you want to test it (make sure those 2 bitmaps exists). Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.