0

I'm working on a "breakout" game and everything worked fine until i tried to add in double buffering.
I tried to take out double buffering since i couldn't get it to work and now, it draws everything from the top left corner of the screen instead of in the client area.
g_hwnd isnt 0

This is the code that's doing the drawing

void C_Graphics::DrawAll(C_Brick Bricks[])
{
    hbr = CreateSolidBrush(RGB(0,0,0));
    RECT r;
    r = Player.Get_BallInfo();
    Ellipse(hdc, r.left, r.top, r.right, r.bottom);
    FillRect(hdc, Player.Get_PlankInfo(), hbr);
    for(int i = 0; i < 20; i++)
    {
        if (Bricks[i].Get_Life() > 0)
            FillRect(hdc, Bricks[i].Get_Info_ptr(), hbr);
    }
    DeleteObject(hbr);
}

void C_Graphics::ClearScreen()
{
    hbr = CreateSolidBrush(RGB(255, 255, 255));
    RECT r;
    GetClientRect(g_hwnd, &r);
    FillRect(hdc, &r, hbr);
    DeleteObject(hbr);
}

http://cboard.cprogramming.com/showthread.php?t=87786

2
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by h_howee
0

Think you'll have to attach complete code (including that of Player, Brick etc classes). From teh code you've posted all looks well.
PS: Please do ATTACH and not paste the whole code here.

0

Another thing, if i open it by double clicking the app, the client area is all grey and nothing gets drawn
It only draws from the top left of the screen when i press run on Codeblock

Attachments
#include "blocks.h"

C_Brick::C_Brick(int i)
{
    Life = i;
}
#ifndef BLOCKS_H
#define BLOCKS_H

#include <windows.h>

class C_Brick
{
    public:
        C_Brick(int i = 1);

        void Set_Info(RECT rect) { Info = rect; }
        RECT Get_Info()         { return Info; }
        RECT* Get_Info_ptr()    { return &Info; }
        void Set_Life(int i)    { Life = i; }
        void Subtract_Life()    { Life--; }
        int  Get_Life()         { return Life; }
    private:
        RECT Info;
        int Life;
};

#endif
#include "main.h"
#include "graphics.h"
#include "player.h"

C_Graphics::C_Graphics()
{
    RECT ClientRect;
    GetClientRect(g_hwnd, &ClientRect);
    hdc = GetDC(g_hwnd);
    if (!hdc)
        MessageBox(g_hwnd, "GetDC(g_hwnd);", "ERROR", MB_OK);
}

C_Graphics::~C_Graphics()
{
    ReleaseDC(g_hwnd, hdc);/*
    DeleteDC(Back_DC);
    SelectObject(Back_DC, Old_bmp);
    DeleteObject(Back_bmp);
    DeleteDC(Img_DC);
    DeleteDC(Back_DC);
    DeleteObject(Ball_bmp);*/
}

void C_Graphics::DrawAll(C_Brick Bricks[])
{
    if (g_hwnd)
        MessageBox(g_hwnd, "g_hwnd is working properly", "Game", MB_OK);
    hbr = CreateSolidBrush(RGB(0,0,0));
    RECT r;
    r = Player.Get_BallInfo();
    Ellipse(hdc, r.left, r.top, r.right, r.bottom);
    FillRect(hdc, Player.Get_PlankInfo(), hbr);
    for(int i = 0; i < 20; i++)
    {
        if (Bricks[i].Get_Life() > 0)
            FillRect(hdc, Bricks[i].Get_Info_ptr(), hbr);
    }
    DeleteObject(hbr);
}

void C_Graphics::ClearScreen()
{
    hbr = CreateSolidBrush(RGB(255, 255, 255));
    RECT r;
    GetClientRect(g_hwnd, &r);
    FillRect(hdc, &r, hbr);
    DeleteObject(hbr);
}

void C_Graphics::DebugDisplay()
{/*
    char temp[5];
    RECT r;
    GetClientRect(g_hwnd, &r);*/
}
#ifndef GRAPHICS_H
#define GRAPHICS_H

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

class C_Graphics
{
    public:
        C_Graphics();
        ~C_Graphics();
/*
        void InitDoubleBuffer();
        void SwitchBuffer();
        void TerminateDoubleBuffer();
*/
        void DrawAll(C_Brick*);
        void ClearScreen();

        void DebugDisplay();
    private:
        //bool fDoubleBuffer;
        HDC hdc;
        //HDC Back_DC;
        HBRUSH hbr;
        //HBITMAP Back_bmp;
        //HBITMAP Old_bmp;
        //HDC Img_DC;

        //HBITMAP Ball_bmp;
};

#endif
#ifndef MAIN_H
#define MAIN_H

#include <windows.h>
#include "blocks.h"
#include "graphics.h"
#include "player.h"

extern HWND g_hwnd;
extern HINSTANCE g_hinstance;

extern C_Brick Brick[];

extern C_Player Player;

extern UINT Counter;

#endif
#include <windows.h>
#include "main.h"

HWND g_hwnd;
HINSTANCE g_hinstance;
C_Brick Brick[20];
C_Graphics Graphics;
C_Player Player;
UINT Counter = 0;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nFunsterStil)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           251,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
    g_hwnd = hwnd;
    //g_hinstance = hThisInstance;
    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);
    UpdateWindow(hwnd);

    RECT r;
    for (int i = 0; i < 5; i++) //x
    {
        for (int j = 0; j < 4; j++) //y
        {
            r.left = i*50;
            r.top = j*20;
            r.right = r.left+49;
            r.bottom = r.top+19;
            Brick[i*4+j].Set_Info(r);
        }
    }

    //Graphics.InitDoubleBuffer();

    while(1)
    {
        if(PeekMessage(&messages, NULL, 0, 0, PM_REMOVE))
        {
			if(messages.message == WM_QUIT)
				break;

			TranslateMessage(&messages);
			DispatchMessage(&messages);
        }
        else
        {
            Counter++;
            if (Counter >= 1000000) Counter = 0;
            Player.Plank_Movement();
            Player.Ball_Movement();
            Player.Collision(Brick);
            Graphics.ClearScreen();
            Graphics.DrawAll(Brick);
            //Graphics.SwitchBuffer();
            Player.DebugDisplay();
        }
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_MOUSEMOVE:/*
            POINT MouseCoord;
            MouseCoord.x = LOWORD(lParam);
            MouseCoord.y = HIWORD(lParam);
            Player.Set_Mouse_Pos(MouseCoord);*/
            break;
        case WM_LBUTTONDOWN:
            Player.LaunchBall();
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
#include "main.h"
#include "player.h"
#include <string>

C_Player::C_Player()
{
    int result;
    RECT r;
    result = GetClientRect(g_hwnd, &r);
    if (result == 0)
        MessageBox(g_hwnd, "GetClientRect", "ERROR", MB_OK);
    Plank_Size = 50;
    Plank_Speed = 1;
    /*
    Plank_Info.left = (r.right-r.left)/2 - Plank_Size/2;
    Plank_Info.right = Plank_Info.left + Plank_Size;
    Plank_Info.top = r.bottom - 30;
    Plank_Info.bottom = Plank_Info.top + 10;
    */

    Player_Life = 5;

    Plank_Info.left = 10;
    Plank_Info.right = 55;
    Plank_Info.top = 305;
    Plank_Info.bottom = 315;

    Ball_Size = 10;
    Ball_Speed = 25;

    Ball_Dir.V = N;
}

void C_Player::Plank_Movement()
{
    if (!Counter%Plank_Speed)
        return;
    Set_Mouse_Pos();
    if (Mouse.x == Plank_Info.right-(Plank_Info.right-Plank_Info.left)/2)
        return;
    if (Mouse.x > Plank_Info.right-(Plank_Info.right-Plank_Info.left)/2)
        { Plank_Info.left++; Plank_Info.right++; }
    if (Mouse.x < Plank_Info.right-(Plank_Info.right-Plank_Info.left)/2)
        { Plank_Info.left--; Plank_Info.right--; }
}

void C_Player::Ball_Movement()
{
    if (Counter%Ball_Speed)
        return;
    if (Ball_Status == 0)
    {
        Ball_Info.left = Plank_Info.left+(Plank_Info.right-Plank_Info.left)/2 - (Ball_Info.right-Ball_Info.left)/2;
        Ball_Info.right = Ball_Info.left + Ball_Size;
        Ball_Info.bottom = Plank_Info.top;
        Ball_Info.top = Ball_Info.bottom - Ball_Size;
        return;
    }

    if(Ball_Dir.V == N)
    {
        Ball_Info.top--;
        Ball_Info.bottom--;
    }
    else
    {
        Ball_Info.top++;
        Ball_Info.bottom++;
    }

    if(Ball_Dir.H == W)
    {
        Ball_Info.left--;
        Ball_Info.right--;
    }
    else
    {
        Ball_Info.left++;
        Ball_Info.right++;
    }
}

void C_Player::Set_Mouse_Pos(POINT c)
{
    Mouse.x = c.x;
    Mouse.y = c.y;
}

void C_Player::Set_Mouse_Pos()
{
    GetCursorPos(&Mouse);
    ScreenToClient(g_hwnd, &Mouse);
}

void C_Player::DebugDisplay()
{
    HDC hdc = GetDC(g_hwnd);
    //std::string sz_temp;
    char sz_temp[30];
    RECT r;
    if (!GetClientRect(g_hwnd, &r))
        MessageBox(g_hwnd, "ERROR", "GetClientRect(g_hwnd, &r)", MB_OK);

    /*
    itoa(Mouse.x, sz_temp, 10);
    TextOut(hdc, 260, 10, sz_temp, sizeof(sz_temp));

    itoa(Mouse.y, sz_temp, 10);
    TextOut(hdc, 260, 50, sz_temp, sizeof(sz_temp));
    */

    itoa(r.left, sz_temp, 10);
    TextOut(hdc, 260, 10, sz_temp, sizeof(sz_temp));

    itoa(r.top, sz_temp, 10);
    TextOut(hdc, 260, 40, sz_temp, sizeof(sz_temp));

    itoa(r.right, sz_temp, 10);
    TextOut(hdc, 260, 70, sz_temp, sizeof(sz_temp));

    itoa(r.bottom, sz_temp, 10);
    TextOut(hdc, 260, 100, sz_temp, sizeof(sz_temp));

    itoa((int)g_hwnd, sz_temp, 10);
    TextOut(hdc, 260, 130, sz_temp, sizeof(sz_temp));

    ReleaseDC(g_hwnd, hdc);
}

void C_Player::Collision(C_Brick b[])
{
    RECT ClientRect;
    RECT BrickRect;
    int result = GetClientRect(g_hwnd, &ClientRect);
    if(!result)
        MessageBox(g_hwnd, "ERROR", "void C_Player::Collision(C_Brick b[]) \n GetClientArea(g_hwnd, &ClientRect);", MB_OK);
    //ball limits
    if (Ball_Info.left <= ClientRect.left)
        Ball_Dir.H = E;
    if (Ball_Info.right >= ClientRect.right)
        Ball_Dir.H = W;
    if (Ball_Info.top <= ClientRect.top)
        Ball_Dir.V = S;
    if (Ball_Info.bottom >= ClientRect.bottom)
        Ball_Dir.V = N;

    RECT CollisionRect;
    if(IntersectRect(&CollisionRect, &Ball_Info, &Plank_Info))
        Ball_Dir.V = N;

    for (int i = 0; i < 20; i++)
    {
        BrickRect = b[i].Get_Info();
        if (b[i].Get_Life() <= 0)
            continue;
            continue;
        if(IntersectRect(&CollisionRect, &Ball_Info, &BrickRect))
        {
            b[i].Subtract_Life();
            MessageBox(g_hwnd, "Game", "Collision", MB_OK);
        }
        else
            continue;
        if (Ball_Info.top == BrickRect.bottom || Ball_Info.top == BrickRect.bottom-1)
            Ball_Dir.V = S;
        if (Ball_Info.bottom == BrickRect.top || Ball_Info.bottom == BrickRect.top-1)
            Ball_Dir.V = N;
        if (Ball_Info.left == BrickRect.right || Ball_Info.left == BrickRect.right-1)
            Ball_Dir.H = E;
        if (Ball_Info.right == BrickRect.left || Ball_Info.right == BrickRect.left-1)
            Ball_Dir.H = W;
    }

    if (Ball_Info.bottom <= ClientRect.bottom && Ball_Status == 1)
    {
        //MessageBox(g_hwnd, "You Lose", "Press OK to continue", MB_OK);
    }
}
#ifndef PLAYER_H
#define PLAYER_H

#include "blocks.h"

enum NESWDirection {N = 0, E = 1, S = 2, W = 3};

struct Direction
{
    NESWDirection V;
    NESWDirection H;
};

class C_Player
{
    public:
        C_Player();

        int  Get_PlayerLife() { return Player_Life; }
        void Set_PlayerLife(int i) { Player_Life = i; }
        void Subtract_PlaterLife() { Player_Life--; }


        void Set_PlankSize(int);
        int  Get_PlankSize();
        void Set_PlankInfo(RECT);
        RECT* Get_PlankInfo() { return &Plank_Info; }
        void Set_PlankSpeed(int);
        int  Get_PlankSpeed();

        void Set_BallSize(int);
        int  Get_BallSize();
        void Set_BallSpeed(int);
        int  Get_BallSpeed();
        void Set_BallInfo(RECT);
        RECT Get_BallInfo() { return Ball_Info; }
        void LaunchBall() { Ball_Status = 1; }

        void Set_Mouse_Pos(POINT); // defined
        void Set_Mouse_Pos(); //defined
        POINT* Get_MousePos_ptr() { return &Mouse; }

        void Ball_Movement();
        void Plank_Movement();
        void Collision(C_Brick*);

        void DebugDisplay();
    private:
        int Player_Life;

        int Plank_Size;
        int Plank_Speed;
        RECT Plank_Info;

        int Ball_Size;
        int Ball_Speed;
        Direction Ball_Dir; //direction
        RECT Ball_Info; //size, coordinate
        bool Ball_Status; //0: stuck to plank, 1: moving

        POINT Mouse;
};

#endif
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.