[Add] Multiple saves.

This commit is contained in:
Tamir Atias 2012-02-03 17:19:33 +02:00
parent 816ea51c86
commit a04ad1f985
6 changed files with 152 additions and 5 deletions

2
.gitignore vendored
View File

@ -12,7 +12,7 @@ Win32/Unuk/Unuk.ncb
Win32/Unuk/Unuk.suo Win32/Unuk/Unuk.suo
Bin/Unuk Bin/Unuk
Bin/*.dll Bin/*.dll
Save/save Save/save_*
*.swp *.swp
*.o *.o
*.exe *.exe

View File

@ -42,7 +42,8 @@ HEADERS += ../src/Libs/wglext.h \
../src/libUnuk/UI/Bar.h \ ../src/libUnuk/UI/Bar.h \
../src/libUnuk/System/Vec2.h \ ../src/libUnuk/System/Vec2.h \
../src/libUnuk/System/MathBox.h \ ../src/libUnuk/System/MathBox.h \
../src/libUnuk/Engine/Pathfinding.h ../src/libUnuk/Engine/Pathfinding.h \
../src/libUnuk/UI/SavegameMenu.h
SOURCES += ../src/libUnuk/Engine/WorldManager.cpp \ SOURCES += ../src/libUnuk/Engine/WorldManager.cpp \
../src/libUnuk/Engine/ParticleEmitter.cpp \ ../src/libUnuk/Engine/ParticleEmitter.cpp \
../src/libUnuk/Engine/NPC.cpp \ ../src/libUnuk/Engine/NPC.cpp \
@ -75,5 +76,6 @@ SOURCES += ../src/libUnuk/Engine/WorldManager.cpp \
../src/libUnuk/UI/EventHistory.cpp \ ../src/libUnuk/UI/EventHistory.cpp \
../src/libUnuk/UI/Bar.cpp \ ../src/libUnuk/UI/Bar.cpp \
../src/libUnuk/System/Vec2.cpp \ ../src/libUnuk/System/Vec2.cpp \
../src/libUnuk/Engine/Pathfinding.cpp ../src/libUnuk/Engine/Pathfinding.cpp \
../src/libUnuk/UI/SavegameMenu.cpp
OTHER_FILES += OTHER_FILES +=

View File

@ -18,11 +18,13 @@ Game::~Game(void) {
} }
void Game::New(const string& savegameIDArg) { void Game::New(const string& savegameIDArg) {
_saveGameID = savegameIDArg;
NewSavegame(savegameIDArg); NewSavegame(savegameIDArg);
_map.Load("map"); _map.Load("map");
} }
void Game::Load(const string& savegameIDArg) { void Game::Load(const string& savegameIDArg) {
_saveGameID = savegameIDArg;
LoadSavegame(savegameIDArg); LoadSavegame(savegameIDArg);
} }

View File

@ -3,8 +3,10 @@
#include <SDL/SDL_ttf.h> #include <SDL/SDL_ttf.h>
#include <time.h> #include <time.h>
#include <iostream> #include <iostream>
#include <sstream>
#include "../libUnuk/UI/MainMenu.h" #include "../libUnuk/UI/MainMenu.h"
#include "../libUnuk/UI/SavegameMenu.h"
#include "../libUnuk/Engine/NPC.h" #include "../libUnuk/Engine/NPC.h"
#include "../libUnuk/System/Debug.h" #include "../libUnuk/System/Debug.h"
#include "../libUnuk/Engine/MemClass.h" #include "../libUnuk/Engine/MemClass.h"
@ -25,13 +27,24 @@
#endif #endif
static gameNavVal_t RunGame(bool load) { static gameNavVal_t RunGame(bool load) {
SavegameMenu savegameMenu;
savegameMenuNavVal_t savegameMenuRet = savegameMenu.Run();
if(savegameMenuRet == savegameMenuQuit) {
return gameQuitGame;
} else if(savegameMenuRet == savegameMenuCancel) {
return gameMainMenu;
}
std::stringstream saveFilename;
saveFilename << "save_" << savegameMenu.GetSelection();
Debug::logger->message("Entering game state.."); Debug::logger->message("Entering game state..");
Game* game = new Game; Game* game = new Game;
if(load) { if(load) {
game->Load("save"); game->Load(saveFilename.str());
} else { } else {
game->New("save"); game->New(saveFilename.str());
} }
gameNavVal_t ret = game->Run(); gameNavVal_t ret = game->Run();

View File

@ -0,0 +1,101 @@
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>
#include <sstream>
#include "SavegameMenu.h"
#include "../../Unuk/Globals.h"
SavegameMenu::SavegameMenu(void) {
_title.SetXY(BOX_SPACING_X, 25);
_title.SetTextBlended("Choose Savegame: ", vlarge, 0, 255, 255);
for(int i = 0; i < 4; i++) {
std::stringstream capText;
if(i != 3) {
capText << "Savegame " << i;
} else {
capText << "Cancel";
}
_captions[i].SetXY(BOX_SPACING_X + BOX_WIDTH/2,
BOXES_OFFSET_Y + (i * BOX_SPACING_Y) + (i * BOX_HEIGHT) + BOX_HEIGHT/2);
_captions[i].SetTextBlended(capText.str(), large, 0, 255, 255);
_captions[i].SetXY(_captions[i].GetX() - _captions[i].GetWidth()/2,
_captions[i].GetY() - _captions[i].GetHeight()/2);
}
_selection = 0;
}
SavegameMenu::~SavegameMenu(void) {
}
savegameMenuNavVal_t SavegameMenu::Run(void) {
SDL_FillRect(screen, NULL, 0);
while(true) {
Render();
SDL_Flip(screen);
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
return savegameMenuQuit;
case SDL_KEYUP:
if(event.key.keysym.sym == SDLK_UP) {
_selection--;
if(_selection < 0) {
_selection = 3;
}
} else if(event.key.keysym.sym == SDLK_DOWN) {
_selection++;
if(_selection > 3) {
_selection = 0;
}
} else if(event.key.keysym.sym == SDLK_RETURN) {
return savegameMenuSave;
}
break;
}
}
}
return savegameMenuCancel;
}
void SavegameMenu::Render(void) {
_title.RenderLiteral();
for(int i = 0; i < 4; i++) {
int borderRed;
int borderGreen;
int borderBlue;
if(i == _selection) {
borderRed = 255;
borderGreen = 128;
borderBlue = 0;
} else {
borderRed = 0;
borderGreen = 255;
borderBlue = 255;
}
roundedBoxRGBA(screen,
BOX_SPACING_X, BOXES_OFFSET_Y + (i * BOX_SPACING_Y) + (i * BOX_HEIGHT),
BOX_SPACING_X + BOX_WIDTH, BOXES_OFFSET_Y + (i * BOX_SPACING_Y) + (i * BOX_HEIGHT) + BOX_HEIGHT,
5,
0, 0, 128, 255);
roundedRectangleRGBA(screen,
BOX_SPACING_X, BOXES_OFFSET_Y + (i * BOX_SPACING_Y) + (i * BOX_HEIGHT),
BOX_SPACING_X + BOX_WIDTH, BOXES_OFFSET_Y + (i * BOX_SPACING_Y) + (i * BOX_HEIGHT) + BOX_HEIGHT,
5,
borderRed, borderGreen, borderBlue, 255);
_captions[i].RenderLiteral();
}
}

View File

@ -0,0 +1,29 @@
#pragma once
#include "../UI/Text.h"
enum savegameMenuNavVal_t { savegameMenuSave, savegameMenuCancel, savegameMenuQuit };
class SavegameMenu
{
public:
SavegameMenu(void);
~SavegameMenu(void);
savegameMenuNavVal_t Run(void);
int GetSelection(void) { return _selection; }
private:
void Render(void);
Text _title;
Text _captions[4];
int _selection;
static const int BOX_WIDTH = 400;
static const int BOX_HEIGHT = 100;
static const int BOX_SPACING_X = 25;
static const int BOX_SPACING_Y = 25;
static const int BOXES_OFFSET_Y = 100;
};