Skip to content

Commit

Permalink
Clear minimap on level load (#1207)
Browse files Browse the repository at this point in the history
The minimap wasn't being cleared on level load. VICT.TR2 made this obvious as it had no rooms to to be loaded, so the old room display was still there.

Now when the level loads the minimap is cleared. Some functions to access room info directly from the level have been removed as they are no longer used. It will now take it from the `Sector` instead of using inidces.

Closes #1206
  • Loading branch information
chreden authored Feb 10, 2024
1 parent 4fe80bc commit 1ed861c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 42 deletions.
5 changes: 4 additions & 1 deletion trview.app.tests/Windows/ViewerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,5 +1089,8 @@ TEST(Viewer, SectorHighlightForwarded)
auto viewer = register_test_module().with_ui(std::move(ui_ptr)).with_sector_highlight(std::move(sector_highlight_ptr)).build();

viewer->open(level, ILevel::OpenMode::Full);
ui.on_sector_hover(mock_shared<MockSector>());

auto room = mock_shared<MockRoom>();
auto sector = mock_shared<MockSector>()->with_room(room);
ui.on_sector_hover(sector);
}
2 changes: 0 additions & 2 deletions trview.app/Elements/ILevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ namespace trview
virtual void render_transparency(const ICamera& camera) = 0;
// Returns the room with ID provided
virtual std::weak_ptr<IRoom> room(uint32_t id) const = 0;
virtual std::vector<RoomInfo> room_info() const = 0;
virtual RoomInfo room_info(uint32_t room) const = 0;
virtual std::vector<std::weak_ptr<IRoom>> rooms() const = 0;
virtual std::optional<uint32_t> selected_item() const = 0;
virtual std::optional<uint32_t> selected_light() const = 0;
Expand Down
15 changes: 0 additions & 15 deletions trview.app/Elements/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ namespace trview
_sampler_state = device->create_sampler_state(sampler_desc);
}

std::vector<RoomInfo> Level::room_info() const
{
std::vector<RoomInfo> room_infos;
for (const auto& r : _rooms)
{
room_infos.push_back(r->info());
}
return room_infos;
}

RoomInfo Level::room_info(uint32_t room) const
{
return _rooms[room]->info();
}

std::vector<graphics::Texture> Level::level_textures() const
{
std::vector<graphics::Texture> textures;
Expand Down
2 changes: 0 additions & 2 deletions trview.app/Elements/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ namespace trview
const std::shared_ptr<ILog>& log,
const graphics::IBuffer::ConstantSource& buffer_source);
virtual ~Level() = default;
virtual std::vector<RoomInfo> room_info() const override;
virtual RoomInfo room_info(uint32_t room) const override;
virtual std::vector<graphics::Texture> level_textures() const override;
virtual std::optional<uint32_t> selected_item() const override;
virtual uint16_t selected_room() const override;
Expand Down
2 changes: 0 additions & 2 deletions trview.app/Mocks/Elements/ILevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace trview
MOCK_METHOD(void, render, (const ICamera&, bool), (override));
MOCK_METHOD(void, render_transparency, (const ICamera&), (override));
MOCK_METHOD(std::weak_ptr<IRoom>, room, (uint32_t), (const, override));
MOCK_METHOD(std::vector<RoomInfo>, room_info, (), (const, override));
MOCK_METHOD(RoomInfo, room_info, (uint32_t), (const, override));
MOCK_METHOD(std::vector<std::weak_ptr<IRoom>>, rooms, (), (const, override));
MOCK_METHOD(std::optional<uint32_t>, selected_item, (), (const, override));
MOCK_METHOD(std::optional<uint32_t>, selected_light, (), (const, override));
Expand Down
22 changes: 13 additions & 9 deletions trview.app/UI/MapRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace trview
void
MapRenderer::render()
{
if (!_render_target || !_visible)
if (!_render_target || !_visible || !_loaded)
{
return;
}
Expand Down Expand Up @@ -146,28 +146,32 @@ namespace trview
_sprite->render(_texture, p.x, p.y, s.width, s.height, c);
}

void
MapRenderer::load(const std::shared_ptr<trview::IRoom>& room)
void MapRenderer::load(const std::shared_ptr<trview::IRoom>& room)
{
_tiles.clear();
_previous_sector.reset();
on_sector_hover(nullptr);
_force_redraw = true;

if (!room)
{
_loaded = false;
return;
}

// Set window position and size
_columns = room->num_x_sectors();
_rows = room->num_z_sectors();
_loaded = true;
update_map_position();
update_map_render_target();

// Load up sectors
_tiles.clear();

const auto& sectors = room->sectors();
std::for_each(sectors.begin(), sectors.end(),
[&] (const auto& sector)
{
_tiles.emplace_back(sector, get_position(*sector), get_size());
});

_previous_sector.reset();
on_sector_hover(nullptr);
}

Point MapRenderer::get_position(const ISector& sector)
Expand Down
1 change: 1 addition & 0 deletions trview.app/UI/ViewerUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ namespace trview
{
_level = level;
_level_info->set_level(name);
_map_renderer->load({});
if (auto new_level = _level.lock())
{
_level_info->set_level_version(new_level->version());
Expand Down
27 changes: 16 additions & 11 deletions trview.app/Windows/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,13 @@ namespace trview

if (open_mode == ILevel::OpenMode::Full || !old_level)
{
// Strip the last part of the path away.
const auto filename = new_level->filename();
auto last_index = std::min(filename.find_last_of('\\'), filename.find_last_of('/'));
auto name = last_index == filename.npos ? filename : filename.substr(std::min(last_index + 1, filename.size()));
_ui->set_level(name, new_level);
window().set_title("trview - " + name);

_camera.reset();
_ui->set_toggle(Options::highlight, false);
_ui->set_toggle(Options::flip, false);
Expand All @@ -661,16 +668,9 @@ namespace trview
{
_ui->set_selected_room(rooms[new_level->selected_room()].lock());
}

auto selected_item = new_level->selected_item();
_ui->set_selected_item(selected_item.value_or(0));

// Strip the last part of the path away.
const auto filename = new_level->filename();
auto last_index = std::min(filename.find_last_of('\\'), filename.find_last_of('/'));
auto name = last_index == filename.npos ? filename : filename.substr(std::min(last_index + 1, filename.size()));
_ui->set_level(name, new_level);
window().set_title("trview - " + name);
}
else if (open_mode == ILevel::OpenMode::Reload && old_level)
{
Expand Down Expand Up @@ -1418,13 +1418,18 @@ namespace trview

void Viewer::set_sector_highlight(const std::shared_ptr<ISector>& sector)
{
const auto level = _level.lock();
if (!level)
if (!sector)
{
return;
}

const auto room_info = level->room_info(level->selected_room());
const auto room = sector->room().lock();
if (!room)
{
return;
}

const auto room_info = room->info();
_sector_highlight->set_sector(sector,
Matrix::CreateTranslation(room_info.x / trlevel::Scale_X, 0, room_info.z / trlevel::Scale_Z));
_scene_changed = true;
Expand Down

0 comments on commit 1ed861c

Please sign in to comment.