GP32 Tile Engine Code


mystic_twin

Still Fresh
Joined
Jul 2, 2003
Messages
30
Hello,

I have been working from Rico's tutorial on how to construct a simple tile engine for a couple of days now, but I find it really difficult to learn from that sort of thing.

I was really hoping that somebody has either the full source code for rico's tile engine, or maybe the source code for an alternate tile engine. this way i can see how it all works and learn from that.

Any help would be really appreciated.

Thanks :)

Twin.
 
what is it you dont know exactly?

the fundimentals of a tile engine are pretty simple, you have a huge map which is a grid of tiles (say 16x16 pixels each), this is normally stored in a 3 dimensional array, such as MAP[x][y].. so if your program wants to know what tile is at 10,5 - it reads it from MAP[10][5].

and then you want to specify the area which is being viewed, camera_x and camera_y.

so now you have a map array, and some camera cords you just need to code it so it works out what tiles to draw.

basically you dont want it drawing the whole map do you? as only a small part of the map will be on the screen, so instead you work out what tiles are on the screen and just draw thoose.

int draw_from_x = (camera_x / tile_size)
int draw_from_y = (camera_y / tile_size)

^ dividing is a cheap way of finding out what tile to start with, but it works.

so now you know where to begin drawing the tiles from, you just need to work out when to stop drawing the tiles.

all you need to do now is work out how many tiles fit on a screen and add that onto the 'draw from' and you'll get where to stop drawing the tiles (its worth adding 1 to the value)

erm.. then you just do a loop like:

for (x = tile draw_from_x; x < draw_to_x; x++) {
for (y = tile draw_from_y; y < draw_to_y; y++) {

Now, it'll keep looping this for every tile which is on screen, so we need to then draw it on the screen, before we do that however we need to calculate where the tile will be on the screen, based on the scroll position.

unsigned int draw_to_x, draw_to_y;
draw_to_x = (x * tilesize) - camera_x
draw_to_y = (y * tilesize) - camera_y

then just plonk your graphics code here to draw the tile to thoose cords on the screen.
}
}


just add some basic input to change the camera_x and camera_y values and your sorted.

if that of any help?
 
Pirotic,

Thanks for your explanation above, your help is really appreciated. But although it has been of help to me, i am still a bit stuck.

You see my problem is'nt so much understanding what the game needs to do, but more how it does it. I find it quite difficult to understand tutorials, but If i'm given the source code, i can deconstruct it and figure out what does what etc... and i find it alot easier, don't ask. :blink:

Anyways, if you have or know of any full code, that would be great.

Thanks. :)

Twin.
 
Alright.

http://www.thaworx.co.uk/gpunk.rar - Here's the source to GPunk. It's a Zelda-perspective tile-based shooter with basic collision detection. You can fire in four directions with R, A, B and Start. Select reboots. L launches a (badly drawn) grenade. D-pad moves independent of fire. There's an actor system and some other stuff, but it's largely just you walking around and firing. Code is sloppy in places where I was planning to rewrite it. Basically, it's tutorial #3's code plus a few additions. If you're looking to 'deconstruct' you should be happy. If you want someone to write your engine for you, this will not be suitable :)

I haven't checked this code much, but it should compile and run. You won't be able to change the graphics without reconverting the original BMPs, but since this is just an engine for you to look at code-wise, I don't think it's neccessary. Player graphics are a 6x4 grid, 4 directions and 6 animation frames for each. Tile graphics are a big strip of tiles like you see in my tutorial.

If I was still actively in the scene I might have worked on this, but at the moment it sits with the rest of my unfinished projects (Night of the Ninja anyone?).

- Rico
 
Thanks guys, :D

This will really help.

and Rico, thanks for the code and thanks for the compiler, if it was'nt for your ready prepared DevKitAdv.rar i don't think i would be coding at all.

well, i'll check it all out and see what i can learn from it.

regards,

Twin.
 
Back
Top