@@ -27,14 +27,14 @@ Game_Map make_map()// NOLINT congetive complexity
2727 Game_Map map{ Size{ 10 , 10 } };// NOLINT magic numbers
2828
2929 auto solid_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
30- [[maybe_unused]] std::chrono::milliseconds game_clock ,
30+ [[maybe_unused]] const Game &game ,
3131 [[maybe_unused]] Point map_location) {
3232 for (std::size_t cur_x = 0 ; cur_x < pixels.size ().width ; ++cur_x) {
3333 for (std::size_t cur_y = 0 ; cur_y < pixels.size ().height ; ++cur_y) {
3434 if (cur_x == 0 || cur_y == 0 || cur_x == pixels.size ().width - 1 || cur_y == pixels.size ().height - 1 ) {
3535 pixels.at (Point{ cur_x, cur_y }) = Color{ 128 , 128 , 128 , 255 };// NOLINT Magic numbers
3636 } else {
37- switch ((game_clock .count () / 1000 ) % 2 ) {// NOLINT Magic numbers
37+ switch ((game. clock .count () / 1000 ) % 2 ) {// NOLINT Magic numbers
3838 case 0 :
3939 pixels.at (Point{ cur_x, cur_y }) = Color{ 255 , 255 , 255 , 255 };// NOLINT Magic numbers
4040 break ;
@@ -47,10 +47,10 @@ Game_Map make_map()// NOLINT congetive complexity
4747 }
4848 };
4949
50- auto cannot_enter = [](std::chrono::milliseconds , Point, Direction) -> bool { return false ; };
50+ auto cannot_enter = [](const Game & , Point, Direction) -> bool { return false ; };
5151
5252 auto empty_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
53- [[maybe_unused]] std::chrono::milliseconds game_clock ,
53+ [[maybe_unused]] const Game &game ,
5454 [[maybe_unused]] Point map_location) {
5555 for (std::size_t cur_x = 0 ; cur_x < pixels.size ().width ; ++cur_x) {
5656 for (std::size_t cur_y = 0 ; cur_y < pixels.size ().height ; ++cur_y) {
@@ -60,31 +60,54 @@ Game_Map make_map()// NOLINT congetive complexity
6060 };
6161
6262 for (std::size_t cur_x = 0 ; cur_x < map.locations .size ().width ; ++cur_x) {
63- for (std::size_t cur_y = 0 ; cur_y < map.locations .size ().height ; ++cur_y) { map.locations .at (Point{ cur_x, cur_y }).draw = empty_draw; }
63+ for (std::size_t cur_y = 0 ; cur_y < map.locations .size ().height ; ++cur_y) {
64+ map.locations .at (Point{ cur_x, cur_y }).draw = empty_draw;
65+ }
6466 }
6567
6668 map.locations .at (Point{ 2 , 3 }).draw = solid_draw;
6769 map.locations .at (Point{ 2 , 3 }).can_enter = cannot_enter;
6870 map.locations .at (Point{ 1 , 4 }).draw = solid_draw;
6971 map.locations .at (Point{ 1 , 4 }).can_enter = cannot_enter;
7072 map.locations .at (Point{ 0 , 2 }).draw = solid_draw;
71- map.locations .at (Point{ 0 , 2 }).can_enter = [](std::chrono::milliseconds , Point, Direction direction) {
73+ map.locations .at (Point{ 0 , 2 }).can_enter = [](const Game & , Point, Direction direction) {
7274 return direction == Direction::South;
7375 };
7476
7577
7678 return map;
7779}
7880
79- void draw (Bitmap &viewport,
80- Size tile_size,
81- Point map_center,
82- std::chrono::milliseconds time,
83- const Game_Map &map,
84- const Character &character)
81+ Game make_game ()
82+ {
83+ Game retval;
84+ retval.maps .emplace (" main" , make_map ());
85+ retval.current_map = " main" ;
86+ retval.tile_size = Size{ 8 , 8 };// NOLINT Magic Number
87+
88+ Character player;
89+ player.draw = [player_bitmap = load_png (" player.png" )]([[maybe_unused]] Vector2D_Span<Color> &pixels,
90+ [[maybe_unused]] const Game &game,
91+ [[maybe_unused]] Point map_location) {
92+ // with with a fully saturated red at 50% alpha
93+ for (std::size_t cur_x = 0 ; cur_x < pixels.size ().width ; ++cur_x) {
94+ for (std::size_t cur_y = 0 ; cur_y < pixels.size ().height ; ++cur_y) {
95+ pixels.at (Point{ cur_x, cur_y }) += player_bitmap.at (Point{ cur_x, cur_y });
96+ }
97+ }
98+ };
99+
100+
101+ retval.player = player;
102+
103+ return retval;
104+ }
105+
106+
107+ void draw (Bitmap &viewport, Point map_center, const Game &game, const Game_Map &map)
85108{
86- const auto num_wide = viewport.pixels .size ().width / tile_size.width ;
87- const auto num_high = viewport.pixels .size ().width / tile_size.height ;
109+ const auto num_wide = viewport.pixels .size ().width / game. tile_size .width ;
110+ const auto num_high = viewport.pixels .size ().width / game. tile_size .height ;
88111
89112 const auto x_offset = num_wide / 2 ;
90113 const auto y_offset = num_high / 2 ;
@@ -102,27 +125,34 @@ void draw(Bitmap &viewport,
102125
103126 for (std::size_t cur_x = 0 ; cur_x < num_wide; ++cur_x) {
104127 for (std::size_t cur_y = 0 ; cur_y < num_high; ++cur_y) {
105- auto span = Vector2D_Span<Color>(Point{ cur_x * tile_size.width , cur_y * tile_size.width }, tile_size, viewport.pixels );
128+ auto span = Vector2D_Span<Color>(
129+ Point{ cur_x * game.tile_size .width , cur_y * game.tile_size .width }, game.tile_size , viewport.pixels );
106130 const auto map_location = Point{ cur_x, cur_y } + upper_left_map_location;
107- map.locations .at (map_location).draw (span, time , map_location);
131+ map.locations .at (map_location).draw (span, game , map_location);
108132 }
109133 }
110134
111- const auto character_relative_location = character.map_location - upper_left_map_location;
135+ const auto character_relative_location = game.player .map_location - upper_left_map_location;
136+
137+ const auto character_location = Point{ character_relative_location.x * game.tile_size .width ,
138+ character_relative_location.y * game.tile_size .height };
112139
113- const auto character_location =
114- Point{ character_relative_location.x * tile_size.width , character_relative_location.y * tile_size.height };
140+ auto character_span = Vector2D_Span<Color>(character_location, game.tile_size , viewport.pixels );
115141
116- auto character_span = Vector2D_Span<Color>(character_location, tile_size, viewport.pixels );
142+ game.player .draw (character_span, game, game.player .map_location );
143+ }
117144
118- character.draw (character_span, time, character.map_location );
145+ void draw (Bitmap &viewport, const Game &game)
146+ {
147+ if (game.maps .contains (game.current_map )) {
148+ draw (viewport, game.player .map_location , game, game.maps .at (game.current_map ));
149+ }
119150}
120151
121152void game_iteration_canvas ()
122153{
123- const auto map = make_map ();
154+ auto game = make_game ();
124155
125- static constexpr auto Tile_Size = Size{ 8 , 8 };
126156
127157 // this should probably have a `bitmap` helper function that does what you expect
128158 // similar to the other parts of FTXUI
@@ -132,19 +162,6 @@ void game_iteration_canvas()
132162 double fps = 0 ;
133163 auto start_time = std::chrono::steady_clock::now ();
134164
135- const auto player_bitmap = load_png (" player.png" );
136-
137- Character player;
138- player.draw = [&]([[maybe_unused]] Vector2D_Span<Color> &pixels,
139- [[maybe_unused]] std::chrono::milliseconds game_clock,
140- [[maybe_unused]] Point map_location) {
141- // with with a fully saturated red at 50% alpha
142- for (std::size_t cur_x = 0 ; cur_x < pixels.size ().width ; ++cur_x) {
143- for (std::size_t cur_y = 0 ; cur_y < pixels.size ().height ; ++cur_y) {
144- pixels.at (Point{ cur_x, cur_y }) += player_bitmap.at (Point{ cur_x, cur_y });
145- }
146- }
147- };
148165
149166 ftxui::Event last_event;
150167 ftxui::Event current_event;
@@ -161,9 +178,11 @@ void game_iteration_canvas()
161178 const auto game_clock =
162179 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now () - start_time);
163180
181+ game.clock = game_clock;
182+
164183 [&] {
165184 if (current_event != last_event) {
166- auto location = player.map_location ;
185+ auto location = game. player .map_location ;
167186 Direction from{};
168187
169188 if (current_event == ftxui::Event::ArrowUp) {
@@ -182,13 +201,14 @@ void game_iteration_canvas()
182201 return ;
183202 }
184203
185- if (map.can_enter_from (game_clock, location, from)) { player.map_location = location; }
204+ if (game.maps .at (game.current_map ).can_enter_from (game, location, from)) {
205+ game.player .map_location = location;
206+ }
186207 }
187208 }();
188209
189210
190- // just draw the map
191- draw (*bm, Tile_Size, player.map_location , game_clock, map, player);
211+ draw (*bm, game);
192212 };
193213
194214 auto screen = ftxui::ScreenInteractive::TerminalOutput ();
0 commit comments