Start using SDL for graphics syncing
Jeffrey Pfau jeffrey@endrift.com
Sat, 20 Apr 2013 20:29:53 -0700
4 files changed,
69 insertions(+),
2 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -10,5 +10,9 @@ include_directories(${CMAKE_SOURCE_DIR}/src/arm)
include_directories(${CMAKE_SOURCE_DIR}/src/gba) include_directories(${CMAKE_SOURCE_DIR}/src/debugger) include_directories(${CMAKE_SOURCE_DIR}/third-party/linenoise) + +find_package(SDL 1.2 REQUIRED) +include_directories(${SDL_INCLUDE_DIR}) + add_executable(gbac ${ARM_SRC} ${GBA_SRC} ${DEBUGGER_SRC} ${RENDERER_SRC} ${THIRD_PARTY} ${CMAKE_SOURCE_DIR}/src/main.c) -target_link_libraries(gbac m pthread) +target_link_libraries(gbac m pthread ${SDL_LIBRARY})
M
src/gba/gba-thread.c
→
src/gba/gba-thread.c
@@ -33,6 +33,7 @@ pthread_cond_broadcast(&threadContext->cond);
pthread_mutex_unlock(&threadContext->mutex); ARMDebuggerRun(&debugger); + threadContext->started = 0; GBADeinit(&gba); return 0;
M
src/gba/renderers/video-software.c
→
src/gba/renderers/video-software.c
@@ -14,10 +14,20 @@ renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister; renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline; renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame; + + { + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + renderer->mutex = mutex; + pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + renderer->cond = cond; + } } static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) { struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer; + + pthread_mutex_init(&softwareRenderer->mutex, 0); + pthread_cond_init(&softwareRenderer->cond, 0); } static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {@@ -35,4 +45,8 @@ }
static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) { struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer; + + pthread_mutex_lock(&softwareRenderer->mutex); + pthread_cond_wait(&softwareRenderer->cond, &softwareRenderer->mutex); + pthread_mutex_unlock(&softwareRenderer->mutex); }
M
src/main.c
→
src/main.c
@@ -1,11 +1,17 @@
+#include "debugger.h" #include "gba-thread.h" #include "renderers/video-software.h" + +#include <sdl.h> #include <fcntl.h> #include <errno.h> #include <signal.h> #include <unistd.h> +static int _GBASDLInit(void); +static void _GBASDLRunloop(struct GBAThread* context, struct GBAVideoSoftwareRenderer* renderer); + int main(int argc, char** argv) { int fd = open("test.rom", O_RDONLY);@@ -14,13 +20,55 @@ sigaddset(&signals, SIGINT);
sigaddset(&signals, SIGTRAP); pthread_sigmask(SIG_BLOCK, &signals, 0); + if (!_GBASDLInit()) { + return 1; + } + struct GBAThread context; struct GBAVideoSoftwareRenderer renderer; + GBAVideoSoftwareRendererCreate(&renderer); context.fd = fd; - context.renderer = 0; + context.renderer = &renderer.d; GBAThreadStart(&context); + + _GBASDLRunloop(&context, &renderer); + GBAThreadJoin(&context); close(fd); + SDL_Quit(); + return 0; } + +static int _GBASDLInit() { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + return 0; + } + + SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + SDL_SetVideoMode(240, 160, 16, SDL_OPENGL); + + return 1; +} + +static void _GBASDLRunloop(struct GBAThread* context, struct GBAVideoSoftwareRenderer* renderer) { + SDL_Event event; + + while (1) { + if (!context->started) { + break; + } + SDL_GL_SwapBuffers(); + pthread_mutex_lock(&renderer->mutex); + pthread_cond_broadcast(&renderer->cond); + pthread_mutex_unlock(&renderer->mutex); + while(SDL_PollEvent(&event)) { + + } + } +}