restructure project
This commit is contained in:
84
src/client/Client.cpp
Normal file
84
src/client/Client.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "client/Client.h"
|
||||
#include "td/misc/Format.h"
|
||||
|
||||
#include "td/protocol/packets/DisconnectPacket.h"
|
||||
#include "td/protocol/packets/PlaceTowerPacket.h"
|
||||
#include "td/protocol/packets/RemoveTowerPacket.h"
|
||||
#include "td/protocol/packets/SelectTeamPacket.h"
|
||||
#include "td/protocol/packets/UpgradeTowerPacket.h"
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
bool Client::Connect(const network::IPAddresses& addresses, std::uint16_t port) {
|
||||
for (const network::IPAddress& address : addresses) {
|
||||
if (address.IsValid() && m_Connexion.Connect(address.ToString(), port)) {
|
||||
m_Connected = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
utils::LOGE("Failed to connect !");
|
||||
return false;
|
||||
}
|
||||
|
||||
void Client::SelectTeam(game::TeamColor team) {
|
||||
if (!m_Connected)
|
||||
return;
|
||||
|
||||
protocol::SelectTeamPacket packet(team);
|
||||
m_Connexion.SendPacket(&packet);
|
||||
}
|
||||
|
||||
void Client::CloseConnection() {
|
||||
if (!m_Connected)
|
||||
return;
|
||||
|
||||
m_Connected = false;
|
||||
|
||||
protocol::DisconnectPacket packet;
|
||||
m_Connexion.SendPacket(&packet);
|
||||
}
|
||||
|
||||
void Client::Tick(std::uint64_t delta) {
|
||||
if (!m_Connected)
|
||||
return;
|
||||
m_Connected = m_Connexion.UpdateSocket();
|
||||
if (!m_Connected) {
|
||||
utils::LOG(utils::format("Disconnected ! (Reason : %s)", m_Connexion.GetDisconnectReason().c_str()));
|
||||
Reset();
|
||||
} else {
|
||||
m_Game->Tick(delta);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::Render() {
|
||||
m_Game->RenderWorld();
|
||||
}
|
||||
|
||||
void Client::Reset() {
|
||||
m_Game.reset(0);
|
||||
m_Game = std::make_unique<ClientGame>(this);
|
||||
}
|
||||
|
||||
void Client::SendMobs(const std::vector<protocol::MobSend>& mobSends) {
|
||||
protocol::SendMobsPacket packet(mobSends);
|
||||
m_Connexion.SendPacket(&packet);
|
||||
}
|
||||
|
||||
void Client::PlaceTower(game::TowerType type, const Vec2f& position) {
|
||||
protocol::PlaceTowerPacket packet(position.x, position.y, type);
|
||||
m_Connexion.SendPacket(&packet);
|
||||
}
|
||||
|
||||
void Client::UpgradeTower(game::TowerID tower, game::TowerLevel level) {
|
||||
protocol::UpgradeTowerPacket packet(tower, level);
|
||||
m_Connexion.SendPacket(&packet);
|
||||
}
|
||||
|
||||
void Client::RemoveTower(game::TowerID tower) {
|
||||
protocol::RemoveTowerPacket packet(tower);
|
||||
m_Connexion.SendPacket(&packet);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
55
src/client/ClientConnexion.cpp
Normal file
55
src/client/ClientConnexion.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "client/ClientConnexion.h"
|
||||
#include "client/render/WorldRenderer.h"
|
||||
|
||||
#include "td/protocol/packets/ConnectionInfoPacket.h"
|
||||
#include "td/protocol/packets/DisconnectPacket.h"
|
||||
#include "td/protocol/packets/KeepAlivePacket.h"
|
||||
#include "td/protocol/packets/PlayerLoginPacket.h"
|
||||
#include "td/protocol/packets/ServerTpsPacket.h"
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
ClientConnexion::ClientConnexion() : Connexion(&m_Dispatcher), m_ServerTPS(0) {
|
||||
RegisterHandlers();
|
||||
}
|
||||
|
||||
void ClientConnexion::RegisterHandlers() {
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::KeepAlive, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::ConnectionInfo, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::Disconnect, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::ServerTps, this);
|
||||
}
|
||||
|
||||
void ClientConnexion::HandlePacket(const protocol::KeepAlivePacket* packet) {
|
||||
protocol::KeepAlivePacket keepAlivePacket(packet->GetAliveID());
|
||||
SendPacket(&keepAlivePacket);
|
||||
}
|
||||
|
||||
void ClientConnexion::HandlePacket(const protocol::ConnexionInfoPacket* packet) {
|
||||
m_ConnectionID = packet->GetConnectionID();
|
||||
Login();
|
||||
}
|
||||
|
||||
void ClientConnexion::HandlePacket(const protocol::ServerTpsPacket* packet) {
|
||||
m_ServerTPS = packet->GetTPS();
|
||||
m_ServerMSPT = packet->GetMSPT();
|
||||
m_Ping = utils::GetTime() - packet->GetPacketSendTime();
|
||||
}
|
||||
|
||||
void ClientConnexion::Login() {
|
||||
td::protocol::PlayerLoginPacket loginPacket("Persson" + std::to_string(static_cast<unsigned int>(m_ConnectionID)));
|
||||
SendPacket(&loginPacket);
|
||||
}
|
||||
|
||||
bool ClientConnexion::UpdateSocket() {
|
||||
return Connexion::UpdateSocket();
|
||||
}
|
||||
|
||||
void ClientConnexion::HandlePacket(const protocol::DisconnectPacket* packet) {
|
||||
m_DisconnectReason = packet->GetReason();
|
||||
CloseConnection();
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
130
src/client/game/ClientGame.cpp
Normal file
130
src/client/game/ClientGame.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "client/game/ClientGame.h"
|
||||
#include "td/protocol/PacketDispatcher.h"
|
||||
#include "client/Client.h"
|
||||
|
||||
#include "td/protocol/packets/ConnectionInfoPacket.h"
|
||||
#include "td/protocol/packets/PlayerJoinPacket.h"
|
||||
#include "td/protocol/packets/PlayerListPacket.h"
|
||||
#include "td/protocol/packets/PlayerLeavePacket.h"
|
||||
#include "td/protocol/packets/UpdatePlayerTeamPacket.h"
|
||||
#include "td/protocol/packets/UpdateLobbyTimePacket.h"
|
||||
#include "td/protocol/packets/UpdateGameStatePacket.h"
|
||||
#include "td/protocol/packets/UpdateMoneyPacket.h"
|
||||
#include "td/protocol/packets/UpdateExpPacket.h"
|
||||
#include "td/protocol/packets/DisconnectPacket.h"
|
||||
#include "td/protocol/packets/WorldDataPacket.h"
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
ClientGame::ClientGame(Client* client) : protocol::PacketHandler(client->GetConnexion().GetDispatcher()),
|
||||
game::Game(&m_WorldClient), m_Client(client), m_Renderer(client->GetRenderer()), m_WorldClient(this),
|
||||
m_WorldRenderer(&m_WorldClient, this) {
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::ConnectionInfo, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerJoin, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerList, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerLeave, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdatePlayerTeam, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateLobbyTime, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateGameState, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateMoney, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateEXP, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::Disconnect, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this);
|
||||
}
|
||||
|
||||
ClientGame::~ClientGame() {
|
||||
GetDispatcher()->UnregisterHandler(this);
|
||||
}
|
||||
|
||||
void ClientGame::Tick(std::uint64_t delta) {
|
||||
game::Game::Tick(delta);
|
||||
m_WorldRenderer.Update();
|
||||
if (m_GameState == game::GameState::Lobby && m_LobbyTime > 0) {
|
||||
m_LobbyTime -= delta;
|
||||
}
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::PlayerJoinPacket* packet) {
|
||||
game::Player player(packet->GetPlayerID());
|
||||
player.SetName(packet->GetPlayerName());
|
||||
|
||||
m_Players.insert({ player.GetID(), player });
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::PlayerLeavePacket* packet) {
|
||||
game::Player* player = &m_Players[packet->GetPlayerID()];
|
||||
if (player->GetTeamColor() != game::TeamColor::None) {
|
||||
m_Teams[static_cast<std::size_t>(player->GetTeamColor())].RemovePlayer(player);
|
||||
}
|
||||
m_Players.erase(player->GetID());
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::PlayerListPacket* packet) {
|
||||
for (auto pair : packet->GetPlayers()) {
|
||||
std::uint8_t playerID = pair.first;
|
||||
protocol::PlayerInfo playerInfo = pair.second;
|
||||
game::Player player(playerID);
|
||||
player.SetName(playerInfo.name);
|
||||
player.SetTeamColor(playerInfo.team);
|
||||
m_Players.insert({ playerID, player });
|
||||
if (player.GetTeamColor() != game::TeamColor::None) {
|
||||
m_Teams[static_cast<std::size_t>(player.GetTeamColor())].AddPlayer(&m_Players[playerID]);
|
||||
}
|
||||
}
|
||||
m_Player = &m_Players[m_ConnexionID];
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::UpdatePlayerTeamPacket* packet) {
|
||||
game::Player* player = &m_Players[packet->GetPlayerID()];
|
||||
if (player->GetTeamColor() == game::TeamColor::None) { //join a team
|
||||
GetTeam(packet->GetSelectedTeam()).AddPlayer(player);
|
||||
} else if (packet->GetSelectedTeam() == game::TeamColor::None) { // leave a team
|
||||
GetTeam(player->GetTeamColor()).RemovePlayer(player);
|
||||
} else { // change team
|
||||
GetTeam(player->GetTeamColor()).RemovePlayer(player);
|
||||
GetTeam(packet->GetSelectedTeam()).AddPlayer(player);
|
||||
}
|
||||
player->SetTeamColor(packet->GetSelectedTeam());
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::UpdateGameStatePacket* packet) {
|
||||
SetGameState(packet->GetGameState());
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::ConnexionInfoPacket* packet) {
|
||||
m_ConnexionID = packet->GetConnectionID();
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::UpdateLobbyTimePacket* packet) {
|
||||
m_LobbyTime = packet->GetRemainingTime();
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::UpdateMoneyPacket* packet) {
|
||||
m_Player->SetGold(packet->GetGold());
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::UpdateExpPacket* packet) {
|
||||
m_Player->SetExp(packet->GetExp());
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::DisconnectPacket* packet) {
|
||||
m_GameState = game::GameState::Disconnected;
|
||||
m_Renderer->SetBackgroundColor({ 0, 0, 0 });
|
||||
}
|
||||
|
||||
void ClientGame::HandlePacket(const protocol::WorldDataPacket* packet) {
|
||||
m_WorldRenderer.LoadModels();
|
||||
// set cam pos to player spawn
|
||||
const game::Spawn& spawn = m_World->GetTeam(m_Player->GetTeamColor()).GetSpawn();
|
||||
m_WorldRenderer.SetCamPos(spawn.GetCenterX(), spawn.GetCenterY());
|
||||
}
|
||||
|
||||
void ClientGame::RenderWorld() {
|
||||
if (m_GameState == game::GameState::Game || m_GameState == game::GameState::EndGame) {
|
||||
m_WorldRenderer.Render();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
85
src/client/game/WorldClient.cpp
Normal file
85
src/client/game/WorldClient.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "client/game/WorldClient.h"
|
||||
#include "td/protocol/PacketDispatcher.h"
|
||||
#include "client/game/ClientGame.h"
|
||||
#include "client/render/WorldRenderer.h"
|
||||
|
||||
#include "td/protocol/packets/WorldAddTowerPacket.h"
|
||||
#include "td/protocol/packets/WorldBeginDataPacket.h"
|
||||
#include "td/protocol/packets/WorldDataPacket.h"
|
||||
#include "td/protocol/packets/SpawnMobPacket.h"
|
||||
#include "td/protocol/packets/UpgradeTowerPacket.h"
|
||||
#include "td/protocol/packets/RemoveTowerPacket.h"
|
||||
#include "td/protocol/packets/UpdateCastleLifePacket.h"
|
||||
#include "td/protocol/packets/UpdateMobStatesPacket.h"
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::PacketHandler(game->GetDispatcher()), m_Game(game) {
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldAddTower, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldBeginData, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::SpawnMob, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpgradeTower, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveTower, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateCastleLife, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateMobStates, this);
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::WorldBeginDataPacket* packet) {
|
||||
LoadMap(packet);
|
||||
if (m_Game->GetGameState() == game::GameState::Game) {
|
||||
const Color& backgroundColor = GetBackgroundColor();
|
||||
m_Game->GetRenderer()->SetBackgroundColor({ static_cast<float>(backgroundColor.r / 255.0f), static_cast<float>(backgroundColor.g / 255.0f),
|
||||
static_cast<float>(backgroundColor.b / 255.0f) });
|
||||
}
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::WorldDataPacket* packet) {
|
||||
LoadMap(packet);
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::SpawnMobPacket* packet) {
|
||||
SpawnMobAt(packet->GetMobID(), packet->GetMobType(), packet->GetMobLevel(), packet->GetSender(),
|
||||
packet->GetMobX(), packet->GetMobY(), packet->GetMobDirection());
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::UpgradeTowerPacket* packet) {
|
||||
game::TowerPtr tower = GetTowerById(packet->GetTowerID());
|
||||
if (tower == nullptr) return; // this should not happen but who knows ?
|
||||
tower->Upgrade(packet->GetTowerLevel().GetLevel(), packet->GetTowerLevel().GetPath());
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::WorldAddTowerPacket* packet) {
|
||||
game::TowerPtr newTower = PlaceTowerAt(packet->GetTowerID(), packet->GetTowerType(), packet->GetTowerX(), packet->GetTowerY(), packet->GetBuilder());
|
||||
|
||||
GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerAdd, newTower);
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
|
||||
game::TowerPtr tower = RemoveTower(packet->GetTowerID());
|
||||
|
||||
if (tower != nullptr) {
|
||||
GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerRemove, tower);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::UpdateMobStatesPacket* packet) {
|
||||
for (auto mobState : packet->GetMobStates()) {
|
||||
game::MobID mobId = mobState.GetMobId();
|
||||
auto it = std::find_if(GetMobList().begin(), GetMobList().end(), [mobId](game::MobPtr mob) { return mob->GetMobID() == mobId; });
|
||||
if (it != GetMobList().end()) {
|
||||
game::MobPtr& mob = *it;
|
||||
mob->SetCenter(mobState.GetMobPosition());
|
||||
mob->SetDirection(mobState.GetMobDirection());
|
||||
mob->SetHealth(mobState.GetMobLife());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(const protocol::UpdateCastleLifePacket* packet) {
|
||||
GetTeam(packet->GetTeamColor()).GetCastle().SetLife(packet->GetCastleLife());
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
166
src/client/render/Renderer.cpp
Normal file
166
src/client/render/Renderer.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Renderer.cpp
|
||||
*
|
||||
* Created on: 4 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "client/render/Renderer.h"
|
||||
#include "client/render/GL.h"
|
||||
|
||||
#include "td/misc/Time.h"
|
||||
#include "td/misc/Easing.h"
|
||||
#include "td/misc/Maths.h"
|
||||
#include "td/misc/Log.h"
|
||||
#include "td/misc/Format.h"
|
||||
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
Renderer::Renderer() : m_BackgroundColor(0, 0, 0) {
|
||||
|
||||
}
|
||||
|
||||
Renderer::~Renderer() {
|
||||
|
||||
}
|
||||
|
||||
void Renderer::InitShaders() {
|
||||
m_WorldShader = std::make_unique<shader::WorldShader>();
|
||||
m_WorldShader->LoadShader();
|
||||
m_EntityShader = std::make_unique<shader::EntityShader>();
|
||||
m_EntityShader->LoadShader();
|
||||
}
|
||||
|
||||
// TODO : change loader check
|
||||
|
||||
bool Renderer::Init() {
|
||||
#if __has_include(<glbinding/glbinding.h>)
|
||||
glbinding::initialize();
|
||||
#elif __has_include(<GL/glew.h>)
|
||||
glewInit();
|
||||
#elif __has_include(<glad/glad.h>)
|
||||
gladLoadGL();
|
||||
#elif __has_include(<GL/gl3w.h>)
|
||||
gl3wInit();
|
||||
#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;
|
||||
}
|
||||
|
||||
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);
|
||||
m_EntityShader->SetColorEffect(model.color);
|
||||
model.vao->Bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.vao->GetVertexCount()));
|
||||
model.vao->Unbind();
|
||||
}
|
||||
|
||||
void Renderer::Prepare() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0);
|
||||
}
|
||||
|
||||
void Renderer::Resize(int width, int height) {
|
||||
m_Camera.projectionMatrix = maths::Perspective(80.0f / 180.0f * PI, static_cast<float>(width) / height, 0.1f, 160.0f);
|
||||
m_Camera.InvProjectionMatrix = maths::Inverse(m_Camera.projectionMatrix);
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetProjectionMatrix(m_Camera.projectionMatrix);
|
||||
m_EntityShader->Start();
|
||||
m_WorldShader->SetProjectionMatrix(m_Camera.projectionMatrix);
|
||||
m_WindowSize = { width, height };
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void Renderer::AddZoom(float zoom) {
|
||||
m_Camera.CamPos.y = std::max(1.0f, m_Camera.CamPos.y - zoom);
|
||||
m_Camera.CamDistance = std::max(1.0f, m_Camera.CamDistance - zoom);
|
||||
SetCamLook(m_Camera.CamLook);
|
||||
}
|
||||
|
||||
void Renderer::SetCamAngularMovement(const Vec2f& mov) {
|
||||
m_Camera.m_Pitch = std::clamp(m_Camera.m_Pitch - mov.y / m_MouseSensitivity, -PI / 2.0f + 0.0000001f, -PI / 12.0f);
|
||||
m_Camera.m_Yaw += mov.x / m_MouseSensitivity;
|
||||
SetCamLook(m_Camera.CamLook);
|
||||
}
|
||||
|
||||
void Renderer::SetCamMovement(const Vec2f& lastCursorPos, const Vec2f& currentCursorPos) {
|
||||
Vec2f worldLastCursorPos = GetCursorWorldPos(lastCursorPos, m_WindowSize.x, m_WindowSize.y);
|
||||
Vec2f worldCurrentCursorPos = GetCursorWorldPos(currentCursorPos, m_WindowSize.x, m_WindowSize.y);
|
||||
Vec2f movement = worldCurrentCursorPos - worldLastCursorPos;
|
||||
SetCamLook(m_Camera.CamLook - movement);
|
||||
}
|
||||
|
||||
void Renderer::SetCamPos(const Vec3f& newPos) {
|
||||
|
||||
Vec3f front = {
|
||||
std::cos(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch),
|
||||
std::sin(m_Camera.m_Pitch),
|
||||
std::sin(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch)
|
||||
};
|
||||
|
||||
m_Camera.CamPos = newPos;
|
||||
m_Camera.viewMatrix = maths::Look(m_Camera.CamPos, front, { 0, 1, 0 });
|
||||
m_Camera.InvViewMatrix = maths::Transpose(maths::Inverse(m_Camera.viewMatrix)); // why transpose ? I don't know
|
||||
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetViewMatrix(m_Camera.viewMatrix);
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetViewMatrix(m_Camera.viewMatrix);
|
||||
}
|
||||
|
||||
void Renderer::SetCamLook(const Vec2f& worldPos) {
|
||||
static const float WORLD_HEIGHT = 0;
|
||||
|
||||
m_Camera.CamLook = worldPos;
|
||||
|
||||
Vec3f front = {
|
||||
std::cos(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch),
|
||||
std::sin(m_Camera.m_Pitch),
|
||||
std::sin(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch)
|
||||
};
|
||||
|
||||
SetCamPos({
|
||||
-m_Camera.CamDistance * front.x + m_Camera.CamLook.x,
|
||||
-m_Camera.CamDistance * front.y + WORLD_HEIGHT,
|
||||
-m_Camera.CamDistance * front.z + m_Camera.CamLook.y
|
||||
});
|
||||
}
|
||||
|
||||
Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) {
|
||||
|
||||
float relativeX = (cursorPos.x / windowWidth * 2) - 1.0f;
|
||||
float relativeY = 1.0f - (cursorPos.y / windowHeight * 2);
|
||||
|
||||
Vec4f rayClip{ relativeX, relativeY, -1.0f, 1.0f };
|
||||
|
||||
Vec4f rayEye = maths::Dot(m_Camera.InvProjectionMatrix, rayClip);
|
||||
|
||||
rayEye = { rayEye.x, rayEye.y, -1.0f, 0.0f };
|
||||
|
||||
Vec4f rayWorld = maths::Dot(m_Camera.InvViewMatrix, rayEye);
|
||||
|
||||
float lambda = -m_Camera.CamPos.y / rayWorld.y;
|
||||
|
||||
return { lambda * rayWorld.x + m_Camera.CamPos.x, lambda * rayWorld.z + m_Camera.CamPos.z };
|
||||
}
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
56
src/client/render/VertexCache.cpp
Normal file
56
src/client/render/VertexCache.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "client/render/VertexCache.h"
|
||||
#include "client/render/loader/GLLoader.h"
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
const static int VERTEX_SIZE = 3;
|
||||
|
||||
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) {
|
||||
auto it = m_Indexes.find(index);
|
||||
if (it != m_Indexes.end()) {
|
||||
m_VertexCount -= it->second.color.size(); // one color per vertex
|
||||
m_Indexes.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void VertexCache::Clear() {
|
||||
m_Indexes.clear();
|
||||
m_VertexCount = 0;
|
||||
}
|
||||
|
||||
void VertexCache::UpdateVertexArray() {
|
||||
m_VertexArray = std::make_unique<GL::VertexArray>(m_VertexCount); // one color per vertex
|
||||
|
||||
Vector positions;
|
||||
positions.reserve(m_VertexCount * VERTEX_SIZE);
|
||||
|
||||
Vector colors;
|
||||
colors.reserve(m_VertexCount);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
GL::VertexBuffer positionsBuffer(positions, VERTEX_SIZE);
|
||||
positionsBuffer.AddVertexAttribPointer(0, VERTEX_SIZE, 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();
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
230
src/client/render/WorldRenderer.cpp
Normal file
230
src/client/render/WorldRenderer.cpp
Normal file
@@ -0,0 +1,230 @@
|
||||
#include "client/render/WorldRenderer.h"
|
||||
#include "client/render/loader/WorldLoader.h"
|
||||
#include "client/render/Renderer.h"
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
#include "gui/imgui/imgui_internal.h"
|
||||
#include "client/window/Display.h"
|
||||
#include "client/game/ClientGame.h"
|
||||
#include "client/Client.h"
|
||||
#include "td/misc/Format.h"
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
WorldRenderer::WorldRenderer(game::World* world, client::ClientGame* client) : m_Client(client), m_Renderer(m_Client->GetRenderer()), m_World(world), m_Zoom(0.1) {
|
||||
m_TowerPlacePopup = std::make_unique<gui::TowerPlacePopup>(m_Client->GetClient());
|
||||
m_TowerUpgradePopup = std::make_unique<gui::TowerUpgradePopup>(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::Update() {
|
||||
if (m_WorldVao == nullptr)
|
||||
return;
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
if (io.MouseDown[0] && !ImGui::IsAnyItemActive() && !ImGui::IsAnyItemHovered()) {
|
||||
ImVec2 mousePos = ImGui::GetIO().MousePos;
|
||||
ImVec2 mouseDelta = ImGui::GetIO().MouseDelta;
|
||||
m_Renderer->SetCamMovement({mousePos.x - mouseDelta.x, mousePos.y - mouseDelta.y}, { mousePos.x, mousePos.y });
|
||||
}
|
||||
|
||||
if (io.MouseDown[1] && !ImGui::IsAnyItemActive() && !ImGui::IsAnyItemHovered()) {
|
||||
ImVec2 mouseDelta = ImGui::GetIO().MouseDelta;
|
||||
m_Renderer->SetCamAngularMovement({ mouseDelta.x, mouseDelta.y });
|
||||
}
|
||||
|
||||
if (io.MouseWheel != 0) {
|
||||
ChangeZoom(io.MouseWheel);
|
||||
}
|
||||
|
||||
UpdateCursorPos();
|
||||
|
||||
if (ImGui::IsMouseClicked(0)) {
|
||||
if (!m_TowerUpgradePopup->IsPopupOpened()) {
|
||||
m_HoldCursorPos = { io.MousePos.x, io.MousePos.y };
|
||||
}
|
||||
}
|
||||
|
||||
if (ImGui::IsMouseDoubleClicked(1)) RemoveTower();
|
||||
}
|
||||
|
||||
void WorldRenderer::RemoveTower() {
|
||||
Vec2f cursorPos = GetCursorWorldPos();
|
||||
|
||||
game::TowerPtr clickedTower = m_World->GetTower(cursorPos);
|
||||
|
||||
if (clickedTower != nullptr) {
|
||||
m_Client->GetClient()->RemoveTower(clickedTower->GetID());
|
||||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::RenderWorld() const {
|
||||
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(), 0, mob->GetCenterY() };
|
||||
model.color = mob->HasTakenDamage() ? Vec3f{ 1, 0.5, 0.5 } : Vec3f{ 1, 1, 1 };
|
||||
m_Renderer->RenderModel(model);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::RenderTowers() const {
|
||||
if (!m_TowersCache.IsEmpty())
|
||||
m_Renderer->RenderVAO(m_TowersCache.GetVertexArray());
|
||||
}
|
||||
|
||||
void WorldRenderer::RenderTileSelect() const {
|
||||
if (ImGui::IsAnyItemHovered()) return;
|
||||
|
||||
if (m_MobTooltip->IsShown() || m_CastleTooltip->IsShown()) return;
|
||||
|
||||
Renderer::Model tileSelectModel;
|
||||
tileSelectModel.vao = m_SelectTileVao.get();
|
||||
tileSelectModel.positon = { std::floor(m_CursorPos.x), 0, std::floor(m_CursorPos.y) };
|
||||
|
||||
m_Renderer->RenderModel(tileSelectModel);
|
||||
}
|
||||
|
||||
void WorldRenderer::RenderPopups() {
|
||||
m_TowerPlacePopup->Render();
|
||||
m_TowerUpgradePopup->Render();
|
||||
}
|
||||
|
||||
void WorldRenderer::Render() {
|
||||
if (m_WorldVao == nullptr)
|
||||
return;
|
||||
RenderWorld();
|
||||
RenderMobs();
|
||||
RenderTowers();
|
||||
RenderTileSelect();
|
||||
RenderTooltips();
|
||||
RenderPopups();
|
||||
DetectClick();
|
||||
}
|
||||
|
||||
WorldRenderer::~WorldRenderer() {
|
||||
|
||||
}
|
||||
|
||||
void WorldRenderer::RenderTooltips() const {
|
||||
RenderMobTooltip();
|
||||
RenderCastleTooltip();
|
||||
}
|
||||
|
||||
void WorldRenderer::MoveCam(float relativeX, float relativeY) {
|
||||
if (m_WorldVao == nullptr)
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
void WorldRenderer::ChangeZoom(float zoomStep) {
|
||||
if (m_WorldVao == nullptr)
|
||||
return;
|
||||
|
||||
m_Renderer->AddZoom(zoomStep);
|
||||
}
|
||||
|
||||
void WorldRenderer::Click() {
|
||||
const game::TowerPtr tower = m_Client->GetWorld().GetTower(GetClickWorldPos());
|
||||
m_TowerPlacePopup->SetClickPos(GetClickWorldPos());
|
||||
m_TowerUpgradePopup->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->SetCamLook({ camX, camY });
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::RenderMobTooltip() const {
|
||||
if (ImGui::IsAnyItemHovered()) return;
|
||||
|
||||
DetectMobHovering();
|
||||
m_MobTooltip->Render();
|
||||
}
|
||||
|
||||
void WorldRenderer::RenderCastleTooltip() const {
|
||||
if (ImGui::IsAnyItemHovered()) return;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void WorldRenderer::OnTowerAdd(game::TowerPtr tower) {
|
||||
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();
|
||||
}
|
||||
|
||||
Vec2f WorldRenderer::GetCursorWorldPos() const {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
return m_Renderer->GetCursorWorldPos({ io.MousePos.x, io.MousePos.y }, Display::GetWindowWidth(), Display::GetWindowHeight());
|
||||
}
|
||||
|
||||
Vec2f WorldRenderer::GetClickWorldPos() const {
|
||||
return m_Renderer->GetCursorWorldPos(m_LastClicked, Display::GetWindowWidth(), Display::GetWindowHeight());
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
35
src/client/render/gui/CastleTooltip.cpp
Normal file
35
src/client/render/gui/CastleTooltip.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "client/render/gui/CastleTooltip.h"
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
#include "client/render/gui/LifeProgress.h"
|
||||
#include "client/render/gui/ImGuiTeamColor.h"
|
||||
|
||||
#include "client/render/WorldRenderer.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
CastleTooltip::CastleTooltip(client::Client* client) : GuiWidget(client) {
|
||||
|
||||
}
|
||||
|
||||
void CastleTooltip::Render() {
|
||||
if (m_Castle == nullptr) return;
|
||||
|
||||
if (ImGui::GetIO().KeyShift) {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, render::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();
|
||||
} else {
|
||||
ImGui::BeginTooltip();
|
||||
RenderLifeProgress(m_Castle->GetLife() / static_cast<float>(game::TeamCastle::CastleMaxLife));
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
37
src/client/render/gui/FrameMenu.cpp
Normal file
37
src/client/render/gui/FrameMenu.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "client/render/gui/FrameMenu.h"
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
FrameMenu::FrameMenu(client::Client* client) : GuiWidget(client), m_VSync(true), m_IsometricView(true), m_ShowDemoWindow(false) {
|
||||
|
||||
}
|
||||
|
||||
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 !defined(NDEBUG)
|
||||
ImGui::Checkbox("Demo Window", &m_ShowDemoWindow);
|
||||
#endif
|
||||
|
||||
ImGui::End();
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
|
||||
if (m_ShowDemoWindow)
|
||||
ImGui::ShowDemoWindow(&m_ShowDemoWindow);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
99
src/client/render/gui/GameMenu.cpp
Normal file
99
src/client/render/gui/GameMenu.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "client/render/gui/GameMenu.h"
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
#include "client/render/gui/ImGuiTeamColor.h"
|
||||
|
||||
#include "client/render/WorldRenderer.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
#include "server/Lobby.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
GameMenu::GameMenu(client::Client* client) : GuiWidget(client), m_SummonMenu(std::make_unique<SummonMenu>(client)) {
|
||||
|
||||
}
|
||||
|
||||
void GameMenu::Render() {
|
||||
if (!m_Client->IsConnected()) return;
|
||||
|
||||
if (GetClient()->GetGame().GetGameState() == td::game::GameState::Lobby) {
|
||||
ImGui::Begin("Lobby");
|
||||
|
||||
ShowTPS();
|
||||
ShowPlayers();
|
||||
ShowLobbyProgress();
|
||||
ShowTeamSelection();
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
if (GetClient()->GetGame().GetGameState() == td::game::GameState::Game) {
|
||||
ImGui::Begin("Game");
|
||||
|
||||
ShowTPS();
|
||||
ShowStats();
|
||||
ShowPlayers();
|
||||
|
||||
ImGui::End();
|
||||
|
||||
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::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 (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");
|
||||
}
|
||||
}
|
||||
|
||||
void GameMenu::ShowTPS() {
|
||||
ImGui::Text("Server TPS : %.1f", GetClient()->GetConnexion().GetServerTPS());
|
||||
ImGui::Text("Server MSPT : %i", (int)GetClient()->GetConnexion().GetServerMSPT());
|
||||
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());
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
19
src/client/render/gui/ImGuiTeamColor.cpp
Normal file
19
src/client/render/gui/ImGuiTeamColor.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "client/render/gui/ImGuiTeamColor.h"
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
ImVec4 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);
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
15
src/client/render/gui/LifeProgress.cpp
Normal file
15
src/client/render/gui/LifeProgress.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "client/render/gui/LifeProgress.h"
|
||||
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
void RenderLifeProgress(float progress) {
|
||||
ImGui::PushStyleColor(ImGuiCol_PlotHistogram, { 1 - progress, progress, 0, 1 });
|
||||
ImGui::ProgressBar(progress, { 100, 25 }, "");
|
||||
ImGui::PopStyleColor();
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
91
src/client/render/gui/MainMenu.cpp
Normal file
91
src/client/render/gui/MainMenu.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "client/render/gui/MainMenu.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
|
||||
#include "server/Server.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
MainMenu::MainMenu(client::Client* client) : GuiWidget(client), m_ConnectPort(25565) {
|
||||
m_ConnectAddress = "localhost";
|
||||
m_ConnectAddress.reserve(256);
|
||||
}
|
||||
|
||||
MainMenu::~MainMenu() {
|
||||
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_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
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
52
src/client/render/gui/MobTooltip.cpp
Normal file
52
src/client/render/gui/MobTooltip.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "client/render/gui/MobTooltip.h"
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
#include "client/render/gui/LifeProgress.h"
|
||||
#include "client/render/gui/ImGuiTeamColor.h"
|
||||
|
||||
#include "client/render/WorldRenderer.h"
|
||||
|
||||
#include "td/game/Mobs.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
MobTooltip::MobTooltip(client::Client* client) : GuiWidget(client) {
|
||||
|
||||
}
|
||||
|
||||
void MobTooltip::Render() {
|
||||
if (m_Mob == nullptr) return;
|
||||
|
||||
// TODO: add sender null check
|
||||
|
||||
if (ImGui::GetIO().KeyShift) {
|
||||
const game::Player* sender = GetClient()->GetGame().GetPlayerById(m_Mob->GetSender());
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::Text("Sender :");
|
||||
ImGui::SameLine();
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, render::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();
|
||||
} else {
|
||||
ImGui::BeginTooltip();
|
||||
RenderLifeProgress(m_Mob->GetHealth() / m_Mob->GetStats()->GetMaxLife());
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
96
src/client/render/gui/SummonMenu.cpp
Normal file
96
src/client/render/gui/SummonMenu.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "client/render/gui/SummonMenu.h"
|
||||
#include "client/Client.h"
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
SummonMenu::SummonMenu(client::Client* client) : GuiWidget(client), m_MenuOpened(true) {
|
||||
m_Values.fill(0);
|
||||
SetCooldown(10);
|
||||
}
|
||||
|
||||
void SummonMenu::SetCooldown(float cooldown) {
|
||||
m_LastCooldown = cooldown;
|
||||
m_Cooldown = cooldown;
|
||||
}
|
||||
|
||||
void SummonMenu::Render() {
|
||||
if (m_Cooldown > 0)
|
||||
m_Cooldown = std::max(0.0f, m_Cooldown - ImGui::GetIO().DeltaTime);
|
||||
|
||||
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 (m_Cooldown > 0) {
|
||||
ImGui::ProgressBar((m_LastCooldown - m_Cooldown) / m_LastCooldown, {}, std::string{ std::to_string((int)m_Cooldown + 1) + "s" }.c_str());
|
||||
} else 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);
|
||||
}
|
||||
}
|
||||
if (mobSent.size() > 0) {
|
||||
m_Client->SendMobs(mobSent);
|
||||
m_Values.fill(0);
|
||||
SetCooldown(10);
|
||||
}
|
||||
}
|
||||
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--;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
86
src/client/render/gui/TowerGui.cpp
Normal file
86
src/client/render/gui/TowerGui.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* TowerGui.cpp
|
||||
*
|
||||
* Created on: 5 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include "client/render/gui/TowerGui.h"
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
|
||||
#include "client/render/gui/MainMenu.h"
|
||||
#include "client/render/gui/GameMenu.h"
|
||||
#include "client/render/gui/FrameMenu.h"
|
||||
#include "client/render/gui/UpdateMenu.h"
|
||||
|
||||
#include "imgui/imgui_impl_opengl3.h"
|
||||
#include "imgui/imgui_impl_sdl.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void TowerGui::BeginFrame() {
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
std::uint64_t delta = time - lastTime;
|
||||
|
||||
m_Client->Tick(delta);
|
||||
|
||||
lastTime = td::utils::GetTime();
|
||||
}
|
||||
|
||||
void TowerGui::Render() {
|
||||
Tick();
|
||||
BeginFrame();
|
||||
|
||||
m_Client->Render();
|
||||
|
||||
m_GuiManager.RenderWidgets();
|
||||
|
||||
EndFrame();
|
||||
}
|
||||
|
||||
TowerGui::~TowerGui() {
|
||||
m_Client->CloseConnection();
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
65
src/client/render/gui/TowerPlacePopup.cpp
Normal file
65
src/client/render/gui/TowerPlacePopup.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "client/render/gui/TowerPlacePopup.h"
|
||||
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
|
||||
#include "td/game/Towers.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
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();
|
||||
|
||||
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()))) {
|
||||
|
||||
ImGui::BeginChild(std::to_string(i).c_str(), ImVec2(m_TowerPopupTileWidth, m_TowerPopupTileHeight), true);
|
||||
|
||||
ImGui::Text("%s", towerInfo.GetName().c_str());
|
||||
|
||||
ImGui::SameLine();
|
||||
ImGui::SetCursorPosX(m_TowerPopupTileWidth - 10 - ImGui::CalcTextSize("(?)").x);
|
||||
ImGui::TextDisabled("(?)");
|
||||
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::Text("%s", towerInfo.GetDescription().c_str());
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (ImGui::Button(buyText.c_str(), ImVec2(m_PlaceTowerButtonWidth, m_PlaceTowerButtonHeight))) {
|
||||
GetClient()->PlaceTower(towerType, m_ClickWorldPos);
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
|
||||
ImGui::EndChild();
|
||||
}
|
||||
}
|
||||
ImGui::EndChild();
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
}
|
||||
|
||||
void TowerPlacePopup::SetClickPos(const Vec2f& worldPos) {
|
||||
m_ClickWorldPos = worldPos;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
100
src/client/render/gui/TowerUpgradePopup.cpp
Normal file
100
src/client/render/gui/TowerUpgradePopup.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "client/render/gui/TowerUpgradePopup.h"
|
||||
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
|
||||
#include "td/game/Towers.h"
|
||||
|
||||
#include "client/Client.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
TowerUpgradePopup::TowerUpgradePopup(client::Client* client) : GuiWidget(client), m_ShouldBeClosed(false), m_Opened(false) {
|
||||
|
||||
}
|
||||
|
||||
void TowerUpgradePopup::SetClickPos(const Vec2f& worldPos) {
|
||||
m_ClickWorldPos = worldPos;
|
||||
}
|
||||
|
||||
bool TowerUpgradePopup::IsPopupOpened() {
|
||||
return m_Opened;
|
||||
}
|
||||
|
||||
void TowerUpgradePopup::Render() {
|
||||
if (ImGui::BeginPopup("TowerUpgrade")) {
|
||||
ImGui::BeginChild("TowerUpgradePopupChild", { 450, 350 });
|
||||
if (m_ShouldBeClosed) {
|
||||
ImGui::CloseCurrentPopup();
|
||||
ImGui::EndPopup();
|
||||
return;
|
||||
}
|
||||
|
||||
game::TowerPtr tower = m_Client->GetGame().GetWorld().GetTower(m_ClickWorldPos);
|
||||
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
|
||||
|
||||
if (x > 0)
|
||||
ImGui::SameLine();
|
||||
|
||||
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();
|
||||
|
||||
bool disabled = towerStats == nullptr;
|
||||
|
||||
int towerLevel = tower->GetLevel().GetLevel();
|
||||
|
||||
bool alreadyUpgraded = currentLevel <= towerLevel;
|
||||
bool canUpgrade = (towerLevel + 1) == currentLevel;
|
||||
|
||||
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->UpgradeTower(tower->GetID(), { currentLevel, currentPath });
|
||||
}
|
||||
} else {
|
||||
ImGui::BeginDisabled();
|
||||
ImGui::Button("Locked", ImVec2(100, 100));
|
||||
ImGui::EndDisabled();
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::EndPopup();
|
||||
|
||||
m_Opened = true;
|
||||
return;
|
||||
}
|
||||
|
||||
m_Opened = false;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
100
src/client/render/gui/UpdateMenu.cpp
Normal file
100
src/client/render/gui/UpdateMenu.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "client/render/gui/UpdateMenu.h"
|
||||
|
||||
#include "client/updater/Updater.h"
|
||||
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
UpdateMenu::UpdateMenu(client::Client* client) : GuiWidget(client), m_Opened(true), m_Updater(std::make_unique<utils::Updater>()) {
|
||||
CheckUpdates();
|
||||
}
|
||||
|
||||
UpdateMenu::~UpdateMenu() {}
|
||||
|
||||
void UpdateMenu::Render() {
|
||||
RenderErrorPopup();
|
||||
if (m_Opened) {
|
||||
ImGui::Begin("Updater", &m_Opened);
|
||||
if (IsUpdateChecked()) {
|
||||
|
||||
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());
|
||||
|
||||
bool canDownloadFile = m_Updater->CanUpdate();
|
||||
|
||||
if (!canDownloadFile) ImGui::BeginDisabled();
|
||||
|
||||
if (ImGui::Button("Download")) {
|
||||
m_Updater->DownloadUpdate();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateMenu::RenderErrorPopup() {
|
||||
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;
|
||||
}
|
||||
|
||||
void UpdateMenu::CheckUpdates() {
|
||||
m_UpdateAvailable = std::async(std::launch::async, [&]() { return m_Updater->CheckUpdate();});
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
871
src/client/render/gui/imgui/Dirent/dirent.h
Normal file
871
src/client/render/gui/imgui/Dirent/dirent.h
Normal file
@@ -0,0 +1,871 @@
|
||||
/*
|
||||
* Dirent interface for Microsoft Visual Studio
|
||||
* Version 1.23.1
|
||||
*
|
||||
* Copyright (C) 2006-2012 Toni Ronkko
|
||||
* This file is part of dirent. Dirent may be freely distributed
|
||||
* under the MIT license. For all details and documentation, see
|
||||
* https://github.com/tronkko/dirent
|
||||
*/
|
||||
#ifndef DIRENT_H
|
||||
#define DIRENT_H
|
||||
|
||||
/*
|
||||
* Include windows.h without Windows Sockets 1.1 to prevent conflicts with
|
||||
* Windows Sockets 2.0.
|
||||
*/
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <wchar.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* Indicates that d_type field is available in dirent structure */
|
||||
#define _DIRENT_HAVE_D_TYPE
|
||||
|
||||
/* Indicates that d_namlen field is available in dirent structure */
|
||||
#define _DIRENT_HAVE_D_NAMLEN
|
||||
|
||||
/* Entries missing from MSVC 6.0 */
|
||||
#if !defined(FILE_ATTRIBUTE_DEVICE)
|
||||
# define FILE_ATTRIBUTE_DEVICE 0x40
|
||||
#endif
|
||||
|
||||
/* File type and permission flags for stat(), general mask */
|
||||
#if !defined(S_IFMT)
|
||||
# define S_IFMT _S_IFMT
|
||||
#endif
|
||||
|
||||
/* Directory bit */
|
||||
#if !defined(S_IFDIR)
|
||||
# define S_IFDIR _S_IFDIR
|
||||
#endif
|
||||
|
||||
/* Character device bit */
|
||||
#if !defined(S_IFCHR)
|
||||
# define S_IFCHR _S_IFCHR
|
||||
#endif
|
||||
|
||||
/* Pipe bit */
|
||||
#if !defined(S_IFFIFO)
|
||||
# define S_IFFIFO _S_IFFIFO
|
||||
#endif
|
||||
|
||||
/* Regular file bit */
|
||||
#if !defined(S_IFREG)
|
||||
# define S_IFREG _S_IFREG
|
||||
#endif
|
||||
|
||||
/* Read permission */
|
||||
#if !defined(S_IREAD)
|
||||
# define S_IREAD _S_IREAD
|
||||
#endif
|
||||
|
||||
/* Write permission */
|
||||
#if !defined(S_IWRITE)
|
||||
# define S_IWRITE _S_IWRITE
|
||||
#endif
|
||||
|
||||
/* Execute permission */
|
||||
#if !defined(S_IEXEC)
|
||||
# define S_IEXEC _S_IEXEC
|
||||
#endif
|
||||
|
||||
/* Pipe */
|
||||
#if !defined(S_IFIFO)
|
||||
# define S_IFIFO _S_IFIFO
|
||||
#endif
|
||||
|
||||
/* Block device */
|
||||
#if !defined(S_IFBLK)
|
||||
# define S_IFBLK 0
|
||||
#endif
|
||||
|
||||
/* Link */
|
||||
#if !defined(S_IFLNK)
|
||||
# define S_IFLNK 0
|
||||
#endif
|
||||
|
||||
/* Socket */
|
||||
#if !defined(S_IFSOCK)
|
||||
# define S_IFSOCK 0
|
||||
#endif
|
||||
|
||||
/* Read user permission */
|
||||
#if !defined(S_IRUSR)
|
||||
# define S_IRUSR S_IREAD
|
||||
#endif
|
||||
|
||||
/* Write user permission */
|
||||
#if !defined(S_IWUSR)
|
||||
# define S_IWUSR S_IWRITE
|
||||
#endif
|
||||
|
||||
/* Execute user permission */
|
||||
#if !defined(S_IXUSR)
|
||||
# define S_IXUSR 0
|
||||
#endif
|
||||
|
||||
/* Read group permission */
|
||||
#if !defined(S_IRGRP)
|
||||
# define S_IRGRP 0
|
||||
#endif
|
||||
|
||||
/* Write group permission */
|
||||
#if !defined(S_IWGRP)
|
||||
# define S_IWGRP 0
|
||||
#endif
|
||||
|
||||
/* Execute group permission */
|
||||
#if !defined(S_IXGRP)
|
||||
# define S_IXGRP 0
|
||||
#endif
|
||||
|
||||
/* Read others permission */
|
||||
#if !defined(S_IROTH)
|
||||
# define S_IROTH 0
|
||||
#endif
|
||||
|
||||
/* Write others permission */
|
||||
#if !defined(S_IWOTH)
|
||||
# define S_IWOTH 0
|
||||
#endif
|
||||
|
||||
/* Execute others permission */
|
||||
#if !defined(S_IXOTH)
|
||||
# define S_IXOTH 0
|
||||
#endif
|
||||
|
||||
/* Maximum length of file name */
|
||||
#if !defined(PATH_MAX)
|
||||
# define PATH_MAX MAX_PATH
|
||||
#endif
|
||||
#if !defined(FILENAME_MAX)
|
||||
# define FILENAME_MAX MAX_PATH
|
||||
#endif
|
||||
#if !defined(NAME_MAX)
|
||||
# define NAME_MAX FILENAME_MAX
|
||||
#endif
|
||||
|
||||
/* File type flags for d_type */
|
||||
#define DT_UNKNOWN 0
|
||||
#define DT_REG S_IFREG
|
||||
#define DT_DIR S_IFDIR
|
||||
#define DT_FIFO S_IFIFO
|
||||
#define DT_SOCK S_IFSOCK
|
||||
#define DT_CHR S_IFCHR
|
||||
#define DT_BLK S_IFBLK
|
||||
#define DT_LNK S_IFLNK
|
||||
|
||||
/* Macros for converting between st_mode and d_type */
|
||||
#define IFTODT(mode) ((mode) & S_IFMT)
|
||||
#define DTTOIF(type) (type)
|
||||
|
||||
/*
|
||||
* File type macros. Note that block devices, sockets and links cannot be
|
||||
* distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
|
||||
* only defined for compatibility. These macros should always return false
|
||||
* on Windows.
|
||||
*/
|
||||
#if !defined(S_ISFIFO)
|
||||
# define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
|
||||
#endif
|
||||
#if !defined(S_ISDIR)
|
||||
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
||||
#endif
|
||||
#if !defined(S_ISREG)
|
||||
# define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
||||
#endif
|
||||
#if !defined(S_ISLNK)
|
||||
# define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
|
||||
#endif
|
||||
#if !defined(S_ISSOCK)
|
||||
# define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
|
||||
#endif
|
||||
#if !defined(S_ISCHR)
|
||||
# define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
|
||||
#endif
|
||||
#if !defined(S_ISBLK)
|
||||
# define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
|
||||
#endif
|
||||
|
||||
/* Return the exact length of the file name without zero terminator */
|
||||
#define _D_EXACT_NAMLEN(p) ((p)->d_namlen)
|
||||
|
||||
/* Return the maximum size of a file name */
|
||||
#define _D_ALLOC_NAMLEN(p) ((PATH_MAX)+1)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Wide-character version */
|
||||
struct _wdirent {
|
||||
/* Always zero */
|
||||
long d_ino;
|
||||
|
||||
/* File position within stream */
|
||||
long d_off;
|
||||
|
||||
/* Structure size */
|
||||
unsigned short d_reclen;
|
||||
|
||||
/* Length of name without \0 */
|
||||
size_t d_namlen;
|
||||
|
||||
/* File type */
|
||||
int d_type;
|
||||
|
||||
/* File name */
|
||||
wchar_t d_name[PATH_MAX+1];
|
||||
};
|
||||
typedef struct _wdirent _wdirent;
|
||||
|
||||
struct _WDIR {
|
||||
/* Current directory entry */
|
||||
struct _wdirent ent;
|
||||
|
||||
/* Private file data */
|
||||
WIN32_FIND_DATAW data;
|
||||
|
||||
/* True if data is valid */
|
||||
int cached;
|
||||
|
||||
/* Win32 search handle */
|
||||
HANDLE handle;
|
||||
|
||||
/* Initial directory name */
|
||||
wchar_t *patt;
|
||||
};
|
||||
typedef struct _WDIR _WDIR;
|
||||
|
||||
/* Multi-byte character version */
|
||||
struct dirent {
|
||||
/* Always zero */
|
||||
long d_ino;
|
||||
|
||||
/* File position within stream */
|
||||
long d_off;
|
||||
|
||||
/* Structure size */
|
||||
unsigned short d_reclen;
|
||||
|
||||
/* Length of name without \0 */
|
||||
size_t d_namlen;
|
||||
|
||||
/* File type */
|
||||
int d_type;
|
||||
|
||||
/* File name */
|
||||
char d_name[PATH_MAX+1];
|
||||
};
|
||||
typedef struct dirent dirent;
|
||||
|
||||
struct DIR {
|
||||
struct dirent ent;
|
||||
struct _WDIR *wdirp;
|
||||
};
|
||||
typedef struct DIR DIR;
|
||||
|
||||
|
||||
/* Dirent functions */
|
||||
static DIR *opendir (const char *dirname);
|
||||
static _WDIR *_wopendir (const wchar_t *dirname);
|
||||
|
||||
static struct dirent *readdir (DIR *dirp);
|
||||
|
||||
static int readdir_r(
|
||||
DIR *dirp, struct dirent *entry, struct dirent **result);
|
||||
|
||||
static int closedir (DIR *dirp);
|
||||
static int _wclosedir (_WDIR *dirp);
|
||||
|
||||
/* For compatibility with Symbian */
|
||||
#define wdirent _wdirent
|
||||
#define WDIR _WDIR
|
||||
#define wopendir _wopendir
|
||||
#define wreaddir _wreaddir
|
||||
#define wclosedir _wclosedir
|
||||
#define wrewinddir _wrewinddir
|
||||
|
||||
|
||||
/* Internal utility functions */
|
||||
static WIN32_FIND_DATAW *dirent_first (_WDIR *dirp);
|
||||
static WIN32_FIND_DATAW *dirent_next (_WDIR *dirp);
|
||||
|
||||
static int dirent_mbstowcs_s(
|
||||
size_t *pReturnValue,
|
||||
wchar_t *wcstr,
|
||||
size_t sizeInWords,
|
||||
const char *mbstr,
|
||||
size_t count);
|
||||
|
||||
static int dirent_wcstombs_s(
|
||||
size_t *pReturnValue,
|
||||
char *mbstr,
|
||||
size_t sizeInBytes,
|
||||
const wchar_t *wcstr,
|
||||
size_t count);
|
||||
|
||||
static void dirent_set_errno (int error);
|
||||
|
||||
|
||||
/*
|
||||
* Open directory stream DIRNAME for read and return a pointer to the
|
||||
* internal working area that is used to retrieve individual directory
|
||||
* entries.
|
||||
*/
|
||||
static _WDIR*
|
||||
_wopendir(
|
||||
const wchar_t *dirname)
|
||||
{
|
||||
_WDIR *dirp = NULL;
|
||||
int error;
|
||||
|
||||
/* Must have directory name */
|
||||
if (dirname == NULL || dirname[0] == '\0') {
|
||||
dirent_set_errno (ENOENT);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate new _WDIR structure */
|
||||
dirp = (_WDIR*) malloc (sizeof (struct _WDIR));
|
||||
if (dirp != NULL) {
|
||||
DWORD n;
|
||||
|
||||
/* Reset _WDIR structure */
|
||||
dirp->handle = INVALID_HANDLE_VALUE;
|
||||
dirp->patt = NULL;
|
||||
dirp->cached = 0;
|
||||
|
||||
/* Compute the length of full path plus zero terminator
|
||||
*
|
||||
* Note that on WinRT there's no way to convert relative paths
|
||||
* into absolute paths, so just assume it is an absolute path.
|
||||
*/
|
||||
# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
|
||||
n = wcslen(dirname);
|
||||
# else
|
||||
n = GetFullPathNameW (dirname, 0, NULL, NULL);
|
||||
# endif
|
||||
|
||||
/* Allocate room for absolute directory name and search pattern */
|
||||
dirp->patt = (wchar_t*) malloc (sizeof (wchar_t) * n + 16);
|
||||
if (dirp->patt) {
|
||||
|
||||
/*
|
||||
* Convert relative directory name to an absolute one. This
|
||||
* allows rewinddir() to function correctly even when current
|
||||
* working directory is changed between opendir() and rewinddir().
|
||||
*
|
||||
* Note that on WinRT there's no way to convert relative paths
|
||||
* into absolute paths, so just assume it is an absolute path.
|
||||
*/
|
||||
# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
|
||||
wcsncpy_s(dirp->patt, n+1, dirname, n);
|
||||
# else
|
||||
n = GetFullPathNameW (dirname, n, dirp->patt, NULL);
|
||||
# endif
|
||||
if (n > 0) {
|
||||
wchar_t *p;
|
||||
|
||||
/* Append search pattern \* to the directory name */
|
||||
p = dirp->patt + n;
|
||||
if (dirp->patt < p) {
|
||||
switch (p[-1]) {
|
||||
case '\\':
|
||||
case '/':
|
||||
case ':':
|
||||
/* Directory ends in path separator, e.g. c:\temp\ */
|
||||
/*NOP*/;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Directory name doesn't end in path separator */
|
||||
*p++ = '\\';
|
||||
}
|
||||
}
|
||||
*p++ = '*';
|
||||
*p = '\0';
|
||||
|
||||
/* Open directory stream and retrieve the first entry */
|
||||
if (dirent_first (dirp)) {
|
||||
/* Directory stream opened successfully */
|
||||
error = 0;
|
||||
} else {
|
||||
/* Cannot retrieve first entry */
|
||||
error = 1;
|
||||
dirent_set_errno (ENOENT);
|
||||
}
|
||||
|
||||
} else {
|
||||
/* Cannot retrieve full path name */
|
||||
dirent_set_errno (ENOENT);
|
||||
error = 1;
|
||||
}
|
||||
|
||||
} else {
|
||||
/* Cannot allocate memory for search pattern */
|
||||
error = 1;
|
||||
}
|
||||
|
||||
} else {
|
||||
/* Cannot allocate _WDIR structure */
|
||||
error = 1;
|
||||
}
|
||||
|
||||
/* Clean up in case of error */
|
||||
if (error && dirp) {
|
||||
_wclosedir (dirp);
|
||||
dirp = NULL;
|
||||
}
|
||||
|
||||
return dirp;
|
||||
}
|
||||
|
||||
/*
|
||||
* Close directory stream opened by opendir() function. This invalidates the
|
||||
* DIR structure as well as any directory entry read previously by
|
||||
* _wreaddir().
|
||||
*/
|
||||
static int
|
||||
_wclosedir(
|
||||
_WDIR *dirp)
|
||||
{
|
||||
int ok;
|
||||
if (dirp) {
|
||||
|
||||
/* Release search handle */
|
||||
if (dirp->handle != INVALID_HANDLE_VALUE) {
|
||||
FindClose (dirp->handle);
|
||||
dirp->handle = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
/* Release search pattern */
|
||||
if (dirp->patt) {
|
||||
free (dirp->patt);
|
||||
dirp->patt = NULL;
|
||||
}
|
||||
|
||||
/* Release directory structure */
|
||||
free (dirp);
|
||||
ok = /*success*/0;
|
||||
|
||||
} else {
|
||||
|
||||
/* Invalid directory stream */
|
||||
dirent_set_errno (EBADF);
|
||||
ok = /*failure*/-1;
|
||||
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* Get first directory entry (internal) */
|
||||
static WIN32_FIND_DATAW*
|
||||
dirent_first(
|
||||
_WDIR *dirp)
|
||||
{
|
||||
WIN32_FIND_DATAW *datap;
|
||||
|
||||
/* Open directory and retrieve the first entry */
|
||||
dirp->handle = FindFirstFileExW(
|
||||
dirp->patt, FindExInfoStandard, &dirp->data,
|
||||
FindExSearchNameMatch, NULL, 0);
|
||||
if (dirp->handle != INVALID_HANDLE_VALUE) {
|
||||
|
||||
/* a directory entry is now waiting in memory */
|
||||
datap = &dirp->data;
|
||||
dirp->cached = 1;
|
||||
|
||||
} else {
|
||||
|
||||
/* Failed to re-open directory: no directory entry in memory */
|
||||
dirp->cached = 0;
|
||||
datap = NULL;
|
||||
|
||||
}
|
||||
return datap;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get next directory entry (internal).
|
||||
*
|
||||
* Returns
|
||||
*/
|
||||
static WIN32_FIND_DATAW*
|
||||
dirent_next(
|
||||
_WDIR *dirp)
|
||||
{
|
||||
WIN32_FIND_DATAW *p;
|
||||
|
||||
/* Get next directory entry */
|
||||
if (dirp->cached != 0) {
|
||||
|
||||
/* A valid directory entry already in memory */
|
||||
p = &dirp->data;
|
||||
dirp->cached = 0;
|
||||
|
||||
} else if (dirp->handle != INVALID_HANDLE_VALUE) {
|
||||
|
||||
/* Get the next directory entry from stream */
|
||||
if (FindNextFileW (dirp->handle, &dirp->data) != FALSE) {
|
||||
/* Got a file */
|
||||
p = &dirp->data;
|
||||
} else {
|
||||
/* The very last entry has been processed or an error occurred */
|
||||
FindClose (dirp->handle);
|
||||
dirp->handle = INVALID_HANDLE_VALUE;
|
||||
p = NULL;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
/* End of directory stream reached */
|
||||
p = NULL;
|
||||
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
* Open directory stream using plain old C-string.
|
||||
*/
|
||||
static DIR*
|
||||
opendir(
|
||||
const char *dirname)
|
||||
{
|
||||
struct DIR *dirp;
|
||||
int error;
|
||||
|
||||
/* Must have directory name */
|
||||
if (dirname == NULL || dirname[0] == '\0') {
|
||||
dirent_set_errno (ENOENT);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate memory for DIR structure */
|
||||
dirp = (DIR*) malloc (sizeof (struct DIR));
|
||||
if (dirp) {
|
||||
wchar_t wname[PATH_MAX + 1];
|
||||
size_t n;
|
||||
|
||||
/* Convert directory name to wide-character string */
|
||||
error = dirent_mbstowcs_s(
|
||||
&n, wname, PATH_MAX + 1, dirname, PATH_MAX + 1);
|
||||
if (!error) {
|
||||
|
||||
/* Open directory stream using wide-character name */
|
||||
dirp->wdirp = _wopendir (wname);
|
||||
if (dirp->wdirp) {
|
||||
/* Directory stream opened */
|
||||
error = 0;
|
||||
} else {
|
||||
/* Failed to open directory stream */
|
||||
error = 1;
|
||||
}
|
||||
|
||||
} else {
|
||||
/*
|
||||
* Cannot convert file name to wide-character string. This
|
||||
* occurs if the string contains invalid multi-byte sequences or
|
||||
* the output buffer is too small to contain the resulting
|
||||
* string.
|
||||
*/
|
||||
error = 1;
|
||||
}
|
||||
|
||||
} else {
|
||||
/* Cannot allocate DIR structure */
|
||||
error = 1;
|
||||
}
|
||||
|
||||
/* Clean up in case of error */
|
||||
if (error && dirp) {
|
||||
free (dirp);
|
||||
dirp = NULL;
|
||||
}
|
||||
|
||||
return dirp;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read next directory entry.
|
||||
*/
|
||||
static struct dirent*
|
||||
readdir(
|
||||
DIR *dirp)
|
||||
{
|
||||
struct dirent *entry;
|
||||
|
||||
/*
|
||||
* Read directory entry to buffer. We can safely ignore the return value
|
||||
* as entry will be set to NULL in case of error.
|
||||
*/
|
||||
(void) readdir_r (dirp, &dirp->ent, &entry);
|
||||
|
||||
/* Return pointer to statically allocated directory entry */
|
||||
return entry;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read next directory entry into called-allocated buffer.
|
||||
*
|
||||
* Returns zero on success. If the end of directory stream is reached, then
|
||||
* sets result to NULL and returns zero.
|
||||
*/
|
||||
static int
|
||||
readdir_r(
|
||||
DIR *dirp,
|
||||
struct dirent *entry,
|
||||
struct dirent **result)
|
||||
{
|
||||
WIN32_FIND_DATAW *datap;
|
||||
|
||||
/* Read next directory entry */
|
||||
datap = dirent_next (dirp->wdirp);
|
||||
if (datap) {
|
||||
size_t n;
|
||||
int error;
|
||||
|
||||
/* Attempt to convert file name to multi-byte string */
|
||||
error = dirent_wcstombs_s(
|
||||
&n, entry->d_name, PATH_MAX + 1, datap->cFileName, PATH_MAX + 1);
|
||||
|
||||
/*
|
||||
* If the file name cannot be represented by a multi-byte string,
|
||||
* then attempt to use old 8+3 file name. This allows traditional
|
||||
* Unix-code to access some file names despite of unicode
|
||||
* characters, although file names may seem unfamiliar to the user.
|
||||
*
|
||||
* Be ware that the code below cannot come up with a short file
|
||||
* name unless the file system provides one. At least
|
||||
* VirtualBox shared folders fail to do this.
|
||||
*/
|
||||
if (error && datap->cAlternateFileName[0] != '\0') {
|
||||
error = dirent_wcstombs_s(
|
||||
&n, entry->d_name, PATH_MAX + 1,
|
||||
datap->cAlternateFileName, PATH_MAX + 1);
|
||||
}
|
||||
|
||||
if (!error) {
|
||||
DWORD attr;
|
||||
|
||||
/* Length of file name excluding zero terminator */
|
||||
entry->d_namlen = n - 1;
|
||||
|
||||
/* File attributes */
|
||||
attr = datap->dwFileAttributes;
|
||||
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
|
||||
entry->d_type = DT_CHR;
|
||||
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
|
||||
entry->d_type = DT_DIR;
|
||||
} else {
|
||||
entry->d_type = DT_REG;
|
||||
}
|
||||
|
||||
/* Reset dummy fields */
|
||||
entry->d_ino = 0;
|
||||
entry->d_off = 0;
|
||||
entry->d_reclen = sizeof (struct dirent);
|
||||
|
||||
} else {
|
||||
|
||||
/*
|
||||
* Cannot convert file name to multi-byte string so construct
|
||||
* an erroneous directory entry and return that. Note that
|
||||
* we cannot return NULL as that would stop the processing
|
||||
* of directory entries completely.
|
||||
*/
|
||||
entry->d_name[0] = '?';
|
||||
entry->d_name[1] = '\0';
|
||||
entry->d_namlen = 1;
|
||||
entry->d_type = DT_UNKNOWN;
|
||||
entry->d_ino = 0;
|
||||
entry->d_off = -1;
|
||||
entry->d_reclen = 0;
|
||||
|
||||
}
|
||||
|
||||
/* Return pointer to directory entry */
|
||||
*result = entry;
|
||||
|
||||
} else {
|
||||
|
||||
/* No more directory entries */
|
||||
*result = NULL;
|
||||
|
||||
}
|
||||
|
||||
return /*OK*/0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Close directory stream.
|
||||
*/
|
||||
static int
|
||||
closedir(
|
||||
DIR *dirp)
|
||||
{
|
||||
int ok;
|
||||
if (dirp) {
|
||||
|
||||
/* Close wide-character directory stream */
|
||||
ok = _wclosedir (dirp->wdirp);
|
||||
dirp->wdirp = NULL;
|
||||
|
||||
/* Release multi-byte character version */
|
||||
free (dirp);
|
||||
|
||||
} else {
|
||||
|
||||
/* Invalid directory stream */
|
||||
dirent_set_errno (EBADF);
|
||||
ok = /*failure*/-1;
|
||||
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* Convert multi-byte string to wide character string */
|
||||
static int
|
||||
dirent_mbstowcs_s(
|
||||
size_t *pReturnValue,
|
||||
wchar_t *wcstr,
|
||||
size_t sizeInWords,
|
||||
const char *mbstr,
|
||||
size_t count)
|
||||
{
|
||||
int error;
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
||||
|
||||
/* Microsoft Visual Studio 2005 or later */
|
||||
error = mbstowcs_s (pReturnValue, wcstr, sizeInWords, mbstr, count);
|
||||
|
||||
#else
|
||||
|
||||
/* Older Visual Studio or non-Microsoft compiler */
|
||||
size_t n;
|
||||
|
||||
/* Convert to wide-character string (or count characters) */
|
||||
n = mbstowcs (wcstr, mbstr, sizeInWords);
|
||||
if (!wcstr || n < count) {
|
||||
|
||||
/* Zero-terminate output buffer */
|
||||
if (wcstr && sizeInWords) {
|
||||
if (n >= sizeInWords) {
|
||||
n = sizeInWords - 1;
|
||||
}
|
||||
wcstr[n] = 0;
|
||||
}
|
||||
|
||||
/* Length of resulting multi-byte string WITH zero terminator */
|
||||
if (pReturnValue) {
|
||||
*pReturnValue = n + 1;
|
||||
}
|
||||
|
||||
/* Success */
|
||||
error = 0;
|
||||
|
||||
} else {
|
||||
|
||||
/* Could not convert string */
|
||||
error = 1;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/* Convert wide-character string to multi-byte string */
|
||||
static int
|
||||
dirent_wcstombs_s(
|
||||
size_t *pReturnValue,
|
||||
char *mbstr,
|
||||
size_t sizeInBytes, /* max size of mbstr */
|
||||
const wchar_t *wcstr,
|
||||
size_t count)
|
||||
{
|
||||
int error;
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
||||
|
||||
/* Microsoft Visual Studio 2005 or later */
|
||||
error = wcstombs_s (pReturnValue, mbstr, sizeInBytes, wcstr, count);
|
||||
|
||||
#else
|
||||
|
||||
/* Older Visual Studio or non-Microsoft compiler */
|
||||
size_t n;
|
||||
|
||||
/* Convert to multi-byte string (or count the number of bytes needed) */
|
||||
n = wcstombs (mbstr, wcstr, sizeInBytes);
|
||||
if (!mbstr || n < count) {
|
||||
|
||||
/* Zero-terminate output buffer */
|
||||
if (mbstr && sizeInBytes) {
|
||||
if (n >= sizeInBytes) {
|
||||
n = sizeInBytes - 1;
|
||||
}
|
||||
mbstr[n] = '\0';
|
||||
}
|
||||
|
||||
/* Length of resulting multi-bytes string WITH zero-terminator */
|
||||
if (pReturnValue) {
|
||||
*pReturnValue = n + 1;
|
||||
}
|
||||
|
||||
/* Success */
|
||||
error = 0;
|
||||
|
||||
} else {
|
||||
|
||||
/* Cannot convert string */
|
||||
error = 1;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/* Set errno variable */
|
||||
static void
|
||||
dirent_set_errno(
|
||||
int error)
|
||||
{
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
||||
|
||||
/* Microsoft Visual Studio 2005 and later */
|
||||
_set_errno (error);
|
||||
|
||||
#else
|
||||
|
||||
/* Non-Microsoft compiler or older Microsoft compiler */
|
||||
errno = error;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /*DIRENT_H*/
|
||||
|
||||
12255
src/client/render/gui/imgui/imgui.cpp
Normal file
12255
src/client/render/gui/imgui/imgui.cpp
Normal file
File diff suppressed because it is too large
Load Diff
7675
src/client/render/gui/imgui/imgui_demo.cpp
Normal file
7675
src/client/render/gui/imgui/imgui_demo.cpp
Normal file
File diff suppressed because it is too large
Load Diff
6200
src/client/render/gui/imgui/imgui_draw.cpp
Normal file
6200
src/client/render/gui/imgui/imgui_draw.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1197
src/client/render/gui/imgui/imgui_filebrowser.cpp
Normal file
1197
src/client/render/gui/imgui/imgui_filebrowser.cpp
Normal file
File diff suppressed because it is too large
Load Diff
677
src/client/render/gui/imgui/imgui_impl_opengl3.cpp
Executable file
677
src/client/render/gui/imgui/imgui_impl_opengl3.cpp
Executable file
@@ -0,0 +1,677 @@
|
||||
// dear imgui: Renderer for modern OpenGL with shaders / programmatic pipeline
|
||||
// - Desktop GL: 2.x 3.x 4.x
|
||||
// - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0)
|
||||
// This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
|
||||
// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
||||
// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
// CHANGELOG
|
||||
// (minor and older changes stripped away, please see git history for details)
|
||||
// 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX.
|
||||
// 2020-04-21: OpenGL: Fixed handling of glClipControl(GL_UPPER_LEFT) by inverting projection matrix.
|
||||
// 2020-04-12: OpenGL: Fixed context version check mistakenly testing for 4.0+ instead of 3.2+ to enable ImGuiBackendFlags_RendererHasVtxOffset.
|
||||
// 2020-03-24: OpenGL: Added support for glbinding 2.x OpenGL loader.
|
||||
// 2020-01-07: OpenGL: Added support for glbinding 3.x OpenGL loader.
|
||||
// 2019-10-25: OpenGL: Using a combination of GL define and runtime GL version to decide whether to use glDrawElementsBaseVertex(). Fix building with pre-3.2 GL loaders.
|
||||
// 2019-09-22: OpenGL: Detect default GL loader using __has_include compiler facility.
|
||||
// 2019-09-16: OpenGL: Tweak initialization code to allow application calling ImGui_ImplOpenGL3_CreateFontsTexture() before the first NewFrame() call.
|
||||
// 2019-05-29: OpenGL: Desktop GL only: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
|
||||
// 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
|
||||
// 2019-03-29: OpenGL: Not calling glBindBuffer more than necessary in the render loop.
|
||||
// 2019-03-15: OpenGL: Added a dummy GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early.
|
||||
// 2019-03-03: OpenGL: Fix support for ES 2.0 (WebGL 1.0).
|
||||
// 2019-02-20: OpenGL: Fix for OSX not supporting OpenGL 4.5, we don't try to read GL_CLIP_ORIGIN even if defined by the headers/loader.
|
||||
// 2019-02-11: OpenGL: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display.
|
||||
// 2019-02-01: OpenGL: Using GLSL 410 shaders for any version over 410 (e.g. 430, 450).
|
||||
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
|
||||
// 2018-11-13: OpenGL: Support for GL 4.5's glClipControl(GL_UPPER_LEFT) / GL_CLIP_ORIGIN.
|
||||
// 2018-08-29: OpenGL: Added support for more OpenGL loaders: glew and glad, with comments indicative that any loader can be used.
|
||||
// 2018-08-09: OpenGL: Default to OpenGL ES 3 on iOS and Android. GLSL version default to "#version 300 ES".
|
||||
// 2018-07-30: OpenGL: Support for GLSL 300 ES and 410 core. Fixes for Emscripten compilation.
|
||||
// 2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link.
|
||||
// 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples.
|
||||
// 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
|
||||
// 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state.
|
||||
// 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a NULL pointer.
|
||||
// 2018-03-06: OpenGL: Added const char* glsl_version parameter to ImGui_ImplOpenGL3_Init() so user can override the GLSL version e.g. "#version 150".
|
||||
// 2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context.
|
||||
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself.
|
||||
// 2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150.
|
||||
// 2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode.
|
||||
// 2017-05-01: OpenGL: Fixed save and restore of current blend func state.
|
||||
// 2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE.
|
||||
// 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle.
|
||||
// 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752)
|
||||
|
||||
//----------------------------------------
|
||||
// OpenGL GLSL GLSL
|
||||
// version version string
|
||||
//----------------------------------------
|
||||
// 2.0 110 "#version 110"
|
||||
// 2.1 120 "#version 120"
|
||||
// 3.0 130 "#version 130"
|
||||
// 3.1 140 "#version 140"
|
||||
// 3.2 150 "#version 150"
|
||||
// 3.3 330 "#version 330 core"
|
||||
// 4.0 400 "#version 400 core"
|
||||
// 4.1 410 "#version 410 core"
|
||||
// 4.2 420 "#version 410 core"
|
||||
// 4.3 430 "#version 430 core"
|
||||
// ES 2.0 100 "#version 100" = WebGL 1.0
|
||||
// ES 3.0 300 "#version 300 es" = WebGL 2.0
|
||||
//----------------------------------------
|
||||
|
||||
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
#include "imgui_impl_opengl3.h"
|
||||
#include <stdio.h>
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier
|
||||
#include <stddef.h> // intptr_t
|
||||
#else
|
||||
#include <stdint.h> // intptr_t
|
||||
#endif
|
||||
|
||||
|
||||
// GL includes
|
||||
#if defined(IMGUI_IMPL_OPENGL_ES2)
|
||||
#include <GLES2/gl2.h>
|
||||
#elif defined(IMGUI_IMPL_OPENGL_ES3)
|
||||
#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV))
|
||||
#include <OpenGLES/ES3/gl.h> // Use GL ES 3
|
||||
#else
|
||||
#include <GLES3/gl3.h> // Use GL ES 3
|
||||
#endif
|
||||
#else
|
||||
// About Desktop OpenGL function loaders:
|
||||
// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers.
|
||||
// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad).
|
||||
// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
|
||||
#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
|
||||
#include <GL/gl3w.h> // Needs to be initialized with gl3wInit() in user's code
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
|
||||
#include <GL/glew.h> // Needs to be initialized with glewInit() in user's code.
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
|
||||
#include <glad/glad.h> // Needs to be initialized with gladLoadGL() in user's code.
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
|
||||
#ifndef GLFW_INCLUDE_NONE
|
||||
#define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors.
|
||||
#endif
|
||||
#include <glbinding/Binding.h> // Needs to be initialized with glbinding::Binding::initialize() in user's code.
|
||||
#include <glbinding/gl/gl.h>
|
||||
using namespace gl;
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
|
||||
#ifndef GLFW_INCLUDE_NONE
|
||||
#define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors.
|
||||
#endif
|
||||
#include <glbinding/glbinding.h>// Needs to be initialized with glbinding::initialize() in user's code.
|
||||
#include <glbinding/gl/gl.h>
|
||||
using namespace gl;
|
||||
#else
|
||||
#include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have.
|
||||
#if defined(IMGUI_IMPL_OPENGL_ES2) || defined(IMGUI_IMPL_OPENGL_ES3) || !defined(GL_VERSION_3_2)
|
||||
#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 0
|
||||
#else
|
||||
#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 1
|
||||
#endif
|
||||
|
||||
// OpenGL Data
|
||||
static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2)
|
||||
static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings.
|
||||
static GLuint g_FontTexture = 0;
|
||||
static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
|
||||
static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location
|
||||
static int g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location
|
||||
static unsigned int g_VboHandle = 0, g_ElementsHandle = 0;
|
||||
|
||||
// Functions
|
||||
bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
|
||||
{
|
||||
// Query for GL version (e.g. 320 for GL 3.2)
|
||||
#if !defined(IMGUI_IMPL_OPENGL_ES2)
|
||||
GLint major, minor;
|
||||
glGetIntegerv(GL_MAJOR_VERSION, &major);
|
||||
glGetIntegerv(GL_MINOR_VERSION, &minor);
|
||||
g_GlVersion = major * 100 + minor * 10;
|
||||
#else
|
||||
g_GlVersion = 200; // GLES 2
|
||||
#endif
|
||||
|
||||
// Setup back-end capabilities flags
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.BackendRendererName = "imgui_impl_opengl3";
|
||||
#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
|
||||
if (g_GlVersion >= 320)
|
||||
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
|
||||
#endif
|
||||
|
||||
// Store GLSL version string so we can refer to it later in case we recreate shaders.
|
||||
// Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure.
|
||||
#if defined(IMGUI_IMPL_OPENGL_ES2)
|
||||
if (glsl_version == NULL)
|
||||
glsl_version = "#version 100";
|
||||
#elif defined(IMGUI_IMPL_OPENGL_ES3)
|
||||
if (glsl_version == NULL)
|
||||
glsl_version = "#version 300 es";
|
||||
#elif defined(__APPLE__)
|
||||
if (glsl_version == NULL)
|
||||
glsl_version = "#version 150";
|
||||
#else
|
||||
if (glsl_version == NULL)
|
||||
glsl_version = "#version 130";
|
||||
#endif
|
||||
IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString));
|
||||
strcpy(g_GlslVersionString, glsl_version);
|
||||
strcat(g_GlslVersionString, "\n");
|
||||
|
||||
// Dummy construct to make it easily visible in the IDE and debugger which GL loader has been selected.
|
||||
// The code actually never uses the 'gl_loader' variable! It is only here so you can read it!
|
||||
// If auto-detection fails or doesn't select the same GL loader file as used by your application,
|
||||
// you are likely to get a crash below.
|
||||
// You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line.
|
||||
const char* gl_loader = "Unknown";
|
||||
IM_UNUSED(gl_loader);
|
||||
#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
|
||||
gl_loader = "GL3W";
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
|
||||
gl_loader = "GLEW";
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
|
||||
gl_loader = "GLAD";
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
|
||||
gl_loader = "glbinding2";
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
|
||||
gl_loader = "glbinding3";
|
||||
#elif defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM)
|
||||
gl_loader = "custom";
|
||||
#else
|
||||
gl_loader = "none";
|
||||
#endif
|
||||
|
||||
// Make a dummy GL call (we don't actually need the result)
|
||||
// IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code.
|
||||
// Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
|
||||
GLint current_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ImGui_ImplOpenGL3_Shutdown()
|
||||
{
|
||||
ImGui_ImplOpenGL3_DestroyDeviceObjects();
|
||||
}
|
||||
|
||||
void ImGui_ImplOpenGL3_NewFrame()
|
||||
{
|
||||
if (!g_ShaderHandle)
|
||||
ImGui_ImplOpenGL3_CreateDeviceObjects();
|
||||
}
|
||||
|
||||
static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object)
|
||||
{
|
||||
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
|
||||
glEnable(GL_BLEND);
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
#ifdef GL_POLYGON_MODE
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
#endif
|
||||
|
||||
// Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
|
||||
bool clip_origin_lower_left = true;
|
||||
#if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__)
|
||||
GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)¤t_clip_origin);
|
||||
if (current_clip_origin == GL_UPPER_LEFT)
|
||||
clip_origin_lower_left = false;
|
||||
#endif
|
||||
|
||||
// Setup viewport, orthographic projection matrix
|
||||
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
|
||||
glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
|
||||
float L = draw_data->DisplayPos.x;
|
||||
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
||||
float T = draw_data->DisplayPos.y;
|
||||
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
||||
if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left
|
||||
const float ortho_projection[4][4] =
|
||||
{
|
||||
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
|
||||
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
|
||||
{ 0.0f, 0.0f, -1.0f, 0.0f },
|
||||
{ (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f },
|
||||
};
|
||||
glUseProgram(g_ShaderHandle);
|
||||
glUniform1i(g_AttribLocationTex, 0);
|
||||
glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]);
|
||||
#ifdef GL_SAMPLER_BINDING
|
||||
glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise.
|
||||
#endif
|
||||
|
||||
(void)vertex_array_object;
|
||||
#ifndef IMGUI_IMPL_OPENGL_ES2
|
||||
glBindVertexArray(vertex_array_object);
|
||||
#endif
|
||||
|
||||
// Bind vertex/index buffers and setup attributes for ImDrawVert
|
||||
glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle);
|
||||
glEnableVertexAttribArray(g_AttribLocationVtxPos);
|
||||
glEnableVertexAttribArray(g_AttribLocationVtxUV);
|
||||
glEnableVertexAttribArray(g_AttribLocationVtxColor);
|
||||
glVertexAttribPointer(g_AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos));
|
||||
glVertexAttribPointer(g_AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv));
|
||||
glVertexAttribPointer(g_AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col));
|
||||
}
|
||||
|
||||
// OpenGL3 Render function.
|
||||
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
|
||||
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
|
||||
void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
|
||||
{
|
||||
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
|
||||
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
|
||||
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
|
||||
if (fb_width <= 0 || fb_height <= 0)
|
||||
return;
|
||||
|
||||
// Backup GL state
|
||||
GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
|
||||
GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||
#ifdef GL_SAMPLER_BINDING
|
||||
GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler);
|
||||
#endif
|
||||
GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
|
||||
#ifndef IMGUI_IMPL_OPENGL_ES2
|
||||
GLint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array_object);
|
||||
#endif
|
||||
#ifdef GL_POLYGON_MODE
|
||||
GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
|
||||
#endif
|
||||
GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
|
||||
GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
|
||||
GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
|
||||
GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb);
|
||||
GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha);
|
||||
GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha);
|
||||
GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb);
|
||||
GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha);
|
||||
GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
|
||||
GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
|
||||
GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
|
||||
GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
|
||||
|
||||
// Setup desired GL state
|
||||
// Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts)
|
||||
// The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound.
|
||||
GLuint vertex_array_object = 0;
|
||||
#ifndef IMGUI_IMPL_OPENGL_ES2
|
||||
glGenVertexArrays(1, &vertex_array_object);
|
||||
#endif
|
||||
ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
|
||||
|
||||
// Will project scissor/clipping rectangles into framebuffer space
|
||||
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
|
||||
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
|
||||
|
||||
// Render command lists
|
||||
for (int n = 0; n < draw_data->CmdListsCount; n++)
|
||||
{
|
||||
const ImDrawList* cmd_list = draw_data->CmdLists[n];
|
||||
|
||||
// Upload vertex/index buffers
|
||||
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW);
|
||||
|
||||
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
|
||||
{
|
||||
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
|
||||
if (pcmd->UserCallback != NULL)
|
||||
{
|
||||
// User callback, registered via ImDrawList::AddCallback()
|
||||
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
|
||||
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
|
||||
ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
|
||||
else
|
||||
pcmd->UserCallback(cmd_list, pcmd);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Project scissor/clipping rectangles into framebuffer space
|
||||
ImVec4 clip_rect;
|
||||
clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x;
|
||||
clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y;
|
||||
clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x;
|
||||
clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y;
|
||||
|
||||
if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
|
||||
{
|
||||
// Apply scissor/clipping rectangle
|
||||
glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y));
|
||||
|
||||
// Bind texture, Draw
|
||||
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
|
||||
#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
|
||||
if (g_GlVersion >= 320)
|
||||
glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset);
|
||||
else
|
||||
#endif
|
||||
glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Destroy the temporary VAO
|
||||
#ifndef IMGUI_IMPL_OPENGL_ES2
|
||||
glDeleteVertexArrays(1, &vertex_array_object);
|
||||
#endif
|
||||
|
||||
// Restore modified GL state
|
||||
glUseProgram(last_program);
|
||||
glBindTexture(GL_TEXTURE_2D, last_texture);
|
||||
#ifdef GL_SAMPLER_BINDING
|
||||
glBindSampler(0, last_sampler);
|
||||
#endif
|
||||
glActiveTexture(last_active_texture);
|
||||
#ifndef IMGUI_IMPL_OPENGL_ES2
|
||||
glBindVertexArray(last_vertex_array_object);
|
||||
#endif
|
||||
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
|
||||
glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
|
||||
glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
|
||||
if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
|
||||
if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
|
||||
if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
|
||||
if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
|
||||
#ifdef GL_POLYGON_MODE
|
||||
glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]);
|
||||
#endif
|
||||
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
|
||||
glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
|
||||
}
|
||||
|
||||
bool ImGui_ImplOpenGL3_CreateFontsTexture()
|
||||
{
|
||||
// Build texture atlas
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
unsigned char* pixels;
|
||||
int width, height;
|
||||
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
|
||||
|
||||
// Upload texture to graphics system
|
||||
GLint last_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||
glGenTextures(1, &g_FontTexture);
|
||||
glBindTexture(GL_TEXTURE_2D, g_FontTexture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
#ifdef GL_UNPACK_ROW_LENGTH
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
||||
#endif
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
// Store our identifier
|
||||
io.Fonts->TexID = (ImTextureID)(intptr_t)g_FontTexture;
|
||||
|
||||
// Restore state
|
||||
glBindTexture(GL_TEXTURE_2D, last_texture);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ImGui_ImplOpenGL3_DestroyFontsTexture()
|
||||
{
|
||||
if (g_FontTexture)
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
glDeleteTextures(1, &g_FontTexture);
|
||||
io.Fonts->TexID = 0;
|
||||
g_FontTexture = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file.
|
||||
static bool CheckShader(GLuint handle, const char* desc)
|
||||
{
|
||||
GLint status = 0, log_length = 0;
|
||||
glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
|
||||
glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
|
||||
if ((GLboolean)status == GL_FALSE)
|
||||
fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc);
|
||||
if (log_length > 1)
|
||||
{
|
||||
ImVector<char> buf;
|
||||
buf.resize((int)(log_length + 1));
|
||||
glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
|
||||
fprintf(stderr, "%s\n", buf.begin());
|
||||
}
|
||||
return (GLboolean)status == GL_TRUE;
|
||||
}
|
||||
|
||||
// If you get an error please report on GitHub. You may try different GL context version or GLSL version.
|
||||
static bool CheckProgram(GLuint handle, const char* desc)
|
||||
{
|
||||
GLint status = 0, log_length = 0;
|
||||
glGetProgramiv(handle, GL_LINK_STATUS, &status);
|
||||
glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
|
||||
if ((GLboolean)status == GL_FALSE)
|
||||
fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! (with GLSL '%s')\n", desc, g_GlslVersionString);
|
||||
if (log_length > 1)
|
||||
{
|
||||
ImVector<char> buf;
|
||||
buf.resize((int)(log_length + 1));
|
||||
glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin());
|
||||
fprintf(stderr, "%s\n", buf.begin());
|
||||
}
|
||||
return (GLboolean)status == GL_TRUE;
|
||||
}
|
||||
|
||||
bool ImGui_ImplOpenGL3_CreateDeviceObjects()
|
||||
{
|
||||
// Backup GL state
|
||||
GLint last_texture, last_array_buffer;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
|
||||
#ifndef IMGUI_IMPL_OPENGL_ES2
|
||||
GLint last_vertex_array;
|
||||
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
|
||||
#endif
|
||||
|
||||
// Parse GLSL version string
|
||||
int glsl_version = 130;
|
||||
sscanf(g_GlslVersionString, "#version %d", &glsl_version);
|
||||
|
||||
const GLchar* vertex_shader_glsl_120 =
|
||||
"uniform mat4 ProjMtx;\n"
|
||||
"attribute vec2 Position;\n"
|
||||
"attribute vec2 UV;\n"
|
||||
"attribute vec4 Color;\n"
|
||||
"varying vec2 Frag_UV;\n"
|
||||
"varying vec4 Frag_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Frag_UV = UV;\n"
|
||||
" Frag_Color = Color;\n"
|
||||
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
|
||||
"}\n";
|
||||
|
||||
const GLchar* vertex_shader_glsl_130 =
|
||||
"uniform mat4 ProjMtx;\n"
|
||||
"in vec2 Position;\n"
|
||||
"in vec2 UV;\n"
|
||||
"in vec4 Color;\n"
|
||||
"out vec2 Frag_UV;\n"
|
||||
"out vec4 Frag_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Frag_UV = UV;\n"
|
||||
" Frag_Color = Color;\n"
|
||||
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
|
||||
"}\n";
|
||||
|
||||
const GLchar* vertex_shader_glsl_300_es =
|
||||
"precision mediump float;\n"
|
||||
"layout (location = 0) in vec2 Position;\n"
|
||||
"layout (location = 1) in vec2 UV;\n"
|
||||
"layout (location = 2) in vec4 Color;\n"
|
||||
"uniform mat4 ProjMtx;\n"
|
||||
"out vec2 Frag_UV;\n"
|
||||
"out vec4 Frag_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Frag_UV = UV;\n"
|
||||
" Frag_Color = Color;\n"
|
||||
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
|
||||
"}\n";
|
||||
|
||||
const GLchar* vertex_shader_glsl_410_core =
|
||||
"layout (location = 0) in vec2 Position;\n"
|
||||
"layout (location = 1) in vec2 UV;\n"
|
||||
"layout (location = 2) in vec4 Color;\n"
|
||||
"uniform mat4 ProjMtx;\n"
|
||||
"out vec2 Frag_UV;\n"
|
||||
"out vec4 Frag_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Frag_UV = UV;\n"
|
||||
" Frag_Color = Color;\n"
|
||||
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
|
||||
"}\n";
|
||||
|
||||
const GLchar* fragment_shader_glsl_120 =
|
||||
"#ifdef GL_ES\n"
|
||||
" precision mediump float;\n"
|
||||
"#endif\n"
|
||||
"uniform sampler2D Texture;\n"
|
||||
"varying vec2 Frag_UV;\n"
|
||||
"varying vec4 Frag_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n"
|
||||
"}\n";
|
||||
|
||||
const GLchar* fragment_shader_glsl_130 =
|
||||
"uniform sampler2D Texture;\n"
|
||||
"in vec2 Frag_UV;\n"
|
||||
"in vec4 Frag_Color;\n"
|
||||
"out vec4 Out_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
|
||||
"}\n";
|
||||
|
||||
const GLchar* fragment_shader_glsl_300_es =
|
||||
"precision mediump float;\n"
|
||||
"uniform sampler2D Texture;\n"
|
||||
"in vec2 Frag_UV;\n"
|
||||
"in vec4 Frag_Color;\n"
|
||||
"layout (location = 0) out vec4 Out_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
|
||||
"}\n";
|
||||
|
||||
const GLchar* fragment_shader_glsl_410_core =
|
||||
"in vec2 Frag_UV;\n"
|
||||
"in vec4 Frag_Color;\n"
|
||||
"uniform sampler2D Texture;\n"
|
||||
"layout (location = 0) out vec4 Out_Color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
|
||||
"}\n";
|
||||
|
||||
// Select shaders matching our GLSL versions
|
||||
const GLchar* vertex_shader = NULL;
|
||||
const GLchar* fragment_shader = NULL;
|
||||
if (glsl_version < 130)
|
||||
{
|
||||
vertex_shader = vertex_shader_glsl_120;
|
||||
fragment_shader = fragment_shader_glsl_120;
|
||||
}
|
||||
else if (glsl_version >= 410)
|
||||
{
|
||||
vertex_shader = vertex_shader_glsl_410_core;
|
||||
fragment_shader = fragment_shader_glsl_410_core;
|
||||
}
|
||||
else if (glsl_version == 300)
|
||||
{
|
||||
vertex_shader = vertex_shader_glsl_300_es;
|
||||
fragment_shader = fragment_shader_glsl_300_es;
|
||||
}
|
||||
else
|
||||
{
|
||||
vertex_shader = vertex_shader_glsl_130;
|
||||
fragment_shader = fragment_shader_glsl_130;
|
||||
}
|
||||
|
||||
// Create shaders
|
||||
const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader };
|
||||
g_VertHandle = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL);
|
||||
glCompileShader(g_VertHandle);
|
||||
CheckShader(g_VertHandle, "vertex shader");
|
||||
|
||||
const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader };
|
||||
g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL);
|
||||
glCompileShader(g_FragHandle);
|
||||
CheckShader(g_FragHandle, "fragment shader");
|
||||
|
||||
g_ShaderHandle = glCreateProgram();
|
||||
glAttachShader(g_ShaderHandle, g_VertHandle);
|
||||
glAttachShader(g_ShaderHandle, g_FragHandle);
|
||||
glLinkProgram(g_ShaderHandle);
|
||||
CheckProgram(g_ShaderHandle, "shader program");
|
||||
|
||||
g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture");
|
||||
g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx");
|
||||
g_AttribLocationVtxPos = glGetAttribLocation(g_ShaderHandle, "Position");
|
||||
g_AttribLocationVtxUV = glGetAttribLocation(g_ShaderHandle, "UV");
|
||||
g_AttribLocationVtxColor = glGetAttribLocation(g_ShaderHandle, "Color");
|
||||
|
||||
// Create buffers
|
||||
glGenBuffers(1, &g_VboHandle);
|
||||
glGenBuffers(1, &g_ElementsHandle);
|
||||
|
||||
ImGui_ImplOpenGL3_CreateFontsTexture();
|
||||
|
||||
// Restore modified GL state
|
||||
glBindTexture(GL_TEXTURE_2D, last_texture);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
|
||||
#ifndef IMGUI_IMPL_OPENGL_ES2
|
||||
glBindVertexArray(last_vertex_array);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ImGui_ImplOpenGL3_DestroyDeviceObjects()
|
||||
{
|
||||
if (g_VboHandle) { glDeleteBuffers(1, &g_VboHandle); g_VboHandle = 0; }
|
||||
if (g_ElementsHandle) { glDeleteBuffers(1, &g_ElementsHandle); g_ElementsHandle = 0; }
|
||||
if (g_ShaderHandle && g_VertHandle) { glDetachShader(g_ShaderHandle, g_VertHandle); }
|
||||
if (g_ShaderHandle && g_FragHandle) { glDetachShader(g_ShaderHandle, g_FragHandle); }
|
||||
if (g_VertHandle) { glDeleteShader(g_VertHandle); g_VertHandle = 0; }
|
||||
if (g_FragHandle) { glDeleteShader(g_FragHandle); g_FragHandle = 0; }
|
||||
if (g_ShaderHandle) { glDeleteProgram(g_ShaderHandle); g_ShaderHandle = 0; }
|
||||
|
||||
ImGui_ImplOpenGL3_DestroyFontsTexture();
|
||||
}
|
||||
84
src/client/render/gui/imgui/imgui_impl_opengl3.h
Executable file
84
src/client/render/gui/imgui/imgui_impl_opengl3.h
Executable file
@@ -0,0 +1,84 @@
|
||||
// dear imgui: Renderer for modern OpenGL with shaders / programmatic pipeline
|
||||
// - Desktop GL: 2.x 3.x 4.x
|
||||
// - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0)
|
||||
// This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID!
|
||||
// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices.
|
||||
|
||||
// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
|
||||
// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
// About Desktop OpenGL function loaders:
|
||||
// Modern Desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers.
|
||||
// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad).
|
||||
// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
|
||||
|
||||
// About GLSL version:
|
||||
// The 'glsl_version' initialization parameter should be NULL (default) or a "#version XXX" string.
|
||||
// On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es"
|
||||
// Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp.
|
||||
|
||||
#pragma once
|
||||
#include "client/render/gui/imgui/imgui.h" // IMGUI_IMPL_API
|
||||
|
||||
// Backend API
|
||||
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = NULL);
|
||||
IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown();
|
||||
IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame();
|
||||
IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data);
|
||||
|
||||
// (Optional) Called by Init/NewFrame/Shutdown
|
||||
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture();
|
||||
IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture();
|
||||
IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects();
|
||||
IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects();
|
||||
|
||||
// Specific OpenGL ES versions
|
||||
//#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten
|
||||
//#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android
|
||||
|
||||
// Attempt to auto-detect the default Desktop GL loader based on available header files.
|
||||
// If auto-detection fails or doesn't select the same GL loader file as used by your application,
|
||||
// you are likely to get a crash in ImGui_ImplOpenGL3_Init().
|
||||
// You can explicitly select a loader by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line.
|
||||
#if !defined(IMGUI_IMPL_OPENGL_ES2) \
|
||||
&& !defined(IMGUI_IMPL_OPENGL_ES3) \
|
||||
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \
|
||||
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \
|
||||
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \
|
||||
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) \
|
||||
&& !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) \
|
||||
&& !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM)
|
||||
|
||||
// Try to detect GLES on matching platforms
|
||||
#if defined(__APPLE__)
|
||||
#include "TargetConditionals.h"
|
||||
#endif
|
||||
#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__))
|
||||
#define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es"
|
||||
#elif defined(__EMSCRIPTEN__)
|
||||
#define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100"
|
||||
|
||||
// Otherwise try to detect supported Desktop OpenGL loaders..
|
||||
#elif defined(__has_include)
|
||||
#if __has_include(<GL/glew.h>)
|
||||
#define IMGUI_IMPL_OPENGL_LOADER_GLEW
|
||||
#elif __has_include(<glad/glad.h>)
|
||||
#define IMGUI_IMPL_OPENGL_LOADER_GLAD
|
||||
#elif __has_include(<GL/gl3w.h>)
|
||||
#define IMGUI_IMPL_OPENGL_LOADER_GL3W
|
||||
#elif __has_include(<glbinding/glbinding.h>)
|
||||
#define IMGUI_IMPL_OPENGL_LOADER_GLBINDING3
|
||||
#elif __has_include(<glbinding/Binding.h>)
|
||||
#define IMGUI_IMPL_OPENGL_LOADER_GLBINDING2
|
||||
#else
|
||||
#error "Cannot detect OpenGL loader!"
|
||||
#endif
|
||||
#else
|
||||
#define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W embedded in our repository
|
||||
#endif
|
||||
|
||||
#endif
|
||||
466
src/client/render/gui/imgui/imgui_impl_sdl.cpp
Normal file
466
src/client/render/gui/imgui/imgui_impl_sdl.cpp
Normal file
@@ -0,0 +1,466 @@
|
||||
// dear imgui: Platform Backend for SDL2
|
||||
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
|
||||
// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
|
||||
// (Prefer SDL 2.0.5+ for full feature support.)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||
// [X] Platform: Clipboard support.
|
||||
// [X] Platform: Keyboard arrays indexed using SDL_SCANCODE_* codes, e.g. ImGui::IsKeyPressed(SDL_SCANCODE_SPACE).
|
||||
// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
|
||||
// Missing features:
|
||||
// [ ] Platform: SDL2 handling of IME under Windows appears to be broken and it explicitly disable the regular Windows IME. You can restore Windows IME by compiling SDL with SDL_DISABLE_WINDOWS_IME.
|
||||
|
||||
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
|
||||
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
||||
|
||||
// CHANGELOG
|
||||
// (minor and older changes stripped away, please see git history for details)
|
||||
// 2021-08-17: Calling io.AddFocusEvent() on SDL_WINDOWEVENT_FOCUS_GAINED/SDL_WINDOWEVENT_FOCUS_LOST.
|
||||
// 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using SDL_GetMouseFocus() + SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, requires SDL 2.0.5+)
|
||||
// 2021-06-29: *BREAKING CHANGE* Removed 'SDL_Window* window' parameter to ImGui_ImplSDL2_NewFrame() which was unnecessary.
|
||||
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
|
||||
// 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950)
|
||||
// 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends.
|
||||
// 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2).
|
||||
// 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state).
|
||||
// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
|
||||
// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
|
||||
// 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
|
||||
// 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
|
||||
// 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls.
|
||||
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
|
||||
// 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'.
|
||||
// 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
|
||||
// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
|
||||
// 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples.
|
||||
// 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter.
|
||||
// 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText).
|
||||
// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
|
||||
// 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
|
||||
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
|
||||
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
|
||||
// 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
|
||||
// 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
|
||||
// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
|
||||
// 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS.
|
||||
// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
|
||||
// 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
|
||||
// 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
|
||||
|
||||
#include "client/render/gui/imgui/imgui.h"
|
||||
#include "imgui_impl_sdl.h"
|
||||
|
||||
// SDL
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_syswm.h>
|
||||
#if defined(__APPLE__)
|
||||
#include <TargetConditionals.h>
|
||||
#endif
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS)
|
||||
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
|
||||
#else
|
||||
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
|
||||
#endif
|
||||
#define SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH SDL_VERSION_ATLEAST(2,0,5)
|
||||
#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
|
||||
|
||||
// SDL Data
|
||||
struct ImGui_ImplSDL2_Data
|
||||
{
|
||||
SDL_Window* Window;
|
||||
Uint64 Time;
|
||||
bool MousePressed[3];
|
||||
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
|
||||
char* ClipboardTextData;
|
||||
bool MouseCanUseGlobalState;
|
||||
|
||||
ImGui_ImplSDL2_Data() { memset(this, 0, sizeof(*this)); }
|
||||
};
|
||||
|
||||
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
|
||||
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
|
||||
// FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
|
||||
// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
|
||||
static ImGui_ImplSDL2_Data* ImGui_ImplSDL2_GetBackendData()
|
||||
{
|
||||
return ImGui::GetCurrentContext() ? (ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData : NULL;
|
||||
}
|
||||
|
||||
// Functions
|
||||
static const char* ImGui_ImplSDL2_GetClipboardText(void*)
|
||||
{
|
||||
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||
if (bd->ClipboardTextData)
|
||||
SDL_free(bd->ClipboardTextData);
|
||||
bd->ClipboardTextData = SDL_GetClipboardText();
|
||||
return bd->ClipboardTextData;
|
||||
}
|
||||
|
||||
static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
|
||||
{
|
||||
SDL_SetClipboardText(text);
|
||||
}
|
||||
|
||||
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||||
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
|
||||
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
|
||||
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
||||
// If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field.
|
||||
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||
|
||||
switch (event->type)
|
||||
{
|
||||
case SDL_MOUSEWHEEL:
|
||||
{
|
||||
if (event->wheel.x > 0) io.MouseWheelH += 1;
|
||||
if (event->wheel.x < 0) io.MouseWheelH -= 1;
|
||||
if (event->wheel.y > 0) io.MouseWheel += 1;
|
||||
if (event->wheel.y < 0) io.MouseWheel -= 1;
|
||||
return true;
|
||||
}
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
{
|
||||
if (event->button.button == SDL_BUTTON_LEFT) { bd->MousePressed[0] = true; }
|
||||
if (event->button.button == SDL_BUTTON_RIGHT) { bd->MousePressed[1] = true; }
|
||||
if (event->button.button == SDL_BUTTON_MIDDLE) { bd->MousePressed[2] = true; }
|
||||
return true;
|
||||
}
|
||||
case SDL_TEXTINPUT:
|
||||
{
|
||||
io.AddInputCharactersUTF8(event->text.text);
|
||||
return true;
|
||||
}
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
{
|
||||
int key = event->key.keysym.scancode;
|
||||
if (key == SDLK_BACKSPACE) {
|
||||
io.KeysDown[key] = true;
|
||||
} else {
|
||||
io.KeysDown[key] = (event->type == SDL_KEYDOWN);
|
||||
}
|
||||
IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
|
||||
io.KeysDown[key] = (event->type == SDL_KEYDOWN);
|
||||
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
|
||||
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
|
||||
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
|
||||
#ifdef _WIN32
|
||||
io.KeySuper = false;
|
||||
#else
|
||||
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
case SDL_WINDOWEVENT:
|
||||
{
|
||||
if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED)
|
||||
io.AddFocusEvent(true);
|
||||
else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
|
||||
io.AddFocusEvent(false);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// activating screen keyboard for Android devices
|
||||
#ifdef __ANDROID__
|
||||
static bool keyboardShown = false;
|
||||
if(ImGui::GetIO().WantTextInput != keyboardShown){
|
||||
if(ImGui::GetIO().WantTextInput){
|
||||
SDL_StartTextInput();
|
||||
}else{
|
||||
SDL_StopTextInput();
|
||||
}
|
||||
keyboardShown = ImGui::GetIO().WantTextInput;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool ImGui_ImplSDL2_Init(SDL_Window* window)
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
IM_ASSERT(io.BackendPlatformUserData == NULL && "Already initialized a platform backend!");
|
||||
|
||||
// Check and store if we are on a SDL backend that supports global mouse position
|
||||
// ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
|
||||
bool mouse_can_use_global_state = false;
|
||||
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
|
||||
const char* sdl_backend = SDL_GetCurrentVideoDriver();
|
||||
const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
|
||||
for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
|
||||
if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
|
||||
mouse_can_use_global_state = true;
|
||||
#endif
|
||||
|
||||
// Setup backend capabilities flags
|
||||
ImGui_ImplSDL2_Data* bd = IM_NEW(ImGui_ImplSDL2_Data)();
|
||||
io.BackendPlatformUserData = (void*)bd;
|
||||
io.BackendPlatformName = "imgui_impl_sdl";
|
||||
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
|
||||
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
||||
|
||||
bd->Window = window;
|
||||
bd->MouseCanUseGlobalState = mouse_can_use_global_state;
|
||||
|
||||
// Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array.
|
||||
io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
|
||||
io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
|
||||
io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
|
||||
io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
|
||||
io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
|
||||
io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
|
||||
io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
|
||||
io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
|
||||
io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
|
||||
io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
|
||||
io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
|
||||
io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
|
||||
io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
|
||||
io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
|
||||
io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
|
||||
io.KeyMap[ImGuiKey_KeyPadEnter] = SDL_SCANCODE_KP_ENTER;
|
||||
io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
|
||||
io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
|
||||
io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
|
||||
io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
|
||||
io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
|
||||
io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
|
||||
|
||||
io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
|
||||
io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
|
||||
io.ClipboardUserData = NULL;
|
||||
|
||||
// Load mouse cursors
|
||||
bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
||||
bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
|
||||
bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
|
||||
bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
|
||||
bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
|
||||
bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
|
||||
bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
|
||||
bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
|
||||
bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
|
||||
|
||||
#ifdef _WIN32
|
||||
SDL_SysWMinfo info;
|
||||
SDL_VERSION(&info.version);
|
||||
if (SDL_GetWindowWMInfo(window, &info))
|
||||
io.ImeWindowHandle = info.info.win.window;
|
||||
#else
|
||||
(void)window;
|
||||
#endif
|
||||
|
||||
// Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
|
||||
// Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
|
||||
// (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
|
||||
// It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
|
||||
// you can ignore SDL_MOUSEBUTTONDOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
|
||||
#if SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH
|
||||
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
|
||||
{
|
||||
IM_UNUSED(sdl_gl_context); // Viewport branch will need this.
|
||||
return ImGui_ImplSDL2_Init(window);
|
||||
}
|
||||
|
||||
bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
|
||||
{
|
||||
#if !SDL_HAS_VULKAN
|
||||
IM_ASSERT(0 && "Unsupported");
|
||||
#endif
|
||||
return ImGui_ImplSDL2_Init(window);
|
||||
}
|
||||
|
||||
bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
|
||||
{
|
||||
#if !defined(_WIN32)
|
||||
IM_ASSERT(0 && "Unsupported");
|
||||
#endif
|
||||
return ImGui_ImplSDL2_Init(window);
|
||||
}
|
||||
|
||||
bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
|
||||
{
|
||||
return ImGui_ImplSDL2_Init(window);
|
||||
}
|
||||
|
||||
bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window)
|
||||
{
|
||||
return ImGui_ImplSDL2_Init(window);
|
||||
}
|
||||
|
||||
void ImGui_ImplSDL2_Shutdown()
|
||||
{
|
||||
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||
IM_ASSERT(bd != NULL && "No platform backend to shutdown, or already shutdown?");
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
if (bd->ClipboardTextData)
|
||||
SDL_free(bd->ClipboardTextData);
|
||||
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
|
||||
SDL_FreeCursor(bd->MouseCursors[cursor_n]);
|
||||
|
||||
io.BackendPlatformName = NULL;
|
||||
io.BackendPlatformUserData = NULL;
|
||||
IM_DELETE(bd);
|
||||
}
|
||||
|
||||
static void ImGui_ImplSDL2_UpdateMousePosAndButtons()
|
||||
{
|
||||
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
ImVec2 mouse_pos_prev = io.MousePos;
|
||||
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
|
||||
|
||||
// Update mouse buttons
|
||||
int mouse_x_local, mouse_y_local;
|
||||
Uint32 mouse_buttons = SDL_GetMouseState(&mouse_x_local, &mouse_y_local);
|
||||
io.MouseDown[0] = bd->MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
|
||||
io.MouseDown[1] = bd->MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
|
||||
io.MouseDown[2] = bd->MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
|
||||
bd->MousePressed[0] = bd->MousePressed[1] = bd->MousePressed[2] = false;
|
||||
|
||||
// Obtain focused and hovered window. We forward mouse input when focused or when hovered (and no other window is capturing)
|
||||
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
|
||||
SDL_Window* focused_window = SDL_GetKeyboardFocus();
|
||||
SDL_Window* hovered_window = SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH ? SDL_GetMouseFocus() : NULL; // This is better but is only reliably useful with SDL 2.0.5+ and SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH.
|
||||
SDL_Window* mouse_window = NULL;
|
||||
if (hovered_window && bd->Window == hovered_window)
|
||||
mouse_window = hovered_window;
|
||||
else if (focused_window && bd->Window == focused_window)
|
||||
mouse_window = focused_window;
|
||||
|
||||
// SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't e.g. trigger other operations outside
|
||||
SDL_CaptureMouse(ImGui::IsAnyMouseDown() ? SDL_TRUE : SDL_FALSE);
|
||||
#else
|
||||
// SDL 2.0.3 and non-windowed systems: single-viewport only
|
||||
SDL_Window* mouse_window = (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_INPUT_FOCUS) ? bd->Window : NULL;
|
||||
#endif
|
||||
|
||||
if (mouse_window == NULL)
|
||||
return;
|
||||
|
||||
// Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
|
||||
if (io.WantSetMousePos)
|
||||
SDL_WarpMouseInWindow(bd->Window, (int)mouse_pos_prev.x, (int)mouse_pos_prev.y);
|
||||
|
||||
// Set Dear ImGui mouse position from OS position + get buttons. (this is the common behavior)
|
||||
if (bd->MouseCanUseGlobalState)
|
||||
{
|
||||
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
|
||||
// Unlike local position obtained earlier this will be valid when straying out of bounds.
|
||||
int mouse_x_global, mouse_y_global;
|
||||
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
|
||||
int window_x, window_y;
|
||||
SDL_GetWindowPosition(mouse_window, &window_x, &window_y);
|
||||
io.MousePos = ImVec2((float)(mouse_x_global - window_x), (float)(mouse_y_global - window_y));
|
||||
}
|
||||
else
|
||||
{
|
||||
io.MousePos = ImVec2((float)mouse_x_local, (float)mouse_y_local);
|
||||
}
|
||||
}
|
||||
|
||||
static void ImGui_ImplSDL2_UpdateMouseCursor()
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
|
||||
return;
|
||||
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||
|
||||
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
|
||||
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
|
||||
{
|
||||
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
|
||||
SDL_ShowCursor(SDL_FALSE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Show OS mouse cursor
|
||||
SDL_SetCursor(bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]);
|
||||
SDL_ShowCursor(SDL_TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
static void ImGui_ImplSDL2_UpdateGamepads()
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
memset(io.NavInputs, 0, sizeof(io.NavInputs));
|
||||
if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
|
||||
return;
|
||||
|
||||
// Get gamepad
|
||||
SDL_GameController* game_controller = SDL_GameControllerOpen(0);
|
||||
if (!game_controller)
|
||||
{
|
||||
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
||||
return;
|
||||
}
|
||||
|
||||
// Update gamepad inputs
|
||||
#define MAP_BUTTON(NAV_NO, BUTTON_NO) { io.NavInputs[NAV_NO] = (SDL_GameControllerGetButton(game_controller, BUTTON_NO) != 0) ? 1.0f : 0.0f; }
|
||||
#define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float vn = (float)(SDL_GameControllerGetAxis(game_controller, AXIS_NO) - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; }
|
||||
const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
|
||||
MAP_BUTTON(ImGuiNavInput_Activate, SDL_CONTROLLER_BUTTON_A); // Cross / A
|
||||
MAP_BUTTON(ImGuiNavInput_Cancel, SDL_CONTROLLER_BUTTON_B); // Circle / B
|
||||
MAP_BUTTON(ImGuiNavInput_Menu, SDL_CONTROLLER_BUTTON_X); // Square / X
|
||||
MAP_BUTTON(ImGuiNavInput_Input, SDL_CONTROLLER_BUTTON_Y); // Triangle / Y
|
||||
MAP_BUTTON(ImGuiNavInput_DpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT); // D-Pad Left
|
||||
MAP_BUTTON(ImGuiNavInput_DpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); // D-Pad Right
|
||||
MAP_BUTTON(ImGuiNavInput_DpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP); // D-Pad Up
|
||||
MAP_BUTTON(ImGuiNavInput_DpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN); // D-Pad Down
|
||||
MAP_BUTTON(ImGuiNavInput_FocusPrev, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB
|
||||
MAP_BUTTON(ImGuiNavInput_FocusNext, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB
|
||||
MAP_BUTTON(ImGuiNavInput_TweakSlow, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); // L1 / LB
|
||||
MAP_BUTTON(ImGuiNavInput_TweakFast, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); // R1 / RB
|
||||
MAP_ANALOG(ImGuiNavInput_LStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
|
||||
MAP_ANALOG(ImGuiNavInput_LStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
|
||||
MAP_ANALOG(ImGuiNavInput_LStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32767);
|
||||
MAP_ANALOG(ImGuiNavInput_LStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
|
||||
|
||||
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
||||
#undef MAP_BUTTON
|
||||
#undef MAP_ANALOG
|
||||
}
|
||||
|
||||
void ImGui_ImplSDL2_NewFrame()
|
||||
{
|
||||
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||||
IM_ASSERT(bd != NULL && "Did you call ImGui_ImplSDL2_Init()?");
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
// Setup display size (every frame to accommodate for window resizing)
|
||||
int w, h;
|
||||
int display_w, display_h;
|
||||
SDL_GetWindowSize(bd->Window, &w, &h);
|
||||
if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED)
|
||||
w = h = 0;
|
||||
SDL_GL_GetDrawableSize(bd->Window, &display_w, &display_h);
|
||||
io.DisplaySize = ImVec2((float)w, (float)h);
|
||||
if (w > 0 && h > 0)
|
||||
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
|
||||
|
||||
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
|
||||
static Uint64 frequency = SDL_GetPerformanceFrequency();
|
||||
Uint64 current_time = SDL_GetPerformanceCounter();
|
||||
io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
|
||||
bd->Time = current_time;
|
||||
|
||||
ImGui_ImplSDL2_UpdateMousePosAndButtons();
|
||||
ImGui_ImplSDL2_UpdateMouseCursor();
|
||||
|
||||
// Update game controllers (if enabled and available)
|
||||
ImGui_ImplSDL2_UpdateGamepads();
|
||||
}
|
||||
35
src/client/render/gui/imgui/imgui_impl_sdl.h
Normal file
35
src/client/render/gui/imgui/imgui_impl_sdl.h
Normal file
@@ -0,0 +1,35 @@
|
||||
// dear imgui: Platform Backend for SDL2
|
||||
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
|
||||
// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
|
||||
|
||||
// Implemented features:
|
||||
// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||||
// [X] Platform: Clipboard support.
|
||||
// [X] Platform: Keyboard arrays indexed using SDL_SCANCODE_* codes, e.g. ImGui::IsKeyPressed(SDL_SCANCODE_SPACE).
|
||||
// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
|
||||
// Missing features:
|
||||
// [ ] Platform: SDL2 handling of IME under Windows appears to be broken and it explicitly disable the regular Windows IME. You can restore Windows IME by compiling SDL with SDL_DISABLE_WINDOWS_IME.
|
||||
|
||||
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
|
||||
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
|
||||
// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
|
||||
// Read online: https://github.com/ocornut/imgui/tree/master/docs
|
||||
|
||||
#pragma once
|
||||
#include "client/render/gui/imgui/imgui.h" // IMGUI_IMPL_API
|
||||
|
||||
struct SDL_Window;
|
||||
typedef union SDL_Event SDL_Event;
|
||||
|
||||
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context);
|
||||
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window);
|
||||
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window);
|
||||
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window);
|
||||
IMGUI_IMPL_API bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window);
|
||||
IMGUI_IMPL_API void ImGui_ImplSDL2_Shutdown();
|
||||
IMGUI_IMPL_API void ImGui_ImplSDL2_NewFrame();
|
||||
IMGUI_IMPL_API bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event);
|
||||
|
||||
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
|
||||
static inline void ImGui_ImplSDL2_NewFrame(SDL_Window*) { ImGui_ImplSDL2_NewFrame(); } // 1.84: removed unnecessary parameter
|
||||
#endif
|
||||
2810
src/client/render/gui/imgui/imgui_internal.h
Normal file
2810
src/client/render/gui/imgui/imgui_internal.h
Normal file
File diff suppressed because it is too large
Load Diff
4051
src/client/render/gui/imgui/imgui_tables.cpp
Normal file
4051
src/client/render/gui/imgui/imgui_tables.cpp
Normal file
File diff suppressed because it is too large
Load Diff
8218
src/client/render/gui/imgui/imgui_widgets.cpp
Normal file
8218
src/client/render/gui/imgui/imgui_widgets.cpp
Normal file
File diff suppressed because it is too large
Load Diff
639
src/client/render/gui/imgui/imstb_rectpack.h
Normal file
639
src/client/render/gui/imgui/imstb_rectpack.h
Normal file
@@ -0,0 +1,639 @@
|
||||
// [DEAR IMGUI]
|
||||
// This is a slightly modified version of stb_rect_pack.h 1.00.
|
||||
// Those changes would need to be pushed into nothings/stb:
|
||||
// - Added STBRP__CDECL
|
||||
// Grep for [DEAR IMGUI] to find the changes.
|
||||
|
||||
// stb_rect_pack.h - v1.00 - public domain - rectangle packing
|
||||
// Sean Barrett 2014
|
||||
//
|
||||
// Useful for e.g. packing rectangular textures into an atlas.
|
||||
// Does not do rotation.
|
||||
//
|
||||
// Not necessarily the awesomest packing method, but better than
|
||||
// the totally naive one in stb_truetype (which is primarily what
|
||||
// this is meant to replace).
|
||||
//
|
||||
// Has only had a few tests run, may have issues.
|
||||
//
|
||||
// More docs to come.
|
||||
//
|
||||
// No memory allocations; uses qsort() and assert() from stdlib.
|
||||
// Can override those by defining STBRP_SORT and STBRP_ASSERT.
|
||||
//
|
||||
// This library currently uses the Skyline Bottom-Left algorithm.
|
||||
//
|
||||
// Please note: better rectangle packers are welcome! Please
|
||||
// implement them to the same API, but with a different init
|
||||
// function.
|
||||
//
|
||||
// Credits
|
||||
//
|
||||
// Library
|
||||
// Sean Barrett
|
||||
// Minor features
|
||||
// Martins Mozeiko
|
||||
// github:IntellectualKitty
|
||||
//
|
||||
// Bugfixes / warning fixes
|
||||
// Jeremy Jaussaud
|
||||
// Fabian Giesen
|
||||
//
|
||||
// Version history:
|
||||
//
|
||||
// 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles
|
||||
// 0.99 (2019-02-07) warning fixes
|
||||
// 0.11 (2017-03-03) return packing success/fail result
|
||||
// 0.10 (2016-10-25) remove cast-away-const to avoid warnings
|
||||
// 0.09 (2016-08-27) fix compiler warnings
|
||||
// 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
|
||||
// 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
|
||||
// 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
|
||||
// 0.05: added STBRP_ASSERT to allow replacing assert
|
||||
// 0.04: fixed minor bug in STBRP_LARGE_RECTS support
|
||||
// 0.01: initial release
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// See end of file for license information.
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// INCLUDE SECTION
|
||||
//
|
||||
|
||||
#ifndef STB_INCLUDE_STB_RECT_PACK_H
|
||||
#define STB_INCLUDE_STB_RECT_PACK_H
|
||||
|
||||
#define STB_RECT_PACK_VERSION 1
|
||||
|
||||
#ifdef STBRP_STATIC
|
||||
#define STBRP_DEF static
|
||||
#else
|
||||
#define STBRP_DEF extern
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct stbrp_context stbrp_context;
|
||||
typedef struct stbrp_node stbrp_node;
|
||||
typedef struct stbrp_rect stbrp_rect;
|
||||
|
||||
#ifdef STBRP_LARGE_RECTS
|
||||
typedef int stbrp_coord;
|
||||
#else
|
||||
typedef unsigned short stbrp_coord;
|
||||
#endif
|
||||
|
||||
STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
|
||||
// Assign packed locations to rectangles. The rectangles are of type
|
||||
// 'stbrp_rect' defined below, stored in the array 'rects', and there
|
||||
// are 'num_rects' many of them.
|
||||
//
|
||||
// Rectangles which are successfully packed have the 'was_packed' flag
|
||||
// set to a non-zero value and 'x' and 'y' store the minimum location
|
||||
// on each axis (i.e. bottom-left in cartesian coordinates, top-left
|
||||
// if you imagine y increasing downwards). Rectangles which do not fit
|
||||
// have the 'was_packed' flag set to 0.
|
||||
//
|
||||
// You should not try to access the 'rects' array from another thread
|
||||
// while this function is running, as the function temporarily reorders
|
||||
// the array while it executes.
|
||||
//
|
||||
// To pack into another rectangle, you need to call stbrp_init_target
|
||||
// again. To continue packing into the same rectangle, you can call
|
||||
// this function again. Calling this multiple times with multiple rect
|
||||
// arrays will probably produce worse packing results than calling it
|
||||
// a single time with the full rectangle array, but the option is
|
||||
// available.
|
||||
//
|
||||
// The function returns 1 if all of the rectangles were successfully
|
||||
// packed and 0 otherwise.
|
||||
|
||||
struct stbrp_rect
|
||||
{
|
||||
// reserved for your use:
|
||||
int id;
|
||||
|
||||
// input:
|
||||
stbrp_coord w, h;
|
||||
|
||||
// output:
|
||||
stbrp_coord x, y;
|
||||
int was_packed; // non-zero if valid packing
|
||||
|
||||
}; // 16 bytes, nominally
|
||||
|
||||
|
||||
STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
|
||||
// Initialize a rectangle packer to:
|
||||
// pack a rectangle that is 'width' by 'height' in dimensions
|
||||
// using temporary storage provided by the array 'nodes', which is 'num_nodes' long
|
||||
//
|
||||
// You must call this function every time you start packing into a new target.
|
||||
//
|
||||
// There is no "shutdown" function. The 'nodes' memory must stay valid for
|
||||
// the following stbrp_pack_rects() call (or calls), but can be freed after
|
||||
// the call (or calls) finish.
|
||||
//
|
||||
// Note: to guarantee best results, either:
|
||||
// 1. make sure 'num_nodes' >= 'width'
|
||||
// or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
|
||||
//
|
||||
// If you don't do either of the above things, widths will be quantized to multiples
|
||||
// of small integers to guarantee the algorithm doesn't run out of temporary storage.
|
||||
//
|
||||
// If you do #2, then the non-quantized algorithm will be used, but the algorithm
|
||||
// may run out of temporary storage and be unable to pack some rectangles.
|
||||
|
||||
STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
|
||||
// Optionally call this function after init but before doing any packing to
|
||||
// change the handling of the out-of-temp-memory scenario, described above.
|
||||
// If you call init again, this will be reset to the default (false).
|
||||
|
||||
|
||||
STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
|
||||
// Optionally select which packing heuristic the library should use. Different
|
||||
// heuristics will produce better/worse results for different data sets.
|
||||
// If you call init again, this will be reset to the default.
|
||||
|
||||
enum
|
||||
{
|
||||
STBRP_HEURISTIC_Skyline_default=0,
|
||||
STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
|
||||
STBRP_HEURISTIC_Skyline_BF_sortHeight
|
||||
};
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// the details of the following structures don't matter to you, but they must
|
||||
// be visible so you can handle the memory allocations for them
|
||||
|
||||
struct stbrp_node
|
||||
{
|
||||
stbrp_coord x,y;
|
||||
stbrp_node *next;
|
||||
};
|
||||
|
||||
struct stbrp_context
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
int align;
|
||||
int init_mode;
|
||||
int heuristic;
|
||||
int num_nodes;
|
||||
stbrp_node *active_head;
|
||||
stbrp_node *free_head;
|
||||
stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPLEMENTATION SECTION
|
||||
//
|
||||
|
||||
#ifdef STB_RECT_PACK_IMPLEMENTATION
|
||||
#ifndef STBRP_SORT
|
||||
#include <stdlib.h>
|
||||
#define STBRP_SORT qsort
|
||||
#endif
|
||||
|
||||
#ifndef STBRP_ASSERT
|
||||
#include <assert.h>
|
||||
#define STBRP_ASSERT assert
|
||||
#endif
|
||||
|
||||
// [DEAR IMGUI] Added STBRP__CDECL
|
||||
#ifdef _MSC_VER
|
||||
#define STBRP__NOTUSED(v) (void)(v)
|
||||
#define STBRP__CDECL __cdecl
|
||||
#else
|
||||
#define STBRP__NOTUSED(v) (void)sizeof(v)
|
||||
#define STBRP__CDECL
|
||||
#endif
|
||||
|
||||
enum
|
||||
{
|
||||
STBRP__INIT_skyline = 1
|
||||
};
|
||||
|
||||
STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
|
||||
{
|
||||
switch (context->init_mode) {
|
||||
case STBRP__INIT_skyline:
|
||||
STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
|
||||
context->heuristic = heuristic;
|
||||
break;
|
||||
default:
|
||||
STBRP_ASSERT(0);
|
||||
}
|
||||
}
|
||||
|
||||
STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
|
||||
{
|
||||
if (allow_out_of_mem)
|
||||
// if it's ok to run out of memory, then don't bother aligning them;
|
||||
// this gives better packing, but may fail due to OOM (even though
|
||||
// the rectangles easily fit). @TODO a smarter approach would be to only
|
||||
// quantize once we've hit OOM, then we could get rid of this parameter.
|
||||
context->align = 1;
|
||||
else {
|
||||
// if it's not ok to run out of memory, then quantize the widths
|
||||
// so that num_nodes is always enough nodes.
|
||||
//
|
||||
// I.e. num_nodes * align >= width
|
||||
// align >= width / num_nodes
|
||||
// align = ceil(width/num_nodes)
|
||||
|
||||
context->align = (context->width + context->num_nodes-1) / context->num_nodes;
|
||||
}
|
||||
}
|
||||
|
||||
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
|
||||
{
|
||||
int i;
|
||||
#ifndef STBRP_LARGE_RECTS
|
||||
STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
|
||||
#endif
|
||||
|
||||
for (i=0; i < num_nodes-1; ++i)
|
||||
nodes[i].next = &nodes[i+1];
|
||||
nodes[i].next = NULL;
|
||||
context->init_mode = STBRP__INIT_skyline;
|
||||
context->heuristic = STBRP_HEURISTIC_Skyline_default;
|
||||
context->free_head = &nodes[0];
|
||||
context->active_head = &context->extra[0];
|
||||
context->width = width;
|
||||
context->height = height;
|
||||
context->num_nodes = num_nodes;
|
||||
stbrp_setup_allow_out_of_mem(context, 0);
|
||||
|
||||
// node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
|
||||
context->extra[0].x = 0;
|
||||
context->extra[0].y = 0;
|
||||
context->extra[0].next = &context->extra[1];
|
||||
context->extra[1].x = (stbrp_coord) width;
|
||||
#ifdef STBRP_LARGE_RECTS
|
||||
context->extra[1].y = (1<<30);
|
||||
#else
|
||||
context->extra[1].y = 65535;
|
||||
#endif
|
||||
context->extra[1].next = NULL;
|
||||
}
|
||||
|
||||
// find minimum y position if it starts at x1
|
||||
static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
|
||||
{
|
||||
stbrp_node *node = first;
|
||||
int x1 = x0 + width;
|
||||
int min_y, visited_width, waste_area;
|
||||
|
||||
STBRP__NOTUSED(c);
|
||||
|
||||
STBRP_ASSERT(first->x <= x0);
|
||||
|
||||
#if 0
|
||||
// skip in case we're past the node
|
||||
while (node->next->x <= x0)
|
||||
++node;
|
||||
#else
|
||||
STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
|
||||
#endif
|
||||
|
||||
STBRP_ASSERT(node->x <= x0);
|
||||
|
||||
min_y = 0;
|
||||
waste_area = 0;
|
||||
visited_width = 0;
|
||||
while (node->x < x1) {
|
||||
if (node->y > min_y) {
|
||||
// raise min_y higher.
|
||||
// we've accounted for all waste up to min_y,
|
||||
// but we'll now add more waste for everything we've visted
|
||||
waste_area += visited_width * (node->y - min_y);
|
||||
min_y = node->y;
|
||||
// the first time through, visited_width might be reduced
|
||||
if (node->x < x0)
|
||||
visited_width += node->next->x - x0;
|
||||
else
|
||||
visited_width += node->next->x - node->x;
|
||||
} else {
|
||||
// add waste area
|
||||
int under_width = node->next->x - node->x;
|
||||
if (under_width + visited_width > width)
|
||||
under_width = width - visited_width;
|
||||
waste_area += under_width * (min_y - node->y);
|
||||
visited_width += under_width;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
*pwaste = waste_area;
|
||||
return min_y;
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int x,y;
|
||||
stbrp_node **prev_link;
|
||||
} stbrp__findresult;
|
||||
|
||||
static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
|
||||
{
|
||||
int best_waste = (1<<30), best_x, best_y = (1 << 30);
|
||||
stbrp__findresult fr;
|
||||
stbrp_node **prev, *node, *tail, **best = NULL;
|
||||
|
||||
// align to multiple of c->align
|
||||
width = (width + c->align - 1);
|
||||
width -= width % c->align;
|
||||
STBRP_ASSERT(width % c->align == 0);
|
||||
|
||||
// if it can't possibly fit, bail immediately
|
||||
if (width > c->width || height > c->height) {
|
||||
fr.prev_link = NULL;
|
||||
fr.x = fr.y = 0;
|
||||
return fr;
|
||||
}
|
||||
|
||||
node = c->active_head;
|
||||
prev = &c->active_head;
|
||||
while (node->x + width <= c->width) {
|
||||
int y,waste;
|
||||
y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
|
||||
if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
|
||||
// bottom left
|
||||
if (y < best_y) {
|
||||
best_y = y;
|
||||
best = prev;
|
||||
}
|
||||
} else {
|
||||
// best-fit
|
||||
if (y + height <= c->height) {
|
||||
// can only use it if it first vertically
|
||||
if (y < best_y || (y == best_y && waste < best_waste)) {
|
||||
best_y = y;
|
||||
best_waste = waste;
|
||||
best = prev;
|
||||
}
|
||||
}
|
||||
}
|
||||
prev = &node->next;
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
best_x = (best == NULL) ? 0 : (*best)->x;
|
||||
|
||||
// if doing best-fit (BF), we also have to try aligning right edge to each node position
|
||||
//
|
||||
// e.g, if fitting
|
||||
//
|
||||
// ____________________
|
||||
// |____________________|
|
||||
//
|
||||
// into
|
||||
//
|
||||
// | |
|
||||
// | ____________|
|
||||
// |____________|
|
||||
//
|
||||
// then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
|
||||
//
|
||||
// This makes BF take about 2x the time
|
||||
|
||||
if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
|
||||
tail = c->active_head;
|
||||
node = c->active_head;
|
||||
prev = &c->active_head;
|
||||
// find first node that's admissible
|
||||
while (tail->x < width)
|
||||
tail = tail->next;
|
||||
while (tail) {
|
||||
int xpos = tail->x - width;
|
||||
int y,waste;
|
||||
STBRP_ASSERT(xpos >= 0);
|
||||
// find the left position that matches this
|
||||
while (node->next->x <= xpos) {
|
||||
prev = &node->next;
|
||||
node = node->next;
|
||||
}
|
||||
STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
|
||||
y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
|
||||
if (y + height <= c->height) {
|
||||
if (y <= best_y) {
|
||||
if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
|
||||
best_x = xpos;
|
||||
STBRP_ASSERT(y <= best_y);
|
||||
best_y = y;
|
||||
best_waste = waste;
|
||||
best = prev;
|
||||
}
|
||||
}
|
||||
}
|
||||
tail = tail->next;
|
||||
}
|
||||
}
|
||||
|
||||
fr.prev_link = best;
|
||||
fr.x = best_x;
|
||||
fr.y = best_y;
|
||||
return fr;
|
||||
}
|
||||
|
||||
static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
|
||||
{
|
||||
// find best position according to heuristic
|
||||
stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
|
||||
stbrp_node *node, *cur;
|
||||
|
||||
// bail if:
|
||||
// 1. it failed
|
||||
// 2. the best node doesn't fit (we don't always check this)
|
||||
// 3. we're out of memory
|
||||
if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
|
||||
res.prev_link = NULL;
|
||||
return res;
|
||||
}
|
||||
|
||||
// on success, create new node
|
||||
node = context->free_head;
|
||||
node->x = (stbrp_coord) res.x;
|
||||
node->y = (stbrp_coord) (res.y + height);
|
||||
|
||||
context->free_head = node->next;
|
||||
|
||||
// insert the new node into the right starting point, and
|
||||
// let 'cur' point to the remaining nodes needing to be
|
||||
// stiched back in
|
||||
|
||||
cur = *res.prev_link;
|
||||
if (cur->x < res.x) {
|
||||
// preserve the existing one, so start testing with the next one
|
||||
stbrp_node *next = cur->next;
|
||||
cur->next = node;
|
||||
cur = next;
|
||||
} else {
|
||||
*res.prev_link = node;
|
||||
}
|
||||
|
||||
// from here, traverse cur and free the nodes, until we get to one
|
||||
// that shouldn't be freed
|
||||
while (cur->next && cur->next->x <= res.x + width) {
|
||||
stbrp_node *next = cur->next;
|
||||
// move the current node to the free list
|
||||
cur->next = context->free_head;
|
||||
context->free_head = cur;
|
||||
cur = next;
|
||||
}
|
||||
|
||||
// stitch the list back in
|
||||
node->next = cur;
|
||||
|
||||
if (cur->x < res.x + width)
|
||||
cur->x = (stbrp_coord) (res.x + width);
|
||||
|
||||
#ifdef _DEBUG
|
||||
cur = context->active_head;
|
||||
while (cur->x < context->width) {
|
||||
STBRP_ASSERT(cur->x < cur->next->x);
|
||||
cur = cur->next;
|
||||
}
|
||||
STBRP_ASSERT(cur->next == NULL);
|
||||
|
||||
{
|
||||
int count=0;
|
||||
cur = context->active_head;
|
||||
while (cur) {
|
||||
cur = cur->next;
|
||||
++count;
|
||||
}
|
||||
cur = context->free_head;
|
||||
while (cur) {
|
||||
cur = cur->next;
|
||||
++count;
|
||||
}
|
||||
STBRP_ASSERT(count == context->num_nodes+2);
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// [DEAR IMGUI] Added STBRP__CDECL
|
||||
static int STBRP__CDECL rect_height_compare(const void *a, const void *b)
|
||||
{
|
||||
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||
if (p->h > q->h)
|
||||
return -1;
|
||||
if (p->h < q->h)
|
||||
return 1;
|
||||
return (p->w > q->w) ? -1 : (p->w < q->w);
|
||||
}
|
||||
|
||||
// [DEAR IMGUI] Added STBRP__CDECL
|
||||
static int STBRP__CDECL rect_original_order(const void *a, const void *b)
|
||||
{
|
||||
const stbrp_rect *p = (const stbrp_rect *) a;
|
||||
const stbrp_rect *q = (const stbrp_rect *) b;
|
||||
return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
|
||||
}
|
||||
|
||||
#ifdef STBRP_LARGE_RECTS
|
||||
#define STBRP__MAXVAL 0xffffffff
|
||||
#else
|
||||
#define STBRP__MAXVAL 0xffff
|
||||
#endif
|
||||
|
||||
STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
|
||||
{
|
||||
int i, all_rects_packed = 1;
|
||||
|
||||
// we use the 'was_packed' field internally to allow sorting/unsorting
|
||||
for (i=0; i < num_rects; ++i) {
|
||||
rects[i].was_packed = i;
|
||||
}
|
||||
|
||||
// sort according to heuristic
|
||||
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
|
||||
|
||||
for (i=0; i < num_rects; ++i) {
|
||||
if (rects[i].w == 0 || rects[i].h == 0) {
|
||||
rects[i].x = rects[i].y = 0; // empty rect needs no space
|
||||
} else {
|
||||
stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
|
||||
if (fr.prev_link) {
|
||||
rects[i].x = (stbrp_coord) fr.x;
|
||||
rects[i].y = (stbrp_coord) fr.y;
|
||||
} else {
|
||||
rects[i].x = rects[i].y = STBRP__MAXVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// unsort
|
||||
STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
|
||||
|
||||
// set was_packed flags and all_rects_packed status
|
||||
for (i=0; i < num_rects; ++i) {
|
||||
rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
|
||||
if (!rects[i].was_packed)
|
||||
all_rects_packed = 0;
|
||||
}
|
||||
|
||||
// return the all_rects_packed status
|
||||
return all_rects_packed;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------------
|
||||
This software is available under 2 licenses -- choose whichever you prefer.
|
||||
------------------------------------------------------------------------------
|
||||
ALTERNATIVE A - MIT License
|
||||
Copyright (c) 2017 Sean Barrett
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
------------------------------------------------------------------------------
|
||||
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||
This is free and unencumbered software released into the public domain.
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||
software, either in source code form or as a compiled binary, for any purpose,
|
||||
commercial or non-commercial, and by any means.
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||
software dedicate any and all copyright interest in the software to the public
|
||||
domain. We make this dedication for the benefit of the public at large and to
|
||||
the detriment of our heirs and successors. We intend this dedication to be an
|
||||
overt act of relinquishment in perpetuity of all present and future rights to
|
||||
this software under copyright law.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
------------------------------------------------------------------------------
|
||||
*/
|
||||
1449
src/client/render/gui/imgui/imstb_textedit.h
Normal file
1449
src/client/render/gui/imgui/imstb_textedit.h
Normal file
File diff suppressed because it is too large
Load Diff
4903
src/client/render/gui/imgui/imstb_truetype.h
Normal file
4903
src/client/render/gui/imgui/imstb_truetype.h
Normal file
File diff suppressed because it is too large
Load Diff
72
src/client/render/loader/GLLoader.cpp
Normal file
72
src/client/render/loader/GLLoader.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* GLLoader.cpp
|
||||
*
|
||||
* Created on: 4 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
|
||||
#include "client/render/loader/GLLoader.h"
|
||||
#include "client/render/GL.h"
|
||||
|
||||
namespace GL {
|
||||
|
||||
VertexArray::~VertexArray() {
|
||||
if (m_ID != 0)
|
||||
glDeleteVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
VertexArray::VertexArray(unsigned int vertexCount) : m_VertexCount(vertexCount) {
|
||||
glGenVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
void VertexArray::Bind() const {
|
||||
glBindVertexArray(m_ID);
|
||||
}
|
||||
|
||||
void VertexArray::Unbind() const {
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void VertexArray::BindVertexBuffer(VertexBuffer& VertexBuffer) {
|
||||
VertexBuffer.Bind();
|
||||
VertexBuffer.BindVertexAttribs();
|
||||
m_VertexBuffers.push_back(std::move(VertexBuffer));
|
||||
}
|
||||
|
||||
VertexBuffer::~VertexBuffer() {
|
||||
if (m_ID != 0)
|
||||
glDeleteBuffers(1, &m_ID);
|
||||
}
|
||||
|
||||
VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) {
|
||||
glGenBuffers(1, &m_ID);
|
||||
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();
|
||||
}
|
||||
|
||||
void VertexBuffer::Bind() const {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
||||
}
|
||||
|
||||
void VertexBuffer::Unbind() const {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset) {
|
||||
VertexAttribPointer pointer;
|
||||
pointer.m_Index = index;
|
||||
pointer.m_Size = coordinateSize;
|
||||
pointer.m_Offset = offset;
|
||||
m_VertexAttribs.push_back(pointer);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
46
src/client/render/loader/TextureLoader.cpp
Normal file
46
src/client/render/loader/TextureLoader.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* TextureLoader.cpp
|
||||
*
|
||||
* Created on: 15 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include "client/render/loader/TextureLoader.h"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "client/render/loader/stb_image.h"
|
||||
#include <iostream>
|
||||
#include "client/render/GL.h"
|
||||
|
||||
namespace TextureLoader {
|
||||
|
||||
unsigned int LoadGLTexture(const char* fileName) {
|
||||
|
||||
int width, height, comp;
|
||||
|
||||
const unsigned char* image = stbi_load(fileName, &width, &height, &comp, STBI_rgb_alpha);
|
||||
|
||||
if (image == nullptr) {
|
||||
std::cerr << "Erreur lors du chargement de la texture !" << std::endl;
|
||||
throw(std::runtime_error("Failed to load texture"));
|
||||
}
|
||||
|
||||
GLuint textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
if (comp == 3)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
else if (comp == 4)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
stbi_image_free((void*)image);
|
||||
return textureID;
|
||||
}
|
||||
|
||||
}
|
||||
250
src/client/render/loader/WorldLoader.cpp
Normal file
250
src/client/render/loader/WorldLoader.cpp
Normal file
@@ -0,0 +1,250 @@
|
||||
#include "client/render/loader/WorldLoader.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
#include "td/game/BaseGame.h"
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
const static int VERTEX_SIZE = 3;
|
||||
|
||||
GL::VertexArray LoadMobModel() {
|
||||
std::vector<float> positions = {
|
||||
-0.5, 0, -0.5,
|
||||
0.5, 0, -0.5,
|
||||
-0.5, 0, 0.5,
|
||||
|
||||
0.5, 0, -0.5,
|
||||
-0.5, 0, 0.5,
|
||||
0.5, 0, 0.5
|
||||
};
|
||||
|
||||
float yellowFloat;
|
||||
int yellow = 255 << 24 | 255 << 16 | 255;
|
||||
memcpy(&yellowFloat, &yellow, sizeof(int));
|
||||
|
||||
std::vector<float> colors = {
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
|
||||
yellowFloat,
|
||||
yellowFloat,
|
||||
yellowFloat
|
||||
};
|
||||
|
||||
GL::VertexBuffer positionVBO(positions, VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 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 LoadWorldModel(const td::game::World* world) {
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
if (tile == nullptr)
|
||||
continue;
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
static_cast<float>(chunkX + tileX), 0, static_cast<float>(chunkY + tileY),
|
||||
static_cast<float>(chunkX + tileX + 1), 0, static_cast<float>(chunkY + tileY),
|
||||
static_cast<float>(chunkX + tileX), 0, static_cast<float>(chunkY + tileY + 1),
|
||||
|
||||
static_cast<float>(chunkX + tileX + 1), 0, static_cast<float>(chunkY + tileY),
|
||||
static_cast<float>(chunkX + tileX), 0, static_cast<float>(chunkY + tileY + 1),
|
||||
static_cast<float>(chunkX + tileX + 1), 0, static_cast<float>(chunkY + tileY + 1)
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
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();
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
fromX, 0, fromY,
|
||||
fromX, 0, toY,
|
||||
toX, 0, fromY,
|
||||
|
||||
toX, 0, toY,
|
||||
fromX, 0, toY,
|
||||
toX, 0, 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;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
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();
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
fromX, 0, fromY,
|
||||
fromX, 0, toY,
|
||||
toX, 0, fromY,
|
||||
|
||||
toX, 0, toY,
|
||||
fromX, 0, toY,
|
||||
toX, 0, 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;
|
||||
|
||||
int newColorIndex = colors.size();
|
||||
colors.push_back(0);
|
||||
|
||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
||||
}
|
||||
}
|
||||
|
||||
GL::VertexBuffer positionVBO(positions, VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 0);
|
||||
GL::VertexBuffer colorVBO(colors, 1);
|
||||
colorVBO.AddVertexAttribPointer(1, 1, 0);
|
||||
|
||||
GL::VertexArray worldVao(positions.size() / VERTEX_SIZE); // each pos = 3 vertecies
|
||||
worldVao.Bind();
|
||||
worldVao.BindVertexBuffer(positionVBO);
|
||||
worldVao.BindVertexBuffer(colorVBO);
|
||||
worldVao.Unbind();
|
||||
return worldVao;
|
||||
}
|
||||
|
||||
GL::VertexArray LoadTileSelectModel() {
|
||||
std::vector<float> positions = {
|
||||
0, 0, 0,
|
||||
1, 0, 0,
|
||||
0, 0, 1,
|
||||
|
||||
1, 0, 0,
|
||||
0, 0, 1,
|
||||
1, 0, 1
|
||||
};
|
||||
|
||||
int color = 255 << 24 | 255 << 16 | 255 << 8 | 150;
|
||||
float colorFloat;
|
||||
|
||||
memcpy(reinterpret_cast<std::uint8_t*>(&colorFloat), &color, sizeof(float));
|
||||
|
||||
std::vector<float> colors(6, colorFloat);
|
||||
|
||||
GL::VertexBuffer positionVBO(positions, VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 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();
|
||||
|
||||
return tileSelectVao;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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, 0, towerY,
|
||||
towerDX, 0, towerY,
|
||||
towerX, 0, towerDY,
|
||||
|
||||
towerDX, 0, towerY,
|
||||
towerX, 0, towerDY,
|
||||
towerDX, 0, towerDY
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
return renderData;
|
||||
}
|
||||
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
120
src/client/render/shaders/EntityShader.cpp
Normal file
120
src/client/render/shaders/EntityShader.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "client/render/shaders/EntityShader.h"
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
#ifdef __ANDROID__
|
||||
static const char vertexSource[] =
|
||||
R"(#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform vec3 modelPosition;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
pass_color = color;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
static const char fragmentSource[] =
|
||||
R"(#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
flat in int pass_color;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
uniform vec3 ColorEffect;
|
||||
|
||||
void main(void){
|
||||
|
||||
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||
float a = float(pass_color & 0xFF) / 255.0;
|
||||
vec3 intermediate_color = vec3(r, g, b) * ColorEffect;
|
||||
out_color = vec4(intermediate_color, a);
|
||||
|
||||
}
|
||||
)";
|
||||
#else
|
||||
static const char vertexSource[] = R"(
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform vec3 modelPosition;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
pass_color = color;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
static const char fragmentSource[] = R"(
|
||||
#version 330
|
||||
|
||||
flat in int pass_color;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
uniform vec3 ColorEffect;
|
||||
|
||||
void main(void){
|
||||
|
||||
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||
float a = float(pass_color & 0xFF) / 255.0;
|
||||
vec3 intermediate_color = vec3(r, g, b) * ColorEffect;
|
||||
out_color = vec4(intermediate_color, a);
|
||||
|
||||
}
|
||||
)";
|
||||
#endif
|
||||
|
||||
EntityShader::EntityShader() : ShaderProgram() {}
|
||||
|
||||
void EntityShader::LoadShader() {
|
||||
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
||||
}
|
||||
|
||||
void EntityShader::GetAllUniformLocation() {
|
||||
m_LocationColorEffect = static_cast<unsigned int>(GetUniformLocation("ColorEffect"));
|
||||
m_LocationViewMatrix = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
||||
m_LocationPosition = static_cast<unsigned int>(GetUniformLocation("modelPosition"));
|
||||
m_LocationProjectionMatrix = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
||||
}
|
||||
|
||||
void EntityShader::SetColorEffect(const Vec3f& color) {
|
||||
LoadVector(m_LocationColorEffect, color);
|
||||
}
|
||||
|
||||
void EntityShader::SetProjectionMatrix(const Mat4f& proj) const {
|
||||
LoadMat4(m_LocationProjectionMatrix, proj);
|
||||
}
|
||||
|
||||
void EntityShader::SetViewMatrix(const Mat4f& view) const {
|
||||
LoadMat4(m_LocationViewMatrix, view);
|
||||
}
|
||||
|
||||
void EntityShader::SetModelPos(const Vec3f& pos) const {
|
||||
LoadVector(m_LocationPosition, pos);
|
||||
}
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
149
src/client/render/shaders/ShaderProgram.cpp
Executable file
149
src/client/render/shaders/ShaderProgram.cpp
Executable file
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* ShaderProgram.cpp
|
||||
*
|
||||
* Created on: 31 janv. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include "client/render/shaders/ShaderProgram.h"
|
||||
#include "td/misc/Log.h"
|
||||
#include "td/misc/Format.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <android/log.h>
|
||||
#endif
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
ShaderProgram::ShaderProgram() :
|
||||
m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram() {
|
||||
CleanUp();
|
||||
}
|
||||
|
||||
void ShaderProgram::Start() const {
|
||||
glUseProgram(m_ProgramID);
|
||||
}
|
||||
|
||||
void ShaderProgram::Stop() const {
|
||||
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;
|
||||
}
|
||||
|
||||
void ShaderProgram::LoadFloat(unsigned int location, float value) const {
|
||||
glUniform1f(static_cast<GLint>(location), value);
|
||||
}
|
||||
|
||||
void ShaderProgram::LoadInt(unsigned int location, int value) const {
|
||||
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);
|
||||
}
|
||||
|
||||
void ShaderProgram::LoadVector(unsigned int location,
|
||||
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);
|
||||
}
|
||||
|
||||
void ShaderProgram::LoadMat4(unsigned int location, const Mat4f& mat) const {
|
||||
glUniformMatrix4fv(static_cast<GLint>(location), 1, false, reinterpret_cast<const float*>(&mat));
|
||||
}
|
||||
|
||||
void ShaderProgram::CleanUp() const {
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
unsigned int ShaderProgram::LoadShader(const std::string& source, GLenum 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());
|
||||
|
||||
utils::LOGE("Could not compile shader !");
|
||||
|
||||
utils::LOGE(shaderError.data());
|
||||
|
||||
utils::LOGD(utils::format("\nShader source : \n"
|
||||
"------------------------------------------------------------------------------------------------------------------------------------\n"
|
||||
"%s\n"
|
||||
"------------------------------------------------------------------------------------------------------------------------------------\n"
|
||||
, source.c_str()));
|
||||
}
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
unsigned int ShaderProgram::LoadShaderFromFile(const std::string& file, GLenum type) {
|
||||
std::stringstream stream;
|
||||
std::ifstream fileStream(file);
|
||||
|
||||
if (fileStream) {
|
||||
stream << fileStream.rdbuf();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return LoadShader(stream.str(), type);
|
||||
}
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
104
src/client/render/shaders/WorldShader.cpp
Normal file
104
src/client/render/shaders/WorldShader.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "client/render/shaders/WorldShader.h"
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
// TODO: GLES Shaders
|
||||
|
||||
#ifdef __ANDROID__
|
||||
static const char vertexSource[] =
|
||||
R"(#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
pass_color = color;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
static const char fragmentSource[] =
|
||||
R"(#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
flat in int pass_color;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
void main(void){
|
||||
|
||||
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||
float a = float(pass_color & 0xFF) / 255.0;
|
||||
out_color = vec4(r, g, b, a);
|
||||
|
||||
}
|
||||
)";
|
||||
#else
|
||||
static const char vertexSource[] = R"(
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in int color;
|
||||
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
|
||||
flat out int pass_color;
|
||||
|
||||
void main(void){
|
||||
pass_color = color;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
static const char fragmentSource[] = R"(
|
||||
#version 330
|
||||
|
||||
flat in int pass_color;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
void main(void){
|
||||
|
||||
float r = float(pass_color >> 24 & 0xFF) / 255.0;
|
||||
float g = float(pass_color >> 16 & 0xFF) / 255.0;
|
||||
float b = float(pass_color >> 8 & 0xFF) / 255.0;
|
||||
float a = float(pass_color & 0xFF) / 255.0;
|
||||
out_color = vec4(r, g, b, a);
|
||||
|
||||
}
|
||||
)";
|
||||
#endif
|
||||
|
||||
WorldShader::WorldShader() : ShaderProgram() {}
|
||||
|
||||
void WorldShader::LoadShader() {
|
||||
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
||||
}
|
||||
|
||||
void WorldShader::GetAllUniformLocation() {
|
||||
m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
||||
m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
||||
}
|
||||
|
||||
void WorldShader::SetProjectionMatrix(const Mat4f& proj) const {
|
||||
LoadMat4(m_LocationProjection, proj);
|
||||
}
|
||||
|
||||
void WorldShader::SetViewMatrix(const Mat4f& view) const {
|
||||
LoadMat4(m_LocationView, view);
|
||||
}
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
134
src/client/updater/Updater.cpp
Normal file
134
src/client/updater/Updater.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "client/updater/Updater.h"
|
||||
|
||||
#include "td/misc/Platform.h"
|
||||
#include "td/misc/DataBuffer.h"
|
||||
#include "td/misc/Log.h"
|
||||
#include "td/misc/Format.h"
|
||||
|
||||
#include "td/network/HttpLib.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
namespace td {
|
||||
namespace utils {
|
||||
|
||||
bool Updater::CheckUpdate() {
|
||||
httplib::Client client("thesims.freeboxos.fr", 30000);
|
||||
|
||||
if (auto res = client.Get("/Tower%20Defense/version")) {
|
||||
|
||||
std::string currentVersion = GetCurrentVersion();
|
||||
m_LastVersion = res.value().body;
|
||||
|
||||
// we only look at the first line
|
||||
m_LastVersion = m_LastVersion.substr(0, m_LastVersion.find('\n'));
|
||||
|
||||
utils::LOG(utils::format("Current version : [%s]", GetCurrentVersion().c_str()));
|
||||
utils::LOG(utils::format("Last version : [%s]", m_LastVersion.c_str()));
|
||||
|
||||
if (currentVersion != m_LastVersion) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
utils::LOGE(utils::format("Error code : %u", res.error()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Updater::CanUpdate() {
|
||||
return !GetDownloadFileURL().empty();
|
||||
}
|
||||
|
||||
std::string Updater::GetDownloadFileURL() {
|
||||
Os systemOs = GetSystemOs();
|
||||
Architecture systemArch = GetSystemArchitecture();
|
||||
|
||||
switch (systemOs) {
|
||||
|
||||
case Os::Windows: {
|
||||
|
||||
switch (systemArch) {
|
||||
case Architecture::x86_64:
|
||||
return "/Tower%20Defense/Tower%20Defense%20Windows%20(x86_64).exe";
|
||||
case Architecture::x86:
|
||||
return "/Tower%20Defense/Tower%20Defense%20Windows%20(x86).exe";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
default: // not supported on Android and Linux (package managers are better)
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
std::string Updater::GetLocalFilePath() {
|
||||
#ifdef _WIN32
|
||||
char filePathBuffer[1024];
|
||||
GetModuleFileName(nullptr, filePathBuffer, sizeof(filePathBuffer));
|
||||
|
||||
std::string filePath = filePathBuffer;
|
||||
return filePath;
|
||||
#endif
|
||||
return "";
|
||||
}
|
||||
|
||||
void Updater::DownloadUpdate() {
|
||||
if (m_DownloadComplete) return;
|
||||
|
||||
m_CancelDownload = false;
|
||||
|
||||
std::string newFileUrl = GetDownloadFileURL();
|
||||
|
||||
httplib::Client client("thesims.freeboxos.fr", 30000);
|
||||
|
||||
httplib::ContentReceiver reciever = [&](const char* data, size_t data_length) {
|
||||
std::string fileData(data, data_length);
|
||||
m_FileBuffer << fileData;
|
||||
return true;
|
||||
};
|
||||
|
||||
httplib::Progress progress = [&](uint64_t len, uint64_t total) {
|
||||
m_Progress = (float)len * 100.0f / total;
|
||||
return !m_CancelDownload;
|
||||
};
|
||||
|
||||
auto res = client.Get(newFileUrl.c_str(), reciever, progress);
|
||||
|
||||
if (!m_CancelDownload) {
|
||||
m_DownloadComplete = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Updater::RemoveOldFile() {
|
||||
std::remove(std::string(GetLocalFilePath() + "_old").c_str());
|
||||
}
|
||||
|
||||
bool Updater::WriteFile() {
|
||||
if (m_FileWrited || !m_DownloadComplete) return false;
|
||||
|
||||
if (!m_FileBuffer.WriteFile(GetLocalFilePath() + "_recent"))
|
||||
return false;
|
||||
|
||||
#ifdef _WIN32
|
||||
std::rename(GetLocalFilePath().c_str(), std::string(GetLocalFilePath() + "_old").c_str());
|
||||
std::rename(std::string(GetLocalFilePath() + "_recent").c_str(), GetLocalFilePath().c_str());
|
||||
#endif
|
||||
|
||||
m_FileWrited = true;
|
||||
m_DownloadComplete = false;
|
||||
|
||||
ClearCache();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
} // namespace td
|
||||
172
src/client/window/Display.cpp
Normal file
172
src/client/window/Display.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Display.cpp
|
||||
*
|
||||
* Created on: 4 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
#include "client/window/Display.h"
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include "client/render/gui/TowerGui.h"
|
||||
#include <iostream>
|
||||
#include "td/game/GameManager.h"
|
||||
#include "client/render/Renderer.h"
|
||||
#include "client/render/WorldRenderer.h"
|
||||
#include "../render/gui/imgui/imgui_impl_sdl.h"
|
||||
#include "td/misc/Log.h"
|
||||
#include "td/misc/Format.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
|
||||
namespace Display {
|
||||
|
||||
static SDL_Window* window;
|
||||
static SDL_GLContext glContext;
|
||||
|
||||
static constexpr int WindowWidth = 800;
|
||||
static constexpr int WindowHeight = 600;
|
||||
static constexpr const char WindowName[] = "Tower Defense";
|
||||
|
||||
std::unique_ptr<td::render::Renderer> renderer = std::make_unique<td::render::Renderer>();
|
||||
std::unique_ptr<td::render::TowerGui> towerGui;
|
||||
|
||||
static int lastWidth = 0, lastHeight = 0;
|
||||
static float aspectRatio;
|
||||
|
||||
static bool shouldClose = false;
|
||||
|
||||
void WindowResizeEvent(int width, int height) {
|
||||
aspectRatio = (float)width / height;
|
||||
renderer->Resize(width, height);
|
||||
lastWidth = width;
|
||||
lastHeight = height;
|
||||
}
|
||||
|
||||
bool Create() {
|
||||
window = SDL_CreateWindow(WindowName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WindowWidth, WindowHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||
// Prepare and create context
|
||||
#ifdef __ANDROID__
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
#else
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
#endif
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
|
||||
if (!glContext) {
|
||||
std::cerr << "Could not create context! SDL error: " << SDL_GetError() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
int major, minor, mask;
|
||||
int r, g, b, a, depth;
|
||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &mask);
|
||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
|
||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
|
||||
|
||||
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);
|
||||
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);
|
||||
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);
|
||||
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &a);
|
||||
|
||||
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth);
|
||||
|
||||
const char* mask_desc;
|
||||
|
||||
if (mask & SDL_GL_CONTEXT_PROFILE_CORE) {
|
||||
mask_desc = "core";
|
||||
} else if (mask & SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) {
|
||||
mask_desc = "compatibility";
|
||||
} else if (mask & SDL_GL_CONTEXT_PROFILE_ES) {
|
||||
mask_desc = "es";
|
||||
} else {
|
||||
mask_desc = "?";
|
||||
}
|
||||
|
||||
td::utils::LOG(td::utils::format("GL Context : %i.%i %s, Color : R:%i G:%i B:%i A:%i, Depth bits : %i", major, minor, mask_desc, r, g, b, a, depth));
|
||||
|
||||
SDL_GL_MakeCurrent(window, glContext);
|
||||
|
||||
if (!renderer->Init()) {
|
||||
exit(1);
|
||||
}
|
||||
towerGui = std::make_unique<td::render::TowerGui>(window, glContext, renderer.get());
|
||||
WindowResizeEvent(WindowWidth, WindowHeight);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Render() {
|
||||
renderer->Prepare();
|
||||
towerGui->Render();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
void Destroy() {
|
||||
renderer.reset(0);
|
||||
towerGui.reset(0);
|
||||
SDL_GL_DeleteContext(glContext);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
window = NULL;
|
||||
}
|
||||
|
||||
void PollEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_WINDOWEVENT) {
|
||||
switch (event.window.event) {
|
||||
case SDL_WINDOWEVENT_CLOSE: {
|
||||
shouldClose = true;
|
||||
}
|
||||
|
||||
case SDL_WINDOWEVENT_RESIZED: {
|
||||
int windowWidth, windowHeight;
|
||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||
WindowResizeEvent(windowWidth, windowHeight);
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsCloseRequested() {
|
||||
return shouldClose;
|
||||
}
|
||||
|
||||
bool IsMouseDown(int button) {
|
||||
return ImGui::GetIO().MouseDown[button];
|
||||
}
|
||||
|
||||
float GetAspectRatio() {
|
||||
return aspectRatio;
|
||||
}
|
||||
|
||||
int GetWindowWidth() {
|
||||
return lastWidth;
|
||||
}
|
||||
int GetWindowHeight() {
|
||||
return lastHeight;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user