git repos / blockattack-game

commit b283e0f7

sago007 · 2012-07-29 14:01
b283e0f7c9e7c7ccb21658b977dd8495a4a67580 patch · browse files
parent 18055aa033fbdd35fc5fc7614bb28911109ea6c2

Started to create  support for two games in a replay file. Not working yet


git-svn-id: https://blockattack.svn.sourceforge.net/svnroot/blockattack/trunk@156 9d7177f8-192b-0410-8f35-a16a89829b06

Changed files

M source/code/MenuSystem.cc before
M source/code/MenuSystem.h before
M source/code/main.cpp before
M source/code/menudef.cpp before
M source/code/replay.cpp before
diff --git a/source/code/MenuSystem.cc b/source/code/MenuSystem.cc index 8be23c0..5f8edfe 100644 --- a/source/code/MenuSystem.cc +++ b/source/code/MenuSystem.cc
@@ -42,11 +42,13 @@ inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y)
SDL_BlitSurface(img, NULL, target, &dest);
}
-CppSdl::CppSdlImageHolder ButtonGfx::_marked;
+/*CppSdl::CppSdlImageHolder ButtonGfx::_marked;
CppSdl::CppSdlImageHolder ButtonGfx::_unmarked;
int ButtonGfx::xsize;
int ButtonGfx::ysize;
-NFont ButtonGfx::thefont;
+NFont ButtonGfx::thefont;*/
+
+ButtonGfx standardButton;
void ButtonGfx::setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked)
{
@@ -59,6 +61,7 @@ void ButtonGfx::setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImage
Button::Button()
{
+ gfx = &standardButton;
label = "";
marked = false;
action = NULL;
@@ -71,6 +74,7 @@ Button::~Button()
Button::Button(const Button& b)
{
+ gfx = b.gfx;
label = b.label;
marked = b.marked;
action = b.action;
@@ -89,7 +93,7 @@ void Button::setAction(void (*action2run)(Button*))
bool Button::isClicked(int x,int y)
{
- if ( x >= this->x && y >= this->y && x<= this->x+ButtonGfx::xsize && y <= this->y + ButtonGfx::ysize)
+ if ( x >= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize)
return true;
else
return false;
@@ -109,14 +113,14 @@ void Button::drawTo(SDL_Surface **surface)
//cout << "Painting button: " << label << " at: " << x << "," << y << endl;
#endif
if (marked)
- ButtonGfx::_marked.PaintTo(*surface,x,y);
+ gfx->_marked.PaintTo(*surface,x,y);
else
- ButtonGfx::_unmarked.PaintTo(*surface,x,y);
+ 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);
- ButtonGfx::thefont.setDest(*surface);
- ButtonGfx::thefont.drawCenter(x+ButtonGfx::xsize/2,y+ButtonGfx::ysize/2-ButtonGfx::thefont.getHeight(label.c_str())/2,label.c_str());
+ 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)
@@ -129,6 +133,15 @@ 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);
@@ -136,7 +149,7 @@ void Menu::drawSelf()
for(it = buttons.begin(); it < buttons.end(); it++)
(*it)->drawTo(&screen);
exit.drawTo(&screen);
- ButtonGfx::thefont.draw(50,50,title.c_str());
+ standardButton.thefont.draw(50,50,title.c_str());
mouse.PaintTo(screen,mousex,mousey);
}
@@ -164,7 +177,7 @@ void Menu::placeButtons()
{
(*it)->x = X;
(*it)->y = nextY;
- nextY += ButtonGfx::ysize+10;
+ nextY += (*it)->getHeight()+10;
}
exit.x = X;
exit.y = nextY;
diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h index 180bcb3..63651e1 100644 --- a/source/code/MenuSystem.h +++ b/source/code/MenuSystem.h
@@ -38,17 +38,19 @@ class ButtonGfx
{
public:
//Holds the graphic for a button that is selected
- static CppSdl::CppSdlImageHolder _marked;
+ CppSdl::CppSdlImageHolder _marked;
//Holds the graphic for a button that is not selected
- static CppSdl::CppSdlImageHolder _unmarked;
+ CppSdl::CppSdlImageHolder _unmarked;
//The size of the buttons, so we don't have to ask w and h from the SDL Surfaces each time
- static int xsize;
- static int ysize;
+ int xsize;
+ int ysize;
//A TTFont used for writing the label on the buttons
- static NFont thefont;
- static void setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked);
+ NFont thefont;
+ void setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked);
};
+extern ButtonGfx standardButton;
+
//A button
class Button
{
@@ -60,6 +62,8 @@ private:
//If true the menu should also be closed then the button is clicked
bool popOnRun;
+
+ ButtonGfx *gfx;
public:
//Is the button marked?
@@ -82,7 +86,9 @@ public:
virtual void doAction(); //Run the callback function
void drawTo(SDL_Surface **surface);
void setPopOnRun(bool popOnRun);
- bool isPopOnRun() const; //Draws to screen
+ bool isPopOnRun() const;
+ void setGfx(ButtonGfx* gfx); //Draws to screen
+ int getHeight();
//May hold any other information the callback might need
int iGeneric1;
diff --git a/source/code/main.cpp b/source/code/main.cpp index c820a26..16f1fbc 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -3128,9 +3128,11 @@ static void StartTwoPlayerVs()
static void StartReplay(string filename)
{
- Replay r1;
+ Replay r1,r2;
r1.loadReplay(filename);
player1->playReplay(50,100,SDL_GetTicks(),r1);
+ r2.loadReplay2(filename);
+ player2->playReplay(xsize-500,100,SDL_GetTicks(),r2);
}
static void StartHostServer()
@@ -3941,10 +3943,23 @@ int runGame(int gametype, int level)
*/
mustsetupgame = true;
}
+ if ( event.key.keysym.sym == SDLK_F10 ) {
+ StartReplay("/home/poul/.gamesaves/blockattack/quicksave");
+ }
if ( event.key.keysym.sym == SDLK_F9 )
{
writeScreenShot();
}
+ if ( event.key.keysym.sym == SDLK_F5 )
+ {
+ if(theGame.isGameOver() && theGame2.isGameOver()) {
+ string filename = "/home/poul/.gamesaves/blockattack/quicksave";
+ if(!twoPlayers)
+ theGame.theReplay.saveReplay(filename);
+ else
+ theGame.theReplay.saveReplay(filename,theGame2.theReplay);
+ }
+ }
if ( event.key.keysym.sym == SDLK_F11 )
{
/*This is the test place, place function to test here*/
@@ -4408,17 +4423,17 @@ int runGame(int gametype, int level)
theGame.setDraw();
theGame2.setDraw();
}
- twoPlayers = false;
+ //twoPlayers = false;
}
if ((theGame.isGameOver()) && (!theGame2.isGameOver()))
{
theGame2.setPlayerWon();
- twoPlayers = false;
+ //twoPlayers = false;
}
if ((!theGame.isGameOver()) && (theGame2.isGameOver()))
{
theGame.setPlayerWon();
- twoPlayers = false;
+ //twoPlayers = false;
}
}
diff --git a/source/code/menudef.cpp b/source/code/menudef.cpp index 29392ad..7143e5f 100644 --- a/source/code/menudef.cpp +++ b/source/code/menudef.cpp
@@ -99,8 +99,8 @@ void Button_changekey::doAction()
void InitMenues()
{
- ButtonGfx::setSurfaces(menuMarked,menuUnmarked);
- ButtonGfx::thefont = nf_scoreboard_font;
+ standardButton.setSurfaces(menuMarked,menuUnmarked);
+ standardButton.thefont = nf_scoreboard_font;
}
void runGame(int gametype,int level);
diff --git a/source/code/replay.cpp b/source/code/replay.cpp index f20f2fa..5556e52 100644 --- a/source/code/replay.cpp +++ b/source/code/replay.cpp
@@ -72,16 +72,22 @@ bool Replay::saveReplay(string filename)
bool Replay::saveReplay(string filename,Replay p2)
{
- //Saving as fileversion 3
- cout << "Saving as version 3 save file (2 players)" << endl;
+ //Saving as fileversion 4
+ cout << "Saving as version 4 save file (2 players)" << endl;
ofstream saveFile;
saveFile.open(filename.c_str(),ios::binary|ios::trunc);
if (saveFile)
{
- Uint32 version = 4;
- saveFile.write(reinterpret_cast<char*>(&version),sizeof(Uint32)); //Fileversion
- Uint8 nrOfReplays = 2;
-
+ saveFile << "0 16 PLAYER 1\n";
+ saveFile << "0 16 NAME " << name << "\n";
+ for(int i = 0;i < actions.size(); ++i) {
+ saveFile << actions.at(i).time << " " << actions.at(i).action << " " << actions.at(i).param << "\n";
+ }
+ saveFile << "0 16 PLAYER 2\n";
+ saveFile << "0 16 NAME " << name << "\n";
+ for(int i = 0;i < p2.getActions().size(); ++i) {
+ saveFile << p2.getActions().at(i).time << " " << p2.getActions().at(i).action << " " << p2.getActions().at(i).param << "\n";
+ }
saveFile.close();
return true;
}
@@ -143,31 +149,46 @@ bool Replay::loadReplay(string filename)
bool Replay::loadReplay2(string filename)
{
isLoaded = true;
+ bool player2started = false; //set to true once the player two part is reached
+ actions.clear();
ifstream loadFile;
- loadFile.open(filename.c_str(),ios::binary);
+ loadFile.open(filename.c_str());
if (loadFile)
{
- Uint32 version;
- loadFile.read(reinterpret_cast<char*>(&version),sizeof(Uint32));
- switch (version)
- {
- case 3:
- Uint8 nrOfPlayers;
- loadFile.read(reinterpret_cast<char*>(&nrOfPlayers),sizeof(Uint8));
- if (nrOfPlayers<2)
+ while(!loadFile.eof()) {
+ Sint32 time;
+ Uint32 action;
+ string restOfLine;
+ loadFile >> time >> action;
+ if(action == 16)
{
- loadFile.close();
- cout << "Only one player in replay" << endl;
- return false;
+ string command;
+ loadFile >> command;
+ if(command == "NAME")
+ getline(loadFile,name,'\n');
+ if(command == "PLAYER") {
+ getline(loadFile,restOfLine,'\n');
+ if(restOfLine == " 2")
+ player2started = true;
+ }
+ } else
+ {
+ getline(loadFile,restOfLine,'\n');
+ if(!player2started)
+ continue;
+ Action a;
+ a.time = time;
+ a.action = action;
+ if(restOfLine.length()>1)
+ a.param = restOfLine.substr(1);
+ else
+ a.param = "";
+ actions.push_back(a);
}
- cout << "loading player 2" << endl;
- break;
- default:
- cout << "Unknown version: " << version << endl;
- return false;
- };
+ }
loadFile.close();
+ cout << "Loaded 2 player, actions.size="<< actions.size() << endl;
}
else
{