This commit is contained in:
2026-04-18 13:22:13 +02:00
parent 2e1a82ff40
commit 27f8b0ae36
21 changed files with 54 additions and 66 deletions

View File

@@ -0,0 +1 @@
{"sessionId":"7b0c9c32-3dd4-48a3-ba06-c2303dc08243","pid":123890,"acquiredAt":1776510185734}

View File

@@ -265,13 +265,13 @@ inline JA_Music_t* JA_LoadMusic(const Uint8* buffer, Uint32 length) {
auto* music = new JA_Music_t(); auto* music = new JA_Music_t();
music->ogg_data.assign(buffer, buffer + length); music->ogg_data.assign(buffer, buffer + length);
int error = 0; int vorbis_error = 0;
music->vorbis = stb_vorbis_open_memory(music->ogg_data.data(), music->vorbis = stb_vorbis_open_memory(music->ogg_data.data(),
static_cast<int>(length), static_cast<int>(length),
&error, &vorbis_error,
nullptr); nullptr);
if (!music->vorbis) { if (!music->vorbis) {
std::cout << "JA_LoadMusic: stb_vorbis_open_memory failed (error " << error << ")" << '\n'; std::cout << "JA_LoadMusic: stb_vorbis_open_memory failed (error " << vorbis_error << ")" << '\n';
delete music; delete music;
return nullptr; return nullptr;
} }

View File

@@ -40,8 +40,11 @@ void JD8_ClearScreen(Uint8 color) {
} }
JD8_Surface JD8_NewSurface() { JD8_Surface JD8_NewSurface() {
JD8_Surface surface = (JD8_Surface)malloc(64000); JD8_Surface surface = (JD8_Surface)calloc(1, 64000);
memset(surface, 0, 64000); if (surface == NULL) {
printf("JD8_NewSurface: out of memory\n");
exit(1);
}
return surface; return surface;
} }
@@ -89,6 +92,10 @@ JD8_Palette JD8_LoadPalette(const char* file) {
// d'alliberar amb free() — mateixa convenció que el LoadPalette // d'alliberar amb free() — mateixa convenció que el LoadPalette
// original (retornava un malloc). // original (retornava un malloc).
JD8_Palette palette = (JD8_Palette)malloc(768); JD8_Palette palette = (JD8_Palette)malloc(768);
if (palette == NULL) {
printf("JD8_LoadPalette: out of memory\n");
exit(1);
}
memcpy(palette, cached.data(), 768); memcpy(palette, cached.data(), 768);
return palette; return palette;
} catch (const std::exception&) { } catch (const std::exception&) {

View File

@@ -82,10 +82,8 @@ void file_setconfigfolder(const char* foldername) {
config_folder = std::string(homedir) + "/.config/" + foldername; config_folder = std::string(homedir) + "/.config/" + foldername;
#endif #endif
if (!config_folder.empty()) {
std::filesystem::create_directories(config_folder); std::filesystem::create_directories(config_folder);
} }
}
const char* file_getconfigfolder() { const char* file_getconfigfolder() {
thread_local std::string folder; thread_local std::string folder;

View File

@@ -80,27 +80,27 @@ void Text::loadFont(const char* fnt_file) {
// Elimina comentaris inline // Elimina comentaris inline
auto comment_pos = line.find('#'); auto comment_pos = line.find('#');
if (comment_pos != std::string::npos) { if (comment_pos != std::string::npos) {
line = line.substr(0, comment_pos); line.resize(comment_pos);
} }
// Parseja directives // Parseja directives
if (line.find("box_width") == 0) { if (line.starts_with("box_width")) {
sscanf(line.c_str(), "box_width %d", &box_width_); sscanf(line.c_str(), "box_width %d", &box_width_);
continue; continue;
} }
if (line.find("box_height") == 0) { if (line.starts_with("box_height")) {
sscanf(line.c_str(), "box_height %d", &box_height_); sscanf(line.c_str(), "box_height %d", &box_height_);
continue; continue;
} }
if (line.find("columns") == 0) { if (line.starts_with("columns")) {
sscanf(line.c_str(), "columns %d", &columns_); sscanf(line.c_str(), "columns %d", &columns_);
continue; continue;
} }
if (line.find("cell_spacing") == 0) { if (line.starts_with("cell_spacing")) {
sscanf(line.c_str(), "cell_spacing %d", &cell_spacing_); sscanf(line.c_str(), "cell_spacing %d", &cell_spacing_);
continue; continue;
} }
if (line.find("row_spacing") == 0) { if (line.starts_with("row_spacing")) {
sscanf(line.c_str(), "row_spacing %d", &row_spacing_); sscanf(line.c_str(), "row_spacing %d", &row_spacing_);
continue; continue;
} }

View File

@@ -8,7 +8,7 @@ class Bola : public Sprite {
public: public:
Bola(JD8_Surface gfx, Prota* sam); Bola(JD8_Surface gfx, Prota* sam);
void draw(); void draw() override;
void update(); void update();
protected: protected:

View File

@@ -29,10 +29,6 @@ Engendro::Engendro(JD8_Surface gfx, Uint16 x, Uint16 y)
this->cycles_per_frame = 30; this->cycles_per_frame = 30;
} }
void Engendro::draw() {
Sprite::draw();
}
bool Engendro::update() { bool Engendro::update() {
bool mort = false; bool mort = false;

View File

@@ -6,7 +6,6 @@ class Engendro : public Sprite {
public: public:
Engendro(JD8_Surface gfx, Uint16 x, Uint16 y); Engendro(JD8_Surface gfx, Uint16 x, Uint16 y);
void draw();
bool update(); bool update();
protected: protected:

View File

@@ -9,28 +9,21 @@ ModuleGame::ModuleGame() {
this->gfx = JD8_LoadSurface(info::ctx.pepe_activat ? "gfx/frames2.gif" : "gfx/frames.gif"); this->gfx = JD8_LoadSurface(info::ctx.pepe_activat ? "gfx/frames2.gif" : "gfx/frames.gif");
JG_SetUpdateTicks(10); JG_SetUpdateTicks(10);
this->sam = new Prota(this->gfx); this->sam = std::make_unique<Prota>(this->gfx);
this->mapa = new Mapa(this->gfx, this->sam); this->mapa = std::make_unique<Mapa>(this->gfx, this->sam.get());
this->marcador = new Marcador(this->gfx, this->sam); this->marcador = std::make_unique<Marcador>(this->gfx, this->sam.get());
if (info::ctx.num_piramide == 2) { if (info::ctx.num_piramide == 2) {
this->bola = new Bola(this->gfx, this->sam); this->bola = std::make_unique<Bola>(this->gfx, this->sam.get());
} else {
this->bola = nullptr;
} }
this->momies = nullptr;
this->iniciarMomies(); this->iniciarMomies();
} }
ModuleGame::~ModuleGame() { ModuleGame::~ModuleGame() {
if (this->bola != nullptr) delete this->bola;
if (this->momies != nullptr) { if (this->momies != nullptr) {
this->momies->clear(); this->momies->clear();
delete this->momies; delete this->momies;
} }
delete this->marcador;
delete this->mapa;
delete this->sam;
JD8_FreeSurface(this->gfx); JD8_FreeSurface(this->gfx);
} }
@@ -123,7 +116,7 @@ void ModuleGame::Draw() {
this->marcador->draw(); this->marcador->draw();
this->sam->draw(); this->sam->draw();
if (this->momies != nullptr) this->momies->draw(); if (this->momies != nullptr) this->momies->draw();
if (this->bola != nullptr) this->bola->draw(); if (this->bola) this->bola->draw();
} }
void ModuleGame::Update() { void ModuleGame::Update() {
@@ -137,14 +130,14 @@ void ModuleGame::Update() {
this->momies = seguent; this->momies = seguent;
info::ctx.momies--; info::ctx.momies--;
} }
if (this->bola != nullptr) this->bola->update(); if (this->bola) this->bola->update();
this->mapa->update(); this->mapa->update();
if (this->mapa->novaMomia()) { if (this->mapa->novaMomia()) {
if (this->momies != nullptr) { if (this->momies != nullptr) {
this->momies->insertar(new Momia(this->gfx, true, 0, 0, this->sam)); this->momies->insertar(new Momia(this->gfx, true, 0, 0, this->sam.get()));
info::ctx.momies++; info::ctx.momies++;
} else { } else {
this->momies = new Momia(this->gfx, true, 0, 0, this->sam); this->momies = new Momia(this->gfx, true, 0, 0, this->sam.get());
info::ctx.momies++; info::ctx.momies++;
} }
} }
@@ -187,9 +180,9 @@ void ModuleGame::iniciarMomies() {
bool dimonis = info::ctx.num_piramide == 6; bool dimonis = info::ctx.num_piramide == 6;
for (int i = 0; i < info::ctx.momies; i++) { for (int i = 0; i < info::ctx.momies; i++) {
if (this->momies == nullptr) { if (this->momies == nullptr) {
this->momies = new Momia(this->gfx, dimonis, x, y, this->sam); this->momies = new Momia(this->gfx, dimonis, x, y, this->sam.get());
} else { } else {
this->momies->insertar(new Momia(this->gfx, dimonis, x, y, this->sam)); this->momies->insertar(new Momia(this->gfx, dimonis, x, y, this->sam.get()));
} }
x += 65; x += 65;
if (x == 345) { if (x == 345) {

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <memory>
#include "game/bola.hpp" #include "game/bola.hpp"
#include "game/info.hpp" #include "game/info.hpp"
#include "game/mapa.hpp" #include "game/mapa.hpp"
@@ -52,9 +54,9 @@ class ModuleGame : public scenes::Scene {
Uint8 final_{0}; Uint8 final_{0};
JD8_Surface gfx{nullptr}; JD8_Surface gfx{nullptr};
Mapa* mapa{nullptr}; std::unique_ptr<Mapa> mapa;
Prota* sam{nullptr}; std::unique_ptr<Prota> sam;
Marcador* marcador{nullptr}; std::unique_ptr<Marcador> marcador;
Momia* momies{nullptr}; Momia* momies{nullptr}; // llista enllaçada, ownership enredat amb `next`
Bola* bola{nullptr}; std::unique_ptr<Bola> bola;
}; };

View File

@@ -10,15 +10,15 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
this->sam = sam; this->sam = sam;
entitat.frames.reserve(20); entitat.frames.reserve(20);
for (int y = 0; y < 4; y++) { for (int row = 0; row < 4; row++) {
for (int x = 0; x < 5; x++) { for (int col = 0; col < 5; col++) {
Frame f; Frame f;
f.w = 15; f.w = 15;
f.h = 15; f.h = 15;
if (info::ctx.num_piramide == 4) f.h -= 5; if (info::ctx.num_piramide == 4) f.h -= 5;
f.x = (x * 15) + 75; f.x = (col * 15) + 75;
if (this->dimoni) f.x += 75; if (this->dimoni) f.x += 75;
f.y = 20 + (y * 15); f.y = 20 + (row * 15);
entitat.frames.push_back(f); entitat.frames.push_back(f);
} }
} }
@@ -57,9 +57,8 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
} else { } else {
this->y = y; this->y = y;
} }
this->engendro = new Engendro(gfx, this->x, this->y); this->engendro = std::make_unique<Engendro>(gfx, this->x, this->y);
} else { } else {
this->engendro = NULL;
this->x = x; this->x = x;
this->y = y; this->y = y;
} }
@@ -67,12 +66,11 @@ Momia::Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam)
void Momia::clear() { void Momia::clear() {
if (this->next != NULL) this->next->clear(); if (this->next != NULL) this->next->clear();
if (this->engendro != NULL) delete this->engendro;
delete this->next; delete this->next;
} }
void Momia::draw() { void Momia::draw() {
if (this->engendro != NULL) { if (this->engendro) {
this->engendro->draw(); this->engendro->draw();
} else { } else {
Sprite::draw(); Sprite::draw();
@@ -91,10 +89,9 @@ void Momia::draw() {
bool Momia::update() { bool Momia::update() {
bool morta = false; bool morta = false;
if (this->engendro != NULL) { if (this->engendro) {
if (this->engendro->update()) { if (this->engendro->update()) {
delete this->engendro; this->engendro.reset();
this->engendro = NULL;
} }
} else { } else {
if (this->sam->o < 4 && (this->dimoni || info::ctx.num_piramide == 5 || JG_GetCycleCounter() % 2 == 0)) { if (this->sam->o < 4 && (this->dimoni || info::ctx.num_piramide == 5 || JG_GetCycleCounter() % 2 == 0)) {

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <memory>
#include "game/engendro.hpp" #include "game/engendro.hpp"
#include "game/info.hpp" #include "game/info.hpp"
#include "game/prota.hpp" #include "game/prota.hpp"
@@ -10,7 +12,7 @@ class Momia : public Sprite {
Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam); Momia(JD8_Surface gfx, bool dimoni, Uint16 x, Uint16 y, Prota* sam);
void clear(); void clear();
void draw(); void draw() override;
bool update(); bool update();
void insertar(Momia* momia); void insertar(Momia* momia);
@@ -19,5 +21,5 @@ class Momia : public Sprite {
protected: protected:
Prota* sam; Prota* sam;
Engendro* engendro; std::unique_ptr<Engendro> engendro;
}; };

View File

@@ -5,9 +5,9 @@
class Prota : public Sprite { class Prota : public Sprite {
public: public:
Prota(JD8_Surface gfx); explicit Prota(JD8_Surface gfx);
void draw(); void draw() override;
Uint8 update(); Uint8 update();
Uint8 frame_pejades; Uint8 frame_pejades;

View File

@@ -22,10 +22,10 @@ struct Entitat {
class Sprite { class Sprite {
public: public:
Sprite(JD8_Surface gfx); explicit Sprite(JD8_Surface gfx);
virtual ~Sprite() = default; virtual ~Sprite() = default;
void draw(); virtual void draw();
Entitat entitat; Entitat entitat;
Uint8 cur_frame = 0; Uint8 cur_frame = 0;

View File

@@ -16,7 +16,6 @@ namespace scenes {
void onEnter() override; void onEnter() override;
void tick(int delta_ms) override; void tick(int delta_ms) override;
bool done() const override { return done_; } bool done() const override { return done_; }
int nextState() const override { return 1; } // 1 → SceneRegistry::tryCreate(num_piramide=255 → intro)
private: private:
void render() const; void render() const;

View File

@@ -29,7 +29,6 @@ namespace scenes {
void onEnter() override; void onEnter() override;
void tick(int delta_ms) override; void tick(int delta_ms) override;
bool done() const override { return phase_ == Phase::Done; } bool done() const override { return phase_ == Phase::Done; }
int nextState() const override { return 1; }
private: private:
enum class Phase { Rolling, enum class Phase { Rolling,

View File

@@ -37,7 +37,6 @@ namespace scenes {
void onEnter() override; void onEnter() override;
void tick(int delta_ms) override; void tick(int delta_ms) override;
bool done() const override { return phase_ == Phase::Done; } bool done() const override { return phase_ == Phase::Done; }
int nextState() const override { return 1; }
private: private:
enum class Phase { enum class Phase {

View File

@@ -38,7 +38,6 @@ namespace scenes {
void onEnter() override; void onEnter() override;
void tick(int delta_ms) override; void tick(int delta_ms) override;
bool done() const override { return phase_ == Phase::Done; } bool done() const override { return phase_ == Phase::Done; }
int nextState() const override { return 1; }
private: private:
enum class Phase { enum class Phase {

View File

@@ -29,7 +29,6 @@ namespace scenes {
void onEnter() override; void onEnter() override;
void tick(int delta_ms) override; void tick(int delta_ms) override;
bool done() const override { return done_; } bool done() const override { return done_; }
int nextState() const override { return 1; }
private: private:
SurfaceHandle gfx_; SurfaceHandle gfx_;

View File

@@ -27,7 +27,6 @@ namespace scenes {
void onEnter() override; void onEnter() override;
void tick(int delta_ms) override; void tick(int delta_ms) override;
bool done() const override { return phase_ == Phase::Done; } bool done() const override { return phase_ == Phase::Done; }
int nextState() const override { return 1; }
private: private:
enum class Phase { FadingIn, enum class Phase { FadingIn,

View File

@@ -19,7 +19,6 @@ namespace scenes {
void onEnter() override; void onEnter() override;
void tick(int delta_ms) override; void tick(int delta_ms) override;
bool done() const override { return phase_ == Phase::Done; } bool done() const override { return phase_ == Phase::Done; }
int nextState() const override { return 1; }
private: private:
enum class Phase { FadingIn, enum class Phase { FadingIn,