From a51deebd87036ceb87c77a20117977d077b771e3 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 5 Feb 2008 20:44:36 +0000 Subject: fixed cvar, added cvars r_width r_height r_fullscreen, added function r_restart --- src/client/client.cc | 17 ++++++++ src/client/console.cc | 6 +-- src/client/console.h | 2 +- src/client/input.cc | 14 +++---- src/client/video.cc | 22 +++++++++- src/core/application.cc | 33 ++++++++++----- src/core/commandbuffer.cc | 18 +++++++- src/core/core.h | 1 + src/core/cvar.cc | 103 +++++++++++++++++++++++++++++++--------------- src/core/cvar.h | 62 +++++++++++++++++++--------- src/core/func.cc | 19 ++++++--- src/core/func.h | 13 ++++-- 12 files changed, 225 insertions(+), 85 deletions(-) (limited to 'src') diff --git a/src/client/client.cc b/src/client/client.cc index ba1c0bc..993e849 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -44,6 +44,14 @@ public: Client app; +//--- engine functions -------------------------------------------- + +extern "C" void func_r_restart(std::stringstream &args) +{ + video::shutdown(); + video::init(); +} + //--- public ------------------------------------------------------ game::Game game; @@ -73,6 +81,9 @@ void Client::init() // initialize core core::Application::init(); + // initialize SDL, but do not initialize any subsystems + SDL_Init(0); + // Initialize the video subsystem if (!client::video::init()) quit(1); @@ -82,6 +93,9 @@ void Client::init() // initialize input input::init(); + + // add engine functions + core::func::add("r_restart", func_r_restart); } void Client::run() @@ -119,6 +133,9 @@ void Client::shutdown() con_print << "Shutting down client..." << std::endl; console::flush(); + // remove engine functions + core::func::remove("r_restart"); + console::shutdown(); console::flush(); diff --git a/src/client/console.cc b/src/client/console.cc index 127481a..bf193e5 100644 --- a/src/client/console.cc +++ b/src/client/console.cc @@ -64,7 +64,7 @@ void init() console_visible = false; - // register our engine functions + // add engine functions core::func::add("con_toggle", func_con_toggle); } @@ -72,7 +72,7 @@ void shutdown() { con_print << "Shutting down console..." << std::endl; - // unregister our engine functions + // remove engine functions core::func::remove("con_toggle"); } @@ -159,7 +159,7 @@ void toggle() console_visible = !console_visible; } -void handle_keyreleased(SDL_keysym* keysym) +void handle_keypressed(SDL_keysym* keysym) { switch( keysym->sym ) { case SDLK_RETURN: diff --git a/src/client/console.h b/src/client/console.h index 9eeb57e..d185390 100644 --- a/src/client/console.h +++ b/src/client/console.h @@ -41,7 +41,7 @@ void draw(); void toggle(); /// handle keyboard input -void handle_keyreleased(SDL_keysym* keysym); +void handle_keypressed(SDL_keysym* keysym); /// true of the console is visible bool visible(); diff --git a/src/client/input.cc b/src/client/input.cc index 91f462c..9bcf4fe 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -23,8 +23,8 @@ void init() con_print << "Initializing input..." << std::endl; //condebug << "SDL_DEFAULT_REPEAT_DELAY " << SDL_DEFAULT_REPEAT_DELAY << std::endl; - //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - SDL_EnableKeyRepeat(10, SDL_DEFAULT_REPEAT_INTERVAL); + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + //SDL_EnableKeyRepeat(10, SDL_DEFAULT_REPEAT_INTERVAL); } void shutdown() @@ -99,7 +99,10 @@ void frame(float seconds) client::application.shutdown(); } */ - if (core::connected() && !console::visible()) { + if (console::visible()) { + // send key events to the console + console::handle_keypressed( &event.key.keysym ); + } else if (core::connected()) { // send key events to the game world handle_keypressed( &event.key.keysym ); } @@ -108,10 +111,7 @@ void frame(float seconds) case SDL_KEYUP: if (event.key.keysym.sym == '`' || event.key.keysym.sym == '~') { console::toggle(); - } else if (console::visible()) { - // send key events to the console - console::handle_keyreleased( &event.key.keysym ); - } else if (core::connected()) { + } else if (!console::visible() && core::connected()) { // send key events to the game world handle_keyreleased( &event.key.keysym ); } diff --git a/src/client/video.cc b/src/client/video.cc index bb748d0..df11b90 100644 --- a/src/client/video.cc +++ b/src/client/video.cc @@ -7,6 +7,7 @@ #include "client/video.h" #include "client/view.h" #include "render/render.h" +#include "core/core.h" #include "sys/sys.h" #include @@ -24,6 +25,12 @@ int width = 0; int height = 0; float aspect = 1; +//--- cvars ------------------------------------------------------- + +core::Cvar r_width; +core::Cvar r_height; +core::Cvar r_fullscreen; + void reset() { // recalculate the video aspect @@ -40,10 +47,15 @@ bool init() { con_print << "Initializing video..." << std::endl; + // initialize cvars + r_width = core::cvar::get("r_width", defaultwidth); + r_height = core::cvar::get("r_height", defaultheight); + r_fullscreen = core::cvar::get("r_fullscreen", 1); + int bpp = 0; int flags = 0; - if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { + if( SDL_InitSubSystem(SDL_INIT_VIDEO) < 0 ) { std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl; return false; } @@ -72,10 +84,13 @@ bool init() return false; } - aspect = (float) width / (float) height; con_print << " video mode " << width << "x" << height << "x" << bpp << "bpp" << std::endl; + + aspect = (float) width / (float) height; + render::init(); + view::init(); video::reset(); @@ -96,10 +111,13 @@ void shutdown() con_print << "Shutting down video..." << std::endl; view::shutdown(); + render::shutdown(); width = 0; height = 0; + + SDL_QuitSubSystem(SDL_INIT_VIDEO); } } // namespace video diff --git a/src/core/application.cc b/src/core/application.cc index 810bacb..a5d2937 100644 --- a/src/core/application.cc +++ b/src/core/application.cc @@ -19,9 +19,12 @@ namespace core extern "C" void func_print(std::stringstream &args) { char text[MAXCMDSIZE]; - // FIXME leading space - if (args.getline(text, MAXCMDSIZE)) - con_print << text << std::endl; + if (args.getline(text, MAXCMDSIZE)) { + // remove the leading space + if (text[0] && text[1]) { + con_print << text+1 << std::endl; + } + } } extern "C" void func_help(std::stringstream &args) @@ -49,6 +52,16 @@ extern "C" void func_disconnect(std::stringstream &args) Application::instance()->disconnect(); } +extern "C" void func_listfunc(std::stringstream &args) +{ + func::list(); +} + +extern "C" void func_listcvar(std::stringstream &args) +{ + cvar::list(); +} + // --------------- signal_handler ----------------------------------- extern "C" void signal_handler(int signum) @@ -118,10 +131,11 @@ void Application::init() func::add("connect", func_connect); func::add("disconnect", func_disconnect); + func::add("listcvar", func_listcvar); + func::add("listfunc", func_listfunc); + if (game()) game()->connected = false; - else - con_warn << "No game module loaded!" << std::endl; current_time = 0; } @@ -129,11 +143,10 @@ 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; + if (game() && game()->connected) + disconnect(); + + //if (game()) unload(); filesystem::shutdown(); } diff --git a/src/core/commandbuffer.cc b/src/core/commandbuffer.cc index 7ba1e08..67e90d9 100644 --- a/src/core/commandbuffer.cc +++ b/src/core/commandbuffer.cc @@ -25,13 +25,29 @@ void exec(const char *text) cmdstream >> cmdname; + // is it a function Func f = func::find(cmdname); - if (f) { + // function exists, execute it f(cmdstream); return; } + // is it a cvar + Cvar cv = cvar::find(cmdname); + if (cv) { + // cvar exists + std::string args; + if (cmdstream >> args) { + // we're setting a new value + char c; + while(cmdstream >> c) + args += c; + (*cv) = args; + } + con_print << cmdname << cv->text() << std::endl; + } + con_print << "unknown command '" << cmdname << "'" << std::endl; } diff --git a/src/core/core.h b/src/core/core.h index 3802cca..688d4c9 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -27,6 +27,7 @@ namespace core }; #include "core/commandbuffer.h" +#include "core/cvar.h" #include "core/func.h" #endif // __INCLUDED_CORE_H__ diff --git a/src/core/cvar.cc b/src/core/cvar.cc index 21105b4..f49438c 100644 --- a/src/core/cvar.cc +++ b/src/core/cvar.cc @@ -21,96 +21,131 @@ Cvar_t::Cvar_t(unsigned int cvarflags) cvar_flags = cvarflags; } -Cvar_t & Cvar_t::operator=(const std::string &cvarvalue) +Cvar_t & Cvar_t::operator=(const std::string &other) { - cvar_value = cvarvalue; + cvar_text = other; + std::stringstream s(cvar_text); + if (!(s >> cvar_value)) + cvar_value = cvar_text.size(); return (*this); } -Cvar_t & Cvar_t::operator=(const char *cvarvalue) +Cvar_t & Cvar_t::operator=(const char *other) { - cvar_value.assign(cvarvalue); + return ((*this) = std::string(other)); +} + +Cvar_t & Cvar_t::operator=(int other) +{ + std::stringstream s; + s << other; + s >> cvar_text; + cvar_value = (float) other; return (*this); } -Cvar_t & Cvar_t::operator=(int cvarvalue) +Cvar_t & Cvar_t::operator=(float other) { std::stringstream s; - s << cvarvalue; - s >> cvar_value; + s << other; + s >> cvar_text; + cvar_value = other; return (*this); } -namespace cvar +namespace cvar { -std::map registry; +std::map cvarregistry; -Cvar set(const char *cvarname, const char *cvarvalue, int cvarflags) +Cvar get(const char *name, const char *value, int flags) { - Cvar c = registry[std::string(cvarname)]; + Cvar c = cvarregistry[std::string(name)]; if (c) { - con_debug << "cvar::set " << cvarname << " already exist with value " << cvarvalue << std::endl; + con_debug << "cvar::get " << name << " already exist with value " << value << std::endl; } else { - con_debug << "cvar::set " << cvarname << " with value " << cvarvalue << std::endl; - c = new Cvar_t(cvarflags); - registry[std::string(cvarname)] = c; - (*c) = cvarvalue; + con_debug << "cvar::get " << name << " " << value << std::endl; + c = new Cvar_t(flags); + cvarregistry[std::string(name)] = c; + (*c) = value; } return c; } -Cvar set(const char *cvarname, int cvarvalue, int cvarflags) +Cvar get(const char *name, int value, int flags) { - Cvar c = registry[std::string(cvarname)]; + Cvar c = cvarregistry[std::string(name)]; if (c) { - con_debug << "cvar::set " << cvarname << " already exist with value " << cvarvalue << std::endl; + con_debug << "cvar::get " << name << " already exist with value " << value << std::endl; } else { - con_debug << "cvar::set " << cvarname << " with value " << cvarvalue << std::endl; - c = new Cvar_t(cvarflags); - registry[std::string(cvarname)] = c; - (*c) = cvarvalue; + con_debug << "cvar::get " << name << " " << value << std::endl; + c = new Cvar_t(flags); + cvarregistry[std::string(name)] = c; + (*c) = value; + } + return c; +} + +Cvar set(const char *name, const char *value, int flags) +{ + Cvar c = cvarregistry[std::string(name)]; + if (!c) { + c = new Cvar_t(flags); + cvarregistry[std::string(name)] = c; + } + con_debug << "cvar::set " << name << " " << value << std::endl; + (*c) = value; + return c; +} + +Cvar set(const char *name, int value, int flags) +{ + Cvar c = cvarregistry[std::string(name)]; + if (!c) { + c = new Cvar_t(flags); + cvarregistry[std::string(name)] = c; } + con_debug << "cvar::set " << name << " " << value << std::endl; + (*c) = value; return c; } void unset(const char *cvarname) { - Cvar c = registry[std::string(cvarname)]; + Cvar c = cvarregistry[std::string(cvarname)]; if (c) { con_debug << "cvar::unset " << cvarname << std::endl; - registry.erase(std::string(cvarname)); + cvarregistry.erase(std::string(cvarname)); delete c; } } void unset(const std::string &cvarname) { - Cvar c = registry[cvarname]; + Cvar c = cvarregistry[cvarname]; if (c) { con_debug << "cvar::unset " << cvarname << std::endl; - registry.erase(cvarname); + cvarregistry.erase(cvarname); delete c; } } Cvar find(const std::string &cvarname) { - return registry[cvarname]; + return cvarregistry[cvarname]; } Cvar find(const char *cvarname) { - return registry[std::string(cvarname)]; + return cvarregistry[std::string(cvarname)]; } void list() { - std::map::iterator registryiterator; - for (registryiterator = registry.begin(); registryiterator != registry.end(); registryiterator++) { - con_print << std::setw(4) << (*registryiterator).second->flags() - << " " << (*registryiterator).first - << " " << (*registryiterator).second->value() << std::endl; + con_print << "-- listcvar -----------------" << std::endl; + std::map::iterator cvarregistryiterator; + for (cvarregistryiterator = cvarregistry.begin(); cvarregistryiterator != cvarregistry.end(); cvarregistryiterator++) { + con_print << " "<< (*cvarregistryiterator).first << " " << (*cvarregistryiterator).second->text() << std::endl; } } diff --git a/src/core/cvar.h b/src/core/cvar.h index 8ad0fa1..fa04c49 100644 --- a/src/core/cvar.h +++ b/src/core/cvar.h @@ -11,40 +11,61 @@ namespace core { -/// Don't use - need a decent cvar +/// the cvar container class class Cvar_t { public: - Cvar_t(unsigned int cvarflags = 0); + Cvar_t(unsigned int cvflags = 0); - inline const std::string &value() const {return cvar_value; } - inline unsigned int flags() const { return cvar_flags; } - - Cvar_t &operator=(const char *cvarvalue); - Cvar_t &operator=(const std::string &cvarvalue); - Cvar_t &operator=(int cvarvalue); + Cvar_t &operator=(const char *other); + Cvar_t &operator=(const std::string &other); + Cvar_t &operator=(int other); + Cvar_t &operator=(float other); + inline unsigned int flags() const { return cvar_flags; } + inline float value() const { return(cvar_value); } + const std::string &text() const { return(cvar_text); } + private: - std::string cvar_value; - unsigned int cvar_flags; + std::string cvar_text; + float cvar_value; + unsigned int cvar_flags; }; /// general cvar type typedef Cvar_t *Cvar; -/// Don't use - need a decent cvar +/// the cvar registry namespace cvar { -/// create a new cvar containing a string value -Cvar set(const char *cvarname, const char *cvarvalue, int cvarflags=0); - -/// create a new cvar containing an integer value -Cvar set(const char *cvarname, int cvarvalue, int cvarflags=0); - -/// delete a cvar +/// get a cvar value from the registry +/** If the a cvar with the given name already exists in the registry, + * its value will not be changed. If the cvar does not exist, + * it will be created + */ +Cvar get(const char *name, const char *value, int flags=0); +/// get a cvar value from the registry +/** If the a cvar with the given name already exists in the registry, + * its value will not be changed. If the cvar does not exist, + * it will be created + */ +Cvar get(const char *name, int value, int flags=0); + +/// set a cvar value +/** If the a cvar with the given name already exists in the registry, + * its value will be replaced + */ +Cvar set(const char *name, const char *value, int flags=0); +/// set a cvar value +/** If the a cvar with the given name already exists in the registry, + * its value will be replaced + */ +Cvar set(const char *name, int value, int flags=0); + +/// delete a cvar from the registry void unset(const char *cvarname); -/// delete a cvar +/// delete a cvar from the registry void unset(const std::string &cvarname); /// search for a named cvar, returns 0 if not found @@ -53,6 +74,9 @@ Cvar find(const std::string &cvarname); /// search for a named cvar, returns 0 if not found Cvar find(const char *cvarname); +/// list the cvar registry +void list(); + } // namespace cvar } // namespace core diff --git a/src/core/func.cc b/src/core/func.cc index bcc6f50..c1ed826 100644 --- a/src/core/func.cc +++ b/src/core/func.cc @@ -14,26 +14,35 @@ namespace core namespace func { -std::map registry; +std::map funcregistry; void add(const char * functionname, Func functionptr) { - registry[std::string(functionname)] = functionptr; + funcregistry[std::string(functionname)] = functionptr; } void remove(const char *functionname) { - registry.erase(std::string(functionname)); + funcregistry.erase(std::string(functionname)); } void remove(const std::string &functionname) { - registry.erase(functionname); + funcregistry.erase(functionname); } Func find(const std::string &functionname) { - return registry[functionname]; + return funcregistry[functionname]; +} + +void list() +{ + con_print << "-- listfunc -----------------" << std::endl; + std::map::iterator funcregistryiterator; + for (funcregistryiterator = funcregistry.begin(); funcregistryiterator != funcregistry.end(); funcregistryiterator++) { + con_print << " " << (*funcregistryiterator).first << std::endl; + } } } // namespace func diff --git a/src/core/func.h b/src/core/func.h index 1af69c0..ad189b8 100644 --- a/src/core/func.h +++ b/src/core/func.h @@ -1,5 +1,5 @@ /* - core/funct.h + core/func.h This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ @@ -7,6 +7,8 @@ #ifndef __INCLUDED_CORE_FUNC_H__ #define __INCLUDED_CORE_FUNC_H__ +#include "sys/sys.h" + #include namespace core @@ -28,8 +30,13 @@ void remove(const std::string &functionname); /** Returns 0 if the function pointer could not be found */ Func find(const std::string &functionname); -} -} +/// list the function registry +void list(); + +} // namespace func + +} // namespace core #endif // __INCLUDED_CORE_FUNC_H__ + -- cgit v1.2.3