Jump to content
  • Advertisement

Archived

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

MeoDea

DirectDraw.... problem

This topic is 5775 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

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...

Share this post


Link to post
Share on other sites
Advertisement
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).

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

  /*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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I am not a DD guru, but when you use "surface" here:
//create our primary surface with one backbuffer ZERO_STRUCT(surface); surface.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; surface.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |

it seems that you are assigning surface''s members values before surface has been initialized. This seems to be causing the error. Also, you could set surface to NULL at its declaration as you have done with your other pointers, and check surface against NULL before using it to be safe. Good luck!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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,

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!