aboutsummaryrefslogtreecommitdiff
path: root/src/fb.c
blob: 1e3d601a782c9eb097fa0960a4ec201d91ea01a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#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_blit(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 true;
}

bool pw_fb_render(struct pw_framebuffer *fb) {
  if (!fb) {
    SDL_InvalidParamError("fb");
    return false;
  }

  SDL_Renderer *renderer = SDL_GetRendererFromTexture(fb->texture);
  if (!renderer) {
    return false;
  }

  return SDL_RenderClear(renderer) &&
         SDL_RenderTexture(renderer, fb->texture, NULL, &fb->target) &&
         SDL_RenderPresent(renderer);
}