refactor: format code
This commit is contained in:
@@ -14,18 +14,18 @@
|
||||
|
||||
using namespace gl;
|
||||
|
||||
namespace td{
|
||||
namespace render{
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
Renderer::Renderer(){
|
||||
Renderer::Renderer() {
|
||||
|
||||
}
|
||||
|
||||
Renderer::~Renderer(){
|
||||
Renderer::~Renderer() {
|
||||
|
||||
}
|
||||
|
||||
void Renderer::updateIsometricView(){
|
||||
void Renderer::updateIsometricView() {
|
||||
float isometricEased = utils::easeInOutExpo(m_IsometricShade);
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setIsometricView(isometricEased);
|
||||
@@ -33,7 +33,7 @@ void Renderer::updateIsometricView(){
|
||||
m_EntityShader->setIsometricView(isometricEased);
|
||||
}
|
||||
|
||||
void Renderer::initShader(){
|
||||
void Renderer::initShader() {
|
||||
m_WorldShader = std::make_unique<WorldShader>();
|
||||
m_WorldShader->loadShader();
|
||||
m_EntityShader = std::make_unique<EntityShader>();
|
||||
@@ -42,23 +42,23 @@ void Renderer::initShader(){
|
||||
updateIsometricView();
|
||||
}
|
||||
|
||||
bool Renderer::init(){
|
||||
bool Renderer::init() {
|
||||
glbinding::Binding::initialize();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
initShader();
|
||||
return true;
|
||||
}
|
||||
|
||||
void Renderer::renderVAO(const GL::VertexArray& vao){
|
||||
void Renderer::renderVAO(const GL::VertexArray& vao) {
|
||||
m_WorldShader->start();
|
||||
vao.bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount());
|
||||
vao.unbind();
|
||||
}
|
||||
|
||||
void Renderer::renderModel(const Model& model){
|
||||
void Renderer::renderModel(const Model& model) {
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setModelPos(model.positon);
|
||||
model.vao->bind();
|
||||
@@ -66,13 +66,13 @@ void Renderer::renderModel(const Model& model){
|
||||
model.vao->unbind();
|
||||
}
|
||||
|
||||
void Renderer::updateIsometricFade(){
|
||||
void Renderer::updateIsometricFade() {
|
||||
static std::uint64_t lastTime = utils::getTime();
|
||||
if(m_IsometricShade != (float) m_IsometricView){
|
||||
float step = (float) (utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed;
|
||||
if(m_IsometricShade < m_IsometricView){
|
||||
if (m_IsometricShade != (float)m_IsometricView) {
|
||||
float step = (float)(utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed;
|
||||
if (m_IsometricShade < m_IsometricView) {
|
||||
m_IsometricShade += step;
|
||||
}else{
|
||||
} else {
|
||||
m_IsometricShade -= step;
|
||||
}
|
||||
m_IsometricShade = std::min(m_IsometricShade, 1.0f);
|
||||
@@ -82,13 +82,13 @@ void Renderer::updateIsometricFade(){
|
||||
lastTime = utils::getTime();
|
||||
}
|
||||
|
||||
void Renderer::prepare(){
|
||||
void Renderer::prepare() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClearColor(0, 0, 0, 0);
|
||||
updateIsometricFade();
|
||||
}
|
||||
|
||||
void Renderer::resize(int width, int height){
|
||||
void Renderer::resize(int width, int height) {
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setAspectRatio((float)width / height);
|
||||
m_EntityShader->start();
|
||||
@@ -96,20 +96,20 @@ void Renderer::resize(int width, int height){
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void Renderer::setZoom(float zoom){
|
||||
void Renderer::setZoom(float zoom) {
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setZoom(zoom);
|
||||
m_EntityShader->start();
|
||||
m_EntityShader->setZoom(zoom);
|
||||
}
|
||||
|
||||
void Renderer::setCamMovement(const glm::vec2& mov){
|
||||
void Renderer::setCamMovement(const glm::vec2& mov) {
|
||||
m_CamPos.x += mov.x * (1 - m_IsometricView) + (0.5 * mov.x - mov.y) * m_IsometricView;
|
||||
m_CamPos.y += -mov.y * (1 - m_IsometricView) + (-0.5 * mov.x - mov.y) * m_IsometricView;
|
||||
setCamPos(m_CamPos);
|
||||
}
|
||||
|
||||
void Renderer::setCamPos(const glm::vec2& newPos){
|
||||
void Renderer::setCamPos(const glm::vec2& newPos) {
|
||||
m_CamPos = newPos;
|
||||
m_WorldShader->start();
|
||||
m_WorldShader->setCamPos(newPos);
|
||||
@@ -117,11 +117,11 @@ void Renderer::setCamPos(const glm::vec2& newPos){
|
||||
m_EntityShader->setCamPos(newPos);
|
||||
}
|
||||
|
||||
void Renderer::setIsometricView(bool isometric){
|
||||
void Renderer::setIsometricView(bool isometric) {
|
||||
m_IsometricView = isometric;
|
||||
}
|
||||
|
||||
glm::vec2 Renderer::getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight){
|
||||
glm::vec2 Renderer::getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight) {
|
||||
float isometricEased = utils::easeInOutExpo(m_IsometricShade);
|
||||
|
||||
float relativeX = (cursorPos.x / windowWidth * 2) - 1;
|
||||
@@ -133,7 +133,7 @@ glm::vec2 Renderer::getCursorWorldPos(const glm::vec2& cursorPos, float aspectRa
|
||||
float worldX = m_CamPos.x + deltaX * (1 - isometricEased) + (0.5 * deltaX + deltaY) * isometricEased;
|
||||
float worldY = m_CamPos.y + deltaY * (1 - isometricEased) + (-0.5 * deltaX + deltaY) * isometricEased;
|
||||
|
||||
return {worldX, worldY};
|
||||
return { worldX, worldY };
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ void WorldRenderer::changeZoom(float zoomStep) {
|
||||
static float sensibility = 1.5f;
|
||||
if (zoomStep < 0) {
|
||||
m_Zoom /= -zoomStep * sensibility;
|
||||
} else {
|
||||
} else {
|
||||
m_Zoom *= zoomStep * sensibility;
|
||||
}
|
||||
m_Renderer->setZoom(m_Zoom);
|
||||
@@ -157,7 +157,7 @@ glm::vec2 WorldRenderer::getCursorWorldPos() const {
|
||||
return m_Renderer->getCursorWorldPos({ io.MousePos.x, io.MousePos.y }, Display::getAspectRatio(), m_Zoom, Display::getWindowWidth(), Display::getWindowHeight());
|
||||
}
|
||||
|
||||
glm::vec2 WorldRenderer::getClickWorldPos() const{
|
||||
glm::vec2 WorldRenderer::getClickWorldPos() const {
|
||||
return m_Renderer->getCursorWorldPos(m_HoldCursorPos, Display::getAspectRatio(), m_Zoom, Display::getWindowWidth(), Display::getWindowHeight());
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
namespace TowerGui{
|
||||
namespace TowerGui {
|
||||
|
||||
static GLFWwindow* window;
|
||||
static std::unique_ptr<td::client::Client> client;
|
||||
@@ -29,7 +29,7 @@ static td::render::Renderer* renderer;
|
||||
|
||||
bool serverShouldStop = false;
|
||||
|
||||
void init(GLFWwindow* glfw_window, td::render::Renderer* render){
|
||||
void init(GLFWwindow* glfw_window, td::render::Renderer* render) {
|
||||
window = glfw_window;
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
@@ -44,47 +44,47 @@ void init(GLFWwindow* glfw_window, td::render::Renderer* render){
|
||||
client = std::make_unique<td::client::Client>(render);
|
||||
}
|
||||
|
||||
void beginFrame(){
|
||||
void beginFrame() {
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
}
|
||||
|
||||
void endFrame(){
|
||||
void endFrame() {
|
||||
ImGui::EndFrame();
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
}
|
||||
|
||||
void renderFPSCounter(){
|
||||
void renderFPSCounter() {
|
||||
ImGui::Begin("FPS Counter");
|
||||
ImGui::Text("FPS : %i", (int)ImGui::GetIO().Framerate);
|
||||
static bool vsync = true;
|
||||
if (ImGui::Checkbox("V-Sync", &vsync)){
|
||||
if (ImGui::Checkbox("V-Sync", &vsync)) {
|
||||
glfwSwapInterval(vsync);
|
||||
}
|
||||
static bool isometric = true;
|
||||
if (ImGui::Checkbox("Vue Isometrique ?", &isometric)){
|
||||
if (ImGui::Checkbox("Vue Isometrique ?", &isometric)) {
|
||||
renderer->setIsometricView(isometric);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
bool startServer(int port, const std::string& worldFilePath){
|
||||
bool startServer(int port, const std::string& worldFilePath) {
|
||||
if (worldFilePath.empty())
|
||||
return false;
|
||||
std::shared_ptr<td::server::Server> server = std::make_shared<td::server::Server>(worldFilePath);
|
||||
if (!server->start(port)){
|
||||
if (!server->start(port)) {
|
||||
return false;
|
||||
}
|
||||
serverThread = new std::thread([server](){
|
||||
while (!serverShouldStop){
|
||||
serverThread = new std::thread([server]() {
|
||||
while (!serverShouldStop) {
|
||||
static std::uint64_t lastTime = td::utils::getTime();
|
||||
std::uint64_t time = td::utils::getTime();
|
||||
|
||||
std::uint64_t delta = time - lastTime;
|
||||
|
||||
if (delta >= SERVER_TICK){
|
||||
if (delta >= SERVER_TICK) {
|
||||
server->tick(delta);
|
||||
lastTime = td::utils::getTime();
|
||||
std::uint64_t sleepTime = SERVER_TICK - (delta - SERVER_TICK);
|
||||
@@ -97,38 +97,37 @@ bool startServer(int port, const std::string& worldFilePath){
|
||||
return true;
|
||||
}
|
||||
|
||||
void renderMainMenu(){
|
||||
void renderMainMenu() {
|
||||
ImGui::Begin("Main Menu");
|
||||
if (ImGui::Button("Rejoindre une partie##join")){
|
||||
if (ImGui::Button("Rejoindre une partie##join")) {
|
||||
ImGui::OpenPopup("Rejoindre une partie##join_popup");
|
||||
}
|
||||
if (ImGui::Button("Créer une partie")){
|
||||
if (ImGui::Button("Créer une partie")) {
|
||||
ImGui::OpenPopup("Créer une partie##create_popup");
|
||||
}
|
||||
if (ImGui::Button("Options")){
|
||||
if (ImGui::Button("Options")) {
|
||||
|
||||
}
|
||||
static bool triedToConnect = false;
|
||||
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")){
|
||||
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")) {
|
||||
static char buffer[512] = "localhost";
|
||||
static int port = 25565;
|
||||
ImGui::InputText("Server Adress", buffer, sizeof(buffer));
|
||||
ImGui::InputInt("Port", &port, -1);
|
||||
if (ImGui::Button("Rejoindre")){
|
||||
if (ImGui::Button("Rejoindre")) {
|
||||
client->connect(buffer, port);
|
||||
triedToConnect = true;
|
||||
}
|
||||
if (triedToConnect){
|
||||
if (triedToConnect) {
|
||||
ImGui::Text("Impossible de se connecter");
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
triedToConnect = false;
|
||||
}
|
||||
|
||||
static bool triedToCreate = false;
|
||||
if (ImGui::BeginPopup("Créer une partie##create_popup")){
|
||||
if (ImGui::BeginPopup("Créer une partie##create_popup")) {
|
||||
static imgui_addons::ImGuiFileBrowser file_dialog;
|
||||
static int port = 25565;
|
||||
static std::string worldFilePath;
|
||||
@@ -136,33 +135,31 @@ void renderMainMenu(){
|
||||
ImGui::InputInt("Server Port", &port, -1);
|
||||
ImGui::Text(std::string("Fichier de monde sélectionné : " + (worldFilePath.empty() ? std::string("Aucun") : worldFilePath)).c_str());
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Ouvrir un fichier")){
|
||||
if (ImGui::Button("Ouvrir un fichier")) {
|
||||
ImGui::OpenPopup("WorldFileDialog");
|
||||
}
|
||||
if (file_dialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")){
|
||||
if (file_dialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")) {
|
||||
worldFilePath = file_dialog.selected_path;
|
||||
}
|
||||
if (ImGui::Button("Créer")){
|
||||
if (!startServer(port, worldFilePath)){
|
||||
if (ImGui::Button("Créer")) {
|
||||
if (!startServer(port, worldFilePath)) {
|
||||
triedToCreate = true;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
client->connect("localhost", port);
|
||||
}
|
||||
}
|
||||
if (triedToCreate)
|
||||
ImGui::Text("Failed to launch server");
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
triedToCreate = false;
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
ImVec4 getImGuiTeamColor(td::game::TeamColor color){
|
||||
switch (color){
|
||||
ImVec4 getImGuiTeamColor(td::game::TeamColor color) {
|
||||
switch (color) {
|
||||
case td::game::TeamColor::None:
|
||||
break;
|
||||
case td::game::TeamColor::Red:
|
||||
@@ -173,9 +170,9 @@ ImVec4 getImGuiTeamColor(td::game::TeamColor color){
|
||||
return ImVec4(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
void showPlayers(){
|
||||
if (ImGui::TreeNode(std::string("Players (" + std::to_string(client->getGame().getPlayers().size()) + ")##player_list").c_str())){
|
||||
for (auto pair : client->getGame().getPlayers()){
|
||||
void showPlayers() {
|
||||
if (ImGui::TreeNode(std::string("Players (" + std::to_string(client->getGame().getPlayers().size()) + ")##player_list").c_str())) {
|
||||
for (auto pair : client->getGame().getPlayers()) {
|
||||
const td::game::Player& player = pair.second;
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, getImGuiTeamColor(player.getTeamColor()));
|
||||
ImGui::Text(player.getName().c_str());
|
||||
@@ -185,19 +182,19 @@ void showPlayers(){
|
||||
}
|
||||
}
|
||||
|
||||
void showTeamSelection(){
|
||||
void showTeamSelection() {
|
||||
if (client->getGame().getPlayer() == nullptr)
|
||||
return;
|
||||
td::game::TeamColor playerTeam = client->getGame().getPlayer()->getTeamColor();
|
||||
|
||||
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Red ? "Leave" : "Join") + std::string(" Red Team")).c_str())){
|
||||
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Red ? "Leave" : "Join") + std::string(" Red Team")).c_str())) {
|
||||
if (playerTeam == td::game::TeamColor::Red)
|
||||
client->selectTeam(td::game::TeamColor::None);
|
||||
else
|
||||
client->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 (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Blue ? "Leave" : "Join") + std::string(" Blue Team")).c_str())) {
|
||||
if (playerTeam == td::game::TeamColor::Blue)
|
||||
client->selectTeam(td::game::TeamColor::None);
|
||||
else
|
||||
@@ -205,36 +202,35 @@ void showTeamSelection(){
|
||||
}
|
||||
}
|
||||
|
||||
void showLobbyProgress(){
|
||||
void showLobbyProgress() {
|
||||
const int timePassed = LOBBY_WAITING_TIME - client->getGame().getLobbyTime();
|
||||
const float progress = (float)timePassed / (float)(LOBBY_WAITING_TIME);
|
||||
if (progress > 0 && progress < 1){
|
||||
if (progress > 0 && progress < 1) {
|
||||
ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f), std::string(std::to_string(client->getGame().getLobbyTime() / 1000) + "s").c_str());
|
||||
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
|
||||
ImGui::Text("Time Remaining");
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
ImGui::Text("Waiting for players ...\n");
|
||||
}
|
||||
}
|
||||
|
||||
void showTPS(){
|
||||
void showTPS() {
|
||||
ImGui::Text("Server TPS : %.1f", client->getConnexion().getServerTPS());
|
||||
ImGui::Text("Server Ping : %i", client->getConnexion().getServerPing());
|
||||
}
|
||||
|
||||
void showStats(){
|
||||
void showStats() {
|
||||
ImGui::Text("Gold : %i", client->getGame().getPlayer()->getGold());
|
||||
}
|
||||
|
||||
void renderSummonMenu(){
|
||||
void renderSummonMenu() {
|
||||
static bool menu_open = false;
|
||||
if (menu_open){
|
||||
if (menu_open) {
|
||||
|
||||
ImGui::Begin("Summon", &menu_open);
|
||||
static int width = 100;
|
||||
ImTextureID my_tex_id = ImGui::GetIO().Fonts->TexID;
|
||||
for (int i = 0; i < 8; i++){
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::Image(my_tex_id, ImVec2(100, 100));
|
||||
@@ -243,7 +239,7 @@ void renderSummonMenu(){
|
||||
ImGui::Separator();
|
||||
static int values[16];
|
||||
ImGui::PushItemWidth(width);
|
||||
for (int i = 0; i < 8; i++){
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::InputInt("", values + i, 1, 10);
|
||||
@@ -251,7 +247,7 @@ void renderSummonMenu(){
|
||||
}
|
||||
ImGui::PopItemWidth();
|
||||
ImGui::Separator();
|
||||
for (int i = 0; i < 8; i++){
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::Image(my_tex_id, ImVec2(100, 100));
|
||||
@@ -259,7 +255,7 @@ void renderSummonMenu(){
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::PushItemWidth(width);
|
||||
for (int i = 8; i < 16; i++){
|
||||
for (int i = 8; i < 16; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::InputInt("", values + i, 1, 10);
|
||||
@@ -270,8 +266,8 @@ void renderSummonMenu(){
|
||||
}
|
||||
}
|
||||
|
||||
void renderGame(){
|
||||
if (client->getGame().getGameState() == td::game::GameState::Lobby){
|
||||
void renderGame() {
|
||||
if (client->getGame().getGameState() == td::game::GameState::Lobby) {
|
||||
ImGui::Begin("Lobby");
|
||||
|
||||
showTPS();
|
||||
@@ -281,7 +277,7 @@ void renderGame(){
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
if (client->getGame().getGameState() == td::game::GameState::Game){
|
||||
if (client->getGame().getGameState() == td::game::GameState::Game) {
|
||||
ImGui::Begin("Game");
|
||||
|
||||
showTPS();
|
||||
@@ -292,7 +288,7 @@ void renderGame(){
|
||||
}
|
||||
}
|
||||
|
||||
void tick(){
|
||||
void tick() {
|
||||
static std::uint64_t lastTime = td::utils::getTime();
|
||||
std::uint64_t time = td::utils::getTime();
|
||||
|
||||
@@ -303,7 +299,7 @@ void tick(){
|
||||
lastTime = td::utils::getTime();
|
||||
}
|
||||
|
||||
void render(){
|
||||
void render() {
|
||||
tick();
|
||||
beginFrame();
|
||||
client->render();
|
||||
@@ -319,11 +315,11 @@ void render(){
|
||||
endFrame();
|
||||
}
|
||||
|
||||
void destroy(){
|
||||
void destroy() {
|
||||
client->closeConnection();
|
||||
client.reset();
|
||||
serverShouldStop = true;
|
||||
if (serverThread != nullptr){
|
||||
if (serverThread != nullptr) {
|
||||
serverThread->join();
|
||||
delete serverThread;
|
||||
}
|
||||
|
||||
@@ -11,64 +11,64 @@
|
||||
|
||||
using namespace gl;
|
||||
|
||||
namespace GL{
|
||||
namespace GL {
|
||||
|
||||
VertexArray::~VertexArray(){
|
||||
if(m_ID != 0)
|
||||
glDeleteVertexArrays(1, &m_ID);
|
||||
}
|
||||
VertexArray::~VertexArray() {
|
||||
if (m_ID != 0)
|
||||
glDeleteVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
VertexArray::VertexArray(unsigned int vertexCount) : m_VertexCount(vertexCount){
|
||||
glGenVertexArrays(1, &m_ID);
|
||||
}
|
||||
VertexArray::VertexArray(unsigned int vertexCount) : m_VertexCount(vertexCount) {
|
||||
glGenVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
void VertexArray::bind() const{
|
||||
glBindVertexArray(m_ID);
|
||||
}
|
||||
void VertexArray::bind() const {
|
||||
glBindVertexArray(m_ID);
|
||||
}
|
||||
|
||||
void VertexArray::unbind() const{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
void VertexArray::unbind() const {
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void VertexArray::bindVertexBuffer(VertexBuffer& VertexBuffer){
|
||||
VertexBuffer.bind();
|
||||
VertexBuffer.bindVertexAttribs();
|
||||
m_VertexBuffers.push_back(std::move(VertexBuffer));
|
||||
}
|
||||
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() {
|
||||
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, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, data.size() * sizeof(float), data.data());
|
||||
unbind();
|
||||
}
|
||||
VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) {
|
||||
glGenBuffers(1, &m_ID);
|
||||
bind();
|
||||
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, data.size() * sizeof(float), data.data());
|
||||
unbind();
|
||||
}
|
||||
|
||||
void VertexBuffer::bind() const{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
||||
}
|
||||
void VertexBuffer::bind() const {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
||||
}
|
||||
|
||||
void VertexBuffer::unbind() const{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
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::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, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*)(intptr_t) pointer.m_Offset);
|
||||
}
|
||||
void VertexBuffer::bindVertexAttribs() const {
|
||||
for (const VertexAttribPointer& pointer : m_VertexAttribs) {
|
||||
glEnableVertexAttribArray(pointer.m_Index);
|
||||
glVertexAttribPointer(pointer.m_Index, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*)(intptr_t)pointer.m_Offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,8 @@ const unsigned int loadGLTexture(const char* fileName) {
|
||||
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"));
|
||||
std::cerr << "Erreur lors du chargement de la texture !" << std::endl;
|
||||
throw(std::runtime_error("Failed to load texture"));
|
||||
}
|
||||
|
||||
GLuint textureID;
|
||||
@@ -34,13 +34,13 @@ const unsigned int loadGLTexture(const char* fileName) {
|
||||
|
||||
if (comp == 3)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
else if (comp == 4)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
GL_UNSIGNED_BYTE, image);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
stbi_image_free((void*) image);
|
||||
stbi_image_free((void*)image);
|
||||
return textureID;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
GL::VertexArray loadMobModel(){
|
||||
GL::VertexArray loadMobModel() {
|
||||
std::vector<float> positions = {
|
||||
-0.5, -0.5,
|
||||
0.5, -0.5,
|
||||
@@ -48,19 +48,19 @@ GL::VertexArray loadMobModel(){
|
||||
return mobVao;
|
||||
}
|
||||
|
||||
GL::VertexArray loadWorldModel(const td::game::World* world){
|
||||
GL::VertexArray loadWorldModel(const td::game::World* world) {
|
||||
std::vector<float> positions;
|
||||
std::vector<float> colors;
|
||||
|
||||
for (const auto& chunkInfo : world->getChunks()){
|
||||
for (const auto& chunkInfo : world->getChunks()) {
|
||||
const td::game::ChunkCoord& coords = chunkInfo.first;
|
||||
td::game::ChunkPtr chunk = chunkInfo.second;
|
||||
|
||||
std::int32_t chunkX = coords.first * td::game::Chunk::ChunkWidth;
|
||||
std::int32_t chunkY = coords.second * td::game::Chunk::ChunkHeight;
|
||||
|
||||
for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++){
|
||||
for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++){
|
||||
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);
|
||||
@@ -100,7 +100,7 @@ GL::VertexArray loadWorldModel(const td::game::World* world){
|
||||
|
||||
const td::game::Color* tileColor = world->getTileColor(tile);
|
||||
|
||||
for (int i = 0; i < 6; i++){
|
||||
for (int i = 0; i < 6; i++) {
|
||||
int color = 255;
|
||||
color |= tileColor->r << 24;
|
||||
color |= tileColor->g << 16;
|
||||
@@ -115,7 +115,7 @@ GL::VertexArray loadWorldModel(const td::game::World* world){
|
||||
}
|
||||
}
|
||||
|
||||
for (int spawnColor = 0; spawnColor < 2; spawnColor++){
|
||||
for (int spawnColor = 0; spawnColor < 2; spawnColor++) {
|
||||
const game::Spawn& spawn = world->getTeam(game::TeamColor(spawnColor)).getSpawn();
|
||||
float fromX = spawn.x - 2, toX = spawn.x + 3;
|
||||
float fromY = spawn.y - 2, toY = spawn.y + 3;
|
||||
@@ -130,7 +130,7 @@ GL::VertexArray loadWorldModel(const td::game::World* world){
|
||||
toX, fromY,
|
||||
});
|
||||
|
||||
for (int i = 0; i < 6; i++){
|
||||
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;
|
||||
@@ -156,7 +156,7 @@ GL::VertexArray loadWorldModel(const td::game::World* world){
|
||||
return worldVao;
|
||||
}
|
||||
|
||||
GL::VertexArray loadTileSelectModel(){
|
||||
GL::VertexArray loadTileSelectModel() {
|
||||
std::vector<float> positions = {
|
||||
0, 0,
|
||||
1, 0,
|
||||
@@ -170,7 +170,7 @@ GL::VertexArray loadTileSelectModel(){
|
||||
int color = 255 << 24 | 255 << 16 | 255 << 8 | 150;
|
||||
float colorFloat;
|
||||
|
||||
memcpy((std::uint8_t*) &colorFloat, &color, sizeof(float));
|
||||
memcpy((std::uint8_t*)&colorFloat, &color, sizeof(float));
|
||||
|
||||
std::vector<float> colors(6, colorFloat);
|
||||
|
||||
|
||||
@@ -49,13 +49,13 @@ void main(void){
|
||||
}
|
||||
)";
|
||||
|
||||
EntityShader::EntityShader(): ShaderProgram(){}
|
||||
EntityShader::EntityShader() : ShaderProgram() {}
|
||||
|
||||
void EntityShader::loadShader(){
|
||||
void EntityShader::loadShader() {
|
||||
ShaderProgram::loadProgram(vertexSource, fragmentSource);
|
||||
}
|
||||
|
||||
void EntityShader::getAllUniformLocation(){
|
||||
void EntityShader::getAllUniformLocation() {
|
||||
location_aspect_ratio = getUniformLocation("aspectRatio");
|
||||
location_zoom = getUniformLocation("zoom");
|
||||
location_cam = getUniformLocation("camPos");
|
||||
@@ -63,18 +63,18 @@ void EntityShader::getAllUniformLocation(){
|
||||
location_viewtype = getUniformLocation("isometricView");
|
||||
}
|
||||
|
||||
void EntityShader::setCamPos(const glm::vec2& camPos){
|
||||
void EntityShader::setCamPos(const glm::vec2& camPos) {
|
||||
loadVector(location_cam, camPos);
|
||||
}
|
||||
void EntityShader::setZoom(float zoom){
|
||||
void EntityShader::setZoom(float zoom) {
|
||||
loadFloat(location_zoom, zoom);
|
||||
}
|
||||
void EntityShader::setAspectRatio(float aspectRatio){
|
||||
void EntityShader::setAspectRatio(float aspectRatio) {
|
||||
loadFloat(location_aspect_ratio, aspectRatio);
|
||||
}
|
||||
void EntityShader::setModelPos(const glm::vec2& modelPos){
|
||||
void EntityShader::setModelPos(const glm::vec2& modelPos) {
|
||||
loadVector(location_translation, modelPos);
|
||||
}
|
||||
void EntityShader::setIsometricView(float isometric){
|
||||
void EntityShader::setIsometricView(float isometric) {
|
||||
loadFloat(location_viewtype, isometric);
|
||||
}
|
||||
|
||||
@@ -15,58 +15,58 @@
|
||||
|
||||
using namespace gl;
|
||||
|
||||
ShaderProgram::ShaderProgram():
|
||||
programID(0), vertexShaderID(0), fragmentShaderID(0){
|
||||
ShaderProgram::ShaderProgram() :
|
||||
programID(0), vertexShaderID(0), fragmentShaderID(0) {
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram(){
|
||||
ShaderProgram::~ShaderProgram() {
|
||||
cleanUp();
|
||||
}
|
||||
|
||||
void ShaderProgram::start() const{
|
||||
void ShaderProgram::start() const {
|
||||
glUseProgram(programID);
|
||||
}
|
||||
|
||||
void ShaderProgram::stop() const{
|
||||
void ShaderProgram::stop() const {
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
int ShaderProgram::getUniformLocation(const std::string& uniformName) const{
|
||||
int ShaderProgram::getUniformLocation(const std::string& uniformName) const {
|
||||
const int location = glGetUniformLocation(programID, uniformName.c_str());
|
||||
if (location == -1){
|
||||
if (location == -1) {
|
||||
std::cout << "Warning ! Uniform variable " << uniformName << " not found !\n";
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
void ShaderProgram::loadFloat(const int location, const float value) const{
|
||||
void ShaderProgram::loadFloat(const int location, const float value) const {
|
||||
glUniform1f(location, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadInt(const int& location, const int& value) const{
|
||||
void ShaderProgram::loadInt(const int& location, const int& value) const {
|
||||
glUniform1i(location, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadVector(const int& location,
|
||||
const glm::vec2& vector) const{
|
||||
const glm::vec2& vector) const {
|
||||
glUniform2f(location, vector.x, vector.y);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadVector(const int& location,
|
||||
const glm::vec3& vector) const{
|
||||
const glm::vec3& vector) const {
|
||||
glUniform3f(location, vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadVector(const int& location,
|
||||
const glm::vec4& vector) const{
|
||||
const glm::vec4& vector) const {
|
||||
glUniform4f(location, vector.x, vector.y, vector.z, vector.w);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadBoolean(const int& location, const bool& value) const{
|
||||
void ShaderProgram::loadBoolean(const int& location, const bool& value) const {
|
||||
glUniform1i(location, value);
|
||||
}
|
||||
|
||||
void ShaderProgram::cleanUp() const{
|
||||
void ShaderProgram::cleanUp() const {
|
||||
stop();
|
||||
glDetachShader(programID, vertexShaderID);
|
||||
glDetachShader(programID, fragmentShaderID);
|
||||
@@ -76,7 +76,7 @@ void ShaderProgram::cleanUp() const{
|
||||
}
|
||||
|
||||
void ShaderProgram::loadProgramFile(const std::string& vertexFile,
|
||||
const std::string& fragmentFile){
|
||||
const std::string& fragmentFile) {
|
||||
vertexShaderID = loadShaderFromFile(vertexFile, GL_VERTEX_SHADER);
|
||||
fragmentShaderID = loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER);
|
||||
programID = glCreateProgram();
|
||||
@@ -88,7 +88,7 @@ void ShaderProgram::loadProgramFile(const std::string& vertexFile,
|
||||
}
|
||||
|
||||
void ShaderProgram::loadProgram(const std::string& vertexSource,
|
||||
const std::string& fragmentSource){
|
||||
const std::string& fragmentSource) {
|
||||
vertexShaderID = loadShader(vertexSource, GL_VERTEX_SHADER);
|
||||
fragmentShaderID = loadShader(fragmentSource, GL_FRAGMENT_SHADER);
|
||||
programID = glCreateProgram();
|
||||
@@ -99,7 +99,7 @@ void ShaderProgram::loadProgram(const std::string& vertexSource,
|
||||
getAllUniformLocation();
|
||||
}
|
||||
|
||||
int ShaderProgram::loadShader(const std::string& source, GLenum type){
|
||||
int ShaderProgram::loadShader(const std::string& source, GLenum type) {
|
||||
unsigned int shaderID = glCreateShader(type);
|
||||
|
||||
const char* c_str = source.c_str();
|
||||
@@ -108,7 +108,7 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type){
|
||||
glCompileShader(shaderID);
|
||||
GLint compilesuccessful;
|
||||
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
|
||||
if (compilesuccessful == false){
|
||||
if (compilesuccessful == false) {
|
||||
std::cout << "Could not compile shader !\n";
|
||||
GLsizei size;
|
||||
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
|
||||
@@ -119,12 +119,12 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type){
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type){
|
||||
int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) {
|
||||
std::string shaderSource = "";
|
||||
std::ifstream fileStream(file);
|
||||
if (fileStream.is_open()){
|
||||
if (fileStream.is_open()) {
|
||||
std::string line;
|
||||
while (getline(fileStream, line)){
|
||||
while (getline(fileStream, line)) {
|
||||
shaderSource += line + "\n";
|
||||
}
|
||||
fileStream.close();
|
||||
@@ -137,7 +137,7 @@ int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type){
|
||||
glCompileShader(shaderID);
|
||||
GLint compilesuccessful;
|
||||
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
|
||||
if (compilesuccessful == false){
|
||||
if (compilesuccessful == false) {
|
||||
std::cout << "Could not compile shader !\n";
|
||||
GLsizei size;
|
||||
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
|
||||
@@ -148,6 +148,6 @@ int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type){
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
void ShaderProgram::loadMatrix(const int& location, const glm::mat4& matrix){
|
||||
void ShaderProgram::loadMatrix(const int& location, const glm::mat4& matrix) {
|
||||
glUniformMatrix4fv(location, 1, false, glm::value_ptr(matrix));
|
||||
}
|
||||
|
||||
@@ -46,28 +46,28 @@ void main(void){
|
||||
}
|
||||
)";
|
||||
|
||||
WorldShader::WorldShader(): ShaderProgram(){}
|
||||
WorldShader::WorldShader() : ShaderProgram() {}
|
||||
|
||||
void WorldShader::loadShader(){
|
||||
void WorldShader::loadShader() {
|
||||
ShaderProgram::loadProgram(vertexSource, fragmentSource);
|
||||
}
|
||||
|
||||
void WorldShader::getAllUniformLocation(){
|
||||
void WorldShader::getAllUniformLocation() {
|
||||
location_aspect_ratio = getUniformLocation("aspectRatio");
|
||||
location_zoom = getUniformLocation("zoom");
|
||||
location_cam = getUniformLocation("camPos");
|
||||
location_viewtype = getUniformLocation("isometricView");
|
||||
}
|
||||
|
||||
void WorldShader::setCamPos(const glm::vec2& camPos){
|
||||
void WorldShader::setCamPos(const glm::vec2& camPos) {
|
||||
loadVector(location_cam, camPos);
|
||||
}
|
||||
void WorldShader::setZoom(float zoom){
|
||||
void WorldShader::setZoom(float zoom) {
|
||||
loadFloat(location_zoom, zoom);
|
||||
}
|
||||
void WorldShader::setAspectRatio(float aspectRatio){
|
||||
void WorldShader::setAspectRatio(float aspectRatio) {
|
||||
loadFloat(location_aspect_ratio, aspectRatio);
|
||||
}
|
||||
void WorldShader::setIsometricView(float isometric){
|
||||
void WorldShader::setIsometricView(float isometric) {
|
||||
loadFloat(location_viewtype, isometric);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user