so://phie

freezo

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

blob: 81a9aac5e4d70a39a2307547ad4be2833cf23903 (2691 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

69

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

71 pos_t text_pos = pos;

72 int text_len = strlen(text);

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

74 switch (alignment) {

75 case TEXT_ALIGNMENT_LEFT:

76 break;

77 case TEXT_ALIGNMENT_RIGHT:

78 text_pos.x -= text_width;

79 break;

80 case TEXT_ALIGNMENT_CENTER: {

81 text_pos.x -= text_width / 2.0;

82 break;

83 }

84 }

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

86 char c = text[i];

87 int x = 0;

88 int y = 0;

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

90 x = text[i] - 48;

91 y = 0;

92 }

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

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

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

96 }

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

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

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

100 }

101 else if (c == ' ') {

102 x = 9;

103 y = 3;

104 }

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

106 x = 7;

107 y = 3;

108 }

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

110 x = 8;

111 y = 3;

112 }

113 else {

114 x = 6;

115 y = 3;

116 }

117 DrawTextureRec(

118 game->assets.font,

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

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

121 WHITE

122 );

123 }

124}