diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 70 | ||||
-rw-r--r-- | src/utils/timer.cpp | 71 | ||||
-rw-r--r-- | src/utils/timer.h | 86 |
3 files changed, 179 insertions, 48 deletions
diff --git a/src/main.cpp b/src/main.cpp index 679408fb..951d9b43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,6 +50,7 @@ #include "utils/logger.h" #include "utils/stringfilter.h" +#include "utils/timer.h" // Scripting #ifdef SCRIPT_SUPPORT @@ -79,15 +80,13 @@ std::string scriptLanugage = "none"; #define DEFAULT_SERVER_PORT 9601 #endif -#define TMW_WORLD_TICK SDL_USEREVENT +tmwserv::utils::Timer worldTimer(100, false); /**< Timer for world tics set to 100 ms */ +int worldTime = 0; /**< Current world time in 100ms ticks */ +bool running = true; /**< Determines if server keeps running */ -SDL_TimerID worldTimerID; /**< Timer ID of world timer */ -int worldTime = 0; /**< Current world time in 100ms ticks */ -bool running = true; /**< Determines if server keeps running */ +Skill skillTree("base"); /**< Skill tree */ -Skill skillTree("base"); /**< Skill tree */ - -Configuration config; /**< XML config reader */ +Configuration config; /**< XML config reader */ tmwserv::utils::StringFilter *stringFilter; /**< Slang's Filter */ @@ -106,23 +105,6 @@ GameHandler *gameHandler; ConnectionHandler *connectionHandler; /** - * SDL timer callback, sends a <code>TMW_WORLD_TICK</code> event. - */ -Uint32 worldTick(Uint32 interval, void *param) -{ - // Push the custom world tick event - SDL_Event event; - event.type = TMW_WORLD_TICK; - - if (SDL_PushEvent(&event)) { - LOG_WARN("couldn't push world tick into event queue!", 0) - } - - return interval; -} - - -/** * Initializes the server. */ void initialize() @@ -187,7 +169,7 @@ void initialize() connectionHandler = new ConnectionHandler(); // Make SDL use a dummy videodriver so that it doesn't require an X server - putenv("SDL_VIDEODRIVER=dummy"); + //putenv("SDL_VIDEODRIVER=dummy"); // Initialize SDL if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { @@ -210,9 +192,6 @@ void initialize() exit(2); } - // initialize world timer at 10 times per second. - worldTimerID = SDL_AddTimer(100, worldTick, NULL); - // initialize scripting subsystem. #ifdef RUBY_SUPPORT LOG_INFO("Script Language: " << scriptLanguage, 0) @@ -262,7 +241,7 @@ void deinitialize() config.write(); // Stop world timer - SDL_RemoveTimer(worldTimerID); + worldTimer.stop(); // Quit SDL SDL_Quit(); @@ -414,28 +393,22 @@ int main(int argc, char *argv[]) // create state machine State &state = State::instance(); - // - SDL_Event event; + // initialize world timer + worldTimer.start(); while (running) { - while (SDL_PollEvent(&event)) { - if (event.type == TMW_WORLD_TICK) { - // Move the world forward in time - worldTime++; - - // Print world time at 10 second intervals to show we're alive - if (worldTime % 100 == 0) { - LOG_INFO("World time: " << worldTime, 0); - } + if (worldTimer.poll()) { + worldTime++; - // - Handle all messages that are in the message queue - // - Update all active objects/beings - state.update(*connectionHandler); - } - else if (event.type == SDL_QUIT) { - running = false; + // Print world time at 10 second intervals to show we're alive + if (worldTime % 100 == 0) { + LOG_INFO("World time: " << worldTime, 0); } + + // - Handle all messages that are in the message queue + // - Update all active objects/beings + state.update(*connectionHandler); } /*ENetEvent netEvent; @@ -486,8 +459,9 @@ int main(int argc, char *argv[]) }*/ // We know only about 10 events will happen per second, - // so give the CPU a break for a while. - SDL_Delay(100); + // so give the CPU a break for a while with pthreads sched_yield + // function + sched_yield(); } LOG_INFO("Received: Quit signal, closing down...", 0) diff --git a/src/utils/timer.cpp b/src/utils/timer.cpp new file mode 100644 index 00000000..eecbb7f1 --- /dev/null +++ b/src/utils/timer.cpp @@ -0,0 +1,71 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "timer.h" + +namespace tmwserv +{ +namespace utils +{ + +Timer::Timer(signed int ms, bool createActive) +{ + active = createActive; + interval = (ms * CLOCKS_PER_SEC) / 1000; + nextpulse = clock() + interval; +}; + +bool Timer::poll() +{ + if (!active) return false; + + if (nextpulse < clock()) + { + nextpulse += interval; + return true; + } + else { + return false; + }; + +}; + +void Timer::start() +{ + active = true; + nextpulse = clock() + interval; +}; + +void Timer::stop() +{ + active = false; +}; + +void Timer::changeInterval(signed int ms) +{ + signed int newinterval = ms * CLOCKS_PER_SEC / 1000; + nextpulse = nextpulse - interval + newinterval; + interval = newinterval; +}; + + +} // ::utils +} // ::tmwserv diff --git a/src/utils/timer.h b/src/utils/timer.h new file mode 100644 index 00000000..14f0638a --- /dev/null +++ b/src/utils/timer.h @@ -0,0 +1,86 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _TMWSERV_TIMER_H_ +#define _TMWSERV_TIMER_H_ + +#include <time.h> + +namespace tmwserv +{ +namespace utils +{ + +/** + * This class is for timing purpose as a replacement for SDL_TIMER + * connections from and connecting to other computers. + */ + +class Timer +{ + public: + /** + * Constructor. + */ + Timer(signed int ms, bool createActive = true); + + /** + * checks if the desired time has passed + * returns true if yes and false if not + */ + bool poll(); + + /** + * activates the timer + */ + void start(); + + /** + * deactivates the timer + */ + void stop(); + + /** + * changes the interval between two pulses + */ + void changeInterval (signed int ms); + + private: + /** + * interval between two pulses + */ + signed int interval; + + /** + * time for next pulse + */ + signed int nextpulse; + + /** + * activity status of the timer + */ + bool active; +}; + +} // ::utils +} // ::tmwserv + +#endif |