SDL: Use some common GL functionality between GL and GLESv2
Jeffrey Pfau jeffrey@endrift.com
Fri, 31 Jul 2015 16:11:49 -0700
3 files changed,
27 insertions(+),
47 deletions(-)
M
src/platform/sdl/CMakeLists.txt
→
src/platform/sdl/CMakeLists.txt
@@ -52,7 +52,7 @@
if(BUILD_RASPI) add_definitions(-DBUILD_RASPI) list(APPEND PLATFORM_SRC ${CMAKE_SOURCE_DIR}/src/platform/opengl/gles2.c) - list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/egl-sdl.c) + list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/gles2-sdl.c) set(OPENGLES2_LIBRARY "-lEGL -lGLESv2 -lbcm_host") set(BUILD_GLES2 ON CACHE BOOL "Using OpenGL|ES 2" FORCE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fgnu89-inline")@@ -68,8 +68,13 @@ else()
list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl.c) if(BUILD_GL) list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/gl-sdl.c) - list(APPEND PLATFORM_SRC ${CMAKE_SOURCE_DIR}/src/platform/opengl/gl.c) - include_directories(${OPENGL_INCLUDE_DIR} ${OPENGLES2_INCLUDE_DIR}) + list(APPEND PLATFORM_SRC ${CMAKE_SOURCE_DIR}/src/platform/opengl/gl.c ${CMAKE_SOURCE_DIR}/src/platform/sdl/gl-common.c) + include_directories(${OPENGL_INCLUDE_DIR}) + endif() + if(BUILD_GLES2) + list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/gles2-sdl.c) + list(APPEND PLATFORM_SRC ${CMAKE_SOURCE_DIR}/src/platform/opengl/gles2.c ${CMAKE_SOURCE_DIR}/src/platform/sdl/gl-common.c) + include_directories(${OPENGLES2_INCLUDE_DIR}) endif() endif()
M
src/platform/sdl/egl-sdl.c
→
src/platform/sdl/egl-sdl.c
@@ -5,10 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "main.h" -static void _sdlSwap(struct VideoBackend* context) { - UNUSED(context); - SDL_GL_SwapBuffers(); -} +#include "gl-common.h" static bool GBASDLGLES2Init(struct SDLSoftwareRenderer* renderer); static void GBASDLGLES2Runloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer);@@ -21,6 +18,7 @@ renderer->runloop = GBASDLGLES2Runloop;
} bool GBASDLGLES2Init(struct SDLSoftwareRenderer* renderer) { +#ifdef BUILD_RASPI bcm_host_init(); renderer->display = eglGetDisplay(EGL_DEFAULT_DISPLAY); int major, minor;@@ -89,6 +87,9 @@ renderer->surface = eglCreateWindowSurface(renderer->display, config, &renderer->window, 0);
if (EGL_FALSE == eglMakeCurrent(renderer->display, renderer->surface, renderer->surface, renderer->context)) { return false; } +#else + GBASDLGLCommonInit(renderer); +#endif renderer->d.outputBuffer = memalign(16, 256 * 256 * 4); renderer->d.outputBufferStride = 256;@@ -97,7 +98,7 @@ GBAGLES2ContextCreate(&renderer->gl);
renderer->gl.d.user = renderer; renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio; renderer->gl.d.filter = renderer->filter; - renderer->gl.d.swap = _sdlSwap; + renderer->gl.d.swap = GBASDLGLCommonSwap; renderer->gl.d.init(&renderer->gl.d, 0); return true; }@@ -116,7 +117,11 @@ v->postFrame(v, renderer->d.outputBuffer);
} v->drawFrame(v); GBASyncWaitFrameEnd(&context->sync); +#ifdef BUILD_RASPI eglSwapBuffers(renderer->display, renderer->surface); +#else + v->swap(v); +#endif } }@@ -124,10 +129,14 @@ void GBASDLGLES2Deinit(struct SDLSoftwareRenderer* renderer) {
if (renderer->gl.d.deinit) { renderer->gl.d.deinit(&renderer->gl.d); } +#ifdef BUILD_RASPI eglMakeCurrent(renderer->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglDestroySurface(renderer->display, renderer->surface); eglDestroyContext(renderer->display, renderer->context); eglTerminate(renderer->display); + bcm_host_deinit(); +#elif SDL_VERSION_ATLEAST(2, 0, 0) + SDL_GL_DeleteContext(renderer->glCtx); +#endif free(renderer->d.outputBuffer); - bcm_host_deinit(); }
M
src/platform/sdl/gl-sdl.c
→
src/platform/sdl/gl-sdl.c
@@ -5,17 +5,10 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "main.h" +#include "gl-common.h" + #include "gba/supervisor/thread.h" #include "platform/opengl/gl.h" - -static void _sdlSwap(struct VideoBackend* context) { - struct SDLSoftwareRenderer* renderer = (struct SDLSoftwareRenderer*) context->user; -#if SDL_VERSION_ATLEAST(2, 0, 0) - SDL_GL_SwapWindow(renderer->window); -#else - SDL_GL_SwapBuffers(); -#endif -} static void _doViewport(int w, int h, struct VideoBackend* v) { v->resized(v, w, h);@@ -35,34 +28,7 @@ renderer->runloop = GBASDLGLRunloop;
} bool GBASDLGLInit(struct SDLSoftwareRenderer* renderer) { -#ifndef COLOR_16_BIT - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); -#else - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); -#ifdef COLOR_5_6_5 - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); -#else - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); -#endif - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); -#endif - -#if SDL_VERSION_ATLEAST(2, 0, 0) - renderer->window = SDL_CreateWindow(projectName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->player.fullscreen)); - renderer->glCtx = SDL_GL_CreateContext(renderer->window); - SDL_GL_SetSwapInterval(1); - SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight); - renderer->player.window = renderer->window; -#else - SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); -#ifdef COLOR_16_BIT - SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL); -#else - SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL); -#endif -#endif + GBASDLGLCommonInit(renderer); renderer->d.outputBuffer = malloc(256 * 256 * BYTES_PER_PIXEL); renderer->d.outputBufferStride = 256;@@ -71,7 +37,7 @@ GBAGLContextCreate(&renderer->gl);
renderer->gl.d.user = renderer; renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio; renderer->gl.d.filter = renderer->filter; - renderer->gl.d.swap = _sdlSwap; + renderer->gl.d.swap = GBASDLGLCommonSwap; renderer->gl.d.init(&renderer->gl.d, 0); _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);