• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Can't clip tiles without crashing

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1DrNicholas  Members

Posted 28 June 2014 - 07:15 PM

typedef struct tiles {
int x, y;
int w, h;
} TILES;

TILES *tiles_i;

int clip_tiles() {
int y, x;
tiles_across = tileset_i->w / 16;
tiles_down = tileset_i->h / 16;
printf("%d,%d,%d\n",tiles_across, tiles_down, tiles_across * tiles_down);
tiles_i = malloc(sizeof(int) * tiles_across * tiles_down);
for( y = 0; y < tiles_down; y++ ) {
for( x = 0; x < tiles_across; x++ ) {
tiles_i[(y * tiles_across) + x].x = 16;
tiles_i[(y * tiles_across) + x].y = 16;
tiles_i[(y * tiles_across) + x].w = 16;
tiles_i[(y * tiles_across) + x].h = 16;

printf("%d\n",(y * tiles_across) + x);
}
}
}


Hello,

I am trying to clip tiles, but the game keep crashing when I run this code. At the moment, I just have every value set at 16, though that will change. tiles_across is 9, and tiles_down is 6, so tiles_across times tiles_down should be 54, right? And when I malloc tiles_i, it should have an array of 54 right? When I set the those tables manually, like:

tiles_i[53].x = 16;
tiles_i[53].y = 16;
tiles_i[53].w = 16;
tiles_i[53].h = 16;


then the game does not crash (on the side note I can go from 0 to any number past 53), though when I print (y * tiles_across) + x, it goes from 0 to 53, so that formula should not be the issue. The way my levels are parsed, 0 means nothing is there, and any number but 0 corresponds to a tile on the tilesheet. However since I know 0 means no tiles, the tile placing object has been adjusted accordingly (it's given an ID number for the tile, it just subtracts 1 to find the correct tiles_i).

Do you have an idea as to why my game crashes when I parse tiles like this? Thanks

EDIT:

I fixed it with sizeof(TILES), I got another issue at the bottom

Also, but any chance, do you think someone could help me figure out a way to parse a tile sheet, if the tiles inside are not touching eachother? like instead of this:

it's this

also that's not the tilesheet I am talking about at the top

Edited by DrNicholas, 28 June 2014 - 09:05 PM.

### #2Buckeye  GDNet+

Posted 29 June 2014 - 07:06 AM

It appears that, by "clip tiles," you mean that you want each tile structure to contain the pixel coordinates of some region of the texture.

If so, then set the tile x & y coordinates to the appropriate corner of each region, and set the width and height parameters to the width/height of the desired region.

E.g., using the second image you posted, assuming the upper left-hand corner is ( 0,0 ), the width/height of each colored rectangle is (for instance) 16, and the blank "gap" between colored rectangles is 2 pixels:

int gapWidth = 2;

int rectWidth = 16;

int rectHeight = 16;

// upper left rectangle runs from pixel locations 0 through 15 in both x and y directions

tile 0: x=0, y=0, w = rectWidth, h = rectHeight (upper left corner)

// the upper left corner of the colored rectangle on the upper right - the brown one -

// pixel numbers in x direction -> grey rectangle 0-15, gap 16-17, brown rectangle 18-33

tile 1: x = rectWidth + gapWidth, y = 0, w = rectWidth, h = rectHeight (brown)

// etc.

tile 2: x = 0, y = rectHeight + gapWidth, w = ..., h = ... (pink)

tile 3: x = rectWidth + gapWidth, y = rectHeight + gapWidth, w = ..., h = ... (green)

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #3Vortez  Members

Posted 29 June 2014 - 10:44 AM

You're repeating yourself a lot in this code, and the readability is not very good, i suggest caching those values into variables or consts.

Example:

tiles_i[(y * tiles_across) + x].x = 16;
tiles_i[(y * tiles_across) + x].y = 16;
tiles_i[(y * tiles_across) + x].w = 16;
tiles_i[(y * tiles_across) + x].h = 16;
printf
("%d\n",(y * tiles_across) + x);

could be replaced by

const TileSize = 16;

...

int Index = (y * tiles_across) + x)

tiles_i(Index).x = TileSize;

tiles_i(Index).y = TileSize;

tiles_i(Index).z = TileSize;

tiles_i(Index).w = TileSize;

Even better would be using a vector class, and overload the = operator

tiles[Index] = TileSize; // Set all four values at once

Mine look like this. Notice i didn't think about overloading the = operator with a float, but it would take 30 seconds to add one, or i could use the Set() method.

Edited by Vortez, 29 June 2014 - 10:57 AM.

### #4DrNicholas  Members

Posted 29 June 2014 - 12:05 PM

Thanks Buckeye. And to Vortez, I did the index thing, but I don't know how to set it all at once with different coordinates. The tilesize is read from the file it parses.

I have another problem. My level file is like this:

return {
....
layers = {
{
....
data = {
....
}
},
{
....
data = {
....
}
},
{
....
data = {
....
}
}
}
}


I know how to get into the first data table:

lua_getfield(L, -1, "layers");
lua_rawgeti(L, -1, 1);
lua_getfield(L, -1, "data");
lua_pop(L, 1);


that code will get me into the layers table, and then into the first data table, and then pop takes me out of that data table, but I am not sure where it takes me to then. How do I get into the other data tables? Thanks

Edited by DrNicholas, 29 June 2014 - 12:07 PM.

### #5Vortez  Members

Posted 29 June 2014 - 02:34 PM

And to Vortez, I did the index thing, but I don't know how to set it all at once with different coordinates.

You dont have to have a vector class to do this, that was just a suggestion, you could roll up a function for this, for example.

void SetTile(TILES *pTile, int x, int y, int TileSize)
{
pTile->x = x;
pTile->y = y;
pTile->w = TileSize;
pTile->h = TileSize;
}



Calculating the x and y coordinate shouldn't be too difficult. I'll let you figure it out.

As for the lua part, i can't help you on that, haven't done that sort of thing yet.

Edited by Vortez, 29 June 2014 - 02:46 PM.

### #6DrNicholas  Members

Posted 29 June 2014 - 05:04 PM

Ah okay. So I may ave figured out the issue with getting other tile layers to show. lua_rawgeti(L, -1, z); with z representing the data table I want (1, 2, or 3) will grab the data from one of those tables. However I am having an issue now.

    lua_getfield(L, -1, "layers");
lua_rawgeti(L, -1, 1);
lua_getfield(L, -1, "data");
int i;
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_back2[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1);
lua_rawgeti(L, -1, 2);
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_back1[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1)


shouldn't this run correctly? I cannot get it to, it crashes at this part

    lua_rawgeti(L, -1, 2);
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_back1[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 1)


namely the lua_rawgeti(L, -1, 2); part. Any ideas on fixing this? Thanks

### #7DrNicholas  Members

Posted 29 June 2014 - 06:31 PM

EDIT: (edit button wasn't working for me at the time of this typing, so if an admin wants to add it to my previous post go ahead)

This system appears to work for now:

		lua_getfield(L, -1, "layers");
lua_rawgeti(L, -1, 1);
lua_getfield(L, -1, "data");
int i;
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_back2[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 3);
lua_getfield(L, -1, "layers");
lua_rawgeti(L, -1, 2);
lua_getfield(L, -1, "data");
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_back1[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 3);
lua_getfield(L, -1, "layers");
lua_rawgeti(L, -1, 3);
lua_getfield(L, -1, "data");
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_fore[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 3);


just doesn't feel very optimized

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.