Render byte buffers from the application
This commit is contained in:
parent
ce01974243
commit
a4b81c2603
|
@ -0,0 +1,12 @@
|
|||
#ifndef SHROOMS_VB_NATIVE_ASSETS_
|
||||
#define SHROOMS_VB_NATIVE_ASSETS_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern const uint8_t asset_lefteye_start;
|
||||
const uint8_t *LEFT_EYE_DEFAULT = &asset_lefteye_start;
|
||||
|
||||
extern const uint8_t asset_righteye_start;
|
||||
const uint8_t *RIGHT_EYE_DEFAULT = &asset_righteye_start;
|
||||
|
||||
#endif
|
|
@ -0,0 +1,15 @@
|
|||
.section .note.GNU-stack,"",@progbits
|
||||
.section .bindata, "a", @progbits
|
||||
|
||||
.global asset_lefteye_start
|
||||
.type asset_lefteye_start, @object
|
||||
.global asset_righteye_start
|
||||
.type asset_righteye_start, @object
|
||||
|
||||
.section .bindata
|
||||
.balign 64
|
||||
asset_lefteye_start:
|
||||
.incbin "assets/lefteye.bin"
|
||||
.balign 64
|
||||
asset_righteye_start:
|
||||
.incbin "assets/righteye.bin"
|
Binary file not shown.
Binary file not shown.
9
game.c
9
game.c
|
@ -1,3 +1,4 @@
|
|||
#include <assets.h>
|
||||
#include <game.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <time.h>
|
||||
|
@ -11,13 +12,17 @@ int sleepNanos(long int ns) {
|
|||
|
||||
#define MAX_STEP_CLOCKS 20000
|
||||
|
||||
int runGame(VB *sim) {
|
||||
int runGame(VB *sim, GraphicsContext *gfx) {
|
||||
uint32_t clocks;
|
||||
SDL_Event event;
|
||||
|
||||
gfxUpdateLeftEye(gfx, LEFT_EYE_DEFAULT);
|
||||
gfxUpdateRightEye(gfx, RIGHT_EYE_DEFAULT);
|
||||
|
||||
while (1) {
|
||||
clocks = MAX_STEP_CLOCKS;
|
||||
vbEmulate(sim, &clocks);
|
||||
gfxRender(gfx);
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
return 0;
|
||||
|
@ -25,4 +30,4 @@ int runGame(VB *sim) {
|
|||
}
|
||||
sleepNanos((MAX_STEP_CLOCKS - clocks) * 50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
3
game.h
3
game.h
|
@ -1,8 +1,9 @@
|
|||
#ifndef SHROOMS_VB_NATIVE_GAME_
|
||||
#define SHROOMS_VB_NATIVE_GAME_
|
||||
|
||||
#include "graphics.h"
|
||||
#include "shrooms-vb-core/core/vb.h"
|
||||
|
||||
int runGame(VB *sim);
|
||||
int runGame(VB *sim, GraphicsContext *gfx);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,92 @@
|
|||
#include <graphics.h>
|
||||
|
||||
static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) {
|
||||
int x, y, i;
|
||||
uint8_t color;
|
||||
int delta = pitch / 384;
|
||||
for (y = 0; y < 224; ++y) {
|
||||
for (x = 0; x < 384; x += 1) {
|
||||
color = src[(y * 384) + x];
|
||||
for (i = 0; i < delta; ++i) {
|
||||
dst[(y * pitch) + (x * delta) + i] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int gfxInit(GraphicsContext *gfx) {
|
||||
gfx->window = SDL_CreateWindow("Shrooms VB",
|
||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
1536, 896, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
if (!gfx->window) {
|
||||
fprintf(stderr, "Error creating window: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
gfx->winSurface = SDL_GetWindowSurface(gfx->window);
|
||||
if (!gfx->winSurface) {
|
||||
fprintf(stderr, "Error getting surface: %s\n", SDL_GetError());
|
||||
goto cleanup_window;
|
||||
}
|
||||
|
||||
gfx->renderer = SDL_GetRenderer(gfx->window);
|
||||
if (!gfx->renderer) {
|
||||
fprintf(stderr, "Error getting renderer: %s\n", SDL_GetError());
|
||||
goto cleanup_window;
|
||||
}
|
||||
|
||||
gfx->leftEye = SDL_CreateTexture(gfx->renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, 384, 224);
|
||||
if (!gfx->leftEye) {
|
||||
fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError());
|
||||
goto cleanup_window;
|
||||
}
|
||||
SDL_SetTextureColorMod(gfx->leftEye, 255, 0, 0);
|
||||
|
||||
gfx->rightEye = SDL_CreateTexture(gfx->renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, 384, 224);
|
||||
if (!gfx->rightEye) {
|
||||
fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError());
|
||||
goto cleanup_left_eye;
|
||||
}
|
||||
SDL_SetTextureColorMod(gfx->rightEye, 0, 0, 255);
|
||||
SDL_SetTextureBlendMode(gfx->rightEye, SDL_BLENDMODE_ADD);
|
||||
|
||||
return 0;
|
||||
|
||||
cleanup_left_eye:
|
||||
SDL_DestroyTexture(gfx->leftEye);
|
||||
cleanup_window:
|
||||
SDL_DestroyWindow(gfx->window);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void gfxDestroy(GraphicsContext *gfx) {
|
||||
SDL_DestroyTexture(gfx->rightEye);
|
||||
SDL_DestroyTexture(gfx->leftEye);
|
||||
SDL_DestroyWindow(gfx->window);
|
||||
}
|
||||
|
||||
static void gfxUpdateEye(SDL_Texture *eye, const uint8_t *bytes) {
|
||||
void *target;
|
||||
int pitch;
|
||||
if (SDL_LockTexture(eye, NULL, &target, &pitch)) {
|
||||
fprintf(stderr, "Error locking buffer for eye: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
copyScreenTexture(target, bytes, pitch);
|
||||
SDL_UnlockTexture(eye);
|
||||
}
|
||||
|
||||
void gfxUpdateLeftEye(GraphicsContext *gfx, const uint8_t *bytes) {
|
||||
gfxUpdateEye(gfx->leftEye, bytes);
|
||||
}
|
||||
void gfxUpdateRightEye(GraphicsContext *gfx, const uint8_t *bytes) {
|
||||
gfxUpdateEye(gfx->rightEye, bytes);
|
||||
}
|
||||
|
||||
void gfxRender(GraphicsContext *gfx) {
|
||||
SDL_RenderClear(gfx->renderer);
|
||||
SDL_RenderCopy(gfx->renderer, gfx->leftEye, NULL, NULL);
|
||||
SDL_RenderCopy(gfx->renderer, gfx->rightEye, NULL, NULL);
|
||||
SDL_RenderPresent(gfx->renderer);
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef SHROOMS_VB_NATIVE_GRAPHICS_
|
||||
#define SHROOMS_VB_NATIVE_GRAPHICS_
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
typedef struct {
|
||||
SDL_Window *window;
|
||||
SDL_Surface *winSurface;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Texture *leftEye;
|
||||
SDL_Texture *rightEye;
|
||||
} GraphicsContext;
|
||||
|
||||
int gfxInit(GraphicsContext *gfx);
|
||||
void gfxDestroy(GraphicsContext *gfx);
|
||||
|
||||
void gfxUpdateLeftEye(GraphicsContext *gfx, const uint8_t *bytes);
|
||||
void gfxUpdateRightEye(GraphicsContext *gfx, const uint8_t *bytes);
|
||||
|
||||
void gfxRender(GraphicsContext *gfx);
|
||||
|
||||
#endif
|
25
main.c
25
main.c
|
@ -1,5 +1,6 @@
|
|||
#include <cli.h>
|
||||
#include <game.h>
|
||||
#include <graphics.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include "shrooms-vb-core/core/vb.h"
|
||||
#include <stdio.h>
|
||||
|
@ -51,8 +52,7 @@ int main(int argc, char **argv) {
|
|||
VB *sim;
|
||||
uint8_t *rom;
|
||||
uint32_t romSize;
|
||||
SDL_Surface *winSurface;
|
||||
SDL_Window *window;
|
||||
GraphicsContext gfx;
|
||||
CLIArgs args;
|
||||
int status;
|
||||
|
||||
|
@ -74,27 +74,12 @@ int main(int argc, char **argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow("Shrooms VB",
|
||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
384, 224, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Error creating window: %s\n", SDL_GetError());
|
||||
if (gfxInit(&gfx)) {
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
winSurface = SDL_GetWindowSurface(window);
|
||||
if (!winSurface) {
|
||||
fprintf(stderr, "Error getting surface: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_FillRect(winSurface, NULL, SDL_MapRGB(winSurface->format, 255, 0, 0));
|
||||
SDL_UpdateWindowSurface(window);
|
||||
|
||||
status = runGame(sim);
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
status = runGame(sim, &gfx);
|
||||
SDL_Quit();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue