git repos / blockattack-game

commit 26ddb424

sago007 · 2011-06-09 20:06
26ddb424cd3770714a1061bb02f305205695cb91 patch · browse files
parent 72da9ba11a508b7f811fa7eed566bd0db5510589

Startet to change the menu system


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

Changed files

M source/code/MenuSystem.cc before
M source/code/MenuSystem.h before
M source/code/block.make before
M source/code/main.cpp before
M source/code/mainVars.hpp before
A source/code/menudef.cpp
diff --git a/source/code/MenuSystem.cc b/source/code/MenuSystem.cc index 895085e..cc6b398 100644 --- a/source/code/MenuSystem.cc +++ b/source/code/MenuSystem.cc
@@ -83,7 +83,7 @@ void Button::setLabel(string text)
label = text;
}
-void Button::setAction(void (*action2run)())
+void Button::setAction(void (*action2run)(Button*))
{
action = action2run;
}
@@ -99,7 +99,7 @@ bool Button::isClicked(int x,int y)
void Button::doAction()
{
if(action)
- action();
+ action(this);
else
cout << "Warning: button \"" << label << "\" has no action assigned!";
}
diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h index 6459cdb..f97ff57 100644 --- a/source/code/MenuSystem.h +++ b/source/code/MenuSystem.h
@@ -63,7 +63,7 @@ class Button {
//The label. This is written on the button
string label;
//Pointer to a callback function.
- void (*action)();
+ void (*action)(Button *b);
public:
//Is the button marked?
@@ -80,13 +80,14 @@ class Button {
//Set the text to write on the button
void setLabel(string text);
//Set the action to run
- void setAction(void (*action2run)(void));
+ void setAction(void (*action2run)(Button*));
bool isClicked(int x,int y); //Returns true if (x,y) is within the borders of the button
- void doAction(); //Run the callback function
+ virtual void doAction(); //Run the callback function
void drawTo(SDL_Surface **surface); //Draws to screen
};
+
class Menu {
private:
vector<Button> buttons; //Vector holder the buttons
diff --git a/source/code/block.make b/source/code/block.make index 2d518a5..6104040 100644 --- a/source/code/block.make +++ b/source/code/block.make
@@ -43,9 +43,9 @@ 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)/nfont.o $(BUILDDIR)/MenuSystem.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)/nfont.o $(BUILDDIR)/MenuSystem.o $(BUILDDIR)/menudef.o
@make -C CppSdl
- $(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 $(BASE_LIBS)
+ $(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
@@ -79,6 +79,9 @@ $(BUILDDIR)/common.o: common.h common.cc
$(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
diff --git a/source/code/main.cpp b/source/code/main.cpp index ffe9a7c..43659fd 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -25,7 +25,7 @@ Copyright (C) 2008 Poul Sander
*/
#include "common.h"
-
+#include "global.hpp"
#include <string.h>
@@ -856,55 +856,17 @@ void NFont_Write(SDL_Surface *target,int x,int y,string text) {
nf_standard_blue_font.setDest(screen);
}
-//Menu
-void PrintHi()
-{
- cout << "Hi" <<endl;
-}
-
-void InitMenues()
-{
- ButtonGfx::setSurfaces(menuMarked,menuUnmarked);
- ButtonGfx::thefont = nf_scoreboard_font;
-}
-
-void runGame(int gametype);
-
-void runSinglePlayerEndless() {
- runGame(0);
-}
-
-void runSinglePlayerTimeTrial() {
- runGame(1);
-}
-
-void runSinglePlayerPuzzle() {
- runGame(3);
-}
-
-void runSinglePlayerVs() {
- runGame(4);
+void ResetFullscreen(){
+#if defined(WIN32)
+ if (bFullscreen) screen=SDL_SetVideoMode(xsize,ysize,32,SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ANYFORMAT);
+ else screen=SDL_SetVideoMode(xsize,ysize,32,SDL_SWSURFACE|SDL_ANYFORMAT);
+ DrawIMG(background, screen, 0, 0);
+#else
+ SDL_WM_ToggleFullScreen(screen); //Will only work in Linux
+#endif
+ SDL_ShowCursor(SDL_DISABLE);
}
-void MainMenu()
-{
- InitMenues();
- Menu m(&screen,true);
- Button bHi,bTimetrial1, bPuzzle, bVs1;
- bHi.setLabel("Single player - endless");
- bHi.setAction(runSinglePlayerEndless);
- bTimetrial1.setLabel("Single player - time trial");
- bTimetrial1.setAction(runSinglePlayerTimeTrial);
- bPuzzle.setLabel("Single player - puzzle mode");
- bPuzzle.setAction(runSinglePlayerPuzzle);
- bVs1.setLabel("Single player - vs");
- bVs1.setAction(runSinglePlayerVs);
- m.addButton(bHi);
- m.addButton(bTimetrial1);
- m.addButton(bPuzzle);
- m.addButton(bVs1);
- m.run();
-}
//The small things that are faaling when you clear something
class aBall
@@ -4728,14 +4690,7 @@ int main(int argc, char *argv[])
{
//Fullscreen
bFullscreen = !bFullscreen;
-#if defined(WIN32)
- if (bFullscreen) screen=SDL_SetVideoMode(xsize,ysize,32,SDL_SWSURFACE|SDL_FULLSCREEN|SDL_ANYFORMAT);
- else screen=SDL_SetVideoMode(xsize,ysize,32,SDL_SWSURFACE|SDL_ANYFORMAT);
- DrawIMG(background, screen, 0, 0);
-#else
- SDL_WM_ToggleFullScreen(screen); //Will only work in Linux
-#endif
- SDL_ShowCursor(SDL_DISABLE);
+ ResetFullscreen();
}
if ((showOptions) && (mousex>330) && (mousex<470) && (mousey>535) && (mousey<585))
diff --git a/source/code/mainVars.hpp b/source/code/mainVars.hpp index b24d9d1..d5da3c8 100644 --- a/source/code/mainVars.hpp +++ b/source/code/mainVars.hpp
@@ -77,7 +77,7 @@ static SDL_Surface *bHighScore; //The High Score botton
static SDL_Surface *bExit; //The Exit botton
static SDL_Surface *blackLine; //The seperator in stage clear
static SDL_Surface *stageBobble; //The bobble instage clear
-static SDL_Surface *screen; //The whole screen;
+SDL_Surface *screen; //The whole screen;
static SDL_Surface *iGameOver; //The gameOver image
static SDL_Surface *iWinner; //the "winner" image
static SDL_Surface *iDraw; //the "draw" image
@@ -158,7 +158,7 @@ static SDL_Surface *tmp; //a temporary surface to use DisplayFormat
//static SFont_Font *fSmallFont; //Stores the small font (SFont)
//TTFont ttfont; //Stores the TTF font (TTFSDL)
static NFont nf_button_font; //Font used for buttons!
-static NFont nf_scoreboard_font;
+NFont nf_scoreboard_font;
static NFont nf_standard_blue_font; //Font used instead of the old blue SFont
static NFont nf_standard_small_font;
@@ -190,11 +190,11 @@ static bool bScreenLocked; //Don't take input or allow any mouse interaction!
static bool showDialog;
static bool NoSound; //if true, absolutely no sound will be played, can be set from the commandline
//prevents crash on systems without a soundcard
-static bool MusicEnabled; //true if background music is enabled
-static bool SoundEnabled; //true if sound effects is enabled
+bool MusicEnabled; //true if background music is enabled
+bool SoundEnabled; //true if sound effects is enabled
static bool bNearDeath; //Play music faster or louder while tru
static bool bNearDeathPrev; //Near death status last time checked.
-static bool bFullscreen; //true if game is running fullscreen
+bool bFullscreen; //true if game is running fullscreen
static bool puzzleLoaded; //true if the puzzle levels have been loaded
static bool drawBalls; //if true balls are drawed to the screen, this might lower framerate too much
static bool standardBackground;
@@ -246,8 +246,8 @@ static int ysize = 768;
static int bsize = 50;
//Stores the players names (way to long, but at least no buffer overflows (max length is 16 for display reasons))
-static char player1name[30];
-static char player2name[30];
+char player1name[30];
+char player2name[30];
//paths
static string stageClearSavePath;
diff --git a/source/code/menudef.cpp b/source/code/menudef.cpp new file mode 100644 index 0000000..c3e1bca --- /dev/null +++ b/source/code/menudef.cpp
@@ -0,0 +1,142 @@
+#include "global.hpp"
+#include <iostream>
+#include <stdlib.h>
+#include "menu/MenuItem.hpp"
+#include "MenuSystem.h"
+
+using namespace std;
+
+//Menu
+void PrintHi()
+{
+ cout << "Hi" <<endl;
+}
+
+bool OpenDialogbox(int x, int y, char *name);
+void OpenScoresDisplay();
+
+void InitMenues()
+{
+ ButtonGfx::setSurfaces(menuMarked,menuUnmarked);
+ ButtonGfx::thefont = nf_scoreboard_font;
+}
+
+void runGame(int gametype);
+
+void runSinglePlayerEndless(Button* b) {
+ runGame(0);
+}
+
+void runSinglePlayerTimeTrial(Button* b) {
+ runGame(1);
+}
+
+void runSinglePlayerPuzzle(Button* b) {
+ runGame(3);
+}
+
+void runSinglePlayerVs(Button* b) {
+ runGame(4);
+}
+
+void buttonActionMusic(Button* b) {
+ MusicEnabled = !MusicEnabled;
+ b->setLabel(MusicEnabled? "Music: On" : "Music: Off");
+}
+
+void buttonActionSound(Button* b) {
+ SoundEnabled = !SoundEnabled;
+ b->setLabel(SoundEnabled? "Music: On" : "Music: Off");
+}
+
+void buttonActionFullscreen(Button* b) {
+ bFullscreen = !bFullscreen;
+ b->setLabel(bFullscreen? "Fullscreen: On" : "Fullscreen: Off");
+ ResetFullscreen();
+}
+
+void buttonActionPlayer1Name(Button *b) {
+ if (OpenDialogbox(200,100,player1name))
+ return; //must save if true
+}
+
+void buttonActionPlayer2Name(Button *b) {
+ if (OpenDialogbox(200,100,player2name))
+ return; //must save if true
+}
+
+void buttonActionHighscores(Button *b) {
+ OpenScoresDisplay();
+}
+
+class ChangeKeysMenu {
+public:
+ static long player;
+ static void CreateKeysMenu(long playernumber);
+ static void ChangeLeftKey(Button *b);
+ static void ChangeRightKey(Button *b);
+ static void ChangeUpKey(Button *b);
+ static void ChangeDownKey(Button *b);
+ static void ChangePushKey(Button *b);
+ static void ChangeSwitchKey(Button *b);
+ static void ChangeJoypadStatus(Button *b);
+ static void ChangeMouseStatus(Button *b);
+};
+
+long ChangeKeysMenu::player;
+
+static void ChangeKeysMenu(long playernumber) {
+ ChangeKeysMenu::player = playernumber;
+ Button bLeft, bRight, bUp, bDown, bPush, bSwitch, bJoypad, bMouse;
+ bLeft.setLabel("Left : ");
+ bRight.setLabel("Right : ");
+ bUp.setLabel("Up : ");
+ bDown.setLabel("Down : ");
+}
+
+void ConfigureMenu(Button *b) {
+ Menu cm(&screen,false);
+ Button bMusic,bSound,buttonFullscreen,bPlayer1Name,bPlayer2Name;
+ bMusic.setLabel(MusicEnabled? "Music: On" : "Music: Off");
+ bMusic.setAction(buttonActionMusic);
+ bSound.setLabel(SoundEnabled? "Music: On" : "Music: Off");
+ bSound.setAction(buttonActionSound);
+ buttonFullscreen.setLabel(bFullscreen? "Fullscreen: On" : "Fullscreen: Off");
+ buttonFullscreen.setAction(buttonActionFullscreen);
+ bPlayer1Name.setAction(buttonActionPlayer1Name);
+ bPlayer1Name.setLabel("Change player 1's name");
+ bPlayer2Name.setAction(buttonActionPlayer2Name);
+ bPlayer2Name.setLabel("Change player 2's name");
+ cm.addButton(bMusic);
+ cm.addButton(bSound);
+ cm.addButton(buttonFullscreen);
+ cm.addButton(bPlayer1Name);
+ cm.addButton(bPlayer2Name);
+ cm.run();
+}
+
+void MainMenu()
+{
+ InitMenues();
+ Menu m(&screen,true);
+ Button bHi,bTimetrial1, bPuzzle, bVs1, bConfigure,bHighscore;
+ bHi.setLabel("Single player - endless");
+ bHi.setAction(runSinglePlayerEndless);
+ bTimetrial1.setLabel("Single player - time trial");
+ bTimetrial1.setAction(runSinglePlayerTimeTrial);
+ bPuzzle.setLabel("Single player - puzzle mode");
+ bPuzzle.setAction(runSinglePlayerPuzzle);
+ bVs1.setLabel("Single player - vs");
+ bVs1.setAction(runSinglePlayerVs);
+ bConfigure.setLabel("Configure");
+ bConfigure.setAction(ConfigureMenu);
+ bHighscore.setLabel("Highscores");
+ bHighscore.setAction(buttonActionHighscores);
+ m.addButton(bHi);
+ m.addButton(bTimetrial1);
+ m.addButton(bPuzzle);
+ m.addButton(bVs1);
+ m.addButton(bConfigure);
+ m.addButton(bHighscore);
+ m.run();
+}
\ No newline at end of file