aboutsummaryrefslogtreecommitdiff
path: root/src/fb.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fb.c')
-rw-r--r--src/fb.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/fb.c b/src/fb.c
new file mode 100644
index 0000000..d0515d9
--- /dev/null
+++ b/src/fb.c
@@ -0,0 +1,141 @@
+#include "fb.h"
+
+struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer) {
+ if (!renderer) {
+ SDL_InvalidParamError("renderer");
+ return false;
+ }
+
+ SDL_Texture *texture = NULL;
+ struct pw_framebuffer *fb = NULL;
+
+ // Note that we use a different pixel format here, this is to ensure we match
+ // the PICO-8 engine whose format is endian-dependant.
+ texture =
+ SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
+ SDL_TEXTUREACCESS_STREAMING, PW_FB_WIDTH, PW_FB_HEIGHT);
+ if (!texture || !SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST)) {
+ goto error;
+ }
+
+ fb = SDL_malloc(sizeof(*fb));
+ if (!fb) {
+ goto error;
+ }
+
+ fb->texture = texture;
+ SDL_zero(fb->target);
+ if (!pw_fb_iterate(fb)) {
+ goto error;
+ }
+
+ return fb;
+
+error:
+ if (!texture) {
+ SDL_DestroyTexture(texture);
+ }
+ if (!fb) {
+ SDL_free(fb);
+ }
+ return NULL;
+}
+
+void pw_fb_free(struct pw_framebuffer *fb) {
+ if (!fb) {
+ SDL_InvalidParamError("fb");
+ return;
+ }
+
+ SDL_DestroyTexture(fb->texture);
+ SDL_free(fb);
+}
+
+bool pw_fb_iterate(struct pw_framebuffer *fb) {
+ if (!fb) {
+ SDL_InvalidParamError("fb");
+ return false;
+ }
+
+ SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
+ if (!renderer) {
+ return false;
+ }
+
+ int rw, rh;
+ if (!SDL_GetRenderOutputSize(renderer, &rw, &rh)) {
+ return false;
+ }
+
+ int scale = SDL_min(rw / PW_FB_WIDTH, rh / PW_FB_HEIGHT);
+ if (!scale) {
+ return false;
+ }
+
+ int w = scale * PW_FB_WIDTH;
+ int h = scale * PW_FB_HEIGHT;
+ int x = (rw - w) / 2;
+ int y = (rh - h) / 2;
+
+ fb->target.w = (float)w;
+ fb->target.h = (float)h;
+ fb->target.x = (float)x;
+ fb->target.y = (float)y;
+ return true;
+}
+
+static uint8_t project_component(float r, float start, float size,
+ uint8_t dim) {
+ float dimf = (float)dim;
+ float df = SDL_roundf(((r - start) * dimf) / size);
+ return (uint8_t)SDL_clamp(df, 0.0, (float)(dim - 1));
+}
+
+bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
+ struct pw_coordinates *coords) {
+ if (!fb) {
+ SDL_InvalidParamError("fb");
+ return false;
+ }
+
+ SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
+ if (!renderer) {
+ return false;
+ }
+
+ float rx, ry;
+ if (!SDL_RenderCoordinatesFromWindow(renderer, wx, wy, &rx, &ry)) {
+ return false;
+ }
+
+ coords->x = project_component(rx, fb->target.x, fb->target.w, PW_FB_WIDTH);
+ coords->y = project_component(ry, fb->target.y, fb->target.h, PW_FB_HEIGHT);
+ return true;
+}
+
+bool pw_fb_flip(struct pw_framebuffer *fb,
+ uint32_t const pixels[PW_FB_WIDTH * PW_FB_HEIGHT]) {
+ if (!fb) {
+ SDL_InvalidParamError("fb");
+ return false;
+ }
+
+ SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
+ if (!renderer) {
+ return false;
+ }
+
+ void *buffer;
+ int pitch;
+ if (!SDL_LockTexture(fb->texture, NULL, &buffer, &pitch) ||
+ pitch != sizeof(uint32_t[PW_FB_WIDTH])) {
+ return false;
+ }
+
+ SDL_memcpy(buffer, pixels, sizeof(uint32_t[PW_FB_WIDTH * PW_FB_HEIGHT]));
+ SDL_UnlockTexture(fb->texture);
+
+ return SDL_RenderClear(renderer) &&
+ SDL_RenderTexture(renderer, fb->texture, NULL, &fb->target) &&
+ SDL_RenderPresent(renderer);
+}