Page 1 of 1

Level Editor in SDL

Posted: Sat Nov 01, 2008 2:13 pm
by KuramaYoko10
Hey there,

I am new here on the forum but an old watcher of the "Adventures in Game Development", and a big fan of the game and team xD.

Well, I am making a "mario type" of game, and I am needing help with the level editor. I followed the lazyfoo's tutorial, and I have done some modifications to it, but I am having problem on selecting the tile.

I am using C and SDL, and what I am basic doing so far is:
-I load everything up and display the tiles on the right side of my window.
-Each one of the tiles have a "code"
-This code is used to render just that "square tile" from the whole tilesheet

Code: Select all

const int TILE_GRASS = 0;
const int TILE_SAND = 1;

bool clipTiles()
{
    clips[ TILE_GRASS ].x = 0;
    clips[ TILE_GRASS ].y = 0;
    clips[ TILE_GRASS ].w = TILE_WIDTH;
    clips[ TILE_GRASS ].h = TILE_HEIGHT;

    clips[ TILE_SAND ].x = TILE_WIDTH * 1;
    clips[ TILE_SAND ].y = 0;
    clips[ TILE_SAND ].w = TILE_WIDTH;
    clips[ TILE_SAND ].h = TILE_HEIGHT;
.
.
.
}

int setTileSheet()
{
	DrawIMG(938, 50, grid, screen, NULL);

	offX = 943;
	offY = 55;
	
	tile = 0;
	z = 1;
	i = 1;

	for( z = 1; z <= TILE_SHEET_Y; z++ )
	{
		tileCellY = offY;
		
		if(z > 1)
			tileCellY = offY + (38 * (z-1));

		for( i = 1; i <= TILE_SHEET_X; i++ )
		{
			tileCellX = offX;
			if(i > 1)
				tileCellX = offX + (40 * (i-1));

			[b]DrawIMG(tileCellX, tileCellY, tileSheet, screen, &clips[ tile ]);[/b]
			tile++;
		}
	}

	SDL_Flip(screen);
}
My problem is that when the program reaches the end of the "render tiles" loop the 'tile' variable is only located at the last tile rendered... all the other tiles doesnt have a 'tile' value, then when I try to read that from my selectTile() function, it can select only the last tile.

I tried rendering the tilesheet of many ways, but everyone of them produces the same problem!!

How should I do to do this : render all the tiles and when I select them it returns the 'tile' value!?


Sorry if I was confusing, please ask if you need more details!
Thanks in advance!

Re: Level Editor in SDL

Posted: Sat Nov 01, 2008 3:04 pm
by M_D_K
you could store the code with the UV coords in clips.

Code: Select all

struct TileInfo
{
        int code;
        int u, v;
}
That way you can go through the clips and find the tile your after

Code: Select all

int TileIndex(int code)
{
       //MAX_TILES - Max number of tiles calculated from the sheet width and height and tile width and height
       for(int i = 0; i < MAX_TILES; ++i)
       {
               if(clips[i].code == code)
                      return i;
       }
       return -1; //fail
};

Re: Level Editor in SDL

Posted: Mon Nov 03, 2008 8:49 am
by KuramaYoko10
Hey M_D_K thanks for the reply... and now I have fixed it xD

I have set a struct for the Tile, and a different way of coding the tiles and the returns:

Code: Select all

struct Tile
{
	int x;
	int y;
	int w;
	int h;
	int code;
}tileClips[TILE_SHEET_T];


void clipTiles()
{
	clips[0].x = TILE_WIDTH * 0;
	clips[0].y = 0;
	clips[0].w = TILE_WIDTH;
	clips[0].h = TILE_HEIGHT;

	tileClips[0].x = offX;
	tileClips[0].y = offY;
	tileClips[0].w = TILE_WIDTH;
	tileClips[0].h = TILE_HEIGHT;
	tileClips[0].code = 0;


	clips[1].x = TILE_WIDTH * 1;
	clips[1].y = 0;
	clips[1].w = TILE_WIDTH;
	clips[1].h = TILE_HEIGHT;

	tileClips[1].x = offX + (40 * 1);
	tileClips[1].y = offY + (38 * 0);
	tileClips[1].w = TILE_WIDTH;
	tileClips[1].h = TILE_HEIGHT;
	tileClips[1].code = 1;

.
.
.
}

//I am going to change the tiling display function, implementing a  "for loop" later
void setTileSheet()
{
	DrawIMG(938, 50, grid, screen, NULL);

	code = 0;
	if(code == 0)
	{
		tileCellX = offX;
		tileCellY = offY;
		DrawIMG(tileCellX, tileCellY, tileSheet, screen, &clips[ code ]);
	}
.
.
.
}

//And now fixed
int getTile()
{
	SDL_GetMouseState(&mouseX, &mouseY);

	tileCellX = mouseX;
	tileCellY = mouseY;


	for(i = 0; i < 4; i++)
	{
		if((tileCellX > tileClips[i].x && tileCellX < tileClips[i].x + TILE_WIDTH) && (tileCellY > tileClips[i].y && tileCellY < tileClips[i].y + TILE_HEIGHT))
		{
			code = tileClips[i].code;
				return code;
		}
	}
}


I am working on the applyTiles() function now, and I am going clean the code later =D

Thanks again!