Skip to content

Commit b0adec1

Browse files
committed
Added map engine into map test. More map engine features.
1 parent 107f018 commit b0adec1

File tree

6 files changed

+78
-111
lines changed

6 files changed

+78
-111
lines changed

CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ cmake_minimum_required(VERSION 3.20)
33
project(Abyss)
44

55
if (NOT CMAKE_BUILD_TYPE)
6-
message(STATUS "No build type selected, defaulting to Release")
7-
set(CMAKE_BUILD_TYPE Release)
6+
message(STATUS "No build type selected, defaulting to Release")
7+
set(CMAKE_BUILD_TYPE Release)
88
endif ()
99

10+
1011
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
1112
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
1213
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
@@ -32,9 +33,9 @@ include_stormlib()
3233
include_casclib()
3334

3435
if (APPLE)
35-
find_library(OSX_VIDEOTOOLBOX VideoToolbox)
36-
find_library(OSX_COREMEDIA CoreMedia)
37-
find_library(OSX_SECURITY Security)
36+
find_library(OSX_VIDEOTOOLBOX VideoToolbox)
37+
find_library(OSX_COREMEDIA CoreMedia)
38+
find_library(OSX_SECURITY Security)
3839
endif ()
3940

4041
add_subdirectory(src)

src/Abyss/MapEngine/MapEngine.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
#include <future>
66

77
namespace Abyss::MapEngine {
8-
MapEngine::MapEngine(DataTypes::Palette _palette, const int width, const int height, std::vector<DataTypes::DT1> dt1s,
9-
std::vector<DataTypes::DS1> ds1s)
10-
: _width(width), _height(height), _palette(std::move(_palette)), _dt1s(std::move(dt1s)), _ds1s(std::move(ds1s)) {
8+
MapEngine::MapEngine(const int width, const int height, std::vector<DataTypes::DT1> dt1s, std::vector<DataTypes::DS1> ds1s)
9+
: _width(width), _height(height), _dt1s(std::move(dt1s)), _ds1s(std::move(ds1s)) {
1110

1211
std::vector<std::future<void>> futures;
1312

@@ -171,13 +170,18 @@ void MapEngine::render() const {
171170
}
172171

173172
void MapEngine::setCameraPosition(int x, int y) {
174-
_cameraPosition.x = x;
175-
_cameraPosition.y = y;
173+
_cameraPosition.x = x - 320;
174+
_cameraPosition.y = y - 260;
176175
}
177176

178177
void MapEngine::getCameraPosition(int &x, int &y) const {
179-
x = _cameraPosition.x;
180-
y = _cameraPosition.y;
178+
x = _cameraPosition.x + 320;
179+
y = _cameraPosition.y + 260;
180+
}
181+
182+
void MapEngine::getMapSize(int &width, int &height) const {
183+
width = _width;
184+
height = _height;
181185
}
182186

183187

src/Abyss/MapEngine/MapEngine.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "Abyss/DataTypes/DS1.h"
44
#include "Abyss/DataTypes/DT1.h"
5-
#include "Abyss/DataTypes/Palette.h"
65

76
#include <vector>
87
#include <string>
@@ -13,10 +12,9 @@ namespace Abyss::MapEngine {
1312
class MapEngine {
1413
const int _width{};
1514
const int _height{};
16-
const DataTypes::Palette _palette;
1715
std::vector<DataTypes::DT1> _dt1s;
1816
std::vector<DataTypes::DS1> _ds1s;
19-
SDL_Point _cameraPosition{0, 0};
17+
SDL_Point _cameraPosition{-320, -260};
2018

2119
struct {
2220
std::vector<DataTypes::TileMap> floor{};
@@ -26,11 +24,11 @@ class MapEngine {
2624
} _layers;
2725

2826
public:
29-
MapEngine(DataTypes::Palette _palette, int width, int height, std::vector<DataTypes::DT1> dt1s,
30-
std::vector<DataTypes::DS1> ds1s);
27+
MapEngine(int width, int height, std::vector<DataTypes::DT1> dt1s, std::vector<DataTypes::DS1> ds1s);
3128
void stampDs1(uint32_t ds1Index, int originX, int originY);
3229
void render() const;
3330
void setCameraPosition(int x, int y);
3431
void getCameraPosition(int &x, int &y) const;
32+
void getMapSize(int &width, int &height) const;
3533
};
3634
} // namespace Abyss::MapEngine

src/OD2/Scenes/MapTest/MapTest.cpp

Lines changed: 54 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ MapTest::MapTest() {
1919
continue;
2020
_mapSelections.push_back(levelName);
2121
}
22+
23+
_selectedLevelName = _mapSelections.front();
24+
onLevelChanged(_selectedLevelName);
25+
if (!_selectedLevelAltName.empty()) {
26+
_selectedLevelAltName = _mapAltSelections.front();
27+
loadTile(_selectedLevelAltName);
28+
}
2229
}
2330

2431
void MapTest::onLevelChanged(const std::string &levelName) {
@@ -94,7 +101,7 @@ void MapTest::processEvent(const SDL_Event &event) {
94101
Abyss::AbyssEngine::getInstance().getMouseState().getPosition(mx, my);
95102
_mousePressedPosition.x = mx;
96103
_mousePressedPosition.y = my;
97-
_startCameraPosition = _cameraPosition;
104+
_mapEngine->getCameraPosition(_startCameraPosition.x, _startCameraPosition.y);
98105
SDL_CaptureMouse(SDL_TRUE);
99106
break;
100107
}
@@ -113,8 +120,8 @@ void MapTest::processEvent(const SDL_Event &event) {
113120
int mx;
114121
int my;
115122
Abyss::AbyssEngine::getInstance().getMouseState().getPosition(mx, my);
116-
_cameraPosition.x = _startCameraPosition.x + (_mousePressedPosition.x - mx);
117-
_cameraPosition.y = _startCameraPosition.y + (_mousePressedPosition.y - my);
123+
_mapEngine->setCameraPosition(_startCameraPosition.x + (_mousePressedPosition.x - mx),
124+
_startCameraPosition.y + (_mousePressedPosition.y - my));
118125
}
119126
break;
120127
}
@@ -131,7 +138,6 @@ void MapTest::loadTile(const std::string &altName) {
131138
const auto &levelType = getLevelType(levelTypeId);
132139
const auto &palette = Common::PaletteManager::getInstance().getPalette("Act" + std::to_string(act));
133140

134-
_dt1s.clear();
135141
std::vector<std::string> dt1sToLoad{};
136142

137143
for (int i = 1; i <= 31; ++i) {
@@ -141,15 +147,14 @@ void MapTest::loadTile(const std::string &altName) {
141147
if (dt1 != "None" && dt1 != "Expansion" && dt1 != "0" && std::ranges::find(dt1sToLoad, dt1) == dt1sToLoad.end()) {
142148
auto filePath = "/data/global/tiles/" + dt1;
143149
std::ranges::transform(filePath, filePath.begin(), [](const auto c) { return std::tolower(c); });
144-
_dt1s.emplace_back("/data/global/tiles/" + dt1, palette);
145150
dt1sToLoad.push_back("/data/global/tiles/" + dt1);
146151
}
147152
}
148153

149-
_ds1 = std::make_unique<Abyss::DataTypes::DS1>("/data/global/tiles/" + altName);
154+
Abyss::DataTypes::DS1 ds1("/data/global/tiles/" + altName);
150155

151156
// Add all the DS1 files to the DT1s
152-
for (auto &file : _ds1->files) {
157+
for (auto &file : ds1.files) {
153158
std::ranges::replace(file, '\\', '/');
154159
if (file.starts_with("/d2/")) // Because why not?
155160
file = file.substr(3);
@@ -161,99 +166,43 @@ void MapTest::loadTile(const std::string &altName) {
161166
if (file.starts_with("/d2/"))
162167
file = file.substr(3);
163168

164-
_dt1s.emplace_back(file, palette);
165169
dt1sToLoad.push_back(file);
166170
}
167171
}
168172

169-
_ds1->bindTileReferences(_dt1s);
173+
const auto dsSubIndex = std::ranges::find(_mapAltSelections, altName) - _mapAltSelections.begin();
174+
// Load all dt1s into a vector
175+
std::vector<Abyss::DataTypes::DT1> dt1s{};
176+
dt1s.reserve(dt1sToLoad.size());
177+
for (const auto &dt1 : dt1sToLoad)
178+
dt1s.emplace_back(dt1, palette);
179+
180+
const auto mapWidth = ds1.width;
181+
const auto mapHeight = ds1.height;
182+
183+
_mapEngine = std::make_unique<Abyss::MapEngine::MapEngine>(mapWidth, mapHeight, std::move(dt1s), std::vector{std::move(ds1)});
184+
_mapEngine->stampDs1(0, 0, 0);
170185

171-
// // Center the map
172-
const auto mapCenterX = _ds1->width / 2;
173-
const auto mapCenterY = _ds1->height / 2;
174-
//
175-
// // Convert center to ortho
186+
// Center the map
187+
const auto mapCenterX = mapWidth / 2;
188+
const auto mapCenterY = mapHeight / 2;
189+
190+
// Convert center to ortho
176191
const auto orthoCenterX = (mapCenterX - mapCenterY) * 80;
177192
const auto orthoCenterY = (mapCenterX + mapCenterY) * 40;
178193

179194
// Adjust camera position
180-
_cameraPosition.x = orthoCenterX - 320;
181-
_cameraPosition.y = orthoCenterY - 260;
182-
195+
_mapEngine->setCameraPosition(orthoCenterX, orthoCenterY);
183196
}
184197

185198
void MapTest::render() {
186-
const auto &renderer = Abyss::AbyssEngine::getInstance().getRenderer();
187-
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
188-
SDL_RenderClear(renderer);
189-
190-
if (_ds1) {
191-
for (int tileY = 0; tileY < _ds1->height; ++tileY) {
192-
for (int tileX = 0; tileX < _ds1->width; ++tileX) {
193-
const auto posX = (tileX - tileY) * 80;
194-
const auto posY = (tileX + tileY) * 40;
195-
196-
// Draw lower walls
197-
for (const auto &layer : _ds1->layers.wall) {
198-
if (const auto &tile = layer[tileX + tileY * _ds1->width];
199-
tile.dt1Ref && tile.type >= Abyss::DataTypes::TileType::LowerWallsEquivalentToLeftWall)
200-
tile.dt1Ref->drawTile(posX - _cameraPosition.x, posY - _cameraPosition.y, tile.dt1Index);
201-
}
199+
SDL_SetRenderDrawColor(Abyss::AbyssEngine::getInstance().getRenderer(), 0, 0, 0, 255);
200+
SDL_RenderClear(Abyss::AbyssEngine::getInstance().getRenderer());
202201

203-
// Draw floors
204-
for (const auto &layer : _ds1->layers.floor) {
205-
if (const auto &tile = layer[tileX + tileY * _ds1->width]; tile.dt1Ref)
206-
tile.dt1Ref->drawTile(posX - _cameraPosition.x, posY - _cameraPosition.y, tile.dt1Index);
207-
}
208-
// Draw shadows
209-
for (const auto &layer : _ds1->layers.shadow) {
210-
if (const auto &tile = layer[tileX + tileY * _ds1->width]; tile.dt1Ref)
211-
tile.dt1Ref->drawTile(posX - _cameraPosition.x, posY - _cameraPosition.y, tile.dt1Index);
212-
}
213-
}
214-
}
215-
216-
for (int tileY = 0; tileY < _ds1->height; ++tileY) {
217-
for (int tileX = 0; tileX < _ds1->width; ++tileX) {
218-
const auto posX = (tileX - tileY) * 80;
219-
const auto posY = (tileX + tileY) * 40;
220-
221-
// Draw upper
222-
for (const auto &layer : _ds1->layers.wall) {
223-
if (const auto &tile = layer[tileX + tileY * _ds1->width];
224-
tile.dt1Ref && ((tile.type >= Abyss::DataTypes::TileType::LeftWall && tile.type <=
225-
Abyss::DataTypes::TileType::RightWallWithDoor) || (
226-
tile.type >= Abyss::DataTypes::TileType::PillarsColumnsAndStandaloneObjects && tile.type <=
227-
Abyss::DataTypes::TileType::Tree))) {
228-
tile.dt1Ref->drawTile(posX - _cameraPosition.x, posY - _cameraPosition.y + 96, tile.dt1Index);
229-
230-
// Super special condition. This was fun to figure out :(
231-
if (tile.type == Abyss::DataTypes::TileType::RightPartOfNorthCornerWall)
232-
tile.dt1Ref->drawTile(posX - _cameraPosition.x, posY - _cameraPosition.y + 96, tile.dt1IndexAlt);
233-
}
234-
235-
}
202+
if (_mapEngine)
203+
_mapEngine->render();
236204

237-
}
238-
}
239-
240-
for (int tileY = 0; tileY < _ds1->height; ++tileY) {
241-
for (int tileX = 0; tileX < _ds1->width; ++tileX) {
242-
const auto posX = (tileX - tileY) * 80;
243-
const auto posY = (tileX + tileY) * 40;
244-
245-
// Draw Roof
246-
for (const auto &layer : _ds1->layers.wall) {
247-
if (const auto &tile = layer[tileX + tileY * _ds1->width];
248-
tile.dt1Ref && tile.type == Abyss::DataTypes::TileType::Roof)
249-
tile.dt1Ref->drawTile(posX - _cameraPosition.x, posY - _cameraPosition.y - 72, tile.dt1Index);
250-
}
251-
252-
}
253-
}
254-
}
255-
256-
ImGui::SetNextWindowSize(ImVec2(250, 200), ImGuiCond_FirstUseEver);
205+
ImGui::SetNextWindowSize(ImVec2(350, 200), ImGuiCond_FirstUseEver);
257206
ImGui::Begin("Debug");
258207

259208
// Level selection
@@ -264,7 +213,9 @@ void MapTest::render() {
264213
if (_selectedLevelName != levelName) {
265214
onLevelChanged(levelName);
266215
_selectedLevelName = levelName;
267-
_selectedLevelAltName = "";
216+
_selectedLevelAltName = _mapAltSelections.empty() ? "" : _mapAltSelections.front();
217+
if (!_selectedLevelAltName.empty())
218+
loadTile(_selectedLevelAltName);
268219
}
269220

270221
if (isSelected)
@@ -291,6 +242,22 @@ void MapTest::render() {
291242
}
292243
}
293244

245+
ImGui::Separator();
246+
247+
// Show map information
248+
if (_mapEngine) {
249+
int mapWidth;
250+
int mapHeight;
251+
_mapEngine->getMapSize(mapWidth, mapHeight);
252+
int cameraPosX;
253+
int cameraPosY;
254+
_mapEngine->getCameraPosition(cameraPosX, cameraPosY);
255+
256+
ImGui::LabelText("Map size", "%d, %d", mapWidth, mapHeight);
257+
ImGui::LabelText("Camera position", "%d, %d", cameraPosX, cameraPosY);
258+
ImGui::LabelText("DS1", "%s", _selectedLevelAltName.c_str());
259+
}
260+
294261
ImGui::End();
295262
}
296263

src/OD2/Scenes/MapTest/MapTest.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

33
#include "Abyss/Common/Scene.h"
4-
#include "Abyss/DataTypes/DS1.h"
5-
#include "Abyss/DataTypes/DT1.h"
4+
#include "Abyss/MapEngine/MapEngine.h"
65
#include "OD2/Common/DataTableManager.h"
76

87
#include <memory>
@@ -16,13 +15,11 @@ class MapTest final : public Abyss::Common::Scene {
1615
std::vector<std::string> _mapAltSelections{};
1716
std::string _selectedLevelName{};
1817
std::string _selectedLevelAltName{};
19-
std::vector<Abyss::DataTypes::DT1> _dt1s;
20-
std::unique_ptr<Abyss::DataTypes::DS1> _ds1{};
21-
SDL_Point _cameraPosition{0, 0};
2218
SDL_Point _mousePressedPosition{0, 0};
2319
SDL_Point _startCameraPosition{0, 0};
24-
2520
bool _isMouseDragging{false};
21+
std::unique_ptr<Abyss::MapEngine::MapEngine> _mapEngine;
22+
int _mapWidth;
2623

2724
void onLevelChanged(const std::string &levelName);
2825
static const Common::DataTableRow &getLevelPrest(std::string_view name);

src/OD2/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ int main(const int argc, char **argv) {
201201
engine.setScene(std::make_unique<OD2::Scenes::MainMenu::MainMenu>());
202202

203203
Abyss::Common::Log::info("Startup complete");
204-
engine.setMasterVolumeLevel(0.0f);
204+
//engine.setMasterVolumeLevel(0.0f);
205205

206206
engine.run();
207207
} catch (const std::exception &exception) {

0 commit comments

Comments
 (0)