Page 1 of 1

Displaying Sprites

PostPosted: Mon Jul 23, 2012 9:34 pm
by mothdragon
Okay, so this isn't all of the executable code, but it is the pertinent stuff. All of this code, in my mind should result in the displaying of a 72x72 graphic, from a tile sheet where the tile in question is from (0,0) - (71,71)... I've adapted this from the KBState sample, but so far as I could tell I kept everything from that sample that was needed, so why is it that my graphic isn't shown? Note that there are some variables included here that aren't used yet... Knowing me it's some tiny little oversight...

Code: Select all
import "ecere"

#define TILESIZE 72

BitmapResource tilesetGfx { ":Tileset.png" };

class TileObject
{
   BitmapResource res;

   void Render(Surface surface)
   {
      Bitmap bmp = res ? res.bitmap : null;
      if(bmp)
      {
         surface.Blit(bmp, x, y, (frameStep * TILESIZE) + srcX, srcY, TILESIZE, TILESIZE);
      }     
      if(Animate)
         if(++frameStep >= numFrames) frameStep = 0;
   }

public:
   int x, y;
   int srcX, srcY;
   bool Animate;
   int numFrames;

   int tileType;
   bool visited;
   int dir[4];

   int frameStep;
   Uint32 curTime, lastTime;
}


class FloorObject : TileObject { res = tilesetGfx; srcX = 72; srcY = 0; }
class BushObject : TileObject { res = tilesetGfx; srcX = 0; srcY = 0; }
class ExitObject : TileObject { res = tilesetGfx; srcX = 144; srcY = 0; Animate = true; numFrames = 4; }

class GameWindow : Window
{
   background = black;
   clientSize = { 1024, 768 };
   autoCreate = false;

   BushObject bush {100, 100};

   bool OnCreate()
   {
      return true;
   }

   bool OnLoadGraphics()
   {
      AddResource(tilesetGfx);
      return true;
   }

   void OnUnloadGraphics()
   {
      RemoveResource(tilesetGfx);
   }

   void OnDestroy()
   {

   }

   void OnRedraw(Surface surface)
   {
      bush.Render(surface);
   }

   bool OnKeyHit(Key key, unichar ch)
   {
      return true;
   }
}

Re: Displaying Sprites

PostPosted: Mon Jul 23, 2012 10:15 pm
by jerome
Hi Charlie,

I've tried this with a random bitmap and I did see a tile show up.

Could you please attach your exact code and bitmap, always easier to help you find what the problem is :)

I had to add a 'GameWindow gameWindow { };' instance , change the file name to the graphics I was using, and comment out the autoCreate = false...

Regards,

Jerome

Re: Displaying Sprites

PostPosted: Mon Jul 23, 2012 11:08 pm
by mothdragon
I suppose that's reassuring, but that really confuses me... I've attached a .7z of my project.

**Correction, I attempted to attach the .7z file... but I have no idea where it went, and no link appeared in my post. I'll email it to you directly.

Re: Displaying Sprites

PostPosted: Tue Jul 24, 2012 12:24 pm
by jerome
I attached the .7z you sent me to the post. Don't know why it worked for me?

I figured out what the problem was.
It's a bit tricky. If you take out MainProg.ec of the project, and then add it back at the end of the project, the bush will display fine.

The problem is that the MainFrame program was being instanciated before the tilesetGfx, and so when doing res = tilesetGfx, res was null.
Thus the order in which instances are created became important... Ideally the order of files in the project shouldn't matter, but this is still something I need to improve :)

I found this out by putting a breakpoint in TileObject::Render and looking at the value of 'bmp' and 'res' (null).

Another way would be to move the program instance after tilesetGfx in game.ec (You would need to add import "MainProg" at the top), or move tilesetGfx before program in MainProg.ec (You would need to add import "Game" at the top).

Regards,

Jerome