diff --git a/source/code/MenuSystem.cc b/source/code/MenuSystem.cc deleted file mode 100644 index 145ad56..0000000 --- a/source/code/MenuSystem.cc +++ /dev/null @@ -1,337 +0,0 @@ -/* -=========================================================================== -blockattack - Block Attack - Rise of the Blocks -Copyright (C) 2005-2012 Poul Sander - -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 2 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/ - -Source information and contacts persons can be found at -http://blockattack.sf.net -=========================================================================== -*/ - -#include - -#include "MenuSystem.h" -#include "common.h" -#include "CppSdlImageHolder.hpp" - -extern CppSdl::CppSdlImageHolder mouse; -extern SDL_Surface *backgroundImage; -extern bool highPriority; -extern int verboseLevel; -int mousex; -int mousey; - -/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/ -inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y) -{ - SDL_Rect dest; - dest.x = x; - dest.y = y; - SDL_BlitSurface(img, NULL, target, &dest); -} - -/*CppSdl::CppSdlImageHolder ButtonGfx::_marked; -CppSdl::CppSdlImageHolder ButtonGfx::_unmarked; -int ButtonGfx::xsize; -int ButtonGfx::ysize; -NFont ButtonGfx::thefont;*/ - -ButtonGfx standardButton; - -void ButtonGfx::setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked) -{ - ButtonGfx::_marked = marked; - ButtonGfx::_unmarked = unmarked; - xsize=(marked).GetWidth(); - ysize=(marked).GetHeight(); - if(verboseLevel) - cout << "Surfaces set, size: " <= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) - return true; - else - return false; -} - -void Button::doAction() -{ - if(action) - action(this); - else - cerr << "Warning: button \"" << label << "\" has no action assigned!"; -} - -void Button::drawTo(SDL_Surface **surface) -{ -#if DEBUG - //cout << "Painting button: " << label << " at: " << x << "," << y << endl; -#endif - if (marked) - gfx->_marked.PaintTo(*surface,x,y); - else - gfx->_unmarked.PaintTo(*surface,x,y); - //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; - //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2; - //ButtonGfx::ttf->writeText(label,surface,stringx,stringy); - gfx->thefont.setDest(*surface); - gfx->thefont.drawCenter(x+gfx->xsize/2,y+gfx->ysize/2-gfx->thefont.getHeight(label.c_str())/2,label.c_str()); -} - -void Button::setPopOnRun(bool popOnRun) -{ - this->popOnRun = popOnRun; -} - -bool Button::isPopOnRun() const -{ - return popOnRun; -} - -void Button::setGfx(ButtonGfx* gfx) -{ - this->gfx = gfx; -} - -int Button::getHeight() -{ - return this->gfx->ysize; -} - -void Menu::drawSelf() -{ - DrawIMG(backgroundImage,screen,0,0); - vector::iterator it; - for(it = buttons.begin(); it < buttons.end(); it++) - (*it)->drawTo(&screen); - exit.drawTo(&screen); - standardButton.thefont.draw(50,50,title.c_str()); - mouse.PaintTo(screen,mousex,mousey); -} - -void Menu::performClick(int x,int y) -{ - vector::iterator it; - for(it = buttons.begin(); it < buttons.end(); it++) - { - Button *b = (*it); - if(b->isClicked(x,y)) - b->doAction(); - if(b->isPopOnRun()) - running = false; - } - if(exit.isClicked(x,y)) - running = false; -} - -void Menu::placeButtons() -{ - int nextY = 100; - const int X = 50; - vector::iterator it; - for(it = buttons.begin(); it < buttons.end(); it++) - { - (*it)->x = X; - (*it)->y = nextY; - nextY += (*it)->getHeight()+10; - } - exit.x = X; - exit.y = nextY; -} - -void Menu::addButton(Button *b) -{ - buttons.push_back(b); - b->marked = false; - placeButtons(); -} - -Menu::Menu(SDL_Surface **screen) -{ - this->screen = *screen; - buttons = vector(10); - isSubmenu = true; - exit.setLabel( _("Back") ); -} - -Menu::Menu(SDL_Surface **screen,bool submenu) -{ - this->screen = *screen; - buttons = vector(0); - isSubmenu = submenu; - if(isSubmenu) - exit.setLabel( _("Back") ); - else - exit.setLabel( _("Exit") ); -} - -Menu::Menu(SDL_Surface** screen, string title, bool submenu) -{ - this->screen = *screen; - buttons = vector(0); - isSubmenu = submenu; - this->title = title; - if(isSubmenu) - exit.setLabel(_("Back") ); - else - exit.setLabel(_("Exit") ); -} - -void Menu::run() -{ - running = true; - bool bMouseUp = false; - long oldmousex = mousex; - long oldmousey = mousey; - while(running && !Config::getInstance()->isShuttingDown()) - { - if (!(highPriority)) SDL_Delay(10); - - - SDL_Event event; - - while ( SDL_PollEvent(&event) ) - { - if ( event.type == SDL_QUIT ) - { - Config::getInstance()->setShuttingDown(5); - running = false; - } - - if ( event.type == SDL_KEYDOWN ) - { - if ( event.key.keysym.sym == SDLK_ESCAPE ) - { - running = false; - } - - if (event.key.keysym.sym == SDLK_UP) - { - marked--; - if(marked<0) - marked = buttons.size(); //not -1, since exit is after the last element in the list - } - - if (event.key.keysym.sym == SDLK_DOWN) - { - marked++; - if(marked>buttons.size()) - marked = 0; - } - - if(event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) - { - if(marked < buttons.size()) - { - buttons.at(marked)->doAction(); - if(buttons.at(marked)->isPopOnRun()) - running = false; - } - if(marked == buttons.size()) - running = false; - } - } - - - } - - for(int i=0; imarked = (i == marked); - } - exit.marked = (marked == buttons.size()); - Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey); - // If the mouse button is released, make bMouseUp equal true - if ( (buttonState&SDL_BUTTON(1))==0) - { - bMouseUp=true; - } - - if(abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) - { - for(int i=0; i< buttons.size(); ++i) - { - if(buttons.at(i)->isClicked(mousex,mousey)) - { - marked = i; - } - } - if(exit.isClicked(mousex,mousey)) - { - marked = buttons.size(); - } - oldmousex = mousex; - oldmousey = mousey; - } - - //mouse clicked - if( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) - { - bMouseUp = false; - for(int i=0; i< buttons.size(); ++i) - { - if(buttons.at(i)->isClicked(mousex,mousey)) - { - buttons.at(i)->doAction(); - if(buttons.at(i)->isPopOnRun()) - running = false; - mousex = 0; - } - } - if(exit.isClicked(mousex,mousey)) - { - running = false; - } - } - - drawSelf(); - SDL_Flip(screen); - } -} diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp new file mode 100644 index 0000000..145ad56 --- /dev/null +++ b/source/code/MenuSystem.cpp @@ -0,0 +1,337 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2012 Poul Sander + +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 2 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/ + +Source information and contacts persons can be found at +http://blockattack.sf.net +=========================================================================== +*/ + +#include + +#include "MenuSystem.h" +#include "common.h" +#include "CppSdlImageHolder.hpp" + +extern CppSdl::CppSdlImageHolder mouse; +extern SDL_Surface *backgroundImage; +extern bool highPriority; +extern int verboseLevel; +int mousex; +int mousey; + +/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/ +inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y) +{ + SDL_Rect dest; + dest.x = x; + dest.y = y; + SDL_BlitSurface(img, NULL, target, &dest); +} + +/*CppSdl::CppSdlImageHolder ButtonGfx::_marked; +CppSdl::CppSdlImageHolder ButtonGfx::_unmarked; +int ButtonGfx::xsize; +int ButtonGfx::ysize; +NFont ButtonGfx::thefont;*/ + +ButtonGfx standardButton; + +void ButtonGfx::setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked) +{ + ButtonGfx::_marked = marked; + ButtonGfx::_unmarked = unmarked; + xsize=(marked).GetWidth(); + ysize=(marked).GetHeight(); + if(verboseLevel) + cout << "Surfaces set, size: " <= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) + return true; + else + return false; +} + +void Button::doAction() +{ + if(action) + action(this); + else + cerr << "Warning: button \"" << label << "\" has no action assigned!"; +} + +void Button::drawTo(SDL_Surface **surface) +{ +#if DEBUG + //cout << "Painting button: " << label << " at: " << x << "," << y << endl; +#endif + if (marked) + gfx->_marked.PaintTo(*surface,x,y); + else + gfx->_unmarked.PaintTo(*surface,x,y); + //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; + //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2; + //ButtonGfx::ttf->writeText(label,surface,stringx,stringy); + gfx->thefont.setDest(*surface); + gfx->thefont.drawCenter(x+gfx->xsize/2,y+gfx->ysize/2-gfx->thefont.getHeight(label.c_str())/2,label.c_str()); +} + +void Button::setPopOnRun(bool popOnRun) +{ + this->popOnRun = popOnRun; +} + +bool Button::isPopOnRun() const +{ + return popOnRun; +} + +void Button::setGfx(ButtonGfx* gfx) +{ + this->gfx = gfx; +} + +int Button::getHeight() +{ + return this->gfx->ysize; +} + +void Menu::drawSelf() +{ + DrawIMG(backgroundImage,screen,0,0); + vector::iterator it; + for(it = buttons.begin(); it < buttons.end(); it++) + (*it)->drawTo(&screen); + exit.drawTo(&screen); + standardButton.thefont.draw(50,50,title.c_str()); + mouse.PaintTo(screen,mousex,mousey); +} + +void Menu::performClick(int x,int y) +{ + vector::iterator it; + for(it = buttons.begin(); it < buttons.end(); it++) + { + Button *b = (*it); + if(b->isClicked(x,y)) + b->doAction(); + if(b->isPopOnRun()) + running = false; + } + if(exit.isClicked(x,y)) + running = false; +} + +void Menu::placeButtons() +{ + int nextY = 100; + const int X = 50; + vector::iterator it; + for(it = buttons.begin(); it < buttons.end(); it++) + { + (*it)->x = X; + (*it)->y = nextY; + nextY += (*it)->getHeight()+10; + } + exit.x = X; + exit.y = nextY; +} + +void Menu::addButton(Button *b) +{ + buttons.push_back(b); + b->marked = false; + placeButtons(); +} + +Menu::Menu(SDL_Surface **screen) +{ + this->screen = *screen; + buttons = vector(10); + isSubmenu = true; + exit.setLabel( _("Back") ); +} + +Menu::Menu(SDL_Surface **screen,bool submenu) +{ + this->screen = *screen; + buttons = vector(0); + isSubmenu = submenu; + if(isSubmenu) + exit.setLabel( _("Back") ); + else + exit.setLabel( _("Exit") ); +} + +Menu::Menu(SDL_Surface** screen, string title, bool submenu) +{ + this->screen = *screen; + buttons = vector(0); + isSubmenu = submenu; + this->title = title; + if(isSubmenu) + exit.setLabel(_("Back") ); + else + exit.setLabel(_("Exit") ); +} + +void Menu::run() +{ + running = true; + bool bMouseUp = false; + long oldmousex = mousex; + long oldmousey = mousey; + while(running && !Config::getInstance()->isShuttingDown()) + { + if (!(highPriority)) SDL_Delay(10); + + + SDL_Event event; + + while ( SDL_PollEvent(&event) ) + { + if ( event.type == SDL_QUIT ) + { + Config::getInstance()->setShuttingDown(5); + running = false; + } + + if ( event.type == SDL_KEYDOWN ) + { + if ( event.key.keysym.sym == SDLK_ESCAPE ) + { + running = false; + } + + if (event.key.keysym.sym == SDLK_UP) + { + marked--; + if(marked<0) + marked = buttons.size(); //not -1, since exit is after the last element in the list + } + + if (event.key.keysym.sym == SDLK_DOWN) + { + marked++; + if(marked>buttons.size()) + marked = 0; + } + + if(event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) + { + if(marked < buttons.size()) + { + buttons.at(marked)->doAction(); + if(buttons.at(marked)->isPopOnRun()) + running = false; + } + if(marked == buttons.size()) + running = false; + } + } + + + } + + for(int i=0; imarked = (i == marked); + } + exit.marked = (marked == buttons.size()); + Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey); + // If the mouse button is released, make bMouseUp equal true + if ( (buttonState&SDL_BUTTON(1))==0) + { + bMouseUp=true; + } + + if(abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) + { + for(int i=0; i< buttons.size(); ++i) + { + if(buttons.at(i)->isClicked(mousex,mousey)) + { + marked = i; + } + } + if(exit.isClicked(mousex,mousey)) + { + marked = buttons.size(); + } + oldmousex = mousex; + oldmousey = mousey; + } + + //mouse clicked + if( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) + { + bMouseUp = false; + for(int i=0; i< buttons.size(); ++i) + { + if(buttons.at(i)->isClicked(mousex,mousey)) + { + buttons.at(i)->doAction(); + if(buttons.at(i)->isPopOnRun()) + running = false; + mousex = 0; + } + } + if(exit.isClicked(mousex,mousey)) + { + running = false; + } + } + + drawSelf(); + SDL_Flip(screen); + } +} diff --git a/source/code/block.make b/source/code/block.make index d89c8a7..dc40544 100644 --- a/source/code/block.make +++ b/source/code/block.make @@ -43,61 +43,24 @@ endif BASE_LIBS += -lphysfs -$(BINARY): $(BUILDDIR)/main.o $(BUILDDIR)/highscore.o $(BUILDDIR)/ReadKeyboard.o $(BUILDDIR)/joypad.o $(BUILDDIR)/listFiles.o $(BUILDDIR)/replay.o $(BUILDDIR)/common.o $(BUILDDIR)/stats.o $(BUILDDIR)/CppSdlException.o $(BUILDDIR)/CppSdlImageHolder.o $(BUILDDIR)/nfont.o $(BUILDDIR)/MenuSystem.o $(BUILDDIR)/menudef.o - $(CPP) -O -o $(BINARY) $(BUILDDIR)/main.o $(BUILDDIR)/highscore.o $(BUILDDIR)/ReadKeyboard.o $(BUILDDIR)/joypad.o $(BUILDDIR)/listFiles.o $(BUILDDIR)/replay.o $(BUILDDIR)/common.o $(BUILDDIR)/stats.o $(BUILDDIR)/CppSdlException.o $(BUILDDIR)/CppSdlImageHolder.o $(BUILDDIR)/nfont.o $(BUILDDIR)/MenuSystem.o $(BUILDDIR)/menudef.o $(BASE_LIBS) -#-lphysfs - -$(BUILDDIR)/main.o: main.cpp mainVars.hpp common.h BlockGame.hpp BlockGame.cpp - $(CPP) $(BASE_CFLAGS) main.cpp -o $(BUILDDIR)/main.o - -$(BUILDDIR)/blockgame.o: BlockGame.hpp BlockGame.cpp - $(CPP) $(BASE_CFLAGS) BlockGame.cpp -o $(BUILDDIR)/blockgame.o - -$(BUILDDIR)/highscore.o: highscore.h highscore.cpp - $(CPP) $(BASE_CFLAGS) highscore.cpp -o $(BUILDDIR)/highscore.o - - -$(BUILDDIR)/ReadKeyboard.o: ReadKeyboard.h ReadKeyboard.cpp - $(CPP) $(BASE_CFLAGS) ReadKeyboard.cpp -o $(BUILDDIR)/ReadKeyboard.o - -$(BUILDDIR)/joypad.o: joypad.h joypad.cpp - $(CPP) $(BASE_CFLAGS) joypad.cpp -o $(BUILDDIR)/joypad.o - -$(BUILDDIR)/listFiles.o: listFiles.h listFiles.cpp - $(CPP) $(BASE_CFLAGS) listFiles.cpp -o $(BUILDDIR)/listFiles.o +OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o replay.o common.o stats.o CppSdlException.o CppSdlImageHolder.o Libs/NFont.o MenuSystem.o menudef.o -$(BUILDDIR)/replay.o: replay.h replay.cpp - $(CPP) $(BASE_CFLAGS) replay.cpp -o $(BUILDDIR)/replay.o - -$(BUILDDIR)/stats.o: stats.h stats.cc - $(CPP) $(BASE_CFLAGS) stats.cc -o $(BUILDDIR)/stats.o - -$(BUILDDIR)/common.o: common.h common.cc - $(CPP) $(BASE_CFLAGS) common.cc -o $(BUILDDIR)/common.o - -$(BUILDDIR)/nfont.o: Libs/NFont.h Libs/NFont.cpp - $(CPP) $(BASE_CFLAGS) Libs/NFont.cpp -o $(BUILDDIR)/nfont.o - -$(BUILDDIR)/menudef.o: menudef.cpp - $(CPP) $(BASE_CFLAGS) menudef.cpp -o $(BUILDDIR)/menudef.o - -#$(BUILDDIR)/uploadReplay.o: uploadReplay.cc uploadReplay.h -# $(CPP) $(BASE_CFLAGS) uploadReplay.cc -o $(BUILDDIR)/uploadReplay.o - -$(BUILDDIR)/MenuSystem.o: MenuSystem.cc MenuSystem.h - $(CPP) $(BASE_CFLAGS) MenuSystem.cc -o $(BUILDDIR)/MenuSystem.o - -#$(BUILDDIR)/ttfont.o: ttfont.h ttfont.cc -# $(CPP) $(BASE_CFLAGS) ttfont.cc -o $(BUILDDIR)/ttfont.o +$(BINARY): $(OFILES) + $(CPP) -O -o $(BINARY) $(OFILES) $(BASE_LIBS) +#-lphysfs -$(BUILDDIR)/CppSdlImageHolder.o: CppSdlImageHolder.hpp CppSdlImageHolder.cpp - $(CPP) $(BASE_CFLAGS) -o $(BUILDDIR)/CppSdlImageHolder.o CppSdlImageHolder.cpp +%.o : %.cpp + g++ -MD $(BASE_CFLAGS) -c -o $@ $< + @cp $*.d $*.P; \ + sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \ + -e '/^$$/ d' -e 's/$$/ :/' < $*.d >> $*.P; \ + rm -f $*.d -$(BUILDDIR)/CppSdlException.o: CppSdlException.hpp CppSdlException.cpp - $(CPP) $(BASE_CFLAGS) -o $(BUILDDIR)/CppSdlException.o CppSdlException.cpp +-include *.P run: $(BINARY) + cd $(GAMEDIR) && ./blockattack clean: - rm $(BUILDDIR)/*o + rm *.o *.P diff --git a/source/code/common.cc b/source/code/common.cc deleted file mode 100644 index eb88fda..0000000 --- a/source/code/common.cc +++ /dev/null @@ -1,324 +0,0 @@ -/* -=========================================================================== -blockattack - Block Attack - Rise of the Blocks -Copyright (C) 2005-2012 Poul Sander - -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 2 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/ - -Source information and contacts persons can be found at -http://blockattack.sf.net -=========================================================================== -*/ - -#include "common.h" -#include - -static stringstream converter; - -//Function to convert numbers to string -string itoa(int num) -{ - converter.str(std::string()); - converter.clear(); - converter << num; - return converter.str(); -} - -string double2str(double num) -{ - converter.str(std::string()); - converter.clear(); - converter << num; - return converter.str(); -} - -/** - * str2double parses a string and returns a double with the value of the string. - * if the string is not a double then 0.0 is returned instead of throing an error - * in that way this function will always return a useable value. - */ -double str2double(const string &str2parse) -{ - try - { - converter.clear(); - converter.str(str2parse); - double val = 0.0; - converter >> val; - return val; - } - catch(ios_base::failure &f) - { - return 0.0; - } -} - -/** - * str2int parses a string and returns an int with the value of the string. - * if the string is not an int then 0 is returned instead of throing an error - * in that way this function will always return a useable value. - */ -int str2int(const string &str2parse) -{ - try - { - converter.clear(); - converter.str(str2parse); - int val = 0; - converter >> val; - return val; - } - catch(ios_base::failure &f) - { - return 0; - } -} - -#ifdef WIN32 -//Returns path to "my Documents" in windows: -string getMyDocumentsPath() -{ - TCHAR pszPath[MAX_PATH]; - //if (SUCCEEDED(SHGetSpecialFolderPath(NULL, pszPath, CSIDL_PERSONAL, FALSE))) { - if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, pszPath))) - { - // pszPath is now the path that you want -#if DEBUG - cout << "MyDocuments Located: " << pszPath << endl; -#endif - string theResult= pszPath; - return theResult; - } - else - { - cout << "Warning: My Documents not found!" << endl; - string theResult =""; - return theResult; - } -} - -#endif - -/** - * Returns the path to where all settings must be saved. - * On unix-like systems this is the home-folder under: ~/.gamesaves/GAMENAME - * In Windows it is My Documents/My Games - * Consider changing this for Vista that has a special save games folder - */ -string getPathToSaveFiles() -{ -#ifdef __unix__ - return (string)getenv("HOME")+(string)"/.gamesaves/"+GAMENAME; -#elif WIN32 - return getMyDocumentsPath()+(string)"/My Games/"+GAMENAME; -#else - return "."; -#endif -} - -/** - * Takes a number of milliseconds and returns the value in commonTime format. - */ -commonTime TimeHandler::ms2ct(unsigned int milliseconds) -{ - commonTime ct; - ct.days = 0; - unsigned int time = milliseconds; - ct.hours = time/(1000*60*60); - time = time % (1000*60*60); - ct.minutes = time/(1000*60); - time = time % (1000*60); - ct.seconds = time/1000; - return ct; -} - -commonTime TimeHandler::getTime(const string &name) -{ - commonTime ct; - ct.days = Config::getInstance()->getInt(name+"Days"); - ct.hours = Config::getInstance()->getInt(name+"Hours"); - ct.minutes = Config::getInstance()->getInt(name+"Minutes"); - ct.seconds = Config::getInstance()->getInt(name+"Seconds"); - return ct; -} - -/** - * Returns the total runtime with toAdd added but without writing it to config file. - * Used for stats - */ -commonTime TimeHandler::peekTime(const string &name, const commonTime &toAdd) -{ - commonTime ct = getTime(name); - - ct.seconds +=toAdd.seconds; - ct.minutes +=ct.seconds/60; - ct.seconds = ct.seconds%60; - - ct.minutes += toAdd.minutes; - ct.hours += ct.minutes/60; - ct.minutes = ct.minutes%60; - - ct.hours += toAdd.hours; - ct.days += ct.hours/24; - ct.hours = ct.hours%24; - - ct.days += toAdd.days; - return ct; -} - -/** - * Same as peekTotalTime but writes the time to the config file. - * Should only be called only once! when the program shuts down - */ -commonTime TimeHandler::addTime(const string &name, const commonTime &toAdd) -{ - commonTime ct = peekTime(name,toAdd); - - Config::getInstance()->setInt(name+"Days",ct.days); - Config::getInstance()->setInt(name+"Hours",ct.hours); - Config::getInstance()->setInt(name+"Minutes",ct.minutes); - Config::getInstance()->setInt(name+"Seconds",ct.seconds); - return ct; -} - -Config* Config::instance = 0; - -Config::Config() -{ - configMap.clear(); - load(); - shuttingDown = 0; // Not shutting down -} - -void Config::load() -{ - string filename = getPathToSaveFiles()+"/configFile"; - ifstream inFile(filename.c_str()); - string key; - string previuskey; - char value[MAX_VAR_LENGTH]; - if(inFile) - { - while(!inFile.eof()) - { - inFile >> key; - if(key==previuskey) //the last entry will be read 2 times if a linebreak is missing in the end - continue; - previuskey = key; - inFile.get(); //Read the space between the key and the content - inFile.getline(value,MAX_VAR_LENGTH); -#if DEBUG - cout << "Config "<< "read: " << key << " with:\"" << value << "\"" << endl; -#endif - configMap[key] = (string)value; - } - inFile.close(); - } -} - -Config* Config::getInstance() -{ - if(Config::instance==0) - { - Config::instance = new Config(); - - } - return Config::instance; -} - -void Config::save() -{ - string filename = getPathToSaveFiles()+"/configFile"; - ofstream outFile(filename.c_str(),ios::trunc); - - if(outFile) - { - map::iterator iter; - for(iter = configMap.begin(); iter != configMap.end(); iter++) - { - outFile << iter->first << " " << iter->second << endl; - } - outFile << "\n"; //The last entry in the file will be read double if a linebreak is missing - //This is checked on load too in case a user changes it himself. - } - outFile.close(); -} - -bool Config::exists(const string &varName) const -{ - //Using that find returns an iterator to the end of the map if not found - return configMap.find(varName) != configMap.end(); -} - -void Config::setDefault(const string &varName,const string &content) -{ - if(exists(varName)) - return; //Already exists do not change - setString(varName,content); -} - -void Config::setShuttingDown(long shuttingDown) -{ - this->shuttingDown = shuttingDown; -} - -long Config::isShuttingDown() const -{ - return shuttingDown; -} - -void Config::setString(const string &varName, const string &content) -{ - configMap[varName] = content; -} - -void Config::setInt(const string &varName, int content) -{ - configMap[varName] = itoa(content); -} - -void Config::setValue(const string &varName,double content) -{ - configMap[varName] = double2str(content); -} - -string Config::getString(const string &varName) -{ - if(exists(varName)) - { - return configMap[varName]; - } - else - return ""; -} - -int Config::getInt(const string &varName) -{ - if(exists(varName)) - { - return str2int(configMap[varName]); - } - else - return 0; -} - -double Config::getValue(const string &varName) -{ - if(exists(varName)) - { - return str2double(configMap[varName]); - } - else - return 0.0; -} diff --git a/source/code/common.cpp b/source/code/common.cpp new file mode 100644 index 0000000..eb88fda --- /dev/null +++ b/source/code/common.cpp @@ -0,0 +1,324 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2012 Poul Sander + +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 2 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/ + +Source information and contacts persons can be found at +http://blockattack.sf.net +=========================================================================== +*/ + +#include "common.h" +#include + +static stringstream converter; + +//Function to convert numbers to string +string itoa(int num) +{ + converter.str(std::string()); + converter.clear(); + converter << num; + return converter.str(); +} + +string double2str(double num) +{ + converter.str(std::string()); + converter.clear(); + converter << num; + return converter.str(); +} + +/** + * str2double parses a string and returns a double with the value of the string. + * if the string is not a double then 0.0 is returned instead of throing an error + * in that way this function will always return a useable value. + */ +double str2double(const string &str2parse) +{ + try + { + converter.clear(); + converter.str(str2parse); + double val = 0.0; + converter >> val; + return val; + } + catch(ios_base::failure &f) + { + return 0.0; + } +} + +/** + * str2int parses a string and returns an int with the value of the string. + * if the string is not an int then 0 is returned instead of throing an error + * in that way this function will always return a useable value. + */ +int str2int(const string &str2parse) +{ + try + { + converter.clear(); + converter.str(str2parse); + int val = 0; + converter >> val; + return val; + } + catch(ios_base::failure &f) + { + return 0; + } +} + +#ifdef WIN32 +//Returns path to "my Documents" in windows: +string getMyDocumentsPath() +{ + TCHAR pszPath[MAX_PATH]; + //if (SUCCEEDED(SHGetSpecialFolderPath(NULL, pszPath, CSIDL_PERSONAL, FALSE))) { + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, pszPath))) + { + // pszPath is now the path that you want +#if DEBUG + cout << "MyDocuments Located: " << pszPath << endl; +#endif + string theResult= pszPath; + return theResult; + } + else + { + cout << "Warning: My Documents not found!" << endl; + string theResult =""; + return theResult; + } +} + +#endif + +/** + * Returns the path to where all settings must be saved. + * On unix-like systems this is the home-folder under: ~/.gamesaves/GAMENAME + * In Windows it is My Documents/My Games + * Consider changing this for Vista that has a special save games folder + */ +string getPathToSaveFiles() +{ +#ifdef __unix__ + return (string)getenv("HOME")+(string)"/.gamesaves/"+GAMENAME; +#elif WIN32 + return getMyDocumentsPath()+(string)"/My Games/"+GAMENAME; +#else + return "."; +#endif +} + +/** + * Takes a number of milliseconds and returns the value in commonTime format. + */ +commonTime TimeHandler::ms2ct(unsigned int milliseconds) +{ + commonTime ct; + ct.days = 0; + unsigned int time = milliseconds; + ct.hours = time/(1000*60*60); + time = time % (1000*60*60); + ct.minutes = time/(1000*60); + time = time % (1000*60); + ct.seconds = time/1000; + return ct; +} + +commonTime TimeHandler::getTime(const string &name) +{ + commonTime ct; + ct.days = Config::getInstance()->getInt(name+"Days"); + ct.hours = Config::getInstance()->getInt(name+"Hours"); + ct.minutes = Config::getInstance()->getInt(name+"Minutes"); + ct.seconds = Config::getInstance()->getInt(name+"Seconds"); + return ct; +} + +/** + * Returns the total runtime with toAdd added but without writing it to config file. + * Used for stats + */ +commonTime TimeHandler::peekTime(const string &name, const commonTime &toAdd) +{ + commonTime ct = getTime(name); + + ct.seconds +=toAdd.seconds; + ct.minutes +=ct.seconds/60; + ct.seconds = ct.seconds%60; + + ct.minutes += toAdd.minutes; + ct.hours += ct.minutes/60; + ct.minutes = ct.minutes%60; + + ct.hours += toAdd.hours; + ct.days += ct.hours/24; + ct.hours = ct.hours%24; + + ct.days += toAdd.days; + return ct; +} + +/** + * Same as peekTotalTime but writes the time to the config file. + * Should only be called only once! when the program shuts down + */ +commonTime TimeHandler::addTime(const string &name, const commonTime &toAdd) +{ + commonTime ct = peekTime(name,toAdd); + + Config::getInstance()->setInt(name+"Days",ct.days); + Config::getInstance()->setInt(name+"Hours",ct.hours); + Config::getInstance()->setInt(name+"Minutes",ct.minutes); + Config::getInstance()->setInt(name+"Seconds",ct.seconds); + return ct; +} + +Config* Config::instance = 0; + +Config::Config() +{ + configMap.clear(); + load(); + shuttingDown = 0; // Not shutting down +} + +void Config::load() +{ + string filename = getPathToSaveFiles()+"/configFile"; + ifstream inFile(filename.c_str()); + string key; + string previuskey; + char value[MAX_VAR_LENGTH]; + if(inFile) + { + while(!inFile.eof()) + { + inFile >> key; + if(key==previuskey) //the last entry will be read 2 times if a linebreak is missing in the end + continue; + previuskey = key; + inFile.get(); //Read the space between the key and the content + inFile.getline(value,MAX_VAR_LENGTH); +#if DEBUG + cout << "Config "<< "read: " << key << " with:\"" << value << "\"" << endl; +#endif + configMap[key] = (string)value; + } + inFile.close(); + } +} + +Config* Config::getInstance() +{ + if(Config::instance==0) + { + Config::instance = new Config(); + + } + return Config::instance; +} + +void Config::save() +{ + string filename = getPathToSaveFiles()+"/configFile"; + ofstream outFile(filename.c_str(),ios::trunc); + + if(outFile) + { + map::iterator iter; + for(iter = configMap.begin(); iter != configMap.end(); iter++) + { + outFile << iter->first << " " << iter->second << endl; + } + outFile << "\n"; //The last entry in the file will be read double if a linebreak is missing + //This is checked on load too in case a user changes it himself. + } + outFile.close(); +} + +bool Config::exists(const string &varName) const +{ + //Using that find returns an iterator to the end of the map if not found + return configMap.find(varName) != configMap.end(); +} + +void Config::setDefault(const string &varName,const string &content) +{ + if(exists(varName)) + return; //Already exists do not change + setString(varName,content); +} + +void Config::setShuttingDown(long shuttingDown) +{ + this->shuttingDown = shuttingDown; +} + +long Config::isShuttingDown() const +{ + return shuttingDown; +} + +void Config::setString(const string &varName, const string &content) +{ + configMap[varName] = content; +} + +void Config::setInt(const string &varName, int content) +{ + configMap[varName] = itoa(content); +} + +void Config::setValue(const string &varName,double content) +{ + configMap[varName] = double2str(content); +} + +string Config::getString(const string &varName) +{ + if(exists(varName)) + { + return configMap[varName]; + } + else + return ""; +} + +int Config::getInt(const string &varName) +{ + if(exists(varName)) + { + return str2int(configMap[varName]); + } + else + return 0; +} + +double Config::getValue(const string &varName) +{ + if(exists(varName)) + { + return str2double(configMap[varName]); + } + else + return 0.0; +} diff --git a/source/code/stats.cc b/source/code/stats.cc deleted file mode 100644 index b2782a1..0000000 --- a/source/code/stats.cc +++ /dev/null @@ -1,107 +0,0 @@ -/* -=========================================================================== -blockattack - Block Attack - Rise of the Blocks -Copyright (C) 2005-2012 Poul Sander - -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 2 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/ - -Source information and contacts persons can be found at -http://blockattack.sf.net -=========================================================================== -*/ - -#include "stats.h" -#include "common.h" - -Stats* Stats::instance = NULL; - -Stats::Stats() -{ - statMap.clear(); - load(); -} - -void Stats::load() -{ - string filename = getPathToSaveFiles()+"/statsFile"; - ifstream inFile(filename.c_str()); - string key; - char value[MAX_VAR_LENGTH]; - if(inFile) - { - while(!inFile.eof()) - { - inFile >> key; // The key is first on line - inFile.get(); //Take the space - inFile.getline(value,MAX_VAR_LENGTH); //The rest of the line is the value. - statMap[key] = str2int(value); - } - inFile.close(); - } -} - -Stats* Stats::getInstance() -{ - if(Stats::instance==NULL) - { - Stats::instance = new Stats(); - - } - return Stats::instance; -} - -void Stats::save() -{ - string filename = getPathToSaveFiles()+"/statsFile"; - ofstream outFile(filename.c_str(),ios::trunc); - - if(outFile) - { - //outFile << statMap.size() << endl; - map::iterator iter; - for(iter = statMap.begin(); iter != statMap.end(); iter++) - { - outFile << iter->first << " " << iter->second << endl; - } - } -} - -unsigned int Stats::getNumberOf(const string &statName) -{ - if(exists(statName)) - { - return statMap[statName]; - } - else - return 0; -} - -void Stats::addOne(const string &statName) -{ - map::iterator iter = statMap.find(statName); - if(iter == statMap.end()) - { - statMap[statName] = 1; - } - else - { - iter->second++; - } -} - -bool Stats::exists(const string &statName) -{ - //Using that 'find' returns an iterator to the end of the map if not found - return statMap.find(statName) != statMap.end(); -} diff --git a/source/code/stats.cpp b/source/code/stats.cpp new file mode 100644 index 0000000..b2782a1 --- /dev/null +++ b/source/code/stats.cpp @@ -0,0 +1,107 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2012 Poul Sander + +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 2 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/ + +Source information and contacts persons can be found at +http://blockattack.sf.net +=========================================================================== +*/ + +#include "stats.h" +#include "common.h" + +Stats* Stats::instance = NULL; + +Stats::Stats() +{ + statMap.clear(); + load(); +} + +void Stats::load() +{ + string filename = getPathToSaveFiles()+"/statsFile"; + ifstream inFile(filename.c_str()); + string key; + char value[MAX_VAR_LENGTH]; + if(inFile) + { + while(!inFile.eof()) + { + inFile >> key; // The key is first on line + inFile.get(); //Take the space + inFile.getline(value,MAX_VAR_LENGTH); //The rest of the line is the value. + statMap[key] = str2int(value); + } + inFile.close(); + } +} + +Stats* Stats::getInstance() +{ + if(Stats::instance==NULL) + { + Stats::instance = new Stats(); + + } + return Stats::instance; +} + +void Stats::save() +{ + string filename = getPathToSaveFiles()+"/statsFile"; + ofstream outFile(filename.c_str(),ios::trunc); + + if(outFile) + { + //outFile << statMap.size() << endl; + map::iterator iter; + for(iter = statMap.begin(); iter != statMap.end(); iter++) + { + outFile << iter->first << " " << iter->second << endl; + } + } +} + +unsigned int Stats::getNumberOf(const string &statName) +{ + if(exists(statName)) + { + return statMap[statName]; + } + else + return 0; +} + +void Stats::addOne(const string &statName) +{ + map::iterator iter = statMap.find(statName); + if(iter == statMap.end()) + { + statMap[statName] = 1; + } + else + { + iter->second++; + } +} + +bool Stats::exists(const string &statName) +{ + //Using that 'find' returns an iterator to the end of the map if not found + return statMap.find(statName) != statMap.end(); +} diff --git a/source/code/ttfont.cc b/source/code/ttfont.cc deleted file mode 100644 index 46fe677..0000000 --- a/source/code/ttfont.cc +++ /dev/null @@ -1,140 +0,0 @@ -/* -=========================================================================== -blockattack - Block Attack - Rise of the Blocks -Copyright (C) 2005-2012 Poul Sander - -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 2 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/ - -Source information and contacts persons can be found at -http://blockattack.sf.net -=========================================================================== -*/ - -#include "ttfont.h" - -//extern SDL_Surface *tmp; - -//#define CONVERTA(n) tmp = SDL_DisplayFormatAlpha(n); SDL_FreeSurface(n); n = tmp - -TTFont::TTFont() -{ - font = NULL; - actualInstance = false; -} - -int TTFont::count = 0; - -TTFont::TTFont(TTF_Font *f) -{ - if(!(TTF_WasInit())) - { - //Init TTF for the first time - TTF_Init(); - } - - - if(f == NULL) - cout << "Font was null!" << endl; - - actualInstance = false; //We have not yet copied to final location - font = f; - -} - -TTFont::~TTFont() -{ - if(!actualInstance) - return; - - cout << "Closing a font" << endl; - - TTF_CloseFont(font); - font = NULL; - - count--; - if(count==0) - TTF_Quit(); -} - -//Copy constructor, you cannot copy an actual instance -TTFont::TTFont(const TTFont &t) -{ - if(t.font == NULL || t.actualInstance) - { - font = NULL; - ac/* -Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack -Copyright (C) 2008 Poul Sander - - 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 2 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, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - Poul Sander - R�vehjvej 36, V. 1111 - 2800 Kgs. Lyngby - DENMARK - blockattack@poulsander.com - http://blockattack.sf.net -*/tualInstance = false; - } - else - { - font = t.font; - actualInstance = true; - count++; - } -} - -int TTFont::getTextHeight() -{ - return TTF_FontHeight(font); -} - -int TTFont::getTextWidth(string text) -{ - int width = 0; - if(TTF_SizeText(font,text.c_str(),&width,NULL)!=0) - cout << "Failed to get text width!" << endl; - return width; -} - -void TTFont::writeText(string text, SDL_Surface *target, int x, int y) -{ - SDL_Surface *text_surface; - SDL_Color color= {255,255,255}; - if(!(text_surface=TTF_RenderText_Solid(font,text.c_str(), color))) - { - cout << "Error writing text: " << TTF_GetError() << endl; - } - else - { - SDL_Rect dest; - dest.x = x; - dest.y = y; - SDL_BlitSurface(text_surface,NULL,target,&dest); - SDL_FreeSurface(text_surface); - } -} - diff --git a/source/code/ttfont.cpp b/source/code/ttfont.cpp new file mode 100644 index 0000000..46fe677 --- /dev/null +++ b/source/code/ttfont.cpp @@ -0,0 +1,140 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2012 Poul Sander + +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 2 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/ + +Source information and contacts persons can be found at +http://blockattack.sf.net +=========================================================================== +*/ + +#include "ttfont.h" + +//extern SDL_Surface *tmp; + +//#define CONVERTA(n) tmp = SDL_DisplayFormatAlpha(n); SDL_FreeSurface(n); n = tmp + +TTFont::TTFont() +{ + font = NULL; + actualInstance = false; +} + +int TTFont::count = 0; + +TTFont::TTFont(TTF_Font *f) +{ + if(!(TTF_WasInit())) + { + //Init TTF for the first time + TTF_Init(); + } + + + if(f == NULL) + cout << "Font was null!" << endl; + + actualInstance = false; //We have not yet copied to final location + font = f; + +} + +TTFont::~TTFont() +{ + if(!actualInstance) + return; + + cout << "Closing a font" << endl; + + TTF_CloseFont(font); + font = NULL; + + count--; + if(count==0) + TTF_Quit(); +} + +//Copy constructor, you cannot copy an actual instance +TTFont::TTFont(const TTFont &t) +{ + if(t.font == NULL || t.actualInstance) + { + font = NULL; + ac/* +Block Attack - Rise of the Blocks, SDL game, besed on Nintendo's Tetris Attack +Copyright (C) 2008 Poul Sander + + 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 2 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, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Poul Sander + R�vehjvej 36, V. 1111 + 2800 Kgs. Lyngby + DENMARK + blockattack@poulsander.com + http://blockattack.sf.net +*/tualInstance = false; + } + else + { + font = t.font; + actualInstance = true; + count++; + } +} + +int TTFont::getTextHeight() +{ + return TTF_FontHeight(font); +} + +int TTFont::getTextWidth(string text) +{ + int width = 0; + if(TTF_SizeText(font,text.c_str(),&width,NULL)!=0) + cout << "Failed to get text width!" << endl; + return width; +} + +void TTFont::writeText(string text, SDL_Surface *target, int x, int y) +{ + SDL_Surface *text_surface; + SDL_Color color= {255,255,255}; + if(!(text_surface=TTF_RenderText_Solid(font,text.c_str(), color))) + { + cout << "Error writing text: " << TTF_GetError() << endl; + } + else + { + SDL_Rect dest; + dest.x = x; + dest.y = y; + SDL_BlitSurface(text_surface,NULL,target,&dest); + SDL_FreeSurface(text_surface); + } +} + diff --git a/source/code/uploadReplay.cc b/source/code/uploadReplay.cc deleted file mode 100644 index 61519e2..0000000 --- a/source/code/uploadReplay.cc +++ /dev/null @@ -1,86 +0,0 @@ -/* -=========================================================================== -blockattack - Block Attack - Rise of the Blocks -Copyright (C) 2005-2012 Poul Sander - -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 2 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/ - -Source information and contacts persons can be found at -http://blockattack.sf.net -=========================================================================== -*/ - -#include "uploadReplay.h" -#include "common.h" - -bool isDownloading; -bool uploadReplayInitialized; -bool thresholdUp2Date; - -SDL_Thread *threadThresholdFile; - -void uploadReplay_init() -{ - if(uploadReplayInitialized) - return; - //mutDownload = SDL_CreateMutex(); - mutUpload = SDL_CreateMutex(); - thresholdUp2Date = false; -} - -int downloadThresholdFile(void *ptr) -{ - //if(SDL_mutexP(mutDownload)==-1){ - // isDownloading = false; - // return -1; //We don't crash if we can't lock. Just failing to do so - //} - FILE *outfile; - CURL *easyhandle = curl_easy_init(); - if(!easyhandle) - { - isDownloading=false; - //SDL_mutexV(mutDownload); - return -1; - } - string filename = getPathToSaveFiles()+"/scoreThreshold"; - outfile = fopen(filename.c_str(), "w"); - - curl_easy_setopt(easyhandle, CURLOPT_URL, "http://localhost/~poul/blockattackHighscores/result/threshold"); - curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, outfile); - - CURLcode res = curl_easy_perform(easyhandle); - - fclose(outfile); - - if(res == 200) - thresholdUp2Date = true; - - curl_easy_cleanup(easyhandle); - isDownloading=false; - //if(SDL_mutexV(mutDownload)==-1){ - //return 0; //We don't crash if we can't lock. Just failing to do so - //} - return 0; -} - -bool uploadReplay_canUpload() -{ - if(thresholdUp2Date) - return true; - if(isDownloading) - return false; - isDownloading=true; //We mark before start the thread - threadThresholdFile = SDL_CreateThread(downloadThresholdFile, NULL); - return thresholdUp2Date; -} diff --git a/source/code/uploadReplay.cpp b/source/code/uploadReplay.cpp new file mode 100644 index 0000000..61519e2 --- /dev/null +++ b/source/code/uploadReplay.cpp @@ -0,0 +1,86 @@ +/* +=========================================================================== +blockattack - Block Attack - Rise of the Blocks +Copyright (C) 2005-2012 Poul Sander + +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 2 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/ + +Source information and contacts persons can be found at +http://blockattack.sf.net +=========================================================================== +*/ + +#include "uploadReplay.h" +#include "common.h" + +bool isDownloading; +bool uploadReplayInitialized; +bool thresholdUp2Date; + +SDL_Thread *threadThresholdFile; + +void uploadReplay_init() +{ + if(uploadReplayInitialized) + return; + //mutDownload = SDL_CreateMutex(); + mutUpload = SDL_CreateMutex(); + thresholdUp2Date = false; +} + +int downloadThresholdFile(void *ptr) +{ + //if(SDL_mutexP(mutDownload)==-1){ + // isDownloading = false; + // return -1; //We don't crash if we can't lock. Just failing to do so + //} + FILE *outfile; + CURL *easyhandle = curl_easy_init(); + if(!easyhandle) + { + isDownloading=false; + //SDL_mutexV(mutDownload); + return -1; + } + string filename = getPathToSaveFiles()+"/scoreThreshold"; + outfile = fopen(filename.c_str(), "w"); + + curl_easy_setopt(easyhandle, CURLOPT_URL, "http://localhost/~poul/blockattackHighscores/result/threshold"); + curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, outfile); + + CURLcode res = curl_easy_perform(easyhandle); + + fclose(outfile); + + if(res == 200) + thresholdUp2Date = true; + + curl_easy_cleanup(easyhandle); + isDownloading=false; + //if(SDL_mutexV(mutDownload)==-1){ + //return 0; //We don't crash if we can't lock. Just failing to do so + //} + return 0; +} + +bool uploadReplay_canUpload() +{ + if(thresholdUp2Date) + return true; + if(isDownloading) + return false; + isDownloading=true; //We mark before start the thread + threadThresholdFile = SDL_CreateThread(downloadThresholdFile, NULL); + return thresholdUp2Date; +}