aboutsummaryrefslogtreecommitdiff
path: root/src/scale.c
blob: 637db86a5ceefe6273f0bb9c671f811fd8ef8eb3 (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
#include "scale.h"

static bool is_safei(int i) {
  return i >= -PW_MAX_SAFE_INT && i <= PW_MAX_SAFE_INT;
}

static bool is_safef(float f) {
  return f >= -PW_MAX_SAFE_INT && f <= PW_MAX_SAFE_INT && (int32_t)f != 0;
}

static SDL_ScaleMode scale_pixel_perfect(float twf, float thf, int rw, int rh,
                                         SDL_FRect *target) {
  int32_t tw = (int32_t)twf;
  int32_t th = (int32_t)thf;
  int32_t scale = SDL_min(rw / tw, rh / th);

  int32_t w = scale * tw;
  int32_t h = scale * th;
  int32_t x = (rw - w) / 2;
  int32_t y = (rh - h) / 2;

  target->w = (float)w;
  target->h = (float)h;
  target->x = (float)x;
  target->y = (float)y;

  return SDL_SCALEMODE_NEAREST;
}

static SDL_ScaleMode scale_fill(float twf, float thf, int rw, int rh,
                                SDL_FRect *target) {
  float rwf = (float)rw;
  float rhf = (float)rh;
  float scale = SDL_min(rwf / twf, rhf / thf);

  float w = scale * twf;
  float h = scale * thf;
  float x = (rw - w) / 2.;
  float y = (rh - h) / 2.;

  target->w = w;
  target->h = h;
  target->x = x;
  target->y = y;

  return SDL_SCALEMODE_PIXELART;
}

static SDL_ScaleMode scale_stretch(float twf, float thf, int rw, int rh,
                                   SDL_FRect *target) {
  (void)twf;
  (void)thf;

  target->w = rw;
  target->h = rh;
  target->x = 0.;
  target->y = 0.;

  return SDL_SCALEMODE_PIXELART;
}

bool pw_scale(enum pw_scale_mode mode, SDL_Texture *texture,
              SDL_FRect *target) {
  SDL_ScaleMode (*scalefn)(float, float, int, int, SDL_FRect *);
  switch (mode) {
  case PW_SCALE_MODE_PIXEL_PERFECT:
    scalefn = scale_pixel_perfect;
    break;
  case PW_SCALE_MODE_FILL:
    scalefn = scale_fill;
    break;
  case PW_SCALE_MODE_STRETCH:
    scalefn = scale_stretch;
    break;
  default:
    SDL_InvalidParamError("mode");
    return false;
  }
  if (!texture) {
    SDL_InvalidParamError("texture");
    return false;
  }
  if (!target) {
    SDL_InvalidParamError("target");
    return false;
  }

  float twf, thf;
  if (!SDL_GetTextureSize(texture, &twf, &thf)) {
    return false;
  }

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

  int rw, rh;
  if (!SDL_GetRenderOutputSize(renderer, &rw, &rh)) {
    return false;
  }

  if (!is_safef(twf) || !is_safef(thf) || !is_safei(rw) || !is_safei(rh)) {
    return SDL_SetError("unreasonable texture or render sizes");
  }

  SDL_FRect t;
  SDL_ScaleMode m = scalefn(twf, thf, rw, rh, &t);
  if (!SDL_SetTextureScaleMode(texture, m)) {
    return false;
  }

  SDL_memcpy(target, &t, sizeof(*target));
  return true;
}