PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

help a noob....

iam quite new to gapidraw and game programming in general.... iam just wondering if there is a comprehensive guide for gapidraw, one with some illustrations...for easy understanding.... i tried searching the forums but i cant find one.... maybe you guys can help....

i tried to also run a program with a struct that i have made it is like this...

struct Sprite
{
DWORD x_pos;
DWORD y_pos
};

it is included in a separate header file from my main class that is derived from CMyApplication.... i put a Sprite object called red_sprt as a protected member of the class that is derived from CMyApplication... and i also initialized its member in InitInstace but when i tried to run the program it produces an error that says "Sprite red_sprt: No default constructor"... can anyone help me solve this??
Hi impe20,


Welcome to the gapidraw forums,

Your best bet for learning gapidraw is to work your way through the sample applications that come with the sdk and looking at the documentation, other than that theres not much out there. If you’re having trouble following them I suggest brushing up on your c++ first.


I cant be sure without looking at your code but it seems that you have made a constructor for your sprite object that takes input values

Ie:

Code:

struct Sprite
{
public:
DWORD x_pos;
DWORD y_pos;

Sprite(DWORD x, DWORD y)
{
x_pos = x;
y_pos = y;

}


};



But have made an object on the stack inside your derived class

Ie
Code:

Sprite red_sprt;


When you do this the object must have a default constructor that takes no inputs as the stack size must be pre allocated when your derived class is constructed.


There are 2 ways to get around this 1 give your class a default constructor:


Code:

struct Sprite
{
public:
DWORD x_pos;
DWORD y_pos;

Sprite() //default constructor
{
x_pos = 0;
y_pos = 0;


}

Sprite(DWORD x, DWORD y)
{
x_pos = x;
y_pos = y;

}


};


Or

Store a pointer to the sprite in your derived class ie:
Code:

Sprite* red_sprt; // Sprite* <- pointer

Then dynamicly allocate the momory on the heap in the derived class constructor with
Code:

red_sprt = new Sprite(20,20);


just remember to free it when your finished.



Ok :)
thanks for the help... i also tried making my struct a class and it worked.....

well i have another problem.... here is the code...
Code:
#ifndef STRICT
#define STRICT
#endif

#include "GapiDraw.h"
#include "GapiApplication.h"
#include "resource.h"

// ----------------------------------------

class Sprite
{
public:
Sprite();
DWORD X_Pos;
DWORD Y_Pos;
DWORD last_direction;
DWORD X_Disp;
DWORD Y_Disp;
CGapiSurface *bitmap; // alive sprite
CGapiSurface *explosion_bitmap;//explode Sprite
CGapiSurface *dead_bitmap; //dead sprite
DWORD status; // 0 = alive, 1 = exploding, 2 = dead/sleeping
DWORD deadtime; // number of frames since collision
};

Sprite::Sprite()
{
X_Pos = 0;
Y_Pos = 0;
last_direction = 0;
X_Disp = 0;
Y_Disp = 0;
status = 0;
deadtime = 0;
}


class CMyApplication : public CGapiApplication
{
protected:
Sprite user_sprite; //player controlled
Sprite robot_sprite; //random moving
CGapiSurface *m_background;
DWORD m_dwKey;
DWORD m_dwKills;

public:
CMyApplication(const GDAPPCONFIG& config) : CGapiApplication(config) {
m_background = new CGapiSurface(m_pGapiDraw);
// initialize user sprite
user_sprite.X_Pos = 100;
user_sprite.Y_Pos = 140;
user_sprite.last_direction = 0;
user_sprite.X_Disp =10;
user_sprite.Y_Disp =10;
user_sprite.bitmap = new CGapiSurface(m_pGapiDraw);

// initialize random moving sprite
robot_sprite.X_Pos = 100;
robot_sprite.Y_Pos = 200;
robot_sprite.last_direction = 0;
robot_sprite.X_Disp =10;
robot_sprite.Y_Disp =10;
robot_sprite.bitmap = new CGapiSurface(m_pGapiDraw);
robot_sprite.status = 0;
robot_sprite.deadtime = 0;


// initialize key presses
m_dwKey = 0;
//initialise number of kills
m_dwKills=0;


};

virtual HRESULT InitInstance()
{



return GD_OK;
}


virtual HRESULT CreateSysMemSurfaces(CGapiDisplay* pDisplay, HINSTANCE hInstance)
{
// Add code to create surfaces here
// create background from resource
m_background->CreateSurface(GDSURFACE_SYSTEMMEMORY, hInstance, IDR_PNG1, TEXT("PNG"));

//set bitmap for user sprite
user_sprite.bitmap->CreateSurface(GDSURFACE_SYSTEMMEMORY, hInstance, IDB_BITMAP1, NULL);

//set bitmap for autonomous sprite
robot_sprite.bitmap->CreateSurface(GDSURFACE_SYSTEMMEMORY, hInstance, IDB_BITMAP2, NULL);
robot_sprite.explosion_bitmap->CreateSurface(GDSURFACE_SYSTEMMEMORY, hInstance, IDB_BITMAP3, NULL);
robot_sprite.dead_bitmap->CreateSurface(GDSURFACE_SYSTEMMEMORY, hInstance, IDB_BITMAP4, NULL);

return GD_OK;
}


virtual HRESULT CreateVidMemSurfaces(CGapiDisplay* pDisplay, HINSTANCE hInstance)
{
// Add code to create surfaces stored in video memory here
return GD_OK;
}


virtual HRESULT ProcessNextFrame(CGapiSurface* pBackbuffer, DWORD dwFlags)
{
// draw background
pBackbuffer->BltFast(0, 0, m_background, NULL, NULL, NULL);

// how big is the screen?
DWORD dwScreenWidth = pBackbuffer->GetWidth();
DWORD dwScreenHeight = pBackbuffer->GetHeight();

// calculate next move for the user sprite
MoveSpriteByKeyPresses(dwScreenWidth,dwScreenHeight,
user_sprite.bitmap->GetWidth(), user_sprite.bitmap->GetHeight(),
user_sprite.X_Pos, user_sprite.Y_Pos,
user_sprite.X_Disp, user_sprite.Y_Disp);

// calculate next move for the autonomous sprite
MoveSpriteRandomly(robot_sprite.last_direction,dwScreenWidth,dwScreenHeight,
robot_sprite.bitmap->GetWidth(), robot_sprite.bitmap->GetHeight(),
robot_sprite.X_Pos, robot_sprite.Y_Pos,
robot_sprite.X_Disp, robot_sprite.Y_Disp);

// do collision detection

POINT intersection = {0, 0};

if (robot_sprite.status==0) // do collision detection only if robot sprite is alive
{
m_background->Intersect(user_sprite.X_Pos, user_sprite.Y_Pos, NULL, user_sprite.bitmap,
robot_sprite.X_Pos, robot_sprite.Y_Pos, NULL, &intersection);


if ((intersection.x==-1) && (intersection.y==-1))
{ // we have a collision
MessageBeep(0xFFFFFFFF);
robot_sprite.status = 1;
m_dwKills++;
}
}

// draw the robot sprite
if (robot_sprite.status!=0) robot_sprite.deadtime++; // count how many frames sprite has been dead for
if (robot_sprite.deadtime==6) robot_sprite.status=2; // sprite has finished exploding and is dead
if (robot_sprite.deadtime==100)
{
robot_sprite.status=0; // sprite comes alive again after 100 frames
robot_sprite.deadtime=0;
}

switch(robot_sprite.status)
{
case 0: // draw normal sprite
pBackbuffer->BltFast(robot_sprite.X_Pos, robot_sprite.Y_Pos,
robot_sprite.bitmap, NULL, GDBLTFAST_KEYSRC, NULL);
break;
case 1: // draw exploding sprite
pBackbuffer->BltFast(robot_sprite.X_Pos, robot_sprite.Y_Pos,
robot_sprite.explosion_bitmap, NULL, GDBLTFAST_KEYSRC, NULL);
break;
case 2: // draw dead sprite
pBackbuffer->BltFast(robot_sprite.X_Pos, robot_sprite.Y_Pos,
robot_sprite.dead_bitmap, NULL, GDBLTFAST_KEYSRC, NULL);
break;
default: break;// should never happen - but do zippo
}

// draw the two sprites
pBackbuffer->BltFast(user_sprite.X_Pos, user_sprite.Y_Pos, user_sprite.bitmap, NULL, GDBLTFAST_KEYSRC, NULL);


// draw text
TCHAR str[128];
_stprintf(str, TEXT("Number of Kills: %d"),m_dwKills);
pBackbuffer->DrawText(10, 300, str, m_pDisplay->GetSystemFontBorder(), 0, NULL, 0, NULL);


return S_OK;
};

HRESULT CMyApplication::MoveSpriteByKeyPresses(DWORD ScreenWidth, DWORD ScreenHeight,
DWORD bitmap_width, DWORD bitmap_height,
DWORD &X, DWORD &Y, DWORD X_Disp, DWORD Y_Disp)
{
DWORD prevX = X;
DWORD prevY = Y;

if (m_dwKey & 1) // up
{
Y = Y- Y_Disp;
}
if (m_dwKey & 2) // down
{
Y = Y+Y_Disp;
}
if (m_dwKey & 4) // left
{
X = X-X_Disp;
}
if (m_dwKey & 8) // right
{
X=X+X_Disp;
}
// check new position is not outside screen bounds
// if new position would be outside screen bounds
if ((X<0) || (X>(ScreenWidth-bitmap_width)) || (Y<0) || (Y>(ScreenHeight-bitmap_height)))
{
X=prevX; // don't move the sprite
Y=prevY;
}

return S_OK;
};


HRESULT CMyApplication::MoveSpriteRandomly(DWORD &last_direction, DWORD ScreenWidth, DWORD ScreenHeight,
DWORD bitmap_width, DWORD bitmap_height,
DWORD &X, DWORD &Y, DWORD X_Disp, DWORD Y_Disp)
{
// calculate new position based on current sprite direction
DWORD prevX = X;
DWORD prevY = Y;
switch(last_direction)
{
case 0: // move N
Y = Y-Y_Disp; break;
case 1: // move NE
Y = Y-Y_Disp;
X = X+X_Disp; break;
case 2: // move E
X = X+X_Disp; break;
case 3: //move SE
X = X+X_Disp;
Y = Y+Y_Disp; break;
case 4: //move S
Y = Y+Y_Disp; break;
case 5: // move SW
Y = Y+Y_Disp;
X = X-X_Disp; break;
case 6: // move W
X = X-X_Disp; break;
case 7: // move NW
X = X-X_Disp;
Y = Y-Y_Disp; break;
default: break;// should never happen - but do zippo
}

// if new position would be outside screen bounds
if ((X<0) || (X>(ScreenWidth-bitmap_width)) || (Y<0) || (Y>(ScreenHeight-bitmap_height)))
{
X=prevX; // don't move the sprite
Y=prevY;
DWORD new_direction = last_direction;
while (new_direction==last_direction) // make sure new direction is different from old!
{
new_direction = (rand()*(DWORD)8)/RAND_MAX;
}
last_direction = new_direction; // change sprite direction
//MessageBeep(0xFFFFFFFF); // beep when sprite hits screen edge
}

return S_OK;
};

HRESULT CMyApplication::KeyDown(DWORD dwKey, GDKEYLIST& keylist)
{
if (dwKey == keylist.vkA)
{
// Key '1' (Calendar button on X5) pressed
Shutdown();
}
else if (dwKey == keylist.vkUp)
{
m_dwKey |= 1; // up
}
else if (dwKey == keylist.vkDown)
{
m_dwKey |= 2; // down
}
else if (dwKey == keylist.vkLeft)
{
m_dwKey |= 4; // left
}
else if (dwKey == keylist.vkRight)
{
m_dwKey |= 8; // right
}
return S_OK;
};

HRESULT CMyApplication::KeyUp(DWORD dwKey, GDKEYLIST& keylist)
{
if (dwKey == keylist.vkUp)
{
m_dwKey &= ~1; // up
}
else if (dwKey == keylist.vkDown)
{
m_dwKey &= ~2; // down
}
else if (dwKey == keylist.vkLeft)
{
m_dwKey &= ~4; // left
}
else if (dwKey == keylist.vkRight)
{
m_dwKey &= ~8; // right
}
return S_OK;
};



virtual ~CMyApplication() {};
};

// ----------------------------------------

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
LPTSTR pCmdLine, int nCmdShow)
{
GDAPPCONFIG config;
::ZeroMemory(&config, sizeof(GDAPPCONFIG));

config.hInstance = hInst;
config.pAppTitle = TEXT("Welcome to GapiDraw!");
config.dwMaxFPS = 30;
config.dwWindowIcon = IDI_ICON1;

// Create the application
CMyApplication simple(config);

// Start main loop
return simple.Run();
}

i used 5 resources here one for the background... one for the player controlled sprite, one for the random moving sprite, another for the exploding sprite, one for the blank sprite when it is dead and the last is for the icon...

when i run this on winxp it always closes with the send error window, i havent tried this on other OS ... hope someone could help...
To be honest I cant immediately see whats wrong with the code,

I would suggest making a copy of your application, then stripping out most of your code to make sure that you get a blank window running ok,

Then try drawing a surface onto that,

If that all goes ok then it must be something in your user code and not how you set up gapidraw.
On suggestion is to check the return values on ALL gapidraw calls. This will help you solve many of issues you are finding. (Waiting for Frank to stop chuckling...)

For instance:

Code:

m_background->CreateSurface(GDSURFACE_SYSTEMMEMORY, hInstance, IDR_PNG1, TEXT("PNG"));


should at a minimum be:

Code:

HRESULT hr = m_background->CreateSurface(GDSURFACE_SYSTEMMEMORY, hInstance, IDR_PNG1, TEXT("PNG")); if(FAILED(hr)) return hr;


Your methods all make the assumption that each GD call is successful.

Now, your specific problem is that you don't have an ExitInstance method (maybe you just didn't cut & paste it here) that deletes the CGapiSurfaces you created. That will cause your application to exit with an error.
:oops: ok ill try your suggestions... if it still come up ill post here again...

by the way do you guys know where i could get some resources and info on game programming AI?? i am quite new to programming and i needed somme ideas... thanks...
AI implementations really depend on the type of game you are creating. There are a number of books (and ebooks) and websites that describe various AI techniques ranging from finite state machines to neural networks to genetic algorithms and everything in between.
(Waiting for Frank to stop chuckling...)


BWAHAHAHA! Gonna be a while :)

Seriously though, it's good advice. I actually have wrapper functions for some of the GD calls (like loading surfaces and creating bitmaps)... It presents a nice error dialog with some good details if any problems arise, that way I don't have to remember or look up error codes, it tells me exactly what happened and where.
InexorableTash · 24 Jan 2005, 06:58 UTC
Learn to use and love assertions. Trust me!

Every call to anything with a failure code should look like this:

Code:

HRESULT hr;

hr = SomeFunction();
Assert( SUCCEEDED(hr) );
if( FAILED(hr) )
return hr;


You can wrap the whole thing into a single macro if you prefer (e.g. CheckHR( SomeFunction() ) ), but I personally don't like macros doing flow control.

MFC comes with the source to a reasonable Assert() macro that you can rip apart and make into something useful. I can share the source to mine if anyone wants. It pops a MessageBox with the file, line, an optional message, and lets you break, ignore, always ignore this assert, disable all asserts, etc.
ppcStudios wrote:
Now, your specific problem is that you don't have an ExitInstance method (maybe you just didn't cut & paste it here) that deletes the CGapiSurfaces you created. That will cause your application to exit with an error.


i have tried adding an exitinstance method but it still gets an error... i will try to use assert when i get home... i will post again when i get it running....thanks....
i have tried using assert to find if there are trouble in my code but it still isnt working... hmmm.. could anybody try to run this code??? it would really help a lot... thanks....
If you want to email me the project I'll give it a try.
InexorableTash wrote:Learn to use and love assertions. Trust me!

Every call to anything with a failure code should look like this:

Code:

HRESULT hr;

hr = SomeFunction();
Assert( SUCCEEDED(hr) );
if( FAILED(hr) )
return hr;


You can wrap the whole thing into a single macro if you prefer (e.g. CheckHR( SomeFunction() ) ), but I personally don't like macros doing flow control.

MFC comes with the source to a reasonable Assert() macro that you can rip apart and make into something useful. I can share the source to mine if anyone wants. It pops a MessageBox with the file, line, an optional message, and lets you break, ignore, always ignore this assert, disable all asserts, etc.


Can you post your source for this? I'd love to have a nice assert function like this.
Oops, forgot to login when I requested the source for the assertion function you use. :)
InexorableTash · 29 Jan 2005, 06:38 UTC
Okie dokie - here's assert.h:

Code:

#pragma once

#ifdef _DEBUG

#include <signal.h>
#include <windows.h>

#pragma warning( disable : 4127 ) // Conditional expression is constant

enum AssertAction
{
Abort,
Break,
Ignore,
IgnoreAlways,
};

AssertAction _assert(void* expr,
void* filename,
unsigned lineno,
char* msg=NULL );

#ifdef _WIN32_WCE
#define AssertDebugBreak() DebugBreak()
#else
#define AssertDebugBreak() { _asm{int 0x03} }
#endif

#define AssertSz(exp,msg) \
\
{ \
static bool _fIgnore = false; \
if( !_fIgnore && !(exp) ) \
{ \
switch( _assert( #exp, __FILE__, __LINE__, msg ) ) \
{ \
case Abort: \
raise(SIGABRT); \
exit(3); \
break; \
\
case Break: \
AssertDebugBreak(); \
break; \
\
case IgnoreAlways: \
_fIgnore = true; \
case Ignore: \
default: \
break; \
} \
} \
} \

#define Assert(exp) AssertSz(exp,NULL)

#else // !_DEBUG

#define Assert(exp)
#define AssertSz(exp,msg)

#endif // _DEBUG


And assert.cpp:

Code:

// Self Include
#include "assert.h"

#ifdef _DEBUG

// Library includes
#include <windows.h>
#include <stdio.h>

AssertAction _assert(void* expr,
void* filename,
unsigned lineno,
char* msg )
{

char buf[ 1024 ];

::wsprintfA(
buf,
"Assert: %s\n"
"%s(%d)\n"
"%s\n"
"\n"
"Retry = Break into debugger?\n"
"Shift+Ignore = Always Ignore",
expr,
filename,
lineno,
(msg ? msg : "") );

int retval = ::MessageBox( NULL,
buf,
"Assertion Failure",
MB_ABORTRETRYIGNORE | MB_TOPMOST |
MB_SETFOREGROUND | MB_TASKMODAL | MB_ICONERROR );

bool fShift = (::GetKeyState( VK_SHIFT ) & 0x8000) != 0;

if( retval == IDABORT )
{
return Abort;
}
else if( retval == IDIGNORE )
{
return (fShift) ? IgnoreAlways: Ignore;
}
else
{
return Break;
}
}


#endif // _DEBUG


Enjoy!
Awesome. Thanks! :wink:
ppcStudios wrote:If you want to email me the project I'll give it a try.


ok here are my codes... i hope someone could look at them.. there are two folders there.. one is for the sprite problem wherein there is always an error that comes up... the second is my tic tac toe program... i just put it there so that maybe someone could take a look at it and see if it could be optimized... hope someone could help....thanks....
Attachments:
myCodes.zip (101 KB)