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:
rm -rf $(OBJ_DIR) $(BIN_DIR)
# Run the program
run: $(EXEC)
./$(EXEC)
# Phony targets
.PHONY: all clean run

View File

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

View File

@@ -29,7 +29,7 @@ public:
float getX() const noexcept { return x; }
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; }
virtual void update() noexcept = 0;

View File

@@ -16,14 +16,15 @@ Fish::Fish(int asset_index)
const auto &aquarium = Aquarium::getInstance();
y = Random::intInRange(static_cast<int>(image.size()) + 6,
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();
}
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['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
for (char digit = '1'; digit <= '9'; ++digit) {

View File

@@ -6,7 +6,7 @@
Whale::Whale() : Whale(getRandomDirection()) {}
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) {
const auto &aquarium = Aquarium::getInstance();

View File

@@ -9,14 +9,32 @@ struct CastleAsset {
};
inline CastleAsset castleAsset = {
{R"( T~~)", R"( |)", R"( /^\)",
R"( / \)", R"( _ _ _ / \ _ _ _)",
R"([ ]_[ ]_[ ]/ _ _ \[ ]_[ ]_[ ])", R"(|_=__-_ =_|_[ ]_[ ]_|_=-___-__|)",
R"( | _- = | =_ = _ |= _= |)", R"( |= -[] |- = _ = |_-=_[] |)",
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)"}};
{
R"( T~~)",
R"( |)",
R"( /^\)",
R"( / \)",
R"( _ _ _ / \ _ _ _)",
R"([ ]_[ ]_[ ]/ _ _ \[ ]_[ ]_[ ])",
R"(|_=__-_ =_|_[ ]_[ ]_|_=-___-__|)",
R"( | _- = | =_ = _ |= _= |)",
R"( |= -[] |- = _ = |_-=_[] |)",
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
#include "../Entity.h"
#include "../SpriteUtils.h"
#include <vector>
struct WhaleAsset {
@@ -7,47 +8,91 @@ struct WhaleAsset {
std::vector<std::string> mask;
};
inline const std::vector<WhaleAsset> whaleAssets = {
{{{{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"( . .)", R"( -:-)", R"( :)",
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"()", R"()", R"( ; ;)", R"( .-----.)",
R"( .' `.)", R"(,????/ (o) \)",
R"(\`._/ ,__)"}},
{R"( C C)", R"( CCCCCCC)", R"( C C C)", R"()",
R"()", R"( W)", 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"( . .)", R"( -:-)", R"( :)", 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"()", R"()", R"( ; ;)", R"( .-----.)", R"( .' `.)",
R"( / (o) \????)", R"((__, \_.'/)"}},
{R"( C C)", R"( CCCCCCC)", R"( C C C)", R"()", R"()",
R"( W)", R"()"}}}};
const WhaleAsset whale = {
{
{
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"( . . )",
R"( -:- )",
R"( : )",
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"( )",
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);