Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit fe933fb

Browse files
committed
Create a "builder" module that abstracts the loading of all assets.
1 parent a85b673 commit fe933fb

4 files changed

Lines changed: 117 additions & 53 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ configure_file("${PROJECT_SOURCE_DIR}/src/generated.hpp.in" "${PROJECT_BINARY_DI
9191
add_library(${PROJECT_NAME}-lib STATIC
9292
# find src -name "*.cpp" ! -name "main.cpp" | sort
9393
src/app.cpp
94+
src/assets/builder.cpp
9495
src/assets/sounds.cpp
9596
src/assets/textures.cpp
9697
src/core/backend.cpp

src/app.cpp

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <spdlog/spdlog.h>
1818

1919
#include "app.hpp"
20+
#include "assets/builder.hpp"
2021
#include "assets/sounds.hpp"
2122
#include "assets/textures.hpp"
2223
#include "core/backend.hpp"
@@ -32,29 +33,6 @@
3233
#include "generated.hpp"
3334
#include "settings.hpp"
3435

35-
// Embedded road textures
36-
#include "assets/data/textures/road/road_sand01.hpp"
37-
#include "assets/data/textures/road/road_sand35.hpp"
38-
#include "assets/data/textures/road/road_sand37.hpp"
39-
#include "assets/data/textures/road/road_sand39.hpp"
40-
#include "assets/data/textures/road/road_sand87.hpp"
41-
#include "assets/data/textures/road/road_sand88.hpp"
42-
#include "assets/data/textures/road/road_sand89.hpp"
43-
44-
// Embedded car textures
45-
#include "assets/data/textures/car/car_black_1.hpp"
46-
#include "assets/data/textures/car/car_blue_1.hpp"
47-
#include "assets/data/textures/car/car_green_1.hpp"
48-
#include "assets/data/textures/car/car_red_1.hpp"
49-
#include "assets/data/textures/car/car_yellow_1.hpp"
50-
51-
// Embedded sounds
52-
#include "assets/data/sounds/car/engine.hpp"
53-
#include "assets/data/sounds/car/hit.hpp"
54-
#include "assets/data/sounds/car/tires.hpp"
55-
#include "assets/data/sounds/ui/ok.hpp"
56-
#include "assets/data/sounds/ui/other.hpp"
57-
5836
namespace app {
5937

6038
void run()
@@ -91,39 +69,11 @@ void run()
9169

9270
// Setup texture manager and load textures
9371
// Note: This cannot be "static", because the destructor for static objects is called after "main()" has finished
94-
assets::textures::TextureManager textures;
95-
for (const auto &[identifier, data, size] : {
96-
// Road textures
97-
std::tuple{"top_left", road_sand89::data, road_sand89::size},
98-
std::tuple{"top_right", road_sand01::data, road_sand01::size},
99-
std::tuple{"bottom_right", road_sand37::data, road_sand37::size},
100-
std::tuple{"bottom_left", road_sand35::data, road_sand35::size},
101-
std::tuple{"vertical", road_sand87::data, road_sand87::size},
102-
std::tuple{"horizontal", road_sand88::data, road_sand88::size},
103-
std::tuple{"horizontal_finish", road_sand39::data, road_sand39::size},
104-
// Car textures
105-
std::tuple{"car_black", car_black_1::data, car_black_1::size},
106-
std::tuple{"car_blue", car_blue_1::data, car_blue_1::size},
107-
std::tuple{"car_green", car_green_1::data, car_green_1::size},
108-
std::tuple{"car_red", car_red_1::data, car_red_1::size},
109-
std::tuple{"car_yellow", car_yellow_1::data, car_yellow_1::size},
110-
}) {
111-
textures.load(identifier, {data, size});
112-
}
72+
assets::textures::TextureManager textures = assets::builder::build_texture_manager();
11373

11474
// Setup sound manager and load sounds
11575
// Note: This cannot be "static", because the destructor for static objects is called after "main()" has finished
116-
assets::sounds::SoundManager sounds;
117-
for (const auto &[identifier, data, size] : {
118-
// Car sounds
119-
std::tuple{"engine", engine::data, engine::size},
120-
std::tuple{"tires", tires::data, tires::size},
121-
std::tuple{"hit", hit::data, hit::size},
122-
std::tuple{"ok", ok::data, ok::size},
123-
std::tuple{"other", other::data, other::size},
124-
}) {
125-
sounds.load(identifier, {data, size});
126-
}
76+
assets::sounds::SoundManager sounds = assets::builder::build_sound_manager();
12777

12878
// Create race track
12979
core::world::Track race_track(

src/assets/builder.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @file builder.cpp
3+
*/
4+
5+
// Managers
6+
#include "sounds.hpp"
7+
#include "textures.hpp"
8+
9+
// Embedded road textures
10+
#include "data/textures/road/road_sand01.hpp"
11+
#include "data/textures/road/road_sand35.hpp"
12+
#include "data/textures/road/road_sand37.hpp"
13+
#include "data/textures/road/road_sand39.hpp"
14+
#include "data/textures/road/road_sand87.hpp"
15+
#include "data/textures/road/road_sand88.hpp"
16+
#include "data/textures/road/road_sand89.hpp"
17+
18+
// Embedded car textures
19+
#include "data/textures/car/car_black_1.hpp"
20+
#include "data/textures/car/car_blue_1.hpp"
21+
#include "data/textures/car/car_green_1.hpp"
22+
#include "data/textures/car/car_red_1.hpp"
23+
#include "data/textures/car/car_yellow_1.hpp"
24+
25+
// Embedded sounds
26+
#include "data/sounds/car/engine.hpp"
27+
#include "data/sounds/car/hit.hpp"
28+
#include "data/sounds/car/tires.hpp"
29+
#include "data/sounds/ui/ok.hpp"
30+
#include "data/sounds/ui/other.hpp"
31+
32+
#include "builder.hpp"
33+
34+
namespace assets::builder {
35+
36+
assets::textures::TextureManager build_texture_manager()
37+
{
38+
assets::textures::TextureManager texture_manager;
39+
40+
// Road textures
41+
texture_manager.load("top_left", {road_sand89::data, road_sand89::size});
42+
texture_manager.load("top_right", {road_sand01::data, road_sand01::size});
43+
texture_manager.load("bottom_right", {road_sand37::data, road_sand37::size});
44+
texture_manager.load("bottom_left", {road_sand35::data, road_sand35::size});
45+
texture_manager.load("vertical", {road_sand87::data, road_sand87::size});
46+
texture_manager.load("horizontal", {road_sand88::data, road_sand88::size});
47+
texture_manager.load("horizontal_finish", {road_sand39::data, road_sand39::size});
48+
49+
// Car textures
50+
texture_manager.load("car_black", {car_black_1::data, car_black_1::size});
51+
texture_manager.load("car_blue", {car_blue_1::data, car_blue_1::size});
52+
texture_manager.load("car_green", {car_green_1::data, car_green_1::size});
53+
texture_manager.load("car_red", {car_red_1::data, car_red_1::size});
54+
texture_manager.load("car_yellow", {car_yellow_1::data, car_yellow_1::size});
55+
56+
return texture_manager;
57+
}
58+
59+
assets::sounds::SoundManager build_sound_manager()
60+
{
61+
assets::sounds::SoundManager sound_manager;
62+
63+
// Car sounds
64+
sound_manager.load("engine", {engine::data, engine::size});
65+
sound_manager.load("tires", {tires::data, tires::size});
66+
sound_manager.load("hit", {hit::data, hit::size});
67+
68+
// UI sounds
69+
sound_manager.load("ok", {ok::data, ok::size});
70+
sound_manager.load("other", {other::data, other::size});
71+
72+
return sound_manager;
73+
}
74+
75+
} // namespace assets::builder

src/assets/builder.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file builder.hpp
3+
*
4+
* @brief Provides functions to construct TextureManager and SoundManager instances, preloaded with all embedded game assets.
5+
*
6+
* This header centralizes asset management for the game, offering a higher-level interface compared to `sounds.hpp` and `textures.hpp`.
7+
*/
8+
9+
#pragma once
10+
11+
#include "sounds.hpp"
12+
#include "textures.hpp"
13+
14+
namespace assets::builder {
15+
16+
/**
17+
* @brief Build and return a TextureManager populated with all embedded textures.
18+
*
19+
* Available texture identifiers:
20+
* - Road textures: "top_left", "top_right", "bottom_right", "bottom_left", "vertical", "horizontal", "horizontal_finish"
21+
* - Car textures: "car_black", "car_blue", "car_green", "car_red", "car_yellow"
22+
*
23+
* @return TextureManager instance with all game textures loaded and ready to use.
24+
*/
25+
[[nodiscard]] assets::textures::TextureManager build_texture_manager();
26+
27+
/**
28+
* @brief Build and return a SoundManager populated with all embedded sounds.
29+
*
30+
* Available sound identifiers:
31+
* - Car sounds: "engine", "tires", "hit"
32+
* - UI sounds: "ok", "other"
33+
*
34+
* @return SoundManager instance with all game audio loaded and ready to use.
35+
*/
36+
[[nodiscard]] assets::sounds::SoundManager build_sound_manager();
37+
38+
} // namespace assets::builder

0 commit comments

Comments
 (0)