z_mirror/sdl-osu
git clone https://git.raiguard.me/z_mirror/sdl-osu
dbd643c
trunk
1#include "graphics.h" 2#include "beatmapselector.h" 3#include "defines.h" 4#include "util.h" 5#include <SDL3/SDL.h> 6#include <SDL3_image/SDL_image.h> 7#include <SDL3_ttf/SDL_ttf.h> 8#include <stdio.h> 9#include <stdlib.h> 10 11struct Osu_Graphics * Osu_CreateGraphics (const char * dataPath ) { 12SDL_SAFECALL (SDL_Init ,SDL_INIT_VIDEO ); 13 14struct Osu_Graphics * graphics = calloc (1 ,sizeof (struct Osu_Graphics )); 15SDL_SAFECALL (SDL_CreateWindowAndRenderer ,"osu! " OSU_VERSION ,800 ,600 , 16SDL_WINDOW_HIGH_PIXEL_DENSITY |SDL_WINDOW_RESIZABLE |SDL_WINDOW_FULLSCREEN , 17& graphics -> window ,& graphics -> renderer ) 18 19// TODO: Go through all calls and check which actually need to be fatal errors. 20SDL_SAFECALL (SDL_SetRenderVSync ,graphics -> renderer , true); 21 22SDL_SAFECALL (SDL_HideCursor ); 23 24char path [512 ]; 25snprintf (path ,512 ,"%s" OSU_PATHSEP "assets" OSU_PATHSEP "Inter-Medium.ttf" ,dataPath ); 26SDL_SAFECALL (TTF_Init ); 27SDL_SAFEASSN (graphics -> font ,TTF_OpenFont ,path ,32 ); 28SDL_SAFEASSN (graphics -> debugFont ,TTF_OpenFont ,path ,16 ); 29SDL_SAFEASSN (graphics -> textEngine ,TTF_CreateRendererTextEngine ,graphics -> renderer ); 30 31SDL_SAFEASSN (graphics -> debugText ,TTF_CreateText ,graphics -> textEngine ,graphics -> debugFont ,"" ,0 ); 32 33// TODO: There is a lot of conditional logic regarding skins that we are outright skipping. 34snprintf (path ,512 ,"%s" OSU_PATHSEP "skins" OSU_PATHSEP OSU_ACTIVE_SKIN OSU_PATHSEP "%s" ,dataPath ,"menu-button-background@2x.png" ); 35SDL_SAFEASSN (graphics -> beatmapWedgeTexture ,IMG_LoadTexture ,graphics -> renderer ,path ); 36snprintf (path ,512 ,"%s" OSU_PATHSEP "skins" OSU_PATHSEP OSU_ACTIVE_SKIN OSU_PATHSEP "%s" ,dataPath ,"cursor.png" ); 37SDL_SAFEASSN (graphics -> cursorTexture ,IMG_LoadTexture ,graphics -> renderer ,path ); 38snprintf (path ,512 ,"%s" OSU_PATHSEP "skins" OSU_PATHSEP OSU_ACTIVE_SKIN OSU_PATHSEP "%s" ,dataPath ,"menu-background.png" ); 39SDL_SAFEASSN (graphics -> menuBackgroundTexture ,IMG_LoadTexture ,graphics -> renderer ,path ); 40snprintf (path ,512 ,"%s" OSU_PATHSEP "skins" OSU_PATHSEP OSU_ACTIVE_SKIN OSU_PATHSEP "%s" ,dataPath ,"songselect-top.png" ); 41SDL_SAFEASSN (graphics -> songSelectTopTexture ,IMG_LoadTexture ,graphics -> renderer ,path ); 42snprintf (path ,512 ,"%s" OSU_PATHSEP "skins" OSU_PATHSEP OSU_ACTIVE_SKIN OSU_PATHSEP "%s" ,dataPath ,"hitcircle.png" ); 43SDL_SAFEASSN (graphics -> hitCircleTexture ,IMG_LoadTexture ,graphics -> renderer ,path ); 44snprintf (path ,512 ,"%s" OSU_PATHSEP "skins" OSU_PATHSEP OSU_ACTIVE_SKIN OSU_PATHSEP "%s" ,dataPath ,"hitcircleoverlay.png" ); 45SDL_SAFEASSN (graphics -> hitCircleOverlayTexture ,IMG_LoadTexture ,graphics -> renderer ,path ); 46snprintf (path ,512 ,"%s" OSU_PATHSEP "skins" OSU_PATHSEP OSU_ACTIVE_SKIN OSU_PATHSEP "%s" ,dataPath ,"approachcircle.png" ); 47SDL_SAFEASSN (graphics -> approachCircleTexture ,IMG_LoadTexture ,graphics -> renderer ,path ); 48 49graphics -> beatmapDimFactor = 255 ; 50 51return graphics ; 52} 53 54void Osu_DestroyGraphics (struct Osu_Graphics * graphics ) { 55if (graphics -> selectedBeatmapWallpaper ) { 56SDL_DestroyTexture (graphics -> selectedBeatmapWallpaper ); 57 } 58 59SDL_DestroyTexture (graphics -> beatmapWedgeTexture ); 60SDL_DestroyTexture (graphics -> cursorTexture ); 61SDL_DestroyTexture (graphics -> menuBackgroundTexture ); 62SDL_DestroyTexture (graphics -> songSelectTopTexture ); 63SDL_DestroyTexture (graphics -> hitCircleTexture ); 64SDL_DestroyTexture (graphics -> hitCircleOverlayTexture ); 65SDL_DestroyTexture (graphics -> approachCircleTexture ); 66 67TTF_DestroyText (graphics -> debugText ); 68graphics -> debugText = NULL ; 69 70TTF_DestroyRendererTextEngine (graphics -> textEngine ); 71TTF_CloseFont (graphics -> debugFont ); 72TTF_CloseFont (graphics -> font ); 73TTF_Quit (); 74 75SDL_DestroyRenderer (graphics -> renderer ); 76SDL_DestroyWindow (graphics -> window ); 77 78SDL_QuitSubSystem (SDL_INIT_VIDEO ); 79 80free (graphics ); 81} 82 83void Osu_PrepareRender (struct Osu_Graphics * graphics ) { 84int screenW ,screenH ; 85SDL_SAFECALL (SDL_GetWindowSizeInPixels ,graphics -> window ,& screenW ,& screenH ); 86graphics -> screenRect .w = screenW ; 87graphics -> screenRect .h = screenH ; 88 89SDL_SAFECALL (SDL_SetRenderDrawColor ,graphics -> renderer ,0 ,0 ,0 ,255 ); 90SDL_SAFECALL (SDL_RenderClear ,graphics -> renderer ); 91} 92 93void Osu_RenderWallpaper (struct Osu_Graphics * graphics ) { 94struct SDL_Texture * wallpaper = graphics -> menuBackgroundTexture ; 95if (graphics -> selectedBeatmapWallpaper ) { 96wallpaper = graphics -> selectedBeatmapWallpaper ; 97 } 98float texW ,texH ; 99SDL_SAFECALL (SDL_GetTextureSize ,wallpaper ,& texW ,& texH ); 100SDL_assert (texW > 0 ); 101SDL_assert (texH > 0 ); 102double texAspectRatio = texW /texH ; 103double screenAspectRatio = graphics -> screenRect .w /graphics -> screenRect .h ; 104double scalingFactor = 1.0 ; 105if (screenAspectRatio > texAspectRatio ) { 106scalingFactor = graphics -> screenRect .w /texW ; 107 }else { 108scalingFactor = graphics -> screenRect .h /texH ; 109 } 110float adjustedTexW = texW * scalingFactor ; 111float adjustedTexH = texH * scalingFactor ; 112SDL_FRect destRect = { 113 .x = (graphics -> screenRect .w /2 )- (adjustedTexW /2 ), 114 .y = (graphics -> screenRect .h /2 )- (adjustedTexH /2 ), 115 .w = adjustedTexW , 116 .h = adjustedTexH , 117 }; 118SDL_SAFECALL (SDL_RenderTexture ,graphics -> renderer ,wallpaper ,NULL ,& destRect ); 119} 120 121void Osu_RenderCursor (struct Osu_Graphics * graphics ) { 122struct SDL_FRect texRect = Osu_GetTextureRect (graphics -> cursorTexture ); 123texRect .w *=1.25 ; 124texRect .h *=1.25 ; 125 126float mouseX ,mouseY ; 127SDL_GetMouseState (& mouseX ,& mouseY ); 128float scale ; 129SDL_SAFEASSN (scale ,SDL_GetWindowDisplayScale ,graphics -> window ); 130mouseX *=scale ; 131mouseY *=scale ; 132 133SDL_FRect destRect = { 134 .x = mouseX - (texRect .w /2 ), 135 .y = mouseY - (texRect .h /2 ), 136 .w = texRect .w , 137 .h = texRect .h , 138 }; 139SDL_SAFECALL (SDL_RenderTexture ,graphics -> renderer ,graphics -> cursorTexture ,& texRect ,& destRect ); 140} 141 142void Osu_SetWallpaper (struct Osu_Graphics * graphics ,const char * filePath ) { 143if (graphics -> selectedBeatmapWallpaper ) { 144SDL_DestroyTexture (graphics -> selectedBeatmapWallpaper ); 145 } 146 147SDL_SAFEASSN (graphics -> selectedBeatmapWallpaper ,IMG_LoadTexture ,graphics -> renderer ,filePath ); 148}