Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL sprites

Started by Muffin Mar 11, 2005 at 5:14 AM 8 replies 2.3k views
Original Post
Muffin
Muffin
You have probably heard this question many times. For months ago I found a thread in this forum or in some tutorial that explained how to load pictures into an array. More exactly you have a sprit with many pictures in it. Then you make a loop that split the sprite into many pieces and save the pieces into the array. I thought I would make this code in SDL but I can't find the example :D So if anyone knows where I can find a tutorial about it or a thread in some forum, please answer as soon as possible.
Rob Loach
Rob Loach
Instead of looking at the whole task, look at each individual step instead:


Quote:
Original post by Muffin
how to load pictures into an array.
Easily done, if you know how to load pictures. Just look in your favourite C++ book for std::vector if you don't know how to manage a dynamic array.

Quote:
Original post by Muffin
a sprit with many pictures in it.
Once you have the picture loaded, you can do anything you want with it. Copy it to a new picture, crop it down, anything. Copy only parts of it to the screen...

Quote:
Original post by Muffin
Then you make a loop that split the sprite into many pieces and save the pieces into the array.
If you know how wide and tall each tile of the entire sprite image will be, you can make a loop to go through it. Just try out your own logic with a peice of paper and a pen and see what you can come up with. You'd need to take care of four things: TileX, TileY, TileWidth and TileHeight.

Quote:
Original post by Muffin
I thought I would make this code in SDL but I can't find the example
As you can see, you don't need one! Once I get the time, I'll upload part of the sprite class of the engine I'm working on (it's almost done now).
Rob Loach [Website] [Projects] [
Muffin
Muffin
Thanks for the answer. I can't try to make the code for the moment because my computer will not start. I think it have something to do with the power supply. But I have one question. Should I use SDL or is it better to use something else to make 2D games? Maybe in the future I will try to make 3D games so should I use something else?
jbadams
jbadams
Quote:
Original post by Muffin
Thanks for the answer. I can't try to make the code for the moment because my computer will not start. I think it have something to do with the power supply. But I have one question. Should I use SDL or is it better to use something else to make 2D games? Maybe in the future I will try to make 3D games so should I use something else?


SDL is perfectly fine. It can even be used in conjunction with OpenGL for 3d applications when you're ready.
- Jason Astle-Adams
Muffin
Muffin
Kazgoroth: Ok I think I’ll stick to SDL then, But I’m just curious. Is it better to use directX later then using SDL and OpenGL? Wich is faster and so on?
Rob Loach
Rob Loach
It's completely up to you which API you choose. Try them all out and see which you prefer using. But definately start off with SDL.
Rob Loach [Website] [Projects] [
Muffin
Muffin
I have started to make my code to cut the image into tiles. But I wonder if I though a bit wrong. This is how I started to think: I made a vector "tiles" but started to wonder if I should use string or something else. Then it hit me after some time at libsdl.org, should I do a new surface like tiles[5] and then in some way use SDL_rect to cut the tiles out of the image surface with a loop that checks the x,y,width and height and then blit them into the tile[] surface. I think I can get that code to work but I wonder if I’m thinking the whole thing wrong now.

string* tiles = new string [numberOfTiles]; //my vector

image = SDL_LoadBMP("sprites.bmp");
Muffin
Muffin
#define tileWidth 20
#define tileHeight 20
#define numberOfTiles 5
#define numberOfTilesOnColumn 5

SDL_surface *image
SDL_Surface *tiles[numberOfTiles];

I try with this code to make tiles from the image and save them into the tile array surface. But it doesn't seems to be the best way to do it and it dosn't seems to work. When i try to draw tile[0] on another surface "screen". It doesn't draw anything at all. I can't figure it out.

image = SDL_LoadBMP("sprites.bmp");

int tileX = 0;
int tileY = 0;
int j = 1;

for(int i = 0; i < numberOfTiles; i++)
{
SDL_Rect cut;
cut.x = tileX;
cut.y = tileY;
cut.w = tileWidth;
cut.h = tileHeight;
SDL_Rect paste;
paste.x = 0;
paste.y = 0;
paste.w = tileWidth;
paste.h = tileHeight;
SDL_BlitSurface(image, &cut, tiles, &paste);

tileX += tileWidth;

if(j < numberOfTilesOnColumn)
{
j++;
}
else
{
tileX = 0;
tileY += tileHeight;
j = 1;
}
}
evillive2
evillive2
It is completely up to you if you want to blit each sprite from a larger image into it's own smaller surface but it is unnecessary. Once you have the large image onto a surface you only need to know each sprite's individual rectangle in order to blit that particular sprite to the screen or any other surface.

Instead of:
SDL_surface *image
SDL_Surface *tiles[numberOfTiles];

Try:
SDL_surface *image
SDL_Rect tiles[numberOfTiles];

It may also be beneficial to use a different structure for an array of tiles:
possibly:
// this is just a suggestion and is in pseudo code... don' take it literally :)typedef struct tile_struct{    SDL_Surface *source; // the image surface    SDL_Rect source_rect; // the source rectangle of the sprite on the surface    Uint8 tile_id; // the tile id}TILE_STRUCT;// I imagine these were being used as globalsSDL_Surface *image;// the STL can be your friend here later but to go with your previous code...TILE_STRUCT tiles[numberOfTiles];void cut_spritesheet( char *bmp, Uint16 tile_width, Uint16 tile_height ){    Uint8 tile = 0;    Sint16 tile_x, tile_y;    image = SDL_LoadBMP( bmp );    // might want to use SDL_DisplayFormat here to convert the surface to the    // same format as the display. This will help your performance.    for ( tile_y = 0; tile_y < image->h; tile_y += tile_height )    {        for ( tile_x = 0; tile_x < image->w; tile_x += tile_width )        {            // the tile image surface            tiles[tile].source = image;            // the source rectangle of the sprite            tiles[tile].source_rect.x = tile_x;            tiles[tile].source_rect.y = tile_y;            tiles[tile].source_rect.w = tile_width;            tiles[tile].source_rect.h = tile_height;            // the tile id             // useful if the tile_id isn't the same as it's index            // in the tiles[] array            tiles[tile].tile_id = tile;            tile++;        }    }}void DrawTile( Sint16 screen_x, Sint16 screen_y, TILE_STRUCT *tile ){    SDL_Rect dest_rect = {screen_x, screen_y, 0, 0};    SDL_Surface *screen = SDL_GetVideoSurface();    SDL_BlitSurface( tile->source, &tile->source_rect, screen, &dest_rect );}DrawTile( 10, 20, &tiles[3] );

The above code is just off the top of my head and is more of a temporary thing than anything else to get used to how SDL_BlitSurface works to save yourself some extra work.

Think of your source surface as a sheet of cookie dough and the source rectangle as a cookie cutter. The pan or cookie sheet you will be placing the cutouts on is the screen or destination surface. The source surface remains the same but you can place pieces of it on the screen to create whatever you want.

Our cookie cutter happens to be limited to a rectangle shape but look into SDL_ColorKey for now to get the concept then later on move onto the much nicer looking/slower/more complicated alpha blending techniques you can use with SDL. This is where OpenGL will be a big boost for you later on but for starting simple games it isn't really necessary.

I hope that helps a little.
Evillive2

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.