From df497cf336fb542a49b2a0b45c3262f175c53c50 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Wed, 28 Nov 2018 18:01:20 +0000 Subject: [PATCH] SDL: Split sw-sdl.c into separate files --- src/platform/sdl/CMakeLists.txt | 6 +- src/platform/sdl/{sw-sdl.c => sw-sdl1.c} | 35 ------------ src/platform/sdl/sw-sdl2.c | 72 ++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 36 deletions(-) rename src/platform/sdl/{sw-sdl.c => sw-sdl1.c} (68%) create mode 100644 src/platform/sdl/sw-sdl2.c diff --git a/src/platform/sdl/CMakeLists.txt b/src/platform/sdl/CMakeLists.txt index 0c2fc771b..bed74eea8 100644 --- a/src/platform/sdl/CMakeLists.txt +++ b/src/platform/sdl/CMakeLists.txt @@ -85,7 +85,11 @@ else() include_directories(${OPENGLES2_INCLUDE_DIR}) endif() if(NOT BUILD_GL AND NOT BUILD_GLES2) - list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl.c) + if(SDL_VERSION EQUAL "2") + list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl2.c) + else() + list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl1.c) + endif() endif() endif() diff --git a/src/platform/sdl/sw-sdl.c b/src/platform/sdl/sw-sdl1.c similarity index 68% rename from src/platform/sdl/sw-sdl.c rename to src/platform/sdl/sw-sdl1.c index 2451eb2c6..374d11bdf 100644 --- a/src/platform/sdl/sw-sdl.c +++ b/src/platform/sdl/sw-sdl1.c @@ -21,35 +21,14 @@ void mSDLSWCreate(struct mSDLRenderer* renderer) { } bool mSDLSWInit(struct mSDLRenderer* renderer) { -#if !SDL_VERSION_ATLEAST(2, 0, 0) #ifdef COLOR_16_BIT SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_DOUBLEBUF | SDL_HWSURFACE); #else SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_DOUBLEBUF | SDL_HWSURFACE); -#endif #endif unsigned width, height; renderer->core->desiredVideoDimensions(renderer->core, &width, &height); -#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_FULLSCREEN_DESKTOP * renderer->player.fullscreen)); - SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight); - renderer->player.window = renderer->window; - renderer->sdlRenderer = SDL_CreateRenderer(renderer->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); -#ifdef COLOR_16_BIT -#ifdef COLOR_5_6_5 - renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, width, height); -#else - renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR1555, SDL_TEXTUREACCESS_STREAMING, width, height); -#endif -#else - renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, width, height); -#endif - - int stride; - SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->outputBuffer, &stride); - renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, stride / BYTES_PER_PIXEL); -#else SDL_Surface* surface = SDL_GetVideoSurface(); SDL_LockSurface(surface); @@ -81,7 +60,6 @@ bool mSDLSWInit(struct mSDLRenderer* renderer) { return false; #endif } -#endif return true; } @@ -89,9 +67,7 @@ bool mSDLSWInit(struct mSDLRenderer* renderer) { void mSDLSWRunloop(struct mSDLRenderer* renderer, void* user) { struct mCoreThread* context = user; SDL_Event event; -#if !SDL_VERSION_ATLEAST(2, 0, 0) SDL_Surface* surface = SDL_GetVideoSurface(); -#endif while (mCoreThreadIsActive(context)) { while (SDL_PollEvent(&event)) { @@ -99,14 +75,6 @@ void mSDLSWRunloop(struct mSDLRenderer* renderer, void* user) { } if (mCoreSyncWaitFrameStart(&context->impl->sync)) { -#if SDL_VERSION_ATLEAST(2, 0, 0) - SDL_UnlockTexture(renderer->sdlTex); - SDL_RenderCopy(renderer->sdlRenderer, renderer->sdlTex, 0, 0); - SDL_RenderPresent(renderer->sdlRenderer); - int stride; - SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->outputBuffer, &stride); - renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, stride / BYTES_PER_PIXEL); -#else #ifdef USE_PIXMAN if (renderer->ratio > 1) { pixman_image_composite32(PIXMAN_OP_SRC, renderer->pix, 0, renderer->screenpix, @@ -132,7 +100,6 @@ void mSDLSWRunloop(struct mSDLRenderer* renderer, void* user) { SDL_UnlockSurface(surface); SDL_Flip(surface); SDL_LockSurface(surface); -#endif } mCoreSyncWaitFrameEnd(&context->impl->sync); } @@ -142,12 +109,10 @@ void mSDLSWDeinit(struct mSDLRenderer* renderer) { if (renderer->ratio > 1) { free(renderer->outputBuffer); } -#if !SDL_VERSION_ATLEAST(2, 0, 0) SDL_Surface* surface = SDL_GetVideoSurface(); SDL_UnlockSurface(surface); #ifdef USE_PIXMAN pixman_image_unref(renderer->pix); pixman_image_unref(renderer->screenpix); #endif -#endif } diff --git a/src/platform/sdl/sw-sdl2.c b/src/platform/sdl/sw-sdl2.c new file mode 100644 index 000000000..fb5a3f0b9 --- /dev/null +++ b/src/platform/sdl/sw-sdl2.c @@ -0,0 +1,72 @@ +/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * 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 +#include +#include +#include + +static bool mSDLSWInit(struct mSDLRenderer* renderer); +static void mSDLSWRunloop(struct mSDLRenderer* renderer, void* user); +static void mSDLSWDeinit(struct mSDLRenderer* renderer); + +void mSDLSWCreate(struct mSDLRenderer* renderer) { + renderer->init = mSDLSWInit; + renderer->deinit = mSDLSWDeinit; + renderer->runloop = mSDLSWRunloop; +} + +bool mSDLSWInit(struct mSDLRenderer* renderer) { + unsigned width, height; + renderer->core->desiredVideoDimensions(renderer->core, &width, &height); + renderer->window = SDL_CreateWindow(projectName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->player.fullscreen)); + SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight); + renderer->player.window = renderer->window; + renderer->sdlRenderer = SDL_CreateRenderer(renderer->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); +#ifdef COLOR_16_BIT +#ifdef COLOR_5_6_5 + renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, width, height); +#else + renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR1555, SDL_TEXTUREACCESS_STREAMING, width, height); +#endif +#else + renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, width, height); +#endif + + int stride; + SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->outputBuffer, &stride); + renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, stride / BYTES_PER_PIXEL); + + return true; +} + +void mSDLSWRunloop(struct mSDLRenderer* renderer, void* user) { + struct mCoreThread* context = user; + SDL_Event event; + + while (mCoreThreadIsActive(context)) { + while (SDL_PollEvent(&event)) { + mSDLHandleEvent(context, &renderer->player, &event); + } + + if (mCoreSyncWaitFrameStart(&context->impl->sync)) { + SDL_UnlockTexture(renderer->sdlTex); + SDL_RenderCopy(renderer->sdlRenderer, renderer->sdlTex, 0, 0); + SDL_RenderPresent(renderer->sdlRenderer); + int stride; + SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->outputBuffer, &stride); + renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, stride / BYTES_PER_PIXEL); + } + mCoreSyncWaitFrameEnd(&context->impl->sync); + } +} + +void mSDLSWDeinit(struct mSDLRenderer* renderer) { + if (renderer->ratio > 1) { + free(renderer->outputBuffer); + } +}