Start using SDL for graphics syncing

This commit is contained in:
Jeffrey Pfau 2013-04-20 20:29:53 -07:00
parent a094024ad5
commit eba5d547b9
4 changed files with 69 additions and 2 deletions

View File

@ -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})

View File

@ -33,6 +33,7 @@ static void* _GBAThreadRun(void* context) {
pthread_mutex_unlock(&threadContext->mutex);
ARMDebuggerRun(&debugger);
threadContext->started = 0;
GBADeinit(&gba);
return 0;

View File

@ -14,10 +14,20 @@ void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
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 GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* render
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);
}

View File

@ -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 @@ int main(int argc, char** argv) {
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)) {
}
}
}