Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectDraw.... problem

Started by MeoDea Jan 20, 2003 at 10:50 PM 7 replies 400+ views
Original Post
MeoDea
MeoDea
Ok, I am a little new to direct draw... and am useing the dx7sdk for my project, and Microsoft VC++ 6.0. My code compiles, and then build all fine and dandy, but when ran it makes an illegal opperation. I don''t understand this... if i knew how to post large blocks of text without the huge, laggy post, i would...
Miserable
Miserable
I obviously cannot help you with your code without seeing any of it (and very possibly not even then), but as the FAQ clearly states, you should post code using the [ source ] and [ /source ] tags (without the spaces, of course).
RaphaelMun
RaphaelMun
It''s very likely that you''re trying to access a pointer to null. Maybe the dx7device or the dx7 interface itself hasn''t been created correctly but you still use it (not very likely). Or maybe it''s that you call a function with dx7device to get a surface pointer or something to it but it failed and you didn''t check for error returns... who knows, good luck!
MeoDea
MeoDea

  /*Windows Initialization by Carter Sonnenberg
01001100110010101101111010001000110010101100001*/


#include <stdlib.h>
#include <iostream.h>
#include <windows.h>
#include <ddraw.h>

//test if key is hit

#define KEYHIT(key) ((GetAsyncKeyState(key) & 0x8000) ? TRUE : FALSE)

//zero mem. of dx struct

#define ZERO_STRUCT(struct) {ZeroMemory(&struct,sizeof(struct));struct.dwSize=sizeof(struct);}

//window globals

HINSTANCE hinstance;
HWND hwindow;
char CLASSNAME[10] = "Window";
char CLASSNAMEb[10] = "Window";

//direct draw globals

LPDIRECTDRAW7 dd7 = NULL;
LPDIRECTDRAWSURFACE7 primary = NULL;
LPDIRECTDRAWSURFACE7 back = NULL;
LPDIRECTDRAWSURFACE7 tileset = NULL;
LPDIRECTDRAWSURFACE7 characters = NULL;
LPDIRECTDRAWCLIPPER clipper = NULL;
DDSURFACEDESC2 surface;

//prototype funcs

int INIT_DD_MAIN(void);
void DEINIT_DD_MAIN(void);

//prog starts here

int APIENTRY WinMain(HINSTANCE hinstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = CLASSNAME;
winclass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

if (!RegisterClassEx(&winclass))
{return(0);}

if (!(hwnd = CreateWindowEx(NULL,
CLASSNAME,
CLASSNAMEb,
WS_POPUP | WS_VISIBLE,
0, 0,
640, 480,
NULL,
NULL,
hinstance,
NULL))){
return(0);}

//Get time (used for frame rate)

DWORD dwStart = GetTickCount();

if(INIT_DD_MAIN()){

while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
if (msg.message == WM_QUIT){
break;}
TranslateMessage(&msg);
DispatchMessage(&msg);}
if (KEYHIT(VK_ESCAPE)){
PostQuitMessage(0);}

//computing crap here



// Set to 30 fps (33ms per frame)

while ((GetTickCount() - dwStart) < 33){};
}
}

else {return(0);}
DEINIT_DD_MAIN();
return(msg.wParam);
}


int INIT_DD_MAIN(void){
//create the directdraw7 interface

if(FAILED(DirectDrawCreateEx(NULL, (void**)&dd7, IID_IDirectDraw7, NULL)))
{return(FALSE);}

//set cooperative level to fullscreen

if(FAILED(dd7->SetCooperativeLevel(hwindow, DDSCL_FULLSCREEN |
DDSCL_EXCLUSIVE |
DDSCL_ALLOWREBOOT)))
{return(FALSE);}

//hide cursor

ShowCursor(FALSE);

//set 640x480x16 resolution

if(FAILED(dd7->SetDisplayMode(640, 480, 16, 0, 0)))
{return(FALSE);}

//create our primary surface with one backbuffer

ZERO_STRUCT(surface);
surface.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
surface.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_COMPLEX |
DDSCAPS_FLIP;
surface.dwBackBufferCount = 1;
if(FAILED(dd7->CreateSurface(&surface, &primary, NULL)))
{return(FALSE);}

//get the backbuffer

surface.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if(FAILED(primary->GetAttachedSurface(&surface.ddsCaps, &back)))
{return(FALSE);}

//create the clipper

if(FAILED(dd7->CreateClipper(0, &clipper, NULL)))
{return(FALSE);}

//clip list stuff

LPRGNDATA lpClipList = (LPRGNDATA)malloc(sizeof(RGNDATAHEADER) +
sizeof(RECT));
RECT rcBoundary = {0, 0, 640, 480};
memcpy(lpClipList->Buffer, &rcBoundary, sizeof(RECT)); // set list

lpClipList->rdh.dwSize = sizeof(RGNDATAHEADER); // structure size

lpClipList->rdh.iType = RDH_RECTANGLES; // bound type

lpClipList->rdh.nCount = 1; // num of bounds

lpClipList->rdh.nRgnSize = sizeof(RECT); // size of Buffer

lpClipList->rdh.rcBound = rcBoundary; // bounding RECT

if (FAILED(clipper->SetClipList(lpClipList, 0)))
{
//still free malloc if failed

free(lpClipList);
return(FALSE);
}

// attach clipper to back buffer

if(FAILED(back->SetClipper(clipper)))
{
//still free malloc if failed

free(lpClipList);
return(FALSE);
}

// free the memory

free(lpClipList);

//create surface for tileset

//size augmented for tileset demensions

ZERO_STRUCT(surface);
surface.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT;
surface.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
surface.dwWidth = 1;
surface.dwHeight = 1;
surface.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
surface.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
if(FAILED(dd7->CreateSurface(&surface, &tileset, NULL)))
{return(FALSE);}

//create surface for characters

//size augmented for character demensions

ZERO_STRUCT(surface);
surface.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT;
surface.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
surface.dwWidth = 1;
surface.dwHeight = 1;
surface.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
surface.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
if(FAILED(dd7->CreateSurface(&surface, &characters, NULL)))
{return(FALSE);}

return(TRUE);
};

void DEINIT_DD_MAIN(void){
if (characters){
characters->Release();
characters = NULL;}
if (tileset){
tileset->Release();
tileset = NULL;}
if (clipper){
clipper->Release();
clipper = NULL;}
if (back){
back->Release();
back = NULL;}
if (primary){
primary->Release();
primary = NULL;}
if (dd7){
dd7->Release();
dd7 = NULL;}
CloseWindow(hwindow);
};


I am hopeing this works, as the FAQ i read had no info on this...

[edited by - MeoDea on January 20, 2003 12:40:19 AM]
MeoDea
MeoDea
Ok, i have tryed everything i can think of, and i still can''t this code to run properly...
JonW
JonW
Works everytime:

1.) Compile the program in debug mode (NOT release! Never compile in release until you are ready to distribute the program).

2.) Go to Tools | Start Debug | Go.

3.) MSVC++ should tell you the line that the exception occurs on. Either try to fix it yourself or post it here.

One tip, though, is that you should never try to compile and test a huge amount of code like this. Add it in section by section, trying the program every time you reach a point where it will compile. Then you can know what section is causing the error.

For example, you could do the following, testing in between each step:

1.) Create a plain ol'' window that doesn''t do anything
2.) Attach DirectDraw to the window (create the handle, set the display mode, set the cooperative mode)
3.) Create a primary surface and draw something to it.
4.) Create a backbuffer surface, draw something to it, and flip it with the primary surface.
5.) Load in a bitmap and blit it to backbuffer.
6.) continue until game is finished.

G''luck.

Jon Woyame
Programmer & Cheeto Consumer
http://www.moonthrone.com
MeoDea
MeoDea
this still doesn''t work... well, it works, but illegal operations...

[source} /*Windows Initialization by Carter Sonnenberg
01001100110010101101111010001000110010101100001*/

#include <stdlib.h>
#include <iostream.h>
#include <windows.h>
#include <ddraw.h>

//test if key is hit
#define KEYHIT(key) ((GetAsyncKeyState(key) & 0x8000) ? TRUE : FALSE)

//zero mem. of dx struct
#define ZERO_STRUCT(struct) {ZeroMemory(&struct,sizeof(struct));struct.dwSize=sizeof(struct);}

//window globals
HINSTANCE hinstance;
HWND hwindow;
char CLASSNAME[10] = "Window";
char CLASSNAMEb[10] = "Window";

//direct draw globals
//LPDIRECTDRAW7 dd7 = NULL;
//LPDIRECTDRAWSURFACE7 primary = NULL;
//LPDIRECTDRAWSURFACE7 back = NULL;
//LPDIRECTDRAWSURFACE7 tileset = NULL;
//LPDIRECTDRAWSURFACE7 characters = NULL;
//LPDIRECTDRAWCLIPPER clipper = NULL;
//DDSURFACEDESC2 surface;

//prototype funcs
int INIT_DD_MAIN(void);
void DEINIT_DD_MAIN(void);

//prog starts here
int APIENTRY WinMain(HINSTANCE hinstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = CLASSNAME;
winclass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

if (!RegisterClassEx(&winclass))
{return(0);}

if (!(hwnd = CreateWindowEx(NULL,
CLASSNAME,
CLASSNAMEb,
WS_POPUP | WS_VISIBLE,
0, 0,
640, 480,
NULL,
NULL,
hinstance,
NULL))){
return(0);}

//Get time (used for frame rate)
DWORD dwStart = GetTickCount();

if(INIT_DD_MAIN()){

while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
if (msg.message == WM_QUIT){
break;}
TranslateMessage(&msg);
DispatchMessage(&msg);}
if (KEYHIT(VK_ESCAPE)){
PostQuitMessage(0);}

//computing crap here


// Set to 30 fps (33ms per frame)
while ((GetTickCount() - dwStart) < 33){};
}
}

else {return(0);}
DEINIT_DD_MAIN();
return(msg.wParam);
}


int INIT_DD_MAIN(void){

/* //create the directdraw7 interface
if(FAILED(DirectDrawCreateEx(NULL, (void**)&dd7, IID_IDirectDraw7, NULL)))
{return(FALSE);} */

/* //set cooperative level to fullscreen
if(FAILED(dd7->SetCooperativeLevel(hwindow, DDSCL_FULLSCREEN |
DDSCL_EXCLUSIVE |
DDSCL_ALLOWREBOOT)))
{return(FALSE);} */

//hide cursor
ShowCursor(FALSE);

//set 640x480x16 resolution
/* if(FAILED(dd7->SetDisplayMode(640, 480, 16, 0, 0)))
{return(FALSE);} */

//create our primary surface with one backbuffer
/* ZERO_STRUCT(surface);
surface.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
surface.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_COMPLEX |
DDSCAPS_FLIP;
surface.dwBackBufferCount = 1;
if(FAILED(dd7->CreateSurface(&surface, &primary, NULL)))
{return(FALSE);} */

//get the backbuffer
/* surface.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
if(FAILED(primary->GetAttachedSurface(&surface.ddsCaps, &back)))
{return(FALSE);} */

//create the clipper
/* if(FAILED(dd7->CreateClipper(0, &clipper, NULL)))
{return(FALSE);} */

//clip list stuff
/* LPRGNDATA lpClipList = (LPRGNDATA)malloc(sizeof(RGNDATAHEADER) +
sizeof(RECT));
RECT rcBoundary = {0, 0, 640, 480};
memcpy(lpClipList->Buffer, &rcBoundary, sizeof(RECT)); // set list
lpClipList->rdh.dwSize = sizeof(RGNDATAHEADER); // structure size
lpClipList->rdh.iType = RDH_RECTANGLES; // bound type
lpClipList->rdh.nCount = 1; // num of bounds
lpClipList->rdh.nRgnSize = sizeof(RECT); // size of Buffer
lpClipList->rdh.rcBound = rcBoundary; // bounding RECT
if (FAILED(clipper->SetClipList(lpClipList, 0)))
{
//still free malloc if failed
free(lpClipList);
return(FALSE);
} */

// attach clipper to back buffer
/* if(FAILED(back->SetClipper(clipper)))
{
//still free malloc if failed
free(lpClipList);
return(FALSE);
}

// free the memory
free(lpClipList); */

//create surface for tileset
//size augmented for tileset demensions
//ZERO_STRUCT(surface);
/* surface.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT;
surface.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
surface.dwWidth = 1;
surface.dwHeight = 1;
surface.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
surface.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
if(FAILED(dd7->CreateSurface(&surface, &tileset, NULL)))
{return(FALSE);} */

//create surface for characters
//size augmented for character demensions
//ZERO_STRUCT(surface);
/* surface.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT;
surface.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
surface.dwWidth = 1;
surface.dwHeight = 1;
surface.ddckCKSrcBlt.dwColorSpaceLowValue = 0;
surface.ddckCKSrcBlt.dwColorSpaceHighValue = 0;
if(FAILED(dd7->CreateSurface(&surface, &characters, NULL)))
{return(FALSE);} */

return(TRUE);
};

void DEINIT_DD_MAIN(void){
//if (characters){
// characters->Release();
// characters = NULL;}
//if (tileset){
// tileset->Release();
// tileset = NULL;}
//if (clipper){
// clipper->Release();
// clipper = NULL;}
//if (back){
// back->Release();
// back = NULL;}
//if (primary){
// primary->Release();
// primary = NULL;}
//if (dd7){
// dd7->Release();
// dd7 = NULL;}
CloseWindow(hwindow);
}; [/source]
wazoo69
wazoo69
Okay from a quick scan of that code, it looks like you''re calling CreateWindow and return the HWND into hwnd.

THEN in your INIT_DD_MAIN function, you''re using hwindow as your window handle (in your call to SetCooperativeLevel)..yet I don''t see anywhere where you''ve assigned it a value..(ie. hwindow = hwnd )..

hth,

Topic Locked

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

Sign in to reply to this topic.