the game context and in the render context, and a ticker class that takes care of the ticking.legacy
| @@ -5,7 +5,8 @@ noinst_LIBRARIES = libcommon.a | |||||
| libcommon_a_SOURCES = \ | libcommon_a_SOURCES = \ | ||||
| game.cpp tiler.cpp tileset.cpp scene.cpp \ | game.cpp tiler.cpp tileset.cpp scene.cpp \ | ||||
| font.cpp layer.cpp map.cpp joystick.cpp | |||||
| font.cpp layer.cpp map.cpp joystick.cpp \ | |||||
| asset.h asset.cpp ticker.h ticker.cpp | |||||
| libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` | libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` | ||||
| test_map_SOURCES = test-map.cpp sdlvideo.cpp | test_map_SOURCES = test-map.cpp sdlvideo.cpp | ||||
| @@ -18,24 +19,3 @@ editor_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image gtk+-2.0 gtkgl-2.0` | |||||
| editor_LDADD = libcommon.a | editor_LDADD = libcommon.a | ||||
| editor_LDFLAGS = `pkg-config --libs sdl gl gtk+-2.0 gtkgl-2.0 SDL_image` | editor_LDFLAGS = `pkg-config --libs sdl gl gtk+-2.0 gtkgl-2.0 SDL_image` | ||||
| #COMMON = $(libcommon_a_SOURCES) | |||||
| # | |||||
| #TEST_SRC = test-map.cpp sdlvideo.cpp $(COMMON) | |||||
| #EDITOR_SRC = editor.cpp gtkvideo.cpp $(COMMON) | |||||
| # | |||||
| #CPPFLAGS = `pkg-config --cflags sdl gl SDL_image gtk+-2.0 gtkgl-2.0` | |||||
| #CXXFLAGS = -g -O3 -Wall \ | |||||
| # -Wpointer-arith -Wcast-align -Wcast-qual -Wshadow -Wsign-compare | |||||
| # | |||||
| #test-map: $(TEST_SRC:%.cpp=%.o) | |||||
| # g++ $(CXXFLAGS) $^ -o $@ `pkg-config --libs sdl gl SDL_image` | |||||
| # | |||||
| #editor: $(EDITOR_SRC:%.cpp=%.o) | |||||
| # g++ $(CXXFLAGS) $^ -o $@ `pkg-config --libs sdl gl gtk+-2.0 gtkgl-2.0 SDL_image` | |||||
| # | |||||
| #%.o: %.cpp | |||||
| # g++ -c $^ -o $@ $(CXXFLAGS) $(CPPFLAGS) | |||||
| # | |||||
| #clean: | |||||
| # rm -f *.o test-map editor | |||||
| @@ -0,0 +1,42 @@ | |||||
| #include <malloc.h> | |||||
| #include "asset.h" | |||||
| #include "ticker.h" | |||||
| /* | |||||
| * Public Asset class | |||||
| */ | |||||
| Asset::Asset() : | |||||
| index(0), | |||||
| ref(0), | |||||
| destroy(0) | |||||
| { | |||||
| Ticker::Register(this); | |||||
| } | |||||
| Asset::~Asset() | |||||
| { | |||||
| } | |||||
| void Asset::TickGame(float delta_time) | |||||
| { | |||||
| } | |||||
| void Asset::TickRender(float delta_time) | |||||
| { | |||||
| } | |||||
| void Asset::Ref() | |||||
| { | |||||
| ref++; | |||||
| } | |||||
| int Asset::Unref() | |||||
| { | |||||
| return --ref; | |||||
| } | |||||
| @@ -0,0 +1,30 @@ | |||||
| /* | |||||
| * The asset object | |||||
| */ | |||||
| #if !defined __DH_ASSET_H__ | |||||
| #define __DH_ASSET_H__ | |||||
| #include <stdint.h> | |||||
| class Asset | |||||
| { | |||||
| friend class Ticker; | |||||
| public: | |||||
| Asset(); | |||||
| virtual ~Asset(); | |||||
| virtual void TickGame(float delta_time); | |||||
| virtual void TickRender(float delta_time); | |||||
| virtual void Ref(); | |||||
| virtual int Unref(); | |||||
| private: | |||||
| int index, ref, destroy; | |||||
| }; | |||||
| #endif // __DH_ASSET_H__ | |||||
| @@ -0,0 +1,66 @@ | |||||
| /* | |||||
| * The ticker | |||||
| */ | |||||
| #include <stdint.h> | |||||
| #include <stdlib.h> | |||||
| #include "ticker.h" | |||||
| #include "asset.h" | |||||
| /* | |||||
| * Ticker implementation class | |||||
| */ | |||||
| static class TickerData | |||||
| { | |||||
| friend class Ticker; | |||||
| public: | |||||
| TickerData() : | |||||
| assets(0), | |||||
| nassets(0) | |||||
| { | |||||
| /* Nothing to do */ | |||||
| } | |||||
| ~TickerData() | |||||
| { | |||||
| free(assets); | |||||
| } | |||||
| private: | |||||
| Asset **assets; | |||||
| int nassets; | |||||
| } | |||||
| tickerdata; | |||||
| static TickerData * const data = &tickerdata; | |||||
| /* | |||||
| * Ticker public class | |||||
| */ | |||||
| void Ticker::Register(Asset *asset) | |||||
| { | |||||
| int tmp = data->nassets++; | |||||
| data->assets = (Asset **)realloc(data->assets, | |||||
| data->nassets * sizeof(Asset *)); | |||||
| data->assets[tmp] = asset; | |||||
| asset->index = tmp; | |||||
| } | |||||
| void Ticker::TickGame(float delta_time) | |||||
| { | |||||
| for (int i = 0; i < data->nassets; i++) | |||||
| data->assets[i]->TickGame(delta_time); | |||||
| } | |||||
| void Ticker::TickRender(float delta_time) | |||||
| { | |||||
| for (int i = 0; i < data->nassets; i++) | |||||
| data->assets[i]->TickRender(delta_time); | |||||
| } | |||||
| @@ -0,0 +1,23 @@ | |||||
| /* | |||||
| * The tick manager | |||||
| */ | |||||
| #if !defined __DH_TICKER_H__ | |||||
| #define __DH_TICKER_H__ | |||||
| #include <stdint.h> | |||||
| #include "asset.h" | |||||
| class Ticker | |||||
| { | |||||
| public: | |||||
| static void Register(Asset *asset); | |||||
| static void TickGame(float delta_time); | |||||
| static void TickRender(float delta_time); | |||||
| }; | |||||
| #endif // __DH_TICKER_H__ | |||||