Sign in to follow this  
rogerdv

problem with isometric drawing

Recommended Posts

rogerdv    418
I got a piece of code to draw a map in isometric view. But what I got is just one strip of squares Here is the code:
void DrawMap()
{
int tile1, cameray, camerax;
int z, x;
SDL_Rect dest, src;
Uint32 colorkey;
camerax=400;
cameray=300;
src.x = 0;
src.y = 0;
src.w = TILE_WIDTH;
src.h = TILE_HEIGHT;

colorkey = SDL_MapRGB(tiles->format, 0, 0, 0);
SDL_SetColorKey(tiles, SDL_SRCCOLORKEY, colorkey);
              
for (z = 0; z < 5; z++)
    {
    dest.y = z * -TILE_HEIGHT/2 + cameray;
    dest.x = z * -TILE_WIDTH/2 + camerax;
    dest.w = TILE_WIDTH;
    dest.h = TILE_HEIGHT;

    for (x = 0; x < 5; x++)
     {
       // draw tile
   
       
     /*
     dest.x = TILE_WIDTH * x;
     dest.y += (TILE_HEIGHT * y) /2;
     dest.w = TILE_WIDTH;
     dest.h = TILE_HEIGHT;*/
       
       SDL_BlitSurface(tiles, &src, screen, &dest);

       // update location
       dest.y += TILE_HEIGHT/2;
       dest.x += TILE_WIDTH/2;
       }
    }
}    

Can somebody identofy what is the error here?

Share this post


Link to post
Share on other sites
JDUK    100
This forum (the isometric one) moves SLOWLY! and i have had trouble getting replies to my isometirc engine questions.
May i suggest commenting your code, it will make it far more readable for people willing to help you.

Also are you trying to make a dimond shape map or the "staggered row" map (for want of a better phrase)?

Share this post


Link to post
Share on other sites
Solias    564
Outer Loop:
Quote:

for (z = 0; z < 5; z++)
{
dest.y = z * -TILE_HEIGHT/2 + cameray;
dest.x = z * -TILE_WIDTH/2 + camerax;



z | d.x | d.y
0 | 0.0 | 0.0
1 |-0.5 |-0.5
2 |-1.0 |-1.0
3 |-1.5 |-1.5
4 |-2.0 |-2.0

* x is in unit of TILE_WIDTH, y is in units of TILE_HEIGHT, relative to
(camerax, cameray) which is constant over the loop.


Inner Loop:
Quote:


for (x = 0; x < 5; x++) {
...
dest.y += TILE_HEIGHT/2;
dest.x += TILE_WIDTH/2;




z = 1
x | d.x | d.y
0 | 0.0 | 0.0
1 |-0.5 |-0.5 *
2 |-1.0 |-1.0 *
3 |-1.5 |-1.5 *
4 |-2.0 |-2.0 *

z = 2
x | d.x | d.y
0 |-0.5 |-0.5 *
1 |-1.0 |-1.0 *
2 |-1.5 |-1.5 *
3 |-2.0 |-2.0 *
4 |-2.5 |-2.5


Note that lines marked with * are duplicates. Basicly you are drawing your tiles on top of each other over and over. The correct drawing method will depend on if your map is stored in screen space or in a rotated world space (this is refered to as a "diamond map").


In either case you want to loop over your map in 2d and draw each tile in screen space.


for(x = 0; x = width; x++) {
for(y = 0; y < width; y++) {
position = map_to_screen_transfrom(x, y);
}
}


Your actual transfrom will depend on your map representation and parameters such as screen size, tile size, etc. If you post which map representation you are using, someone should be able to help you more. The screen space ("rectangle map") transform is very easy, but will make game logic more difficult later. The rotated world space ("diamond map") transform is a little more tricky, but makes game logic easier. There are some good posts on working with both of these representation in this forum, although the search is a little hard to use so you may need to hunt around a bit.

Here are a couple threads to get you started:
http://www.gamedev.net/community/forums/topic.asp?topic_id=309720&whichpage=1
http://www.gamedev.net/community/forums/topic.asp?topic_id=310213&whichpage=1
http://www.gamedev.net/community/forums/topic.asp?topic_id=322653&whichpage=1





Share this post


Link to post
Share on other sites
JayeAeotiv    138
You could try this:

You are stepping in the x AND y direction in the same iteration.

so you are saying....

puthere, go right, go down, put here, go right, go down...

x
 x
  x
   x
    x
is what you get.

Try doing this...


for(int y=0; y < 5; y++)
{
for (x = 0; x < 5; x++)
{
// draw tile


/*
dest.x = TILE_WIDTH * x;
dest.y += (TILE_HEIGHT * y) /2;
dest.w = TILE_WIDTH;
dest.h = TILE_HEIGHT;*/


SDL_BlitSurface(tiles, &src, screen, &dest);

// update location
dest.x += TILE_WIDTH/2;
}
dest.y += TILE_HEIGHT/2;
}



You should get something closer to

xxxxxx
xxxxxx
xxxxxx
...etc

Without being able to test your code, it's a little harder...

In theory you'll want something like...

for Z
{
for Y
{
for X
{
Blit x,y,z
update x
}
update y
}
update z
}

Share this post


Link to post
Share on other sites
rogerdv    418
Im storing the map in an array like this:
x
y 0 0 0 0 0
1 1 0 0 0
0 0 1 0 0

It is a bit clearer for me to edit the map by hand.
I will try the solutions and post the results here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this