Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include "SDL_image.h"
- #include <string>
- #include "constants.h"
- #include "functions.h"
- #include "globals.h"
- #include "classes.h"
- SDL_Surface *load_image( std::string filename )
- {
- SDL_Surface* loadedImage = NULL;
- SDL_Surface* optimizedImage = NULL;
- loadedImage = IMG_Load( filename.c_str() );
- if( loadedImage != NULL )
- {
- optimizedImage = SDL_DisplayFormat( loadedImage );
- SDL_FreeSurface( loadedImage );
- if( optimizedImage != NULL )
- {
- SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
- }
- }
- return optimizedImage;
- }
- void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip )
- {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface( source, clip, destination, &offset );
- }
- bool init()
- {
- if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
- {
- return false;
- }
- screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
- if( screen == NULL )
- {
- return false;
- }
- SDL_WM_SetCaption( "Dan's Tetris", NULL );
- if( TTF_Init() == -1 )
- {
- return false;
- }
- return true;
- }
- bool load_files()
- {
- skyrider = load_image( "skyrider.png" );
- if( skyrider == NULL )
- {
- return false;
- }
- JShapeBlock = load_image( "JShapeBlock.png" );
- if( JShapeBlock == NULL )
- {
- return false;
- }
- LShapeBlock = load_image( "LShapeBlock.png" );
- if( LShapeBlock == NULL )
- {
- return false;
- }
- OShapeBlock = load_image( "OShapeBlock.png" );
- if( OShapeBlock == NULL )
- {
- return false;
- }
- ZShapeBlock = load_image( "ZShapeBlock.png" );
- if( ZShapeBlock == NULL )
- {
- return false;
- }
- TShapeBlock = load_image( "TShapeBlock.png" );
- if( TShapeBlock == NULL )
- {
- return false;
- }
- SShapeBlock = load_image( "SShapeBlock.png" );
- if( SShapeBlock == NULL )
- {
- return false;
- }
- font = TTF_OpenFont( "pirulen.ttf", 28 );
- SDL_Color WhiteTextColor = {255, 255, 255};
- LinesClearedText = TTF_RenderText_Solid( font, "Lines", WhiteTextColor );
- GameOverText = TTF_RenderText_Solid( font, "Game Over", WhiteTextColor );
- ScoreText = TTF_RenderText_Solid( font, "Score", WhiteTextColor );
- PlayAgainText = TTF_RenderText_Solid( font, "Press Enter To", WhiteTextColor );
- PlayAgain2Text = TTF_RenderText_Solid( font, "Play Again", WhiteTextColor );
- return true;
- }
- void clean_up()
- {
- SDL_FreeSurface( skyrider );
- SDL_FreeSurface( JShapeBlock );
- SDL_FreeSurface( LShapeBlock );
- SDL_FreeSurface( OShapeBlock );
- SDL_FreeSurface( ZShapeBlock );
- SDL_FreeSurface( TShapeBlock );
- SDL_FreeSurface( SShapeBlock );
- SDL_FreeSurface( LinesClearedText );
- SDL_FreeSurface( ScoreText );
- SDL_FreeSurface( GameOverText );
- SDL_FreeSurface( PlayAgainText );
- SDL_FreeSurface( PlayAgain2Text );
- SDL_FreeSurface( ScoreNumberText );
- SDL_FreeSurface( LinesClearedNumberText );
- TTF_CloseFont( font );
- TTF_Quit();
- SDL_Quit();
- }
- void CheckForLine(Block *aBlock, int maxsize, int &linesclearedint, int &scorevalue)
- {
- int counter = 0, RowCounter = 0;
- const int NumberOfRows = 18;
- int Row[NumberOfRows] = {0};
- while (counter < (maxsize - 1))
- {
- while (RowCounter < (NumberOfRows - 1))
- {
- if (aBlock[counter].Gety() == 35 * (RowCounter + 1))
- Row[RowCounter]++;
- RowCounter++;
- }
- RowCounter = 0;
- counter++;
- }
- RowCounter = 0;
- counter = 0;
- while (RowCounter < NumberOfRows)
- {
- if (Row[RowCounter] == 10)
- {
- while (counter < maxsize - 1)
- {
- if (aBlock[counter].Gety() == 35 * (RowCounter + 1))
- {
- aBlock[counter].Sety(-1);
- aBlock[counter].Setx(-1);
- aBlock[counter].SetShowBlock(0);
- }
- counter++;
- }
- }
- counter = 0;
- RowCounter++;
- }
- counter = 0;
- RowCounter = 0;
- while (counter < (maxsize - 1))
- {
- while (RowCounter < NumberOfRows)
- {
- if (Row[RowCounter] == 10)
- {
- if (aBlock[counter].Gety() != -1 && aBlock[counter].Gety() <= 35 * (RowCounter + 1))
- aBlock[counter].Sety(aBlock[counter].Gety() + 35);
- }
- RowCounter++;
- }
- RowCounter = 0;
- counter++;
- }
- RowCounter = 0;
- int temp = 0;
- bool CheckIfRunAgain = 0;
- while (RowCounter < NumberOfRows)
- {
- if (Row[RowCounter] == 10)
- {
- CheckIfRunAgain = 1;
- linesclearedint++;
- temp++;
- }
- RowCounter++;
- }
- scorevalue = scorevalue + (17 * (temp + 1));
- if (CheckIfRunAgain == 1)
- CheckForLine(aBlock, maxsize, linesclearedint, scorevalue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement