GIGA REFACTOR

This commit is contained in:
2022-03-02 18:51:42 +01:00
parent 553b2f6aad
commit 6df59b1487
92 changed files with 1807 additions and 1785 deletions

View File

@@ -25,53 +25,53 @@ 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::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();
void Renderer::InitShaders() {
m_WorldShader = std::make_unique<shader::WorldShader>();
m_WorldShader->LoadShader();
m_EntityShader = std::make_unique<shader::EntityShader>();
m_EntityShader->LoadShader();
SetIsometricView(true);
UpdateIsometricView();
}
bool Renderer::init() {
bool Renderer::Init() {
#ifndef ANDROID
glbinding::Binding::initialize();
#endif
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
initShader();
InitShaders();
return true;
}
void Renderer::renderVAO(const GL::VertexArray& vao) {
m_WorldShader->start();
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vao.getVertexCount()));
vao.unbind();
void Renderer::RenderVAO(const GL::VertexArray& vao) {
m_WorldShader->Start();
vao.Bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vao.GetVertexCount()));
vao.Unbind();
}
void Renderer::renderModel(const Model& model) {
m_EntityShader->start();
m_EntityShader->setModelPos(model.positon);
model.vao->bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.vao->getVertexCount()));
model.vao->unbind();
void Renderer::RenderModel(const Model& model) {
m_EntityShader->Start();
m_EntityShader->SetModelPos(model.positon);
model.vao->Bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.vao->GetVertexCount()));
model.vao->Unbind();
}
void Renderer::updateIsometricFade() {
static std::uint64_t lastTime = utils::getTime();
void Renderer::UpdateIsometricFade() {
static std::uint64_t lastTime = utils::GetTime();
if (m_IsometricShade != static_cast<float>(m_IsometricView)) {
float step = static_cast<float>(utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed;
float step = static_cast<float>(utils::GetTime() - lastTime) / 1000.0f * m_AnimationSpeed;
if (m_IsometricShade < m_IsometricView) {
m_IsometricShade += step;
} else {
@@ -79,52 +79,52 @@ void Renderer::updateIsometricFade() {
}
m_IsometricShade = std::min(m_IsometricShade, 1.0f);
m_IsometricShade = std::max(m_IsometricShade, 0.0f);
updateIsometricView();
UpdateIsometricView();
}
lastTime = utils::getTime();
lastTime = utils::GetTime();
}
void Renderer::prepare() {
void Renderer::Prepare() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0);
updateIsometricFade();
UpdateIsometricFade();
}
void Renderer::resize(int width, int height) {
m_WorldShader->start();
m_WorldShader->setAspectRatio(static_cast<float>(width) / height);
m_EntityShader->start();
m_EntityShader->setAspectRatio(static_cast<float>(width) / height);
void Renderer::Resize(int width, int height) {
m_WorldShader->Start();
m_WorldShader->SetAspectRatio(static_cast<float>(width) / height);
m_EntityShader->Start();
m_EntityShader->SetAspectRatio(static_cast<float>(width) / height);
glViewport(0, 0, width, height);
}
void Renderer::setZoom(float zoom) {
m_WorldShader->start();
m_WorldShader->setZoom(zoom);
m_EntityShader->start();
m_EntityShader->setZoom(zoom);
void Renderer::SetZoom(float zoom) {
m_WorldShader->Start();
m_WorldShader->SetZoom(zoom);
m_EntityShader->Start();
m_EntityShader->SetZoom(zoom);
}
void Renderer::setCamMovement(const glm::vec2& mov) {
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);
SetCamPos(m_CamPos);
}
void Renderer::setCamPos(const glm::vec2& 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);
m_WorldShader->Start();
m_WorldShader->SetCamPos(newPos);
m_EntityShader->Start();
m_EntityShader->SetCamPos(newPos);
}
void Renderer::setIsometricView(bool isometric) {
void Renderer::SetIsometricView(bool isometric) {
m_IsometricView = isometric;
}
glm::vec2 Renderer::getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight) {
float isometricEased = utils::easeInOutExpo(m_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;

View File

@@ -4,12 +4,12 @@
namespace td {
namespace render {
void VertexCache::addData(std::uint64_t index, std::vector<float> positions, std::vector<float> colors) {
void VertexCache::AddData(std::uint64_t index, std::vector<float> positions, std::vector<float> colors) {
m_Indexes.insert({ index, {positions, colors} });
m_VertexCount += colors.size(); // one color per vertex
}
void VertexCache::removeData(std::uint64_t index) {
void VertexCache::RemoveData(std::uint64_t index) {
auto it = m_Indexes.find(index);
if (it != m_Indexes.end()) {
m_Indexes.erase(it);
@@ -17,12 +17,12 @@ void VertexCache::removeData(std::uint64_t index) {
}
}
void VertexCache::clear() {
void VertexCache::Clear() {
m_Indexes.clear();
m_VertexCount = 0;
}
void VertexCache::updateVertexArray() {
void VertexCache::UpdateVertexArray() {
m_VertexArray = std::make_unique<GL::VertexArray>(m_VertexCount); // one color per vertex
Vector positions;
@@ -39,15 +39,15 @@ void VertexCache::updateVertexArray() {
}
GL::VertexBuffer positionsBuffer(positions, 2);
positionsBuffer.addVertexAttribPointer(0, 2, 0);
positionsBuffer.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorsBuffer(colors, 1);
colorsBuffer.addVertexAttribPointer(1, 1, 0);
colorsBuffer.AddVertexAttribPointer(1, 1, 0);
m_VertexArray->bind();
m_VertexArray->bindVertexBuffer(positionsBuffer);
m_VertexArray->bindVertexBuffer(colorsBuffer);
m_VertexArray->unbind();
m_VertexArray->Bind();
m_VertexArray->BindVertexBuffer(positionsBuffer);
m_VertexArray->BindVertexBuffer(colorsBuffer);
m_VertexArray->Unbind();
}
} // namespace render

View File

@@ -12,7 +12,7 @@
namespace td {
namespace render {
ImVec4 WorldRenderer::getImGuiTeamColor(game::TeamColor color) {
ImVec4 WorldRenderer::GetImGuiTeamColor(game::TeamColor color) {
switch (color) {
case td::game::TeamColor::None:
break;
@@ -24,41 +24,41 @@ ImVec4 WorldRenderer::getImGuiTeamColor(game::TeamColor color) {
return ImVec4(1, 1, 1, 1);
}
void WorldRenderer::loadModels() {
void WorldRenderer::LoadModels() {
std::cout << "World Created !\n";
m_WorldVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadWorldModel(m_World)));
m_MobVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadMobModel()));
m_SelectTileVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadTileSelectModel()));
std::cout << "Vertex Count : " << m_WorldVao->getVertexCount() << std::endl;
m_WorldVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::LoadWorldModel(m_World)));
m_MobVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::LoadMobModel()));
m_SelectTileVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::LoadTileSelectModel()));
std::cout << "Vertex Count : " << m_WorldVao->GetVertexCount() << std::endl;
}
WorldRenderer::WorldRenderer(game::World* world, client::ClientGame* client) : m_Client(client), m_Renderer(m_Client->getRenderer()), m_World(world), m_Zoom(0.1) {
m_Renderer->setZoom(m_Zoom);
m_Renderer->setCamMovement({});
m_TowerPlacePopup = std::make_unique<gui::TowerPlacePopup>(m_Client->getClient());
m_MobTooltip = std::make_unique<gui::MobTooltip>(m_Client->getClient());
m_CastleTooltip = std::make_unique<gui::CastleTooltip>(m_Client->getClient());
m_Client->getWorldClient().getWorldNotifier().bindListener(this);
WorldRenderer::WorldRenderer(game::World* world, client::ClientGame* client) : m_Client(client), m_Renderer(m_Client->GetRenderer()), m_World(world), m_Zoom(0.1) {
m_Renderer->SetZoom(m_Zoom);
m_Renderer->SetCamMovement({});
m_TowerPlacePopup = std::make_unique<gui::TowerPlacePopup>(m_Client->GetClient());
m_MobTooltip = std::make_unique<gui::MobTooltip>(m_Client->GetClient());
m_CastleTooltip = std::make_unique<gui::CastleTooltip>(m_Client->GetClient());
m_Client->GetWorldClient().GetWorldNotifier().BindListener(this);
}
void WorldRenderer::updateCursorPos() {
m_CursorPos = getCursorWorldPos();
void WorldRenderer::UpdateCursorPos() {
m_CursorPos = GetCursorWorldPos();
}
void WorldRenderer::update() {
void WorldRenderer::Update() {
if (m_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());
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);
ChangeZoom(io.MouseWheel);
}
updateCursorPos();
UpdateCursorPos();
if (ImGui::IsMouseClicked(0)) {
if (!m_PopupOpened) {
m_HoldCursorPos = { io.MousePos.x, io.MousePos.y };
@@ -67,84 +67,84 @@ void WorldRenderer::update() {
}
}
if (ImGui::IsMouseDoubleClicked(1)) removeTower();
if (ImGui::IsMouseDoubleClicked(1)) RemoveTower();
}
void WorldRenderer::removeTower() {
glm::vec2 cursorPos = getCursorWorldPos();
void WorldRenderer::RemoveTower() {
glm::vec2 cursorPos = GetCursorWorldPos();
game::TowerPtr clickedTower = m_World->getTower(cursorPos);
game::TowerPtr clickedTower = m_World->GetTower(cursorPos);
if (clickedTower != nullptr) {
m_Client->getClient()->removeTower(clickedTower->getID());
m_Client->GetClient()->RemoveTower(clickedTower->GetID());
}
}
void WorldRenderer::renderWorld() const {
m_Renderer->renderVAO(*m_WorldVao);
void WorldRenderer::RenderWorld() const {
m_Renderer->RenderVAO(*m_WorldVao);
}
void WorldRenderer::renderMobs() const {
for (game::MobPtr mob : m_World->getMobList()) {
void WorldRenderer::RenderMobs() const {
for (game::MobPtr mob : m_World->GetMobList()) {
Renderer::Model model;
model.vao = m_MobVao.get();
model.positon = { mob->getCenterX(), mob->getCenterY() };
m_Renderer->renderModel(model);
model.positon = { mob->GetCenterX(), mob->GetCenterY() };
m_Renderer->RenderModel(model);
}
}
void WorldRenderer::renderTowers() const {
if (!m_TowersCache.isEmpty())
m_Renderer->renderVAO(m_TowersCache.getVertexArray());
void WorldRenderer::RenderTowers() const {
if (!m_TowersCache.IsEmpty())
m_Renderer->RenderVAO(m_TowersCache.GetVertexArray());
}
void WorldRenderer::renderTileSelect() const {
void WorldRenderer::RenderTileSelect() const {
if (ImGui::IsAnyItemHovered()) return;
if(m_MobTooltip->isShown() || m_CastleTooltip->isShown()) return;
if (m_MobTooltip->IsShown() || m_CastleTooltip->IsShown()) return;
Renderer::Model tileSelectModel;
tileSelectModel.vao = m_SelectTileVao.get();
tileSelectModel.positon = { (int)m_CursorPos.x, (int)m_CursorPos.y };
m_Renderer->renderModel(tileSelectModel);
m_Renderer->RenderModel(tileSelectModel);
}
void WorldRenderer::renderPopups() {
m_TowerPlacePopup->render();
renderTowerUpgradePopup();
void WorldRenderer::RenderPopups() {
m_TowerPlacePopup->Render();
RenderTowerUpgradePopup();
}
void WorldRenderer::render() {
void WorldRenderer::Render() {
if (m_WorldVao == nullptr)
return;
renderWorld();
renderMobs();
renderTowers();
renderTileSelect();
renderTooltips();
renderPopups();
detectClick();
RenderWorld();
RenderMobs();
RenderTowers();
RenderTileSelect();
RenderTooltips();
RenderPopups();
DetectClick();
}
WorldRenderer::~WorldRenderer() {
}
void WorldRenderer::renderTooltips() const {
renderMobTooltip();
renderCastleTooltip();
void WorldRenderer::RenderTooltips() const {
RenderMobTooltip();
RenderCastleTooltip();
}
void WorldRenderer::moveCam(float relativeX, float relativeY, float aspectRatio) {
void WorldRenderer::MoveCam(float relativeX, float relativeY, float aspectRatio) {
if (m_WorldVao == nullptr)
return;
float movementX = -relativeX / m_Zoom * aspectRatio;
float movementY = relativeY / m_Zoom;
m_Renderer->setCamMovement({ movementX, movementY });
m_Renderer->SetCamMovement({ movementX, movementY });
}
void WorldRenderer::changeZoom(float zoomStep) {
void WorldRenderer::ChangeZoom(float zoomStep) {
if (m_WorldVao == nullptr)
return;
static float sensibility = 1.5f;
@@ -153,34 +153,34 @@ void WorldRenderer::changeZoom(float zoomStep) {
} else {
m_Zoom *= zoomStep * sensibility;
}
m_Renderer->setZoom(m_Zoom);
m_Renderer->setCamMovement({});
m_Renderer->SetZoom(m_Zoom);
m_Renderer->SetCamMovement({});
}
void WorldRenderer::click() {
const game::TowerPtr tower = m_Client->getWorld().getTower(getClickWorldPos());
m_TowerPlacePopup->setClickPos(getClickWorldPos());
void WorldRenderer::Click() {
const game::TowerPtr tower = m_Client->GetWorld().GetTower(GetClickWorldPos());
m_TowerPlacePopup->SetClickPos(GetClickWorldPos());
if (tower != nullptr) { // there is a tower here
ImGui::OpenPopup("TowerUpgrade");
} else if (m_Client->getWorld().CanPlaceLittleTower(getClickWorldPos(), m_Client->getPlayer()->getID())) {
} else if (m_Client->GetWorld().CanPlaceLittleTower(GetClickWorldPos(), m_Client->GetPlayer()->GetID())) {
ImGui::OpenPopup("TowerPlace");
}
}
void WorldRenderer::setCamPos(float camX, float camY) {
void WorldRenderer::SetCamPos(float camX, float camY) {
m_CamPos = { camX, camY };
m_Renderer->setCamPos(m_CamPos);
m_Renderer->SetCamPos(m_CamPos);
}
void WorldRenderer::renderTowerUpgradePopup() {
void WorldRenderer::RenderTowerUpgradePopup() {
if (ImGui::BeginPopup("TowerUpgrade")) {
m_PopupOpened = true;
game::TowerPtr tower = m_Client->getWorld().getTower(getClickWorldPos());
game::TowerPtr tower = m_Client->GetWorld().GetTower(GetClickWorldPos());
if (tower == nullptr) {
ImGui::EndPopup();
return;
}
ImGui::Text("Tower : %s", game::TowerFactory::getTowerName(tower->getType()).c_str());
ImGui::Text("Tower : %s", game::TowerFactory::GetTowerName(tower->GetType()).c_str());
for (int y = 0; y < 3; y++) { // path: 0 -> top 1 -> middle 2 -> bottom
for (int x = 0; x < 4; x++) { // level: 1, 2, 3, 4
@@ -191,12 +191,12 @@ void WorldRenderer::renderTowerUpgradePopup() {
std::uint8_t currentLevel = x + 1;
game::TowerPath currentPath = game::TowerPath(y);
const game::TowerStats* towerStats = game::getTowerStats(tower->getType(), { currentLevel, currentPath });
game::TowerPath towerPath = tower->getLevel().getPath();
const game::TowerStats* towerStats = game::GetTowerStats(tower->GetType(), { currentLevel, currentPath });
game::TowerPath towerPath = tower->GetLevel().GetPath();
bool disabled = towerStats == nullptr;
int towerLevel = tower->getLevel().getLevel();
int towerLevel = tower->GetLevel().GetLevel();
bool alreadyUpgraded = currentLevel <= towerLevel;
bool canUpgrade = (towerLevel + 1) == currentLevel;
@@ -220,7 +220,7 @@ void WorldRenderer::renderTowerUpgradePopup() {
ImGui::EndDisabled();
} else if (canUpgrade) {
if (ImGui::Button("Upgrade", ImVec2(100, 100))) {
m_Client->getClient()->upgradeTower(tower->getID(), { currentLevel, currentPath });
m_Client->GetClient()->UpgradeTower(tower->GetID(), { currentLevel, currentPath });
}
} else {
ImGui::BeginDisabled();
@@ -235,71 +235,71 @@ void WorldRenderer::renderTowerUpgradePopup() {
}
}
void WorldRenderer::detectClick() {
void WorldRenderer::DetectClick() {
ImGuiIO& io = ImGui::GetIO();
if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) {
glm::vec2 cursorPos = { io.MousePos.x, io.MousePos.y };
if (cursorPos == m_HoldCursorPos) {
m_LastClicked = m_HoldCursorPos;
click();
Click();
}
}
}
void WorldRenderer::renderMobTooltip() const {
void WorldRenderer::RenderMobTooltip() const {
if (ImGui::IsAnyItemHovered()) return;
detectMobHovering();
m_MobTooltip->render();
DetectMobHovering();
m_MobTooltip->Render();
}
void WorldRenderer::renderCastleTooltip() const {
void WorldRenderer::RenderCastleTooltip() const {
if (ImGui::IsAnyItemHovered()) return;
detectCastleHovering();
m_CastleTooltip->render();
DetectCastleHovering();
m_CastleTooltip->Render();
}
void WorldRenderer::detectMobHovering() const {
glm::vec2 cursorWorldPos = getCursorWorldPos();
for (game::MobPtr mob : m_World->getMobList()) {
if (mob->collidesWith({ cursorWorldPos.x, cursorWorldPos.y })) {
m_MobTooltip->setMob(mob.get());
void WorldRenderer::DetectMobHovering() const {
glm::vec2 cursorWorldPos = GetCursorWorldPos();
for (game::MobPtr mob : m_World->GetMobList()) {
if (mob->CollidesWith({ cursorWorldPos.x, cursorWorldPos.y })) {
m_MobTooltip->SetMob(mob.get());
return;
}
}
m_MobTooltip->setMob(nullptr);
m_MobTooltip->SetMob(nullptr);
}
void WorldRenderer::detectCastleHovering() const {
glm::vec2 cursorWorldPos = getCursorWorldPos();
for (const game::Team& team : m_World->getTeams()) {
if (team.getCastle().collidesWith({ cursorWorldPos.x, cursorWorldPos.y })) {
m_CastleTooltip->setCastle(&team.getCastle());
void WorldRenderer::DetectCastleHovering() const {
glm::vec2 cursorWorldPos = GetCursorWorldPos();
for (const game::Team& team : m_World->GetTeams()) {
if (team.GetCastle().CollidesWith({ cursorWorldPos.x, cursorWorldPos.y })) {
m_CastleTooltip->SetCastle(&team.GetCastle());
return;
}
}
m_CastleTooltip->setCastle(nullptr);
m_CastleTooltip->SetCastle(nullptr);
}
void WorldRenderer::OnTowerAdd(game::TowerPtr tower) {
WorldLoader::RenderData renderData = WorldLoader::loadTowerModel(tower);
m_TowersCache.addData(tower->getID(), renderData.positions, renderData.colors);
m_TowersCache.updateVertexArray();
WorldLoader::RenderData RenderData = WorldLoader::LoadTowerModel(tower);
m_TowersCache.AddData(tower->GetID(), RenderData.positions, RenderData.colors);
m_TowersCache.UpdateVertexArray();
}
void WorldRenderer::OnTowerRemove(game::TowerPtr tower) {
m_TowersCache.removeData(tower->getID());
m_TowersCache.updateVertexArray();
m_TowersCache.RemoveData(tower->GetID());
m_TowersCache.UpdateVertexArray();
}
glm::vec2 WorldRenderer::getCursorWorldPos() const {
glm::vec2 WorldRenderer::GetCursorWorldPos() const {
ImGuiIO& io = ImGui::GetIO();
return m_Renderer->getCursorWorldPos({ io.MousePos.x, io.MousePos.y }, Display::getAspectRatio(), m_Zoom, Display::getWindowWidth(), Display::getWindowHeight());
return m_Renderer->GetCursorWorldPos({ io.MousePos.x, io.MousePos.y }, Display::GetAspectRatio(), m_Zoom, Display::GetWindowWidth(), Display::GetWindowHeight());
}
glm::vec2 WorldRenderer::getClickWorldPos() const {
return m_Renderer->getCursorWorldPos(m_LastClicked, Display::getAspectRatio(), m_Zoom, Display::getWindowWidth(), Display::getWindowHeight());
glm::vec2 WorldRenderer::GetClickWorldPos() const {
return m_Renderer->GetCursorWorldPos(m_LastClicked, Display::GetAspectRatio(), m_Zoom, Display::GetWindowWidth(), Display::GetWindowHeight());
}
} // namespace render

View File

@@ -12,14 +12,14 @@ CastleTooltip::CastleTooltip(client::Client* client) : GuiWidget(client) {
}
void CastleTooltip::render() {
void CastleTooltip::Render() {
if (m_Castle == nullptr) return;
ImGui::BeginTooltip();
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::getImGuiTeamColor(m_Castle->getTeam()->getColor()));
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::GetImGuiTeamColor(m_Castle->GetTeam()->GetColor()));
ImGui::Text("Castle : ");
ImGui::PopStyleColor();
ImGui::Text("\tCastle HP : %i/%i", static_cast<int>(m_Castle->getLife()), game::TeamCastle::CastleMaxLife);
ImGui::Text("\tCastle HP : %i/%i", static_cast<int>(m_Castle->GetLife()), game::TeamCastle::CastleMaxLife);
ImGui::EndTooltip();
}

View File

@@ -12,14 +12,14 @@ FrameMenu::FrameMenu(client::Client* client) : GuiWidget(client), m_VSync(true),
}
void FrameMenu::render() {
void FrameMenu::Render() {
ImGui::Begin("FPS Counter");
ImGui::Text("FPS : %i", (int)ImGui::GetIO().Framerate);
if (ImGui::Checkbox("V-Sync", &m_VSync)) {
SDL_GL_SetSwapInterval(m_VSync);
}
if (ImGui::Checkbox("Vue Isometrique ?", &m_IsometricView)) {
getClient()->getRenderer()->setIsometricView(m_IsometricView);
GetClient()->GetRenderer()->SetIsometricView(m_IsometricView);
}
#if !defined(NDEBUG)

View File

@@ -13,69 +13,69 @@ GameMenu::GameMenu(client::Client* client) : GuiWidget(client), m_SummonMenu(std
}
void GameMenu::render() {
if(!m_Client->isConnected()) return;
void GameMenu::Render() {
if (!m_Client->IsConnected()) return;
if (getClient()->getGame().getGameState() == td::game::GameState::Lobby) {
if (GetClient()->GetGame().GetGameState() == td::game::GameState::Lobby) {
ImGui::Begin("Lobby");
showTPS();
showPlayers();
showLobbyProgress();
showTeamSelection();
ShowTPS();
ShowPlayers();
ShowLobbyProgress();
ShowTeamSelection();
ImGui::End();
}
if (getClient()->getGame().getGameState() == td::game::GameState::Game) {
if (GetClient()->GetGame().GetGameState() == td::game::GameState::Game) {
ImGui::Begin("Game");
showTPS();
showStats();
showPlayers();
ShowTPS();
ShowStats();
ShowPlayers();
ImGui::End();
m_SummonMenu->render();
m_SummonMenu->Render();
}
}
void GameMenu::showPlayers() {
if (ImGui::TreeNode(std::string("Players (" + std::to_string(getClient()->getGame().getPlayers().size()) + ")##player_list").c_str())) {
for (auto pair : getClient()->getGame().getPlayers()) {
void GameMenu::ShowPlayers() {
if (ImGui::TreeNode(std::string("Players (" + std::to_string(GetClient()->GetGame().GetPlayers().size()) + ")##player_list").c_str())) {
for (auto pair : GetClient()->GetGame().GetPlayers()) {
const td::game::Player& player = pair.second;
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::getImGuiTeamColor(player.getTeamColor()));
ImGui::Text("%s", player.getName().c_str());
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::GetImGuiTeamColor(player.GetTeamColor()));
ImGui::Text("%s", player.GetName().c_str());
ImGui::PopStyleColor();
}
ImGui::TreePop();
}
}
void GameMenu::showTeamSelection() {
if (getClient()->getGame().getPlayer() == nullptr)
void GameMenu::ShowTeamSelection() {
if (GetClient()->GetGame().GetPlayer() == nullptr)
return;
td::game::TeamColor playerTeam = getClient()->getGame().getPlayer()->getTeamColor();
td::game::TeamColor playerTeam = GetClient()->GetGame().GetPlayer()->GetTeamColor();
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Red ? "Leave" : "Join") + std::string(" Red Team")).c_str())) {
if (playerTeam == td::game::TeamColor::Red)
getClient()->selectTeam(td::game::TeamColor::None);
GetClient()->SelectTeam(td::game::TeamColor::None);
else
getClient()->selectTeam(td::game::TeamColor::Red);
GetClient()->SelectTeam(td::game::TeamColor::Red);
}
ImGui::SameLine();
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Blue ? "Leave" : "Join") + std::string(" Blue Team")).c_str())) {
if (playerTeam == td::game::TeamColor::Blue)
getClient()->selectTeam(td::game::TeamColor::None);
GetClient()->SelectTeam(td::game::TeamColor::None);
else
getClient()->selectTeam(td::game::TeamColor::Blue);
GetClient()->SelectTeam(td::game::TeamColor::Blue);
}
}
void GameMenu::showLobbyProgress() {
const int timePassed = server::Lobby::LobbyWaitingTime - getClient()->getGame().getLobbyTime();
void GameMenu::ShowLobbyProgress() {
const int timePassed = server::Lobby::LobbyWaitingTime - GetClient()->GetGame().GetLobbyTime();
const float progress = (float)timePassed / (float)(server::Lobby::LobbyWaitingTime);
if (progress > 0 && progress < 1) {
ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f), std::string(std::to_string(getClient()->getGame().getLobbyTime() / 1000) + "s").c_str());
ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f), std::string(std::to_string(GetClient()->GetGame().GetLobbyTime() / 1000) + "s").c_str());
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
ImGui::Text("Time Remaining");
} else {
@@ -83,14 +83,14 @@ void GameMenu::showLobbyProgress() {
}
}
void GameMenu::showTPS() {
ImGui::Text("Server TPS : %.1f", getClient()->getConnexion().getServerTPS());
ImGui::Text("Server Ping : %i", getClient()->getConnexion().getServerPing());
void GameMenu::ShowTPS() {
ImGui::Text("Server TPS : %.1f", GetClient()->GetConnexion().GetServerTPS());
ImGui::Text("Server Ping : %i", GetClient()->GetConnexion().GetServerPing());
}
void GameMenu::showStats() {
ImGui::Text("Gold : %i", getClient()->getGame().getPlayer()->getGold());
ImGui::Text("EXP: %i", getClient()->getGame().getPlayer()->getExp());
void GameMenu::ShowStats() {
ImGui::Text("Gold : %i", GetClient()->GetGame().GetPlayer()->GetGold());
ImGui::Text("EXP: %i", GetClient()->GetGame().GetPlayer()->GetExp());
}
} // namespace gui

View File

@@ -14,15 +14,15 @@ MainMenu::MainMenu(client::Client* client) : GuiWidget(client), m_ConnectPort(25
MainMenu::~MainMenu() {
if (m_Server != nullptr)
m_Server->stop();
m_Server->Stop();
}
void MainMenu::render() {
if (m_Server != nullptr && !m_Server->isRunning()) {
void MainMenu::Render() {
if (m_Server != nullptr && !m_Server->IsRunning()) {
m_Server.reset(0); // destroying server if it stoped
}
if(m_Client->isConnected()) return;
if (m_Client->IsConnected()) return;
ImGui::Begin("Main Menu");
if (ImGui::Button("Rejoindre une partie##join")) {
@@ -39,7 +39,7 @@ void MainMenu::render() {
ImGui::InputText("Server Adress", &m_ConnectAddress.front(), m_ConnectAddress.capacity());
ImGui::InputInt("Port", &m_ConnectPort, -1);
if (ImGui::Button("Rejoindre")) {
getClient()->connect(td::network::Dns::Resolve(m_ConnectAddress), m_ConnectPort);
GetClient()->Connect(td::network::Dns::Resolve(m_ConnectAddress), m_ConnectPort);
m_TriedToConnect = true;
}
if (m_TriedToConnect) {
@@ -61,10 +61,10 @@ void MainMenu::render() {
m_WorldFilePath = m_FileDialog.selected_path;
}
if (ImGui::Button("Créer")) {
if (!startServer()) {
if (!StartServer()) {
m_TriedToCreate = true;
} else {
getClient()->connect(td::network::Dns::Resolve("localhost"), m_ServerPort);
GetClient()->Connect(td::network::Dns::Resolve("localhost"), m_ServerPort);
}
}
if (m_TriedToCreate)
@@ -77,11 +77,11 @@ void MainMenu::render() {
ImGui::End();
}
bool MainMenu::startServer() {
bool MainMenu::StartServer() {
if (m_WorldFilePath.empty())
return false;
m_Server = std::make_unique<td::server::Server>(m_WorldFilePath);
if (!m_Server->start(m_ServerPort)) {
if (!m_Server->Start(m_ServerPort)) {
return false;
}
return true;

View File

@@ -14,29 +14,29 @@ MobTooltip::MobTooltip(client::Client* client) : GuiWidget(client) {
}
void MobTooltip::render() {
void MobTooltip::Render() {
if (m_Mob == nullptr) return;
// TODO: add sender null check
const game::Player* sender = getClient()->getGame().getPlayerById(m_Mob->getSender());
const game::Player* sender = GetClient()->GetGame().GetPlayerById(m_Mob->GetSender());
ImGui::BeginTooltip();
ImGui::Text("Sender :");
ImGui::SameLine();
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::getImGuiTeamColor(sender->getTeamColor()));
ImGui::Text("%s", sender->getName().c_str());
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::GetImGuiTeamColor(sender->GetTeamColor()));
ImGui::Text("%s", sender->GetName().c_str());
ImGui::PopStyleColor();
ImGui::Text("Mob HP : %.1f/%i", m_Mob->getHealth(), m_Mob->getStats()->getMaxLife());
ImGui::Text("Mob Type : %s", game::MobFactory::getMobName(m_Mob->getType()).c_str());
ImGui::Text("Mob Level : %i", m_Mob->getLevel());
ImGui::Text("Mob HP : %.1f/%i", m_Mob->GetHealth(), m_Mob->GetStats()->GetMaxLife());
ImGui::Text("Mob Type : %s", game::MobFactory::GetMobName(m_Mob->GetType()).c_str());
ImGui::Text("Mob Level : %i", m_Mob->GetLevel());
ImGui::NewLine();
ImGui::Text("Mob Stats :");
ImGui::Text("\tMax health : %i", m_Mob->getStats()->getMaxLife());
ImGui::Text("\tSpeed : %.1f", m_Mob->getStats()->getMovementSpeed());
ImGui::Text("\tDamage : %.1f", m_Mob->getStats()->getDamage());
ImGui::Text("\tMoney cost : %i", m_Mob->getStats()->getMoneyCost());
ImGui::Text("\tEXP cost : %i", m_Mob->getStats()->getExpCost());
ImGui::Text("\tEXP reward : %i", m_Mob->getStats()->getExpReward());
ImGui::Text("\tMax health : %i", m_Mob->GetStats()->GetMaxLife());
ImGui::Text("\tSpeed : %.1f", m_Mob->GetStats()->GetMovementSpeed());
ImGui::Text("\tDamage : %.1f", m_Mob->GetStats()->GetDamage());
ImGui::Text("\tMoney cost : %i", m_Mob->GetStats()->GetMoneyCost());
ImGui::Text("\tEXP cost : %i", m_Mob->GetStats()->GetExpCost());
ImGui::Text("\tEXP reward : %i", m_Mob->GetStats()->GetExpReward());
ImGui::EndTooltip();
}

View File

@@ -9,7 +9,7 @@ SummonMenu::SummonMenu(client::Client* client) : GuiWidget(client), m_MenuOpened
m_Values.fill(0);
}
void SummonMenu::render() {
void SummonMenu::Render() {
if (m_MenuOpened) {
ImGui::Begin("Summon", &m_MenuOpened);
ImTextureID my_tex_id = ImGui::GetIO().Fonts->TexID;
@@ -25,7 +25,7 @@ void SummonMenu::render() {
ImGui::SameLine();
ImGui::PushID(i);
if (ImGui::InputInt("", m_Values.data() + i, 1, 10)) {
setSummonMax(i);
SetSummonMax(i);
}
ImGui::PopID();
}
@@ -43,7 +43,7 @@ void SummonMenu::render() {
ImGui::SameLine();
ImGui::PushID(i);
if (ImGui::InputInt("", m_Values.data() + i, 1, m_MobTypeCount)) {
setSummonMax(i);
SetSummonMax(i);
}
ImGui::PopID();
}
@@ -59,14 +59,14 @@ void SummonMenu::render() {
mobSent.push_back(mobSend);
}
}
m_Client->sendMobs(mobSent);
m_Client->SendMobs(mobSent);
m_Values.fill(0);
}
ImGui::End();
}
}
void SummonMenu::setSummonMax(int valueIndex) {
void SummonMenu::SetSummonMax(int valueIndex) {
int& value = m_Values[valueIndex];
value = std::max(0, value);
value = std::min(12, value);

View File

@@ -21,11 +21,11 @@
namespace td {
namespace render {
void TowerGui::initWidgets() {
m_GuiManager.addWidget(std::make_unique<td::gui::MainMenu>(m_Client.get()));
m_GuiManager.addWidget(std::make_unique<td::gui::GameMenu>(m_Client.get()));
m_GuiManager.addWidget(std::make_unique<td::gui::FrameMenu>(m_Client.get()));
m_GuiManager.addWidget(std::make_unique<td::gui::UpdateMenu>(m_Client.get()));
void TowerGui::InitWidgets() {
m_GuiManager.AddWidget(std::make_unique<td::gui::MainMenu>(m_Client.get()));
m_GuiManager.AddWidget(std::make_unique<td::gui::GameMenu>(m_Client.get()));
m_GuiManager.AddWidget(std::make_unique<td::gui::FrameMenu>(m_Client.get()));
m_GuiManager.AddWidget(std::make_unique<td::gui::UpdateMenu>(m_Client.get()));
}
TowerGui::TowerGui(SDL_Window* sdl_window, SDL_GLContext glContext, td::render::Renderer* renderer) : m_Window(sdl_window),
@@ -38,45 +38,45 @@ m_GlContext(glContext), m_Renderer(renderer), m_Client(std::make_unique<client::
ImFontConfig c;
c.SizePixels = 25;
ImGui::GetIO().Fonts->AddFontDefault(&c);
initWidgets();
InitWidgets();
}
void TowerGui::beginFrame() {
void TowerGui::BeginFrame() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
}
void TowerGui::endFrame() {
void TowerGui::EndFrame() {
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void TowerGui::tick() {
static std::uint64_t lastTime = td::utils::getTime();
std::uint64_t time = td::utils::getTime();
void TowerGui::Tick() {
static std::uint64_t lastTime = td::utils::GetTime();
std::uint64_t time = td::utils::GetTime();
std::uint64_t delta = time - lastTime;
m_Client->tick(delta);
m_Client->Tick(delta);
lastTime = td::utils::getTime();
lastTime = td::utils::GetTime();
}
void TowerGui::render() {
tick();
beginFrame();
void TowerGui::Render() {
Tick();
BeginFrame();
m_Client->render();
m_Client->Render();
m_GuiManager.renderWidgets();
m_GuiManager.RenderWidgets();
endFrame();
EndFrame();
}
TowerGui::~TowerGui() {
m_Client->closeConnection();
m_Client->CloseConnection();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();

View File

@@ -13,21 +13,21 @@ TowerPlacePopup::TowerPlacePopup(client::Client* client) : GuiWidget(client) {
}
void TowerPlacePopup::render() {
void TowerPlacePopup::Render() {
if (ImGui::BeginPopup("TowerPlace")) {
ImGui::BeginChild("TowerPlacePopupChild", ImVec2(800, m_TowerPopupTileHeight + 20), false, ImGuiWindowFlags_HorizontalScrollbar);
for (int i = 0; i < (int)game::TowerType::TowerCount; i++) {
if (i > 0) ImGui::SameLine();
game::TowerType towerType = game::TowerType(i);
const game::TowerInfo& towerInfo = game::getTowerInfo(towerType);
const game::TowerInfo& towerInfo = game::GetTowerInfo(towerType);
if (!towerInfo.isBigTower() || (towerInfo.isBigTower() &&
getClient()->getGame().getWorld().CanPlaceBigTower(m_ClickWorldPos, getClient()->getGame().getPlayer()->getID()))) {
if (!towerInfo.IsBigTower() || (towerInfo.IsBigTower() &&
GetClient()->GetGame().GetWorld().CanPlaceBigTower(m_ClickWorldPos, GetClient()->GetGame().GetPlayer()->GetID()))) {
ImGui::BeginChild(std::to_string(i).c_str(), ImVec2(m_TowerPopupTileWidth, m_TowerPopupTileHeight), true);
ImGui::Text(towerInfo.getName().c_str());
ImGui::Text(towerInfo.GetName().c_str());
ImGui::SameLine();
ImGui::SetCursorPosX(m_TowerPopupTileWidth - 10 - ImGui::CalcTextSize("(?)").x);
@@ -35,7 +35,7 @@ void TowerPlacePopup::render() {
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text(towerInfo.getDescription().c_str());
ImGui::Text(towerInfo.GetDescription().c_str());
ImGui::EndTooltip();
}
@@ -45,7 +45,7 @@ void TowerPlacePopup::render() {
ImGui::SetCursorPosX(m_TowerPopupTileWidth / 2.0f - m_PlaceTowerButtonWidth / 2.0f);
if (ImGui::Button(buyText.c_str(), ImVec2(m_PlaceTowerButtonWidth, m_PlaceTowerButtonHeight))) {
getClient()->placeTower(towerType, m_ClickWorldPos);
GetClient()->PlaceTower(towerType, m_ClickWorldPos);
ImGui::CloseCurrentPopup();
}
@@ -57,7 +57,7 @@ void TowerPlacePopup::render() {
}
}
void TowerPlacePopup::setClickPos(const glm::vec2& worldPos) {
void TowerPlacePopup::SetClickPos(const glm::vec2& worldPos) {
m_ClickWorldPos = worldPos;
}

View File

@@ -8,52 +8,52 @@ namespace td {
namespace gui {
UpdateMenu::UpdateMenu(client::Client* client) : GuiWidget(client), m_Opened(true) {
checkUpdates();
CheckUpdates();
}
void UpdateMenu::render() {
renderErrorPopup();
void UpdateMenu::Render() {
RenderErrorPopup();
if (m_Opened) {
ImGui::Begin("Updater", &m_Opened);
if (isUpdateChecked()) {
if (IsUpdateChecked()) {
bool updateAvailable = m_UpdateAvailable.get();
if (updateAvailable) {
if (m_Updater.isFileWrited()) {
if (m_Updater.IsFileWrited()) {
ImGui::Text("The update is now installed");
ImGui::Text("The game needs to be restarted");
} else if (m_Updater.isDownloadComplete()) {
} else if (m_Updater.IsDownloadComplete()) {
ImGui::Text("Download done!");
if (ImGui::Button("Install")) {
if (!m_Updater.writeFile()) {
if (!m_Updater.WriteFile()) {
m_Error = "Failed to write file !\n";
ImGui::OpenPopup("UpdateError");
}
}
if (ImGui::Button("Cancel")) {
m_Updater.cancelDownload();
m_Updater.clearCache();
m_Updater.CancelDownload();
m_Updater.ClearCache();
}
} else {
if (m_Updater.getDownloadProgress() > 0) {
if (m_Updater.GetDownloadProgress() > 0) {
ImGui::Text("Downloading ...");
ImGui::ProgressBar(m_Updater.getDownloadProgress());
ImGui::ProgressBar(m_Updater.GetDownloadProgress());
if (ImGui::Button("Cancel")) {
m_Updater.cancelDownload();
m_Updater.CancelDownload();
}
} else {
ImGui::Text("An update is available!");
ImGui::Separator();
ImGui::Text("Current version : %s", m_Updater.getCurrentVersion().c_str());
ImGui::Text("Last version : %s", m_Updater.getLastVersion().c_str());
ImGui::Text("Current version : %s", m_Updater.GetCurrentVersion().c_str());
ImGui::Text("Last version : %s", m_Updater.GetLastVersion().c_str());
bool canDownloadFile = m_Updater.canUpdate();
bool canDownloadFile = m_Updater.CanUpdate();
if (!canDownloadFile) ImGui::BeginDisabled();
if (ImGui::Button("Download")) {
m_Updater.downloadUpdate();
m_Updater.DownloadUpdate();
}
if (!canDownloadFile) ImGui::EndDisabled();
@@ -67,8 +67,8 @@ void UpdateMenu::render() {
} else {
ImGui::Text("No update available!");
ImGui::Separator();
ImGui::Text("Current version : %s", m_Updater.getCurrentVersion().c_str());
ImGui::Text("Last version : %s", m_Updater.getLastVersion().c_str());
ImGui::Text("Current version : %s", m_Updater.GetCurrentVersion().c_str());
ImGui::Text("Last version : %s", m_Updater.GetLastVersion().c_str());
}
} else {
ImGui::Text("Checking updates ...");
@@ -77,19 +77,19 @@ void UpdateMenu::render() {
}
}
void UpdateMenu::renderErrorPopup() {
void UpdateMenu::RenderErrorPopup() {
if (ImGui::BeginPopup("UpdateError")) {
ImGui::Text("Error : %s", m_Error.c_str());
ImGui::EndPopup();
}
}
bool UpdateMenu::isUpdateChecked() {
bool UpdateMenu::IsUpdateChecked() {
return m_UpdateAvailable.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
}
void UpdateMenu::checkUpdates() {
m_UpdateAvailable = std::async(std::launch::async, [&]() { return m_Updater.checkUpdate();});
void UpdateMenu::CheckUpdates() {
m_UpdateAvailable = std::async(std::launch::async, [&]() { return m_Updater.CheckUpdate();});
}
} // namespace gui

View File

@@ -20,17 +20,17 @@ VertexArray::VertexArray(unsigned int vertexCount) : m_VertexCount(vertexCount)
glGenVertexArrays(1, &m_ID);
}
void VertexArray::bind() const {
void VertexArray::Bind() const {
glBindVertexArray(m_ID);
}
void VertexArray::unbind() const {
void VertexArray::Unbind() const {
glBindVertexArray(0);
}
void VertexArray::bindVertexBuffer(VertexBuffer& VertexBuffer) {
VertexBuffer.bind();
VertexBuffer.bindVertexAttribs();
void VertexArray::BindVertexBuffer(VertexBuffer& VertexBuffer) {
VertexBuffer.Bind();
VertexBuffer.BindVertexAttribs();
m_VertexBuffers.push_back(std::move(VertexBuffer));
}
@@ -41,21 +41,21 @@ VertexBuffer::~VertexBuffer() {
VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) {
glGenBuffers(1, &m_ID);
bind();
Bind();
glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(data.size() * sizeof(float)), data.data());
unbind();
Unbind();
}
void VertexBuffer::bind() const {
void VertexBuffer::Bind() const {
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
}
void VertexBuffer::unbind() const {
void VertexBuffer::Unbind() const {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void VertexBuffer::addVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset) {
void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset) {
VertexAttribPointer pointer;
pointer.m_Index = index;
pointer.m_Size = coordinateSize;
@@ -63,7 +63,7 @@ void VertexBuffer::addVertexAttribPointer(unsigned int index, unsigned int coord
m_VertexAttribs.push_back(pointer);
}
void VertexBuffer::bindVertexAttribs() const {
void VertexBuffer::BindVertexAttribs() const {
for (const VertexAttribPointer& pointer : m_VertexAttribs) {
glEnableVertexAttribArray(pointer.m_Index);
glVertexAttribPointer(pointer.m_Index, static_cast<GLint>(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float), reinterpret_cast<void*>(pointer.m_Offset));

View File

@@ -13,7 +13,7 @@
namespace TextureLoader {
unsigned int loadGLTexture(const char* fileName) {
unsigned int LoadGLTexture(const char* fileName) {
int width, height, comp;

View File

@@ -10,7 +10,7 @@ namespace render {
namespace WorldLoader {
GL::VertexArray loadMobModel() {
GL::VertexArray LoadMobModel() {
std::vector<float> positions = {
-0.5, -0.5,
0.5, -0.5,
@@ -36,23 +36,23 @@ GL::VertexArray loadMobModel() {
};
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.addVertexAttribPointer(0, 2, 0);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
colorVBO.addVertexAttribPointer(1, 1, 0);
colorVBO.AddVertexAttribPointer(1, 1, 0);
GL::VertexArray mobVao(colors.size()); // each pos = 1 color
mobVao.bind();
mobVao.bindVertexBuffer(positionVBO);
mobVao.bindVertexBuffer(colorVBO);
mobVao.unbind();
mobVao.Bind();
mobVao.BindVertexBuffer(positionVBO);
mobVao.BindVertexBuffer(colorVBO);
mobVao.Unbind();
return mobVao;
}
GL::VertexArray loadWorldModel(const td::game::World* world) {
GL::VertexArray LoadWorldModel(const td::game::World* world) {
std::vector<float> positions;
std::vector<float> colors;
for (const auto& chunkInfo : world->getChunks()) {
for (const auto& chunkInfo : world->GetChunks()) {
const td::game::ChunkCoord& coords = chunkInfo.first;
td::game::ChunkPtr chunk = chunkInfo.second;
@@ -62,8 +62,8 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
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);
td::game::TileIndex tileIndex = chunk->GetTileIndex(tileNumber);
td::game::TilePtr tile = world->GetTilePtr(tileIndex);
if (tile == nullptr)
continue;
@@ -78,7 +78,7 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
(float)chunkX + tileX + 1, (float)chunkY + tileY + 1,
});
const td::game::Color* tileColor = world->getTileColor(tile);
const td::game::Color* tileColor = world->GetTileColor(tile);
for (int i = 0; i < 6; i++) {
int color = 255;
@@ -96,9 +96,9 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
}
for (int spawnColor = 0; spawnColor < 2; spawnColor++) {
const game::Spawn& spawn = world->getTeam(game::TeamColor(spawnColor)).getSpawn();
float fromX = spawn.getTopLeft().getX(), toX = spawn.getBottomRight().getX();
float fromY = spawn.getTopLeft().getY(), toY = spawn.getBottomRight().getY();
const game::Spawn& spawn = world->GetTeam(game::TeamColor(spawnColor)).GetSpawn();
float fromX = spawn.GetTopLeft().GetX(), toX = spawn.GetBottomRight().GetX();
float fromY = spawn.GetTopLeft().GetY(), toY = spawn.GetBottomRight().GetY();
positions.insert(positions.end(), {
fromX, fromY,
@@ -112,9 +112,9 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
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;
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);
@@ -124,9 +124,9 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
}
for (int castleColor = 0; castleColor < 2; castleColor++) {
const game::TeamCastle& castle = world->getTeam(game::TeamColor(castleColor)).getCastle();
float fromX = castle.getTopLeft().getX(), toX = castle.getBottomRight().getX();
float fromY = castle.getTopLeft().getY(), toY = castle.getBottomRight().getY();
const game::TeamCastle& castle = world->GetTeam(game::TeamColor(castleColor)).GetCastle();
float fromX = castle.GetTopLeft().GetX(), toX = castle.GetBottomRight().GetX();
float fromY = castle.GetTopLeft().GetY(), toY = castle.GetBottomRight().GetY();
positions.insert(positions.end(), {
fromX, fromY,
@@ -140,9 +140,9 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
for (int i = 0; i < 6; i++) {
int color = 255;
color |= world->getSpawnColor(game::TeamColor(castleColor)).r << 24;
color |= world->getSpawnColor(game::TeamColor(castleColor)).g << 16;
color |= world->getSpawnColor(game::TeamColor(castleColor)).b << 8;
color |= world->GetSpawnColor(game::TeamColor(castleColor)).r << 24;
color |= world->GetSpawnColor(game::TeamColor(castleColor)).g << 16;
color |= world->GetSpawnColor(game::TeamColor(castleColor)).b << 8;
int newColorIndex = colors.size();
colors.push_back(0);
@@ -152,19 +152,19 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
}
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.addVertexAttribPointer(0, 2, 0);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
colorVBO.addVertexAttribPointer(1, 1, 0);
colorVBO.AddVertexAttribPointer(1, 1, 0);
GL::VertexArray worldVao(positions.size() / 2); // each pos = 2 vertecies
worldVao.bind();
worldVao.bindVertexBuffer(positionVBO);
worldVao.bindVertexBuffer(colorVBO);
worldVao.unbind();
worldVao.Bind();
worldVao.BindVertexBuffer(positionVBO);
worldVao.BindVertexBuffer(colorVBO);
worldVao.Unbind();
return worldVao;
}
GL::VertexArray loadTileSelectModel() {
GL::VertexArray LoadTileSelectModel() {
std::vector<float> positions = {
0, 0,
1, 0,
@@ -183,37 +183,37 @@ GL::VertexArray loadTileSelectModel() {
std::vector<float> colors(6, colorFloat);
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.addVertexAttribPointer(0, 2, 0);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
colorVBO.addVertexAttribPointer(1, 1, 0);
colorVBO.AddVertexAttribPointer(1, 1, 0);
GL::VertexArray tileSelectVao(positions.size() / 2); // each pos = 2 vertecies
tileSelectVao.bind();
tileSelectVao.bindVertexBuffer(positionVBO);
tileSelectVao.bindVertexBuffer(colorVBO);
tileSelectVao.unbind();
tileSelectVao.Bind();
tileSelectVao.BindVertexBuffer(positionVBO);
tileSelectVao.BindVertexBuffer(colorVBO);
tileSelectVao.Unbind();
return tileSelectVao;
}
RenderData loadTowerModel(game::TowerPtr tower) {
RenderData LoadTowerModel(game::TowerPtr tower) {
RenderData renderData;
float towerX, towerDX;
float towerY, towerDY;
if (tower->getSize() == game::TowerSize::Little) {
towerX = tower->getCenterX() - 1.5f;
towerDX = tower->getCenterX() + 1.5f;
if (tower->GetSize() == game::TowerSize::Little) {
towerX = tower->GetCenterX() - 1.5f;
towerDX = tower->GetCenterX() + 1.5f;
towerY = tower->getCenterY() - 1.5f;
towerDY = tower->getCenterY() + 1.5f;
towerY = tower->GetCenterY() - 1.5f;
towerDY = tower->GetCenterY() + 1.5f;
} else {
towerX = tower->getCenterX() - 2.5f;
towerDX = tower->getCenterX() + 2.5f;
towerX = tower->GetCenterX() - 2.5f;
towerDX = tower->GetCenterX() + 2.5f;
towerY = tower->getCenterY() - 2.5f;
towerDY = tower->getCenterY() + 2.5f;
towerY = tower->GetCenterY() - 2.5f;
towerDY = tower->GetCenterY() + 2.5f;
}
std::vector<float> positions = {
towerX, towerY,
@@ -227,7 +227,7 @@ RenderData loadTowerModel(game::TowerPtr tower) {
renderData.positions = positions;
std::uint8_t towerType = static_cast<std::uint8_t>(tower->getType());
std::uint8_t towerType = static_cast<std::uint8_t>(tower->GetType());
std::uint8_t r = 10 * towerType + 40, g = 5 * towerType + 30, b = 10 * towerType + 20;
float colorFloat;

View File

@@ -1,5 +1,8 @@
#include "render/shaders/EntityShader.h"
namespace td {
namespace shader {
#ifdef __ANDROID__
static const char vertexSource[] =
R"(#version 300 es
@@ -92,30 +95,34 @@ void main(void){
EntityShader::EntityShader() : ShaderProgram() {}
void EntityShader::loadShader() {
ShaderProgram::loadProgram(vertexSource, fragmentSource);
void EntityShader::LoadShader() {
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
}
void EntityShader::getAllUniformLocation() {
location_aspect_ratio = static_cast<unsigned int>(getUniformLocation("aspectRatio"));
location_zoom = static_cast<unsigned int>(getUniformLocation("zoom"));
location_cam = static_cast<unsigned int>(getUniformLocation("camPos"));
location_translation = static_cast<unsigned int>(getUniformLocation("translation"));
location_viewtype = static_cast<unsigned int>(getUniformLocation("isometricView"));
void EntityShader::GetAllUniformLocation() {
m_LocationAspectRatio = static_cast<unsigned int>(GetUniformLocation("aspectRatio"));
m_LocationZoom = static_cast<unsigned int>(GetUniformLocation("zoom"));
m_LocationCam = static_cast<unsigned int>(GetUniformLocation("camPos"));
m_LocationTranslation = static_cast<unsigned int>(GetUniformLocation("translation"));
m_LocationViewtype = static_cast<unsigned int>(GetUniformLocation("isometricView"));
}
void EntityShader::setCamPos(const glm::vec2& camPos) {
loadVector(location_cam, camPos);
void EntityShader::SetCamPos(const glm::vec2& camPos) {
LoadVector(m_LocationCam, camPos);
}
void EntityShader::setZoom(float zoom) {
loadFloat(location_zoom, zoom);
void EntityShader::SetZoom(float zoom) {
LoadFloat(m_LocationZoom, zoom);
}
void EntityShader::setAspectRatio(float aspectRatio) {
loadFloat(location_aspect_ratio, aspectRatio);
void EntityShader::SetAspectRatio(float aspectRatio) {
LoadFloat(m_LocationAspectRatio, aspectRatio);
}
void EntityShader::setModelPos(const glm::vec2& modelPos) {
loadVector(location_translation, modelPos);
void EntityShader::SetModelPos(const glm::vec2& modelPos) {
LoadVector(m_LocationTranslation, modelPos);
}
void EntityShader::setIsometricView(float isometric) {
loadFloat(location_viewtype, isometric);
void EntityShader::SetIsometricView(float isometric) {
LoadFloat(m_LocationViewtype, isometric);
}
} // namespace shader
} // namespace td

View File

@@ -17,23 +17,26 @@
#include <android/log.h>
#endif
namespace td {
namespace shader {
ShaderProgram::ShaderProgram() :
m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {
}
ShaderProgram::~ShaderProgram() {
cleanUp();
CleanUp();
}
void ShaderProgram::start() const {
void ShaderProgram::Start() const {
glUseProgram(m_ProgramID);
}
void ShaderProgram::stop() const {
void ShaderProgram::Stop() const {
glUseProgram(0);
}
int ShaderProgram::getUniformLocation(const std::string& uniformName) const {
int ShaderProgram::GetUniformLocation(const std::string& uniformName) const {
const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
if (location == -1) {
std::cout << "Warning ! Uniform variable " << uniformName << " not found !\n";
@@ -41,39 +44,39 @@ int ShaderProgram::getUniformLocation(const std::string& uniformName) const {
return location;
}
void ShaderProgram::loadFloat(unsigned int location, float value) const {
void ShaderProgram::LoadFloat(unsigned int location, float value) const {
glUniform1f(static_cast<GLint>(location), value);
}
void ShaderProgram::loadInt(unsigned int location, int value) const {
void ShaderProgram::LoadInt(unsigned int location, int value) const {
glUniform1i(static_cast<GLint>(location), value);
}
void ShaderProgram::loadVector(unsigned int location,
void ShaderProgram::LoadVector(unsigned int location,
const glm::vec2& vector) const {
glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
}
void ShaderProgram::loadVector(unsigned int location,
void ShaderProgram::LoadVector(unsigned int location,
const glm::vec3& vector) const {
glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
}
void ShaderProgram::loadVector(unsigned int location,
void ShaderProgram::LoadVector(unsigned int location,
const glm::vec4& vector) const {
glUniform4f(static_cast<GLint>(location), vector.x, vector.y, vector.z, vector.w);
}
void ShaderProgram::loadBoolean(unsigned int location, bool value) const {
void ShaderProgram::LoadBoolean(unsigned int location, bool value) const {
glUniform1i(static_cast<GLint>(location), value);
}
void ShaderProgram::loadMatrix(unsigned int location, const glm::mat4& matrix) const {
void ShaderProgram::LoadMatrix(unsigned int location, const glm::mat4& matrix) const {
glUniformMatrix4fv(static_cast<GLint>(location), 1, false, glm::value_ptr(matrix));
}
void ShaderProgram::cleanUp() const {
stop();
void ShaderProgram::CleanUp() const {
Stop();
glDetachShader(m_ProgramID, m_VertexShaderID);
glDetachShader(m_ProgramID, m_FragmentShaderID);
glDeleteShader(m_VertexShaderID);
@@ -81,31 +84,31 @@ void ShaderProgram::cleanUp() const {
glDeleteProgram(m_ProgramID);
}
void ShaderProgram::loadProgramFile(const std::string& vertexFile,
void ShaderProgram::LoadProgramFile(const std::string& vertexFile,
const std::string& fragmentFile) {
m_VertexShaderID = static_cast<unsigned int>(loadShaderFromFile(vertexFile, GL_VERTEX_SHADER));
m_FragmentShaderID = static_cast<unsigned int>(loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER));
m_VertexShaderID = static_cast<unsigned int>(LoadShaderFromFile(vertexFile, GL_VERTEX_SHADER));
m_FragmentShaderID = static_cast<unsigned int>(LoadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER));
m_ProgramID = glCreateProgram();
glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(m_ProgramID);
glValidateProgram(m_ProgramID);
getAllUniformLocation();
GetAllUniformLocation();
}
void ShaderProgram::loadProgram(const std::string& vertexSource,
void ShaderProgram::LoadProgram(const std::string& vertexSource,
const std::string& fragmentSource) {
m_VertexShaderID = static_cast<unsigned int>(loadShader(vertexSource, GL_VERTEX_SHADER));
m_FragmentShaderID = static_cast<unsigned int>(loadShader(fragmentSource, GL_FRAGMENT_SHADER));
m_VertexShaderID = static_cast<unsigned int>(LoadShader(vertexSource, GL_VERTEX_SHADER));
m_FragmentShaderID = static_cast<unsigned int>(LoadShader(fragmentSource, GL_FRAGMENT_SHADER));
m_ProgramID = glCreateProgram();
glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(m_ProgramID);
glValidateProgram(m_ProgramID);
getAllUniformLocation();
GetAllUniformLocation();
}
unsigned int ShaderProgram::loadShader(const std::string& source, GLenum type) {
unsigned int ShaderProgram::LoadShader(const std::string& source, GLenum type) {
unsigned int shaderID = glCreateShader(type);
const char* c_str = source.c_str();
@@ -128,15 +131,18 @@ unsigned int ShaderProgram::loadShader(const std::string& source, GLenum type) {
return shaderID;
}
unsigned int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) {
unsigned int ShaderProgram::LoadShaderFromFile(const std::string& file, GLenum type) {
std::stringstream stream;
std::ifstream fileStream(file);
if(fileStream) {
if (fileStream) {
stream << fileStream.rdbuf();
} else {
return 0;
}
return loadShader(stream.str(), type);
return LoadShader(stream.str(), type);
}
} // namespace shader
} // namespace td

View File

@@ -1,12 +1,8 @@
/*
* WorldShader.cpp
*
* Created on: 4 nov. 2020
* Author: simon
*/
#include "render/shaders/WorldShader.h"
namespace td {
namespace shader {
#ifdef __ANDROID__
static const char vertexSource[] =
R"(#version 300 es
@@ -93,26 +89,29 @@ void main(void){
WorldShader::WorldShader() : ShaderProgram() {}
void WorldShader::loadShader() {
ShaderProgram::loadProgram(vertexSource, fragmentSource);
void WorldShader::LoadShader() {
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
}
void WorldShader::getAllUniformLocation() {
location_aspect_ratio = static_cast<unsigned int>(getUniformLocation("aspectRatio"));
location_zoom = static_cast<unsigned int>(getUniformLocation("zoom"));
location_cam = static_cast<unsigned int>(getUniformLocation("camPos"));
location_viewtype = static_cast<unsigned int>(getUniformLocation("isometricView"));
void WorldShader::GetAllUniformLocation() {
m_LocationAspectRatio = static_cast<unsigned int>(GetUniformLocation("aspectRatio"));
m_LocationZoom = static_cast<unsigned int>(GetUniformLocation("zoom"));
m_LocationCam = static_cast<unsigned int>(GetUniformLocation("camPos"));
m_LocationViewtype = static_cast<unsigned int>(GetUniformLocation("isometricView"));
}
void WorldShader::setCamPos(const glm::vec2& camPos) {
loadVector(location_cam, camPos);
void WorldShader::SetCamPos(const glm::vec2& camPos) {
LoadVector(m_LocationCam, camPos);
}
void WorldShader::setZoom(float zoom) {
loadFloat(location_zoom, zoom);
void WorldShader::SetZoom(float zoom) {
LoadFloat(m_LocationZoom, zoom);
}
void WorldShader::setAspectRatio(float aspectRatio) {
loadFloat(location_aspect_ratio, aspectRatio);
void WorldShader::SetAspectRatio(float aspectRatio) {
LoadFloat(m_LocationAspectRatio, aspectRatio);
}
void WorldShader::setIsometricView(float isometric) {
loadFloat(location_viewtype, isometric);
void WorldShader::SetIsometricView(float isometric) {
LoadFloat(m_LocationViewtype, isometric);
}
} // namespace shader
} // namespace td