Skip to content
Snippets Groups Projects
Commit 8f3e3495 authored by David Maul's avatar David Maul :crab:
Browse files

cleanup clang-tidy warnings

parent 3288c79f
Branches clang-tidy-cleanup
No related tags found
1 merge request!48cleanup clang-tidy warnings
Showing
with 203 additions and 216 deletions
......@@ -17,7 +17,7 @@ ZoomInButton::ZoomInButton(QWidget* parent) : QPushButton("+", parent)
setFixedWidth(20);
}
void ZoomInButton::mousePressEvent(QMouseEvent* event)
void ZoomInButton::mousePressEvent(QMouseEvent* /*event*/)
{
EventHandler::send([](EventHandler* e) { e->onZoomed(0.25); });
}
......
......@@ -10,19 +10,21 @@
namespace editor
{
ZoomInfo::ZoomInfo(QWidget* parent) : QPushButton(parent), current_scale(2)
ZoomInfo::ZoomInfo(QWidget* parent) : QPushButton(parent), m_currentScale(2)
{
setDisabled(true);
setText((std::to_string((int)std::round(current_scale * 100)) + "%").c_str());
setText((std::to_string((int)std::round(m_currentScale * 100)) + "%").c_str());
setFixedWidth(50);
}
void ZoomInfo::onZoomed(double delta)
{
if (current_scale + delta <= 0)
if (m_currentScale + delta <= 0)
{
return;
current_scale += delta;
setText((std::to_string((int)std::round(current_scale * 100)) + " %").c_str());
}
m_currentScale += delta;
setText((std::to_string((int)std::round(m_currentScale * 100)) + " %").c_str());
}
} // namespace editor
\ No newline at end of file
......@@ -14,11 +14,13 @@
namespace editor
{
class ZoomInfo : public QPushButton, public EventHandler {
class ZoomInfo : public QPushButton, public EventHandler
{
public:
ZoomInfo(QWidget* parent = nullptr);
private:
double current_scale;
double m_currentScale;
void onZoomed(double delta) override;
};
......
......@@ -17,7 +17,7 @@ ZoomOutButton::ZoomOutButton(QWidget* parent) : QPushButton("-", parent)
setFixedWidth(20);
}
void ZoomOutButton::mousePressEvent(QMouseEvent* event)
void ZoomOutButton::mousePressEvent(QMouseEvent* /*event*/)
{
EventHandler::send([](EventHandler* e) { e->onZoomed(-0.25); });
}
......
......@@ -44,7 +44,7 @@ int main(int argc, char* argv[])
// to get the QGraphicsPixmap for a specific tile.
SpriteProvider::initialize("../res/spritesheet.h5");
LevelScene* level;
LevelScene* level = nullptr;
if (argc == 2)
{ // 1 argument provided => create Level from file
level = LevelScene::fromFile(argv[1]);
......
......@@ -137,25 +137,25 @@ CombatEngine::getUnitsInRangeWithDamagePotential(Unit& attacker, std::vector<Uni
return unitsInRangeWithDamage;
}
void CombatEngine::handleAttackingEvents(Engine& engine, SDL_Event& event, Level& m_level)
void CombatEngine::handleAttackingEvents(Engine& engine, SDL_Event& event, Level& level)
{
if (m_level.getAttackableUnitIds().empty())
if (level.getAttackableUnitIds().empty())
{
std::cout << "No units are within attack range." << "\n";
m_level.setState(LevelState::MENUACTIVE_STATE);
level.setState(LevelState::MENUACTIVE_STATE);
return; // Early exit if no units to attack
}
switch (event.type)
{
case SDL_KEYDOWN:
m_level.handlePositionMarker(engine, event);
level.handlePositionMarker(engine, event);
if (event.key.keysym.sym == SDLK_ESCAPE)
{
m_level.setState(LevelState::MENUACTIVE_STATE);
level.setState(LevelState::MENUACTIVE_STATE);
}
if (event.key.keysym.sym == SDLK_RETURN)
{
handleAttack(m_level.getTilemarker().getPosition(), m_level);
handleAttack(level.getTilemarker().getPosition(), level);
}
break;
default:
......@@ -163,15 +163,14 @@ void CombatEngine::handleAttackingEvents(Engine& engine, SDL_Event& event, Level
}
}
void CombatEngine::handleAttack(std::pair<int, int> tilePos, Level& m_level)
void CombatEngine::handleAttack(std::pair<int, int> tilePos, Level& level)
{
std::unordered_map<int, std::unique_ptr<Unit>>& units = m_level.getUnits();
int selectedUnit = m_level.getSelectedUnit();
int targetedUnit = m_level.selectUnit(tilePos.first, tilePos.second);
std::unordered_map<int, std::unique_ptr<Unit>>& units = level.getUnits();
int selectedUnit = level.getSelectedUnit();
int targetedUnit = level.selectUnit(tilePos.first, tilePos.second);
if (targetedUnit >= 0)
{
if (units.at(m_level.getSelectedUnit())->getFaction() ==
units.at(targetedUnit)->getFaction())
if (units.at(level.getSelectedUnit())->getFaction() == units.at(targetedUnit)->getFaction())
{
std::cout << "You cannot attack your allies!" << "\n";
return;
......@@ -188,7 +187,7 @@ void CombatEngine::handleAttack(std::pair<int, int> tilePos, Level& m_level)
std::unique_ptr<Unit>& attacking = itAttacker->second;
std::unique_ptr<Unit>& defending = itDefender->second;
std::unordered_set<int>& attackableUnitIds = m_level.getAttackableUnitIds();
std::unordered_set<int>& attackableUnitIds = level.getAttackableUnitIds();
if (attackableUnitIds.find(targetedUnit) != attackableUnitIds.end())
{
attack(*attacking, *defending);
......@@ -196,7 +195,7 @@ void CombatEngine::handleAttack(std::pair<int, int> tilePos, Level& m_level)
// attacking->attack(defending);
if (attacking->getHealth() <= 0)
{
m_level.removeUnit(selectedUnit);
level.removeUnit(selectedUnit);
}
else
{
......@@ -204,12 +203,12 @@ void CombatEngine::handleAttack(std::pair<int, int> tilePos, Level& m_level)
}
if (defending->getHealth() <= 0)
{
m_level.removeUnit(targetedUnit);
level.removeUnit(targetedUnit);
}
m_level.setSelectedUnit(-1);
m_level.setShowAttackableTiles(false);
m_level.setShowReachableTiles(false);
m_level.setState(LevelState::SELECTING_STATE);
level.setSelectedUnit(-1);
level.setShowAttackableTiles(false);
level.setShowReachableTiles(false);
level.setState(LevelState::SELECTING_STATE);
}
else
{
......
......@@ -14,8 +14,8 @@ class CombatEngine
CombatEngine();
~CombatEngine() = default;
void handleAttackingEvents(Engine& engine, SDL_Event& event, Level& m_level);
void handleAttack(std::pair<int, int> tilePos, Level& m_level);
void handleAttackingEvents(Engine& engine, SDL_Event& event, Level& level);
void handleAttack(std::pair<int, int> tilePos, Level& level);
static void attack(Unit& attacker, Unit& target);
static int calculateDamage(Unit& attacker, Unit& target);
static void takeDamage(Unit& attacker, Unit& target, int damage);
......
......@@ -2,7 +2,7 @@
namespace advanced_wars
{
Weapon::Weapon() : m_name(""), m_damage() {}
Weapon::Weapon() {}
Weapon::Weapon(
const std::string& weaponName, const std::unordered_map<UnitTypeId, int>& damageValues)
......
......@@ -21,13 +21,15 @@ Config::Config(std::string filename)
for (const auto& unit : tree.get_child("Units"))
{
if (unit.first != "Unit")
{
continue;
}
const auto& unitData = unit.second;
std::string unit_key = unitData.get<std::string>("<xmlattr>.key");
auto unitKey = unitData.get<std::string>("<xmlattr>.key");
try
{
UnitTypeId unitId = mapUnitKeyToID(unit_key);
UnitTypeId unitId = mapUnitKeyToID(unitKey);
m_unitCosts[unitId] = unitData.get<int>("Cost");
m_unitMovementPoints[unitId] = unitData.get<int>("MovementPoints");
......@@ -35,15 +37,15 @@ Config::Config(std::string filename)
m_unitMinRange[unitId] = unitData.get<int>("minRange", 0);
m_unitMaxRange[unitId] = unitData.get<int>("maxRange", 0);
std::string movement_type_str = unitData.get<std::string>("MovementType");
auto movementTypeStr = unitData.get<std::string>("MovementType");
try
{
m_unitMovementType[unitId] = mapMovementType(movement_type_str);
m_unitMovementType[unitId] = mapMovementType(movementTypeStr);
}
catch (const std::out_of_range& e)
{
std::cerr << "Unknown movement type: " << movement_type_str
<< " for unit key: " << unit_key << "\n";
std::cerr << "Unknown movement type: " << movementTypeStr
<< " for unit key: " << unitKey << "\n";
continue;
}
......@@ -56,10 +58,12 @@ Config::Config(std::string filename)
for (const auto& damage : weapon.second.get_child("DamageTable"))
{
if (damage.first != "Damage")
{
continue;
}
std::string target_key = damage.second.get<std::string>("<xmlattr>.unitId");
UnitTypeId targetId = mapUnitKeyToID(target_key);
auto targetKey = damage.second.get<std::string>("<xmlattr>.unitId");
UnitTypeId targetId = mapUnitKeyToID(targetKey);
m_primaryWeaponDamage[unitId][targetId] =
damage.second.get<int>("<xmlattr>.value");
}
......@@ -72,10 +76,12 @@ Config::Config(std::string filename)
for (const auto& damage : weapon.second.get_child("DamageTable"))
{
if (damage.first != "Damage")
{
continue;
}
std::string target_key = damage.second.get<std::string>("<xmlattr>.unitId");
UnitTypeId targetId = mapUnitKeyToID(target_key);
auto targetKey = damage.second.get<std::string>("<xmlattr>.unitId");
UnitTypeId targetId = mapUnitKeyToID(targetKey);
m_secondaryWeaponDamage[unitId][targetId] =
damage.second.get<int>("<xmlattr>.value");
}
......@@ -90,9 +96,9 @@ Config::Config(std::string filename)
}
}
UnitTypeId Config::mapUnitKeyToID(const std::string& unit_key) const
UnitTypeId Config::mapUnitKeyToID(const std::string& unitKey) const
{
static const std::unordered_map<std::string, UnitTypeId> unit_map = {
static const std::unordered_map<std::string, UnitTypeId> UNIT_MAP = {
{ "infantry", UnitTypeId::INFANTERY},
{ "mechanized_infantry", UnitTypeId::MECHANIZED_INFANTERY},
{ "recon", UnitTypeId::RECON},
......@@ -114,17 +120,17 @@ UnitTypeId Config::mapUnitKeyToID(const std::string& unit_key) const
{ "bomber", UnitTypeId::BOMBER}
};
auto it = unit_map.find(unit_key);
if (it != unit_map.end())
auto it = UNIT_MAP.find(unitKey);
if (it != UNIT_MAP.end())
{
return it->second;
}
throw std::out_of_range("Unknown unit key: " + unit_key);
throw std::out_of_range("Unknown unit key: " + unitKey);
}
MovementType Config::mapMovementType(const std::string& movementTypeStr) const
{
static const std::unordered_map<std::string, MovementType> movement_map = {
static const std::unordered_map<std::string, MovementType> MOVEMENT_MAP = {
{ "Foot", MovementType::FOOT},
{"Wheeled", MovementType::WHEELED},
{ "Tread", MovementType::TREAD},
......@@ -133,8 +139,8 @@ MovementType Config::mapMovementType(const std::string& movementTypeStr) const
{ "Lander", MovementType::LANDER}
};
auto it = movement_map.find(movementTypeStr);
if (it != movement_map.end())
auto it = MOVEMENT_MAP.find(movementTypeStr);
if (it != MOVEMENT_MAP.end())
{
return it->second;
}
......
......@@ -203,7 +203,7 @@ class Config
* @param unit_key The string key representing a unit type.
* @return The corresponding UnitTypeId.
*/
UnitTypeId mapUnitKeyToID(const std::string& unit_key) const;
UnitTypeId mapUnitKeyToID(const std::string& unitKey) const;
/**
* @brief Converts a movement type string from the XML file to its corresponding
......
......@@ -19,13 +19,13 @@ namespace advanced_wars
{
Engine::Engine(Window& window)
: m_window(window), m_quit(false), m_unitConfig("../config.xml"),
: m_window(window),
m_SDLRenderer(SDL_CreateRenderer(
this->m_window.sdlWindow(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)),
m_quit(false), m_stage(0), m_unitConfig("../config.xml"),
m_gameManager(std::make_unique<GameManager>())
{
this->m_SDLRenderer = SDL_CreateRenderer(
this->m_window.sdl_window(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (m_SDLRenderer == nullptr)
{
throw std::runtime_error("SDL could not generate renderer: " + std::string(SDL_GetError()));
......@@ -72,7 +72,7 @@ void Engine::setSpritesheet(Spritesheet& spritesheet)
void Engine::pump()
{
SDL_Event e;
while (SDL_PollEvent(&e))
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
......
......@@ -22,6 +22,7 @@ namespace advanced_wars
{
Spritesheet::Spritesheet(std::string path, Engine& engine)
: m_tileWidth(16), m_tileHeight(16), m_buildingWidth(16), m_buildingHeight(32)
{
HighFive::File file(path, HighFive::File::ReadOnly);
......@@ -60,86 +61,83 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
"cliff_inverse_corner_bottom_right"});
// every sub data set of tiles
for (size_t tile_idx = 0; tile_idx < tiles.size(); tile_idx++)
for (const auto& tile : tiles)
{
HighFive::DataSet units_ds = file.getDataSet("tiles/" + tiles[tile_idx]);
HighFive::DataSet unitsDs = file.getDataSet("tiles/" + tile);
std::vector<std::vector<std::vector<uint32_t>>> tile_frames;
units_ds.read(tile_frames);
std::vector<std::vector<std::vector<uint32_t>>> tileFrames;
unitsDs.read(tileFrames);
std::vector<uint32_t> tile_buffer(16 * 16 * tile_frames.size(), 0);
std::vector<uint32_t> tileBuffer(16 * 16 * tileFrames.size(), 0);
// every animation frame
for (size_t n = 0; n < tile_frames.size(); n++)
for (size_t n = 0; n < tileFrames.size(); n++)
{
for (size_t y = 0; y < 16; y++)
{
for (size_t x = 0; x < 16; x++)
{
size_t index = (y * tile_frames.size() * 16) + (n * 16 + x);
size_t index = (y * tileFrames.size() * 16) + (n * 16 + x);
tile_buffer.at(index) = tile_frames.at(n).at(16 - y - 1).at(x);
tileBuffer.at(index) = tileFrames.at(n).at(16 - y - 1).at(x);
}
}
}
SDL_Texture* tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
tile_frames.size() * 16, 16);
tileFrames.size() * 16, 16);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
if (tmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Units: " + std::string(SDL_GetError()));
"Fehler beim Erstellen der Textur für die Tiles: " + std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(
tmp, NULL, tile_buffer.data(), tile_frames.size() * 16 * sizeof(int32_t)) != 0)
tmp, nullptr, tileBuffer.data(), tileFrames.size() * 16 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Units: " + std::string(SDL_GetError()));
"Fehler beim updaten der Textur für die Tiles: " + std::string(SDL_GetError()));
}
m_tileTextures.push_back(std::pair<SDL_Texture*, int>(tmp, tile_frames.size()));
m_tileTextures.emplace_back(tmp, tileFrames.size());
}
this->m_tileWidth = 16;
this->m_tileHeight = 16;
// Buildings
std::vector<std::string> building_factions(
std::vector<std::string> buildingFactions(
{"red", "blue", "green", "yellow", "purple", "neutral"});
// every sub data set of buildings
for (std::string faction : building_factions)
for (const std::string& faction : buildingFactions)
{
HighFive::DataSet buildings_ds = file.getDataSet("buildings/" + faction);
HighFive::DataSet buildingsDs = file.getDataSet("buildings/" + faction);
std::vector<std::vector<std::vector<uint32_t>>> buildings_frames;
std::vector<std::vector<std::vector<uint32_t>>> buildingsFrames;
buildings_ds.read(buildings_frames);
buildingsDs.read(buildingsFrames);
std::vector<uint32_t> building_buffer(32 * 16 * buildings_frames.size(), 0);
std::vector<uint32_t> buildingBuffer(32 * 16 * buildingsFrames.size(), 0);
// every type of building
for (size_t n = 0; n < buildings_frames.size(); n++)
for (size_t n = 0; n < buildingsFrames.size(); n++)
{
for (size_t y = 0; y < 32; y++)
{
for (size_t x = 0; x < 16; x++)
{
size_t index = (y * buildings_frames.size() * 16) + (n * 16 + x);
size_t index = (y * buildingsFrames.size() * 16) + (n * 16 + x);
building_buffer.at(index) = buildings_frames.at(n).at(32 - y - 1).at(x);
buildingBuffer.at(index) = buildingsFrames.at(n).at(32 - y - 1).at(x);
}
}
}
SDL_Texture* tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
buildings_frames.size() * 16, 32);
buildingsFrames.size() * 16, 32);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
......@@ -151,8 +149,8 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
}
if (SDL_UpdateTexture(
tmp, NULL, building_buffer.data(),
buildings_frames.size() * 16 * sizeof(int32_t)) != 0)
tmp, nullptr, buildingBuffer.data(),
buildingsFrames.size() * 16 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Buildings: " + std::string(SDL_GetError()));
......@@ -161,11 +159,8 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
this->m_buildingTextures.push_back(tmp);
}
this->m_buildingWidth = 16;
this->m_buildingHeight = 32;
// Units
std::vector<std::string> unit_factions({"red", "blue", "green", "yellow", "purple"});
std::vector<std::string> unitFactions({"red", "blue", "green", "yellow", "purple"});
std::vector<std::string> units(
{"infantery", "mechanized_infantery", "recon", "medium_tank", "heavy_tank", "neo_tank",
......@@ -173,53 +168,51 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
"fighter", "bomber", "battle_helicopter", "transport_helicopter", "battleship", "cruiser",
"lander", "submarine"});
std::vector<std::string> unit_states({"idle", "unavailable"});
std::vector<std::string> unit_movement_states({"left", "right", "down", "up"});
std::vector<std::string> unitStates({"idle", "unavailable"});
std::vector<std::string> unitMovementStates({"left", "right", "down", "up"});
// every factions sub data set
for (size_t faction_idx = 0; faction_idx < unit_factions.size(); faction_idx++)
for (size_t factionIdx = 0; factionIdx < unitFactions.size(); factionIdx++)
{
std::string faction = unit_factions.at(faction_idx);
const std::string& faction = unitFactions.at(factionIdx);
// Create entry for units for in a faction
m_unitTextures.push_back(std::vector<std::vector<std::pair<SDL_Texture*, int>>>());
m_unitTextures.emplace_back();
// every unit sub data set
for (size_t unit_idx = 0; unit_idx < units.size(); unit_idx++)
for (size_t unitIdx = 0; unitIdx < units.size(); unitIdx++)
{
std::string unit = units.at(unit_idx);
const std::string& unit = units.at(unitIdx);
// Create entry for states for a unit
m_unitTextures.at(faction_idx).push_back(std::vector<std::pair<SDL_Texture*, int>>());
m_unitTextures.at(factionIdx).emplace_back();
// every state sub data set
for (size_t state_idx = 0; state_idx < unit_states.size(); state_idx++)
for (const auto& unitState : unitStates)
{
std::string unit_state = unit_states.at(state_idx);
HighFive::DataSet unitsDs =
file.getDataSet("units/" + faction + "/" + unit + "/" + unitState);
HighFive::DataSet units_ds =
file.getDataSet("units/" + faction + "/" + unit + "/" + unit_state);
std::vector<std::vector<std::vector<uint32_t>>> unitFrames;
unitsDs.read(unitFrames);
std::vector<std::vector<std::vector<uint32_t>>> unit_frames;
units_ds.read(unit_frames);
std::vector<uint32_t> unitBuffer(16 * 16 * unitFrames.size(), 0);
std::vector<uint32_t> unit_buffer(16 * 16 * unit_frames.size(), 0);
for (size_t n = 0; n < unit_frames.size(); n++)
for (size_t n = 0; n < unitFrames.size(); n++)
{
for (size_t y = 0; y < 16; y++)
{
for (size_t x = 0; x < 16; x++)
{
size_t index = (y * unit_frames.size() * 16) + (n * 16 + x);
size_t index = (y * unitFrames.size() * 16) + (n * 16 + x);
unit_buffer.at(index) = unit_frames.at(n).at(16 - y - 1).at(x);
unitBuffer.at(index) = unitFrames.at(n).at(16 - y - 1).at(x);
}
}
}
SDL_Texture* tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
unit_frames.size() * 16, 16);
unitFrames.size() * 16, 16);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
......@@ -231,49 +224,44 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
}
if (SDL_UpdateTexture(
tmp, NULL, unit_buffer.data(), unit_frames.size() * 16 * sizeof(int32_t)) !=
0)
tmp, nullptr, unitBuffer.data(),
unitFrames.size() * 16 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Units: " +
std::string(SDL_GetError()));
}
m_unitTextures.at(faction_idx)
.at(unit_idx)
.push_back(std::pair<SDL_Texture*, int>(tmp, unit_frames.size()));
m_unitTextures.at(factionIdx).at(unitIdx).emplace_back(tmp, unitFrames.size());
}
// every movement state sub data set
for (size_t movement_state_idx = 0; movement_state_idx < unit_movement_states.size();
movement_state_idx++)
for (const auto& movementState : unitMovementStates)
{
std::string movement_state = unit_movement_states.at(movement_state_idx);
HighFive::DataSet units_ds = file.getDataSet(
"units/" + faction + "/" + unit + "/movement/" + movement_state);
HighFive::DataSet unitsDs =
file.getDataSet("units/" + faction + "/" + unit + "/movement/" + movementState);
std::vector<std::vector<std::vector<uint32_t>>> unit_frames;
units_ds.read(unit_frames);
std::vector<std::vector<std::vector<uint32_t>>> unitFrames;
unitsDs.read(unitFrames);
std::vector<uint32_t> unit_buffer(24 * 24 * unit_frames.size(), 0);
std::vector<uint32_t> unitBuffer(24 * 24 * unitFrames.size(), 0);
for (size_t n = 0; n < unit_frames.size(); n++)
for (size_t n = 0; n < unitFrames.size(); n++)
{
for (size_t y = 0; y < 24; y++)
{
for (size_t x = 0; x < 24; x++)
{
size_t index = (y * unit_frames.size() * 24) + (n * 24 + x);
size_t index = (y * unitFrames.size() * 24) + (n * 24 + x);
unit_buffer.at(index) = unit_frames.at(n).at(24 - y - 1).at(x);
unitBuffer.at(index) = unitFrames.at(n).at(24 - y - 1).at(x);
}
}
}
SDL_Texture* tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
unit_frames.size() * 24, 24);
unitFrames.size() * 24, 24);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
......@@ -285,17 +273,15 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
}
if (SDL_UpdateTexture(
tmp, NULL, unit_buffer.data(), unit_frames.size() * 24 * sizeof(int32_t)) !=
0)
tmp, nullptr, unitBuffer.data(),
unitFrames.size() * 24 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Units: " +
std::string(SDL_GetError()));
}
m_unitTextures.at(faction_idx)
.at(unit_idx)
.push_back(std::pair<SDL_Texture*, int>(tmp, unit_frames.size()));
m_unitTextures.at(factionIdx).at(unitIdx).emplace_back(tmp, unitFrames.size());
}
}
}
......@@ -311,32 +297,32 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
"submarine_appear"});
// Every effect sub data set
for (size_t effect_idx = 0; effect_idx < effects.size(); effect_idx++)
for (size_t effectIdx = 0; effectIdx < effects.size(); effectIdx++)
{
HighFive::DataSet effect_ds = file.getDataSet("effects/" + effects[effect_idx]);
HighFive::DataSet effectDs = file.getDataSet("effects/" + effects[effectIdx]);
std::vector<std::vector<std::vector<uint32_t>>> effect_frames;
effect_ds.read(effect_frames);
std::vector<std::vector<std::vector<uint32_t>>> effectFrames;
effectDs.read(effectFrames);
std::vector<uint32_t> effect_buffer(32 * 32 * effect_frames.size(), 0);
std::vector<uint32_t> effectBuffer(32 * 32 * effectFrames.size(), 0);
// every animation frame
for (size_t n = 0; n < effect_frames.size(); n++)
for (size_t n = 0; n < effectFrames.size(); n++)
{
for (size_t y = 0; y < 32; y++)
{
for (size_t x = 0; x < 32; x++)
{
size_t index = (y * effect_frames.size() * 32) + (n * 32 + x);
size_t index = (y * effectFrames.size() * 32) + (n * 32 + x);
effect_buffer.at(index) = effect_frames.at(n).at(32 - y - 1).at(x);
effectBuffer.at(index) = effectFrames.at(n).at(32 - y - 1).at(x);
}
}
}
SDL_Texture* tmp = SDL_CreateTexture(
engine.renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC,
effect_frames.size() * 32, 32);
effectFrames.size() * 32, 32);
SDL_SetTextureBlendMode(tmp, SDL_BLENDMODE_BLEND);
......@@ -347,32 +333,32 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
}
if (SDL_UpdateTexture(
tmp, NULL, effect_buffer.data(), effect_frames.size() * 32 * sizeof(int32_t)) != 0)
tmp, nullptr, effectBuffer.data(), effectFrames.size() * 32 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Tiles: " + std::string(SDL_GetError()));
}
m_effectTextures.push_back(std::pair<SDL_Texture*, int>(tmp, effect_frames.size()));
m_effectTextures.emplace_back(tmp, effectFrames.size());
}
this->m_effectWidth = 32;
this->m_effectHeight = 32;
// Numbers
HighFive::DataSet number_ds = file.getDataSet("/misc/numbers");
HighFive::DataSet numberDs = file.getDataSet("/misc/numbers");
std::vector<std::vector<uint32_t>> number_frames;
number_ds.read(number_frames);
std::vector<std::vector<uint32_t>> numberFrames;
numberDs.read(numberFrames);
std::vector<uint32_t> number_buffer(8 * 80, 0);
std::vector<uint32_t> numberBuffer(8 * 80, 0);
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 80; x++)
{
int index = y * 80 + x;
number_buffer.at(index) = number_frames.at(8 - y - 1).at(x);
int index = (y * 80) + x;
numberBuffer.at(index) = numberFrames.at(8 - y - 1).at(x);
}
}
......@@ -384,13 +370,13 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
if (tmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Effects: " + std::string(SDL_GetError()));
"Fehler beim Erstellen der Textur für die Numbers: " + std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(tmp, NULL, number_buffer.data(), 80 * sizeof(int32_t)) != 0)
if (SDL_UpdateTexture(tmp, nullptr, numberBuffer.data(), 80 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Tiles: " + std::string(SDL_GetError()));
"Fehler beim updaten der Textur für die Numbers: " + std::string(SDL_GetError()));
}
this->m_numberTextures = tmp;
......@@ -413,7 +399,7 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
{
size_t index = (y * 8) + x;
number_buffer.at(index) = bulletFrames.at(8 - y - 1).at(x);
numberBuffer.at(index) = bulletFrames.at(8 - y - 1).at(x);
}
}
......@@ -425,13 +411,13 @@ Spritesheet::Spritesheet(std::string path, Engine& engine)
if (bulletTmp == nullptr)
{
throw std::runtime_error(
"Fehler beim Erstellen der Textur für die Effects: " + std::string(SDL_GetError()));
"Fehler beim Erstellen der Textur für die Bullets: " + std::string(SDL_GetError()));
}
if (SDL_UpdateTexture(bulletTmp, NULL, number_buffer.data(), 8 * sizeof(int32_t)) != 0)
if (SDL_UpdateTexture(bulletTmp, nullptr, numberBuffer.data(), 8 * sizeof(int32_t)) != 0)
{
throw std::runtime_error(
"Fehler beim updaten der Textur für die Tiles: " + std::string(SDL_GetError()));
"Fehler beim updaten der Textur für die Bullets: " + std::string(SDL_GetError()));
}
this->m_bulletTexture = bulletTmp;
......@@ -548,19 +534,19 @@ int Spritesheet::getBulletHeight()
Spritesheet::~Spritesheet()
{
for (std::pair<SDL_Texture*, int> tile_texture : m_tileTextures)
for (std::pair<SDL_Texture*, int> tileTexture : m_tileTextures)
{
SDL_DestroyTexture(tile_texture.first);
SDL_DestroyTexture(tileTexture.first);
}
for (SDL_Texture* building_texture : m_buildingTextures)
for (SDL_Texture* buildingTexture : m_buildingTextures)
{
SDL_DestroyTexture(building_texture);
SDL_DestroyTexture(buildingTexture);
}
for (std::vector<std::vector<std::pair<SDL_Texture*, int>>> faction : m_unitTextures)
for (const std::vector<std::vector<std::pair<SDL_Texture*, int>>>& faction : m_unitTextures)
{
for (std::vector<std::pair<SDL_Texture*, int>> unit : faction)
for (const std::vector<std::pair<SDL_Texture*, int>>& unit : faction)
{
for (std::pair<SDL_Texture*, int> state : unit)
{
......
......@@ -12,7 +12,7 @@ class Tile
Tile(TileId id, int x, int y);
void render(Engine& engine, int scale);
TileId getType() const;
[[nodiscard]] TileId getType() const;
private:
TileId m_id;
......
......@@ -33,14 +33,14 @@ int Window::h()
return m_height;
}
SDL_Window* Window::sdl_window()
SDL_Window* Window::sdlWindow()
{
return m_window;
}
Window::~Window()
{
if (m_window)
if (m_window != nullptr)
{
SDL_DestroyWindow(m_window);
m_window = nullptr;
......
......@@ -38,7 +38,7 @@ class Window
/// Returns the current height of the window
int h();
SDL_Window* sdl_window();
SDL_Window* sdlWindow();
private:
/// SDL main window struct
......
......@@ -39,12 +39,11 @@ void Effect::render(Engine& engine, int scale)
&dest, 0, NULL, SDL_FLIP_NONE);
}
bool Effect::is_finished(Engine& engine)
bool Effect::isFinished(Engine& engine)
{
return !(
engine.getStage() - m_start <=
engine.getSpritesheet()->getEffectTextures().at(static_cast<int>(m_id)).second ||
m_repeat);
return engine.getStage() - m_start >
engine.getSpritesheet()->getEffectTextures().at(static_cast<int>(m_id)).second &&
!m_repeat;
}
} // namespace advanced_wars
......@@ -21,7 +21,7 @@ class Effect
void render(Engine& engine, int scale);
bool is_finished(Engine& engine);
bool isFinished(Engine& engine);
private:
int m_x;
......
#include "Building.hpp"
#include "../core/Spritesheet.hpp"
#include <algorithm>
#include <iostream>
namespace advanced_wars
......@@ -36,25 +34,21 @@ Faction Building::getFaction()
return this->m_faction;
}
bool Building::switch_faction(Faction faction)
bool Building::switchFaction(Faction faction)
{
this->m_faction = faction;
if (this->m_id == BuildingId::HEADQUARTER)
{
return true;
}
return false;
return this->m_id == BuildingId::HEADQUARTER;
}
// implement call to UI to show available units
void Building::on_click()
void Building::onClick()
{
std::cout << "A building is selected!" << "\n";
};
bool Building::check_spawn(std::unordered_map<int, std::unique_ptr<Unit>>& units)
bool Building::checkSpawn(std::unordered_map<int, std::unique_ptr<Unit>>& units)
{
for (auto& [id, unit] : units)
{
......@@ -69,7 +63,7 @@ bool Building::check_spawn(std::unordered_map<int, std::unique_ptr<Unit>>& units
return true;
}
// can be added as soon as the playerobject is available
bool Building::check_money(int price, int playerMoney)
bool Building::checkMoney(int price, int playerMoney)
{
// replace 400 with player.money and replace price with chosenUnit.price
return (playerMoney >= price);
......
......@@ -31,23 +31,23 @@ class Building
@return true if building was a headquarter
*/
bool switch_faction(Faction faction);
bool switchFaction(Faction faction);
/*
checks if the tile ontop of the building is free
*/
bool check_spawn(std::unordered_map<int, std::unique_ptr<Unit>>& units);
bool checkSpawn(std::unordered_map<int, std::unique_ptr<Unit>>& units);
/*
checks if the player has enough money for the unit to be recruited
*/
bool check_money(int price, int playerMoney);
bool checkMoney(int price, int playerMoney);
/**
If the building is clicked, it shows information to the player, here it will be a list of
all available units
*/
void on_click();
void onClick();
/**
* Provides a vector of recruitable units, depending on the building id
......
......@@ -4,23 +4,22 @@
#include "../physics/PhysicsEngine.hpp"
#include "box2d/b2_settings.h"
#include <iostream>
#include <stdexcept>
namespace advanced_wars
{
Bullet::Bullet(
b2World* world, float startX, float startY, float velocityX, float velocityY, Unit& target)
: m_renderX(0), m_renderY(0), m_target(target)
: m_target(target)
{
// Erstelle einen PhysicsBody für die Bullet
m_physicsBody = std::make_unique<PhysicsBody>(
world, startX, startY, 4.0F, 4.0F, 1.0, 0.3, true, BodyType::PROJECTILE);
if (m_physicsBody && m_physicsBody->getBody())
if (m_physicsBody && (m_physicsBody->getBody() != nullptr))
{
BodyUserData* bud = new BodyUserData();
bud->type = BodyUserData::Type::Bullet;
bud->type = BodyUserData::Type::BULLET;
bud->id = target.getUnitId();
bud->data = this;
......@@ -64,7 +63,7 @@ void Bullet::update()
void Bullet::render(Engine& engine, int scale)
{
// Falls die Textur noch nicht gesetzt wurde, laden wir sie jetzt
if (!m_texture)
if (m_texture == nullptr)
{
Spritesheet* spritesheet = engine.getSpritesheet();
m_texture = spritesheet->getBulletTexture(); // Textur aus HDF5-File laden
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment