fix: change renderer to class
This commit is contained in:
@@ -7,7 +7,8 @@
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
ClientGame::ClientGame(protocol::PacketDispatcher* dispatcher): protocol::PacketHandler(dispatcher), game::Game(&m_WorldClient){
|
||||
ClientGame::ClientGame(protocol::PacketDispatcher* dispatcher, render::Renderer* renderer): protocol::PacketHandler(dispatcher),
|
||||
game::Game(&m_WorldClient), m_Renderer(renderer), m_WorldClient(this), m_WorldRenderer(&m_WorldClient, m_Renderer){
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::ConnectionInfo, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerJoin, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerList, this);
|
||||
|
||||
@@ -6,11 +6,9 @@
|
||||
*/
|
||||
|
||||
#include "render/Renderer.h"
|
||||
#include "render/shaders/WorldShader.h"
|
||||
#include "render/shaders/EntityShader.h"
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <stdio.h>
|
||||
#include <glbinding/Binding.h>
|
||||
#include <stdio.h>
|
||||
#include "misc/Time.h"
|
||||
#include "misc/Easing.h"
|
||||
|
||||
@@ -19,35 +17,32 @@ using namespace gl;
|
||||
namespace td{
|
||||
namespace render{
|
||||
|
||||
namespace Renderer{
|
||||
Renderer::Renderer(){
|
||||
|
||||
#define ANIMATION_SPEED 2.0f
|
||||
|
||||
static std::unique_ptr<WorldShader> worldShader;
|
||||
static std::unique_ptr<EntityShader> entityShader;
|
||||
|
||||
static bool isometricView = true;
|
||||
static float isometricShade = isometricView;
|
||||
static glm::vec2 camPos{};
|
||||
|
||||
void updateIsometricView(){
|
||||
float isometricEased = utils::easeInOutExpo(isometricShade);
|
||||
worldShader->start();
|
||||
worldShader->setIsometricView(isometricEased);
|
||||
entityShader->start();
|
||||
entityShader->setIsometricView(isometricEased);
|
||||
}
|
||||
|
||||
void initShader(){
|
||||
worldShader = std::make_unique<WorldShader>();
|
||||
worldShader->loadShader();
|
||||
entityShader = std::make_unique<EntityShader>();
|
||||
entityShader->loadShader();
|
||||
Renderer::~Renderer(){
|
||||
|
||||
}
|
||||
|
||||
void Renderer::updateIsometricView(){
|
||||
float isometricEased = utils::easeInOutExpo(m_IsometricShade);
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setIsometricView(isometricEased);
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setIsometricView(isometricEased);
|
||||
}
|
||||
|
||||
void Renderer::initShader(){
|
||||
m_WorldShader = std::make_unique<WorldShader>();
|
||||
m_WorldShader->loadShader();
|
||||
m_EntityShader = std::make_unique<EntityShader>();
|
||||
m_EntityShader->loadShader();
|
||||
setIsometricView(true);
|
||||
updateIsometricView();
|
||||
}
|
||||
|
||||
bool init(){
|
||||
bool Renderer::init(){
|
||||
glbinding::Binding::initialize();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
@@ -56,83 +51,78 @@ bool init(){
|
||||
return true;
|
||||
}
|
||||
|
||||
void destroy(){
|
||||
worldShader.reset(0);
|
||||
entityShader.reset(0);
|
||||
}
|
||||
|
||||
void renderVAO(const GL::VAO& vao){
|
||||
worldShader->start();
|
||||
void Renderer::renderVAO(const GL::VAO& vao){
|
||||
m_WorldShader->start();
|
||||
vao.bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount());
|
||||
vao.unbind();
|
||||
}
|
||||
|
||||
void renderModel(const Model& model){
|
||||
entityShader->start();
|
||||
entityShader->setModelPos(model.positon);
|
||||
void Renderer::renderModel(const Model& model){
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setModelPos(model.positon);
|
||||
model.vao->bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, model.vao->getVertexCount());
|
||||
model.vao->unbind();
|
||||
}
|
||||
|
||||
void updateIsometricFade(){
|
||||
void Renderer::updateIsometricFade(){
|
||||
static std::uint64_t lastTime = utils::getTime();
|
||||
if(isometricShade != (float) isometricView){
|
||||
float step = (float) (utils::getTime() - lastTime) / 1000.0f * ANIMATION_SPEED;
|
||||
if(isometricShade < isometricView){
|
||||
isometricShade += step;
|
||||
if(m_IsometricShade != (float) m_IsometricView){
|
||||
float step = (float) (utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed;
|
||||
if(m_IsometricShade < m_IsometricView){
|
||||
m_IsometricShade += step;
|
||||
}else{
|
||||
isometricShade -= step;
|
||||
m_IsometricShade -= step;
|
||||
}
|
||||
isometricShade = std::min(isometricShade, 1.0f);
|
||||
isometricShade = std::max(isometricShade, 0.0f);
|
||||
m_IsometricShade = std::min(m_IsometricShade, 1.0f);
|
||||
m_IsometricShade = std::max(m_IsometricShade, 0.0f);
|
||||
updateIsometricView();
|
||||
}
|
||||
lastTime = utils::getTime();
|
||||
}
|
||||
|
||||
void prepare(){
|
||||
void Renderer::prepare(){
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClearColor(0, 0, 0, 0);
|
||||
updateIsometricFade();
|
||||
}
|
||||
|
||||
void resize(int width, int height){
|
||||
worldShader->start();
|
||||
worldShader->setAspectRatio((float)width / height);
|
||||
entityShader->start();
|
||||
entityShader->setAspectRatio((float)width / height);
|
||||
void Renderer::resize(int width, int height){
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setAspectRatio((float)width / height);
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setAspectRatio((float)width / height);
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void setZoom(float zoom){
|
||||
worldShader->start();
|
||||
worldShader->setZoom(zoom);
|
||||
entityShader->start();
|
||||
entityShader->setZoom(zoom);
|
||||
void Renderer::setZoom(float zoom){
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setZoom(zoom);
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setZoom(zoom);
|
||||
}
|
||||
|
||||
void setCamMovement(const glm::vec2& mov){
|
||||
camPos.x += mov.x * (1 - isometricView) + (0.5 * mov.x - mov.y) * isometricView;
|
||||
camPos.y += -mov.y * (1 - isometricView) + (-0.5 * mov.x - mov.y) * isometricView;
|
||||
setCamPos(camPos);
|
||||
void Renderer::setCamMovement(const glm::vec2& mov){
|
||||
m_CamPos.x += mov.x * (1 - m_IsometricView) + (0.5 * mov.x - mov.y) * m_IsometricView;
|
||||
m_CamPos.y += -mov.y * (1 - m_IsometricView) + (-0.5 * mov.x - mov.y) * m_IsometricView;
|
||||
setCamPos(m_CamPos);
|
||||
}
|
||||
|
||||
void setCamPos(const glm::vec2& newPos){
|
||||
camPos = newPos;
|
||||
worldShader->start();
|
||||
worldShader->setCamPos(newPos);
|
||||
entityShader->start();
|
||||
entityShader->setCamPos(newPos);
|
||||
void Renderer::setCamPos(const glm::vec2& newPos){
|
||||
m_CamPos = newPos;
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setCamPos(newPos);
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setCamPos(newPos);
|
||||
}
|
||||
|
||||
void setIsometricView(bool isometric){
|
||||
isometricView = isometric;
|
||||
void Renderer::setIsometricView(bool isometric){
|
||||
m_IsometricView = isometric;
|
||||
}
|
||||
|
||||
glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight){
|
||||
float isometricEased = utils::easeInOutExpo(isometricShade);
|
||||
glm::vec2 Renderer::getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight){
|
||||
float isometricEased = utils::easeInOutExpo(m_IsometricShade);
|
||||
|
||||
float relativeX = (cursorPos.x / windowWidth * 2) - 1;
|
||||
float relativeY = (cursorPos.y / windowHeight * 2) - 1;
|
||||
@@ -140,13 +130,12 @@ glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float
|
||||
float deltaX = relativeX * aspectRatio / zoom;
|
||||
float deltaY = relativeY / zoom;
|
||||
|
||||
float worldX = camPos.x + deltaX * (1 - isometricEased) + (0.5 * deltaX + deltaY) * isometricEased;
|
||||
float worldY = camPos.y + deltaY * (1 - isometricEased) + (-0.5 * deltaX + deltaY) * isometricEased;
|
||||
float worldX = m_CamPos.x + deltaX * (1 - isometricEased) + (0.5 * deltaX + deltaY) * isometricEased;
|
||||
float worldY = m_CamPos.y + deltaY * (1 - isometricEased) + (-0.5 * deltaX + deltaY) * isometricEased;
|
||||
|
||||
return {worldX, worldY};
|
||||
}
|
||||
|
||||
} // namespace Renderer
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
|
||||
@@ -17,7 +17,7 @@ void WorldRenderer::loadModels(){
|
||||
std::cout << "Vertex Count : " << m_WorldVao->getVertexCount() << std::endl;
|
||||
}
|
||||
|
||||
WorldRenderer::WorldRenderer(game::World* world) : m_World(world){
|
||||
WorldRenderer::WorldRenderer(game::World* world, Renderer* renderer) : m_Renderer(renderer), m_World(world) {
|
||||
}
|
||||
|
||||
void WorldRenderer::updateCursorPos(){
|
||||
@@ -26,7 +26,7 @@ void WorldRenderer::updateCursorPos(){
|
||||
float mouseX = io.MousePos.x;
|
||||
float mouseY = io.MousePos.y;
|
||||
|
||||
m_CursorPos = Renderer::getCursorWorldPos({mouseX, mouseY}, Display::getAspectRatio(), m_Zoom, Display::getWindowWidth(), Display::getWindowHeight());
|
||||
m_CursorPos = m_Renderer->getCursorWorldPos({mouseX, mouseY}, Display::getAspectRatio(), m_Zoom, Display::getWindowWidth(), Display::getWindowHeight());
|
||||
}
|
||||
|
||||
void WorldRenderer::update(){
|
||||
@@ -46,7 +46,7 @@ void WorldRenderer::update(){
|
||||
}
|
||||
|
||||
void WorldRenderer::renderWorld() const{
|
||||
Renderer::renderVAO(*m_WorldVao);
|
||||
m_Renderer->renderVAO(*m_WorldVao);
|
||||
}
|
||||
|
||||
void WorldRenderer::renderMobs() const{
|
||||
@@ -54,7 +54,7 @@ void WorldRenderer::renderMobs() const{
|
||||
Renderer::Model model;
|
||||
model.vao = m_MobVao.get();
|
||||
model.positon = {mob->getX(), mob->getY()};
|
||||
Renderer::renderModel(model);
|
||||
m_Renderer->renderModel(model);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ void WorldRenderer::renderTowers() const{
|
||||
tileSelectModel.vao = m_SelectTileVao.get();
|
||||
tileSelectModel.positon = {(int) m_CursorPos.x, (int) m_CursorPos.y};
|
||||
|
||||
Renderer::renderModel(tileSelectModel);
|
||||
m_Renderer->renderModel(tileSelectModel);
|
||||
}
|
||||
|
||||
void WorldRenderer::renderTileSelect() const{
|
||||
@@ -88,7 +88,7 @@ void WorldRenderer::moveCam(float relativeX, float relativeY, float aspectRatio)
|
||||
return;
|
||||
float movementX = -relativeX / m_Zoom * aspectRatio;
|
||||
float movementY = relativeY / m_Zoom;
|
||||
Renderer::setCamMovement({movementX, movementY});
|
||||
m_Renderer->setCamMovement({movementX, movementY});
|
||||
}
|
||||
|
||||
void WorldRenderer::changeZoom(float zoomStep){
|
||||
@@ -101,8 +101,8 @@ void WorldRenderer::changeZoom(float zoomStep){
|
||||
else{
|
||||
m_Zoom *= zoomStep * sensibility;
|
||||
}
|
||||
Renderer::setZoom(m_Zoom);
|
||||
Renderer::setCamMovement({});
|
||||
m_Renderer->setZoom(m_Zoom);
|
||||
m_Renderer->setCamMovement({});
|
||||
}
|
||||
|
||||
void WorldRenderer::click(int mouseX, int mouseY){
|
||||
@@ -112,7 +112,7 @@ void WorldRenderer::click(int mouseX, int mouseY){
|
||||
|
||||
void WorldRenderer::setCamPos(float camX, float camY){
|
||||
m_CamPos = {camX, camY};
|
||||
Renderer::setCamPos(m_CamPos);
|
||||
m_Renderer->setCamPos(m_CamPos);
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
|
||||
@@ -23,12 +23,13 @@
|
||||
namespace TowerGui{
|
||||
|
||||
static GLFWwindow* window;
|
||||
static std::unique_ptr<td::client::Client> client = std::make_unique<td::client::Client>();
|
||||
static std::unique_ptr<td::client::Client> client;
|
||||
static std::thread* serverThread;
|
||||
static td::render::Renderer* renderer;
|
||||
|
||||
bool serverShouldStop = false;
|
||||
|
||||
void init(GLFWwindow* glfw_window){
|
||||
void init(GLFWwindow* glfw_window, td::render::Renderer* render){
|
||||
window = glfw_window;
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
@@ -39,6 +40,8 @@ void init(GLFWwindow* glfw_window){
|
||||
ImFontConfig c;
|
||||
c.SizePixels = 25;
|
||||
ImGui::GetIO().Fonts->AddFontDefault(&c);
|
||||
renderer = render;
|
||||
client = std::make_unique<td::client::Client>(render);
|
||||
}
|
||||
|
||||
void beginFrame(){
|
||||
@@ -62,7 +65,7 @@ void renderFPSCounter(){
|
||||
}
|
||||
static bool isometric = true;
|
||||
if (ImGui::Checkbox("Vue Isometrique ?", &isometric)){
|
||||
td::render::Renderer::setIsometricView(isometric);
|
||||
renderer->setIsometricView(isometric);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
namespace Display {
|
||||
|
||||
static GLFWwindow* window;
|
||||
std::unique_ptr<td::render::Renderer> renderer = std::make_unique<td::render::Renderer>();
|
||||
|
||||
static int lastWidth = 0, lastHeight = 0;
|
||||
static float aspectRatio;
|
||||
@@ -31,7 +32,7 @@ void error_callback(int error, const char* description){
|
||||
|
||||
void windowResizeEvent(GLFWwindow* window, int width, int height){
|
||||
aspectRatio = (float) width / height;
|
||||
td::render::Renderer::resize(width, height);
|
||||
renderer->resize(width, height);
|
||||
lastWidth = width;
|
||||
lastHeight = height;
|
||||
}
|
||||
@@ -45,15 +46,15 @@ void create() {
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_NAME, nullptr, nullptr);
|
||||
glfwMakeContextCurrent(window);
|
||||
if(!td::render::Renderer::init()){
|
||||
if(!renderer->init()){
|
||||
exit(1);
|
||||
}
|
||||
TowerGui::init(window);
|
||||
TowerGui::init(window, renderer.get());
|
||||
windowResizeEvent(window, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
}
|
||||
|
||||
void render() {
|
||||
td::render::Renderer::prepare();
|
||||
renderer->prepare();
|
||||
TowerGui::render();
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ void update() {
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
td::render::Renderer::destroy();
|
||||
renderer.reset(0);
|
||||
TowerGui::destroy();
|
||||
glfwDestroyWindow(window);
|
||||
window = NULL;
|
||||
|
||||
Reference in New Issue
Block a user