generated from AfonsoCMSousa/CPP-Template
Fix: Small fix for dependencies
This commit is contained in:
+236
-107
@@ -1,14 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define GL_SILENCE_DEPRECATION
|
||||
|
||||
// glad and GLFW
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
// glm math
|
||||
#include <glm/glm.hpp>
|
||||
@@ -18,11 +16,15 @@
|
||||
// HPP files
|
||||
#include "shader.h"
|
||||
|
||||
//imGui
|
||||
// imGui
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_opengl3.h"
|
||||
|
||||
// CURL
|
||||
#include "http_get.h"
|
||||
#include <curl/curl.h>
|
||||
|
||||
#define VERTEX_FILE "./shaders/vertex.glsl"
|
||||
#define FRAGMENT_FILE "./shaders/fragment.glsl"
|
||||
#define COMPUTE_FILE "./shaders/compute.glsl"
|
||||
@@ -31,134 +33,261 @@
|
||||
const float SCR_WIDTH = 1200.0f;
|
||||
const float SCR_HEIGHT = 1200.0f;
|
||||
|
||||
unsigned char apiExist = 0;
|
||||
unsigned char apiIsValid = 0;
|
||||
unsigned char isVsync = 1;
|
||||
char savedLocation[128] = "Lisbon";
|
||||
float location_latitude = -1; // default invalid location_latitude
|
||||
float location_longitude = -1; // default invalid location_longitude
|
||||
|
||||
int currentAA = 4; // 0=off, 2=2x, 4=4x, 8=8x
|
||||
|
||||
int main(void) {
|
||||
// Initialize GLFW
|
||||
if (!glfwInit()) return -1;
|
||||
// Initialize GLFW
|
||||
if (!glfwInit())
|
||||
return -1;
|
||||
|
||||
// Set OpenGL version (4.1 Core) and other window hints
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // REQUIRED on macOS
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_SAMPLES, 8); // 8x MSAA
|
||||
// Set OpenGL version (4.1 Core) and other window hints
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // REQUIRED on macOS
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_SAMPLES, currentAA);
|
||||
|
||||
GLFWwindow* window = glfwCreateWindow((int)SCR_WIDTH, (int)SCR_HEIGHT, "RASTER", nullptr, nullptr);
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
GLFWwindow *window = glfwCreateWindow((int)SCR_WIDTH, (int)SCR_HEIGHT, "RASTER", nullptr, nullptr);
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cerr << "Failed to initialize GLAD\n";
|
||||
return -1;
|
||||
}
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cerr << "Failed to initialize GLAD\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
glViewport(0, 0, (int)SCR_WIDTH, (int)SCR_HEIGHT);
|
||||
glViewport(0, 0, (int)SCR_WIDTH, (int)SCR_HEIGHT);
|
||||
|
||||
//DEBUG
|
||||
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
|
||||
// DEBUG
|
||||
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
|
||||
|
||||
// Setup Dear ImGui context
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||
// Setup Dear ImGui context
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||
|
||||
// Setup Platform/Renderer backends
|
||||
ImGui_ImplGlfw_InitForOpenGL(window, true); // Second param install_callback=true will install GLFW callbacks and chain to existing ones.
|
||||
ImGui_ImplOpenGL3_Init();
|
||||
// Setup Platform/Renderer backends
|
||||
ImGui_ImplGlfw_InitForOpenGL(window, true); // Second param install_callback=true will install GLFW
|
||||
// callbacks and chain to existing ones.
|
||||
ImGui_ImplOpenGL3_Init();
|
||||
|
||||
//DEBUG
|
||||
std::cout << "ImGui Version: " << IMGUI_VERSION << std::endl;
|
||||
std::string api_key = "";
|
||||
// DEBUG
|
||||
std::cout << "ImGui Version: " << IMGUI_VERSION << std::endl;
|
||||
std::string api_key = "";
|
||||
|
||||
// Get API keys from .env file
|
||||
std::ifstream envFile(ENV_FILE);
|
||||
if (envFile.is_open()) {
|
||||
std::string line;
|
||||
while (std::getline(envFile, line)) {
|
||||
// std::cout << line << std::endl; // Print each line (for debugging)
|
||||
// Here you can parse the line to extract key-value pairs if needed
|
||||
// Get API keys from .env file
|
||||
std::ifstream envFile(ENV_FILE);
|
||||
if (envFile.is_open()) {
|
||||
std::string line;
|
||||
|
||||
if (line.find("API_KEY=") == 0) {
|
||||
api_key = line.substr(8); // Extract value after "API_KEY="
|
||||
}
|
||||
}
|
||||
envFile.close();
|
||||
} else {
|
||||
std::cerr << "Could not open the .env file." << std::endl;
|
||||
}
|
||||
while (std::getline(envFile, line)) {
|
||||
// std::cout << line << std::endl; // Print each line (for
|
||||
// debugging) Here you can parse the line to extract key-value pairs
|
||||
// if needed
|
||||
|
||||
// ERROR: macOS does not support compute shaders in OpenGL < 4.3.
|
||||
// GLuint computeProgram = createComputeProgram(COMPUTE_FILE);
|
||||
if (line.find("WEATHER_API_KEY=") == 0) {
|
||||
api_key = line.substr(16); // Extract value after "WEATHER_API_KEY="
|
||||
apiExist = 1;
|
||||
}
|
||||
}
|
||||
envFile.close();
|
||||
} else {
|
||||
std::cerr << "Could not open the .env file." << std::endl;
|
||||
}
|
||||
|
||||
// Load shaders
|
||||
GLuint shaderProgram = createShaderProgram(VERTEX_FILE, FRAGMENT_FILE);
|
||||
// ERROR: macOS does not support compute shaders in OpenGL < 4.3.
|
||||
// GLuint computeProgram = createComputeProgram(COMPUTE_FILE);
|
||||
|
||||
// Check if shader program was created successfully
|
||||
if (shaderProgram == 0) {
|
||||
std::cerr << "Failed to create shader programs.\n";
|
||||
return -1;
|
||||
}
|
||||
// Load shaders
|
||||
GLuint shaderProgram = createShaderProgram(VERTEX_FILE, FRAGMENT_FILE);
|
||||
|
||||
// FPS tracking
|
||||
double lastTime = glfwGetTime(); // when we last printed FPS
|
||||
double fps = 0.0;
|
||||
int frameCount = 0; // frames since last FPS print
|
||||
float mousePos[2] = {0.0f, 0.0f};
|
||||
// Quad to render vertex too
|
||||
float vertices[] = {// positions
|
||||
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f};
|
||||
|
||||
// Render loop
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// Time
|
||||
float timeValue = (float)glfwGetTime();
|
||||
mousePos[0] = (float)io.MousePos.x;
|
||||
mousePos[1] = (float)io.MousePos.y;
|
||||
unsigned int indices[] = {0, 1, 2, 2, 1, 3};
|
||||
|
||||
// Input
|
||||
glfwPollEvents();
|
||||
GLuint VAO, VBO, EBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenBuffers(1, &EBO);
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
// --- FPS Counter ---
|
||||
double currentTime = glfwGetTime();
|
||||
double delta = currentTime - lastTime;
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
if (delta >= 1.0) { // print every ~1 second
|
||||
fps = double(frameCount) / delta;
|
||||
std::cout << "FPS: " << fps
|
||||
<< " Time: " << timeValue
|
||||
<< " Mouse: (" << mousePos[0] << ", " << mousePos[1] << ")"
|
||||
<< std::endl;
|
||||
// positions
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
frameCount = 0;
|
||||
lastTime = currentTime;
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
|
||||
frameCount++;
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glUseProgram(shaderProgram);
|
||||
// FPS tracking
|
||||
double lastTime = glfwGetTime(); // when we last printed FPS
|
||||
double fps = 0.0;
|
||||
int frameCount = 0; // frames since last FPS print
|
||||
float mousePos[2] = {0.0f, 0.0f};
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
// Check if API key is valid (dummy check here, implement actual validation
|
||||
// as needed)
|
||||
char *url = (char *)malloc(1024);
|
||||
if (apiExist) {
|
||||
snprintf(url, 1024, "http://api.openweathermap.org/geo/1.0/direct?q=%s&appid=%s", savedLocation, api_key.c_str());
|
||||
char *response = http_get(url);
|
||||
if (response) {
|
||||
apiIsValid = 1;
|
||||
// Extract latitude and longitude from response (simple parsing,
|
||||
// improve as needed)
|
||||
std::string respStr(response);
|
||||
size_t latPos = respStr.find("\"lat\":");
|
||||
size_t lonPos = respStr.find("\"lon\":");
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
if (latPos != std::string::npos && lonPos != std::string::npos) {
|
||||
sscanf(respStr.c_str() + latPos, "\"lat\":%f", &location_latitude);
|
||||
sscanf(respStr.c_str() + lonPos, "\"lon\":%f", &location_longitude);
|
||||
std::cout << "Location: " << savedLocation << " Latitude: " << location_latitude << " Longitude: " << location_longitude << std::endl;
|
||||
} else {
|
||||
std::cerr << "Could not parse latitude and longitude from API "
|
||||
"response."
|
||||
<< std::endl;
|
||||
apiIsValid = 0;
|
||||
}
|
||||
|
||||
glDeleteProgram(shaderProgram);
|
||||
free(response);
|
||||
} else {
|
||||
apiIsValid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
char *currentWeather = (char *)malloc(1024); // buffer for current weather data
|
||||
snprintf(currentWeather, 1024, "No data");
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
// Render loop
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
glfwPollEvents();
|
||||
|
||||
// Time
|
||||
float timeValue = (float)glfwGetTime();
|
||||
mousePos[0] = (float)io.MousePos.x;
|
||||
mousePos[1] = (float)io.MousePos.y;
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
GLint resLoc = glGetUniformLocation(shaderProgram, "u_resolution");
|
||||
glUniform2f(resLoc, (float)SCR_WIDTH, (float)SCR_HEIGHT);
|
||||
|
||||
GLint timeLoc = glGetUniformLocation(shaderProgram, "u_time");
|
||||
glUniform1f(timeLoc, timeValue);
|
||||
|
||||
// draw quad
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// API request to get weather data
|
||||
if (((int)(timeValue * 100)) % 1000 == 1 && apiExist && apiIsValid) {
|
||||
snprintf(url, 1024, "http://api.openweathermap.org/data/2.5/direct?q=%s&appid=%s", savedLocation, api_key.c_str());
|
||||
char *response = http_get(url);
|
||||
if (response) {
|
||||
snprintf(currentWeather, 1024, "%s", response);
|
||||
free(response);
|
||||
}
|
||||
}
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::Begin("Debug Window");
|
||||
ImGui::Text("FPS: %.1f", fps);
|
||||
ImGui::Text("Time: %.2f seconds", timeValue);
|
||||
ImGui::Separator();
|
||||
ImGui::Text("Options:");
|
||||
// V-Sync options
|
||||
if (ImGui::Checkbox("V-Sync", (bool *)&isVsync)) {
|
||||
glfwSwapInterval(isVsync ? 1 : 0);
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::Text("API Key Status:");
|
||||
if (apiExist) {
|
||||
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "\t\tAPI Key Loaded OK");
|
||||
} else {
|
||||
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "\t\tAPI Key Missing or Invalid!");
|
||||
}
|
||||
if (apiIsValid) {
|
||||
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "\t\tAPI Key Validated OK");
|
||||
} else {
|
||||
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "\t\tAPI Key Not Validated!");
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::Text("Program Info:");
|
||||
ImGui::Text("\t\tOpenGL Version: %s", glGetString(GL_VERSION));
|
||||
ImGui::Text("\t\tShader Program: %s", shaderProgram ? "Loaded" : "Error");
|
||||
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Begin("Weather Data");
|
||||
if (apiExist && apiIsValid) {
|
||||
ImGui::TextWrapped("%s", currentWeather);
|
||||
} else if (!apiExist) {
|
||||
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "No API Key found in .env file!");
|
||||
} else if (!apiIsValid) {
|
||||
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "API Key is not valid!");
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::Text("API URL: %s", url);
|
||||
ImGui::End();
|
||||
|
||||
// --- FPS Counter ---
|
||||
double currentTime = glfwGetTime();
|
||||
double delta = currentTime - lastTime;
|
||||
|
||||
if (delta >= 1.0) { // print every ~1 second
|
||||
fps = double(frameCount) / delta;
|
||||
/* std::cout << "FPS: " << fps
|
||||
<< " Time: " << timeValue
|
||||
<< " Mouse: (" << mousePos[0] << ", " << mousePos[1] << ")"
|
||||
<< std::endl;
|
||||
*/
|
||||
frameCount = 0;
|
||||
lastTime = currentTime;
|
||||
}
|
||||
|
||||
frameCount++;
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
glDeleteProgram(shaderProgram);
|
||||
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
free(url);
|
||||
free(currentWeather);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -1,5 +1,8 @@
|
||||
#include "shader.h"
|
||||
|
||||
#define GL_SILENCE_DEPRECATION
|
||||
|
||||
|
||||
// Shader loader helper
|
||||
std::string loadShaderSource(const std::string& filepath) {
|
||||
std::ifstream file(filepath);
|
||||
@@ -9,7 +12,7 @@ std::string loadShaderSource(const std::string& filepath) {
|
||||
}
|
||||
std::stringstream buffer;
|
||||
|
||||
std::cout << "Loaded " << filepath << " successfully!\n";
|
||||
// std::cout << "Loaded " << filepath << " successfully!\n";
|
||||
|
||||
buffer << file.rdbuf();
|
||||
return buffer.str();
|
||||
|
||||
Reference in New Issue
Block a user