Need help with directDraw code (c++, windows)

Started by
2 comments, last by kingpinzs 19 years, 5 months ago
I am trying to learn direct draw to get started. I know it is out dated and you can do tha same thing with direct 3d faster and better. But I have 4 books on using direct draw but I cant get it to work here is the code

#define WIN32_LEAN_AND_MEAN
#define VC_LEANMEAN
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include "ddutil.h"
#define WINDOWCLASS "Basic Window"
#define WINDOWTITLE "Game Window"
LPDIRECTDRAW7 g_pdd;//The direct draw object pointer to direct draw object
LPDIRECTDRAWSURFACE7 g_pddsprimary;//the primary surface
LPDIRECTDRAWSURFACE7 g_pddsback;   //the back buffer
LPDIRECTDRAWSURFACE7 g_pddsone;    //a temporary surface also using for 
bitmap
DDSURFACEDESC2 ddsd;               //surface directdraw description
//DDSCAPS2 ddsc;                     //
HWND hwnd;
HRESULT hRet;                     //
int Game_Int();
int  Game_Main();
int Game_Shutdown();
LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM 
lParam)
{
    switch(uMsg)
    {
    case WM_KEYDOWN:
        {
            //check for escape key
            if(wParam==VK_ESCAPE)
            {
                DestroyWindow(hwnd);
                return(0);//handled message
            }
        }break;
    case WM_DESTROY://the window is being destroyed
        {
                      PostQuitMessage(0);
                       return(0);
        }break;
    case WM_SETCURSOR:
        {
            SetCursor(NULL);
            return(0);
        }
       }
       return(DefWindowProc(hwnd,uMsg,wParam,lParam));
}
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
         MSG msg;
        WNDCLASSEX wcx;
       wcx.cbSize=sizeof(WNDCLASSEX);
        wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
       wcx.lpfnWndProc=TheWindowProc;
        wcx.cbClsExtra=0;
       wcx.cbWndExtra=0;
        wcx.hInstance=hInstance;
        wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
        wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
        wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
       wcx.lpszMenuName=NULL;
     wcx.lpszClassName=WINDOWCLASS;
    wcx.hIconSm=NULL;
       if(!RegisterClassEx(&wcx)) return(0);
   hwnd =CreateWindowEx(0,
                            WINDOWCLASS,
	                    WINDOWTITLE,
	                    WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,
	                    0,0,300,200,
	                    NULL,
	                    NULL,
	                    (HINSTANCE) hInstance,
	                    NULL);
    if(!hInstance) return(0);
   ShowWindow(hwnd, nShowCmd);
   UpdateWindow (hwnd);
   Game_Int();
     for(;;)
    {
               if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
               if(msg.message==WM_QUIT) break;
                    TranslateMessage(&msg);
		         DispatchMessage(&msg);
        }
                Game_Main();
    }
        Game_Shutdown();
     return(msg.wParam);
}
int Game_Int()
{
    hRet = DirectDrawCreateEx(NULL, (void**)&g_pdd,IID_IDirectDraw7, NULL);
   if (hRet != DD_OK)
    MessageBox(hwnd,"DirectDrawCreateEX Failed","Error",NULL);
    hRet = g_pdd->SetCooperativeLevel(hwnd,DDSCL_FULLSCREEN | 
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
    if (hRet != DD_OK)
    MessageBox(hwnd,"SetCooperativelevel failed","Error",NULL);
    hRet = g_pdd ->SetDisplayMode(640, 480,16,0,0);
    if (hRet != DD_OK)
    MessageBox(hwnd,"SetDisplayMode failed","Error",NULL);
    ZeroMemory(&ddsd,sizeof(DDSURFACEDESC2));
    ddsd.dwSize = sizeof(DDSURFACEDESC2);
    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP |
DDSCAPS_COMPLEX;
    ddsd.dwBackBufferCount = 2;
    hRet = g_pdd->CreateSurface(&ddsd, &g_pddsprimary, NULL);
     if (hRet != DD_OK)
    MessageBox(hwnd,"CreateSurface failed","Error",NULL);
    ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
    hRet = g_pddsprimary->GetAttachedSurface(&ddsd.ddsCaps, &g_pddsback);
g_pddsone = DDLoadBitmap(g_pdd, "test.bmp",0,0);
return (1);
}
int  Game_Main()
{
if(g_pddsprimary == NULL)
  return(0);
    RECT r;
      r.left = 0 ;
      r.top =0;
      r.right = 640;
      r.left = 480;
      g_pddsback->BltFast(0,0,g_pddsone, 
&r,DDBLTFAST_NOCOLORKEY|DDBLTFAST_WAIT);
g_pddsprimary->Flip(NULL, DDFLIP_WAIT);
return(1);
}
int Game_Shutdown()
{
    g_pddsone->Release();
    g_pddsone = NULL;
    g_pddsprimary->Release();
    g_pddsprimary =NULL;
    g_pdd->Release();
    g_pdd=NULL;
return (1);
}

the problem is that it does not display my bitmap but does give me a bunch of garbge. Can any one help? [Edited by - kingpinzs on November 11, 2004 11:16:37 AM]
Advertisement
Quote:Original post by kingpinzs
the problem is that it does not display my bitmap but does give me a bunch of garbge. Can any one help?


well this line looks a little suspicious:

ddsd.dwBackBufferCount = 2;


Are you trying to do tripple buffering or something?

please tidy the code up a little, it makes it easier for us to stop problems.
      r.left = 0 ;      r.top =0;      r.right = 640;      r.left = 480;


You've specified r.left twice. The second one should have been r.bottom.
that was it thanks for caching that I have been working on this issue for 3 days and it was some little stupid mistake

thanks for the help

This topic is closed to new replies.

Advertisement