Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include "SDL_image.h"
- #include "SDL_ttf.h"
- #include <string>
- #include "constants.h"
- #include "classes.h"
- #include "functions.h"
- #include "globals.h"
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <sstream>
- //10 wide, 18 height. blocks 35 wide. going to be 640 high, 350 wide, then the remainder of it will be the next block
- int main( int argc, char* args[] )
- {
- srand( int( time( NULL ) ) );
- bool quit = false;
- Timer moveshapedown;
- Timer HoldKeyDown;
- if( init() == false )
- {
- return 1;
- }
- if( load_files() == false )
- {
- return 1;
- }
- SDL_Rect playablearea = {0, 0, 350, 640};
- int Scoreint = 0;
- std::stringstream ScoreDisplayText;
- ScoreDisplayText << Scoreint;
- int LinesClearedint = 0;
- std::stringstream LinesClearedDisplayText;
- LinesClearedDisplayText << LinesClearedint;
- SDL_Color WhiteTextColor = {255, 255, 255};
- ScoreNumberText = TTF_RenderText_Solid( font, ScoreDisplayText.str().c_str(), WhiteTextColor );
- LinesClearedNumberText = TTF_RenderText_Solid( font, LinesClearedDisplayText.str().c_str(), WhiteTextColor );
- int LinesClearedTextWidth = 0;
- int ScoreTextWidth = 0;
- int GameOverTextWidth = 0;
- int GameOverTextHeight = 0;
- int PlayAgainTextWidth = 0;
- int ScoreNumberTextWidth = 0;
- int LinesClearedNumberTextWidth = 0;
- int PlayAgain2TextWidth = 0;
- TTF_SizeText(font, "Lines", &LinesClearedTextWidth, 0);
- TTF_SizeText(font, "Score", &ScoreTextWidth, 0);
- TTF_SizeText(font, "Game Over", &GameOverTextWidth, &GameOverTextHeight);
- TTF_SizeText(font, "Press Enter To", &PlayAgainTextWidth, 0);
- TTF_SizeText(font, "Play again", &PlayAgain2TextWidth, 0);
- TTF_SizeText(font, "0", &ScoreNumberTextWidth, 0);
- TTF_SizeText(font, "9", &LinesClearedNumberTextWidth, 0);
- int LinesClearedTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - LinesClearedTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- int LinesClearedTextyLocation = 200;
- int LinesClearedNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - LinesClearedNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- int LinesClearedNumberTextyLocation = LinesClearedTextyLocation + 50;
- int ScoreTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- int ScoreTextyLocation = 500;
- int ScoreTextxLocation2 = ((SCREEN_WIDTH - ScoreTextWidth) / 2);
- int GameOverTextxLocation = ((SCREEN_WIDTH - GameOverTextWidth) / 2);
- int GameOverTextyLocation = ((SCREEN_HEIGHT - GameOverTextHeight) / 2) - 50;
- int PlayAgainTextxLocation = ((SCREEN_WIDTH - PlayAgainTextWidth) / 2);
- int PlayAgainTextyLocation = ((SCREEN_HEIGHT - GameOverTextHeight) / 2);
- int PlayAgain2xLocation = (SCREEN_WIDTH - PlayAgain2TextWidth) / 2;
- int ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- int ScoreNumberTextyLocation = ScoreTextyLocation + 50;
- //(SCREEN_WIDTH - TextWidth) / 2;
- int CenterText = 0;
- int ScoreTempHolder = 0;
- int LinesClearedTempHolder = 0;
- int MaxSize = 200;
- int ShapeIndex = 0, BlockIndex = 0, NextBlockShape = 0, tempvalue = 0;
- bool GetNewShapeCheck = 0, ClearLineCheck = 0;
- int BlockMarker[4] = {0};
- Block SidebarBlock[4];
- Shape SidebarShape;
- Block* myBlock = new Block[MaxSize];
- Shape myShape;
- NextBlockShape = 1 + (rand() % 7);
- myShape.SetShape(NextBlockShape, myBlock, MaxSize);
- NextBlockShape = 1 + (rand() % 7);
- SidebarShape.SpawnNextShapeInSidebar(NextBlockShape, SidebarBlock);
- moveshapedown.start();
- while( quit == false )
- {
- while( SDL_PollEvent( &event ) )
- {
- myShape.handle_input();
- if (myShape.GetIfKeyHeldDown() == 1)
- if (HoldKeyDown.is_started() == false)
- HoldKeyDown.start();
- if( event.type == SDL_QUIT )
- {
- quit = true;
- }
- }
- LinesClearedTempHolder = LinesClearedint;
- ScoreTempHolder = Scoreint;
- if (myShape.GetIfKeyHeldDown() == false)
- if (HoldKeyDown.is_started() == true)
- {
- HoldKeyDown.stop();
- myShape.SetFast(0);
- }
- if (myShape.GetIfKeyHeldDown() == true)
- if (HoldKeyDown.get_ticks() >= 400)
- myShape.SetFast(1);
- if (myShape.GetPause() == false)
- {
- myShape.Rotate(myBlock, myShape, MaxSize);
- GetNewShapeCheck = myShape.move(myBlock, myShape, MaxSize);
- if (GetNewShapeCheck == true)
- {
- myShape.SetOrientation(1);
- Scoreint += 17;
- }
- myShape.SpawnNewShapeIfNeeded(GetNewShapeCheck, myBlock, MaxSize, NextBlockShape, LinesClearedint, Scoreint);
- if (moveshapedown.get_ticks() >= 600)
- {
- GetNewShapeCheck = myShape.MoveShapeDown(myBlock, myShape, MaxSize);
- if (GetNewShapeCheck == true)
- myShape.SetOrientation(1);
- moveshapedown.start();
- }
- myShape.SpawnNewShapeIfNeeded(GetNewShapeCheck, myBlock, MaxSize, NextBlockShape, LinesClearedint, Scoreint);
- SidebarShape.SpawnNextShapeInSidebar(NextBlockShape, SidebarBlock);
- }
- SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 50, 40, 20 ) );
- SDL_FillRect( screen, &playablearea, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
- apply_surface( LinesClearedTextxLocation, 200, LinesClearedText, screen );
- if (Scoreint != ScoreTempHolder)
- {
- if (Scoreint > 9 && Scoreint < 99 && CenterText != 1)
- {
- TTF_SizeText(font, "00", &ScoreNumberTextWidth, 0);
- ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- CenterText = 1;
- }
- if (Scoreint > 99 && Scoreint < 999 && CenterText != 2)
- {
- TTF_SizeText(font, "000", &ScoreNumberTextWidth, 0);
- ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- CenterText = 2;
- }
- if (Scoreint > 999 && Scoreint < 9999 && CenterText != 3)
- {
- TTF_SizeText(font, "0000", &ScoreNumberTextWidth, 0);
- ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- CenterText = 3;
- }
- if (Scoreint > 9999 && Scoreint < 99999 && CenterText != 4)
- {
- TTF_SizeText(font, "00000", &ScoreNumberTextWidth, 0);
- ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- CenterText = 4;
- }
- if (Scoreint > 99999 && Scoreint < 999999 && CenterText != 5)
- {
- TTF_SizeText(font, "000000", &ScoreNumberTextWidth, 0);
- ScoreNumberTextxLocation = ((SCREEN_WIDTH - SCREEN_PLAYABLE_WIDTH - ScoreNumberTextWidth) / 2) + SCREEN_PLAYABLE_WIDTH;
- CenterText = 5;
- }
- ScoreDisplayText.str("");
- ScoreDisplayText << Scoreint;
- ScoreNumberText = TTF_RenderText_Solid( font, ScoreDisplayText.str().c_str(), WhiteTextColor );
- ScoreTempHolder = Scoreint;
- }
- if (LinesClearedint != LinesClearedTempHolder)
- {
- LinesClearedDisplayText.str("");
- LinesClearedDisplayText << LinesClearedint;
- LinesClearedNumberText = TTF_RenderText_Solid( font, LinesClearedDisplayText.str().c_str(), WhiteTextColor );
- LinesClearedTempHolder = LinesClearedint;
- }
- apply_surface( LinesClearedNumberTextxLocation, LinesClearedNumberTextyLocation, LinesClearedNumberText, screen );
- apply_surface( ScoreTextxLocation, ScoreTextyLocation, ScoreText, screen );
- apply_surface( ScoreNumberTextxLocation, ScoreNumberTextyLocation, ScoreNumberText, screen );
- BlockIndex = 0;
- while (BlockIndex < MaxSize)
- {
- myBlock[BlockIndex].DisplayBlocks();
- BlockIndex++;
- }
- BlockIndex = 0;
- while (BlockIndex < 4)
- {
- SidebarBlock[BlockIndex].DisplayBlocks();
- BlockIndex++;
- }
- if (myShape.CheckGameOver(myBlock, MaxSize) == true)
- {
- myShape.SetPause(1);
- SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0, 0, 0 ) );
- apply_surface( GameOverTextxLocation, GameOverTextyLocation, GameOverText, screen );
- apply_surface( PlayAgainTextxLocation, PlayAgainTextyLocation, PlayAgainText, screen );
- apply_surface( PlayAgain2xLocation, PlayAgainTextyLocation + 30, PlayAgain2Text, screen );
- apply_surface( ScoreNumberTextxLocation, ScoreNumberTextyLocation, ScoreNumberText, screen );
- apply_surface( ScoreTextxLocation, ScoreTextyLocation, ScoreText, screen );
- if (myShape.GetRestart() == true)
- {
- myShape.SetPause(0);
- BlockIndex = 0;
- while (BlockIndex < MaxSize - 1)
- {
- myBlock[BlockIndex].Sety(-1);
- myBlock[BlockIndex].Setx(-1);
- myBlock[BlockIndex].SetShowBlock(0);
- BlockIndex++;
- }
- myShape.SetRestart(false);
- GetNewShapeCheck = true;
- myShape.SpawnNewShapeIfNeeded(GetNewShapeCheck, myBlock, MaxSize, NextBlockShape, LinesClearedint, Scoreint);
- }
- }
- if( SDL_Flip( screen ) == -1 )
- {
- return 1;
- }
- }
- delete [] myBlock;
- myBlock = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement