z_mirror/sdl-osu
git clone https://git.raiguard.me/z_mirror/sdl-osu
836a89c
trunk
1#include "beatmapstore.h" 2#include "beatmap.h" 3#include "defines.h" 4#include "util.h" 5#include <SDL3/SDL.h> 6#include <stdio.h> 7#include <stdlib.h> 8 9struct Osu_BeatmapStore * Osu_CreateBeatmapStore (const char * dataPath ) { 10struct Osu_BeatmapStore * beatmaps = calloc (1 ,sizeof (struct Osu_BeatmapStore )); 11beatmaps -> mutex = SDL_CreateMutex (); 12beatmaps -> beatmapsLen = 0 ; 13SDL_asprintf (& beatmaps -> basePath ,"%s" OSU_PATHSEP "beatmaps" ,dataPath ); 14if (SDL_GetPathInfo (beatmaps -> basePath ,NULL )) { 15SDL_SAFEASSN (beatmaps -> loadWorker ,SDL_CreateThread ,& Osu_LoadBeatmapsWorker ,"BeatmapLoader" ,beatmaps ); 16 } 17return beatmaps ; 18} 19 20void Osu_DestroyBeatmapStore (struct Osu_BeatmapStore * beatmaps ) { 21SDL_LockMutex (beatmaps -> mutex ); 22for (Uint32 i = 0 ;i < beatmaps -> beatmapsLen ;i ++ ) { 23Osu_DestroyBeatmap (beatmaps -> beatmaps [i ]); 24 } 25free (beatmaps -> beatmaps ); 26beatmaps -> beatmaps = NULL ; 27SDL_UnlockMutex (beatmaps -> mutex ); 28 29SDL_WaitThread (beatmaps -> loadWorker ,NULL ); 30beatmaps -> loadWorker = NULL ; 31 32SDL_DestroyMutex (beatmaps -> mutex ); 33 34free (beatmaps -> basePath ); 35free (beatmaps ); 36} 37 38int Osu_LoadBeatmapsWorker (void * userdata ) { 39struct Osu_BeatmapStore * beatmaps = userdata ; 40int filesCount ; 41char ** files = SDL_GlobDirectory (beatmaps -> basePath ,"*" OSU_PATHSEP "*.osu" ,SDL_GLOB_CASEINSENSITIVE ,& filesCount ); 42if (!files ) { 43return 0 ; 44 } 45 46size_t filesLen = filesCount ;// Why does SDL return this as an int!? 47 48beatmaps -> beatmaps = calloc (filesCount ,sizeof (struct Osu_Beatmap * )); 49 50char filepath [512 ]; 51for (size_t i = 0 ;i < filesLen ;i ++ ) { 52sprintf (filepath ,"%s" OSU_PATHSEP "%s" ,beatmaps -> basePath ,files [i ]); 53 54struct Osu_Beatmap * beatmap = Osu_LoadBeatmap (filepath ); 55if (!beatmap ) { 56continue ; 57 } 58 59SDL_LockMutex (beatmaps -> mutex ); 60// If beatmaps no longer exists then loading was canceled. 61if (!beatmaps -> beatmaps ) { 62Osu_DestroyBeatmap (beatmap ); 63break ; 64 } 65beatmaps -> beatmaps [beatmaps -> beatmapsLen ]= beatmap ; 66beatmaps -> beatmapsLen ++ ; 67SDL_UnlockMutex (beatmaps -> mutex ); 68 } 69 70SDL_LockMutex (beatmaps -> mutex ); 71 72if (beatmaps -> beatmaps && beatmaps -> beatmapsLen < filesLen ) { 73beatmaps -> beatmaps = realloc (beatmaps -> beatmaps ,sizeof (struct Osu_Beatmap * )* beatmaps -> beatmapsLen ); 74 } 75 76beatmaps -> finishedLoading = true; 77 78SDL_UnlockMutex (beatmaps -> mutex ); 79 80SDL_free (files ); 81return 0 ; 82}