mirror of
https://github.com/MichaelFisher1997/opengl-cpp.git
synced 2025-04-27 14:13:10 +00:00
added OpenGL
This commit is contained in:
parent
18298f046a
commit
438e9cd03a
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
g++ src/*.cpp -o build/opengl -lSDL2
|
g++ src/*.cpp -o build/opengl -lSDL2 -lGLEW -lGL
|
||||||
./build/opengl
|
./build/opengl
|
||||||
|
73
src/sdl.cpp
73
src/sdl.cpp
@ -8,51 +8,71 @@ SdlWindow::SdlWindow(const char* title, int width, int height)
|
|||||||
m_isFullscreen(false),
|
m_isFullscreen(false),
|
||||||
m_width(width),
|
m_width(width),
|
||||||
m_height(height),
|
m_height(height),
|
||||||
m_glContect(nullptr)
|
m_glContext(nullptr)
|
||||||
{
|
{
|
||||||
// 1. Set attributes
|
// 1. Set attributes
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||||
|
|
||||||
// 2. Create the window
|
// 2. Create the window with OpenGL flag
|
||||||
m_window = SDL_CreateWindow(
|
m_window = SDL_CreateWindow(title,
|
||||||
title,
|
SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED,
|
width,
|
||||||
width,
|
height,
|
||||||
height,
|
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
|
||||||
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
|
|
||||||
);
|
|
||||||
if (!m_window) {
|
if (!m_window) {
|
||||||
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
|
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Create the renderer
|
// 3. Create OpenGL context
|
||||||
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);
|
m_glContext = SDL_GL_CreateContext(m_window);
|
||||||
if (!m_renderer) {
|
if (!m_glContext) {
|
||||||
std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl;
|
std::cerr << "Failed to create GL context: " << SDL_GetError() << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 4. Optionally init GLEW (if not on macOS core profile)
|
||||||
|
#ifndef __APPLE__
|
||||||
|
glewExperimental = GL_TRUE;
|
||||||
|
if (glewInit() != GLEW_OK) {
|
||||||
|
std::cerr << "Failed to init GLEW" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// 4. If everything succeeded, mark the application as running
|
// 5. Set vsync (optional)
|
||||||
|
SDL_GL_SetSwapInterval(1);
|
||||||
|
|
||||||
|
// 6. Mark as running
|
||||||
|
m_isRunning = true;
|
||||||
m_isRunning = true;
|
m_isRunning = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
SdlWindow::~SdlWindow() {
|
SdlWindow::~SdlWindow() {
|
||||||
// Cleanup
|
// If using SDL Renderer, destroy it. But if you’re purely using OpenGL, you might remove it.
|
||||||
if (m_renderer) {
|
if (m_renderer) {
|
||||||
SDL_DestroyRenderer(m_renderer);
|
SDL_DestroyRenderer(m_renderer);
|
||||||
m_renderer = nullptr;
|
m_renderer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete the OpenGL context
|
||||||
|
if (m_glContext) {
|
||||||
|
SDL_GL_DeleteContext(m_glContext);
|
||||||
|
m_glContext = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
if (m_window) {
|
if (m_window) {
|
||||||
SDL_DestroyWindow(m_window);
|
SDL_DestroyWindow(m_window);
|
||||||
m_window = nullptr;
|
m_window = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SdlWindow::run() {
|
void SdlWindow::run() {
|
||||||
while (m_isRunning) {
|
while (m_isRunning) {
|
||||||
processEvents();
|
processEvents();
|
||||||
@ -90,13 +110,14 @@ void SdlWindow::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SdlWindow::render() {
|
void SdlWindow::render() {
|
||||||
// Set background color to red
|
// Use GL calls instead of SDL’s renderer
|
||||||
SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 255);
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
SDL_RenderClear(m_renderer);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
// You can draw shapes, textures, etc. here
|
// TODO: Draw with OpenGL here (shaders, triangles, etc.)
|
||||||
|
|
||||||
SDL_RenderPresent(m_renderer);
|
// Swap buffers
|
||||||
|
SDL_GL_SwapWindow(m_window);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SdlWindow::change_res(int newWidth, int newHeight)
|
void SdlWindow::change_res(int newWidth, int newHeight)
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
// Include the OpenGL headers (these may vary by system)
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
#include <OpenGL/gl3.h> // On macOS, <OpenGL/gl3.h> is often used
|
||||||
|
#else
|
||||||
|
#include <GL/glew.h> // or <glad/glad.h> if using GLAD
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
// Forward declaration of classes and structs if needed
|
// Forward declaration of classes and structs if needed
|
||||||
// class SomethingElse;
|
// class SomethingElse;
|
||||||
@ -27,6 +35,7 @@ private:
|
|||||||
bool m_isFullscreen;
|
bool m_isFullscreen;
|
||||||
int m_width;
|
int m_width;
|
||||||
int m_height;
|
int m_height;
|
||||||
|
SDL_GLContext m_glContext;
|
||||||
|
|
||||||
// Private methods
|
// Private methods
|
||||||
void processEvents();
|
void processEvents();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user