1er commit
This commit is contained in:
161
src/render/loader/WorldLoader.cpp
Normal file
161
src/render/loader/WorldLoader.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include "render/loader/WorldLoader.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
GL::VAO loadMobModel(){
|
||||
std::vector<float> positions = {
|
||||
-0.5, -0.5,
|
||||
0.5, -0.5,
|
||||
-0.5, 0.5,
|
||||
|
||||
0.5, -0.5,
|
||||
-0.5, 0.5,
|
||||
0.5, 0.5
|
||||
};
|
||||
|
||||
float yellowFloat;
|
||||
int yellow = 255 << 24 | 255 << 16 | 255;
|
||||
memcpy(&yellowFloat, &yellow, sizeof(int));
|
||||
|
||||
std::vector<float> colors = {
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
yellowFloat
|
||||
};
|
||||
|
||||
GL::VBO positionVBO(positions, 2);
|
||||
positionVBO.addVertexAttribPointer(0, 2, 0);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
colorVBO.addVertexAttribPointer(1, 1, 0);
|
||||
|
||||
GL::VAO mobVao(colors.size()); // each pos = 1 color
|
||||
mobVao.bind();
|
||||
mobVao.bindVBO(positionVBO);
|
||||
mobVao.bindVBO(colorVBO);
|
||||
mobVao.unbind();
|
||||
return mobVao;
|
||||
}
|
||||
|
||||
GL::VAO loadWorldModel(const td::game::World* world){
|
||||
std::vector<float> positions;
|
||||
std::vector<float> colors;
|
||||
|
||||
for (const auto& chunkInfo : world->getChunks()){
|
||||
const td::game::ChunkCoord& coords = chunkInfo.first;
|
||||
td::game::ChunkPtr chunk = chunkInfo.second;
|
||||
|
||||
std::int32_t chunkX = coords.first * td::game::Chunk::ChunkWidth;
|
||||
std::int32_t chunkY = coords.second * td::game::Chunk::ChunkHeight;
|
||||
|
||||
for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++){
|
||||
for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++){
|
||||
int tileNumber = tileY * td::game::Chunk::ChunkWidth + tileX;
|
||||
td::game::TileIndex tileIndex = chunk->getTileIndex(tileNumber);
|
||||
td::game::TilePtr tile = world->getTilePtr(tileIndex);
|
||||
|
||||
if (tile == nullptr)
|
||||
continue;
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
(float)chunkX + tileX, (float)chunkY + tileY,
|
||||
(float)chunkX + tileX + 1, (float)chunkY + tileY,
|
||||
(float)chunkX + tileX, (float)chunkY + tileY + 1,
|
||||
|
||||
(float)chunkX + tileX + 1, (float)chunkY + tileY,
|
||||
(float)chunkX + tileX, (float)chunkY + tileY + 1,
|
||||
(float)chunkX + tileX + 1, (float)chunkY + tileY + 1,
|
||||
});
|
||||
|
||||
/*positions.push_back(chunkX + tileX);
|
||||
positions.push_back(chunkY + tileY);
|
||||
|
||||
positions.push_back(chunkX + tileX + 1);
|
||||
positions.push_back(chunkY + tileY);
|
||||
|
||||
positions.push_back(chunkX + tileX);
|
||||
positions.push_back(chunkY + tileY + 1);
|
||||
|
||||
|
||||
|
||||
positions.push_back(chunkX + tileX + 1);
|
||||
positions.push_back(chunkY + tileY);
|
||||
|
||||
positions.push_back(chunkX + tileX);
|
||||
positions.push_back(chunkY + tileY + 1);
|
||||
|
||||
positions.push_back(chunkX + tileX + 1);
|
||||
positions.push_back(chunkY + tileY + 1);*/
|
||||
|
||||
const td::game::Color& tileColor = world->getTileColor(tile);
|
||||
|
||||
for (int i = 0; i < 6; i++){
|
||||
int color = 255;
|
||||
color |= tileColor.r << 24;
|
||||
color |= tileColor.g << 16;
|
||||
color |= tileColor.b << 8;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int spawnColor = 0; spawnColor < 2; spawnColor++){
|
||||
const game::Spawn& spawn = world->getSpawn(game::TeamColor(spawnColor));
|
||||
float fromX = spawn.x - 2, toX = spawn.x + 3;
|
||||
float fromY = spawn.y - 2, toY = spawn.y + 3;
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
fromX, fromY,
|
||||
fromX, toY,
|
||||
toX, fromY,
|
||||
|
||||
toX, toY,
|
||||
fromX, toY,
|
||||
toX, fromY,
|
||||
});
|
||||
|
||||
for (int i = 0; i < 6; i++){
|
||||
int color = 255;
|
||||
color |= world->getSpawnColor(game::TeamColor(spawnColor)).r << 24;
|
||||
color |= world->getSpawnColor(game::TeamColor(spawnColor)).g << 16;
|
||||
color |= world->getSpawnColor(game::TeamColor(spawnColor)).b << 8;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
|
||||
GL::VBO positionVBO(positions, 2);
|
||||
positionVBO.addVertexAttribPointer(0, 2, 0);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
colorVBO.addVertexAttribPointer(1, 1, 0);
|
||||
|
||||
GL::VAO worldVao(positions.size() / 2); // each pos = 2 vertecies
|
||||
worldVao.bind();
|
||||
worldVao.bindVBO(positionVBO);
|
||||
worldVao.bindVBO(colorVBO);
|
||||
worldVao.unbind();
|
||||
return worldVao;
|
||||
}
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user