Skip to main content
gamedev.net
🔒 Locked

Direct X library - problems

Started by demonrealms Jan 9, 2004 at 4:48 PM 6 replies 350+ views
Original Post
demonrealms
demonrealms
Hi everyone. I feel like granting my computer flight *kicks out the window* Anyway I'm having a problem. For some stupid reason my program won't let me use any of my library functions that are Direct X. For example my major one. I can lock the surface(primary)using the following code in the main .cpp file:
if (FAILED(lpddsprimary->Lock(NULL, &ddsd,
                   DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
                   NULL)))
   {
   // error

   return(0);
   } // end if

But that's in the Game_Main function. Now if I put the code above in a function and put in a .h file it won't plot the pixels on the screen. It just stays black. Here's the source: .h file with function
int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing

// here


// for now test if user is hitting ESC and send WM_CLOSE

if (KEYDOWN(VK_ESCAPE))
   SendMessage(main_window_handle,WM_CLOSE,0,0);
memset(&ddsd,0,sizeof(ddsd)); 
ddsd.dwSize = sizeof(ddsd);
if (FAILED(lpddsprimary->Lock(NULL, &ddsd,
                   DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
                   NULL)))
   {
   // error

   return(0);
   } // end if


// now ddsd.lPitch is valid and so is ddsd.lpSurface


// make a couple aliases to make code cleaner, so we don't

// have to cast

int mempitch        = (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

// plot 1000 random pixels with random colors on the

// primary surface, they will be instantly visible

for (int index=0; index < 1000; index++)
    {
    // select random position and color for 640x480x8

    UCHAR color = rand()%256;
    int x = rand()%SCREEN_WIDTH;
    int y = rand()%SCREEN_HEIGHT;
	int x2 = rand()%700;
	int y2 = rand()%700;
	Plot8(x2, y2, color, (UCHAR *)ddsd.lpSurface, (int)ddsd.lPitch);
	Plot16(x, y, color, color, color, (USHORT *)ddsd.lpSurface, (int)ddsd.lPitch);
    } // end for index

// now unlock the primary surface

if (FAILED(lpddsprimary->Unlock(NULL)))
   return(0);

// sleep a bit

Sleep(30);

// return success or failure or your own return code here

return(1);

} // end Game_Main

And this is the way I want it:
int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing

// here


// for now test if user is hitting ESC and send WM_CLOSE

if (KEYDOWN(VK_ESCAPE))
   SendMessage(main_window_handle,WM_CLOSE,0,0);
memset(&ddsd,0,sizeof(ddsd)); 
ddsd.dwSize = sizeof(ddsd);
locklp(lpddsprimary, ddsd);

// now ddsd.lPitch is valid and so is ddsd.lpSurface


// make a couple aliases to make code cleaner, so we don't

// have to cast

int mempitch        = (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

// plot 1000 random pixels with random colors on the

// primary surface, they will be instantly visible

for (int index=0; index < 1000; index++)
    {
    // select random position and color for 640x480x8

    UCHAR color = rand()%256;
    int x = rand()%SCREEN_WIDTH;
    int y = rand()%SCREEN_HEIGHT;
	int x2 = rand()%700;
	int y2 = rand()%700;
	Plot8(x2, y2, color, (UCHAR *)ddsd.lpSurface, (int)ddsd.lPitch);
	Plot16(x, y, color, color, color, (USHORT *)ddsd.lpSurface, (int)ddsd.lPitch);
    } // end for index

// now unlock the primary surface

if (FAILED(lpddsprimary->Unlock(NULL)))
   return(0);

// sleep a bit

Sleep(30);

// return success or failure or your own return code here

return(1);

} // end Game_Main


Wh ycan't I use my own built in function? Thanks for your time. Oh yes and I have included the ".h" file btw. [edited by - demonrealms on January 9, 2004 5:52:04 PM]
neneboricua19
neneboricua19
It probably has to do with how you''re passing in the parameters to your "locklp" function. Can you post the declaration of this function so that we can take a look at how you pass in the parameters?

neneboricua
Dave Hunt
Dave Hunt
It''s probably because you passed ddsd to your locklp() function by value, rather than by reference or by address. So any changes locklp() makes to ddsd are made to the local copy of ddsd, rather than the one you passed in.
demonrealms
demonrealms

if(FAILED(locklp(lpddsprimary, &ddsd)))
{
MsgBox("Lock surface", "Sorry, failed to lock surface", 1);
return(0);
}

That's how I declaered it thanks for all your help so far.


And this is how I declared it in the library:

void locklp(LPDIRECTDRAWSURFACE7 lpddsprimary, DDSURFACEDESC2 ddsd)
{
lpddsprimary->Lock(NULL, &ddsd,
DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
NULL)
}


[edited by - demonrealms on January 10, 2004 11:28:16 AM]
Endurion
Endurion
Change the function to


void locklp(LPDIRECTDRAWSURFACE7 lpddsprimary, DDSURFACEDESC2& ddsd)


Note the &, making it a reference.
You were making the changes do ddsd to a local copy, exactly as Dave Hunt said.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
demonrealms
demonrealms
Thanks but now I have this problem:

--------------------Configuration: Beginning Dierct Draw - Win32 Debug--------------------
Compiling...
prj1.cpp
c:\directx\dxf\dxsdk\include\dinput.h: DIRECTINPUT_VERSION undefined. Defaulting to version 0x0800
C:\Projects for VC++\Beginning Dierct Draw\prj1.cpp(77) : warning C4101: ''buffer'' : unreferenced local variable
C:\Projects for VC++\Beginning Dierct Draw\prj1.cpp(141) : error C2664: ''locklp'' : cannot convert parameter 2 from ''struct _DDSURFACEDESC2 *'' to ''struct _DDSURFACEDESC2 &''
A reference that is not to ''const'' cannot be bound to a non-lvalue
Error executing cl.exe.

prj1.obj - 1 error(s), 1 warning(s)
Supernat02
Supernat02
Like those guys said, but I would pass the pointer instead to save some confusion.


bool LockSurface(LPDIRECTDRAWSURFACE7 Surface, DDSURFACEDESC2 *LocalDDSD)
{
lpddsprimary->Lock(NULL, LocalDDSD, ........);
}

Pass the struct''s pointer when you call the function.

WinMain(...)
{
// Setup stuff.

DDSURFACEDESC2 ddsd;
bool Locked;

ZeroMemory(ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);

//... More stuff.

Locked = LockSurface(lpddsprimary, &ddsd);
}



This should work.

Good luck,
Chris
Chris ByersMicrosoft DirectX MVP - 2005
demonrealms
demonrealms
Thanks I put everything you guys mentioned together and it works now.

Topic Locked

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

Sign in to reply to this topic.