Skip to main content
gamedev.net
🔒 Locked

DirectDraw Problem HELP!

Started by julienX Feb 21, 2003 at 10:04 AM 7 replies 250+ views
Original Post
julienX
julienX
I''ve been using directdraw for a couple of weeks now but i have this problem that ALWAYS seems to come back no matter what. Whats happens is my app goes into fullscreen mode (like its supposed to) but then it exits immediatly. I come accross this problem very often and its starting to annoy me how I cannot find the solution. I''ve been writing a simple ddraw app at the moment and it has come up again! I''ve checked over for long enought now, so please help me find what the heck is closing my program. I even took out all the error code exits and it still does it! Ok here is my Game_Init and Game_Main. (all im trying to do is create a black screen with moving pixels)
  

//Main game initialisation

void Game_Init()
{
    //Main ddraw object

    DirectDrawCreateEx(NULL,(void**)&lpdd,IID_IDirectDraw7,NULL);
    
    //Cooperation with windows

    lpdd->SetCooperativeLevel(gHwnd,DDSCL_FULLSCREEN |    DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
    
    //Display mode

    lpdd->SetDisplayMode(640,480,8,0,0);
    
    //Set surface description for primary surface

    //First clear memory then set size

    memset(&ddsd,0,sizeof(DDSURFACEDESC2));
    ddsd.dwSize = sizeof(DDSURFACEDESC2);
    
    //Set to primary surface with one back buffer

    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.dwBackBufferCount = 1;
    
    //Set to complex, flippable for page-flipping

    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
    
    //Create both surfaces

    lpdd->CreateSurface(&ddsd,&lpddsPrimary,NULL);
    
    //Attach backbuffer to primary surface

    ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
    lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsBack);
    
    //Init star positions at random

    for (int i=0; i<NUM_STARS; i++)
    {
        stars[i].x = rand()%SCREEN_WIDTH;
        stars[i].y = rand()%SCREEN_HEIGHT;
    }
    
} //End Game_Init




//Main game loop

void Game_Main()
{
    DDBLTFX ddbltfx;
    DD_INIT_STRUCT(ddbltfx);
    ddbltfx.dwFillColor = 0; //Set color to black


    //Check for escape key

    if (KEYDOWN(VK_ESCAPE))
    SendMessage(gHwnd,WM_DESTROY,0,0);
    
    //Clear back buffer

    lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
    
    //Move stars

    for (int i=0; i<NUM_STARS; i++)
    stars[i].x += 2;
    
    //Lock backbuffer

    lpddsBack->Lock(NULL,NULL,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
    
    //Create alias to back buffer

    UCHAR *back_buffer = (UCHAR*)ddsd.lpSurface;
    
    //Plot stars

    for (int i=0; i<NUM_STARS; i++)
    {
        int color = 255;
        back_buffer[stars[i].x + stars[i].y*ddsd.lPitch] = color;
    }
    
    //Unlock backbuffer

    lpddsBack->Unlock(NULL);
    
    //Flip surfaces -remember that this in only 

    //Possible when all surfaces are UNLOCKED

    while (FAILED(lpddsPrimary->Flip(NULL,DDFLIP_WAIT)));
    
    Sleep(100);
    
} //End Game_Main


  
Remember my problem is that the program exits straight away, but i haven''t a single error check in my whole code! Thanks for your precious time
chacha
Dave Hunt
Dave Hunt
Have you cleared the array that holds the key states? If not, then the first time through your game loop, your KEYDOWN(VK_ESACAPE) could be true...

The simplest way to find this kind of problem is to add some debugging statements and then you can see where it exits.
MattS423
MattS423
can we see the WinMain()?
Programmers of the world, UNTIE!
julienX
julienX
Thanks for the advice but ive checked what you said already but i still have this stupid problem.

This is driving me nuts!!
chacha
julienX
julienX
Here is the WinMain:


  

// WINMAIN /////////////////////////////////////////


int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,
LPSTR lpcmdline,int ncmdshow)
{
WNDCLASSEX winclass; //Main window structure

HWND hwnd; //Main window handle

MSG msg; //Windows messages

HDC hdc; //Device context


//Fill out window data structure

winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.hInstance = hinstance;
winclass.lpfnWndProc = WinProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.lpszClassName = WIN_CLASS_NAME;
winclass.lpszMenuName = NULL;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

//Register class with windows

if (!RegisterClassEx(&winclass))
{
MessageBox(NULL,"Failed to register class","Error",MB_OK);
return (0);
}

//Create window

hwnd = CreateWindowEx(NULL,
WIN_CLASS_NAME,
"Ddraw demo",
WS_POPUP | WS_VISIBLE,
0,0, //x and y position

640,
480,
NULL,
NULL,
hinstance,
NULL);
if (!hwnd)
{
MessageBox(NULL,"Failed to create window","Error",MB_OK);
return (0);
}

//Save globals

gHwnd = hwnd;
gHinstance = hinstance;

//Init game

Game_Init();

//Message loop

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

TranslateMessage(&msg);
DispatchMessage(&msg);
}

//Main game processing

Game_Main();

} //End while


//Shutdown game

Game_Shutdown();

//Return to windows

return (msg.wParam);

} //End WinMain


//////////////////////////////////////////////////


chacha
julienX
julienX
Well ill just give you all the code to look at:

(this includes all the stuff from above aswell)


  

//Clipper.cpp Using a ddraw clipper demo


// INCLUDES ////////////////////////////////////


#define WIN32_LEAN_AND_MEAN //Say no to MFC


#define INITGUID //Make sure directx guids are included


#include <windows.h> //For windows stuff
#include <windowsx.h>
#include <mmsystem.h>


#include <iostream.h> //For C/C++ stuff
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <time.h>


#include <ddraw.h> //For DirectDraw


// DEFINES //////////////////////////////////////


#define WIN_CLASS_NAME "WINCLASS1" //Class name


#define SCREEN_WIDTH 640 //Window dimensions
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 8 //Bits per pixel


#define NUM_STARS 1000 //Number of stars in starfield


// MACROS ///////////////////////////////////////


// initializes a direct draw struct

#define DD_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }

// these read the keyboard asynchronously

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ?1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// TYPES ////////////////////////////////////////


struct STARS
{
int x,y;
};

// GLOBALS //////////////////////////////////////


HWND gHwnd = NULL; //Globally track hwnd and hinstance

HINSTANCE gHinstance = NULL;
STARS stars[NUM_STARS]; //Holds the stars


LPDIRECTDRAW7 lpdd = NULL;
LPDIRECTDRAWSURFACE7 lpddsPrimary = NULL;
LPDIRECTDRAWSURFACE7 lpddsBack = NULL;
LPDIRECTDRAWCLIPPER lpddClipper = NULL;
DDSURFACEDESC2 ddsd;

// PROTOTYPES ///////////////////////////////////


void Game_Init();
void Game_Main();
void Game_Shutdown();

// WINDOW PROCEDURE /////////////////////////////


LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC paint_hdc;

switch (msg)
{
case WM_CREATE:
{
return (0);
} break;

case WM_DESTROY:
{
PostQuitMessage(0);
return (0);
} break;

case WM_PAINT:
{
paint_hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return (0);
} break;

default: break;
} //End switch


//Take care of any messages I didn''t handle

return (DefWindowProc(hwnd,msg,wParam,lParam));

} //End WinProc


// WINMAIN /////////////////////////////////////////


int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,
LPSTR lpcmdline,int ncmdshow)
{
WNDCLASSEX winclass; //Main window structure

HWND hwnd; //Main window handle

MSG msg; //Windows messages

HDC hdc; //Device context


//Fill out window data structure

winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.hInstance = hinstance;
winclass.lpfnWndProc = WinProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.lpszClassName = WIN_CLASS_NAME;
winclass.lpszMenuName = NULL;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

//Register class with windows

if (!RegisterClassEx(&winclass))
{
MessageBox(NULL,"Failed to register class","Error",MB_OK);
return (0);
}

//Create window

hwnd = CreateWindowEx(NULL,
WIN_CLASS_NAME,
"",
WS_POPUP | WS_VISIBLE,
0,0, //x and y position

640,
480,
NULL,
NULL,
hinstance,
NULL);
if (!hwnd)
{
MessageBox(NULL,"
Failed to create window","Error",MB_OK);
return (0);
}

//Save globals

gHwnd = hwnd;
gHinstance = hinstance;

//Init game

Game_Init();

//Message loop

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

TranslateMessage(&msg);
DispatchMessage(&msg);
}

//Main game processing

Game_Main();

} //End while


//Shutdown game

Game_Shutdown();

//Return to windows

return (msg.wParam);

} //End WinMain


//////////////////////////////////////////////////


//Main game initialisation

void Game_Init()
{
//Main ddraw object

DirectDrawCreateEx(NULL,(void**)&lpdd,IID_IDirectDraw7,NULL);

//Cooperation with windows

lpdd->SetCooperativeLevel(gHwnd,DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);

//Display mode

lpdd->SetDisplayMode(640,480,8,0,0);

//Set surface description for primary surface

//First clear memory then set size

memset(&ddsd,0,sizeof(DDSURFACEDESC2));
ddsd.dwSize = sizeof(DDSURFACEDESC2);

//Set to primary surface with one back buffer

ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;

//Set to complex, flippable for page-flipping

ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;

//Create both surfaces

lpdd->CreateSurface(&ddsd,&lpddsPrimary,NULL);

//Attach backbuffer to primary surface

ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsBack);

//Init star positions at random

for (int i=0; i<NUM_STARS; i++)
{
stars[i].x = rand()%SCREEN_WIDTH;
stars[i].y = rand()%SCREEN_HEIGHT;
}

} //End Game_Init



//Main game loop

void Game_Main()
{
DDBLTFX ddbltfx;
DD_INIT_STRUCT(ddbltfx);
ddbltfx.dwFillColor = 0; //Set color to black



//Clear back buffer

lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);

//Move stars

for (int i=0; i<NUM_STARS; i++)
stars[i].x += 2;

//Lock backbuffer

lpddsBack->Lock(NULL,NULL,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

//Create alias to back buffer

UCHAR *back_buffer = (UCHAR*)ddsd.lpSurface;

//Plot stars

for (int i=0; i<NUM_STARS; i++)
{
int color = 255;
back_buffer[stars[i].x + stars[i].y*ddsd.lPitch] = color;
}

//Unlock backbuffer

lpddsBack->Unlock(NULL);

//Flip surfaces -remember that this in only

//Possible when all surfaces are UNLOCKED

while (FAILED(lpddsPrimary->Flip(NULL,DDFLIP_WAIT)));

Sleep(100);

} //End Game_Main



//Game cleanup

void Game_Shutdown()
{
if (lpddsBack)
{
lpddsBack->Release();
lpddsBack = NULL;
}

if (lpddsPrimary)
{
lpddsPrimary->Release();
lpddsPrimary = NULL;
}

if (lpdd)
{
lpdd->Release();
lpdd = NULL;
}

} //End Game_Shutdown






And i get no compiler errors. Just that the program exits immediatly.
Thanks for your time!
chacha
julienX
julienX
What the heck! My code doesn''t look like that!
How come when I put my code in the source box, it changes the layout completely?

Doesn''t matter, at least you can read it (hopefully!)

Thanks again
chacha
Dave Hunt
Dave Hunt
Have you tried adding debug statements to see what''s happening? You could have your debug statements write to a log after each step and that should tell you exactly what is going on.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.