Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sakky

DDraw Blt and more

This topic is 6131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can some on help me please? I''m trying to get DDraw Blit my grphics. Its working but I want to know if there is something wrong with my source. I know when I increment the X and Y positions more then 1, little traces of the picture are being left. Is this because I''m not clearing the buffers properly. I''ll show you my compleate source code.
  
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <ddraw.h>

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

    LPDIRECTDRAW lpdd;
    LPDIRECTDRAWSURFACE lpPrimary;
    LPDIRECTDRAWSURFACE lpBack;
    LPDIRECTDRAWSURFACE lpOff;
    DDSURFACEDESC ddsd;
    DDSCAPS ddscaps;
    HRESULT ddrval;
    int Xpos = GetSystemMetrics(SM_CXSCREEN) / 2;
    int Ypos = GetSystemMetrics(SM_CYSCREEN) / 2;

    int DDInit();
    void DDLoad();
    void DDKill();

    HWND hwnd;
    char szClassName[] = "DXApp";

int WINAPI WinMain(HINSTANCE hInst,
                   HINSTANCE hPrev,
                   LPSTR nCmdLine,
                   int ncmdShow)
{
    MSG msg;
    WNDCLASS wc;

         memset(&wc,0,sizeof(wc));
    wc.hInstance     = hInst;
    wc.style         = CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
    wc.hIcon         = LoadIcon(NULL,IDI_WINLOGO);
    wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.lpszClassName = szClassName;
    wc.lpszMenuName  = NULL;
    wc.lpfnWndProc   = WndProc;

    if(RegisterClass(&wc))
    {
         hwnd = CreateWindow(szClassName,
                             NULL,
                             WS_POPUP,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             0,
                             0,
                             hInst,
                             0);
    } else return 0;

    ddrval = DDInit();

    if(FAILED(ddrval))
    {
         MessageBox(NULL,"Failed to Initialize DirectX","Error!",MB_OK);
         return 0;
    }

    DDLoad();

    ShowWindow(hwnd,SW_SHOW);
    UpdateWindow(hwnd);

    while(1)

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

              TranslateMessage(&msg);
              DispatchMessage(&msg);
         }
         else
         {
         // Do Game Processing here

         lpPrimary-> BltFast(Xpos,Ypos,lpOff,NULL,DDBLTFAST_WAIT);


         lpBack-> Flip(NULL,DDFLIP_WAIT);
         }
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
         {
              case WM_DESTROY:
                   DDKill();
                   PostQuitMessage(0);
                   break;

              case WM_KEYDOWN:
                   {
                   int vKeyCode = (int)wParam;
                   switch(vKeyCode)
                        {
                        case VK_ESCAPE:
                             DDKill();
                             PostQuitMessage(0);
                             break;

                        case VK_LEFT:
                             break;

                        case VK_RIGHT:
                             break;

                        case VK_UP:
                             if(Ypos > 0)
                             {
                                  Ypos -= 1;
                             } break;

                        case VK_DOWN:
                             if(Ypos < 445)
                             {
                                  Ypos += 1;
                             } break;
                        }
                   return 0;
                   } break;

              default: break;
         }
    return DefWindowProc(hwnd,msg,wParam,lParam);
}

int DDInit()
{
    ddrval = DirectDrawCreate(NULL,&lpdd,NULL);

    if(FAILED(ddrval))
    {
         MessageBox(NULL,"DirectDrawCreate Failed","Error!",MB_OK);
         return ddrval;
    }

    ddrval = lpdd-> SetCooperativeLevel(hwnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);

    if(FAILED(ddrval))
    {
         MessageBox(NULL,"SetCooperativeLevel Failed","Error!",MB_OK);
         lpdd-> Release();
         return ddrval;
    }

    ddrval = lpdd-> SetDisplayMode(640,480,8);

    if(FAILED(ddrval))
    {
         MessageBox(NULL,"SetDisplayMode Failed","Error!",MB_OK);
         lpdd-> Release();
         return ddrval;
    }

              memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize            = sizeof(ddsd);
    ddsd.dwFlags           = DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps    = DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
    ddsd.dwBackBufferCount = 1;

    ddrval = lpdd-> CreateSurface(&ddsd,&lpPrimary,NULL);

    if(FAILED(ddrval))
    {
         MessageBox(NULL,"CreateSurface Failed","Error!",MB_OK);
         lpPrimary-> Release();
         lpdd-> Release();
         return ddrval;
    }

    ddscaps.dwCaps = DDSCAPS_BACKBUFFER;

    ddrval = lpPrimary-> GetAttachedSurface(&ddscaps,&lpBack);

    if(FAILED(ddrval))
    {
         MessageBox(NULL,"GetAttachedSurface Failed","Error!",MB_OK);
         lpBack-> Release();
         lpPrimary-> Release();
         lpdd-> Release();
         return ddrval;
    }

           memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize         = sizeof(ddsd);
    ddsd.dwFlags        = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
    ddsd.dwWidth        = 32;
    ddsd.dwHeight       = 32;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;

    ddrval = lpdd-> CreateSurface(&ddsd,&lpOff,NULL);

    if(FAILED(ddrval))
    {
         MessageBox(NULL,"Couldn''t create Offscreen surface","Error!",MB_OK);
         lpOff-> Release();
         lpBack-> Release();
         lpPrimary-> Release();
         lpdd-> Release();
         return ddrval;
    }

    return ddrval;
}

void DDLoad()
{
    HDC hDC;
    HDC ImageDC;
    HBITMAP hBitmap;
    HBITMAP oldBMP;

    ddrval  = lpOff->GetDC(&hDC);

    hBitmap = (HBITMAP)LoadImage(NULL,"ship.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

    ImageDC = CreateCompatibleDC(hDC);

    oldBMP  = (HBITMAP)SelectObject(ImageDC,hBitmap);

    ddrval  = BitBlt(hDC,0,0,32,32,ImageDC,0,0,SRCCOPY);

    DeleteObject(oldBMP);

    DeleteDC(ImageDC);

    DeleteObject(hBitmap);

    lpOff->ReleaseDC(hDC);
}

void DDKill()
{
    lpOff-> Release();
    lpBack-> Release();
    lpPrimary-> Release();
    lpdd-> Release();
}
  
Advise or tips on this please!!!! Thanx in advance

Share this post


Link to post
Share on other sites
Advertisement
When you blit to a primary or backbuffer you should clear the surface before hand.

  
// Do Game Processing here

lpPrimary-> BltFast Xpos,Ypos,lpOff,NULL,DDBLTFAST_WAIT);
lpBack-> Flip(NULL,DDFLIP_WAIT);


Surely if you have defined a Primary surface with a back buffer you should be blitting to the backbuffer and flipping the primary not the other way around.

I forget the syntax to fill (black) or clear a surface, but it should be in the help files.

,Jay

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!