From ee0a9e6eef88eb7e6dd10f9a1ae5beaf4e43db53 Mon Sep 17 00:00:00 2001 From: Brian Caine Date: Sun, 7 Sep 2008 04:35:24 -0400 Subject: [PATCH] FRESH AND RAW Yeah, just 'cause I'm paranoid about losing data, I'm pushing this. But it's not polished yet. It's 4:30 am here. --- TODO | 5 ++ dtd/actor.dtd | 8 ++- include/core/Actor.h | 13 +++-- include/core/Game.h | 6 +- include/core/Level.h | 1 + include/core/MediaLoader.h | 6 ++ include/core/parseactor.h | 9 +++ include/plugins/audio/Audio.h | 2 + include/plugins/graphics/Drawable.h | 3 + include/plugins/graphics/Graphics.h | 4 +- src/SDLAnimation/SDLAnimation.cpp | 47 +++++++++++++++ src/SDLAnimation/SDLAnimation.h | 19 +++++- src/SDLAnimation/SDLAnimationPlugin.cpp | 13 ++++- src/SDLAnimation/SDLAnimationPlugin.h | 3 +- src/SDLPlugin/SDLPlugin.cpp | 3 +- src/SDLPlugin/SDLPlugin.h | 3 +- src/SDLPlugin/SDLSurface.cpp | 5 ++ src/SDLPlugin/SDLSurface.h | 1 + src/core/Actor.cpp | 100 +++++++++++++++++++++++++++++++- src/core/Game.cpp | 13 ++++- src/core/Level.cpp | 4 +- src/core/MediaLoader.cpp | 11 ++++ src/core/parseactor.cpp | 31 ++++++++++ 23 files changed, 289 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index d312b08..3967b8f 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,9 @@ Ok, so this is a list of stuff to do. {in no particular order} +!! Important !! + +* Make the level static graphic stuff as it is with the actor graphic stuff + * Finish up the script bindings * Add zindexing for the graphics in sprites * Provide the Level(Level& old_level) function @@ -24,3 +28,4 @@ In the future, rewrites do this: * Clean up the scripting interface; why are there contructors and destructors when they're not used? * Make the coordinate systems consistent +* Use stuff in std::algorithm. The adhoc stuff you have now sucks ass diff --git a/dtd/actor.dtd b/dtd/actor.dtd index e2bf677..d28378f 100644 --- a/dtd/actor.dtd +++ b/dtd/actor.dtd @@ -11,7 +11,13 @@ - + + + + + + + diff --git a/include/core/Actor.h b/include/core/Actor.h index e170dc1..9ecf24c 100644 --- a/include/core/Actor.h +++ b/include/core/Actor.h @@ -48,18 +48,15 @@ namespace fragrant ActorPair makePair(float, float); const int INV_Y = 0; - // so, we need to invert y for graphics/physics stuff - // 500 is an arbitrary point, and shouldn't really matter - - // well what about static lines, eh? - - // yeah, so, fixed it to zero struct ActorGraphics // fixme { std::string sauce; ActorPair pos; float angle; + std::map > params; + // params[param name].x = type + // params[param name].y = value }; // a line fills the first two pairs in PhysicsShape's data member with @@ -96,6 +93,8 @@ namespace fragrant std::vector shapes; // oh god, no! }; + class Drawable; + struct ActorGraphic // fixme: poor choice of naming here { LevelPair position; @@ -107,6 +106,8 @@ namespace fragrant class Game; class PhysicsObject; class PhysicsSimulation; + class Target; + class MediaLoader; struct ActorData { std::string name; diff --git a/include/core/Game.h b/include/core/Game.h index 81b94f7..a904182 100644 --- a/include/core/Game.h +++ b/include/core/Game.h @@ -97,6 +97,7 @@ namespace fragrant class Level; struct ActorData; struct LevelData; + class MediaLoader; class Game : public EventReceiver, public ScriptedClass { public: @@ -110,7 +111,8 @@ namespace fragrant void raiseEvent(Event event); - Drawable* getGraphics(std::string name); + Drawable* getGraphics(std::string name, + std::map params); AudioSample* getAudio(std::string name); std::string getFile(std::string name); @@ -130,6 +132,8 @@ namespace fragrant static Variant* getCurrentGame(std::vector); static GameData parseXML(std::string path, MediaLoader&); + InputManager* getInputManager(); + private: MediaLoader& media_loader; PluginLoader& plugin_loader; diff --git a/include/core/Level.h b/include/core/Level.h index 364dd59..78b20db 100644 --- a/include/core/Level.h +++ b/include/core/Level.h @@ -100,6 +100,7 @@ namespace fragrant class PhysicsSimulation; class Actor; class Game; + class Display; class Level : public ScriptedClass { public: diff --git a/include/core/MediaLoader.h b/include/core/MediaLoader.h index 10f8fa5..2cb0dea 100644 --- a/include/core/MediaLoader.h +++ b/include/core/MediaLoader.h @@ -34,6 +34,7 @@ namespace fragrant { + class Game; class MediaLoader { public: @@ -44,8 +45,13 @@ namespace fragrant std::string loadMedia(std::string source); std::map& plugins(); + Game* getGame(); + void setGame(Game* ngame); + private: std::map plugin_map; + + Game* game; }; } diff --git a/include/core/parseactor.h b/include/core/parseactor.h index 957af6e..bf09b4e 100644 --- a/include/core/parseactor.h +++ b/include/core/parseactor.h @@ -36,11 +36,19 @@ namespace XMLParser std::string m_pair; }; + struct param + { + std::string name; + std::string type; + std::string value; + }; + struct graphics { std::string m_source; pos m_pos; float m_angle; + std::vector m_params; }; struct body @@ -109,6 +117,7 @@ namespace XMLParser actor parseactorFromBuffer(std::string); + param parseparam(xmlpp::Node*); float parseelasticity(xmlpp::Node*); float parsefriction(xmlpp::Node*); actor parseactor(xmlpp::Node*); diff --git a/include/plugins/audio/Audio.h b/include/plugins/audio/Audio.h index d3ad2b2..99c532b 100644 --- a/include/plugins/audio/Audio.h +++ b/include/plugins/audio/Audio.h @@ -32,6 +32,8 @@ namespace fragrant { + class MediaLoader; + typedef AudioSample* (*audio_function)(std::string, MediaLoader&); typedef AudioDevice* (*make_device_function)(); diff --git a/include/plugins/graphics/Drawable.h b/include/plugins/graphics/Drawable.h index c8febbe..5215a8e 100644 --- a/include/plugins/graphics/Drawable.h +++ b/include/plugins/graphics/Drawable.h @@ -26,6 +26,8 @@ #include "Graphics.h" #include "Target.h" +#include "../../core/ScriptedClass.h" + namespace fragrant { class Target; @@ -34,6 +36,7 @@ namespace fragrant public: virtual void draw(Target* target, GraphicsRect* dstrect, GraphicsPair* srcrect, float angle) = 0; + virtual ScriptedClass* getScriptObject() = 0; virtual void destroy() = 0; private: diff --git a/include/plugins/graphics/Graphics.h b/include/plugins/graphics/Graphics.h index 6b51cb7..80853e5 100644 --- a/include/plugins/graphics/Graphics.h +++ b/include/plugins/graphics/Graphics.h @@ -19,8 +19,10 @@ namespace fragrant { class Display; class Drawable; + class MediaLoader; - typedef Drawable* (*image_function)(MediaLoader&,std::string); + typedef Drawable* (*image_function) + (MediaLoader&,std::string, std::map); typedef Display* (*display_function)(); typedef void (*destroy_function)(Drawable*); diff --git a/src/SDLAnimation/SDLAnimation.cpp b/src/SDLAnimation/SDLAnimation.cpp index 09af615..eb0db6a 100644 --- a/src/SDLAnimation/SDLAnimation.cpp +++ b/src/SDLAnimation/SDLAnimation.cpp @@ -95,6 +95,21 @@ void SDLAnimation::draw(Target* target, GraphicsRect* destrect, while (ticks_left <= 0) { cur_frame++; + + if (cur_frame >= data.frames.size()) + { + Event event; + event.name = ANIMATION_LOOP_EVENT; + + if (data.id != "") + { + event.data["id"] = new Variant; + *(event.data["id"]) = data.id; + } + + data.input_manager->appendEvent(event); + } + cur_frame %= data.frames.size(); ticks_left = data.frames.at(cur_frame).y + ticks_left; @@ -141,6 +156,11 @@ void SDLAnimation::draw(Target* target, GraphicsRect* destrect, return; } +ScriptedClass* SDLAnimation::getScriptObject() +{ + return dynamic_cast(this); +} + void SDLAnimation::destroy() { int cur; @@ -152,3 +172,30 @@ void SDLAnimation::destroy() return; } + +func SDLAnimation::getConstructor() +{ + return 0; +} + +std::string SDLAnimation::getClassName() +{ + return "Animation"; +} + +std::vector SDLAnimation::getClassFunctions() +{ + std::vector results; + +// results.push_back("pause"); +// results.push_back("setFrame"); + + return results; +} + +Variant* SDLAnimation::callFunction(std::string funcname, + std::vector params, ScriptVM* script_vm) +{ + Variant* results = new Variant; + return results; +} diff --git a/src/SDLAnimation/SDLAnimation.h b/src/SDLAnimation/SDLAnimation.h index fdb59e7..2aa81dd 100644 --- a/src/SDLAnimation/SDLAnimation.h +++ b/src/SDLAnimation/SDLAnimation.h @@ -25,10 +25,14 @@ #include #include "../../include/plugins/graphics/Graphics.h" +#include "../../include/plugins/input/InputManager.h" +#include "../../include/core/Game.h" #include "../SDLPlugin/SDLSurface.h" namespace fragrant { + const std::string ANIMATION_LOOP_EVENT = "SDLAnimation_Loop"; + struct SDLAnimationFrame { SDL_Surface* image; @@ -41,9 +45,11 @@ namespace fragrant std::vector > frames; // Pair.x = frame; .y = duration std::vector root_surfaces; + InputManager* input_manager; + std::string id; }; - class SDLAnimation : public Drawable + class SDLAnimation : public Drawable, public ScriptedClass { public: SDLAnimation(SDLAnimationData ndata); @@ -51,8 +57,19 @@ namespace fragrant void draw(Target* target, GraphicsRect* destrect, GraphicsPair* srcrect, float angle); + ScriptedClass* getScriptObject(); void destroy(); + // script stuff + + func getConstructor(); + + std::string getClassName(); + std::vector getClassFunctions(); + + Variant* callFunction(std::string funcname, + std::vector params, ScriptVM* script_vm); + protected: SDLAnimationData data; diff --git a/src/SDLAnimation/SDLAnimationPlugin.cpp b/src/SDLAnimation/SDLAnimationPlugin.cpp index ebd87d0..0fe4c3a 100644 --- a/src/SDLAnimation/SDLAnimationPlugin.cpp +++ b/src/SDLAnimation/SDLAnimationPlugin.cpp @@ -100,7 +100,9 @@ void insert_animation_data(XMLAnimationData& data, return; } -Drawable* make_animation(fragrant::MediaLoader& loader, std::string filename) +Drawable* make_animation(fragrant::MediaLoader& loader, + std::string filename, std::map args = std::map()) { std::string buffer; @@ -188,6 +190,15 @@ Drawable* make_animation(fragrant::MediaLoader& loader, std::string filename) } SDLAnimationData data; + data.input_manager = loader.getGame()->getInputManager(); + + if (args.find("id") != args.end() && + args["id"]->verifyType()) + data.id = args["id"]->get(); + + else + data.id = ""; + XMLAnimationData clean_data = pruneData(xml_data); std::vector image_sources; diff --git a/src/SDLAnimation/SDLAnimationPlugin.h b/src/SDLAnimation/SDLAnimationPlugin.h index 822ec58..877faea 100644 --- a/src/SDLAnimation/SDLAnimationPlugin.h +++ b/src/SDLAnimation/SDLAnimationPlugin.h @@ -28,7 +28,8 @@ extern "C" { - fragrant::Drawable* make_animation(fragrant::MediaLoader&, std::string); + fragrant::Drawable* make_animation(fragrant::MediaLoader&, std::string, + std::map); } #endif diff --git a/src/SDLPlugin/SDLPlugin.cpp b/src/SDLPlugin/SDLPlugin.cpp index e19988b..8efe53b 100644 --- a/src/SDLPlugin/SDLPlugin.cpp +++ b/src/SDLPlugin/SDLPlugin.cpp @@ -30,7 +30,8 @@ using namespace fragrant; -Drawable* make_image(fragrant::MediaLoader& loader, std::string filename) +Drawable* make_image(fragrant::MediaLoader& loader, std::string filename, + std::map args = std::map()) { std::string data; diff --git a/src/SDLPlugin/SDLPlugin.h b/src/SDLPlugin/SDLPlugin.h index a52a2b6..7e6564f 100644 --- a/src/SDLPlugin/SDLPlugin.h +++ b/src/SDLPlugin/SDLPlugin.h @@ -29,7 +29,8 @@ extern "C" { - fragrant::Drawable* make_image(fragrant::MediaLoader&, std::string); + fragrant::Drawable* make_image(fragrant::MediaLoader&, std::string, + std::map); fragrant::Display* make_display(); } diff --git a/src/SDLPlugin/SDLSurface.cpp b/src/SDLPlugin/SDLSurface.cpp index 1f98326..db09c8a 100644 --- a/src/SDLPlugin/SDLSurface.cpp +++ b/src/SDLPlugin/SDLSurface.cpp @@ -77,6 +77,11 @@ void SDLSurface::draw(Target* target, GraphicsRect* destrect, return; } +ScriptedClass* SDLSurface::getScriptObject() +{ + return 0; +} + void SDLSurface::destroy() { delete this; diff --git a/src/SDLPlugin/SDLSurface.h b/src/SDLPlugin/SDLSurface.h index 3d71ff0..1748470 100644 --- a/src/SDLPlugin/SDLSurface.h +++ b/src/SDLPlugin/SDLSurface.h @@ -38,6 +38,7 @@ namespace fragrant void draw(Target* target, GraphicsRect* destrect, GraphicsPair* srcrect, float angle); + ScriptedClass* getScriptObject(); void destroy(); SDL_Surface* surf; diff --git a/src/core/Actor.cpp b/src/core/Actor.cpp index 5043642..5ee63e3 100644 --- a/src/core/Actor.cpp +++ b/src/core/Actor.cpp @@ -41,6 +41,61 @@ ActorPair fragrant::makePair(float x, float y) return results; } +void clearStringToVariantMap(std::map params) +{ + std::map::iterator iter; + + for (iter = params.begin(); iter != params.end(); iter++) + delete iter->second; + + return; +} + +std::map + stringToVariantMap(std::map > params) +{ + std::map results; + std::map >::iterator iter; + + for (iter = params.begin(); iter != params.end(); iter++) + { + results[iter->first] = new Variant; + + if (iter->second.x == "std::string") + { + *(results[iter->first]) = iter->second.y; + continue; + } + + if (iter->second.x == "bool") + { + *(results[iter->first]) = fromString(iter->second.y); + continue; + } + + if (iter->second.x == "int") + { + *(results[iter->first]) = fromString(iter->second.y); + continue; + } + + if (iter->second.x == "float") + { + *(results[iter->first]) = fromString(iter->second.y); + continue; + } + + delete results[iter->first]; + results.erase(results.find(iter->first)); + + std::cerr << "stringToVariantMap(): failed " + "to identify type, \"" + << iter->second.x << "\"" << std::endl; + } + + return results; +} + Actor::Actor(ActorData data) { persistent = false; // debug @@ -58,7 +113,10 @@ Actor::Actor(ActorData data) ngraphic.angle = c_graphics.angle; ngraphic.name = c_graphics.sauce; - ngraphic.graphic = data.game->getGraphics(c_graphics.sauce); + std::map params = + stringToVariantMap(c_graphics.params); + ngraphic.graphic = data.game->getGraphics(c_graphics.sauce, params); + clearStringToVariantMap(params); sprites.push_back(ngraphic); } @@ -594,13 +652,36 @@ Variant* Actor::callFunction(std::string funcname, } } + std::map nparams; + + if (params.size() > 5) + { + int cur; + int cap = (params.size() % 2) ? -1 : -2; + for (cur = 4; cur < params.size()+cap; cur+=2) + { + if (!params.at(cur)->verifyType()) + { + std::cout + << "Actor::callFunction(funcname=\"addGraphic\")" + ": wrong params" + << std::endl; + return results; + } + + nparams[params.at(cur)->get()] = + params.at(cur + 1); + } + } + ActorGraphic ngraphic; ngraphic.name = name; ngraphic.position.x = pos.x; ngraphic.position.y = pos.y; ngraphic.angle = angle; - ngraphic.graphic = initial_params.game->getGraphics(ngraphic.name); + ngraphic.graphic = initial_params.game-> + getGraphics(ngraphic.name, nparams); if (ngraphic.graphic) { @@ -826,7 +907,20 @@ ActorData Actor::parseXML(std::string source, MediaLoader& media_loader) graphics.sauce = cur_graphics.m_source; graphics.pos = parsePair(cur_graphics.m_pos.m_pair); - graphics.angle = 0.0f; // for the moment + graphics.angle = cur_graphics.m_angle; // fix'd + + int tcur; + for (tcur = 0; tcur < cur_graphics.m_params.size(); tcur++) + { + std::string param_name = cur_graphics.m_params.at(tcur).name; + std::string param_value = cur_graphics.m_params.at(tcur).value; + std::string param_type = cur_graphics.m_params.at(tcur).type; + + graphics.params[param_name] = Pair(); + + graphics.params[param_name].x = param_type; + graphics.params[param_name].y = param_value; + } results.graphics.push_back(graphics); } diff --git a/src/core/Game.cpp b/src/core/Game.cpp index 2bd1fa5..94534b8 100644 --- a/src/core/Game.cpp +++ b/src/core/Game.cpp @@ -72,6 +72,7 @@ Game::Game(GameData gdata, std::string dpresentation, gdata.printData(); current_game = this; + media_loader.setGame(this); std::string root_path = gdata.base; @@ -405,7 +406,8 @@ void Game::raiseEvent(Event event) return; } -Drawable* Game::getGraphics(std::string name) +Drawable* Game::getGraphics(std::string name, + std::map params = std::map()) { int cur; for (cur = 0; cur < data.media.size(); cur++) @@ -428,8 +430,8 @@ Drawable* Game::getGraphics(std::string name) { Graphics ngraphics = the_plugin->getPayload()->get(); - Drawable* result = ngraphics.makeImage(media_loader, - cur_media.contents); + Drawable* result = ngraphics.makeImage( + media_loader, cur_media.contents, params); if (result) { @@ -625,4 +627,9 @@ GameData Game::parseXML(std::string path, MediaLoader& media_loader) return results; } +InputManager* Game::getInputManager() +{ + return input_manager; +} + Game* Game::current_game = 0; diff --git a/src/core/Level.cpp b/src/core/Level.cpp index 186967c..7179a2b 100644 --- a/src/core/Level.cpp +++ b/src/core/Level.cpp @@ -154,7 +154,9 @@ void Level::init(std::vector nactor_defs, for (cur = 0; cur < level_graphics.size(); cur++) { - Drawable* drawable = game.getGraphics(level_graphics.at(cur).source); + std::map params; + Drawable* drawable = game.getGraphics( + level_graphics.at(cur).source, params); StaticGraphic graphic; graphic.position = level_graphics.at(cur).position; diff --git a/src/core/MediaLoader.cpp b/src/core/MediaLoader.cpp index 4c0de1e..db6c406 100644 --- a/src/core/MediaLoader.cpp +++ b/src/core/MediaLoader.cpp @@ -87,3 +87,14 @@ std::map& MediaLoader::plugins() { return plugin_map; } + +Game* MediaLoader::getGame() +{ + return game; +} + +void MediaLoader::setGame(Game* ngame) +{ + game = ngame; + return; +} diff --git a/src/core/parseactor.cpp b/src/core/parseactor.cpp index ab91272..9b33d86 100644 --- a/src/core/parseactor.cpp +++ b/src/core/parseactor.cpp @@ -90,6 +90,7 @@ XMLParser::parseactor::graphics XMLParser::parseactor::parsegraphics(xmlpp::Node* node) { XMLParser::parseactor::graphics results; + results.m_angle = 0.0; xmlpp::Node::NodeList list = node->get_children(); @@ -115,6 +116,11 @@ XMLParser::parseactor::parsegraphics(xmlpp::Node* node) results.m_angle = temp; } + if ((*iter)->get_name() == "param") + { + param temp = parseparam(*iter); + results.m_params.push_back(temp); + } } return results; @@ -519,6 +525,31 @@ actor parseactorFromBuffer(std::string buffer) } }} +XMLParser::parseactor::param +XMLParser::parseactor::parseparam(xmlpp::Node* node) +{ + XMLParser::parseactor::param results; + + xmlpp::Node::NodeList list = node->get_children(); + + xmlpp::Node::NodeList::iterator iter; + for (iter = list.begin(); iter != list.end(); iter++) + { + xmlpp::Node* cur_node = *iter; + + if (cur_node->get_name() == "name") + results.name = getContents(cur_node); + + if (cur_node->get_name() == "type") + results.type = getContents(cur_node); + + if (cur_node->get_name() == "value") + results.value = getContents(cur_node); + } + + return results; +} + float XMLParser::parseactor::parseelasticity(xmlpp::Node* node) { float results; -- 2.11.4.GIT