Sign in to follow this  
gretty

FillRgn() Wont Work?

Recommended Posts

gretty    273
Hello I have a simple win32 Pong game I am making. But when I try to fill a rectangular region with a white HBRUSH it doesn't work. For some reason the region doesnt get filled? Here is my app; the problem occurs in line 112 in the "WndProc" function:
#include <windows.h>
#include <stdio.h>

static HINSTANCE gInstance;
struct PLAYER {
    
    HRGN rgn;
    int x;
    int y;
    int width;
    int height;    
};
PLAYER player;
bool redrawPlayer = true;

// Functions List //
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


int WINAPI WinMain(HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = gInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+3);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "My Class";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    // if registration of main class fails
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        "My Class",
        "Pong",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,500,500,
        NULL, NULL, gInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {
            player.x = 10;
            player.y = 200;
            player.width = 50;
            player.height = 100;
            player.rgn = CreateRectRgn(player.x,player.y,player.x+player.width,player.y+player.height);
        }     
        break;
        case WM_KEYDOWN:
        {
             switch(LOWORD(wParam)) {
                
                case 65:
                     MessageBox(hwnd,"a pressed","",MB_OK);
                     redrawPlayer = true;
                     InvalidateRect(hwnd,NULL,true); 
                break;
                default:
                break;                    
             }
        }
        break;
        case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            
            hdc = BeginPaint(hwnd,&ps);
            
            if (redrawPlayer) {
                FillRgn(hdc,player.rgn,(HBRUSH)RGB(255,255,255));  // this function isn't working
                //Rectangle(hdc,player.x,player.y,player.x+player.width,player.y+player.height);
                redrawPlayer = false;             
            }
            
            EndPaint(hwnd,&ps); 
        }     
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


Share this post


Link to post
Share on other sites
MrMophead    100
Hi,

You can't just cast the RGB values to an HBRUSH like you are doing. Remember to check return codes. FillRgn was returning an error. Do this instead:


HBRUSH b = CreateSolidBrush(RGB(255, 255, 255));
FillRgn(hdc, player.rgn, b);
DeleteObject(b);



Of course, you probably don't want to create the brush at every WM_PAINT, so create it at the start of the game. You can also use GetStockObject to get the WHITE_BRUSH brush.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this