Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

GDI programming in C++

  • 08-12-2006 5:44pm
    #1
    Registered Users, Registered Users 2 Posts: 1,252 ✭✭✭


    Hi there i have to make a breakout game for college but i cannot get how to make the region to rebound against object or how the make the paddle stop leaving the screen (it breaking into the region). Also, i need to know how i would make the paddle follow the mouse, as in the sprite is already selected to the mouse but that it will also follow it....im getting confused by this little thing...


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil




  • Registered Users, Registered Users 2 Posts: 1,252 ✭✭✭Africa


    Ok, here is the code. Its for a GDI project we have to do, so i have just posted up some parts. Yes it is still very basic with basically drawing some bmps', making rectangles and the ellipse for the ball. The paddle moves but i dont know how to make is stop when it gets to 0 on the X axis.:

    HINSTANCE hInstMain=NULL;//main application handle
    HWND hWndMain=NULL;//handle to our main window
    HBITMAP hBtmpBar; // hBtmp is a handle to a bitmap
    HBITMAP hBtmpBarMask; // hBtmp is a handle to a bitmap

    HBITMAP hBtmpBackground; // hBtmp is a handle to a bitmap



    SIZE szBackground,szBar;
    POINT ptBackground,ptBar;

    enum current_sprite {none, Bar, Ball};

    current_sprite sprite_selected;
    //OBJECT paddle,ball,brick;



    //////////////////////////////////////////////////////////////////////////////
    //WINDOWPROC
    //////////////////////////////////////////////////////////////////////////////
    LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    //which message did we get?
    switch(uMsg)
    {
    case WM_DESTROY://the window is being destroyed
    {

    //tell the application we are quitting
    PostQuitMessage(0);

    //handled message, so return 0
    return(0);

    }break;
    case WM_PAINT://the window needs repainting
    {
    //a variable needed for painting information
    PAINTSTRUCT ps;
    //start painting
    HDC hdc=BeginPaint(hwnd,&ps);

    /////////////////////////////
    //painting code would go here
    /////////////////////////////
    SelectObject(hdc,GetStockObject(NULL_BRUSH));
    HPEN pen=CreatePen(PS_SOLID,1,RGB(250,250,0));

    SelectObject(hdc,pen);
    ShowBitmap(hdc,hBtmpBackground,0,0); //display the bitmap
    pen=CreatePen(PS_SOLID,7,RGB(255,255,0));

    // Ball=Ellipse(hdc,(((ptBar.x+szBar.cx)-ptBar.x)/2)+ptBar.x-5,(((ptBar.y-szBar.cy)-ptBar.y)/2)+ptBar.y-10,(((ptBar.x+szBar.cx)-ptBar.x)/2)+ptBar.x+5,(((ptBar.y)-ptBar.y)/2)+ptBar.y-5);


    pen=CreatePen(PS_SOLID,5,RGB(255,0,0));

    ShowBitmap(hdc,hBtmpBarMask,ptBar.x, ptBar.y, SRCAND); //display the bitmap
    ShowBitmap(hdc,hBtmpBar,ptBar.x, ptBar.y,SRCPAINT); //display the bitmap
    Rectangle(hdc,ptBar.x,ptBar.y,ptBar.x+szBar.cx,ptBar.y+szBar.cy);

    if(sprite_selected==Ball){
    Rectangle(hdc,ptBar.x, ptBar.y,ptBar.x+szBar.cx,ptBar.y+szBar.cy);
    }
    if(ptBar.x==50)
    sprite_selected=none;

    double rectangle[8];
    static int a,b,c,d;
    a=50;
    b=50;
    c=100;
    d=60;
    for(int i=0;i<8;i++)
    {
    Rectangle(hdc,a,b,c,d);
    a+50;
    c+50;
    }

    //end painting
    EndPaint(hwnd,&ps);
    return(0);
    }break;


    case WM_CHAR:
    {
    switch(wParam){
    case VK_SPACE:
    {
    sprite_selected=Ball;
    }break;
    case VK_ESCAPE:
    {
    sprite_selected=none;
    }break;

    }
    InvalidateRect(hWndMain,NULL,false);
    }break;


    case WM_KEYDOWN:
    {
    POINT* pt;

    if (sprite_selected==none){
    break;
    }
    switch (sprite_selected){
    case Bar:
    {
    pt=&ptBar;
    }break;

    }
    switch(wParam){
    case VK_LEFT:
    {
    // if(paddle
    for(int i=0;i<30;i++)
    pt->x--;

    }break;
    case VK_RIGHT:
    {
    for(int i=0;i<30;i++)
    pt->x++;
    }break;

    }


    InvalidateRect(hWndMain,NULL,false);
    }break;
    }

    //pass along any other message to default message handler
    return(DefWindowProc(hwnd,uMsg,wParam,lParam));
    }


    SKIP TO INITIALISATION


    //////////////////////////////////////////////////////////////////////////////
    //INITIALIZATION
    //////////////////////////////////////////////////////////////////////////////
    bool Prog_Init()
    {
    ////////////////////////////////////
    //your initialization code goes here
    ////////////////////////////////////
    BITMAP bm;
    //background
    hBtmpBackground=(HBITMAP)LoadImage(
    NULL, // dont worry, leave as null
    "background.bmp", // name of the file to be loaded
    IMAGE_BITMAP, // load a bitmap as opposed to a cursor or icon
    0, // leave as 0
    0, // leave as 0
    LR_LOADFROMFILE); // load from file as opposed to a resource


    //Ball.x=0,Ball.y=0;

    //bar


    hBtmpBar=(HBITMAP)LoadImage(
    NULL, // dont worry, leave as null
    "Bar.bmp", // name of the file to be loaded
    IMAGE_BITMAP, // load a bitmap as opposed to a cursor or icon
    0, // leave as 0
    0, // leave as 0
    LR_LOADFROMFILE); // load from file as opposed to a resource


    //bar mask
    hBtmpBarMask = CreateBitmapMask(hBtmpBar,RGB(0,255,0));

    ptBar.x=380;
    ptBar.y=550;

    GetObject(hBtmpBar,sizeof(bm), &bm);
    szBar.cx=bm.bmWidth;
    szBar.cy=bm.bmHeight;

    /*
    //brick1

    hBtmpLisa=(HBITMAP)LoadImage(
    NULL, // dont worry, leave as null
    "lisa.bmp", // name of the file to be loaded
    IMAGE_BITMAP, // load a bitmap as opposed to a cursor or icon
    0, // leave as 0
    0, // leave as 0
    LR_LOADFROMFILE); // load from file as opposed to a resource
    //brick1 mask


    // SelectObject(hdc,GetStockObject(NULL_BRUSH));

    hBtmpLisaMask = CreateBitmapMask(hBtmpLisa,RGB(0,0,0));

    ptLisa.x=500;
    ptLisa.y=200;

    GetObject(hBtmpLisa,sizeof(bm), &bm);
    szLisa.cx=bm.bmWidth;
    szLisa.cy=bm.bmHeight;


    //brick2


    hBtmpMag=(HBITMAP)LoadImage(
    NULL, // dont worry, leave as null
    "mag.bmp", // name of the file to be loaded
    IMAGE_BITMAP, // load a bitmap as opposed to a cursor or icon
    0, // leave as 0
    0, // leave as 0
    LR_LOADFROMFILE); // load from file as opposed to a resource
    //brickmask2


    // SelectObject(hdc,GetStockObject(NULL_BRUSH));
    hBtmpMagMask = CreateBitmapMask(hBtmpMag,RGB(0,0,0));

    ptMag.x=100;
    ptMag.y=200;

    GetObject(hBtmpMag,sizeof(bm), &bm);
    szMag.cx=bm.bmWidth;
    szMag.cy=bm.bmHeight;

    sprite_selected=none;





    */



    return(true);//return success
    }


Advertisement