so://phie

freezo

A retro platform game
git clone git://soophie.de/freezo

blob: b8deb3e56de6d6b66ebbccb165befa7de78822c0 (2864 bytes)[doc][blame][raw]

1#include <stdlib.h>

2#include <string.h>

3

4#include "../include/util.h"

5#include "../include/const.h"

6

7fz_timer_t *fz_timer_create(int frames, int step) {

8 fz_timer_t *timer = malloc(sizeof(fz_timer_t));

9 timer->frames = step * frames;

10 timer->count = 0;

11 timer->step = step;

12 return timer;

13}

14

15bool fz_timer_check(fz_timer_t *timer) {

16 return timer->count == 0;

17}

18

19void fz_timer_update(fz_timer_t *timer) {

20 if (timer->count >= timer->frames) {

21 timer->count = 0;

22 }

23 else {

24 timer->count++;

25 }

26}

27

28void fz_timer_reset(fz_timer_t *timer) {

29 timer->count = 0;

30}

31

32int fz_timer_get(fz_timer_t *timer) {

33 return timer->count / timer->step;

34}

35

36void fz_timer_free(fz_timer_t *timer) {

37 free(timer);

38}

39

40pos_t pos_snap(pos_t vec) {

41 return (pos_t) { (int) vec.x, (int) vec.y };

42}

43

44rect_t rect_from_pos(pos_t pos, int width, int height) {

45 return (rect_t) { pos.x, pos.y, width, height };

46}

47

48bool rect_collide(rect_t a, rect_t b) {

49 return !(

50 a.x > b.x + b.width ||

51 a.x + a.width < b.x ||

52 a.y + a.height < b.y ||

53 a.y > b.y + b.height

54 );

55}

56

57Texture2D texture_load(char *filename, int scale) {

58 Image image = LoadImage(filename);

59 ImageResizeNN(&image, scale * image.width, scale * image.height);

60 Texture2D texture = LoadTextureFromImage(image);

61 UnloadImage(image);

62 return texture;

63}

64

65Rectangle texture_rect(int x, int y, int width, int height) {

66 return (Rectangle) { x * width, y * height, width, height };

67}

68

69Rectangle texture_rect_v(int x, int y, int width, int height, int size_x, int size_y) {

70 return (Rectangle) { x * size_x, y * size_y, width * size_x, height * size_y };

71}

72

73

74void text_draw(pos_t pos, char *text, text_alignment_e alignment, game_t *game) {

75 pos_t text_pos = pos;

76 int text_len = strlen(text);

77 float text_width = text_len * (FONT_WIDTH + FONT_WIDTH / 3.0);

78 switch (alignment) {

79 case TEXT_ALIGNMENT_LEFT:

80 break;

81 case TEXT_ALIGNMENT_RIGHT:

82 text_pos.x -= text_width;

83 break;

84 case TEXT_ALIGNMENT_CENTER: {

85 text_pos.x -= text_width / 2.0;

86 break;

87 }

88 }

89 for (int i = 0; i < text_len; i++) {

90 char c = text[i];

91 int x = 0;

92 int y = 0;

93 if (c >= 48 && c <= 58) {

94 x = text[i] - 48;

95 y = 0;

96 }

97 else if (c >= 65 && c <= 90) {

98 x = (text[i] - 65) % 10;

99 y = (text[i] - 65) / 10 + 1;

100 }

101 else if (c >= 97 && c <= 122) {

102 x = (text[i] - 97) % 10;

103 y = (text[i] - 97) / 10 + 1;

104 }

105 else if (c == ' ') {

106 x = 9;

107 y = 3;

108 }

109 else if (c == '+') {

110 x = 7;

111 y = 3;

112 }

113 else if (c == '-') {

114 x = 8;

115 y = 3;

116 }

117 else {

118 x = 6;

119 y = 3;

120 }

121 DrawTextureRec(

122 game->assets.font,

123 texture_rect(x, y, FONT_WIDTH, FONT_HEIGHT),

124 pos_snap((pos_t) { text_pos.x + i * (FONT_WIDTH + FONT_WIDTH / 3.0), text_pos.y }),

125 WHITE

126 );

127 }

128}