z_mirror/sdl-osu

git clone https://git.raiguard.me/z_mirror/sdl-osu

Caleb HeuerSwitch to four-space indents and inline braces836a89c

trunk
1.4 KiB60 linesraw
1#include "util.h"
2#include <SDL3/SDL.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6bool Osu_HasPrefix(const char* str, const char* prefix) {
7    if (!str || !prefix) {
8        return false;
9    }
10
11    int strLen = strlen(str);
12    int prefixLen = strlen(prefix);
13    if (strLen < prefixLen) {
14        return false;
15    }
16
17    return strncmp(str, prefix, prefixLen) == 0;
18}
19
20bool Osu_HasSuffix(const char* str, const char* suffix) {
21    if (!str || !suffix) {
22        return false;
23    }
24
25    int strLen = strlen(str);
26    int suffixLen = strlen(suffix);
27    if (strLen < suffixLen) {
28        return false;
29    }
30
31    return strncmp(str + strLen - suffixLen, suffix, suffixLen) == 0;
32}
33
34struct SDL_FRect Osu_GetTextureRect(struct SDL_Texture* texture) {
35    float texW, texH;
36    SDL_GetTextureSize(texture, &texW, &texH);
37    SDL_assert(texW > 0);
38    SDL_assert(texH > 0);
39    return (SDL_FRect){
40        .x = 0,
41        .y = 0,
42        .w = texW,
43        .h = texH,
44    };
45}
46
47void Osu_Panic(const char* title, const char* description) {
48    char* descriptionCopy;
49    SDL_asprintf(&descriptionCopy, "%s", description);
50    fprintf(stderr, "%s: %s\n", title, descriptionCopy);
51    if (!SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, descriptionCopy, NULL)) {
52        fprintf(stderr, "Failed to show message box: %s\n", SDL_GetError());
53    }
54    free(descriptionCopy);
55    exit(EXIT_FAILURE);
56}
57
58int Osu_Random(int min, int max) {
59    return rand() % (max + 1 - min) + min;
60}