int TileDimension = 32;for(y = 0; y < MapSizeY; y++) { for(x = 0; x < MapSizeX; x++) { // Randomly select a number for the tile RandTile = rand() % 190; // Get the position of the random tile g_SrcRect.x= (RandTile * TileDimension) + RandTile; g_SrcRect.y= 0; g_SrcRect.w= (g_SrcRect.x + TileDimension); g_SrcRect.h= TileDimension; // set the position of the next tile g_DstRect.x = x * TileDimension; g_DstRect.y = y * TileDimension; g_DstRect.w = g_DstRect.x + TileDimension; g_DstRect.h = g_DstRect.y + TileDimension; // Draw the tile to the Map Surface SDL_BlitSurface(g_pTileSurface, &g_SrcRect, g_pMainSurface, &g_DstRect); } }
SDL 2D Tile Engine Question
ok I just don't get it. Here's the routine I use for drawing.
Try this for your source rectangle:
Note that in SDL_BlitSurface(), width and height in the source rectangle specify the dimensions in pixels of the area to draw, not the coordinates of the lower right hand corner. They are ignored in the dest rectangle. Note also that this above does not necessarily take into account any pixel borders around the tiles; you'll have to tweak to compensate.
g_SrcRect.x= (RandTile % 19) * TileDimension;g_SrcRect.y= (RandTile / 19) * TileDimension;g_SrcRect.w= TileDimension;g_SrcRect.h= TileDimension;
Note that in SDL_BlitSurface(), width and height in the source rectangle specify the dimensions in pixels of the area to draw, not the coordinates of the lower right hand corner. They are ignored in the dest rectangle. Note also that this above does not necessarily take into account any pixel borders around the tiles; you'll have to tweak to compensate.
AH! that's why it wasn't working, I totally forgot about the border around my tiles. Thanks for all the help!
Glad you figured it out, and sorry I mis-understood.
Its coming along great now, thanks for asking. :) We kinda had a 2-month hiatus because everyone was so busy with school and other things during October/November, but I only have a week left (and no finals woo!) so I've been working like MAD on it for the past week. I've honestly never been so happy to be able to write C++/Lua code. ^_^ And we got a new site design coming real soon that's going to beat the pants off the old one :D
Quote:Original post by Eric Poitras
btw Roots, how's your game comming along?
Its coming along great now, thanks for asking. :) We kinda had a 2-month hiatus because everyone was so busy with school and other things during October/November, but I only have a week left (and no finals woo!) so I've been working like MAD on it for the past week. I've honestly never been so happy to be able to write C++/Lua code. ^_^ And we got a new site design coming real soon that's going to beat the pants off the old one :D
Why doesn't my code work? It worked until I put in putmap and makemap... now the map shows (if i remove the comments) but the map surface apparently has nothing blitted on to it :(
#include
#include
SDL_Surface *screen;
SDL_Surface *bush;
SDL_Surface *grass;
SDL_Surface *map;
void showthing(SDL_Surface *img, int x, int y);
void drawmap(int height, int width);
void init_tiles();
void makemap(SDL_Surface *img, int x, int y);
void putmap (int x, int y);
int main(int argc, char *argv[])
{
Uint8* keys;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
init_tiles();
drawmap(10,10);
putmap(40,40);
SDL_Flip(screen);
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
}
return 0;
}
void showthing(SDL_Surface *img, int x, int y ) {
SDL_Rect dest; //always blit things to rects
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest); //first the image, then piece u want (as rect)
//Null=all
//then the surface u want to blit it to (screen)
//then the dest. (h, w, x, y and so forth)
}
void init_tiles(){
bush = SDL_LoadBMP("bush.bmp");
grass = SDL_LoadBMP("grass.bmp");
}
void drawmap (int height, int width){
int x,y,tilex,tiley;
int tilesize = 16;
for (tiley=0;tiley y=tiley*tilesize;
for (tilex=1;tilex x=tilex*tilesize;
if ((x%10 + y%5) == 9) {
makemap(bush, x, y);
//showthing(bush, x, y);
}
else {
//showthing(grass, x, y);
makemap(grass, x, y);
}
}
}
}//end func
void makemap(SDL_Surface *img, int x, int y){
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, map, &dest); //first the image, then piece u want (as rect)
//then the surface u want to blit it to (screen)
//then the dest. (h, w, x, y and so forth)
}
void putmap(int x, int y){
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(map, NULL, screen, &dest);
}
#include
#include
SDL_Surface *screen;
SDL_Surface *bush;
SDL_Surface *grass;
SDL_Surface *map;
void showthing(SDL_Surface *img, int x, int y);
void drawmap(int height, int width);
void init_tiles();
void makemap(SDL_Surface *img, int x, int y);
void putmap (int x, int y);
int main(int argc, char *argv[])
{
Uint8* keys;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
init_tiles();
drawmap(10,10);
putmap(40,40);
SDL_Flip(screen);
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
}
return 0;
}
void showthing(SDL_Surface *img, int x, int y ) {
SDL_Rect dest; //always blit things to rects
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest); //first the image, then piece u want (as rect)
//Null=all
//then the surface u want to blit it to (screen)
//then the dest. (h, w, x, y and so forth)
}
void init_tiles(){
bush = SDL_LoadBMP("bush.bmp");
grass = SDL_LoadBMP("grass.bmp");
}
void drawmap (int height, int width){
int x,y,tilex,tiley;
int tilesize = 16;
for (tiley=0;tiley y=tiley*tilesize;
for (tilex=1;tilex x=tilex*tilesize;
if ((x%10 + y%5) == 9) {
makemap(bush, x, y);
//showthing(bush, x, y);
}
else {
//showthing(grass, x, y);
makemap(grass, x, y);
}
}
}
}//end func
void makemap(SDL_Surface *img, int x, int y){
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, map, &dest); //first the image, then piece u want (as rect)
//then the surface u want to blit it to (screen)
//then the dest. (h, w, x, y and so forth)
}
void putmap(int x, int y){
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(map, NULL, screen, &dest);
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement