src/gba/renderers/video-software.c (view raw)
1#include "video-software.h"
2
3#include "gba.h"
4
5static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer);
6static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer);
7static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
8static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
9static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer);
10
11void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) {
12 renderer->d.init = GBAVideoSoftwareRendererInit;
13 renderer->d.deinit = GBAVideoSoftwareRendererDeinit;
14 renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister;
15 renderer->d.drawScanline = GBAVideoSoftwareRendererDrawScanline;
16 renderer->d.finishFrame = GBAVideoSoftwareRendererFinishFrame;
17
18 {
19 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
20 renderer->mutex = mutex;
21 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
22 renderer->cond = cond;
23 }
24}
25
26static void GBAVideoSoftwareRendererInit(struct GBAVideoRenderer* renderer) {
27 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
28
29 pthread_mutex_init(&softwareRenderer->mutex, 0);
30 pthread_cond_init(&softwareRenderer->cond, 0);
31}
32
33static void GBAVideoSoftwareRendererDeinit(struct GBAVideoRenderer* renderer) {
34 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
35}
36
37static uint16_t GBAVideoSoftwareRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
38 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
39 return value;
40}
41
42static void GBAVideoSoftwareRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
43 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
44 int x;
45 uint16_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
46 for (x = 0; x < 16; ++x) {
47 row[(x * 15) + 0] = renderer->palette[x + (y / 5) * 16];
48 row[(x * 15) + 1] = renderer->palette[x + (y / 5) * 16];
49 row[(x * 15) + 2] = renderer->palette[x + (y / 5) * 16];
50 row[(x * 15) + 3] = renderer->palette[x + (y / 5) * 16];
51 row[(x * 15) + 4] = renderer->palette[x + (y / 5) * 16];
52 row[(x * 15) + 5] = renderer->palette[x + (y / 5) * 16];
53 row[(x * 15) + 6] = renderer->palette[x + (y / 5) * 16];
54 row[(x * 15) + 7] = renderer->palette[x + (y / 5) * 16];
55 row[(x * 15) + 8] = renderer->palette[x + (y / 5) * 16];
56 row[(x * 15) + 9] = renderer->palette[x + (y / 5) * 16];
57 row[(x * 15) + 10] = renderer->palette[x + (y / 5) * 16];
58 row[(x * 15) + 11] = renderer->palette[x + (y / 5) * 16];
59 row[(x * 15) + 12] = renderer->palette[x + (y / 5) * 16];
60 row[(x * 15) + 13] = renderer->palette[x + (y / 5) * 16];
61 row[(x * 15) + 14] = renderer->palette[x + (y / 5) * 16];
62 }
63}
64
65static void GBAVideoSoftwareRendererFinishFrame(struct GBAVideoRenderer* renderer) {
66 struct GBAVideoSoftwareRenderer* softwareRenderer = (struct GBAVideoSoftwareRenderer*) renderer;
67
68 pthread_mutex_lock(&softwareRenderer->mutex);
69 pthread_cond_wait(&softwareRenderer->cond, &softwareRenderer->mutex);
70 pthread_mutex_unlock(&softwareRenderer->mutex);
71}