Skip to content

Commit

Permalink
Replace GLFW to SDL2 + code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
desatur committed Sep 2, 2024
1 parent 8c46a3a commit e18ca55
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 38 deletions.
1 change: 1 addition & 0 deletions Future/src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Future {
void Engine::Run() {
auto window = new Future::Window();
window->Init();
auto renderer = new Future::OpenGL(window);
renderer->Init();
//window->Init();
Expand Down
28 changes: 4 additions & 24 deletions Future/src/Rendering/OpenGL/OpenGL.cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,33 @@
#include "OpenGL.hpp"

#include <cmath>
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include "Shaders.hpp"
#include "Camera.hpp"
#include "Buffers/EBO.hpp"
#include "Buffers/VAO.hpp"
#include "Buffers/VBO.hpp"
#include "Textures/Texture.hpp"
#include "Mesh.hpp"
#include "Model.hpp"

namespace Future
{
OpenGL::OpenGL(Window* window)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_window = window;
}

OpenGL::~OpenGL()
{
glfwTerminate();

}

bool OpenGL::Init()
{
GLFWwindow* glfwWindow = glfwCreateWindow(1920, 1080, "GLFW Window", NULL, NULL); // TODO: Migrate do SDL2 later
glfwMakeContextCurrent(glfwWindow);
gladLoadGL();
glViewport(0, 0, 1920, 1080);


// Generates Shader object using shaders default.vert and default.frag
Shaders shaderProgram("Shaders/default.vert", "Shaders/default.frag");


// Take care of all the light related things
glm::vec4 lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec3 lightPos = glm::vec3(0.5f, 0.5f, 0.5f);
Expand All @@ -64,22 +47,19 @@ namespace Future

Model test("ground/scene.gltf");

while (true)
while (m_window->IsRunning())
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); // Clear screen with color (black)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers

camera.Inputs(glfwWindow); // Handles camera input
camera.UpdateMatrix(45.0f, 0.1f, 100.0f); // Updates and exports the camera matrix to the Vertex Shader

test.Draw(shaderProgram, camera);

glfwSwapBuffers(glfwWindow); // Swap the back buffer with the front buffer
glfwPollEvents(); // Take care of all GLFW events
m_window->Tick();
}

shaderProgram.Delete();
glfwDestroyWindow(glfwWindow);
return true;
}
}
19 changes: 8 additions & 11 deletions Future/src/Window/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Future
: mTitle(title), mWidth(width), mHeight(height), mFullscreen(fullscreen),
sdlWindow(nullptr), sdlScreenSurface(nullptr), mRunning(false)
{
gladLoadGLLoader((GLADloadproc) SDL_GL_GetProcAddress);

}

Window::~Window()
Expand Down Expand Up @@ -54,14 +54,12 @@ namespace Future
{
FE_CORE_INFO("Screen surface created");
}
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_ACCELERATED);
if (sdlRenderer != nullptr)
glSdlContext = SDL_GL_CreateContext(sdlWindow);
if (glSdlContext != nullptr)
{
FE_CORE_INFO("SDL Renderer created");
FE_CORE_INFO("GL-SDL Renderer context created");
mRunning = true;
}
mRunning = true; // temp
sdlGLContext = SDL_GL_CreateContext(sdlWindow);
}
}
return success;
Expand Down Expand Up @@ -89,20 +87,19 @@ namespace Future
return mRunning;
}

void Window::Loop()
void Window::Tick()
{
while (mRunning)
if (mRunning)
{
SDL_GL_SwapWindow(sdlWindow);

SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
mRunning = false;
}

SDL_UpdateWindowSurface(sdlWindow);
SDL_Delay(1000 / 30);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions Future/src/Window/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ namespace Future
bool Init(); // Initialization method
void Destroy(); // Cleanup method to free resources
bool IsRunning() const;
void Loop();

SDL_Window* GetWindow() const { return sdlWindow; }
SDL_Surface* GetScreenSurface() const { return sdlScreenSurface; }
[[nodiscard]] unsigned int GetWidth() const { return sdlScreenSurface->w; }
[[nodiscard]] unsigned int GetHeight() const { return sdlScreenSurface->h; }
void Tick();

private:
std::string mTitle;
Expand All @@ -31,8 +33,7 @@ namespace Future

SDL_Window* sdlWindow;
SDL_Surface* sdlScreenSurface;
SDL_Renderer* sdlRenderer;
SDL_GLContext sdlGLContext;
SDL_GLContext glSdlContext;

bool mRunning;
};
Expand Down

0 comments on commit e18ca55

Please sign in to comment.