Sign in to follow this  

Direct Draw 7: Need Help With 2 Errors

This topic is 4367 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, Right now I am adding 2d support into my game engine. I have 2 errors so far trying to just get a basic DD App running. Here is the source file and errors.
/*********************************************************************************
*
*	Insane Game Engine
*	(c)2005 Nathan Krygier. All Rights Reserved.
*
***********************************************************************************/
#include <ddraw.h>
#include <dsound.h>
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dinput.h>
#include "main.h"

void InitDirectDraw7(HWND hwnd)
{
		LPDIRECTDRAW7				lpDirectDraw			= NULL; // Direct Draw Interface 7
		LPDIRECTDRAWSURFACE7		lpPrimaryDDSurface		= NULL; // Direct Draw Surface 7
		DDSURFACEDESC2				ddSD;							// Direct Draw Surface Description

		// Create the Direct Draw Interface
		if(FAILED(DirectDrawCreateEx(NULL,(void **)&lpDirectDraw,IID_IDirectDraw7,NULL)))
		{
			MessageBox(NULL,Error_CreateInterface,Interface_Error, MB_OK);
		}

		// Set The Direct Draw Cooperation Level
		if(FAILED(lpDirectDraw->SetCooperativeLevel(hwnd,DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
												DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
		{
			MessageBox(NULL,Error_SetCooperationLevel,Cooperation_Error, MB_OK);
		}

		// Set The Display Mode
		if(FAILED(lpDirectDraw->SetDisplayMode(Display_Width,Display_Height,Display_Depth,0,0)))
		{
			MessageBox(NULL,Error_SetDisplay,Display_Error, MB_OK);
		}

		// Clear the description and set the size of it
		memset(&ddSD,0,sizeof(ddSD));
		ddSD.dwSize = sizeof(ddSD);

		// Enable the valid fields
		ddSD.dwFlags = DDSD_CAPS;

		// Request The Primary Surface
		ddSD.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

		// Create Primary Surface
		if(FAILED(lpDirectDraw->CreateSurface(&ddSD, &lpPrimaryDDSurface, NULL)))
		{
			MessageBox(NULL,Error_CreateSurface,Surface_Error, MB_OK);
		}

		// Clear ddSD and set size
		memset(ddSD,0,sizeof(ddSD));
		ddSD.dwSize = sizeof(ddSD);

		// Lock Surface
		if(FAILED(lpPrimaryDDSurface->Lock(NULL,&ddSD,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
		{
			MessageBox(NULL,Error_LockSurface,Surface_Error, MB_OK);
		}

		// Create Aliases
		int mempitch		= ddSD.lPitch;
		unsigned char *video_buffer = ddSD.lpSurface;
}

Errors: ------ Build started: Project: Insane Game Engine, Configuration: Debug Win32 ------ Compiling... main.cpp C:\Program Files\Microsoft DirectX 9.0 SDK (October 2005)\Include\dinput.h: DIRECTINPUT_VERSION undefined. Defaulting to version 0x0800 .\main.cpp(57) : error C2664: 'memset' : cannot convert parameter 1 from 'DDSURFACEDESC2' to 'void *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called .\main.cpp(68) : error C2440: 'initializing' : cannot convert from 'LPVOID' to 'unsigned char *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Insane Game Engine\Debug\BuildLog.htm" Insane Game Engine - 2 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
The first error is because you are missing the '&' in the line:

memset(ddSD,0,sizeof(ddSD));

It should be:

memset(&ddSD,0,sizeof(ddSD));


The second error is because you need a cast in the line:

unsigned char *video_buffer = ddSD.lpSurface;

It should be:

unsigned char *video_buffer = (unsigned char *)ddSD.lpSurface;

Share this post


Link to post
Share on other sites
Thanks :-)

Now all thats left are link errors.


------ Build started: Project: Insane Game Engine, Configuration: Debug Win32 ------
Compiling...
main.cpp
C:\Program Files\Microsoft DirectX 9.0 SDK (October 2005)\Include\dinput.h: DIRECTINPUT_VERSION undefined. Defaulting to version 0x0800
Linking...
main.obj : error LNK2019: unresolved external symbol _IID_IDirectDraw7 referenced in function "void __cdecl InitDirectDraw7(struct HWND__ *)" (?InitDirectDraw7@@YAXPAUHWND__@@@Z)
C:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Debug\Insane Game Engine.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Insane Game Engine\Debug\BuildLog.htm"
Insane Game Engine - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
Ok I have my plot pixel function all set, well almost.
Im getting this runtime error:

Unhandled exception at 0x00401005 in Insane Game Engine.exe: 0xC0000005: Access violation reading location 0x00000000.

and

No symbols are loaded for any call stack frame. The source code cannot be displayed.

Here is my plot pixel function, and my call to it


// DRAWS A DIRECT DRAW 7 PIXEL
void DrawPixelDD7(int ix, int iy, int ialpha, int ired, int igreen, int iblue,
unsigned int *video_buffer, int lpitch32)
{
// build color word
unsigned int pixel = _RGB32BIT(ialpha,ired,igreen,iblue);

// write data
video_buffer[ix + iy*lpitch32] = pixel;
}

// LOCKS THE DIRECT DRAW 7 SURFACE
void LockSurfaceDD7()
{
if(FAILED(lpPrimaryDDSurface->Lock(NULL,&ddSD,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
{
MessageBox(NULL,Error_LockSurface,Surface_Error, MB_OK);
}
}

// UNLOCKS THE DIRECT DRAW SURFACE
void UnlockSurfaceDD7()
{
if(FAILED(lpPrimaryDDSurface->Unlock(NULL)))
{
MessageBox(NULL,Error_LockSurface,Surface_Error, MB_OK);
}
}





LockSurfaceDD7();
DrawPixelDD7(40,40,0,255,0,0,video_buffer,lpitch32);
UnlockSurfaceDD7();




[Edited by - Fixxer on December 18, 2005 2:36:48 PM]

Share this post


Link to post
Share on other sites
Your mempitch and video_buffer variables are local variables to your InitDirectDraw7 function. So, setting video_buffer to ddSD.lpSurface doesn't have any effect on your (presumably) global video_buffer variable. Thus, when you go to draw a pixel, video_buffer isn't pointing at your surface.

Share this post


Link to post
Share on other sites
I changed it and still get the same errors.

Ok I have my plot pixel function all set, well almost.
Im getting this runtime error:

Unhandled exception at 0x00401005 in Insane Game Engine.exe: 0xC0000005: Access violation reading location 0x00000000.

and

No symbols are loaded for any call stack frame. The source code cannot be displayed.

Here is my plot pixel function, and my call to it


// DRAWS A DIRECT DRAW 7 PIXEL
void DrawPixelDD7(int ix, int iy, int ialpha, int ired, int igreen, int iblue)
{
// build color word
unsigned int pixel = _RGB32BIT(ialpha,ired,igreen,iblue);

// write data
video_buffer[ix + iy*lpitch32] = pixel;
}

// LOCKS THE DIRECT DRAW 7 SURFACE
void LockSurfaceDD7()
{
if(FAILED(lpPrimaryDDSurface->Lock(NULL,&ddSD,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
{
MessageBox(NULL,Error_LockSurface,Surface_Error, MB_OK);
}
}

// UNLOCKS THE DIRECT DRAW SURFACE
void UnlockSurfaceDD7()
{
if(FAILED(lpPrimaryDDSurface->Unlock(NULL)))
{
MessageBox(NULL,Error_LockSurface,Surface_Error, MB_OK);
}
}





LockSurfaceDD7();
DrawPixelDD7(40,40,0,255,0,0);
UnlockSurfaceDD7();


Share this post


Link to post
Share on other sites
You have a NULL pointer somewhere. I just noticed that ALL of the variables you reference in your Init function are local variables. So NONE of your global variables (lpDirectDraw, lpPrimaryDDSurface, etc) are getting set.

Share this post


Link to post
Share on other sites
I have since put those in a header prior to getting these errors, the program runs fine when not calling the plot pixel function and when I call the lock surface function right after initialization outside of the IGERENDER function and when not calling unlock surface at all.

Share this post


Link to post
Share on other sites
Here is my entire project, in its current state.

IGEWindows.h

/*********************************************************************************
*
* Insane Game Engine
* (c)2005 Nathan Krygier. All Rights Reserved.
*
***********************************************************************************/


// WINDOW DEFINES
#define Window_Title "Insane Game Engine Application"
#define Window_Pos_X 0
#define Window_Pos_Y 0
#define Window_Height GetSystemMetrics(SM_CYFULLSCREEN)
#define Window_Width GetSystemMetrics(SM_CXFULLSCREEN)

// ENGINE FUNCTION PROTOTYPES
void IGERUNTIME();



main.h

/*********************************************************************************
*
* Insane Game Engine
* (c)2005 Nathan Krygier. All Rights Reserved.
*
***********************************************************************************/

//DIRECT X INITIALIZATIONS
#define UsingDirectDraw7

// DIRECT DRAW 7 VARIABLES/DEFINES
LPDIRECTDRAW7 lpDirectDraw = NULL; // Direct Draw Interface 7
LPDIRECTDRAWSURFACE7 lpPrimaryDDSurface = NULL; // Direct Draw Surface 7
DDSURFACEDESC2 ddSD; // Direct Draw Surface Description

int lpitch32 = 0;
unsigned int *video_buffer = 0;

// DIRECT DRAW DISPLAY DEFINES
#define Display_Width 800
#define Display_Height 600
#define Display_Depth 32

// DIRECT DRAW ERROR DEFINES
#define Interface_Error "DD Interface Error"
#define Error_CreateInterface "Error Creating DD Interface"

#define Cooperation_Error "DD Cooperation Error"
#define Error_SetCooperationLevel "Error Setting DD Cooperation Level"

#define Display_Error "DD Display Error"
#define Error_SetDisplay "Error setting DD Display"

#define Surface_Error "DD Surface Error"
#define Error_CreateSurface "Error Creating DD Surface"
#define Error_LockSurface "Error Locking DD Surface"
#define Error_UnlockSurface "Error Unlocking DD Surface"

// DIRECT DRAW RGB DEFINES
#define _RGB32BIT(a,r,g,b) ((b)+((g) << 8) + ((r) << 16) + ((a) << 24))

// DIRECT DRAW7 FUNCTION PROTOTYPES
void InitDirectDraw7(HWND); // Initializes Direct Draw 7
void DrawPixelDD7(int ix, int iy, int ialpha, int ired,
int igreen, int iblue); // Draws a pixel using Direct Draw 7




main.cpp

/*********************************************************************************
*
* Insane Game Engine
* (c)2005 Nathan Krygier. All Rights Reserved.
*
***********************************************************************************/

#include <ddraw.h>
#include <dsound.h>
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dinput.h>

#include "main.h"

// INITIALIZES DIRECT DRAW 7
void InitDirectDraw7(HWND hwnd)
{
// Create the Direct Draw Interface
if(FAILED(DirectDrawCreateEx(NULL,(void **)&lpDirectDraw,IID_IDirectDraw7,NULL)))
{
MessageBox(NULL,Error_CreateInterface,Interface_Error, MB_OK);
}

// Set The Direct Draw Cooperation Level
if(FAILED(lpDirectDraw->SetCooperativeLevel(hwnd,DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
{
MessageBox(NULL,Error_SetCooperationLevel,Cooperation_Error, MB_OK);
}

// Set The Display Mode
if(FAILED(lpDirectDraw->SetDisplayMode(Display_Width,Display_Height,Display_Depth,0,0)))
{
MessageBox(NULL,Error_SetDisplay,Display_Error, MB_OK);
}

// Clear the description and set the size of it
memset(&ddSD,0,sizeof(ddSD));
ddSD.dwSize = sizeof(ddSD);

// Enable the valid fields
ddSD.dwFlags = DDSD_CAPS;

// Request The Primary Surface
ddSD.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

// Create Primary Surface
if(FAILED(lpDirectDraw->CreateSurface(&ddSD, &lpPrimaryDDSurface, NULL)))
{
MessageBox(NULL,Error_CreateSurface,Surface_Error, MB_OK);
}

// Clear ddSD and set size
memset(&ddSD,0,sizeof(ddSD));
ddSD.dwSize = sizeof(ddSD);

// Create Aliases
lpitch32 = (int)(ddSD.lPitch >> 2);
video_buffer = (unsigned int *)ddSD.lpSurface;
}

// DRAWS A DIRECT DRAW 7 PIXEL
void DrawPixelDD7(int ix, int iy, int ialpha, int ired, int igreen, int iblue)
{
// build color word
unsigned int pixel = _RGB32BIT(ialpha,ired,igreen,iblue);

// write data
video_buffer[ix + iy*lpitch32] = pixel;
}

// LOCKS THE DIRECT DRAW 7 SURFACE
void LockSurfaceDD7()
{
if(FAILED(lpPrimaryDDSurface->Lock(NULL,&ddSD,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
{
MessageBox(NULL,Error_LockSurface,Surface_Error, MB_OK);
}
}

// UNLOCKS THE DIRECT DRAW SURFACE
void UnlockSurfaceDD7()
{
if(FAILED(lpPrimaryDDSurface->Unlock(NULL)))
{
MessageBox(NULL,Error_UnlockSurface,Surface_Error, MB_OK);
}
}





//*********************************************************************************
//*********************************************************************************
//*********************************************************************************



void IGERUNTIME()
{
LockSurfaceDD7();
DrawPixelDD7(40,40,0,255,0,0);
UnlockSurfaceDD7();

}



windows.h

/*********************************************************************************
*
* Insane Game Engine
* (c)2005 Nathan Krygier. All Rights Reserved.
*
***********************************************************************************/

#define WIN32_LEAN_AND_MEAN // DO NOT USE MFC
#define INITGUID

#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>

#include "IGEWindows.h"

#define WINDOWSCLASS "WINCLASS1" // DECLARE WINCLASS CONSTANT

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
// EVENT HANDELER/////////////////////////////////////////////////////////////
{
PAINTSTRUCT ps;
HDC hdc;

switch(msg)
{
case WM_CREATE:
{
// WINDOW CREATION CODE
return(0);
}break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return(0);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
default:break;
}
return(DefWindowProc(hwnd,msg,wparam,lparam));
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
LPSTR lpcmdline, int ncmdshow)
// WIN MAIN /////////////////////////////////////////////////////////////
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;

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

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

if(!(hwnd = CreateWindowEx(NULL,WINDOWSCLASS,Window_Title,WS_POPUP | WS_VISIBLE | WS_MAXIMIZE,
Window_Pos_X,Window_Pos_Y,Window_Width,Window_Height,NULL,NULL,
hinstance,NULL)))
{
return(0);
}

// INITIALIZE DIRECT X ////////////////////////////////////////////////
#ifdef UsingDirectDraw7
InitDirectDraw7(hwnd);
#endif

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


Share this post


Link to post
Share on other sites
Here are the disassembly lines the debug points to:

00401012 mov dword ptr [eax+ecx],0FF0000h (.exe file)

These may be missing or incorrect

004011D2 jmp 004011A5 (.exe file)
00401445 mov dword ptr [ebp-1Ch],eax (.exe file)
7C816D4F push eax (kernel32)
7C8399F3 push ebp (kernel32)


I have no idea what this all means, maybe you do :-)

Share this post


Link to post
Share on other sites
The lpSurface member of DDSURFACEDESC2 isn't valid until you call Lock(). So saving that value after the call to CreateSurface isn't saving a valid value. Try setting video_buffer to ddSD.lpSurface in the LockSurfaceDD7() function.

Share this post


Link to post
Share on other sites
i'm having somewhat the same problem, but i know you're doing something wrong, take a look at the improvements:


// LOCKS THE DIRECT DRAW 7 SURFACE
void LockSurfaceDD7()
{
if(FAILED(lpPrimaryDDSurface->Lock(NULL,&ddSD,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
{
MessageBox(NULL,Error_LockSurface,Surface_Error, MB_OK);
return();
}

// update the aliases here since there's no guarantee that vram will stay in the same place
lpitch32 = (int)(ddSD.lPitch >> 2);
*video_buffer = (unsigned int *)ddSD.lpSurface;


}


i think you shouldn't lock the surface directly after creation, only when you want to write to the screen.

be sure that your application's window is ACTIVE! catch the WM_ACTIVATEAPP message in the winproc and only draw to the surface when the application's window is active. pressing a key makes the window inactive apparently (using windows messages). to avoid the window becoming inactive after a keypress you could use GetAsyncKeyState.

[Edited by - rm82 on January 2, 2006 8:05:11 AM]

Share this post


Link to post
Share on other sites

This topic is 4367 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this