PocketMatrix forums

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

'OO' C++ programming with PocketFrog

I am writing an article which needs to contain a project for sprite animation. I wanted to define a Sprite Class so that all the definitions & processing for sprite animation were shown in a modern way ( everything in my non 'OO' demo works perfectly ).

Because I know very little about C++ OO design I can't get my class working properly. After many attempts I can get a class defined as a nested class within MyGame in Blit but the problems I still have are:-

1. I Don't know how to get my class to do the loadimage inside the class so currently I am creating an instance of Sprite in GameInit() containing a surface & then doing the load image in the next line
Code:
Sprite cave(0,240,4,20,FALSE);
cave.image = LoadImage(display,IDB_CAVEMAN);
This is not too big a problem

2. The Sprite defined in GameInit does not exist in GameLoop so I can't use it.

It may be that I can't use a nested class in the way I'm trying to do. As I said my non OO code all works fine and I have added various functions inside Blit which work perfectly ( for SIP keyboard & Starfield )
but I seem to be stuck trying to use a class.

Any advice would be most appreciated. I would rather not force my 1980's programming tecniques onto new coders.
Conan,

I'm doing something like this:

Define a class:

Code:
class Player
{
public:
void Draw(DisplayDevice* display);
void SetSpriteData(DisplayDevice* display, Surface* sprites);
Player();
virtual ~Player();

private:
Surface* SpriteData;
Rect framearray[5];
};


Here's the implementation for SetSpriteData and Draw:

Code:
void Player::SetSpriteData(DisplayDevice* display, Surface* sprites)
{
SpriteData=display->CreateSurface(sprites->GetWidth(),sprites->GetHeight());
display->SetRenderTarget(SpriteData);
display->Blit(0,0,sprites);
display->SetRenderTarget(0);

SpriteData->SetColorMask(0);
}

void Player::Draw(DisplayDevice* display)
{
display->Blit(Xpos,Ypos,SpriteData,&framearray[SpriteIndex]);
}


In GameInit:

Code:
runsprite = LoadImage(display,IDB_BITMAP3);
myplayer.SetSpriteData(display,runsprite);


Both runsprite and myplayer are defined as private members of class MyGame so they are available in GameInit and GameLoop. Now that I think about it, I don't see why I need runsprite anywhere else but GameInit.

This is probably not the best way to do it, but it works for me.

Hope this helps.

Pam
Conan · 26 Jun 2002, 18:59 UTC — Thanks Pam
Your code answers most of my issues. Is your class Player defined inside the Game class?

All my other variables are private members of MyGame but I could not get the sprite to work syntax wise.

I don't get what you are doing with SetRenderTarget but don't tell me. I will figure it out.
I did not define the Player class inside the Game class. Player, and the two classes I inherit from it are each in their own .h and .cpp files.

Actually, I used the Insert->New Class... menu option to create them.

I won't tell you what I'm doing with SetRenderTarget. :wink:

The more I think about it now, I think I'll try having the Player class load its own bitmap, rather than loading it in GameInit and passing it to the Player.

Pam
Conan · 26 Jun 2002, 21:49 UTC — Loading the bitmap
That's the bit I really gave up on. Hope you get it working
Ok, here's what I did.

Define the class:

Code:
class Player
{
public:
void SetDisplayDevice(DisplayDevice* display);
void LoadSpriteData(unsigned resourceID);
void Draw();
void SetSpriteData(Surface* sprites);
Player();
virtual ~Player();

private:
DisplayDevice* m_display;
Rect framearray[5];
Surface* SpriteData;
};


Here's some implementation:

Code:
void Player::SetDisplayDevice(DisplayDevice *display)
{
m_display = display;
}

void Player::LoadSpriteData(unsigned resourceID)
{
SpriteData = LoadImage(m_display, resourceID);
}

void Player::SetSpriteData(Surface* sprites)
{
SpriteData=m_display->CreateSurface(sprites->GetWidth(),sprites->GetHeight());
m_display->SetRenderTarget(SpriteData);
m_display->Blit(0,0,sprites);
m_display->SetRenderTarget(0);

SpriteData->SetColorMask(0);
}

void Player::Draw()
{
if(State==0)
{
m_display->Blit(Xpos,Ypos,SpriteData,&framearray[SpriteIndex]);
}
else if(State==1)
{
m_display->Blit(Xpos,Ypos,SpriteData,&framearray[0]);
}
}


So now in GameInit() I do:

Code:
myplayer.SetDisplayDevice(display);
myplayer.LoadSpriteData(IDB_BITMAP3);


I'm storing the DisplayDevice inside the class so that I don't have to keep passing it in, but that means you have to set it before you can use the class.

Hope this helps.

Pam
jbella · 27 Jun 2002, 08:01 UTC — Is it really the best way?
Is this really the best way to do it, having an object for each sprite, considering the limited resources of a PocketPC? Ok, it very OO and nice, but as having an object for each sprite seams like a little bit of overkill, in my project i use "managers". One TileManager, one EnemyManager etc.. Each manager holds an Array of the Tiles, Enemies etc. It doesn't "look" as good as a nice OO setup, but this way I utilize the resources more efficient than if I had an object for each.

:wink: Johan Bellander
BurningSheep · 27 Jun 2002, 09:26 UTC — Is it really the best way?
it keeps your code nice and clean (which is a big advantage), and the performance hit probably isn't that large. those managers sound like a good idea, are they classes? ;)
Conan · 27 Jun 2002, 09:45 UTC — Best way to do it ?
Hi Johan
you are correct on this point as far as efficiency and also performance goes. I'm not trying to teach gamne design however just show how PocketFrog functions work. My own code is currently 0% OO and this sprite example is being converted to use classes on the assumption that most people will understand what's happening better.
Conan · 27 Jun 2002, 10:00 UTC — Pam, one last question
Pam, with your help I am now passing in the image & display device to my Sprite class & this will allow me to get my next chapter going.

One thing I did not manage to get working was the separate .cpp & .h

If I put these files in the project separate from Blit & include Sprite.h in the Blit.cpp then nothing in the Sprite class is recognised & vice versa. If I paste the code inside the Blit source between the using namespace Frog & the MyGame class then it builds fine.

What is it that I am missing here ? It's not important now for a small demo but I will have numerous included files when I start my game conversion.

( It's taken an unbelievable 20 months for me to get to the point where C++ is starting to work for me. If I had only started C when I got my first PC instead of opting for assembly language :cry: )
jbella · 27 Jun 2002, 10:13 UTC — Managers
The Managers are based on a Manager baseclass, that handles things like Loading, CollisionMask creation etc. Then there are managers derived from that class that handles tiles/enemies etc with for example different blit methods.

Long answer for a short question, Yes the managers are classes... :P

Johan Bellander
BurningSheep · 27 Jun 2002, 10:34 UTC — Pam, one last question
Conan wrote:
What is it that I am missing here ?


The sprite class should be under the pocketfrog namespace as well and of course include pocketfrog.h
maybe I'm stating the obvious here, but I'm just making sure :) I didn't have any problem creating a class that holds a surface and blits it to the display and I always put classes in seperate files.
Conan · 27 Jun 2002, 11:33 UTC — thanks for the tip
I will try again. I was attempting to include the namespace but was not sure whether to use a 'using namespace Frog' or just 'namespace Frog' and the compiler was telling me that it did not exist. I did on one try add in the PocketFrog.h but I don;t know whether it goes in the .h .cpp or both.

My problem isn't coding, it's understanding how to use an alien environment.

Update: OK all working now. You just have to know what to put where.

Pam, Still don't know what you are doing with SetRenderTarget in SetSpriteData but then I can't tell what the function is meant to do either. As your code does not show the setting up of the indexes to the frames I thought that's what it was but it's not. You pass it a surface but what surface is this?

I will start writing my article once my code is converted to use the Sprite class but it will be well into next week before I have this ready to publish. It's nothing fancy just a couple of scrolling backgrounds & 2 sprites to show the commands in action.
Ok. Bad Pam. Slap on the wrist for posting poorly documented code. :wink:

I'll try to clean up the code and repost it with comments.

Each Sprite has a member called SpriteData which holds the bitmap for the sprite. In my case it's actually frames for the animation.

What I do in SetSpriteData() is pass a Surface that I have loaded in GameInit() from a resource.

To copy the contents of the passed-in Surface to the SpriteData surface, I change the rendering target from the default, which is the BackBuffer, to my SpriteData surface. Then I can Blit the passed-in surface onto the SpriteData surface. Then I set the rendering target back to the default.

I got this part from this thread:
http://forums.pocketmatrix.com/viewtopic.php?t=6212

I am definitely taking a peformance hit by using all these classes. But, for my purposes, the performance is still more than adequate. If I get too many sprites going at once, then I'll see it I suppose.

Pam
Conan · 27 Jun 2002, 14:12 UTC — Performance etc
Now I understand what you are doing. I was lost because you have another function which loads the surface anyway.

Re: performance it's common to write code till it functions correctly & then work on inlining those parts which happen all the time. jumping to a sub routine pushes & pops stack values but that's not a lot of processor cycles. It's normally the Blit stuff which is the killer & we are allowing PocketFrog to handle this. I have not looked at the functions copying the data much but I assume that taking machine device independance into account the code used is as optimal as possible. PocketGL ( the 3D toolkit I use ) only supports iPaq & Casio so it can be very specific about the screen routines & there's separate code for each device.