From 8ee5d47d7e1336eb69064ca31e27bbfa7d86b51e Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 5 Feb 2008 00:41:59 +0000 Subject: renamed ApplicationInterface to Application --- src/core/Makefile.am | 4 +- src/core/application.cc | 198 +++++++++++++++++++++++++++++++++++++++ src/core/application.h | 53 +++++++++++ src/core/applicationinterface.cc | 197 -------------------------------------- src/core/applicationinterface.h | 53 ----------- src/core/core.h | 6 +- 6 files changed, 256 insertions(+), 255 deletions(-) create mode 100644 src/core/application.cc create mode 100644 src/core/application.h delete mode 100644 src/core/applicationinterface.cc delete mode 100644 src/core/applicationinterface.h (limited to 'src/core') diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 8afa759..75a2f6c 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -1,13 +1,13 @@ METASOURCES = AUTO INCLUDES = -I$(top_srcdir)/src -libcore_la_SOURCES = applicationinterface.cc commandbuffer.cc cvar.cc func.cc \ +libcore_la_SOURCES = application.cc commandbuffer.cc cvar.cc func.cc \ gameinterface.cc libcore_la_LDFLAGS = -avoid-version -no-undefined libcore_la_LIBADD = $(top_builddir)/src/math/libmath.la \ $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/filesystem/libfilesystem.la noinst_LTLIBRARIES = libcore.la -noinst_HEADERS = applicationinterface.h commandbuffer.h core.h cvar.h func.h \ +noinst_HEADERS = application.h commandbuffer.h core.h cvar.h func.h \ gameinterface.h diff --git a/src/core/application.cc b/src/core/application.cc new file mode 100644 index 0000000..810bacb --- /dev/null +++ b/src/core/application.cc @@ -0,0 +1,198 @@ +/* + core/application.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "core/core.h" +#include "filesystem/filesystem.h" +#include "sys/sys.h" + +#include +#include +#include + +namespace core +{ + +// --------------- function repository ------------------------------ +extern "C" void func_print(std::stringstream &args) +{ + char text[MAXCMDSIZE]; + // FIXME leading space + if (args.getline(text, MAXCMDSIZE)) + con_print << text << std::endl; +} + +extern "C" void func_help(std::stringstream &args) +{ + con_print << "This is the help function" << std::endl; +} + +extern "C" void func_quit(std::stringstream &args) +{ + if (Application::instance()) { + Application::instance()->shutdown(); + Application::instance()->quit(0); + } +} + +extern "C" void func_connect(std::stringstream &args) +{ + if (Application::instance()) + Application::instance()->connect(); +} + +extern "C" void func_disconnect(std::stringstream &args) +{ + if (Application::instance()) + Application::instance()->disconnect(); +} + +// --------------- signal_handler ----------------------------------- + +extern "C" void signal_handler(int signum) +{ + switch (signum) { + case SIGHUP: + case SIGINT: + case SIGQUIT: + case SIGTERM: + if (Application::instance()) { + con_warn << "received signal " << signum << ", shutting down..." << std::endl; + Application::instance()->shutdown(); + Application::instance()->quit(0); + } else { + std::cerr << "received signal " << signum << ", terminated..." << std::endl; + sys::quit(1); + } + break; + default: + std::cerr << "received signal " << signum << ", terminated..." << std::endl; + sys::quit(1); + break; + } +} + +// --------------- Application ----------------------------- + +Application *Application::application_instance = 0; + +Application::Application() +{ + if (application_instance) { + std::cerr << "multiple core::Application instances!" << std::endl; + sys::quit(2); + } + application_instance = this; + + sys::signal(SIGHUP, signal_handler); + sys::signal(SIGINT, signal_handler); + sys::signal(SIGQUIT, signal_handler); + sys::signal(SIGTERM, signal_handler); +} + +Application::~Application() +{ + application_instance = 0; +} + +Application *Application::instance() +{ + return application_instance; +} + +void Application::init() +{ + con_print << "Initializing core..." << std::endl; + con_debug << "Debug messages enabled" << std::endl; + + // initialize core subsystems + filesystem::init(); + + // register our functions + func::add("print", func_print); + func::add("help", func_help); + func::add("quit", func_quit); + + func::add("connect", func_connect); + func::add("disconnect", func_disconnect); + + if (game()) + game()->connected = false; + else + con_warn << "No game module loaded!" << std::endl; + current_time = 0; +} + +void Application::shutdown() +{ + con_print << "Shutting down core..." << std::endl; + + if (game()) + if (game()->connected) + disconnect(); + else + con_warn << "No game module loaded!" << std::endl; + + filesystem::shutdown(); +} + +void Application::quit(int status) +{ + sys::quit(status); +} + +void Application::connect() +{ + if (!game()) { + con_warn << "No game module loaded!" << std::endl; + return; + } + + if (game()->connected) { + con_warn << "Connected. Disconnect first." << std::endl; + } + + game()->current_time = 0; + if (game()->connected = game()->init()) { + con_print << "Connected." << std::endl; + } else { + con_warn << "Connect failed." << std::endl; + } +} + +void Application::disconnect() +{ + if (!game()) { + con_warn << "No game module loaded!" << std::endl; + return; + } + + if (!game()->connected) { + con_warn << "Not connected." << std::endl; + return; + } + + game()->shutdown(); + + game()->connected = false; + game()->current_time = 0; + + con_print << "Disconnected." << std::endl; +} + +void Application::frame(float seconds) +{ + current_time += seconds; + + if (game() && game()->connected) { + game()->current_time += seconds; + game()->frame(seconds); + } + + // execute commands in the buffer + commandbuffer::execute(); +} + +} diff --git a/src/core/application.h b/src/core/application.h new file mode 100644 index 0000000..985c823 --- /dev/null +++ b/src/core/application.h @@ -0,0 +1,53 @@ +/* + core/application.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2. +*/ + +#ifndef __INCLUDED_CORE_APPLICATION_H__ +#define __INCLUDED_CORE_APPLICATION_H__ + +namespace core { + +/// core interface for the client and server Application classes +class Application { +public: + /// default constructor + Application(); + + /// default destructor + virtual ~Application(); + + /// initialize the application + virtual void init(); + + /// shutdown the application + virtual void shutdown(); + + /// run a core frame + virtual void frame(float seconds); + + /// a pointer to the current console instance + static Application *instance(); + + /// quit the application + virtual void quit(int status); + + /// connect to the game module + void connect(); + + /// disconnect from the game module + void disconnect(); + + /// time the core has been running, in seconds + float current_time; + + /// global application object + static Application *application_instance; +}; + + +} // namespace core + +#endif // __INCLUDED_CORE_APPLICATION_H__ + diff --git a/src/core/applicationinterface.cc b/src/core/applicationinterface.cc deleted file mode 100644 index 6add2e0..0000000 --- a/src/core/applicationinterface.cc +++ /dev/null @@ -1,197 +0,0 @@ -/* - core/applicationinterface.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#include "core/core.h" -#include "filesystem/filesystem.h" -#include "sys/sys.h" - -#include -#include -#include - -namespace core -{ - -// --------------- function repository ------------------------------ -extern "C" void func_print(std::stringstream &args) -{ - char text[MAXCMDSIZE]; - // FIXME leading space - if (args.getline(text, MAXCMDSIZE)) - con_print << text << std::endl; -} - -extern "C" void func_help(std::stringstream &args) -{ - con_print << "This is the help function" << std::endl; -} - -extern "C" void func_quit(std::stringstream &args) -{ - if (ApplicationInterface::instance()) { - ApplicationInterface::instance()->shutdown(); - ApplicationInterface::instance()->quit(0); - } -} - -extern "C" void func_connect(std::stringstream &args) -{ - if (ApplicationInterface::instance()) - ApplicationInterface::instance()->connect(); -} - -extern "C" void func_disconnect(std::stringstream &args) -{ - if (ApplicationInterface::instance()) - ApplicationInterface::instance()->disconnect(); -} - -// --------------- signal_handler ----------------------------------- -extern "C" void signal_handler(int signum) -{ - switch (signum) { - case SIGHUP: - case SIGINT: - case SIGQUIT: - case SIGTERM: - if (ApplicationInterface::instance()) { - con_warn << "received signal " << signum << ", shutting down..." << std::endl; - ApplicationInterface::instance()->shutdown(); - ApplicationInterface::instance()->quit(0); - } else { - std::cerr << "received signal " << signum << ", no application found, terminated..." << std::endl; - sys::quit(1); - } - break; - default: - std::cerr << "received signal " << signum << ", terminated..." << std::endl; - sys::quit(1); - break; - } -} - -// --------------- ApplicationInterface ----------------------------- - -ApplicationInterface *ApplicationInterface::applicationinterface_instance = 0; - -ApplicationInterface::ApplicationInterface() -{ - if (applicationinterface_instance) { - std::cerr << "multiple singleton instances: core::ApplicationInterface" << std::endl; - sys::quit(2); - } - applicationinterface_instance = this; - - sys::signal(SIGHUP, signal_handler); - sys::signal(SIGINT, signal_handler); - sys::signal(SIGQUIT, signal_handler); - sys::signal(SIGTERM, signal_handler); -} - -ApplicationInterface::~ApplicationInterface() -{ - applicationinterface_instance = 0; -} - -ApplicationInterface *ApplicationInterface::instance() -{ - return applicationinterface_instance; -} - -void ApplicationInterface::init() -{ - con_print << "Initializing core..." << std::endl; - con_debug << "Debug messages enabled" << std::endl; - - // initialize core subsystems - filesystem::init(); - - // register our functions - func::add("print", func_print); - func::add("help", func_help); - func::add("quit", func_quit); - - func::add("connect", func_connect); - func::add("disconnect", func_disconnect); - - if (game()) - game()->connected = false; - else - con_warn << "No game module loaded!" << std::endl; - current_time = 0; -} - -void ApplicationInterface::shutdown() -{ - con_print << "Shutting down core..." << std::endl; - - if (game()) - if (game()->connected) - disconnect(); - else - con_warn << "No game module loaded!" << std::endl; - - filesystem::shutdown(); -} - -void ApplicationInterface::quit(int status) -{ - sys::quit(status); -} - -void ApplicationInterface::connect() -{ - if (!game()) { - con_warn << "No game module loaded!" << std::endl; - return; - } - - if (game()->connected) { - con_warn << "Connected. Disconnect first." << std::endl; - } - - game()->current_time = 0; - if (game()->connected = game()->init()) { - con_print << "Connected." << std::endl; - } else { - con_warn << "Connect failed." << std::endl; - } -} - -void ApplicationInterface::disconnect() -{ - if (!game()) { - con_warn << "No game module loaded!" << std::endl; - return; - } - - if (!game()->connected) { - con_warn << "Not connected." << std::endl; - return; - } - - game()->shutdown(); - - game()->connected = false; - game()->current_time = 0; - - con_print << "Disconnected." << std::endl; -} - -void ApplicationInterface::frame(float seconds) -{ - current_time += seconds; - - if (game() && game()->connected) { - game()->current_time += seconds; - game()->frame(seconds); - } - - // execute commands in the buffer - commandbuffer::execute(); -} - -} diff --git a/src/core/applicationinterface.h b/src/core/applicationinterface.h deleted file mode 100644 index 76dfe01..0000000 --- a/src/core/applicationinterface.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - core/applicationinterface.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2. -*/ - -#ifndef __INCLUDED_CORE_APPLICATIONINTERFACE_H__ -#define __INCLUDED_CORE_APPLICATIONINTERFACE_H__ - -namespace core { - -/// core interface for the client and server Application classes -class ApplicationInterface { -public: - /// default constructor - ApplicationInterface(); - - /// default destructor - virtual ~ApplicationInterface(); - - /// initialize the application - virtual void init(); - - /// shutdown the application - virtual void shutdown(); - - /// run a core frame - virtual void frame(float seconds); - - /// a pointer to the current console instance - static ApplicationInterface *instance(); - - /// quit the application - virtual void quit(int status); - - /// connect to the game module - void connect(); - - /// disconnect from the game module - void disconnect(); - - /// time the core has been running, in seconds - float current_time; -private: - /// console singleton - static ApplicationInterface *applicationinterface_instance; -}; - - -} // namespace core - -#endif // __INCLUDED_CORE_APPLICATIONINTERFACE_H__ - diff --git a/src/core/core.h b/src/core/core.h index eb8e067..3802cca 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -8,7 +8,7 @@ #define __INCLUDED_CORE_H__ #include "core/gameinterface.h" -#include "core/applicationinterface.h" +#include "core/application.h" /// core contains the basic functionality of the engine namespace core @@ -17,13 +17,13 @@ namespace core inline GameInterface *game() { return GameInterface::instance(); } /// pointer to the current ApplicationInterface - inline ApplicationInterface *application() { return ApplicationInterface::instance(); } + inline Application *application() { return Application::instance(); } /// true if the core is connected to a game module inline bool connected() { return (GameInterface::instance() && GameInterface::instance()->connected); } /// return the time the core has been running, in seconds - inline float time() { return ApplicationInterface::instance()->current_time; } + inline float time() { return Application::instance()->current_time; } }; #include "core/commandbuffer.h" -- cgit v1.2.3