summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/demo.c
blob: 85ee2a38c939823f93df2494f420e7c6e905a70a (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
#include <SDL2/SDL.h>
#include "demo.h"

void update_demo(struct demo* demo) {
  int width, height;
  int pixel_width, pixel_height;

  SDL_GetWindowSize(demo->win, &width, &height);
  SDL_GetRendererOutputSize(demo->ren, &pixel_width, &pixel_height);

  demo->display_scale_x = (float)pixel_width / width;
  demo->display_scale_y = (float)pixel_height / height;
}

void demo_rendercopy(struct demo* demo,
                     SDL_Texture* texture,
                     const SDL_Rect* s_rect,
                     const SDL_Rect* d_rect) {
  const float scale_x = demo->display_scale_x;
  const float scale_y = demo->display_scale_y;

  SDL_Rect new_srect;
  SDL_Rect new_drect;

  if(s_rect) {
    new_srect = (SDL_Rect){
      .x = s_rect->x * scale_x,
      .y = s_rect->y * scale_y,
      .w = s_rect->w * scale_x,
      .h = s_rect->h * scale_y
    };
  }

  if(d_rect) {
    new_drect = (SDL_Rect){
      .x = d_rect->x * scale_x,
      .y = d_rect->y * scale_y,
      .w = d_rect->w * scale_x,
      .h = d_rect->h * scale_y
    };
  }

  //Handle edge case where s_rect or d_rect are NULL
  SDL_RenderCopy(demo->ren, texture,
                 s_rect ? &new_srect : NULL,
                 d_rect ? &new_drect : NULL);
}

void demo_rendercopy_ex(struct demo* demo,
                        SDL_Texture* texture,
                        const SDL_Rect* s_rect,
                        const SDL_Rect* d_rect,
                        const double angle,
                        const SDL_Point* center,
                        const SDL_RendererFlip flip) {
  const float scale_x = demo->display_scale_x;
  const float scale_y = demo->display_scale_y;

  SDL_Rect new_srect;
  SDL_Rect new_drect;

  if(s_rect) {
    new_srect = (SDL_Rect){
      .x = s_rect->x * scale_x,
      .y = s_rect->y * scale_y,
      .w = s_rect->w * scale_x,
      .h = s_rect->h * scale_y
    };
  }

  if(d_rect) {
    new_drect = (SDL_Rect){
      .x = d_rect->x * scale_x,
      .y = d_rect->y * scale_y,
      .w = d_rect->w * scale_x,
      .h = d_rect->h * scale_y
    };
  }

  //Handle edge case where s_rect or d_rect are NULL
  SDL_RenderCopyEx(demo->ren, texture,
                   s_rect ? &new_srect : NULL,
                   d_rect ? &new_drect : NULL,
                   angle,
                   center,
                   flip);
}