summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog1
-rw-r--r--src/game.cpp133
-rw-r--r--src/game.h58
-rw-r--r--src/main.cpp7
4 files changed, 98 insertions, 101 deletions
diff --git a/ChangeLog b/ChangeLog
index 6aac9dc4..40ca8d89 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
2006-02-01 Björn Steinbrink <B.Steinbrink@gmx.de>
+ * src/game.cpp, src/game.h, src/main.cpp: Created a Game class.
* src/gui/passwordfield.cpp: Removed duplicated code.
* src/Makefile.am, src/main.cpp, src/gui/error.cpp, src/gui/error.h:
Replaced ErrorDialog by OkDialog + listener.
diff --git a/src/game.cpp b/src/game.cpp
index 99a83ba0..75545de4 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -74,6 +74,25 @@
#include "resources/imagewriter.h"
+enum {
+ JOY_UP,
+ JOY_DOWN,
+ JOY_LEFT,
+ JOY_RIGHT,
+ JOY_BTN0,
+ JOY_BTN1,
+ JOY_BTN2,
+ JOY_BTN3,
+ JOY_BTN4,
+ JOY_BTN5,
+ JOY_BTN6,
+ JOY_BTN7,
+ JOY_BTN8,
+ JOY_BTN9,
+ JOY_BTN10,
+ JOY_BTN11
+};
+
extern Graphics *graphics;
extern gcn::SDLInput *guiInput;
@@ -84,7 +103,7 @@ std::string map_path;
bool done = false;
volatile int tick_time;
volatile bool action_time = false;
-int fps = 0, frame = 0;
+volatile int fps = 0, frame = 0;
Engine *engine = NULL;
SDL_Joystick *joypad = NULL; /**< Joypad object */
@@ -115,17 +134,6 @@ DebugWindow *debugWindow;
BeingManager *beingManager = NULL;
-BeingHandler *beingHandler;
-BuySellHandler *buySellHandler;
-ChatHandler *chatHandler;
-EquipmentHandler *equipmentHandler;
-InventoryHandler *inventoryHandler;
-ItemHandler *itemHandler;
-NPCHandler *npcHandler;
-PlayerHandler *playerHandler;
-SkillHandler *skillHandler;
-TradeHandler *tradeHandler;
-
const int MAX_TIME = 10000;
/**
@@ -262,7 +270,8 @@ void destroyGuiWindows()
delete debugWindow;
}
-void do_init(Network *network)
+Game::Game(Network *network):
+ mNetwork(network)
{
createGuiWindows(network);
engine = new Engine(network);
@@ -303,27 +312,50 @@ void do_init(Network *network)
}
}
- beingHandler = new BeingHandler();
- buySellHandler = new BuySellHandler();
- chatHandler = new ChatHandler();
- equipmentHandler = new EquipmentHandler();
- inventoryHandler = new InventoryHandler();
- itemHandler = new ItemHandler();
- npcHandler = new NPCHandler();
- playerHandler = new PlayerHandler();
- skillHandler = new SkillHandler();
- tradeHandler = new TradeHandler();
-
- network->registerHandler(beingHandler);
- network->registerHandler(buySellHandler);
- network->registerHandler(chatHandler);
- network->registerHandler(equipmentHandler);
- network->registerHandler(inventoryHandler);
- network->registerHandler(itemHandler);
- network->registerHandler(npcHandler);
- network->registerHandler(playerHandler);
- network->registerHandler(skillHandler);
- network->registerHandler(tradeHandler);
+ mBeingHandler = new BeingHandler();
+ mBuySellHandler = new BuySellHandler();
+ mChatHandler = new ChatHandler();
+ mEquipmentHandler = new EquipmentHandler();
+ mInventoryHandler = new InventoryHandler();
+ mItemHandler = new ItemHandler();
+ mNpcHandler = new NPCHandler();
+ mPlayerHandler = new PlayerHandler();
+ mSkillHandler = new SkillHandler();
+ mTradeHandler = new TradeHandler();
+
+ network->registerHandler(mBeingHandler);
+ network->registerHandler(mBuySellHandler);
+ network->registerHandler(mChatHandler);
+ network->registerHandler(mEquipmentHandler);
+ network->registerHandler(mInventoryHandler);
+ network->registerHandler(mItemHandler);
+ network->registerHandler(mNpcHandler);
+ network->registerHandler(mPlayerHandler);
+ network->registerHandler(mSkillHandler);
+ network->registerHandler(mTradeHandler);
+}
+
+Game::~Game()
+{
+ delete mBeingHandler;
+ delete mBuySellHandler;
+ delete mChatHandler;
+ delete mEquipmentHandler;
+ delete mInventoryHandler;
+ delete mItemHandler;
+ delete mNpcHandler;
+ delete mPlayerHandler;
+ delete mSkillHandler;
+ delete mTradeHandler;
+
+ delete engine;
+ delete player_node;
+ destroyGuiWindows();
+
+ if (joypad != NULL)
+ {
+ SDL_JoystickClose(joypad);
+ }
}
bool saveScreenshot(SDL_Surface *screenshot)
@@ -350,7 +382,7 @@ bool saveScreenshot(SDL_Surface *screenshot)
return ImageWriter::writePNG(screenshot, filename.str());
}
-void game(Network *network)
+void Game::logic()
{
int gameTime = tick_time;
@@ -359,7 +391,7 @@ void game(Network *network)
// Handle all necessary game logic
while (get_elapsed_time(gameTime) > 0)
{
- do_input(network);
+ handleInput();
engine->logic();
gameTime++;
}
@@ -379,35 +411,12 @@ void game(Network *network)
}
// Handle network stuff
- network->flush();
- network->dispatchMessages();
- }
-}
-
-void do_exit()
-{
- delete beingHandler;
- delete buySellHandler;
- delete chatHandler;
- delete equipmentHandler;
- delete inventoryHandler;
- delete itemHandler;
- delete npcHandler;
- delete playerHandler;
- delete skillHandler;
- delete tradeHandler;
-
- delete engine;
- delete player_node;
- destroyGuiWindows();
-
- if (joypad != NULL)
- {
- SDL_JoystickClose(joypad);
+ mNetwork->flush();
+ mNetwork->dispatchMessages();
}
}
-void do_input(Network *network)
+void Game::handleInput()
{
// Get the state of the keyboard keys
Uint8* keys;
diff --git a/src/game.h b/src/game.h
index a1a3cc22..ca7f0ed8 100644
--- a/src/game.h
+++ b/src/game.h
@@ -29,51 +29,37 @@
#define SPEECH_TIME 80
#define SPEECH_MAX_TIME 100
+class MessageHandler;
class Network;
extern std::string map_path;
-extern int fps;
+extern volatile int fps;
extern volatile int tick_time;
-extern int server_tick;
-enum {
- JOY_UP,
- JOY_DOWN,
- JOY_LEFT,
- JOY_RIGHT,
- JOY_BTN0,
- JOY_BTN1,
- JOY_BTN2,
- JOY_BTN3,
- JOY_BTN4,
- JOY_BTN5,
- JOY_BTN6,
- JOY_BTN7,
- JOY_BTN8,
- JOY_BTN9,
- JOY_BTN10,
- JOY_BTN11
-};
+class Game
+{
+ public:
+ Game(Network *network);
+ ~Game();
-/**
- * Main game loop
- */
-void game(Network*);
+ void logic();
-/**
- * Check user input
- */
-void do_input(Network*);
+ void handleInput();
-/**
- * Initialize
- */
-void do_init(Network*);
+ protected:
+ Network *mNetwork;
-/**
- * Clean the engine
- */
-void do_exit();
+ MessageHandler *mBeingHandler;
+ MessageHandler *mBuySellHandler;
+ MessageHandler *mChatHandler;
+ MessageHandler *mEquipmentHandler;
+ MessageHandler *mInventoryHandler;
+ MessageHandler *mItemHandler;
+ MessageHandler *mNpcHandler;
+ MessageHandler *mPlayerHandler;
+ MessageHandler *mSkillHandler;
+ MessageHandler *mTradeHandler;
+};
/**
* Returns elapsed time. (Warning: very unsafe function, it supposes the delay
diff --git a/src/main.cpp b/src/main.cpp
index 28502b5e..0ea554bf 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -501,6 +501,7 @@ int main(int argc, char *argv[])
Window *currentDialog = NULL;
Image *login_wallpaper = NULL;
+ Game *game = NULL;
sound.playMusic(TMW_DATADIR "data/music/Magick - Real.ogg");
@@ -620,9 +621,9 @@ int main(int argc, char *argv[])
login_wallpaper = NULL;
logger->log("State: GAME");
- do_init(network);
- game(network);
- do_exit();
+ game = new Game(network);
+ game->logic();
+ delete game;
state = EXIT_STATE;
break;