165 lines
4.7 KiB
C++

#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#define GL_SILENCE_DEPRECATION
// glad and GLFW
#include <glad/glad.h>
#include <GLFW/glfw3.h>
// glm math
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// HPP files
#include "shader.h"
//imGui
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#define VERTEX_FILE "./shaders/vertex.glsl"
#define FRAGMENT_FILE "./shaders/fragment.glsl"
#define COMPUTE_FILE "./shaders/compute.glsl"
#define ENV_FILE "./.env"
const float SCR_WIDTH = 1200.0f;
const float SCR_HEIGHT = 1200.0f;
int main(void) {
// 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
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;
}
glViewport(0, 0, (int)SCR_WIDTH, (int)SCR_HEIGHT);
//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 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 = "";
// 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
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;
}
// ERROR: macOS does not support compute shaders in OpenGL < 4.3.
// GLuint computeProgram = createComputeProgram(COMPUTE_FILE);
// Load shaders
GLuint shaderProgram = createShaderProgram(VERTEX_FILE, FRAGMENT_FILE);
// Check if shader program was created successfully
if (shaderProgram == 0) {
std::cerr << "Failed to create shader programs.\n";
return -1;
}
// 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};
// Render loop
while (!glfwWindowShouldClose(window)) {
// Time
float timeValue = (float)glfwGetTime();
mousePos[0] = (float)io.MousePos.x;
mousePos[1] = (float)io.MousePos.y;
// Input
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// --- 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++;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
glDeleteProgram(shaderProgram);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}