indent with tabs

This commit is contained in:
2023-01-02 13:05:43 +01:00
parent 8f95b1a750
commit 222b79b40a
100 changed files with 4783 additions and 4781 deletions

View File

@@ -23,27 +23,27 @@ 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);
float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
m_WorldShader->Start();
m_WorldShader->SetIsometricView(isometricEased);
m_EntityShader->Start();
m_EntityShader->SetIsometricView(isometricEased);
}
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();
m_WorldShader = std::make_unique<shader::WorldShader>();
m_WorldShader->LoadShader();
m_EntityShader = std::make_unique<shader::EntityShader>();
m_EntityShader->LoadShader();
SetIsometricView(true);
UpdateIsometricView();
}
// TODO : change loader check
bool Renderer::Init() {
#if __has_include(<glbinding/glbinding.h>)
glbinding::initialize();
glbinding::initialize();
#elif __has_include(<GL/glew.h>)
glewInit();
#elif __has_include(<glad/glad.h>)
@@ -53,96 +53,96 @@ bool Renderer::Init() {
#elif __has_include(<glbinding/Binding.h>)
glbinding::Binding::initialize();
#endif
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
InitShaders();
return true;
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
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();
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();
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();
if (m_IsometricShade != static_cast<float>(m_IsometricView)) {
float step = static_cast<float>(utils::GetTime() - lastTime) / 1000.0f * m_AnimationSpeed;
if (m_IsometricShade < m_IsometricView) {
m_IsometricShade += step;
} else {
m_IsometricShade -= step;
}
m_IsometricShade = std::min(m_IsometricShade, 1.0f);
m_IsometricShade = std::max(m_IsometricShade, 0.0f);
UpdateIsometricView();
}
lastTime = utils::GetTime();
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;
if (m_IsometricShade < m_IsometricView) {
m_IsometricShade += step;
} else {
m_IsometricShade -= step;
}
m_IsometricShade = std::min(m_IsometricShade, 1.0f);
m_IsometricShade = std::max(m_IsometricShade, 0.0f);
UpdateIsometricView();
}
lastTime = utils::GetTime();
}
void Renderer::Prepare() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0);
UpdateIsometricFade();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0);
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);
glViewport(0, 0, width, 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);
m_WorldShader->Start();
m_WorldShader->SetZoom(zoom);
m_EntityShader->Start();
m_EntityShader->SetZoom(zoom);
}
void Renderer::SetCamMovement(const Vec2f& 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);
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 Renderer::SetCamPos(const Vec2f& newPos) {
m_CamPos = newPos;
m_WorldShader->Start();
m_WorldShader->SetCamPos(newPos);
m_EntityShader->Start();
m_EntityShader->SetCamPos(newPos);
m_CamPos = newPos;
m_WorldShader->Start();
m_WorldShader->SetCamPos(newPos);
m_EntityShader->Start();
m_EntityShader->SetCamPos(newPos);
}
void Renderer::SetIsometricView(bool isometric) {
m_IsometricView = isometric;
m_IsometricView = isometric;
}
Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight) {
float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
float relativeX = (cursorPos.x / windowWidth * 2) - 1;
float relativeY = (cursorPos.y / windowHeight * 2) - 1;
float relativeX = (cursorPos.x / windowWidth * 2) - 1;
float relativeY = (cursorPos.y / windowHeight * 2) - 1;
float deltaX = relativeX * aspectRatio / zoom;
float deltaY = relativeY / zoom;
float deltaX = relativeX * aspectRatio / zoom;
float deltaY = relativeY / zoom;
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;
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 };
return { worldX, worldY };
}

View File

@@ -5,49 +5,49 @@ namespace td {
namespace render {
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
m_Indexes.insert({ index, {positions, colors} });
m_VertexCount += colors.size(); // one color per vertex
}
void VertexCache::RemoveData(std::uint64_t index) {
auto it = m_Indexes.find(index);
if (it != m_Indexes.end()) {
m_Indexes.erase(it);
m_VertexCount -= it->second.color.size(); // one color per vertex
}
auto it = m_Indexes.find(index);
if (it != m_Indexes.end()) {
m_Indexes.erase(it);
m_VertexCount -= it->second.color.size(); // one color per vertex
}
}
void VertexCache::Clear() {
m_Indexes.clear();
m_VertexCount = 0;
m_Indexes.clear();
m_VertexCount = 0;
}
void VertexCache::UpdateVertexArray() {
m_VertexArray = std::make_unique<GL::VertexArray>(m_VertexCount); // one color per vertex
m_VertexArray = std::make_unique<GL::VertexArray>(m_VertexCount); // one color per vertex
Vector positions;
positions.reserve(m_VertexCount * 2);
Vector positions;
positions.reserve(m_VertexCount * 2);
Vector colors;
colors.reserve(m_VertexCount);
Vector colors;
colors.reserve(m_VertexCount);
for (auto it = m_Indexes.begin(); it != m_Indexes.end(); it++) {
const DataIndex& data = it->second;
for (auto it = m_Indexes.begin(); it != m_Indexes.end(); it++) {
const DataIndex& data = it->second;
positions.insert(positions.end(), data.position.begin(), data.position.end());
colors.insert(colors.end(), data.color.begin(), data.color.end());
}
positions.insert(positions.end(), data.position.begin(), data.position.end());
colors.insert(colors.end(), data.color.begin(), data.color.end());
}
GL::VertexBuffer positionsBuffer(positions, 2);
positionsBuffer.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer positionsBuffer(positions, 2);
positionsBuffer.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorsBuffer(colors, 1);
colorsBuffer.AddVertexAttribPointer(1, 1, 0);
GL::VertexBuffer colorsBuffer(colors, 1);
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,118 +12,118 @@ namespace td {
namespace render {
ImVec4 WorldRenderer::GetImGuiTeamColor(game::TeamColor color) {
switch (color) {
case td::game::TeamColor::None:
break;
case td::game::TeamColor::Red:
return ImVec4(1, 0, 0, 1);
case td::game::TeamColor::Blue:
return ImVec4(0, 0, 1, 1);
}
return ImVec4(1, 1, 1, 1);
switch (color) {
case td::game::TeamColor::None:
break;
case td::game::TeamColor::Red:
return ImVec4(1, 0, 0, 1);
case td::game::TeamColor::Blue:
return ImVec4(0, 0, 1, 1);
}
return ImVec4(1, 1, 1, 1);
}
void WorldRenderer::LoadModels() {
utils::LOGD("World Created !");
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()));
utils::LOGD(utils::format("Vertex Count : %u", m_WorldVao->GetVertexCount()));
utils::LOGD("World Created !");
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()));
utils::LOGD(utils::format("Vertex Count : %u", m_WorldVao->GetVertexCount()));
}
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);
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();
m_CursorPos = GetCursorWorldPos();
}
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());
}
if (io.MouseWheel != 0) {
ChangeZoom(io.MouseWheel);
}
UpdateCursorPos();
if (ImGui::IsMouseClicked(0)) {
if (!m_PopupOpened) {
m_HoldCursorPos = { io.MousePos.x, io.MousePos.y };
} else {
m_PopupOpened = false;
}
}
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());
}
if (io.MouseWheel != 0) {
ChangeZoom(io.MouseWheel);
}
UpdateCursorPos();
if (ImGui::IsMouseClicked(0)) {
if (!m_PopupOpened) {
m_HoldCursorPos = { io.MousePos.x, io.MousePos.y };
} else {
m_PopupOpened = false;
}
}
if (ImGui::IsMouseDoubleClicked(1)) RemoveTower();
if (ImGui::IsMouseDoubleClicked(1)) RemoveTower();
}
void WorldRenderer::RemoveTower() {
Vec2f cursorPos = GetCursorWorldPos();
Vec2f 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());
}
if (clickedTower != nullptr) {
m_Client->GetClient()->RemoveTower(clickedTower->GetID());
}
}
void WorldRenderer::RenderWorld() const {
m_Renderer->RenderVAO(*m_WorldVao);
m_Renderer->RenderVAO(*m_WorldVao);
}
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);
}
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);
}
}
void WorldRenderer::RenderTowers() const {
if (!m_TowersCache.IsEmpty())
m_Renderer->RenderVAO(m_TowersCache.GetVertexArray());
if (!m_TowersCache.IsEmpty())
m_Renderer->RenderVAO(m_TowersCache.GetVertexArray());
}
void WorldRenderer::RenderTileSelect() const {
if (ImGui::IsAnyItemHovered()) return;
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 = { std::floor(m_CursorPos.x), std::floor(m_CursorPos.y) };
Renderer::Model tileSelectModel;
tileSelectModel.vao = m_SelectTileVao.get();
tileSelectModel.positon = { std::floor(m_CursorPos.x), std::floor(m_CursorPos.y) };
m_Renderer->RenderModel(tileSelectModel);
m_Renderer->RenderModel(tileSelectModel);
}
void WorldRenderer::RenderPopups() {
m_TowerPlacePopup->Render();
RenderTowerUpgradePopup();
m_TowerPlacePopup->Render();
RenderTowerUpgradePopup();
}
void WorldRenderer::Render() {
if (m_WorldVao == nullptr)
return;
RenderWorld();
RenderMobs();
RenderTowers();
RenderTileSelect();
RenderTooltips();
RenderPopups();
DetectClick();
if (m_WorldVao == nullptr)
return;
RenderWorld();
RenderMobs();
RenderTowers();
RenderTileSelect();
RenderTooltips();
RenderPopups();
DetectClick();
}
WorldRenderer::~WorldRenderer() {
@@ -131,174 +131,174 @@ WorldRenderer::~WorldRenderer() {
}
void WorldRenderer::RenderTooltips() const {
RenderMobTooltip();
RenderCastleTooltip();
RenderMobTooltip();
RenderCastleTooltip();
}
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 });
if (m_WorldVao == nullptr)
return;
float movementX = -relativeX / m_Zoom * aspectRatio;
float movementY = relativeY / m_Zoom;
m_Renderer->SetCamMovement({ movementX, movementY });
}
void WorldRenderer::ChangeZoom(float zoomStep) {
if (m_WorldVao == nullptr)
return;
static float sensibility = 1.5f;
if (zoomStep < 0) {
m_Zoom /= -zoomStep * sensibility;
} else {
m_Zoom *= zoomStep * sensibility;
}
m_Renderer->SetZoom(m_Zoom);
m_Renderer->SetCamMovement({});
if (m_WorldVao == nullptr)
return;
static float sensibility = 1.5f;
if (zoomStep < 0) {
m_Zoom /= -zoomStep * sensibility;
} else {
m_Zoom *= zoomStep * sensibility;
}
m_Renderer->SetZoom(m_Zoom);
m_Renderer->SetCamMovement({});
}
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())) {
ImGui::OpenPopup("TowerPlace");
}
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())) {
ImGui::OpenPopup("TowerPlace");
}
}
void WorldRenderer::SetCamPos(float camX, float camY) {
m_CamPos = { camX, camY };
m_Renderer->SetCamPos(m_CamPos);
m_CamPos = { camX, camY };
m_Renderer->SetCamPos(m_CamPos);
}
void WorldRenderer::RenderTowerUpgradePopup() {
if (ImGui::BeginPopup("TowerUpgrade")) {
m_PopupOpened = true;
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());
if (ImGui::BeginPopup("TowerUpgrade")) {
m_PopupOpened = true;
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());
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
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
if (x > 0)
ImGui::SameLine();
if (x > 0)
ImGui::SameLine();
std::uint8_t currentLevel = x + 1;
game::TowerPath currentPath = game::TowerPath(y);
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;
bool disabled = towerStats == nullptr;
int towerLevel = tower->GetLevel().GetLevel();
int towerLevel = tower->GetLevel().GetLevel();
bool alreadyUpgraded = currentLevel <= towerLevel;
bool canUpgrade = (towerLevel + 1) == currentLevel;
bool alreadyUpgraded = currentLevel <= towerLevel;
bool canUpgrade = (towerLevel + 1) == currentLevel;
if (canUpgrade && towerPath != game::TowerPath::Base) {
if (currentPath != towerPath) {
canUpgrade = false;
}
}
if (canUpgrade && towerPath != game::TowerPath::Base) {
if (currentPath != towerPath) {
canUpgrade = false;
}
}
ImGui::PushID(x * 4 + y);
if (disabled) {
ImGui::BeginDisabled();
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::Button("", ImVec2(100, 100));
ImGui::PopStyleColor();
ImGui::EndDisabled();
} else if (alreadyUpgraded) {
ImGui::BeginDisabled();
ImGui::Button("Already", ImVec2(100, 100));
ImGui::EndDisabled();
} else if (canUpgrade) {
if (ImGui::Button("Upgrade", ImVec2(100, 100))) {
m_Client->GetClient()->UpgradeTower(tower->GetID(), { currentLevel, currentPath });
}
} else {
ImGui::BeginDisabled();
ImGui::Button("Locked", ImVec2(100, 100));
ImGui::EndDisabled();
}
ImGui::PopID();
}
}
ImGui::PushID(x * 4 + y);
if (disabled) {
ImGui::BeginDisabled();
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::Button("", ImVec2(100, 100));
ImGui::PopStyleColor();
ImGui::EndDisabled();
} else if (alreadyUpgraded) {
ImGui::BeginDisabled();
ImGui::Button("Already", ImVec2(100, 100));
ImGui::EndDisabled();
} else if (canUpgrade) {
if (ImGui::Button("Upgrade", ImVec2(100, 100))) {
m_Client->GetClient()->UpgradeTower(tower->GetID(), { currentLevel, currentPath });
}
} else {
ImGui::BeginDisabled();
ImGui::Button("Locked", ImVec2(100, 100));
ImGui::EndDisabled();
}
ImGui::PopID();
}
}
ImGui::EndPopup();
}
ImGui::EndPopup();
}
}
void WorldRenderer::DetectClick() {
ImGuiIO& io = ImGui::GetIO();
if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) {
Vec2f cursorPos = { io.MousePos.x, io.MousePos.y };
if (cursorPos == m_HoldCursorPos) {
m_LastClicked = m_HoldCursorPos;
Click();
}
}
ImGuiIO& io = ImGui::GetIO();
if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) {
Vec2f cursorPos = { io.MousePos.x, io.MousePos.y };
if (cursorPos == m_HoldCursorPos) {
m_LastClicked = m_HoldCursorPos;
Click();
}
}
}
void WorldRenderer::RenderMobTooltip() const {
if (ImGui::IsAnyItemHovered()) return;
if (ImGui::IsAnyItemHovered()) return;
DetectMobHovering();
m_MobTooltip->Render();
DetectMobHovering();
m_MobTooltip->Render();
}
void WorldRenderer::RenderCastleTooltip() const {
if (ImGui::IsAnyItemHovered()) return;
if (ImGui::IsAnyItemHovered()) return;
DetectCastleHovering();
m_CastleTooltip->Render();
DetectCastleHovering();
m_CastleTooltip->Render();
}
void WorldRenderer::DetectMobHovering() const {
Vec2f 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);
Vec2f 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);
}
void WorldRenderer::DetectCastleHovering() const {
Vec2f 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);
Vec2f 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);
}
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();
}
Vec2f 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());
ImGuiIO& io = ImGui::GetIO();
return m_Renderer->GetCursorWorldPos({ io.MousePos.x, io.MousePos.y }, Display::GetAspectRatio(), m_Zoom, Display::GetWindowWidth(), Display::GetWindowHeight());
}
Vec2f WorldRenderer::GetClickWorldPos() const {
return m_Renderer->GetCursorWorldPos(m_LastClicked, Display::GetAspectRatio(), m_Zoom, Display::GetWindowWidth(), Display::GetWindowHeight());
return m_Renderer->GetCursorWorldPos(m_LastClicked, Display::GetAspectRatio(), m_Zoom, Display::GetWindowWidth(), Display::GetWindowHeight());
}
} // namespace render

View File

@@ -13,14 +13,14 @@ CastleTooltip::CastleTooltip(client::Client* client) : GuiWidget(client) {
}
void CastleTooltip::Render() {
if (m_Castle == nullptr) return;
if (m_Castle == nullptr) return;
ImGui::BeginTooltip();
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::EndTooltip();
ImGui::BeginTooltip();
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::EndTooltip();
}
} // namespace gui

View File

@@ -13,25 +13,25 @@ FrameMenu::FrameMenu(client::Client* client) : GuiWidget(client), m_VSync(true),
}
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);
}
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);
}
#if !defined(NDEBUG)
ImGui::Checkbox("Demo Window", &m_ShowDemoWindow);
ImGui::Checkbox("Demo Window", &m_ShowDemoWindow);
#endif
ImGui::End();
ImGui::End();
#if !defined(NDEBUG)
if (m_ShowDemoWindow)
ImGui::ShowDemoWindow(&m_ShowDemoWindow);
if (m_ShowDemoWindow)
ImGui::ShowDemoWindow(&m_ShowDemoWindow);
#endif
}

View File

@@ -14,83 +14,83 @@ GameMenu::GameMenu(client::Client* client) : GuiWidget(client), m_SummonMenu(std
}
void GameMenu::Render() {
if (!m_Client->IsConnected()) return;
if (!m_Client->IsConnected()) return;
if (GetClient()->GetGame().GetGameState() == td::game::GameState::Lobby) {
ImGui::Begin("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) {
ImGui::Begin("Game");
ImGui::End();
}
if (GetClient()->GetGame().GetGameState() == td::game::GameState::Game) {
ImGui::Begin("Game");
ShowTPS();
ShowStats();
ShowPlayers();
ShowTPS();
ShowStats();
ShowPlayers();
ImGui::End();
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()) {
const td::game::Player& player = pair.second;
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::GetImGuiTeamColor(player.GetTeamColor()));
ImGui::Text("%s", player.GetName().c_str());
ImGui::PopStyleColor();
}
ImGui::TreePop();
}
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::PopStyleColor();
}
ImGui::TreePop();
}
}
void GameMenu::ShowTeamSelection() {
if (GetClient()->GetGame().GetPlayer() == nullptr)
return;
td::game::TeamColor playerTeam = GetClient()->GetGame().GetPlayer()->GetTeamColor();
if (GetClient()->GetGame().GetPlayer() == nullptr)
return;
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);
else
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);
else
GetClient()->SelectTeam(td::game::TeamColor::Blue);
}
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);
else
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);
else
GetClient()->SelectTeam(td::game::TeamColor::Blue);
}
}
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::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
ImGui::Text("Time Remaining");
} else {
ImGui::Text("Waiting for players ...\n");
}
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::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
ImGui::Text("Time Remaining");
} else {
ImGui::Text("Waiting for players ...\n");
}
}
void GameMenu::ShowTPS() {
ImGui::Text("Server TPS : %.1f", GetClient()->GetConnexion().GetServerTPS());
ImGui::Text("Server Ping : %i", GetClient()->GetConnexion().GetServerPing());
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());
ImGui::Text("Gold : %i", GetClient()->GetGame().GetPlayer()->GetGold());
ImGui::Text("EXP: %i", GetClient()->GetGame().GetPlayer()->GetExp());
}
} // namespace gui

View File

@@ -8,83 +8,83 @@ namespace td {
namespace gui {
MainMenu::MainMenu(client::Client* client) : GuiWidget(client), m_ConnectPort(25565) {
m_ConnectAddress = "localhost";
m_ConnectAddress.reserve(256);
m_ConnectAddress = "localhost";
m_ConnectAddress.reserve(256);
}
MainMenu::~MainMenu() {
if (m_Server != nullptr)
m_Server->Stop();
if (m_Server != nullptr)
m_Server->Stop();
}
void MainMenu::Render() {
if (m_Server != nullptr && !m_Server->IsRunning()) {
m_Server.reset(0); // destroying server if it stoped
}
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")) {
ImGui::OpenPopup("Rejoindre une partie##join_popup");
}
if (ImGui::Button("Créer une partie")) {
ImGui::OpenPopup("Créer une partie##create_popup");
}
if (ImGui::Button("Options")) {
// TODO: add settings
}
ImGui::Begin("Main Menu");
if (ImGui::Button("Rejoindre une partie##join")) {
ImGui::OpenPopup("Rejoindre une partie##join_popup");
}
if (ImGui::Button("Créer une partie")) {
ImGui::OpenPopup("Créer une partie##create_popup");
}
if (ImGui::Button("Options")) {
// TODO: add settings
}
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")) {
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);
m_TriedToConnect = true;
}
if (m_TriedToConnect) {
ImGui::Text("Impossible de se connecter");
}
ImGui::EndPopup();
} else {
m_TriedToConnect = false;
}
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")) {
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);
m_TriedToConnect = true;
}
if (m_TriedToConnect) {
ImGui::Text("Impossible de se connecter");
}
ImGui::EndPopup();
} else {
m_TriedToConnect = false;
}
if (ImGui::BeginPopup("Créer une partie##create_popup")) {
ImGui::InputInt("Server Port", &m_ServerPort, -1);
ImGui::Text("%s", std::string("Fichier de monde sélectionné : " + (m_WorldFilePath.empty() ? std::string("Aucun") : m_WorldFilePath)).c_str());
ImGui::SameLine();
if (ImGui::Button("Ouvrir un fichier")) {
ImGui::OpenPopup("WorldFileDialog");
}
if (m_FileDialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")) {
m_WorldFilePath = m_FileDialog.selected_path;
}
if (ImGui::Button("Créer")) {
if (!StartServer()) {
m_TriedToCreate = true;
} else {
GetClient()->Connect(td::network::Dns::Resolve("localhost"), m_ServerPort);
}
}
if (m_TriedToCreate)
ImGui::Text("Failed to launch server");
ImGui::EndPopup();
} else {
m_TriedToConnect = false;
}
if (ImGui::BeginPopup("Créer une partie##create_popup")) {
ImGui::InputInt("Server Port", &m_ServerPort, -1);
ImGui::Text("%s", std::string("Fichier de monde sélectionné : " + (m_WorldFilePath.empty() ? std::string("Aucun") : m_WorldFilePath)).c_str());
ImGui::SameLine();
if (ImGui::Button("Ouvrir un fichier")) {
ImGui::OpenPopup("WorldFileDialog");
}
if (m_FileDialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")) {
m_WorldFilePath = m_FileDialog.selected_path;
}
if (ImGui::Button("Créer")) {
if (!StartServer()) {
m_TriedToCreate = true;
} else {
GetClient()->Connect(td::network::Dns::Resolve("localhost"), m_ServerPort);
}
}
if (m_TriedToCreate)
ImGui::Text("Failed to launch server");
ImGui::EndPopup();
} else {
m_TriedToConnect = false;
}
ImGui::End();
ImGui::End();
}
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)) {
return false;
}
return true;
if (m_WorldFilePath.empty())
return false;
m_Server = std::make_unique<td::server::Server>(m_WorldFilePath);
if (!m_Server->Start(m_ServerPort)) {
return false;
}
return true;
}
} // namespace gui

View File

@@ -15,29 +15,29 @@ MobTooltip::MobTooltip(client::Client* client) : GuiWidget(client) {
}
void MobTooltip::Render() {
if (m_Mob == nullptr) return;
if (m_Mob == nullptr) return;
// TODO: add sender null check
// TODO: add sender null check
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::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::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::EndTooltip();
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::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::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::EndTooltip();
}
} // namespace gui

View File

@@ -6,76 +6,76 @@ namespace td {
namespace gui {
SummonMenu::SummonMenu(client::Client* client) : GuiWidget(client), m_MenuOpened(true) {
m_Values.fill(0);
m_Values.fill(0);
}
void SummonMenu::Render() {
if (m_MenuOpened) {
ImGui::Begin("Summon", &m_MenuOpened);
ImTextureID my_tex_id = ImGui::GetIO().Fonts->TexID;
for (int i = 0; i < m_MobTypeCount / 2; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::Image(my_tex_id, ImVec2(100, 100));
ImGui::PopID();
}
ImGui::Separator();
ImGui::PushItemWidth(m_ImageWidth);
for (int i = 0; i < m_MobTypeCount / 2; i++) {
ImGui::SameLine();
ImGui::PushID(i);
if (ImGui::InputInt("", m_Values.data() + i, 1, 10)) {
SetSummonMax(i);
}
ImGui::PopID();
}
ImGui::PopItemWidth();
ImGui::Separator();
for (int i = m_MobTypeCount / 2; i < m_MobTypeCount; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::Image(my_tex_id, ImVec2(100, 100));
ImGui::PopID();
}
ImGui::Separator();
ImGui::PushItemWidth(m_ImageWidth);
for (int i = m_MobTypeCount / 2; i < m_MobTypeCount; i++) {
ImGui::SameLine();
ImGui::PushID(i);
if (ImGui::InputInt("", m_Values.data() + i, 1, m_MobTypeCount)) {
SetSummonMax(i);
}
ImGui::PopID();
}
ImGui::PopItemWidth();
if (ImGui::Button("Send")) {
std::vector<protocol::MobSend> mobSent;
protocol::MobSend mobSend;
for (int i = 0; i < m_MobTypeCount; i++) {
if (m_Values[i] != 0) {
mobSend.mobCount = m_Values[i];
mobSend.mobLevel = 1; // TODO: add mob levels
mobSend.mobType = td::game::MobType(i);
mobSent.push_back(mobSend);
}
}
m_Client->SendMobs(mobSent);
m_Values.fill(0);
}
ImGui::End();
}
if (m_MenuOpened) {
ImGui::Begin("Summon", &m_MenuOpened);
ImTextureID my_tex_id = ImGui::GetIO().Fonts->TexID;
for (int i = 0; i < m_MobTypeCount / 2; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::Image(my_tex_id, ImVec2(100, 100));
ImGui::PopID();
}
ImGui::Separator();
ImGui::PushItemWidth(m_ImageWidth);
for (int i = 0; i < m_MobTypeCount / 2; i++) {
ImGui::SameLine();
ImGui::PushID(i);
if (ImGui::InputInt("", m_Values.data() + i, 1, 10)) {
SetSummonMax(i);
}
ImGui::PopID();
}
ImGui::PopItemWidth();
ImGui::Separator();
for (int i = m_MobTypeCount / 2; i < m_MobTypeCount; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::Image(my_tex_id, ImVec2(100, 100));
ImGui::PopID();
}
ImGui::Separator();
ImGui::PushItemWidth(m_ImageWidth);
for (int i = m_MobTypeCount / 2; i < m_MobTypeCount; i++) {
ImGui::SameLine();
ImGui::PushID(i);
if (ImGui::InputInt("", m_Values.data() + i, 1, m_MobTypeCount)) {
SetSummonMax(i);
}
ImGui::PopID();
}
ImGui::PopItemWidth();
if (ImGui::Button("Send")) {
std::vector<protocol::MobSend> mobSent;
protocol::MobSend mobSend;
for (int i = 0; i < m_MobTypeCount; i++) {
if (m_Values[i] != 0) {
mobSend.mobCount = m_Values[i];
mobSend.mobLevel = 1; // TODO: add mob levels
mobSend.mobType = td::game::MobType(i);
mobSent.push_back(mobSend);
}
}
m_Client->SendMobs(mobSent);
m_Values.fill(0);
}
ImGui::End();
}
}
void SummonMenu::SetSummonMax(int valueIndex) {
int& value = m_Values[valueIndex];
value = std::max(0, value);
value = std::min(12, value);
int total = 0;
for (std::size_t i = 0; i < m_Values.size(); i++) {
total += m_Values[i];
}
if (total == 13) // if the total is greater than the maximum, we substract the value
value--;
int& value = m_Values[valueIndex];
value = std::max(0, value);
value = std::min(12, value);
int total = 0;
for (std::size_t i = 0; i < m_Values.size(); i++) {
total += m_Values[i];
}
if (total == 13) // if the total is greater than the maximum, we substract the value
value--;
}
} // namespace gui

View File

@@ -22,64 +22,64 @@ 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()));
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),
m_GlContext(glContext), m_Renderer(renderer), m_Client(std::make_unique<client::Client>(m_Renderer)) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOpenGL(m_Window, m_GlContext);
ImGui_ImplOpenGL3_Init();
ImFontConfig c;
c.SizePixels = 25;
ImGui::GetIO().Fonts->AddFontDefault(&c);
InitWidgets();
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOpenGL(m_Window, m_GlContext);
ImGui_ImplOpenGL3_Init();
ImFontConfig c;
c.SizePixels = 25;
ImGui::GetIO().Fonts->AddFontDefault(&c);
InitWidgets();
}
void TowerGui::BeginFrame() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
}
void TowerGui::EndFrame() {
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
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();
static std::uint64_t lastTime = td::utils::GetTime();
std::uint64_t time = td::utils::GetTime();
std::uint64_t delta = time - lastTime;
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();
Tick();
BeginFrame();
m_Client->Render();
m_Client->Render();
m_GuiManager.RenderWidgets();
m_GuiManager.RenderWidgets();
EndFrame();
EndFrame();
}
TowerGui::~TowerGui() {
m_Client->CloseConnection();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
m_Client->CloseConnection();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
}
} // namespace render

View File

@@ -14,51 +14,51 @@ TowerPlacePopup::TowerPlacePopup(client::Client* client) : GuiWidget(client) {
}
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();
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);
game::TowerType towerType = game::TowerType(i);
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::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);
ImGui::TextDisabled("(?)");
ImGui::SameLine();
ImGui::SetCursorPosX(m_TowerPopupTileWidth - 10 - ImGui::CalcTextSize("(?)").x);
ImGui::TextDisabled("(?)");
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text(towerInfo.GetDescription().c_str());
ImGui::EndTooltip();
}
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text(towerInfo.GetDescription().c_str());
ImGui::EndTooltip();
}
std::string buyText = std::to_string(100) + " golds";
std::string buyText = std::to_string(100) + " golds";
ImGui::SetCursorPosY(m_TowerPopupTileHeight - m_PlaceTowerButtonHeight - 10);
ImGui::SetCursorPosX(m_TowerPopupTileWidth / 2.0f - m_PlaceTowerButtonWidth / 2.0f);
ImGui::SetCursorPosY(m_TowerPopupTileHeight - m_PlaceTowerButtonHeight - 10);
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);
ImGui::CloseCurrentPopup();
}
if (ImGui::Button(buyText.c_str(), ImVec2(m_PlaceTowerButtonWidth, m_PlaceTowerButtonHeight))) {
GetClient()->PlaceTower(towerType, m_ClickWorldPos);
ImGui::CloseCurrentPopup();
}
ImGui::EndChild();
}
}
ImGui::EndChild();
ImGui::EndPopup();
}
ImGui::EndChild();
}
}
ImGui::EndChild();
ImGui::EndPopup();
}
}
void TowerPlacePopup::SetClickPos(const Vec2f& worldPos) {
m_ClickWorldPos = worldPos;
m_ClickWorldPos = worldPos;
}
} // namespace gui

View File

@@ -10,90 +10,90 @@ namespace td {
namespace gui {
UpdateMenu::UpdateMenu(client::Client* client) : GuiWidget(client), m_Opened(true), m_Updater(std::make_unique<utils::Updater>()) {
CheckUpdates();
CheckUpdates();
}
UpdateMenu::~UpdateMenu() {}
void UpdateMenu::Render() {
RenderErrorPopup();
if (m_Opened) {
ImGui::Begin("Updater", &m_Opened);
if (IsUpdateChecked()) {
RenderErrorPopup();
if (m_Opened) {
ImGui::Begin("Updater", &m_Opened);
if (IsUpdateChecked()) {
bool updateAvailable = m_UpdateAvailable.get();
if (updateAvailable) {
bool updateAvailable = m_UpdateAvailable.get();
if (updateAvailable) {
if (m_Updater->IsFileWrited()) {
ImGui::Text("The update is now installed");
ImGui::Text("The game needs to be restarted");
} else if (m_Updater->IsDownloadComplete()) {
ImGui::Text("Download done!");
if (ImGui::Button("Install")) {
if (!m_Updater->WriteFile()) {
m_Error = "Failed to write file !\n";
ImGui::OpenPopup("UpdateError");
}
}
if (ImGui::Button("Cancel")) {
m_Updater->CancelDownload();
m_Updater->ClearCache();
}
} else {
if (m_Updater->GetDownloadProgress() > 0) {
ImGui::Text("Downloading ...");
ImGui::ProgressBar(m_Updater->GetDownloadProgress());
if (ImGui::Button("Cancel")) {
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());
if (m_Updater->IsFileWrited()) {
ImGui::Text("The update is now installed");
ImGui::Text("The game needs to be restarted");
} else if (m_Updater->IsDownloadComplete()) {
ImGui::Text("Download done!");
if (ImGui::Button("Install")) {
if (!m_Updater->WriteFile()) {
m_Error = "Failed to write file !\n";
ImGui::OpenPopup("UpdateError");
}
}
if (ImGui::Button("Cancel")) {
m_Updater->CancelDownload();
m_Updater->ClearCache();
}
} else {
if (m_Updater->GetDownloadProgress() > 0) {
ImGui::Text("Downloading ...");
ImGui::ProgressBar(m_Updater->GetDownloadProgress());
if (ImGui::Button("Cancel")) {
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());
bool canDownloadFile = m_Updater->CanUpdate();
bool canDownloadFile = m_Updater->CanUpdate();
if (!canDownloadFile) ImGui::BeginDisabled();
if (!canDownloadFile) ImGui::BeginDisabled();
if (ImGui::Button("Download")) {
m_Updater->DownloadUpdate();
}
if (ImGui::Button("Download")) {
m_Updater->DownloadUpdate();
}
if (!canDownloadFile) ImGui::EndDisabled();
if (!canDownloadFile) ImGui::EndDisabled();
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
m_Opened = false;
}
}
}
} 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());
}
} else {
ImGui::Text("Checking updates ...");
}
ImGui::End();
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
m_Opened = false;
}
}
}
} 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());
}
} else {
ImGui::Text("Checking updates ...");
}
ImGui::End();
}
}
void UpdateMenu::RenderErrorPopup() {
if (ImGui::BeginPopup("UpdateError")) {
ImGui::Text("Error : %s", m_Error.c_str());
ImGui::EndPopup();
}
if (ImGui::BeginPopup("UpdateError")) {
ImGui::Text("Error : %s", m_Error.c_str());
ImGui::EndPopup();
}
}
bool UpdateMenu::IsUpdateChecked() {
return m_UpdateAvailable.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
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();});
m_UpdateAvailable = std::async(std::launch::async, [&]() { return m_Updater->CheckUpdate();});
}
} // namespace gui

View File

@@ -11,233 +11,233 @@ namespace render {
namespace WorldLoader {
GL::VertexArray LoadMobModel() {
std::vector<float> positions = {
-0.5, -0.5,
0.5, -0.5,
-0.5, 0.5,
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
};
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));
float yellowFloat;
int yellow = 255 << 24 | 255 << 16 | 255;
memcpy(&yellowFloat, &yellow, sizeof(int));
std::vector<float> colors = {
yellowFloat,
yellowFloat,
yellowFloat,
std::vector<float> colors = {
yellowFloat,
yellowFloat,
yellowFloat,
yellowFloat,
yellowFloat,
yellowFloat
};
yellowFloat,
yellowFloat,
yellowFloat
};
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
colorVBO.AddVertexAttribPointer(1, 1, 0);
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
colorVBO.AddVertexAttribPointer(1, 1, 0);
GL::VertexArray mobVao(colors.size()); // each pos = 1 color
mobVao.Bind();
mobVao.BindVertexBuffer(positionVBO);
mobVao.BindVertexBuffer(colorVBO);
mobVao.Unbind();
return mobVao;
GL::VertexArray mobVao(colors.size()); // each pos = 1 color
mobVao.Bind();
mobVao.BindVertexBuffer(positionVBO);
mobVao.BindVertexBuffer(colorVBO);
mobVao.Unbind();
return mobVao;
}
GL::VertexArray LoadWorldModel(const td::game::World* world) {
std::vector<float> positions;
std::vector<float> colors;
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;
for (const auto& chunkInfo : world->GetChunks()) {
const td::game::ChunkCoord& coords = chunkInfo.first;
td::game::ChunkPtr chunk = chunkInfo.second;
std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth;
std::int32_t chunkY = coords.y * td::game::Chunk::ChunkHeight;
std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth;
std::int32_t chunkY = coords.y * 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);
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;
if (tile == nullptr)
continue;
positions.insert(positions.end(), {
static_cast<float>(chunkX + tileX), static_cast<float>(chunkY + tileY),
static_cast<float>(chunkX + tileX + 1), static_cast<float>(chunkY + tileY),
static_cast<float>(chunkX + tileX), static_cast<float>(chunkY + tileY + 1),
positions.insert(positions.end(), {
static_cast<float>(chunkX + tileX), static_cast<float>(chunkY + tileY),
static_cast<float>(chunkX + tileX + 1), static_cast<float>(chunkY + tileY),
static_cast<float>(chunkX + tileX), static_cast<float>(chunkY + tileY + 1),
static_cast<float>(chunkX + tileX + 1), static_cast<float>(chunkY + tileY),
static_cast<float>(chunkX + tileX), static_cast<float>(chunkY + tileY + 1),
static_cast<float>(chunkX + tileX + 1), static_cast<float>(chunkY + tileY + 1),
});
static_cast<float>(chunkX + tileX + 1), static_cast<float>(chunkY + tileY),
static_cast<float>(chunkX + tileX), static_cast<float>(chunkY + tileY + 1),
static_cast<float>(chunkX + tileX + 1), static_cast<float>(chunkY + tileY + 1),
});
const td::Color* tileColor = world->GetTileColor(tile);
const td::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;
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);
int newColorIndex = colors.size();
colors.push_back(0);
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
}
}
}
}
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
}
}
}
}
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();
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();
positions.insert(positions.end(), {
fromX, fromY,
fromX, toY,
toX, fromY,
positions.insert(positions.end(), {
fromX, fromY,
fromX, toY,
toX, fromY,
toX, toY,
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;
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);
int newColorIndex = colors.size();
colors.push_back(0);
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
}
}
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
}
}
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();
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();
positions.insert(positions.end(), {
fromX, fromY,
fromX, toY,
toX, fromY,
positions.insert(positions.end(), {
fromX, fromY,
fromX, toY,
toX, fromY,
toX, toY,
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(castleColor)).r << 24;
color |= world->GetSpawnColor(game::TeamColor(castleColor)).g << 16;
color |= world->GetSpawnColor(game::TeamColor(castleColor)).b << 8;
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;
int newColorIndex = colors.size();
colors.push_back(0);
int newColorIndex = colors.size();
colors.push_back(0);
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
}
}
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
}
}
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
colorVBO.AddVertexAttribPointer(1, 1, 0);
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
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();
return worldVao;
GL::VertexArray worldVao(positions.size() / 2); // each pos = 2 vertecies
worldVao.Bind();
worldVao.BindVertexBuffer(positionVBO);
worldVao.BindVertexBuffer(colorVBO);
worldVao.Unbind();
return worldVao;
}
GL::VertexArray LoadTileSelectModel() {
std::vector<float> positions = {
0, 0,
1, 0,
0, 1,
std::vector<float> positions = {
0, 0,
1, 0,
0, 1,
1, 0,
0, 1,
1, 1
};
1, 0,
0, 1,
1, 1
};
int color = 255 << 24 | 255 << 16 | 255 << 8 | 150;
float colorFloat;
int color = 255 << 24 | 255 << 16 | 255 << 8 | 150;
float colorFloat;
memcpy(reinterpret_cast<std::uint8_t*>(&colorFloat), &color, sizeof(float));
memcpy(reinterpret_cast<std::uint8_t*>(&colorFloat), &color, sizeof(float));
std::vector<float> colors(6, colorFloat);
std::vector<float> colors(6, colorFloat);
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
colorVBO.AddVertexAttribPointer(1, 1, 0);
GL::VertexBuffer positionVBO(positions, 2);
positionVBO.AddVertexAttribPointer(0, 2, 0);
GL::VertexBuffer colorVBO(colors, 1);
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();
GL::VertexArray tileSelectVao(positions.size() / 2); // each pos = 2 vertecies
tileSelectVao.Bind();
tileSelectVao.BindVertexBuffer(positionVBO);
tileSelectVao.BindVertexBuffer(colorVBO);
tileSelectVao.Unbind();
return tileSelectVao;
return tileSelectVao;
}
RenderData LoadTowerModel(game::TowerPtr tower) {
RenderData renderData;
RenderData renderData;
float towerX, towerDX;
float towerY, towerDY;
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;
} else {
towerX = tower->GetCenterX() - 2.5f;
towerDX = tower->GetCenterX() + 2.5f;
towerY = tower->GetCenterY() - 1.5f;
towerDY = tower->GetCenterY() + 1.5f;
} else {
towerX = tower->GetCenterX() - 2.5f;
towerDX = tower->GetCenterX() + 2.5f;
towerY = tower->GetCenterY() - 2.5f;
towerDY = tower->GetCenterY() + 2.5f;
}
std::vector<float> positions = {
towerX, towerY,
towerDX, towerY,
towerX, towerDY,
towerY = tower->GetCenterY() - 2.5f;
towerDY = tower->GetCenterY() + 2.5f;
}
std::vector<float> positions = {
towerX, towerY,
towerDX, towerY,
towerX, towerDY,
towerDX, towerY,
towerX, towerDY,
towerDX, towerDY
};
towerDX, towerY,
towerX, towerDY,
towerDX, towerDY
};
renderData.positions = positions;
renderData.positions = positions;
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;
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;
int color = r << 24 | g << 16 | b << 8 | 255;
memcpy(&colorFloat, &color, sizeof(int));
float colorFloat;
int color = r << 24 | g << 16 | b << 8 | 255;
memcpy(&colorFloat, &color, sizeof(int));
std::vector<float> colors(6, colorFloat);
renderData.colors = colors;
std::vector<float> colors(6, colorFloat);
renderData.colors = colors;
return renderData;
return renderData;
}

View File

@@ -96,31 +96,31 @@ void main(void){
EntityShader::EntityShader() : ShaderProgram() {}
void EntityShader::LoadShader() {
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
}
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"));
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 Vec2f& camPos) {
LoadVector(m_LocationCam, camPos);
LoadVector(m_LocationCam, camPos);
}
void EntityShader::SetZoom(float zoom) {
LoadFloat(m_LocationZoom, zoom);
LoadFloat(m_LocationZoom, zoom);
}
void EntityShader::SetAspectRatio(float aspectRatio) {
LoadFloat(m_LocationAspectRatio, aspectRatio);
LoadFloat(m_LocationAspectRatio, aspectRatio);
}
void EntityShader::SetModelPos(const Vec2f& modelPos) {
LoadVector(m_LocationTranslation, modelPos);
LoadVector(m_LocationTranslation, modelPos);
}
void EntityShader::SetIsometricView(float isometric) {
LoadFloat(m_LocationViewtype, isometric);
LoadFloat(m_LocationViewtype, isometric);
}

View File

@@ -22,117 +22,117 @@ namespace td {
namespace shader {
ShaderProgram::ShaderProgram() :
m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {
m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {
}
ShaderProgram::~ShaderProgram() {
CleanUp();
CleanUp();
}
void ShaderProgram::Start() const {
glUseProgram(m_ProgramID);
glUseProgram(m_ProgramID);
}
void ShaderProgram::Stop() const {
glUseProgram(0);
glUseProgram(0);
}
int ShaderProgram::GetUniformLocation(const std::string& uniformName) const {
const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
if (location == -1) {
utils::LOGD(utils::format("Warning ! Uniform variable %s not found !", uniformName.c_str()));
}
return location;
const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
if (location == -1) {
utils::LOGD(utils::format("Warning ! Uniform variable %s not found !", uniformName.c_str()));
}
return location;
}
void ShaderProgram::LoadFloat(unsigned int location, float value) const {
glUniform1f(static_cast<GLint>(location), value);
glUniform1f(static_cast<GLint>(location), value);
}
void ShaderProgram::LoadInt(unsigned int location, int value) const {
glUniform1i(static_cast<GLint>(location), value);
glUniform1i(static_cast<GLint>(location), value);
}
void ShaderProgram::LoadVector(unsigned int location,
const Vec2f& vector) const {
glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
const Vec2f& vector) const {
glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
}
void ShaderProgram::LoadVector(unsigned int location,
const Vec3f& vector) const {
glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
const Vec3f& vector) const {
glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
}
void ShaderProgram::LoadBoolean(unsigned int location, bool value) const {
glUniform1i(static_cast<GLint>(location), value);
glUniform1i(static_cast<GLint>(location), value);
}
void ShaderProgram::CleanUp() const {
Stop();
glDetachShader(m_ProgramID, m_VertexShaderID);
glDetachShader(m_ProgramID, m_FragmentShaderID);
glDeleteShader(m_VertexShaderID);
glDeleteShader(m_FragmentShaderID);
glDeleteProgram(m_ProgramID);
Stop();
glDetachShader(m_ProgramID, m_VertexShaderID);
glDetachShader(m_ProgramID, m_FragmentShaderID);
glDeleteShader(m_VertexShaderID);
glDeleteShader(m_FragmentShaderID);
glDeleteProgram(m_ProgramID);
}
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_ProgramID = glCreateProgram();
glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(m_ProgramID);
glValidateProgram(m_ProgramID);
GetAllUniformLocation();
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_ProgramID = glCreateProgram();
glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(m_ProgramID);
glValidateProgram(m_ProgramID);
GetAllUniformLocation();
}
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_ProgramID = glCreateProgram();
glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(m_ProgramID);
glValidateProgram(m_ProgramID);
GetAllUniformLocation();
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_ProgramID = glCreateProgram();
glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(m_ProgramID);
glValidateProgram(m_ProgramID);
GetAllUniformLocation();
}
unsigned int ShaderProgram::LoadShader(const std::string& source, GLenum type) {
unsigned int shaderID = glCreateShader(type);
unsigned int shaderID = glCreateShader(type);
const char* c_str = source.c_str();
int* null = 0;
glShaderSource(shaderID, 1, &c_str, null); // @suppress("Function cannot be resolved")
glCompileShader(shaderID);
GLint compilesuccessful;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
if (compilesuccessful == false) {
GLsizei size;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
std::vector<char> shaderError(static_cast<std::size_t>(size));
glGetShaderInfoLog(shaderID, size, &size, shaderError.data());
const char* c_str = source.c_str();
int* null = 0;
glShaderSource(shaderID, 1, &c_str, null); // @suppress("Function cannot be resolved")
glCompileShader(shaderID);
GLint compilesuccessful;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
if (compilesuccessful == false) {
GLsizei size;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
std::vector<char> shaderError(static_cast<std::size_t>(size));
glGetShaderInfoLog(shaderID, size, &size, shaderError.data());
utils::LOGE("Could not compile shader !");
utils::LOGE("Could not compile shader !");
utils::LOGE(shaderError.data());
}
return shaderID;
utils::LOGE(shaderError.data());
}
return shaderID;
}
unsigned int ShaderProgram::LoadShaderFromFile(const std::string& file, GLenum type) {
std::stringstream stream;
std::ifstream fileStream(file);
std::stringstream stream;
std::ifstream fileStream(file);
if (fileStream) {
stream << fileStream.rdbuf();
} else {
return 0;
}
if (fileStream) {
stream << fileStream.rdbuf();
} else {
return 0;
}
return LoadShader(stream.str(), type);
return LoadShader(stream.str(), type);
}
} // namespace shader

View File

@@ -90,27 +90,27 @@ void main(void){
WorldShader::WorldShader() : ShaderProgram() {}
void WorldShader::LoadShader() {
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
}
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"));
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 Vec2f& camPos) {
LoadVector(m_LocationCam, camPos);
LoadVector(m_LocationCam, camPos);
}
void WorldShader::SetZoom(float zoom) {
LoadFloat(m_LocationZoom, zoom);
LoadFloat(m_LocationZoom, zoom);
}
void WorldShader::SetAspectRatio(float aspectRatio) {
LoadFloat(m_LocationAspectRatio, aspectRatio);
LoadFloat(m_LocationAspectRatio, aspectRatio);
}
void WorldShader::SetIsometricView(float isometric) {
LoadFloat(m_LocationViewtype, isometric);
LoadFloat(m_LocationViewtype, isometric);
}
} // namespace shader