diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-07-03 15:33:54 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-07-03 15:33:54 +0200 |
commit | 5205d99629a9b15865e7a1950b49d8da40a91309 (patch) | |
tree | 91b3deb782dac4432d48935b0566805123951d92 | |
parent | 75a90df8bf7f38e746e021c23248e1607931132c (diff) |
-rw-r--r-- | SConstruct | 2 | ||||
m--------- | common | 0 | ||||
-rw-r--r-- | game.cpp | 22 | ||||
-rw-r--r-- | game.h | 1 | ||||
-rw-r--r-- | main.cpp | 3 | ||||
-rw-r--r-- | model.cpp | 23 | ||||
-rw-r--r-- | model.h | 15 | ||||
-rw-r--r-- | scene.cpp | 13 | ||||
-rw-r--r-- | scene.h | 5 | ||||
-rw-r--r-- | terrain.cpp | 6 | ||||
-rw-r--r-- | terrain.h | 2 |
11 files changed, 78 insertions, 14 deletions
@@ -15,7 +15,7 @@ if GetOption('mingw32'): env.Append(CPPPATH = ['/usr/i486-mingw32/include/' + x for x in ('SDL', 'CEGUI', 'freetype2')]) env.Append(LIBS = ['CEGUIOpenGLRenderer', 'CEGUIBase', 'CEGUITGAImageCodec', 'CEGUITinyXMLParser', 'CEGUIFalagardWRBase', 'CEGUISILLYImageCodec', 'SDL_image', 'ftgl', 'freetype-s', 'z', 'pcre', 'opengl32', 'glu32', 'winmm', 'wsock32', 'ws2_32', 'mingw32', 'SDLmain', 'SDL', 'jpeg', 'noise', - 'lua', 'tolua++_Static', 'boost_filesystem-mt-s', 'boost_system-mt-s']) + 'lua', 'tolua++_Static', 'boost_filesystem-mt-s', 'boost_system-mt-s', 'assimp']) env.Append(CPPFLAGS = ['-D_WIN32_WINNT=0x0501']) env.Append(LINKFLAGS = ['-static-libgcc', '-static-libstdc++']) env['CXX'] = 'i486-mingw32-g++' diff --git a/common b/common -Subproject 6e746716d6a5c72fbd42539c6d5d92da8830cb9 +Subproject e95b3cb3e1a054a9d6bd766d4904e569ac2b2a6 @@ -171,9 +171,20 @@ void Game::handle_object() { std::cerr << "got object for non-existing chunk, discarding" << std::endl; return; } - pos.y = chunk->find(pos.x, pos.z)->get_height(pos.x, pos.z); - chunk->objects.push_back(Terrain::Chunk::ObjectPair(scene->tree, pos)); + + int64_t x = pos.x; + x -= x % Terrain::chunk_size; + + int64_t z = pos.z; + z -= z % Terrain::chunk_size; + + TerrainCacheObject::p ob = scene->terrain->tc->get_chunk(x, z, Terrain::chunk_size_total, Terrain::chunk_size_total); + ob->objects.push_back(pos); + + models::ModelManager& model_mgr = models::ModelManager::get_instance(); + + chunk->objects.push_back(Terrain::Chunk::ObjectPair(model_mgr.get_model("tree"), pos)); } Game& Game::get_instance() { @@ -181,3 +192,10 @@ Game& Game::get_instance() { game = new Game(); return *game; } + +void Game::free() { + if(game) { + delete game; + game = NULL; + } +} @@ -33,6 +33,7 @@ class Game { void handle_object(); static Game& get_instance(); + static void free(); }; #endif @@ -39,6 +39,9 @@ int main(int argc, char **argv) { game.run(argv[1], argv[2]); + // free singleton to run destructors + game.free(); + video::free(); return 0; @@ -101,4 +101,27 @@ void Tree::render() { leaves->render(); } +/* ModelManager */ + +ModelManager *ModelManager::model_mgr = NULL; + +void ModelManager::add_model(const std::string name, Model::p model) { + models.insert(std::pair<const std::string, Model::p>(name, model)); +} + +Model::p ModelManager::get_model(const std::string name) { + std::map<const std::string, Model::p>::iterator it = models.find(name); + if(it != models.end()) + return it->second; + + return Model::p(); +} + +ModelManager& ModelManager::get_instance() { + if(!model_mgr) + model_mgr = new ModelManager(); + + return *model_mgr; +} + } // namespace models @@ -5,6 +5,7 @@ #include <assimp/assimp.hpp> #include <assimp/aiScene.h> +#include <boost/shared_ptr.hpp> #include <map> #include <string> @@ -28,6 +29,8 @@ class Mesh { class Model { public: + typedef boost::shared_ptr<Model> p; + virtual ~Model() {}; virtual void render() = 0; @@ -45,6 +48,18 @@ class Tree : public Model { virtual void render(); }; +class ModelManager { + private: + static ModelManager *model_mgr; + std::map<const std::string, Model::p> models; + + public: + void add_model(const std::string name, Model::p model); + Model::p get_model(const std::string name); + + static ModelManager& get_instance(); +}; + } #endif @@ -11,6 +11,8 @@ #include <cmath> +using models::ModelManager; + Scene::Scene() { running = true; grid = false; @@ -70,7 +72,8 @@ Scene::Scene() { marker_texture = load_texture("textures/cross.png"); placeholder_texture = load_texture("textures/placeholder.png"); - tree_scene = ai_importer.ReadFile("models/trees.blend", aiProcess_Triangulate); + Assimp::Importer ai_importer; + const aiScene *tree_scene = ai_importer.ReadFile("models/trees.blend", aiProcess_Triangulate); for(unsigned int i = 0; i < tree_scene->mNumTextures; i++) { aiTexture *texture = tree_scene->mTextures[i]; @@ -79,7 +82,8 @@ Scene::Scene() { scene_textures.insert(std::pair<std::string, GLuint>(name, tx)); } - tree = new models::Tree(tree_scene, scene_textures); + Model::p tree = Model::p(new models::Tree(tree_scene, scene_textures)); + ModelManager::get_instance().add_model("tree", tree); /* init terrain */ terrain = new Terrain(); @@ -105,7 +109,6 @@ Scene::~Scene() { delete lua; if(tool) delete tool; - delete tree; if(terrain) delete terrain; delete font; @@ -621,8 +624,8 @@ void Scene::render() { float height = font->LineHeight(); glColor3f(1, 1, 1); glTranslatef(0, video::height-height, 0); - font->Render((boost::format("chunks: %d cache: %d gravity: %d flying: %d water: %d steps: %d") - % terrain->chunks.size() % terrain->tc->get_size() % gravity % flying % underwater % steps).str().c_str()); + font->Render((boost::format("chunks: %d cache: %d gravity: %d flying: %d water: %d steps: %d trees: %d") + % terrain->chunks.size() % terrain->tc->get_size() % gravity % flying % underwater % steps % trees.size()).str().c_str()); glTranslatef(0, -height, 0); font->Render(move_str.c_str()); glTranslatef(0, -height, 0); @@ -56,11 +56,6 @@ class Scene { GLuint grass_texture, rock_texture, soil_texture, water_texture, marker_texture, placeholder_texture; std::map<std::string, GLuint> scene_textures; - Assimp::Importer ai_importer; - const aiScene *tree_scene; - - Model *tree; - Scene(); ~Scene(); diff --git a/terrain.cpp b/terrain.cpp index e32a52b..f3dcb33 100644 --- a/terrain.cpp +++ b/terrain.cpp @@ -133,6 +133,12 @@ Terrain::Chunk::Chunk(Terrain *terrain, float x, float y) : cache_obj(terrain->t this->vbo_object = this->node_count = this->vertices = 0; this->nodes = NULL; heights = cache_obj->heights; + + for(std::list<Vector3>::iterator it = cache_obj->objects.begin(); it != cache_obj->objects.end(); it++) { + models::Model::p tree = models::ModelManager::get_instance().get_model("tree"); + objects.push_back(ObjectPair(tree, *it)); + } + normals = new Vector3[(int)((n_width)*(n_height))]; calc_normals(); @@ -30,7 +30,7 @@ class Terrain { }; struct Chunk { - typedef std::pair<models::Model*, Vector3> ObjectPair; + typedef std::pair<models::Model::p, Vector3> ObjectPair; typedef std::list<ObjectPair> ObjectList; Terrain *terrain; |