diff --git a/source/code/DialogBox.cpp b/source/code/DialogBox.cpp new file mode 100644 index 0000000..77feb1e --- /dev/null +++ b/source/code/DialogBox.cpp @@ -0,0 +1,133 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: DialogBox.cpp + * Author: poul + * + * Created on 14. februar 2016, 14:56 + */ + +#include "DialogBox.hpp" +#include "global.hpp" +#include "common.h" +#include "ReadKeyboard.h" + +static void NFont_Write(SDL_Renderer* target, int x, int y, const std::string& text) { + nf_standard_blue_font.draw(target, x, y, "%s", text.c_str()); +} + +#if 0 +bool OpenDialogbox(int x, int y, std::string& name) { + bool done = false; //We are done! + bool accept = false; //New name is accepted! (not Cancelled) + SDL_TextInput textInputScope; + ReadKeyboard rk = ReadKeyboard(name.c_str()); + std::string strHolder; + backgroundImage.Draw(screen, SDL_GetTicks(), 0, 0); + while (!done && !Config::getInstance()->isShuttingDown()) { + dialogBox.Draw(screen, SDL_GetTicks(), x, y); + NFont_Write(screen, x+40,y+76,rk.GetString()); + strHolder = rk.GetString(); + strHolder.erase((int)rk.CharsBeforeCursor()); + + if (((SDL_GetTicks()/600)%2)==1) { + NFont_Write(screen, x+40+nf_standard_blue_font.getWidth( "%s", strHolder.c_str()),y+76,"|"); + } + + SDL_Event event; + + while ( SDL_PollEvent(&event) ) { + if ( event.type == SDL_QUIT ) { + Config::getInstance()->setShuttingDown(5); + done = true; + accept = false; + } + + + + } //while(event) + + SDL_RenderPresent(screen); //Update screen + } //while(!done) + name = rk.GetString(); + return accept; +} +#endif + +bool OpenDialogbox(int x, int y, std::string& name) { + DialogBox d(x, y, name); + RunGameState(d); + if (d.IsUpdated()) { + name = d.GetName(); + return true; + } + return false; +} + +DialogBox::DialogBox(int x, int y, const std::string& name) { + this->x = x; + this->y = y; + SetName(name); +} + + +DialogBox::~DialogBox() { +} + +bool DialogBox::IsActive() { + return isActive; +} + + +void DialogBox::Draw(SDL_Renderer* target) { + backgroundImage.Draw(screen, SDL_GetTicks(), 0, 0); + dialogBox.Draw(screen, SDL_GetTicks(), x, y); + NFont_Write(screen, x+40,y+76,rk->GetString()); + std::string strHolder = rk->GetString(); + strHolder.erase((int)rk->CharsBeforeCursor()); + + if (((SDL_GetTicks()/600)%2)==1) { + NFont_Write(screen, x+40+nf_standard_blue_font.getWidth( "%s", strHolder.c_str()),y+76,"|"); + } +} + +void DialogBox::ProcessInput(const SDL_Event& event, bool &processed) { + if (event.type == SDL_TEXTINPUT) { + if ((rk->ReadKey(event))&&(SoundEnabled)&&(!NoSound)) { + Mix_PlayChannel(1, typingChunk, 0); + } + } + + if ( event.type == SDL_KEYDOWN ) { + if ( (event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER) ) { + name = rk->GetString(); + updated = true; + isActive = false; + } + else if ( (event.key.keysym.sym == SDLK_ESCAPE) ) { + isActive = false; + } + else { + if ((rk->ReadKey(event))&&(SoundEnabled)&&(!NoSound)) { + Mix_PlayChannel(1,typingChunk,0); + } + } + } +} + +void DialogBox::SetName(const std::string& name) { + this->name = name; + rk = std::make_shared(name.c_str()); +} + +std::string DialogBox::GetName() const { + return name; +} + +bool DialogBox::IsUpdated() const { + return updated; +} \ No newline at end of file diff --git a/source/code/DialogBox.hpp b/source/code/DialogBox.hpp new file mode 100644 index 0000000..a0543f8 --- /dev/null +++ b/source/code/DialogBox.hpp @@ -0,0 +1,48 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: DialogBox.hpp + * Author: poul + * + * Created on 14. februar 2016, 14:56 + */ + +#ifndef DIALOGBOX_HPP +#define DIALOGBOX_HPP + +#include "sago/GameStateInterface.hpp" +#include +#include "ReadKeyboard.h" +#include +#include "scopeHelpers.hpp" + +bool OpenDialogbox(int x, int y, std::string& name); + +class DialogBox : public sago::GameStateInterface { +public: + DialogBox(int x, int y, const std::string& name); + DialogBox(const DialogBox& orig) = delete; + virtual ~DialogBox(); + + bool IsActive() override; + void Draw(SDL_Renderer* target) override; + void ProcessInput(const SDL_Event& event, bool &processed) override; + void SetName(const std::string& name); + std::string GetName() const; + bool IsUpdated() const; +private: + bool isActive = true; + std::shared_ptr rk; + int x; + int y; + std::string name; + bool updated = false; + SDL_TextInput textInputScope; +}; + +#endif /* DIALOGBOX_HPP */ + diff --git a/source/code/ReadKeyboard.h b/source/code/ReadKeyboard.h index 40a50e9..e9f44d5 100644 --- a/source/code/ReadKeyboard.h +++ b/source/code/ReadKeyboard.h @@ -25,6 +25,9 @@ http://blockattack.net Added to project 5/11-2004 */ +#ifndef READKEYBOARD_HPP +#define READKEYBOARD_HPP + #include "SDL.h" #include @@ -45,3 +48,5 @@ public: bool ReadKey(SDL_Keycode); //true if key accepted const std::string& GetString(void) const; }; + +#endif //READKEYBOARD_HPP \ No newline at end of file diff --git a/source/code/common.cpp b/source/code/common.cpp index e039cd0..5595edf 100644 --- a/source/code/common.cpp +++ b/source/code/common.cpp @@ -26,6 +26,7 @@ http://blockattack.net #include #include "os.hpp" #include "sago/SagoMiscSdl2.hpp" +#include "sago/SagoMisc.hpp" #include #include "physfs_stream.hpp" @@ -205,17 +206,14 @@ Config* Config::getInstance() { } void Config::save() { - PhysFS::ofstream outFile("configFile"); - - 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(); + std::stringstream 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. + sago::WriteFileContent("configFile", outFile.str()); } bool Config::exists(const string& varName) const { diff --git a/source/code/global.hpp b/source/code/global.hpp index 24f8d25..82de425 100644 --- a/source/code/global.hpp +++ b/source/code/global.hpp @@ -28,15 +28,18 @@ http://blockattack.net #include #include "sago/SagoSpriteHolder.hpp" #include "highscore.h" +#include "sago/GameStateInterface.hpp" void MainMenu(); void ResetFullscreen(); +void RunGameState(sago::GameStateInterface& state ); extern sago::SagoSprite menuMarked; extern sago::SagoSprite menuUnmarked; extern sago::SagoSprite bHighScore; extern sago::SagoSprite bBack; extern sago::SagoSprite bNext; +extern sago::SagoSprite dialogBox; extern NFont nf_scoreboard_font; extern NFont nf_standard_blue_font; extern NFont nf_button_font; @@ -46,9 +49,11 @@ extern bool bFullscreen; //true if game is running fullscreen extern std::string player1name; extern std::string player2name; extern SDL_Renderer *screen; //The whole screen; +extern Mix_Chunk *typingChunk; extern sago::SagoSprite mouse; extern sago::SagoSprite backgroundImage; extern bool highPriority; +extern bool NoSound; extern int verboseLevel; extern Highscore theTopScoresEndless; //Stores highscores for endless extern Highscore theTopScoresTimeTrial; //Stores highscores for timetrial diff --git a/source/code/highscore.cpp b/source/code/highscore.cpp index 4314fbf..516f95e 100644 --- a/source/code/highscore.cpp +++ b/source/code/highscore.cpp @@ -64,9 +64,9 @@ void Highscore::writeFile() { filename = filename2; } - PhysFS::ofstream outfile(filename.c_str(), ios::binary); + PhysFS::ofstream outfile(filename.c_str()); if (!outfile) { - cout << "Error writing to file: " << filename << endl; + cout << "Error writing to file: " << filename << ", Error: " << PHYSFS_getLastError() << endl; exit(1); } for (int i = 0; iisShuttingDown()) { - DrawIMG(dialogBox,screen,x,y); - NFont_Write(screen, x+40,y+76,rk.GetString()); - strHolder = rk.GetString(); - strHolder.erase((int)rk.CharsBeforeCursor()); - - if (((SDL_GetTicks()/600)%2)==1) { - NFont_Write(screen, x+40+nf_standard_blue_font.getWidth( "%s", strHolder.c_str()),y+76,"|"); - } - - SDL_Event event; - - while ( SDL_PollEvent(&event) ) { - if ( event.type == SDL_QUIT ) { - Config::getInstance()->setShuttingDown(5); - done = true; - accept = false; - } - - if (event.type == SDL_TEXTINPUT) { - if ((rk.ReadKey(event))&&(SoundEnabled)&&(!NoSound)) { - Mix_PlayChannel(1,typingChunk,0); - } - } - - if ( event.type == SDL_KEYDOWN ) { - if ( (event.key.keysym.sym == SDLK_RETURN)||(event.key.keysym.sym == SDLK_KP_ENTER) ) { - done = true; - accept = true; - } - else if ( (event.key.keysym.sym == SDLK_ESCAPE) ) { - done = true; - accept = false; - } - else { - if ((rk.ReadKey(event))&&(SoundEnabled)&&(!NoSound)) { - Mix_PlayChannel(1,typingChunk,0); - } - } - } - - } //while(event) - - SDL_RenderPresent(screen); //Update screen - } //while(!done) - name = rk.GetString(); - bScreenLocked = false; - showDialog = false; - return accept; -} - - void RunGameState(sago::GameStateInterface& state ) { int mousex,mousey; bool done = false; //We are done! @@ -1711,12 +1651,13 @@ int main(int argc, char* argv[]) { cout << savepath << endl; return 0; } + + PhysFsSetSearchPath(search_paths, savepath); //Os create folders must be after the paramters because they can change the home folder PhysFsCreateFolders(); SoundEnabled = true; MusicEnabled = true; - bScreenLocked = false; twoPlayers = false; //true if two players splitscreen theTopScoresEndless = Highscore(1); theTopScoresTimeTrial = Highscore(2); @@ -1758,7 +1699,7 @@ int main(int argc, char* argv[]) { //Copyright notice: cout << "Block Attack - Rise of the Blocks (" << VERSION_NUMBER << ")" << endl << "http://www.blockattack.net" << endl << "Copyright 2004-2016 Poul Sander" << endl << "A SDL2 based game (see www.libsdl.org)" << endl << - "The game is available under the GPL, see COPYING for details." << endl; + "The game is availeble under the GPL, see COPYING for details." << endl; cout << "-------------------------------------------" << endl; } @@ -1873,7 +1814,6 @@ int main(int argc, char* argv[]) { //SDL_RenderSetLogicalSize(renderer, xsize, ysize); screen = renderer; - PhysFsSetSearchPath(search_paths, savepath); sago::SagoDataHolder d(renderer); d.setVerbose(false); sago::SagoSpriteHolder spriteholder(d); @@ -1962,20 +1902,20 @@ int main(int argc, char* argv[]) { Config::getInstance()->save(); - //Close file system Apstraction layer! - PHYSFS_deinit(); + } catch (exception& e) { sago::SagoFatalError(e.what()); } + //Close file system Apstraction layer! + PHYSFS_deinit(); return 0; } int runGame(int gametype, int level) { int mousex, mousey; //Mouse coordinates - bScreenLocked = false; theTopScoresEndless = Highscore(1); theTopScoresTimeTrial = Highscore(2); drawBalls = true; @@ -2087,7 +2027,7 @@ int runGame(int gametype, int level) { bool mustWriteScreenshot = false; - if (!bScreenLocked) { + if (true) { SDL_Event event; while ( SDL_PollEvent(&event) ) { diff --git a/source/code/mainVars.inc b/source/code/mainVars.inc index 233a164..d8e6a75 100644 --- a/source/code/mainVars.inc +++ b/source/code/mainVars.inc @@ -71,7 +71,7 @@ static sago::SagoSprite counter[3]; //Counts down from 3 static sago::SagoSprite bricks[7]; //The bricks, saved in an array of pointers static sago::SagoSprite crossover; //Cross the bricks that will be cleared soon static sago::SagoSprite balls[7]; //The balls (the small ones that jump around) -static sago::SagoSprite dialogBox; +sago::SagoSprite dialogBox; static sago::SagoSprite iLevelCheck; //To the level select screen static sago::SagoSprite iLevelCheckBox; static sago::SagoSprite iLevelCheckBoxMarked; @@ -112,7 +112,7 @@ static Mix_Music *highbeatMusic; //Background music with higher beat static Mix_Chunk *boing; //boing sound when clearing static Mix_Chunk *applause; //Applause, then the player is good static Mix_Chunk *photoClick; //clickSound -static Mix_Chunk *typingChunk; //When writing +Mix_Chunk *typingChunk; //When writing static Mix_Chunk *counterChunk; //When counting down static Mix_Chunk *counterFinalChunk; @@ -124,9 +124,7 @@ static bool bMouseUp2; //true if the mouse(2) is unpressed #if NETWORK static bool bNetworkOpen; //Show the network menu #endif -static bool bScreenLocked; //Don't take input or allow any mouse interaction! Used for dialogbox and warningbox -static bool showDialog; -static bool NoSound; //if true, absolutely no sound will be played, can be set from the commandline +bool NoSound; //if true, absolutely no sound will be played, can be set from the commandline //prevents crash on systems without a soundcard bool MusicEnabled; //true if background music is enabled bool SoundEnabled; //true if sound effects is enabled diff --git a/source/code/menudef.cpp b/source/code/menudef.cpp index 111d241..02d7006 100644 --- a/source/code/menudef.cpp +++ b/source/code/menudef.cpp @@ -145,13 +145,13 @@ static void buttonActionFullscreen(Button* b) { } static void buttonActionPlayer1Name(Button* b) { - if (OpenDialogbox(200,100,player1name)) { + if (OpenDialogbox(200, 100, player1name)) { return; //must save if true } } static void buttonActionPlayer2Name(Button* b) { - if (OpenDialogbox(200,100,player2name)) { + if (OpenDialogbox(200, 100, player2name)) { return; //must save if true } } diff --git a/source/code/stageclearhandler.cpp b/source/code/stageclearhandler.cpp index 397d40a..bf62506 100644 --- a/source/code/stageclearhandler.cpp +++ b/source/code/stageclearhandler.cpp @@ -47,7 +47,7 @@ void StageClearSetClear(int Level, int score, int time) { } PhysFS::ofstream outfile; - outfile.open(stageClearSaveName, ios::binary |ios::trunc); + outfile.open(stageClearSaveName, ios::binary); if (!outfile) { cerr << "Error writing to file: " << stageClearSaveName << endl; } @@ -64,6 +64,7 @@ void StageClearSetClear(int Level, int score, int time) { Uint32 tempUInt32 = stageTimes[i]; outfile.write(reinterpret_cast(&tempUInt32), sizeof(Uint32)); } + outfile.flush(); outfile.close(); } }