fix segfault

This commit is contained in:
2025-07-08 09:30:11 -04:00
parent c54089ae19
commit d350044cb2
7 changed files with 126 additions and 69 deletions

View File

@@ -36,9 +36,5 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.$(SRC_EXT)
clean: clean:
rm -rf $(OBJ_DIR) $(BIN_DIR) rm -rf $(OBJ_DIR) $(BIN_DIR)
# Run the program
run: $(EXEC)
./$(EXEC)
# Phony targets # Phony targets
.PHONY: all clean run .PHONY: all clean run

View File

@@ -58,9 +58,6 @@ void Entity::draw() const {
bool Entity::shouldBeRemoved() const noexcept { bool Entity::shouldBeRemoved() const noexcept {
const auto &aquarium = Aquarium::getInstance(); const auto &aquarium = Aquarium::getInstance();
if (moving_right) { // unsigned nonsense
return x > static_cast<float>(aquarium.getWidth()); return x < -static_cast<int>(getWidth()) || x > aquarium.getWidth();
} else {
return (x + static_cast<float>(getWidth())) < 0;
}
} }

View File

@@ -29,7 +29,7 @@ public:
float getX() const noexcept { return x; } float getX() const noexcept { return x; }
float getY() const noexcept { return y; } float getY() const noexcept { return y; }
virtual size_t getWidth() const noexcept { return getImage()[0].length(); } virtual int getWidth() const noexcept { return getImage()[0].length(); }
size_t getId() const noexcept { return entity_id; } size_t getId() const noexcept { return entity_id; }
virtual void update() noexcept = 0; virtual void update() noexcept = 0;

View File

@@ -16,14 +16,15 @@ Fish::Fish(int asset_index)
const auto &aquarium = Aquarium::getInstance(); const auto &aquarium = Aquarium::getInstance();
y = Random::intInRange(static_cast<int>(image.size()) + 6, y = Random::intInRange(static_cast<int>(image.size()) + 6,
aquarium.getHeight() - static_cast<int>(image.size())); aquarium.getHeight() - static_cast<int>(image.size()));
x = moving_right ? -20.0f : static_cast<float>(aquarium.getWidth()); x = moving_right ? -this->getWidth()
: static_cast<float>(aquarium.getWidth());
randomizeMask(); randomizeMask();
} }
void Fish::randomizeMask() { void Fish::randomizeMask() {
// Clear and rebuild color map with fresh random colors each time // Clear and rebuild color map with random colors each time
color_map.clear(); color_map.clear();
color_map['4'] = 'W'; // White is always '4' color_map['4'] = 'W'; // White is always '4' for eyes
// Assign random colors to digits 1-3, 5-9 // Assign random colors to digits 1-3, 5-9
for (char digit = '1'; digit <= '9'; ++digit) { for (char digit = '1'; digit <= '9'; ++digit) {

View File

@@ -6,7 +6,7 @@
Whale::Whale() : Whale(getRandomDirection()) {} Whale::Whale() : Whale(getRandomDirection()) {}
Whale::Whale(int asset_index) Whale::Whale(int asset_index)
: Entity((asset_index = 0)), frames(whaleAssets[asset_index].frames), : Entity((asset_index == 0)), frames(whaleAssets[asset_index].frames),
mask(whaleAssets[asset_index].mask), speed(WHALE_SPEED) { mask(whaleAssets[asset_index].mask), speed(WHALE_SPEED) {
const auto &aquarium = Aquarium::getInstance(); const auto &aquarium = Aquarium::getInstance();

View File

@@ -9,14 +9,32 @@ struct CastleAsset {
}; };
inline CastleAsset castleAsset = { inline CastleAsset castleAsset = {
{R"( T~~)", R"( |)", R"( /^\)", {
R"( / \)", R"( _ _ _ / \ _ _ _)", R"( T~~)",
R"([ ]_[ ]_[ ]/ _ _ \[ ]_[ ]_[ ])", R"(|_=__-_ =_|_[ ]_[ ]_|_=-___-__|)", R"( |)",
R"( | _- = | =_ = _ |= _= |)", R"( |= -[] |- = _ = |_-=_[] |)", R"( /^\)",
R"( | =_ |= - ___ | =_ = |)", R"( |= []- |- /| |\ |=_ =[] |)", R"( / \)",
R"( |- =_ | =| | | | |- = - |)", R"( |_______|__|_|_|_|__|_______|)"}, R"( _ _ _ / \ _ _ _)",
{R"( RR)", R"()", R"( yyy)", R"([ ]_[ ]_[ ]/ _ _ \[ ]_[ ]_[ ])",
R"( y y)", R"( y y)", R"(|_=__-_ =_|_[ ]_[ ]_|_=-___-__|)",
R"( y y)", R"()", R"()", R"()", R"( yyy)", R"( | _- = | =_ = _ |= _= |)",
R"( yy yy)", R"( y y y y)", R"( |= -[] |- = _ = |_-=_[] |)",
R"( yyyyyyy)"}}; R"( | =_ |= - ___ | =_ = |)",
R"( |= []- |- /| |\ |=_ =[] |)",
R"( |- =_ | =| | | | |- = - |)",
R"( |_______|__|_|_|_|__|_______|)"},
{
R"( RR)",
R"()",
R"( yyy)",
R"( y y)",
R"( y y)",
R"( y y)",
R"()",
R"()",
R"()",
R"( yyy)",
R"( yy yy)",
R"( y y y y)",
R"( yyyyyyy)"}
};

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "../Entity.h" #include "../Entity.h"
#include "../SpriteUtils.h"
#include <vector> #include <vector>
struct WhaleAsset { struct WhaleAsset {
@@ -7,47 +8,91 @@ struct WhaleAsset {
std::vector<std::string> mask; std::vector<std::string> mask;
}; };
inline const std::vector<WhaleAsset> whaleAssets = { const WhaleAsset whale = {
{{{{R"()", R"()", R"()", R"( .-----.)", R"( .' `.)", {
R"(,????/ (o) \)", R"(\`._/ ,__)"}, {
{R"()", R"()", R"( :)", R"( .-----.)", R"( )",
R"( .' `.)", R"(,????/ (o) \)", R"()",
R"(\`._/ ,__)"}, R"()",
{R"()", R"( :)", R"( :)", R"( .-----.)", R"( .-----. )",
R"( .' `.)", R"(,????/ (o) \)", R"( .' `. )",
R"(\`._/ ,__)"}, R"(,????/ (o) \)",
{R"( . .)", R"( -:-)", R"( :)", R"(\`._/ ,__)"
R"( .-----.)", R"( .' `.)", R"(,????/ (o) \)", },
R"(\`._/ ,__)"}, {
{R"( . .)", R"( .-.-.)", R"( :)", R"( )",
R"( .-----.)", R"( .' `.)", R"(,????/ (o) \)", R"()",
R"(\`._/ ,__)"}, R"( : )",
{R"( . .)", R"( '.-:-.')", R"( ' : ')", R"( .-----. )",
R"( .-----.)", R"( .' `.)", R"(,????/ (o) \)", R"( .' `. )",
R"(\`._/ ,__)"}, R"(,????/ (o) \)",
{R"()", R"( .- -.)", R"( ; : ;)", R"( .-----.)", R"(\`._/ ,__)"
R"( .' `.)", R"(,????/ (o) \)", },
R"(\`._/ ,__)"}, {
{R"()", R"()", R"( ; ;)", R"( .-----.)", R"( )",
R"( .' `.)", R"(,????/ (o) \)", R"( : )",
R"(\`._/ ,__)"}}, R"( : )",
{R"( C C)", R"( CCCCCCC)", R"( C C C)", R"()", R"( .-----. )",
R"()", R"( W)", R"()"}}, R"( .' `. )",
{{{R"()", R"()", R"()", R"( .-----.)", R"( .' `.)", R"(,????/ (o) \)",
R"( / (o) \????)", R"((__, \_.'/)"}, R"(\`._/ ,__)"
{R"()", R"()", R"( :)", R"( .-----.)", R"( .' `.)", },
R"( / (o) \????)", R"((__, \_.'/)"}, {
{R"()", R"( :)", R"( :)", R"( .-----.)", R"( . . )",
R"( .' `.)", R"( / (o) \????)", R"((__, \_.'/)"}, R"( -:- )",
{R"( . .)", R"( -:-)", R"( :)", R"( .-----.)", R"( : )",
R"( .' `.)", R"( / (o) \????)", R"((__, \_.'/)"}, R"( .-----. )",
{R"( . .)", R"( .-.-.)", R"( :)", R"( .-----.)", R"( .' `. )",
R"( .' `.)", R"( / (o) \????)", R"((__, \_.'/)"}, R"(,????/ (o) \)",
{R"( . .)", R"( '.-:-.')", R"( ' : ')", R"( .-----.)", R"(\`._/ ,__)"
R"( .' `.)", R"( / (o) \????)", R"((__, \_.'/)"}, },
{R"()", R"( .- -.)", R"( ; : ;)", R"( .-----.)", {
R"( .' `.)", R"( / (o) \????)", R"((__, \_.'/)"}, R"( . . )",
{R"()", R"()", R"( ; ;)", R"( .-----.)", R"( .' `.)", R"( .-.-. )",
R"( / (o) \????)", R"((__, \_.'/)"}}, R"( : )",
{R"( C C)", R"( CCCCCCC)", R"( C C C)", R"()", R"()", R"( .-----. )",
R"( W)", R"()"}}}}; R"( .' `. )",
R"(,????/ (o) \)",
R"(\`._/ ,__)"
},
{
R"( . . )",
R"( '.-:-.' )",
R"( ' : ' )",
R"( .-----. )",
R"( .' `. )",
R"(,????/ (o) \)",
R"(\`._/ ,__)"
},
{
R"( )",
R"( .- -. )",
R"( ; : ; )",
R"( .-----. )",
R"( .' `. )",
R"(,????/ (o) \)",
R"(\`._/ ,__)"
},
{
R"( )",
R"( )",
R"( ; ; )",
R"( .-----. )",
R"( .' `. )",
R"(,????/ (o) \)",
R"(\`._/ ,__)"
}
},
{
R"( C C )",
R"( CCCCCCC )",
R"( C C C )",
R"()",
R"()",
R"( W )",
R"()"
}
};
inline const std::vector<WhaleAsset> whaleAssets =
createBidirectionalFramedAssets(whale);