1er commit
This commit is contained in:
112
src/render/WorldRenderer.cpp
Normal file
112
src/render/WorldRenderer.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "render/WorldRenderer.h"
|
||||
#include "render/loader/WorldLoader.h"
|
||||
#include "render/Renderer.h"
|
||||
#include "../render/gui/imgui/imgui.h"
|
||||
#include "window/Display.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
namespace WorldRenderer{
|
||||
|
||||
static std::unique_ptr<GL::VAO> worldVAO;
|
||||
static const game::World* worldPtr = nullptr;
|
||||
|
||||
static glm::vec2 camPos{13.5, 13.5};//{60, -10};
|
||||
static float zoom = 1;
|
||||
|
||||
static const float camSensibility = 1;
|
||||
|
||||
static std::unique_ptr<GL::VAO> mobVAO;
|
||||
|
||||
void init(const game::World* world){
|
||||
std::cout << "World Created !\n";
|
||||
worldVAO = std::make_unique<GL::VAO>(std::move(WorldLoader::loadWorldModel(world)));
|
||||
mobVAO = std::make_unique<GL::VAO>(std::move(WorldLoader::loadMobModel()));
|
||||
std::cout << "Vertex Count : " << worldVAO->getVertexCount() << std::endl;
|
||||
Renderer::setCamPos(camPos);
|
||||
worldPtr = world;
|
||||
}
|
||||
|
||||
void update(){
|
||||
if(worldVAO == nullptr)
|
||||
return;
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
if(io.MouseDown[0] && !ImGui::IsAnyItemActive()){
|
||||
ImVec2 mouseDelta = ImGui::GetIO().MouseDelta;
|
||||
const float relativeX = mouseDelta.x / (float) Display::getWindowWidth() * 2;
|
||||
const float relativeY = mouseDelta.y / (float) Display::getWindowHeight() * 2;
|
||||
moveCam(relativeX, relativeY, Display::getAspectRatio());
|
||||
}
|
||||
if(io.MouseWheel != 0){
|
||||
changeZoom(io.MouseWheel);
|
||||
}
|
||||
}
|
||||
|
||||
void renderWorld(){
|
||||
Renderer::renderVAO(*worldVAO);
|
||||
}
|
||||
|
||||
void renderMobs(){
|
||||
for(game::MobPtr mob : worldPtr->getMobList()){
|
||||
Renderer::Model model;
|
||||
model.vao = mobVAO.get();
|
||||
model.positon = {mob->getX(), mob->getY()};
|
||||
Renderer::renderModel(model);
|
||||
}
|
||||
}
|
||||
|
||||
void renderTowers(){
|
||||
|
||||
}
|
||||
|
||||
void render(){
|
||||
if(worldVAO == nullptr)
|
||||
return;
|
||||
renderWorld();
|
||||
renderMobs();
|
||||
renderTowers();
|
||||
}
|
||||
|
||||
void destroy(){
|
||||
if(worldVAO == nullptr)
|
||||
return;
|
||||
worldVAO.reset();
|
||||
worldPtr = nullptr;
|
||||
}
|
||||
|
||||
void moveCam(float relativeX, float relativeY, float aspectRatio){
|
||||
if(worldVAO == nullptr)
|
||||
return;
|
||||
float movementX = -relativeX / zoom * aspectRatio;
|
||||
float movementY = relativeY / zoom;
|
||||
Renderer::setCamMovement({movementX, movementY});
|
||||
}
|
||||
|
||||
void changeZoom(float zoomStep){
|
||||
if(worldVAO == nullptr)
|
||||
return;
|
||||
static float sensibility = 1.5f;
|
||||
if (zoomStep < 0){
|
||||
zoom /= -zoomStep * sensibility;
|
||||
}
|
||||
else{
|
||||
zoom *= zoomStep * sensibility;
|
||||
}
|
||||
Renderer::setZoom(zoom);
|
||||
Renderer::setCamMovement({});
|
||||
}
|
||||
|
||||
void click(int mouseX, int mouseY){
|
||||
if(worldVAO == nullptr)
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
} // namespace WorldRenderer
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user