I am creating a basic 3D game using SDL2 and OpenGL3, and while I intend to stick with these libraries for this project, I still want to abstract/wrap them so that I don’t leak types everywhere. Also, if I ever get around to learning another graphics API (which I’ll be doing in later semesters of college), it could be a little bit easier to swap things. What I’m noticing, though, is it’s not as simple as having SDL implementations and OpenGL implementations because there are functions like SDL_GL_SetAttribute
. Also, I use libraries like ImGui and RmlUi (for creating game UI) which rely on SDL2 and OpenGL creating a similar problem.
I had asked a similar question elsewhere, and the suggestion I got was to have another class to, I guess, facilitate/communicate between the two? The only other solution that I can think of is just to wrap everything in switch statements or macros and change the logic based on some flag. I am open to other suggestions, though.
For reference here is some of my code.
void SDLPlatform::CreateWindow(std::string title, int width, int height, bool resizable)
{
mMainWindow = new SDLWindow(title, width, height, resizable);
}
SDLWindow::SDLWindow(std::string title, int width, int height, bool resizable)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
if (resizable) {
flags |= SDL_WINDOW_RESIZABLE;
}
mWindow = SDL_CreateWindow(
title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width,
height,
flags
);
mContext = SDL_GL_CreateContext(mWindow);
SDL_GL_MakeCurrent(mWindow, mContext);
SDL_GL_SetSwapInterval(0);
}
GLRenderer::GLRenderer()
{
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
glEnable(GL_DEPTH_TEST);
}