stratgame
changeset 2:369b51c9e4a8
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 23 May 2012 07:25:43 +0300 |
parents | 55a43e27339a |
children | 8d95187cb3ee |
files | Makefile common/Makefile common/src/datapath.cc common/src/datapath.h level/Makefile level/src/level.cc level/src/level.h src/game_part.cc src/game_part.h src/main.cc src/part.cc src/part.h |
diffstat | 12 files changed, 329 insertions(+), 9 deletions(-) [+] |
line diff
1.1 --- a/Makefile Tue May 22 05:02:00 2012 +0300 1.2 +++ b/Makefile Wed May 23 07:25:43 2012 +0300 1.3 @@ -4,14 +4,15 @@ 1.4 dep = $(obj:.o=.d) 1.5 bin = strat 1.6 1.7 -incdir = -Isrc -Ilevel/src 1.8 +incdir = -Isrc -Ilevel/src -Icommon/src 1.9 1.10 liblevel = level/liblevel.a 1.11 -slibs = $(liblevel) 1.12 +libcommon = common/libcommon.a 1.13 +slibs = $(liblevel) $(libcommon) 1.14 1.15 CFLAGS = -pedantic -Wall -g $(incdir) 1.16 CXXFLAGS = -std=c++0x $(CFLAGS) 1.17 -LDFLAGS = $(libdir) $(slibs) $(libgl) -limtk 1.18 +LDFLAGS = $(libdir) $(slibs) $(libgl) -limtk -limago -lvmath 1.19 1.20 ifeq ($(shell uname -s), Darwin) 1.21 CC = clang
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/common/Makefile Wed May 23 07:25:43 2012 +0300 2.3 @@ -0,0 +1,23 @@ 2.4 +ccsrc = $(wildcard src/*.cc) 2.5 +obj = $(ccsrc:.cc=.o) 2.6 +dep = $(obj:.o=.d) 2.7 +lib_a = libcommon.a 2.8 + 2.9 +CXXFLAGS = -std=c++0x -pedantic -Wall -g 2.10 + 2.11 +ifeq ($(shell uname -s), Darwin) 2.12 + CC = clang 2.13 + CXX = clang++ 2.14 +endif 2.15 + 2.16 +$(lib_a): $(obj) 2.17 + $(AR) rcs $@ $(obj) 2.18 + 2.19 +-include $(dep) 2.20 + 2.21 +%.d: %.cc 2.22 + @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@ 2.23 + 2.24 +.PHONY: clean 2.25 +clean: 2.26 + rm -f $(obj) $(bin) $(dep)
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/common/src/datapath.cc Wed May 23 07:25:43 2012 +0300 3.3 @@ -0,0 +1,81 @@ 3.4 +#include <string.h> 3.5 +#include <string> 3.6 +#include <vector> 3.7 +#include <stack> 3.8 +#ifdef _MSC_VER 3.9 +#include <malloc.h> 3.10 +#else 3.11 +#include <alloca.h> 3.12 +#endif 3.13 +#include "datapath.h" 3.14 + 3.15 +using namespace std; 3.16 + 3.17 +static vector<string> first; 3.18 +static stack<vector<string>> dpath_stack; 3.19 + 3.20 +static void init() 3.21 +{ 3.22 + if(dpath_stack.empty()) { 3.23 + dpath_stack.push(first); 3.24 + } 3.25 +} 3.26 + 3.27 +void reset_data_path() 3.28 +{ 3.29 + init(); 3.30 + 3.31 + dpath_stack.top().clear(); 3.32 +} 3.33 + 3.34 +void push_data_path() 3.35 +{ 3.36 + init(); 3.37 + 3.38 + dpath_stack.push(dpath_stack.top()); 3.39 +} 3.40 + 3.41 +void pop_data_path() 3.42 +{ 3.43 + init(); 3.44 + 3.45 + dpath_stack.pop(); 3.46 +} 3.47 + 3.48 +void add_data_path(const char *path) 3.49 +{ 3.50 + init(); 3.51 + 3.52 + char *tmp = (char*)alloca(strlen(path) + 1); 3.53 + strcpy(tmp, path); 3.54 + 3.55 + char *endp = strrchr(tmp, '/'); 3.56 + if(endp) { 3.57 + *endp = 0; 3.58 + } 3.59 + 3.60 + dpath_stack.top().push_back(tmp); 3.61 +} 3.62 + 3.63 +bool find_file(const char *fname, char *path) 3.64 +{ 3.65 + FILE *fp; 3.66 + 3.67 + init(); 3.68 + 3.69 + for(size_t i=0; i<dpath_stack.top().size(); i++) { 3.70 + sprintf(path, "%s/%s", dpath_stack.top()[i].c_str(), fname); 3.71 + 3.72 + if((fp = fopen(path, "r"))) { 3.73 + fclose(fp); 3.74 + return true; 3.75 + } 3.76 + } 3.77 + 3.78 + if((fp = fopen(fname, "r"))) { 3.79 + fclose(fp); 3.80 + strcpy(path, fname); 3.81 + return true; 3.82 + } 3.83 + return false; 3.84 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/common/src/datapath.h Wed May 23 07:25:43 2012 +0300 4.3 @@ -0,0 +1,12 @@ 4.4 +#ifndef DATAPATH_H_ 4.5 +#define DATAPATH_H_ 4.6 + 4.7 +void reset_data_path(); 4.8 +void push_data_path(); 4.9 +void pop_data_path(); 4.10 + 4.11 +void add_data_path(const char *path); 4.12 + 4.13 +bool find_file(const char *fname, char *path); 4.14 + 4.15 +#endif // DATAPATH_H_
5.1 --- a/level/Makefile Tue May 22 05:02:00 2012 +0300 5.2 +++ b/level/Makefile Wed May 23 07:25:43 2012 +0300 5.3 @@ -3,7 +3,7 @@ 5.4 dep = $(obj:.o=.d) 5.5 lib_a = liblevel.a 5.6 5.7 -CXXFLAGS = -std=c++0x -pedantic -Wall -g 5.8 +CXXFLAGS = -std=c++0x -pedantic -Wall -g -I../common/src 5.9 5.10 ifeq ($(shell uname -s), Darwin) 5.11 CC = clang
6.1 --- a/level/src/level.cc Tue May 22 05:02:00 2012 +0300 6.2 +++ b/level/src/level.cc Wed May 23 07:25:43 2012 +0300 6.3 @@ -1,6 +1,9 @@ 6.4 #include <stdio.h> 6.5 #include <string.h> 6.6 #include "level.h" 6.7 +#include "datapath.h" 6.8 + 6.9 +using namespace tinyxml2; 6.10 6.11 Level::Level() 6.12 { 6.13 @@ -12,14 +15,162 @@ 6.14 6.15 bool Level::load(const char *fname) 6.16 { 6.17 - if(!xml.LoadFile(fname)) { 6.18 + push_data_path(); 6.19 + add_data_path(fname); 6.20 + 6.21 + if(xml.LoadFile(fname) != 0) { 6.22 fprintf(stderr, "failed to load level: %s\n", fname); 6.23 + pop_data_path(); 6.24 return false; 6.25 } 6.26 6.27 if(strcmp(xml.RootElement()->Name(), "level") != 0) { 6.28 fprintf(stderr, "invalid level file: %s\n", fname); 6.29 + pop_data_path(); 6.30 return false; 6.31 } 6.32 + 6.33 + XMLNode *node = xml.RootElement()->FirstChild(); 6.34 + do { 6.35 + XMLElement *elem = node->ToElement(); 6.36 + if(elem) { 6.37 + if(strcmp(elem->Name(), "map") == 0) { 6.38 + LevelMap map; 6.39 + 6.40 + if(!map.load(elem)) { 6.41 + pop_data_path(); 6.42 + return false; 6.43 + } 6.44 + levelmaps[map.get_name()] = std::move(map); 6.45 + 6.46 + } else { 6.47 + fprintf(stderr, "ignoring unrecognized element: %s\n", elem->Name()); 6.48 + } 6.49 + } 6.50 + } while((node = node->NextSibling())); 6.51 + 6.52 + pop_data_path(); 6.53 + return true; 6.54 +} 6.55 + 6.56 + 6.57 +LevelMap::LevelMap() 6.58 +{ 6.59 + init(); 6.60 +} 6.61 + 6.62 +void LevelMap::init() 6.63 +{ 6.64 + scale = 1.0f; 6.65 + img_init(&img); 6.66 + name = 0; 6.67 +} 6.68 + 6.69 +LevelMap::~LevelMap() 6.70 +{ 6.71 + destroy(); 6.72 +} 6.73 + 6.74 +void LevelMap::destroy() 6.75 +{ 6.76 + if(img.pixels) { 6.77 + img_destroy(&img); 6.78 + } 6.79 + delete [] name; 6.80 +} 6.81 + 6.82 +// copy 6.83 +LevelMap::LevelMap(const LevelMap &map) 6.84 +{ 6.85 + init(); 6.86 + *this = map; 6.87 +} 6.88 + 6.89 +LevelMap &LevelMap::operator =(const LevelMap &map) 6.90 +{ 6.91 + if(this != &map) { 6.92 + destroy(); 6.93 + 6.94 + scale = map.scale; 6.95 + img_init(&img); 6.96 + img_copy(&img, (img_pixmap*)&map.img); 6.97 + 6.98 + name = new char[strlen(map.name) + 1]; 6.99 + strcpy(name, map.name); 6.100 + } 6.101 + return *this; 6.102 +} 6.103 + 6.104 +// move semantics 6.105 +LevelMap::LevelMap(LevelMap &&map) 6.106 +{ 6.107 + init(); 6.108 + *this = std::move(map); 6.109 +} 6.110 + 6.111 +LevelMap &LevelMap::operator =(LevelMap &&map) 6.112 +{ 6.113 + if(this != &map) { 6.114 + destroy(); 6.115 + 6.116 + scale = map.scale; 6.117 + 6.118 + img = map.img; 6.119 + map.img.pixels = 0; 6.120 + map.img.name = 0; 6.121 + 6.122 + name = map.name; 6.123 + map.name = 0; 6.124 + } 6.125 + 6.126 + return *this; 6.127 +} 6.128 + 6.129 +bool LevelMap::load(XMLElement *xelem) 6.130 +{ 6.131 + char fname[PATH_MAX]; 6.132 + 6.133 + const char *attr = xelem->Attribute("type"); 6.134 + if(!attr) { 6.135 + fprintf(stderr, "map element without a type attribute\n"); 6.136 + return false; 6.137 + } 6.138 + name = new char[strlen(attr) + 1]; 6.139 + strcpy(name, attr); 6.140 + 6.141 + attr = xelem->Attribute("file"); 6.142 + if(!attr) { 6.143 + fprintf(stderr, "map element without a file attribute\n"); 6.144 + return false; 6.145 + } 6.146 + if(!find_file(attr, fname)) { 6.147 + fprintf(stderr, "failed to locate image: %s\n", attr); 6.148 + return false; 6.149 + } 6.150 + 6.151 + if(img_load(&img, fname) == -1) { 6.152 + fprintf(stderr, "failed to load image: %s\n", fname); 6.153 + return false; 6.154 + } 6.155 + 6.156 + float val; 6.157 + if(xelem->QueryFloatAttribute("scale", &val) == 0) { 6.158 + scale = val; 6.159 + } 6.160 return true; 6.161 } 6.162 + 6.163 +float LevelMap::get_scale() const 6.164 +{ 6.165 + return scale; 6.166 +} 6.167 + 6.168 +const img_pixmap *LevelMap::get_pixmap() const 6.169 +{ 6.170 + return &img; 6.171 +} 6.172 + 6.173 +const char *LevelMap::get_name() const 6.174 +{ 6.175 + return name; 6.176 +}
7.1 --- a/level/src/level.h Tue May 22 05:02:00 2012 +0300 7.2 +++ b/level/src/level.h Wed May 23 07:25:43 2012 +0300 7.3 @@ -1,11 +1,17 @@ 7.4 #ifndef LEVEL_H_ 7.5 #define LEVEL_H_ 7.6 7.7 +#include <string> 7.8 +#include <map> 7.9 +#include <imago2.h> 7.10 #include "tinyxml2.h" 7.11 7.12 +class LevelMap; 7.13 + 7.14 class Level { 7.15 private: 7.16 tinyxml2::XMLDocument xml; 7.17 + std::map<std::string, LevelMap> levelmaps; 7.18 7.19 public: 7.20 Level(); 7.21 @@ -14,9 +20,31 @@ 7.22 bool load(const char *fname); 7.23 }; 7.24 7.25 -class Map { 7.26 +class LevelMap { 7.27 +private: 7.28 + float scale; 7.29 + img_pixmap img; 7.30 + char *name; 7.31 + 7.32 + void init(); 7.33 + void destroy(); 7.34 + 7.35 public: 7.36 - bool load(tinyxml2::XMLNode *xml); 7.37 + LevelMap(); 7.38 + ~LevelMap(); 7.39 + 7.40 + LevelMap(const LevelMap &map); 7.41 + LevelMap &operator =(const LevelMap &map); 7.42 + 7.43 + // move constructor/op= 7.44 + LevelMap(LevelMap &&map); 7.45 + LevelMap &operator =(LevelMap &&map); 7.46 + 7.47 + bool load(tinyxml2::XMLElement *xelem); 7.48 + 7.49 + float get_scale() const; 7.50 + const img_pixmap *get_pixmap() const; 7.51 + const char *get_name() const; 7.52 }; 7.53 7.54 #endif // LEVEL_H_
8.1 --- a/src/game_part.cc Tue May 22 05:02:00 2012 +0300 8.2 +++ b/src/game_part.cc Wed May 23 07:25:43 2012 +0300 8.3 @@ -4,6 +4,14 @@ 8.4 8.5 Game::~Game() {} 8.6 8.7 +bool Game::init() 8.8 +{ 8.9 + if(!level.load("data/test.level")) { 8.10 + return false; 8.11 + } 8.12 + return true; 8.13 +} 8.14 + 8.15 void Game::draw() const 8.16 { 8.17 glClear(GL_COLOR_BUFFER_BIT);
9.1 --- a/src/game_part.h Tue May 22 05:02:00 2012 +0300 9.2 +++ b/src/game_part.h Wed May 23 07:25:43 2012 +0300 9.3 @@ -6,10 +6,12 @@ 9.4 9.5 class Game : public Part { 9.6 private: 9.7 - Level *level; 9.8 + Level level; 9.9 public: 9.10 ~Game(); 9.11 9.12 + bool init(); 9.13 + 9.14 void draw() const; 9.15 void key(int key, bool pressed); 9.16 };
10.1 --- a/src/main.cc Tue May 22 05:02:00 2012 +0300 10.2 +++ b/src/main.cc Wed May 23 07:25:43 2012 +0300 10.3 @@ -53,9 +53,16 @@ 10.4 glewInit(); 10.5 10.6 menu_part = new MainMenu; 10.7 + if(!menu_part->init()) { 10.8 + return false; 10.9 + } 10.10 + 10.11 game_part = new Game; 10.12 + if(!game_part->init()) { 10.13 + return false; 10.14 + } 10.15 + 10.16 cur_part = menu_part; 10.17 - 10.18 return true; 10.19 } 10.20
11.1 --- a/src/part.cc Tue May 22 05:02:00 2012 +0300 11.2 +++ b/src/part.cc Wed May 23 07:25:43 2012 +0300 11.3 @@ -8,6 +8,11 @@ 11.4 current_time = 0; 11.5 } 11.6 11.7 +bool Part::init() 11.8 +{ 11.9 + return true; 11.10 +} 11.11 + 11.12 void Part::update(unsigned long msec) 11.13 { 11.14 current_time = msec;