PDA

View Full Version : Reading Sprites....



Whitfurrows
12-08-2003, 11:09 AM
Hi everyone....I am a newcomer in the world of Games programming hence the question that follows... :?

How can you read off sprites of a sprite library such as http://www.arifeldman.com/free/downloads.html ?

Thanks for your time and sorry for the daft question... :oops:

Alimonster
12-08-2003, 12:33 PM
It's not a daft question -- we all have to start somewhere!

The process of using those sprites can be divided into these steps:

1) Grab the images from a file and load it into memory
2) Find a way to specify each sprite's position as a rectangle/square (up to you!) of the larger bitmap
3) Draw the things at some point by copying the wanted rectangle from the big bitmap onto the screen (or somewhere else)

There are a great many ways to achieve this since there are lots of different methods for drawing stuff (e.g. Direct3d, DirectDraw, OpenGL, GDI, GDI+, and others I forgot...). It's not difficult though.

The simplest method is to use the TBitmap class. You create a bitmap (and free it once finished) and use the LoadFromFile method to get the _entire_ bitmap into memory. You can then decide what sprites you need -- this is a case of specifying a rectangle (TRect) using the Bounds or Rect functions. You'd pass the coordinates of your sprite when you want to draw it using the CopyRect method available in TCanvas.

If it makes things simpler, you could just cut-and-paste each sprite into many small, individual bitmaps manually (with Paint or whatever) rather than having them all crammed into one tileset. It's your call, as always :).

Here's some quick example code:

// the following is used for the characters
// in the game -- a creature with a position (x, y) on
// the screen and a picture (specified by the coords)
type
TMonster = record
x, y: Integer; // current screen position
PicCoords: TRect; // rect of the picture in overall bitmap
end;

procedure TYourForm.Blah(const Monster: TMonster);
var
Bmp: TBitmap; // stores the large image w/ lots of sprites
begin
Bmp := TBitmap.Create;
try
Bmp.LoadFromFile('somebitmap.bmp'); // load your sprites into memory

// copy this monster onto the screen, grabbing the
// picture as a small rectangle of the big bitmap. Make sense?
// assuming that the sprites are 32 by 32 pixels, change appropriately
Canvas.CopyRect(Bounds(Monster.x, Monster.y, 32, 32), Bmp.Canvas, Monster.PicCoords);
finally
Bmp.Free;
end;
end;
Of course, you'd want to initialise some of your monsters with positions and so on, but that demonstrates the basic idea.

There are variations on this of course. You can store the sprite-coords-within-larger-pic as an array of TRects, then have the monster record store an index instead of the coordinates directly. This would save space if you have lots of monsters with the same picture.

The change would look like this:

type
TMonster = record
x, y: Integer; // current screen position
Picture: Integer; // index of the monster's picture
end;

Coords: array[0..MonsterCount - 1] of TRect; // each sprite you want

// init the coords somehow, maybe
// ... LoadCoordsFromFile(Coords); // write this function :)
// or Coords[0] := Rect(0, 0, 32, 32); Coords[1] := Rect(32, 0, 64, 32);

// then later, when drawing...
Canvas.CopyRect(Bounds(Monster.x, Monster.y, 32, 32), Bmp.Canvas, Coords[Monster.Picture]);

If the above isn't clear then let me know and I'll write a small example app to demonstrate what I mean.

You'd usually only create and load the bitmap at the start, keeping it in memory for as long as you can. At the end, you'd free it. This may well mean creating + loading in the form's OnCreate method and freeing in the OnDestroy. You should hopefully understand the reason why -- avoid unnecessary work each update if you can.

Also, the different graphics libraries will function much like the above. In most cases, there's something like the TBitmap class (like an IDirectDrawSurface7 for DirectDraw, a texture in OpenGL, and so on). There will be a "stores pictures and things" class, and some way to copy smaller parts of it (like Blt in DDraw).

You can also copy the smaller parts of the bitmap into an array and use those pictures directly, instead of with CopyRect. In other words:

type
TMonster = record
x, y: Integer;
Pic: Integer; // index to TBitmap for the sprite
end;
Pics: array[0..TotalSprites - 1] of TBitmap;

procedure CreateThePics;
var
i: Integer;
MainBmp: TBitmap;
begin
MainBmp := TBitmap.Create;
MainBmp.LoadFromFile('c:\wherever.bmp');

for i := Low(Pics) to High(Pics) do
begin
Pics[i] := TBitmap.Create;
Pics[i].Width := SpriteSize; // 32 or whatever
Pics[i].Height := SpriteSize; // 32 or whatever
// Use CopyRect to copy from the big bitmap into this small one
// remembering that you need some way to specify the coords.
end;
MainBmp.Free;
end;

Keep in mind that TBitmap/TCanvas won't produce the best results, but they'll do for starters. You can have a look at http://terraqueous.f2o.org/dgdev/viewtopic.php?t=199 for a tutorial on TBitmap, etc., before moving onto cooler APIs (see the huge amount listed on dgdev's main page and pick one ;)).

The main challenge is specifying the coordinates for your sprites. You can do this either by putting the coordinates into a hard-coded array or by loading them from file (better, since you can change stuff without recompiling the program). It's still pretty tedious work whatever you choose, though!

Whitfurrows
12-08-2003, 05:28 PM
hei....thnx a lot Alimonster... :D ....i ll put it to work right away and see how it goes.....
otherwise i ll be posting more questions soon.... :shock:

thks again..

Whitfurrows
24-08-2003, 04:02 PM
...If the above isn't clear then let me know and I'll write a small example app to demonstrate what I mean.

...

Right! I have been working on the thing for the past week and i am not too sure about the whole thing....
If you could create that application u were talking about that would be really great. Thanks Alimonster

Traveler...:

I have tried your first tutorial on creating a Game Framework and although i did follow everything and downloaded all the required files i only seem to get a Black screen when i compile the code.

I am using DirectX 9.0 with Delphi 7 and WinXP....thnx :oops:

Traveler
25-08-2003, 08:17 AM
I have tried your first tutorial on creating a Game Framework and although i did follow everything and downloaded all the required files i only seem to get a Black screen when i compile the code.

Thanks for using my tutorials :)
I'm not sure what is causing your problems. It might be the DelphiX version (since you're using Delphi7) or perhaps it's something you forgot to include in the code.

If you want to I can have a look at your code, see what's wrong with it.

Whitfurrows
25-08-2003, 05:12 PM
Traveler, that would be great but i do not have webspace to upload....I ll see if i can get some free temporary hosting to upload it and I ll let you know....thnx :P

iLLUNis
25-08-2003, 05:20 PM
If you want i could upload it for you in my website.....just PM me or email me at postmaster@illunis.net :lol:

iLLUNis
25-08-2003, 06:01 PM
Got the code....it can be found here
http://www.illunis.co.uk/Temp/framework.rar

Traveler
25-08-2003, 07:28 PM
Traveler, that would be great but i do not have webspace to upload....

:D I probably should have been more clear, but it was alright for you to e-mail me the code, instead of uploading it to a website. I any case, I received the files. Thanks for mediating iLLUNis!

I have looked through your code and noticed a minor error. You have misplaced some of the code. Instead of placing the code in the onTimer event, you have placed it in the onActivate event. If you change this, then it should work correctly.

iLLUNis
25-08-2003, 08:01 PM
no prob... :wink:

Alimonster
27-08-2003, 09:38 AM
Right! I have been working on the thing for the past week and i am not too sure about the whole thing....
If you could create that application u were talking about that would be really great. Thanks Alimonster
I'll see what I can whip up tonight. Sorry about the little delay -- I've been watching movies on my PC rather than reading forums, etc.

Alimonster
01-09-2003, 09:24 PM
I'd like to apologise for the delay once more. I started working on some example code and realised that I was making a tool that might be useful for some other stuff that I'm doing (tutorials et al). I've been working on-and-off so that the app is more solid, and it's slowly getting there (pic (http://www.alistairkeys.co.uk/images/coords.png)). The idea is that you'll be able to choose a tileset and then add your own rectangles, resizing them appropriately. You can then save all the coords to a file for easy loading.

It's still a day or two from completion. I'll try not being so lazy and will hopefully finish it real soon, after which I can get on with showing you some decent examples! :)

Whitfurrows
01-04-2004, 09:19 PM
Hi there....it has been a while since last visited the forum and i have to say that i am totally dissapointed....if i was going to wait for a reply to my question i would still be in the dark..The last post in my question was on September 2003!!!!!!.....and it was supposed to be a day or two from completion.... :?
I can see that everyone is more or less busy at the moment with the new developments of the site but i would expect that when someone says that can deliver something to deliver it.... :shock:
I would like to know the opinions of other newbies on the subject....thnx again...

cya all

Alimonster
01-04-2004, 11:19 PM
Frankly, I forgot about completing this utility due to commitments at work and you didn't remind me. If you'd sent me a private message then I would have remembered about it (it's not the first time I've forgotten about a task and it won't be the last - those who don't ask don't get).

I guarantee that I'll upload a beta version of the thing tomorrow evening - it may well contain bugs, but it will be there.

Eriken
01-04-2004, 11:51 PM
No need to be annoyed about it, noone here gets paid to answer anything to spend time helping anyone and from what I can see in this thread people have been trying to help.

Nobody guaranteed anything either, it said "hopefully" as far as I can see, and people happen do things like work here and can forget. If you don't check in for 7 months people might just think you didn't bother and they focused on something else, I know I would have done so.

Time passes, goals changes, people change, their situation changes, that's life get used to it.


I would like to know the opinions of other newbies on the subject....thnx again...

Hi, I'm Eriken, and I'm a newbie..

The guys here are great, and I've learned loads from them.. Be thankful and ask more, instead of complain, and they might even want to help you next time.
_____
Eriken

---- It's past midnight, I ran out of sympathy several hours ago ----

Whitfurrows
02-04-2004, 08:52 AM
Hi again,
I am actually logging in quite often but simply i dont post. I have learned in my life not to pressure people by asking something all the time. If i get a positive reply then i simply wait for a result.
I never said that the guys are not great. What i am saying is that u should more effort in order to attract more newbies in this wonderfull programming languages.

Alimonster
02-04-2004, 09:29 AM
Hi again,
I am actually logging in quite often but simply i dont post. I have learned in my life not to pressure people by asking something all the time. If i get a positive reply then i simply wait for a result.
I never said that the guys are not great. What i am saying is that u should more effort in order to attract more newbies in this wonderfull programming languages.
Thank you for not wanting to pressurize me, but really - I need it. ;) I spread myself too thin when it comes to trying to help, so sometimes I simply forget to finish tasks (I think there's another thread at DGDev somewhere where I did exactly the same - something to do with optimizing some code, possibly started by Tux). For future reference, if it seems like I've not responded for a while after saying I would (maybe after a four or five day delay), then I should be private messaged because the most likely reason is that I forgot.

I'd like to apologise for my slackness here. Rest assured, we do try to help where we can, and I don't always suck at trying to help others...

We do try to help newbies where possible. I'll turn this topic (basic animation for sprites) into a tutorial if you want.

Alimonster
03-04-2004, 10:20 AM
Here's the very first (probably buggy) version (http://www.alistairkeys.co.uk/test/anim.php). Expect oddities like buttons disabling or being enabled at inappropriate times, since I've not thoroughly tested any of it. I'll enhance it and fix it up later - it's not really suitable for anything except a quick look around the interface, although it can save and load animations.

Future enhancements include:

* resizing anchors for the selections
* better preview controls (timeline, forward/back, etc.)
* fix any outstanding bugs
* file history
* anything else I can think of
* make the web page nicer!

The idea is that you load a tileset (e.g. one from here (http://www.arifeldman.com/games/spritelib.html)). You then add invididual rectangular selections for individual pictures within the tileset. You can create sequences for animation - each sequence has frames, which can be displayed in order. Clear as mud, no?

Whitfurrows
05-04-2004, 05:03 PM
thnx Ali...i ll look into it... :D