void csprite::init ( const char *filename, bool alpha_channel )
// initializes the sprite
{
if ( alpha_channel )
// if the image has an alpha-channel
// load the bitmap
bitmap = IMG_Load ( filename );
else
// if the image does not have an alpha-channel
{
SDL_Surface* temp;
// load temp
temp = SDL_LoadBMP ( filename );
if ( temp == NULL )
// if there was an error
return_error ( "Failed to initialize sprite..." );
// load the bitmap
bitmap = SDL_DisplayFormat ( temp );
// free temp
SDL_FreeSurface ( temp );
// activate color keying
if ( SDL_SetColorKey ( bitmap, SDL_SRCCOLORKEY, SDL_MapRGB ( bitmap->format, 255, 0, 128 ) ) < 0 )
// if there was an error
return_error ( "Failed to colorkey sprite..." );
}
if ( bitmap == NULL )
// if there was an error
return_error ( "Failed to initialize sprite..." );
// set up other variables
width = bitmap->w;
height = bitmap->h;
}
void csprite::init ( const char *filename, bool alpha_channel, int _width, int _height )
// initializes the sprite
{
// initialize sprite
init ( filename, alpha_channel );
// set up other variables
width = _width;
height = _height;
}
void csprite::draw ( )
// draws the sprite to the screen
{
// create a source rectangle
SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = bitmap->w;
source.h = bitmap->h;
// create a destination rectangle
SDL_Rect destination;
destination.x = ( int ) x;
destination.y = ( int ) y;
destination.w = width;
destination.h = height;
// blit the bitmap to the screen
if ( SDL_BlitSurface ( bitmap, &source, SDL_GetVideoSurface ( ), &destination ) < 0 )
// if there was an error
return_error ( "Failed to draw sprite..." );
}
void csprite::draw ( float _x, float _y )
// draws the sprite to the screen at a given location
{
// position the sprite
x = _x;
y = _y;
// draw it
draw ( );
}
void csprite::draw ( int _x, int _y )
// draws the sprite to the screen at a given location
{
// create a source rectangle
SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = bitmap->w;
source.h = bitmap->h;
// create a destination rectangle
SDL_Rect destination;
destination.x = _x;
destination.y = _y;
destination.w = width;
destination.h = height;
// blit the bitmap to the screen
if ( SDL_BlitSurface ( bitmap, &source, SDL_GetVideoSurface ( ), &destination ) < 0 )
// if there was an error
return_error ( "Failed to draw sprite..." );
}
void csprite::draw ( float _x, float _y, int x_frame, int y_frame )
// draws the sprite to the screen at a given location using a frame
{
// position the sprite
x = _x;
y = _y;
// create a source rectangle
SDL_Rect source;
source.x = x_frame * width;
source.y = y_frame * height;
source.w = width;
source.h = height;
// create a destination rectangle
SDL_Rect destination;
destination.x = ( int ) x;
destination.y = ( int ) y;
destination.w = width;
destination.h = height;
// blit the bitmap to the screen
if ( SDL_BlitSurface ( bitmap, &source, SDL_GetVideoSurface ( ), &destination ) < 0 )
// if there was an error
return_error ( "Failed to draw sprite..." );
}
void csprite::draw_cropped ( int percentage )
// draws the sprite to the screen cropped at a given location
{
// create a source rectangle
SDL_Rect source;
source.x = 0;
source.y = 0;
source.w = ( int ) ( ( 1.0 / 100.0 ) * ( float ) percentage * ( float ) width );
source.h = bitmap->h;
// create a destination rectangle
SDL_Rect destination;
destination.x = ( int ) x;
destination.y = ( int ) y;
destination.w = ( int ) ( ( 1.0 / 100.0 ) * ( float ) percentage * ( float ) width );
destination.h = height;
// blit the bitmap to the screen
if ( SDL_BlitSurface ( bitmap, &source, SDL_GetVideoSurface ( ), &destination ) < 0 )
// if there was an error
return_error ( "Failed to draw sprite..." );
}
void csprite::destroy ( void )
// destroys all used memory of a sprite and frees it
{
// free the surface
SDL_FreeSurface ( bitmap );
}
int tile_offset_x, tile_offset_y;
int tiles_drawn;
ctile tile[TILES_X][TILES_Y];
csprite tileset;
extern screen_width, screen_height;
void ctile::init ( int _x, int _y, tile_type _type )
// initializes the tile
{
if ( _type == dirt )
{
// assign vertical frame
y_frame = 0;
// randomize horizontal frame
x_frame = rand ( ) % 3;
}
else if ( _type == grass )
{
// assign vertical frame
y_frame = 1;
// randomize horizontal frame
x_frame = rand ( ) % 3;
}
/*else
{
// assign vertical frame
y_frame = 2;
if ( _type == tra_grass_dirt_t )
x_frame = 0;
else if ( _type == tra_grass_dirt_tr )
x_frame = 1;
else if ( _type == tra_grass_dirt_r )
x_frame = 2;
else if ( _type == tra_grass_dirt_br )
x_frame = 3;
else if ( _type == tra_grass_dirt_b )
x_frame = 4;
else if ( _type == tra_grass_dirt_bl )
x_frame = 5;
else if ( _type == tra_grass_dirt_l )
x_frame = 6;
else if ( _type == tra_grass_dirt_tl )
x_frame = 7;
}*/
// save position
x = _x;
y = _y;
// save type
type = _type;
}
void ctile::draw ( void )
// draws the tile
{
// draw the sprite
tileset.draw ( ( float ) x, ( float ) y, x_frame, y_frame );
}
void init_tiles ( int start_position_x, int start_position_y )
// inits the tiles
{
int row_index = 0;
tiles_drawn = 0;
// init tileset
tileset.init ( "tilesets/jungle.bmp", false, TILE_WIDTH, TILE_HEIGHT );
for ( int y = 0; y < TILES_Y; y++ )
// loop through all tiles vertically
{
for ( int x = 0; x < ( TILES_X / 2 ); x++ )
// loop through all tiles horizontally
{
if ( row_index == 0 )
// init tile
tile[x * 2][y].init ( ( x * TILE_WIDTH ) - start_position_x, ( y * ( ( TILE_HEIGHT + 1 ) / 2 ) ) - start_position_y, grass );
else if ( row_index == 1 )
// init tile
tile[( x * 2 ) + 1][y].init ( ( x * TILE_WIDTH ) + ( TILE_WIDTH / 2 ) - start_position_x, ( ( y - 1 ) * ( ( TILE_HEIGHT + 1 ) / 2 ) ) + ( ( TILE_HEIGHT + 1 ) / 2 ) - start_position_y, grass );
}
// alternate index
if ( row_index == 0 )
row_index = 1;
else
row_index = 0;
}
}
void draw_tiles ( void )
// draws the tiles
{
tiles_drawn = 0;
for ( int y = 0; y < TILES_Y; y++ )
// loop through all tiles vertically
{
for ( int x = 0; x < TILES_X; x++ )
// loop through all tiles horizontally
{
if ( tile_offset_x > 0 )
// left scrolling
{
if ( tile[0][0].x >= -( TILE_WIDTH / 2 ) )
tile_offset_x = 0;
}
else if ( tile_offset_x < 0 )
// right scrolling
{
if ( tile[0][0].x <= -( ( TILES_X / 2 ) * TILE_WIDTH ) - 2 + screen_width )
tile_offset_x = 0;
}
if ( tile_offset_y > 0 )
// up scrolling
{
if ( tile[0][0].y >= -( ( TILE_HEIGHT + 1 ) / 2 ) )
tile_offset_y = 0;
}
else if ( tile_offset_y < 0 )
// down scrolling
{
if ( tile[0][0].y <= -( ( TILES_Y / 2 ) * ( TILE_HEIGHT + 1 ) ) - 2 + screen_height )
tile_offset_y = 0;
}
// update position
tile[x][y].x += tile_offset_x;
tile[x][y].y += tile_offset_y;
if ( tile[x][y].x >= -TILE_WIDTH && tile[x][y].x <= screen_width )
// if tile is horizontally in view
{
if ( tile[x][y].y >= -TILE_HEIGHT && tile[x][y].y <= screen_height )
// if tile is vertically in view
{
// draw tile
tile[x][y].draw ( );
tiles_drawn++;
}
}
}
}
}
void destroy_tiles ( void )
// destroy the tiles
{
// destroy tileset
tileset.destroy ( );
}