Clipping Sprites
Posted: Tue Sep 30, 2008 8:49 pm
It seems that clipping sprites right now is not working, like there are messed up ones when I select that are the whole tilesheet and I cna size it with my mouse, and others that are normal, and I cant solve it, it may be with the for loop under: Clip.
Code: Select all
/*This source code copyrighted by Lazy Foo' Productions (2004-2007) and may not be redestributed without written permission.*/
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include <fstream>
#include <iostream>
#include <windows.h>
using namespace std;
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 60;
//Tile constants
const int TILE_WIDTH = 16;
const int TILE_HEIGHT = 16;
const int TOTAL_TILES_W = 240;
const int TOTAL_TILES_H = 240;
const int TILE_SPRITES = 22;
//The dimensions of the level
const int LEVEL_WIDTH = TILE_WIDTH*TOTAL_TILES_W;
const int LEVEL_HEIGHT = TILE_HEIGHT*TOTAL_TILES_H;
//The surfaces
SDL_Surface *screen = NULL;
SDL_Surface *tileSheet = NULL;
//Sprite from the tile sheet
SDL_Rect clips[ TILE_SPRITES ];
//The event structure
SDL_Event event;
//The camera
SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
//The tile
class Tile
{
private:
//The attributes of the tile
SDL_Rect box;
//The tile type
int type;
public:
//Initializes the variables
Tile( int x, int y, int tileType );
//Shows the tile
void show();
//Get the tile type
int get_type();
//Get the collision box
SDL_Rect &get_box();
};
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( 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 );
}
bool check_collision( SDL_Rect &A, SDL_Rect &B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//Calculate the sides of rect B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
//If any of the sides from A are outside of B
if( bottomA <= topB )
{
return false;
}
if( topA >= bottomB )
{
return false;
}
if( rightA <= leftB )
{
return false;
}
if( leftA >= rightB )
{
return false;
}
//If none of the sides from A are outside B
return true;
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Level Editior. Version 0.1", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the tile sheet
tileSheet = load_image( "TileSheet.bmp" );
//If there was a problem in loading the tiles
if( tileSheet == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up( Tile *tiles[][TOTAL_TILES_H] )
{
//Free the surface
SDL_FreeSurface( tileSheet );
//Free the tiles
for( int t = 0; t < TOTAL_TILES_W; t++ )
{
for ( int tt = 0; tt < TOTAL_TILES_H; tt++ )
{
//Get rid of old tile
delete tiles[ t ] [ tt ];
}
}
//Quit SDL
SDL_Quit();
}
void set_camera()
{
//Mouse offsets
int x = 0, y = 0;
//Get mouse offsets
SDL_GetMouseState( &x, &y );
//Move camera to the left if needed
if( x < TILE_WIDTH )
{
camera.x -= 5;
}
//Move camera to the right if needed
if( x > SCREEN_WIDTH - TILE_WIDTH )
{
camera.x += 5;
}
//Move camera up if needed
if( y < TILE_WIDTH )
{
camera.y -= 5;
}
//Move camera down if needed
if( y > SCREEN_HEIGHT - TILE_WIDTH )
{
camera.y += 5;
}
//Keep the camera in bounds.
if( camera.x < 0 )
{
camera.x = 0;
}
if( camera.y < 0 )
{
camera.y = 0;
}
if( camera.x > LEVEL_WIDTH - camera.w )
{
camera.x = LEVEL_WIDTH - camera.w;
}
if( camera.y > LEVEL_HEIGHT - camera.h )
{
camera.y = LEVEL_HEIGHT - camera.h;
}
}
void put_tile( Tile *tiles[] [TOTAL_TILES_H], int tileType )
{
//Mouse offsets
int x = 0, y = 0;
//Get mouse offsets
SDL_GetMouseState( &x, &y );
//Adjust to camera
x += camera.x;
y += camera.y;
//Go through tiles
for( int t = 0; t < TOTAL_TILES_W; t++ )
{
for ( int tt = 0; tt < TOTAL_TILES_H; tt++ )
{
//Get tile's collision box
SDL_Rect box = tiles[ t ] [ tt ] ->get_box();
//If the mouse is inside the tile
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
//Get rid of old tile
delete tiles[ t ] [ tt ];
//Replace it with new one
tiles[ t ] [ tt ] = new Tile( box.x, box.y, tileType );
}
}
}
}
void clip_tiles()
{
/*
//Clip the sprite sheet
clips[ TILE_GRASS ].x = 0;
clips[ TILE_GRASS ].y = 0;
clips[ TILE_GRASS ].w = TILE_WIDTH;
clips[ TILE_GRASS ].h = TILE_HEIGHT;
clips[ TILE_GRASS_2 ].x = TILE_WIDTH;
clips[ TILE_GRASS_2 ].y = 0;
clips[ TILE_GRASS_2 ].w = TILE_WIDTH;
clips[ TILE_GRASS_2 ].h = TILE_HEIGHT;
clips[ TILE_DIRT ].x = (TILE_WIDTH*2);
clips[ TILE_DIRT ].y = 0;
clips[ TILE_DIRT ].w = TILE_WIDTH;
clips[ TILE_DIRT ].h = TILE_HEIGHT;
clips[ TILE_DIRT_2 ].x = (TILE_WIDTH*3);
clips[ TILE_DIRT_2 ].y = 0;
clips[ TILE_DIRT_2 ].w = TILE_WIDTH;
clips[ TILE_DIRT_2 ].h = TILE_HEIGHT;
clips[ TILE_DIRT_3 ].x = (TILE_WIDTH*4);
clips[ TILE_DIRT_3 ].y = 0;
clips[ TILE_DIRT_3 ].w = TILE_WIDTH;
clips[ TILE_DIRT_3 ].h = TILE_HEIGHT;
clips[ TILE_SAND ].x = (TILE_WIDTH*5);
clips[ TILE_SAND ].y = 0;
clips[ TILE_SAND ].w = TILE_WIDTH;
clips[ TILE_SAND ].h = TILE_HEIGHT;
clips[ TILE_WATER ].x = (TILE_WIDTH*6);
clips[ TILE_WATER ].y = 0;
clips[ TILE_WATER ].w = TILE_WIDTH;
clips[ TILE_WATER ].h = TILE_HEIGHT;
clips[ TILE_GRAVEL ].x = (TILE_WIDTH*7);
clips[ TILE_GRAVEL ].y = 0;
clips[ TILE_GRAVEL ].w = TILE_WIDTH;
clips[ TILE_GRAVEL ].h = TILE_HEIGHT;
clips[ TILE_WOOD ].x = (TILE_WIDTH*8);
clips[ TILE_WOOD ].y = 0;
clips[ TILE_WOOD ].w = TILE_WIDTH;
clips[ TILE_WOOD ].h = TILE_HEIGHT;
clips[ TILE_DANCE_RED ].x = (TILE_WIDTH*9);
clips[ TILE_DANCE_RED ].y = 0;
clips[ TILE_DANCE_RED ].w = TILE_WIDTH;
clips[ TILE_DANCE_RED ].h = TILE_HEIGHT;
clips[ TILE_DANCE_WHITE ].x = (TILE_WIDTH*10);
clips[ TILE_DANCE_WHITE ].y = 0;
clips[ TILE_DANCE_WHITE ].w = TILE_WIDTH;
clips[ TILE_DANCE_WHITE ].h = TILE_HEIGHT;
*/
for( int i = 0; i < TILE_SPRITES; i++)
{
if( i == 0 )
{
clips[ i ].x = 0;
clips[ i ].y = 0;
clips[ i ].w = TILE_WIDTH;
clips[ i ].h = TILE_HEIGHT;
}
else {
clips[ i ].x = (TILE_WIDTH*i);
clips[ i ].y = 0;
clips[ i ].w = TILE_WIDTH;
clips[ i ].h = TILE_HEIGHT;
}
}
}
bool set_tiles( Tile *tiles[][TOTAL_TILES_H] )
{
//The tile offsets
int x = 0, y = 0;
int tileType;
//Open the map
std::ifstream map( "test.map" );
//If the map couldn't be loaded
if( map == NULL )
{
//Initialize the tiles
for( int t = 0; t < TOTAL_TILES_H; t++ )
{
for( int tt = 0; tt < TOTAL_TILES_W; tt++ )
{
//Put a floor tile
tiles[ t ] [ tt ] = new Tile( tt*TILE_WIDTH, t*TILE_WIDTH, 0 );
}
}
}
else
{
//Initialize the tiles
for( int t = 0; t < TOTAL_TILES_H; t++ )
{
for( int tt = 0; tt < TOTAL_TILES_W; tt++ )
{
map >> tileType;
//Put a floor tile
tiles[ t ] [ tt ] = new Tile( tt*TILE_WIDTH, t*TILE_WIDTH, tileType );
}
}
//Close the file
map.close();
}
return true;
}
void save_tiles( Tile *tiles[][TOTAL_TILES_W] )
{
//Open the map
std::ofstream map( "test.map" );
//Go through the tiles
for( int t = 0; t < TOTAL_TILES_W ; t++ )
{
for( int tt = 0; tt < TOTAL_TILES_W; tt++ )
{
//Write tile type to file
map << tiles[ t ] [tt] ->get_type() << " ";
}
}
//Close the file
map.close();
}
Tile::Tile( int x, int y, int tileType )
{
//Get the offsets
box.x = x;
box.y = y;
//Set the collision box
box.w = TILE_WIDTH;
box.h = TILE_HEIGHT;
//Get the tile type
type = tileType;
}
void Tile::show()
{
//If the tile is on screen
if( check_collision( camera, box ) == true )
{
//Show the tile
apply_surface( box.x - camera.x, box.y - camera.y, tileSheet, screen, &clips[ type ] );
}
}
int Tile::get_type()
{
return type;
}
SDL_Rect &Tile::get_box()
{
return box;
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Current tile type
int currentType = 0;
//The tiles that will be used
Tile *tiles[ TOTAL_TILES_W ] [TOTAL_TILES_H];
//The frame rate regulator
Timer fps;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Clip the tile sheet
clip_tiles();
//Set the tiles
if( set_tiles( tiles ) == false )
{
return 1;
}
//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//When the user clicks
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//On left mouse click
if( event.button.button == SDL_BUTTON_LEFT )
{
//Put the tile
put_tile( tiles, currentType );
}
//On mouse wheel scroll
else if( event.button.button == SDL_BUTTON_WHEELUP )
{
//Scroll through tiles
currentType--;
if( currentType < 0 )
{
currentType = TILE_SPRITES;
}
}
else if( event.button.button == SDL_BUTTON_WHEELDOWN )
{
//Scroll through tiles
currentType++;
if( currentType > TILE_SPRITES )
{
currentType = 0;
}
}
}
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Set the camera
set_camera();
//Show the tiles
for( int t = 0; t < TOTAL_TILES_W ; t++ )
{
for( int tt = 0; tt < TOTAL_TILES_W; tt++ )
{
//Write tile type to file
tiles[ t ] [ tt ]->show();
}
}
apply_surface( 0, 0, tileSheet, screen, &clips[ currentType ] );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Save the tile map
save_tiles( tiles );
//Clean up
clean_up( tiles );
delete tileSheet;
return 0;
}