From 5205d99629a9b15865e7a1950b49d8da40a91309 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 3 Jul 2011 15:33:54 +0200 Subject: Load and save terrain objects, added ModelManager for object models. --- SConstruct | 2 +- common | 2 +- game.cpp | 22 ++++++++++++++++++++-- game.h | 1 + main.cpp | 3 +++ model.cpp | 23 +++++++++++++++++++++++ model.h | 15 +++++++++++++++ scene.cpp | 13 ++++++++----- scene.h | 5 ----- terrain.cpp | 6 ++++++ terrain.h | 2 +- 11 files changed, 79 insertions(+), 15 deletions(-) diff --git a/SConstruct b/SConstruct index eabcf02..7fb161e 100644 --- a/SConstruct +++ b/SConstruct @@ -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 index 6e74671..e95b3cb 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 6e746716d6a5c72fbd42539c6d5d92da8830cb9e +Subproject commit e95b3cb3e1a054a9d6bd766d4904e569ac2b2a68 diff --git a/game.cpp b/game.cpp index 1288b0c..819b35c 100644 --- a/game.cpp +++ b/game.cpp @@ -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; + } +} diff --git a/game.h b/game.h index 76c9413..f32f69d 100644 --- a/game.h +++ b/game.h @@ -33,6 +33,7 @@ class Game { void handle_object(); static Game& get_instance(); + static void free(); }; #endif diff --git a/main.cpp b/main.cpp index f30f67a..7643acf 100644 --- a/main.cpp +++ b/main.cpp @@ -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; diff --git a/model.cpp b/model.cpp index 67e4561..0087c05 100644 --- a/model.cpp +++ b/model.cpp @@ -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(name, model)); +} + +Model::p ModelManager::get_model(const std::string name) { + std::map::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 diff --git a/model.h b/model.h index 7659d9c..dda3228 100644 --- a/model.h +++ b/model.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -28,6 +29,8 @@ class Mesh { class Model { public: + typedef boost::shared_ptr 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 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 diff --git a/scene.cpp b/scene.cpp index 4e884a6..41b5ce8 100644 --- a/scene.cpp +++ b/scene.cpp @@ -11,6 +11,8 @@ #include +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(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); diff --git a/scene.h b/scene.h index 6c75c91..a029d91 100644 --- a/scene.h +++ b/scene.h @@ -56,11 +56,6 @@ class Scene { GLuint grass_texture, rock_texture, soil_texture, water_texture, marker_texture, placeholder_texture; std::map 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::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(); diff --git a/terrain.h b/terrain.h index aff95a4..883685b 100644 --- a/terrain.h +++ b/terrain.h @@ -30,7 +30,7 @@ class Terrain { }; struct Chunk { - typedef std::pair ObjectPair; + typedef std::pair ObjectPair; typedef std::list ObjectList; Terrain *terrain; -- cgit v1.2.3