aboutsummaryrefslogtreecommitdiff
path: root/src/fb.h
blob: e936c7210a2d26fced9226ad5f8bb621218fdfc0 (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
#ifndef PW_FB_H
#define PW_FB_H

#include <SDL3/SDL_render.h>

#include "scale.h"

#define PW_FB_WIDTH 128
#define PW_FB_HEIGHT 128
#define PW_FB_BYTES_PER_PIXEL 4
#define PW_FB_STRIDE (PW_FB_WIDTH * PW_FB_BYTES_PER_PIXEL)
#define PW_FB_SIZE (PW_FB_STRIDE * PW_FB_HEIGHT)

/**
 * A PICO-8 framebuffer.
 */
struct pw_framebuffer {
  enum pw_scale_mode mode;
  SDL_Texture *texture;
  SDL_FRect target;
};

/**
 * Framebuffer coordinates.
 */
struct pw_coordinates {
  float x;
  float y;
};

/**
 * Allocates and initializes the PICO-8 framebuffer.
 */
struct pw_framebuffer *pw_fb_init(SDL_Renderer *renderer,
                                  enum pw_scale_mode mode);

/**
 * Cleanup and free the PICO-8 framebuffer.
 */
void pw_fb_free(struct pw_framebuffer *fb);

/**
 * Handles SDL events to resize the framebuffer.
 */
bool pw_fb_iterate(struct pw_framebuffer *fb);

/**
 * Projects window coordinates to framebuffer coordinates.
 *
 * The resulting coordinate components are guaranteed to fit in a 32-bit
 * integer, meaning it is safe to cast to `int32_t`.
 */
bool pw_fb_coordinates(struct pw_framebuffer *fb, float wx, float wy,
                       bool relative, struct pw_coordinates *coords);

/**
 * Locks the framebuffer for writing.
 */
uint8_t *pw_fb_lock(struct pw_framebuffer *fb, size_t *pitch);

/**
 * Unlocks the framebuffer for writing.
 */
void pw_fb_unlock(struct pw_framebuffer *fb);

/**
 * Renders the the framebuffer.
 */
bool pw_fb_render(struct pw_framebuffer *fb);

#endif