faet: add tile selector
This commit is contained in:
@@ -35,6 +35,8 @@ void setCamMovement(const glm::vec2& mov);
|
||||
void setCamPos(const glm::vec2& newPos);
|
||||
void setIsometricView(bool isometric); // false = 2D true = Isometric
|
||||
|
||||
glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,8 +11,9 @@ namespace render {
|
||||
class WorldRenderer{
|
||||
private:
|
||||
game::World* m_World;
|
||||
std::unique_ptr<GL::VAO> m_WorldVao, m_MobVao;
|
||||
std::unique_ptr<GL::VAO> m_WorldVao, m_MobVao, m_SelectTileVao;
|
||||
glm::vec2 m_CamPos;
|
||||
glm::vec2 m_CursorPos;
|
||||
float m_Zoom = 1;
|
||||
float m_CamSensibility = 1;
|
||||
public:
|
||||
@@ -33,6 +34,9 @@ private:
|
||||
void renderWorld() const;
|
||||
void renderTowers() const;
|
||||
void renderMobs() const;
|
||||
void renderTileSelect() const;
|
||||
|
||||
void updateCursorPos();
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace WorldLoader {
|
||||
|
||||
GL::VAO loadMobModel();
|
||||
GL::VAO loadWorldModel(const td::game::World* world);
|
||||
GL::VAO loadTileSelectModel();
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
@@ -48,6 +48,8 @@ void initShader(){
|
||||
bool init(){
|
||||
glbinding::Binding::initialize();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
initShader();
|
||||
return true;
|
||||
}
|
||||
@@ -127,6 +129,19 @@ void setIsometricView(bool isometric){
|
||||
isometricView = isometric;
|
||||
}
|
||||
|
||||
glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight){
|
||||
float relativeX = (cursorPos.x / windowWidth * 2) - 1;
|
||||
float relativeY = (cursorPos.y / windowHeight * 2) - 1;
|
||||
|
||||
float deltaX = relativeX * aspectRatio / zoom;
|
||||
float deltaY = relativeY / zoom;
|
||||
|
||||
float worldX = camPos.x + deltaX * (1 - isometricView) + (0.5 * deltaX - deltaY) * isometricView;
|
||||
float worldY = camPos.y + deltaY * (1 - isometricView) + (0.5 * deltaX + deltaY) * isometricView;
|
||||
|
||||
return {worldX, worldY};
|
||||
}
|
||||
|
||||
} // namespace Renderer
|
||||
|
||||
} // namespace render
|
||||
|
||||
@@ -13,12 +13,22 @@ void WorldRenderer::loadModels(){
|
||||
std::cout << "World Created !\n";
|
||||
m_WorldVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadWorldModel(m_World)));
|
||||
m_MobVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadMobModel()));
|
||||
m_SelectTileVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadTileSelectModel()));
|
||||
std::cout << "Vertex Count : " << m_WorldVao->getVertexCount() << std::endl;
|
||||
}
|
||||
|
||||
WorldRenderer::WorldRenderer(game::World* world) : m_World(world){
|
||||
}
|
||||
|
||||
void WorldRenderer::updateCursorPos(){
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
float mouseX = io.MousePos.x;
|
||||
float mouseY = io.MousePos.y;
|
||||
|
||||
m_CursorPos = Renderer::getCursorWorldPos({mouseX, mouseY}, Display::getAspectRatio(), m_Zoom, Display::getWindowWidth(), Display::getWindowHeight());
|
||||
}
|
||||
|
||||
void WorldRenderer::update(){
|
||||
if(m_WorldVao == nullptr)
|
||||
return;
|
||||
@@ -32,6 +42,7 @@ void WorldRenderer::update(){
|
||||
if(io.MouseWheel != 0){
|
||||
changeZoom(io.MouseWheel);
|
||||
}
|
||||
updateCursorPos();
|
||||
}
|
||||
|
||||
void WorldRenderer::renderWorld() const{
|
||||
@@ -48,6 +59,14 @@ void WorldRenderer::renderMobs() const{
|
||||
}
|
||||
|
||||
void WorldRenderer::renderTowers() const{
|
||||
Renderer::Model tileSelectModel;
|
||||
tileSelectModel.vao = m_SelectTileVao.get();
|
||||
tileSelectModel.positon = {(int) m_CursorPos.x, (int) m_CursorPos.y};
|
||||
|
||||
Renderer::renderModel(tileSelectModel);
|
||||
}
|
||||
|
||||
void WorldRenderer::renderTileSelect() const{
|
||||
|
||||
}
|
||||
|
||||
@@ -57,6 +76,7 @@ void WorldRenderer::render() const{
|
||||
renderWorld();
|
||||
renderMobs();
|
||||
renderTowers();
|
||||
renderTileSelect();
|
||||
}
|
||||
|
||||
WorldRenderer::~WorldRenderer(){
|
||||
|
||||
@@ -156,6 +156,38 @@ GL::VAO loadWorldModel(const td::game::World* world){
|
||||
return worldVao;
|
||||
}
|
||||
|
||||
GL::VAO loadTileSelectModel(){
|
||||
std::vector<float> positions = {
|
||||
0, 0,
|
||||
1, 0,
|
||||
0, 1,
|
||||
|
||||
1, 0,
|
||||
0, 1,
|
||||
1, 1
|
||||
};
|
||||
|
||||
int color = 255 << 24 | 255 << 16 | 255 << 8 | 150;
|
||||
float colorFloat;
|
||||
|
||||
memcpy((std::uint8_t*) &colorFloat, &color, sizeof(float));
|
||||
|
||||
std::vector<float> colors(6, colorFloat);
|
||||
|
||||
GL::VBO positionVBO(positions, 2);
|
||||
positionVBO.addVertexAttribPointer(0, 2, 0);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
colorVBO.addVertexAttribPointer(1, 1, 0);
|
||||
|
||||
GL::VAO tileSelectVao(positions.size() / 2); // each pos = 2 vertecies
|
||||
tileSelectVao.bind();
|
||||
tileSelectVao.bindVBO(positionVBO);
|
||||
tileSelectVao.bindVBO(colorVBO);
|
||||
tileSelectVao.unbind();
|
||||
|
||||
return tileSelectVao;
|
||||
}
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user