A retro platform game
git clone git://soophie.de/freezo
blob: 2f6914991aaf9d834f45ee383b7c4ec3d8843666 (3114 bytes)[raw]
1
#include <stdlib.h>2
#include <string.h>4
#include "../include/util.h"5
#include "../include/const.h"7
fz_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
}15
bool fz_timer_check(fz_timer_t *timer) {16
return timer->count == 0;17
}19
void 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
}28
void fz_timer_reset(fz_timer_t *timer) {29
timer->count = 0;30
}32
int fz_timer_get(fz_timer_t *timer) {33
return timer->count / timer->step;34
}36
void fz_timer_free(fz_timer_t *timer) {37
free(timer);38
}40
pos_t pos_snap(pos_t vec) {41
return (pos_t) { (int) vec.x, (int) vec.y };42
}44
rect_t rect_from_pos(pos_t pos, int width, int height) {45
return (rect_t) { pos.x, pos.y, width, height };46
}48
bool 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.height54
);55
}57
bool rect_collide_threshold(rect_t a, rect_t b, float threshold) {58
return !(59
a.x > b.x + (b.width - threshold) ||60
a.x + (a.width - threshold) < b.x ||61
a.y + (a.height - threshold) < b.y ||62
a.y > b.y + (b.height - threshold)63
);64
}66
Texture2D texture_load(char *filename, int scale) {67
Image image = LoadImage(filename);68
ImageResizeNN(&image, scale * image.width, scale * image.height);69
Texture2D texture = LoadTextureFromImage(image);70
UnloadImage(image);71
return texture;72
}74
Rectangle texture_rect(int x, int y, int width, int height) {75
return (Rectangle) { x * width, y * height, width, height };76
}78
Rectangle texture_rect_v(int x, int y, int width, int height, int size_x, int size_y) {79
return (Rectangle) { x * size_x, y * size_y, width * size_x, height * size_y };80
}83
void text_draw(pos_t pos, char *text, text_alignment_e alignment, game_t *game) {84
pos_t text_pos = pos;85
int text_len = strlen(text);86
float text_width = text_len * (FONT_WIDTH + FONT_WIDTH / 3.0);87
switch (alignment) {88
case TEXT_ALIGNMENT_LEFT:89
break;90
case TEXT_ALIGNMENT_RIGHT:91
text_pos.x -= text_width;92
break;93
case TEXT_ALIGNMENT_CENTER: {94
text_pos.x -= text_width / 2.0;95
break;96
}97
}98
for (int i = 0; i < text_len; i++) {99
char c = text[i];100
int x = 0;101
int y = 0;102
if (c >= 48 && c <= 58) {103
x = text[i] - 48;104
y = 0;105
}106
else if (c >= 65 && c <= 90) {107
x = (text[i] - 65) % 10;108
y = (text[i] - 65) / 10 + 1;109
}110
else if (c >= 97 && c <= 122) {111
x = (text[i] - 97) % 10;112
y = (text[i] - 97) / 10 + 1;113
}114
else if (c == ' ') {115
x = 9;116
y = 3;117
}118
else if (c == '+') {119
x = 7;120
y = 3;121
}122
else if (c == '-') {123
x = 8;124
y = 3;125
}126
else {127
x = 6;128
y = 3;129
}130
DrawTextureRec(131
game->assets.font,132
texture_rect(x, y, FONT_WIDTH, FONT_HEIGHT),133
pos_snap((pos_t) { text_pos.x + i * (FONT_WIDTH + FONT_WIDTH / 3.0), text_pos.y }),134
WHITE135
);136
}137
}