Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
990 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Puzzlesquare Copyright (C) 2009 Aaron Greene | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "Pch.h" | ||
#include "AppPuzzlesquare.h" | ||
|
||
AppPuzzletile::AppPuzzletile(int squareLength): m_squareLength(squareLength), | ||
m_screen(NULL), | ||
m_square(NULL) | ||
{} | ||
|
||
bool AppPuzzletile::Init() | ||
{ | ||
if(SDL_Init(SDL_INIT_EVERYTHING) == -1) | ||
{ | ||
return false; | ||
} | ||
|
||
if(TTF_Init() == -1) | ||
{ | ||
return false; | ||
} | ||
|
||
SDL_Surface *tileImage = IMG_Load("square.png"); | ||
if (tileImage == NULL) | ||
{ | ||
return false; | ||
} | ||
|
||
TTF_Font *font = TTF_OpenFont("Verdana.ttf", 20); | ||
if (font == NULL) | ||
{ | ||
SDL_FreeSurface(tileImage); | ||
return false; | ||
} | ||
|
||
m_screen = SDL_SetVideoMode(m_squareLength * tileImage->h, m_squareLength * tileImage->h, 32, SDL_SWSURFACE); | ||
if (m_screen == NULL) | ||
{ | ||
TTF_CloseFont(font); | ||
SDL_FreeSurface(tileImage); | ||
return false; | ||
} | ||
|
||
SDL_Color textColor = {0, 0, 0}; //Black text | ||
m_square = new Square(m_squareLength, tileImage, font, textColor); | ||
|
||
SDL_WM_SetCaption("Puzzlesquare", NULL); | ||
|
||
TTF_CloseFont(font); | ||
SDL_FreeSurface(tileImage); | ||
return true; | ||
} | ||
|
||
void AppPuzzletile::Run() | ||
{ | ||
if (!Init()) | ||
{ | ||
CleanUp(); | ||
return; | ||
} | ||
|
||
m_square->Shuffle(); | ||
|
||
bool winner = false; | ||
int winnerTick = 0; | ||
SDL_Event event; | ||
|
||
while(true) | ||
{ | ||
int startTick = SDL_GetTicks(); | ||
|
||
while(SDL_PollEvent(&event)) | ||
{ | ||
if (!winner) | ||
{ | ||
m_square->HandleEvent(event); | ||
} | ||
} | ||
|
||
if (event.type == SDL_QUIT) | ||
{ | ||
break; //Quitter! quit game | ||
} | ||
|
||
SDL_FillRect(m_screen, &m_screen->clip_rect, SDL_MapRGB(m_screen->format, 135, 135, 135)); //Draw grey screen | ||
m_square->Draw(m_screen); | ||
|
||
if (winner) | ||
{ | ||
if ((SDL_GetTicks() - winnerTick) > 2500) | ||
{ | ||
break; //Winner! quit game | ||
} | ||
ShowWinnerMessage(); | ||
} | ||
else | ||
{ | ||
if (m_square->IsWinner()) | ||
{ | ||
winner = true; | ||
winnerTick = SDL_GetTicks(); | ||
} | ||
} | ||
|
||
SDL_Flip(m_screen); | ||
|
||
int deltaTime = SDL_GetTicks() - startTick; | ||
if(deltaTime < 1000 / FRAMES_PER_SECOND) | ||
{ | ||
SDL_Delay(( 1000 / FRAMES_PER_SECOND ) - deltaTime); | ||
} | ||
} | ||
|
||
CleanUp(); | ||
} | ||
|
||
void AppPuzzletile::ShowWinnerMessage() | ||
{ | ||
TTF_Font *font = TTF_OpenFont("Verdana.ttf", 45); | ||
SDL_Color textColor = {87, 174, 255}; //Blueish color | ||
SDL_Surface *message = TTF_RenderText_Solid(font, "You Won!", textColor); | ||
|
||
SDL_Rect offset; | ||
offset.x = (m_screen->w - message->w)/2; | ||
offset.y = (m_screen->h - message->h)/2; | ||
|
||
SDL_BlitSurface(message, NULL, m_screen, &offset); | ||
|
||
TTF_CloseFont(font); | ||
SDL_FreeSurface(message); | ||
} | ||
|
||
void AppPuzzletile::CleanUp() | ||
{ | ||
if (m_square != NULL) | ||
{ | ||
delete m_square; | ||
} | ||
|
||
SDL_FreeSurface(m_screen); | ||
SDL_Quit(); | ||
TTF_Quit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Puzzlesquare Copyright (C) 2009 Aaron Greene | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#pragma once | ||
#include "Square.h" | ||
|
||
#define FRAMES_PER_SECOND 40 | ||
|
||
class AppPuzzletile | ||
{ | ||
public: | ||
AppPuzzletile(int squareLength); | ||
void Run(); | ||
|
||
private: | ||
bool Init(); | ||
bool Draw(); | ||
void ShowWinnerMessage(); | ||
void CleanUp(); | ||
|
||
const int m_squareLength; | ||
SDL_Surface *m_screen; | ||
Square *m_square; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Puzzlesquare Copyright (C) 2009 Aaron Greene | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "Pch.h" | ||
#include "AppPuzzlesquare.h" | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
AppPuzzletile puzzleSquare(4); | ||
puzzleSquare.Run(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
TARGET = Puzzlesquare | ||
|
||
OBJDIRBASE = obj | ||
DEPDIRBASE = dep | ||
TARGETDIRBASE = bin | ||
|
||
CPP = g++ | ||
ECHO = echo | ||
RM = rm | ||
CP = cp | ||
MKDIR = mkdir | ||
MAKE = make | ||
EXIT = exit | ||
LS = ls | ||
|
||
CPPFILES = $(shell $(LS) -t *.cpp) | ||
|
||
CPPFLAGS = `sdl-config --cflags` | ||
LFLAGS = `sdl-config --libs` -lSDL_image -lSDL_ttf | ||
|
||
OBJDIR = $(OBJDIRBASE)/$(CFGSUBDIR) | ||
DEPDIR = $(DEPDIRBASE)/$(CFGSUBDIR) | ||
TARGETDIR = $(TARGETDIRBASE)/$(CFGSUBDIR) | ||
|
||
TARGETFILE = $(TARGETDIR)/$(TARGET) | ||
OBJFILES = $(CPPFILES:%.cpp=$(OBJDIR)/%.o) | ||
DEPFILES = $(CPPFILES:%.cpp=$(DEPDIR)/%.d) | ||
|
||
$(TARGETFILE): dirs $(OBJFILES) | ||
@$(ECHO) "Linking" $(TARGETFILE)... | ||
@$(CPP) $(LFLAGS) $(OBJFILES) -o $(TARGETFILE) | ||
@$(CP) square.png $(TARGETDIRBASE) | ||
@$(CP) Verdana.ttf $(TARGETDIRBASE) | ||
|
||
$(OBJDIR)/%.o: %.cpp | ||
@$(MKDIR) -p $(OBJDIR) $(DEPDIR) | ||
@$(ECHO) "Compiling" $*.cpp... | ||
@$(CPP) $(CPPFLAGS) -c $*.cpp -o $(OBJDIR)/$*.o | ||
@$(CPP) $(CPPFLAGS) -MM $*.cpp >> $(DEPDIR)/$*.d | ||
@$(ECHO) -n $(OBJDIR)"/" > $(DEPDIR)/$*.d ; $(CPP) $(CPPFLAGS) -MM $*.cpp >> $(DEPDIR)/$*.d | ||
|
||
dirs: | ||
@$(MKDIR) -p $(OBJDIR) $(DEPDIR) $(TARGETDIR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "Pch.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Puzzlesquare Copyright (C) 2009 Aaron Greene | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#ifndef PCH_H | ||
#define PCH_H | ||
|
||
#include <cstdlib> | ||
#include <ctime> | ||
#include <vector> | ||
#include <string> | ||
#include <SDL.h> | ||
#include <SDL_image.h> | ||
#include <SDL_ttf.h> | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> | ||
<CodeBlocks_project_file> | ||
<FileVersion major="1" minor="6" /> | ||
<Project> | ||
<Option title="Puzzlesquare" /> | ||
<Option pch_mode="2" /> | ||
<Option compiler="gcc" /> | ||
<Build> | ||
<Target title="Debug"> | ||
<Option output="bin/Debug/Puzzlesquare" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="obj/Debug/" /> | ||
<Option type="0" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-g" /> | ||
</Compiler> | ||
</Target> | ||
<Target title="Release"> | ||
<Option output="bin/Release/Puzzlesquare" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="obj/Release/" /> | ||
<Option type="0" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-O2" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-s" /> | ||
</Linker> | ||
</Target> | ||
</Build> | ||
<Compiler> | ||
<Add option="-Wall" /> | ||
<Add option="`sdl-config --cflags`" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="`sdl-config --libs`" /> | ||
<Add option="-lSDL_image" /> | ||
<Add option="-lSDL_ttf" /> | ||
</Linker> | ||
<Unit filename="AppPuzzlesquare.cpp" /> | ||
<Unit filename="AppPuzzlesquare.h" /> | ||
<Unit filename="Main.cpp" /> | ||
<Unit filename="Pch.h"> | ||
<Option compile="1" /> | ||
<Option weight="0" /> | ||
</Unit> | ||
<Unit filename="Square.cpp" /> | ||
<Unit filename="Square.h" /> | ||
<Extensions> | ||
<code_completion /> | ||
<envvars /> | ||
<lib_finder disable_auto="1" /> | ||
<debugger /> | ||
</Extensions> | ||
</Project> | ||
</CodeBlocks_project_file> |
Oops, something went wrong.