Jump to content
  • Advertisement
Sign in to follow this  
xXMr WaffleXx

Tile background not working

This topic is 2905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been basically trying to figure out how to make a tile based background for my game with an array for a little bit longer than I would have wanted. I've ran out of ideas. I'm close with no errors and think it should work, but it doesn't so if you could look my code over real quick and point me in the right direction that would be freakin sweet. Thanks in advance and sorry there aren't any comments, this is just a test and it's only one little thing of code.


#include "DarkGDK.h"

const int height = 3;
const int width = 3;
int x;
int y;
int n;

int test[height][width]=
{
1,0,0,
0,1,0,
2,0,3,
};

void mapsetup(int map[height][width]);

void DarkGDK ( void )
{
dbSetDisplayMode (1024,768,32);
dbSyncOn ( );
dbSyncRate ( 60 );
dbMaximizeWindow ();
dbSetImageColorKey (255,0,255);

while ( LoopGDK ( ) )
{
mapsetup(test);

dbSync ( );
}


return;
}
void mapsetup(int map[height][width])
{

//tiles
dbLoadImage("brickground.bmp",21);
dbLoadImage("sky.bmp",22);
dbLoadImage("red.bmp",23);

for(y = 0, n = 31; y < height, n < 9; ++y)
{
for(x = 0; x < width; ++x, ++n)
{
if (test[y][x] = 1)
{
dbSprite (21,x*32,y*32,n);
}
if (test[y][x] = 2)
{
dbSprite (22,x*32,y*32,n);
}
if (test[y][x] = 3)
{
dbSprite (23,x*32,y*32,n);
}
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
At a glance, this is probably your problem:

for(y = 0, n = 31; y < height, n < 9; ++y)

The comma operator doesn't do what you seem to expect it to do here (specifically in the y < height, n < 9 bit). What exactly do you want that loop to be accomplishing?


(Also, thread moved to For Beginners.)

Share this post


Link to post
Share on other sites
The loop should be going through the array's numbers and if a number in the array is 1 for an example the tile would be blue or something. Then the tiles are placed on the screen in the order the array has them stored.

Share this post


Link to post
Share on other sites
Yeah, I figured that much [smile] What I mean is, what is your thought process behind the code you typed in? What do you expect that code to be doing the value of, say, n, while the loop is executed?

Share this post


Link to post
Share on other sites
The int n is there and doing the n++ thing so that each tile that is placed has a different image number. The x is the rows and the y is the columns. The if statements that say if (test[x][y] = 1) then blah are basically the thing that asks if this number in the array is 1 then I will draw a tile in the spot with dbSprite while the x++ and y++ cycles through the array's numbers. Then the db sprite (21(a tile I loaded),x*32(32 is tile width*the x number for the array),y*32(same thing),n(making a different image number for each tile)) Wait the n wouldn't work because all the tiles would have the same image number if I did it that way. Overlooked the fact that n changes the image number of the tiles that are already set. Or does it? idk. Prolly does.

Share this post


Link to post
Share on other sites
I'm gonna have to redo all this in SDL anyway so I'm just gonna scrap the dbSprite thing and figure something out for SDL. But any ideas why it isn't working besides the int n thing?

Share this post


Link to post
Share on other sites
You initialize n at 31, and you run the loop as long as it's less than 9... Doesn't sound like that was what you intended to do. Either way, you shouldn't use a comma there, you should use a logical and (&&) - but you'll probably want to remove that n check anyway. There's no use for it. That, and you probably want to initialize n at 0.

Then again, you're passing n as the image ID, and 21/22/23 as the sprite ID. You should swap those.


Either way, you should store valid image IDs in a list and store indices into that list in your tilemap data. Then, just look up the image ID from that list, rather than writing a check for each different image. Or just store these image IDs directly into the tilemap data - it looks like you're able to specify an ID when loading an image anyway.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!