Jump to content
  • Advertisement
Sign in to follow this  
slowburn15

SDL_Surface question

This topic is 3007 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

BOTTOM LINE: How can i save a bunch of clipped surfaces/tiles to a SDL_Surface ?

IMPORTANT: The lines of code dealing with the surfaces have a couple *'s next to them.

I'm trying to make a 2D game that uses tiling. The only part that is giving me trouble is saving the final picture. I do all the calculations of clipping the tiles out of the sheet and bliting them to a SDL_Surface called map. I expected it to stay there unless i over wrote it with some more tiles. when I blit map to the screen all i get is a black screen. However when i blit the tiles to the screen it works. But i have to do the calculations every frame and that takes up a lot of processing. So as i said up at the top. How can i save the information on an SDL_Surface ?

I call the update_map function once to do the calculations. Then call map_display every frame to... display the map.

this is my logic


void _game_state::logic()
{
if(state == 1)
{
SDL_Flip(screen);
map.load_map();
map.update_map();
state = 2;
}
if(state == 2)
{
map.display_map();
}
}



functions that you might need to know about


void apply_surface_clip( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

void _map::display_map()
{
*** apply_surface(0,0,map,screen); ***
*** SDL_Flip(screen); ***
}

void _map::update_map()
{

SDL_Rect square;
square.x = 0;
square.y = 0;
square.w = 32;
square.h = 32;
char number[3]; // the number gotten from the txt file relates to a tile
// on the tile sheet
int tile;
std::ifstream map_file ("maps/map01.txt"); // load the map file

std::string temp;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); // clear
// the screen
for(int index = 0; index < ply_loc_y - 8; index++)
{
getline(map_file, temp);
}

for(int index = 0; index <= (ply_loc_x - 11) * 3; index++)
{
map_file.get();
}

for(int next_tile_line = 0; next_tile_line < 17; next_tile_line++)
{
for(int index = 0; index < 22; index++)
{
number[0] = map_file.get(); // get the
number[1] = map_file.get(); // the three
number[2] = map_file.get(); // digit number
map_file.get(); // move past the space in the txt file
tile = atoi( number ); // convert the char to a number and store it in tile
square.x = 32*(tile-((tile/20)*20)); //calculate where the tiles x is on the sheet
square.y = 32*(tile/20); // calculate where the tiles y is on the sheet
*** apply_surface_clip( index*32,next_tile_line*32, tile_sheet, map, &square); // blit the clip onto the map surface
}
getline(map_file, temp);
for(int index = 0; index <= (ply_loc_x - 11) * 3; index++)
{
map_file.get();
}

}





Thanks in advance to anyone who can tell me why the SDL_Surface map doesn't seem to hold any of the data I throw at it.

[Edited by - slowburn15 on July 17, 2010 12:17:46 AM]

Share this post


Link to post
Share on other sites
Advertisement
Hi!
I recently wrote a Sprite class using SDL. I use it for all my Tiled 2D games (for the good ol' SNES feel :))

The sprite class and source can be found here:
http://ken-soft.com/?p=234

main.cpp demonstrates basic implementation

the function Sprite->getRect(x,y,width,height) has all the code for grabbing a rectangle from an SDL_Surface*
and the function Sprite->draw(screen, x, y) has all the code for drawing the sprite to a screen.

I hope this is of some use to you!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!