From 0be5a7fb0af12fe7e4e160f8909d817384f7ec0d Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 22 Sep 2024 14:46:29 +0300 Subject: [PATCH] moved old minimap image data refactored map data loading --- activeGameState.cpp | 9 ----- activeGameState.h | 2 - map.cpp | 87 ++++++++++++++++++++++++++----------------- map.h | 9 ++++- mapEditorAppState.cpp | 22 ++++++----- mapEditorAppState.h | 4 +- minimapButton.cpp | 5 ++- 7 files changed, 76 insertions(+), 62 deletions(-) diff --git a/activeGameState.cpp b/activeGameState.cpp index 054de0d..08d5c74 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -45,15 +45,6 @@ namespace battleship{ initDragbox(); mainPlayer = Game::getSingleton()->getPlayer(playerId); - - string minimapPath = GameManager::getSingleton()->getPath() + "Models/Maps/" + Map::getSingleton()->getMapName() + "/minimap.jpg"; - ImageAsset *asset = (ImageAsset*)AssetManager::getSingleton()->getAsset(minimapPath); - int imgSize = asset->width * asset->height * asset->numChannels; - - oldImageData = new u8[imgSize]; - - for(int i = 0; i < imgSize; i++) - oldImageData[i] = asset->image[i]; } ActiveGameState::~ActiveGameState() { diff --git a/activeGameState.h b/activeGameState.h index 0f94ed5..b6dd726 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -34,7 +34,6 @@ namespace battleship{ inline std::vector getButtons(){return buttons;} inline Player* getPlayer(){return mainPlayer;} inline std::vector& getUnitGroup(int i){return unitGroups[i];} - inline u8 *getOldMinimapImage(){return oldImageData;} private: enum CursorState{ NORMAL, @@ -77,7 +76,6 @@ namespace battleship{ vb01::s64 lastLeftMouseClicked = 0; vb01::Node *cursorNode = nullptr; vb01::Texture *pointerTex = nullptr, *attackTex = nullptr, *garrisonTex = nullptr; - vb01::u8 *oldImageData = nullptr; }; } diff --git a/map.cpp b/map.cpp index b2042ad..dd14bb6 100755 --- a/map.cpp +++ b/map.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -239,7 +240,7 @@ namespace battleship{ } } - void Map::preprareScene(){ + void Map::preprareScene(bool empty){ Root *root = Root::getSingleton(); Node *rootNode = root->getRootNode(); string libPath = root->getLibPath(); @@ -263,6 +264,15 @@ namespace battleship{ cam->setPosition(Vector3(1, 1, 1) * configData::CAMERA_DISTANCE); cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); + if(empty){ + Light *light = new Light(Light::Type::AMBIENT); + light->setColor(Vector3::VEC_IJK * .9); + + Node *node = new Node(); + node->addLight(light); + Root::getSingleton()->getRootNode()->attachChild(node); + lights.push_back(node); + } } //TODO implement toggleable cell rendering @@ -303,49 +313,54 @@ namespace battleship{ } } - //TODO implement map size calculation when exporting maps - void Map::load(string mapName, bool empty) { + void Map::loadMinimap(){ + AssetManager *am = AssetManager::getSingleton(); + string minimapPath = GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/minimap.jpg"; + + am->load(minimapPath); + ImageAsset *asset = (ImageAsset*)am->getAsset(minimapPath); + int imgSize = asset->width * asset->height * asset->numChannels; + oldImageData = new u8[imgSize]; + + for(int i = 0; i < imgSize; i++) + oldImageData[i] = asset->image[i]; + } + + void Map::load(string mapName){ this->mapName = mapName; Pathfinder::getSingleton()->setImpassibleNodeVal(u16(0 - 1)); string path = GameManager::getSingleton()->getPath(); + sol::state_view SOL_LUA_STATE = generateView(); + SOL_LUA_STATE.script_file(path + "Models/Maps/" + mapName + "/" + mapName + ".lua"); + + preprareScene(false); + loadMinimap(); + loadSpawnPoints(); + loadSkybox(); + loadCells(); + loadTerrainObject(-1); + + sol::optional lightsOpt = SOL_LUA_STATE[mapTable]["lights"]; - preprareScene(); + if(lightsOpt != sol::nullopt) + loadLights(); - if(!empty){ - string mapDir = path + "Models/Maps/" + mapName + "/"; - AssetManager::getSingleton()->load(mapDir + "minimap.jpg"); - - SOL_LUA_STATE.script_file(mapDir + mapName + ".lua"); - sol::optional lightsOpt = SOL_LUA_STATE[mapTable]["lights"]; - - if(lightsOpt != sol::nullopt) - loadLights(); - - loadSpawnPoints(); - loadSkybox(); - loadCells(); - loadTerrainObject(-1); - - sol::table sizeTable = SOL_LUA_STATE[mapTable]["size"]; - mapSize = Vector3(sizeTable["x"], sizeTable["y"], sizeTable["z"]); - int numWaterbodies = SOL_LUA_STATE[mapTable]["numWaterBodies"]; - - for(int i = 0; i < numWaterbodies; i++) - loadTerrainObject(i); - } - else{ - Light *light = new Light(Light::Type::AMBIENT); - light->setColor(Vector3::VEC_IJK * .9); - - Node *node = new Node(); - node->addLight(light); - Root::getSingleton()->getRootNode()->attachChild(node); - lights.push_back(node); - } + sol::table sizeTable = SOL_LUA_STATE[mapTable]["size"]; + mapSize = Vector3(sizeTable["x"], sizeTable["y"], sizeTable["z"]); + int numWaterbodies = SOL_LUA_STATE[mapTable]["numWaterBodies"]; + + for(int i = 0; i < numWaterbodies; i++) + loadTerrainObject(i); } + void Map::create(string mapName){ + this->mapName = mapName; + addSpawnPoint(Vector3::VEC_ZERO); + preprareScene(true); + } + //TODO unload skybox assets void Map::unloadSkybox(){ Root::getSingleton()->removeSkybox(); @@ -404,6 +419,8 @@ namespace battleship{ } void Map::unload(){ + delete[] oldImageData; + unloadSkybox(); unloadLights(); unloadCells(); diff --git a/map.h b/map.h index bacf47c..6b4980c 100755 --- a/map.h +++ b/map.h @@ -45,7 +45,8 @@ namespace battleship{ ~Map(){} static std::vector generateAdjacentNodeEdges(int, int, int, int, int); void update(); - void load(std::string, bool = false); + void load(std::string); + void create(std::string); void unload(); std::vector raycastTerrain(vb01::Vector3, vb01::Vector3, bool); int getCellId(vb01::Vector3, bool = true); @@ -56,11 +57,13 @@ namespace battleship{ inline vb01::Vector3 getCellSize(){return CELL_SIZE;} inline int getNumSpawnPoints(){return spawnPoints.size();} inline vb01::Vector3 getSpawnPoint(int i){return spawnPoints[i];} + inline void setMapSize(vb01::Vector3 s){this->mapSize = s;} inline vb01::Vector3 getMapSize(){return mapSize;} inline void addSpawnPoint(vb01::Vector3 sp){spawnPoints.push_back(sp);} inline std::vector& getCells(){return cells;} inline vb01::Node* getLight(int i){return lights[i];} inline std::vector getLights(){return lights;} + inline vb01::u8* getOldMinimapImage(){return oldImageData;} private: std::string mapTable = "map"; vb01::Node *terrainNode = nullptr, *cellNode = nullptr; @@ -70,9 +73,11 @@ namespace battleship{ std::vector spawnPoints; std::vector cells; std::vector lights; + vb01::u8 *oldImageData = nullptr; Map(){} - void preprareScene(); + void loadMinimap(); + void preprareScene(bool); void loadSpawnPoints(); void loadLights(); void loadSkybox(); diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 5731ed6..6e88995 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -55,15 +55,17 @@ namespace battleship{ assetManager->load(basePath + DEFAULT_TEXTURE); map = Map::getSingleton(); - map->load(name, newMap); Game *game = Game::getSingleton(); if(newMap){ game->addPlayer(new Player(0, 0, 0, Vector3(1, 1, 1))); - map->addSpawnPoint(Vector3::VEC_ZERO); + map->setMapSize(Vector3(size.x, 0, size.y)); + map->create(name); generatePlane(size); } else{ + map->load(name); + int numPlayers = map->getNumSpawnPoints(); int numVerts = 3 * map->getNodeParent()->getChild(0)->getMesh(0)->getMeshBase().numTris; oldLandmassVertHeights = new float[numVerts]; @@ -395,7 +397,7 @@ namespace battleship{ return cells; } - void MapEditorAppState::MapEditor::generateMinimap(string mapFolder){ + void MapEditorAppState::MapEditor::generateMinimap(string mapFolder, vector &cells){ Vector3 mapSize = map->getMapSize(), cellSize = map->getCellSize(); int width = int(mapSize.x / cellSize.x); int height = int(mapSize.z / cellSize.z); @@ -406,7 +408,7 @@ namespace battleship{ u8 *imgData = new u8[size]; for(u8 *p = imgData; p != imgData + size; p+= numChannels, cellId++){ - Vector3 color = (map->getCells()[cellId].type == Map::Cell::Type::WATER ? Vector3::VEC_K : Vector3::VEC_J); + Vector3 color = (cells[cellId].type == Map::Cell::Type::WATER ? Vector3::VEC_K : Vector3::VEC_J); *p = color.x * 255.f; *(p + 1) = color.y * 255.f; *(p + 2) = color.z * 255.f; @@ -415,11 +417,10 @@ namespace battleship{ stbi_write_jpg(string(mapFolder + "minimap.jpg").c_str(), width, height, numChannels, imgData, 100); } - void MapEditorAppState::MapEditor::generateMapScript(){ + void MapEditorAppState::MapEditor::generateMapScript(vector &cells){ int numWaterBodies = map->getNodeParent()->getNumChildren() - 1; vector players = Game::getSingleton()->getPlayers(); - Vector3 mapSize = map->getMapSize(); string mapScript = "map = {\nlights = {\n"; for(Node *light : map->getLights()){ @@ -434,8 +435,9 @@ namespace battleship{ mapScript += ", color = {x = " + to_string(color.x) + ", y = " + to_string(color.y) + ", z = " + to_string(color.z) + "}},"; } + Vector3 mapSize = map->getMapSize(); mapScript += "}\nnumWaterBodies = " + to_string(numWaterBodies) + ",\n"; - mapScript += "size = {x = " + to_string(mapSize.x) + ", y = 100, z = " + to_string(mapSize.z) + "},\n"; + mapScript += "size = {x = " + to_string(mapSize.x) + ", y = " + to_string(mapSize.y) + ", z = " + to_string(mapSize.z) + "},\n"; mapScript += "impassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n"; mapScript += "numPlayers = " + to_string(players.size()) + ",\n"; @@ -499,7 +501,6 @@ namespace battleship{ mapScript += "}\n"; } - vector cells = generateMapCells(); mapScript += "},\nnumCells = " + to_string(cells.size()) + ",\n"; mapScript += "cells = {\n"; @@ -568,9 +569,10 @@ namespace battleship{ create_directory(mapFolder); copy_file(assetsPath + DEFAULT_TEXTURE, mapFolder + map->getMapName() + ".jpg"); + vector cells = generateMapCells(); generateLandmassXml(); - generateMinimap(mapFolder); - generateMapScript(); + generateMapScript(cells); + generateMinimap(mapFolder, cells); } void MapEditorAppState::MapEditor::togglePush(bool push){ diff --git a/mapEditorAppState.h b/mapEditorAppState.h index 5674eea..72f9bf5 100644 --- a/mapEditorAppState.h +++ b/mapEditorAppState.h @@ -60,8 +60,8 @@ namespace battleship{ void toggleSelection(vb01::Node*, bool); std::vector generateMapCells(); void generateLandmassXml(); - void generateMinimap(std::string); - void generateMapScript(); + void generateMinimap(std::string, std::vector&); + void generateMapScript(std::vector&); void prepareTerrainObject(vb01::u32**, Map::Cell*, int[3], float, bool); Map *map; diff --git a/minimapButton.cpp b/minimapButton.cpp index bf5f23c..7e285b2 100644 --- a/minimapButton.cpp +++ b/minimapButton.cpp @@ -30,7 +30,8 @@ namespace battleship{ ImageAsset *asset = (ImageAsset*)AssetManager::getSingleton()->getAsset(imagePath); int width = asset->width, height = asset->height, numChannels = asset->numChannels; - Vector3 mapSize = Map::getSingleton()->getMapSize(); + Map *map = Map::getSingleton(); + Vector3 mapSize = map->getMapSize(); vector> unitMinimapPos; for(Player *pl : Game::getSingleton()->getPlayers()) @@ -50,7 +51,7 @@ namespace battleship{ int size = width * height * numChannels; int pxId = 0, numIter = 0; - u8 *oldImageData = activeState->getOldMinimapImage(); + u8 *oldImageData = map->getOldMinimapImage(); int unitPxRadius = 1; for(u8 *p = asset->image; p != asset->image + size; p += numChannels, pxId += numChannels, numIter++){