@ -7,33 +7,43 @@
# include <memory>
# include <memory>
namespace textures {
namespace textures {
using std : : shared_ptr , std : : make_shared ;
using std : : shared_ptr , std : : make_shared , nlohmann : : json , std : : string ;
static TextureManager TMGR ;
static TextureManager TMGR ;
static bool initialized = false ;
static bool initialized = false ;
void load_sprites ( ) {
void load_sprite_textures ( SpriteTextureMap & mapping , json & config , bool smooth ) {
Config assets ( " assets/config.json " ) ;
for ( auto & [ name , settings ] : config . items ( ) ) {
for ( auto & [ name , settings ] : assets [ " sprites " ] . items ( ) ) {
auto texture = make_shared < sf : : Texture > ( settings [ " path " ] ) ;
auto texture = make_shared < sf : : Texture > ( settings [ " path " ] ) ;
texture - > setSmooth ( assets [ " graphics " ] [ " smooth_textures " ] ) ;
texture - > setSmooth ( smooth ) ;
auto sprite = make_shared < sf : : Sprite > ( * texture ) ;
auto sprite = make_shared < sf : : Sprite > ( * texture ) ;
int width = settings [ " frame_width " ] ;
int width = settings [ " frame_width " ] ;
int height = settings [ " frame_height " ] ;
int height = settings [ " frame_height " ] ;
dbc : : check ( width % 2 = = 0 ,
dbc : : check ( width % 2 = = 0 ,
fmt : : format ( " sprite {} has invalid frame size " , name ) ) ;
fmt : : format ( " sprite {} has invalid frame size {} " , name , width ) ) ;
sf : : Vector2i frame_size { width , height } ;
sf : : Vector2i frame_size { width , height } ;
sprite - > setTextureRect ( { { 0 , 0 } , frame_size } ) ;
sprite - > setTextureRect ( { { 0 , 0 } , frame_size } ) ;
TMGR . sprite_textures . try_emplace ( name , sprite , texture , frame_size ) ;
dbc : : check ( ! mapping . contains ( name ) ,
fmt : : format ( " duplicate sprite/icon name {} " , ( string ) name ) ) ;
mapping . try_emplace ( name , sprite , texture , frame_size ) ;
}
}
}
}
void load_sprites ( ) {
Config sprites ( " assets/config.json " ) ;
bool smooth = sprites [ " graphics " ] [ " smooth_textures " ] ;
load_sprite_textures ( TMGR . sprite_textures , sprites [ " sprites " ] , smooth ) ;
Config icons ( " assets/icons.json " ) ;
load_sprite_textures ( TMGR . icon_textures , icons . json ( ) , smooth ) ;
}
inline void resize_shit ( size_t size ) {
inline void resize_shit ( size_t size ) {
TMGR . surfaces . resize ( size ) ;
TMGR . surfaces . resize ( size ) ;
TMGR . ceilings . resize ( size ) ;
TMGR . ceilings . resize ( size ) ;
@ -49,12 +59,12 @@ namespace textures {
for ( auto & el : tiles . items ( ) ) {
for ( auto & el : tiles . items ( ) ) {
auto & config = el . value ( ) ;
auto & config = el . value ( ) ;
const std : : st ring & texture_fname = config [ " texture " ] ;
const string & texture_fname = config [ " texture " ] ;
size_t surface_i = config [ " id " ] ;
size_t surface_i = config [ " id " ] ;
dbc : : check ( ! TMGR . name_to_id . contains ( el . key ( ) ) ,
dbc : : check ( ! TMGR . name_to_id . contains ( el . key ( ) ) ,
fmt : : format ( " duplicate key in textures {} " ,
fmt : : format ( " duplicate key in textures {} " ,
( std : : st ring ) el . key ( ) ) ) ;
( string ) el . key ( ) ) ) ;
TMGR . name_to_id . insert_or_assign ( el . key ( ) , surface_i ) ;
TMGR . name_to_id . insert_or_assign ( el . key ( ) , surface_i ) ;
@ -68,9 +78,9 @@ namespace textures {
// NOTE: ceilings defaults to 0 which is floor texture so only need to update
// NOTE: ceilings defaults to 0 which is floor texture so only need to update
if ( config . contains ( " ceiling " ) ) {
if ( config . contains ( " ceiling " ) ) {
const std : : st ring & name = config [ " ceiling " ] ;
const string & name = config [ " ceiling " ] ;
dbc : : check ( tiles . contains ( name ) , fmt : : format ( " invalid ceiling name {} in tile config {} " , name , ( std : : st ring ) el . key ( ) ) ) ;
dbc : : check ( tiles . contains ( name ) , fmt : : format ( " invalid ceiling name {} in tile config {} " , name , ( string ) el . key ( ) ) ) ;
auto & ceiling = tiles [ name ] ;
auto & ceiling = tiles [ name ] ;
TMGR . ceilings [ surface_i ] = ceiling [ " id " ] ;
TMGR . ceilings [ surface_i ] = ceiling [ " id " ] ;
@ -80,7 +90,7 @@ namespace textures {
void load_map_tiles ( ) {
void load_map_tiles ( ) {
Config config ( " ./assets/map_tiles.json " ) ;
Config config ( " ./assets/map_tiles.json " ) ;
nlohmann : : json & tiles = config . json ( ) ;
json & tiles = config . json ( ) ;
for ( auto tile : tiles ) {
for ( auto tile : tiles ) {
sf : : Vector2i coords { tile [ " x " ] , tile [ " y " ] } ;
sf : : Vector2i coords { tile [ " x " ] , tile [ " y " ] } ;
@ -107,12 +117,12 @@ namespace textures {
}
}
}
}
SpriteTexture get ( const std : : string & name ) {
SpriteTexture & get ( const string & name , SpriteTextureMap & mapping ) {
dbc : : check ( initialized , " you forgot to call textures::init() " ) ;
dbc : : check ( initialized , " you forgot to call textures::init() " ) ;
dbc : : check ( TMGR . sprite_textures . contains ( name ) ,
dbc : : check ( mapping . contains ( name ) ,
fmt : : format ( " !!!!! texture pack does not contain {} sprite " , name ) ) ;
fmt : : format ( " !!!!! textures do not contain {} sprite " , name ) ) ;
auto result = TMGR . sprite_textures . at ( name ) ;
auto & result = mapping . at ( name ) ;
dbc : : check ( result . sprite ! = nullptr ,
dbc : : check ( result . sprite ! = nullptr ,
fmt : : format ( " bad sprite from textures::get named {} " , name ) ) ;
fmt : : format ( " bad sprite from textures::get named {} " , name ) ) ;
@ -122,7 +132,15 @@ namespace textures {
return result ;
return result ;
}
}
sf : : Image load_image ( const std : : string & filename ) {
SpriteTexture get_sprite ( const string & name ) {
return get ( name , TMGR . sprite_textures ) ;
}
SpriteTexture get_icon ( const string & name ) {
return get ( name , TMGR . icon_textures ) ;
}
sf : : Image load_image ( const string & filename ) {
sf : : Image texture ;
sf : : Image texture ;
bool good = texture . loadFromFile ( filename ) ;
bool good = texture . loadFromFile ( filename ) ;
dbc : : check ( good , fmt : : format ( " failed to load {} " , filename ) ) ;
dbc : : check ( good , fmt : : format ( " failed to load {} " , filename ) ) ;
@ -150,7 +168,7 @@ namespace textures {
return ( const uint32_t * ) TMGR . surfaces [ ceiling_num ] . getPixelsPtr ( ) ;
return ( const uint32_t * ) TMGR . surfaces [ ceiling_num ] . getPixelsPtr ( ) ;
}
}
size_t get_id ( const std : : st ring & name ) {
size_t get_id ( const string & name ) {
dbc : : check ( TMGR . name_to_id . contains ( name ) ,
dbc : : check ( TMGR . name_to_id . contains ( name ) ,
fmt : : format ( " there is no texture named {} in tiles.json " , name ) ) ;
fmt : : format ( " there is no texture named {} in tiles.json " , name ) ) ;
return TMGR . name_to_id . at ( name ) ;
return TMGR . name_to_id . at ( name ) ;