From 6140e13db6187e687cf75d4929a66800eaf80d13 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 13 Oct 2023 15:14:04 +0200 Subject: [PATCH] - Reestructurat el codi dels gamestates per a que siga mes llegible --- source/gamestate_menu.cpp | 36 ++++++------ source/gamestate_prefase.cpp | 101 ++++++++++++++++++--------------- source/gamestate_sequence.cpp | 104 ++++++++++++++++++---------------- 3 files changed, 129 insertions(+), 112 deletions(-) diff --git a/source/gamestate_menu.cpp b/source/gamestate_menu.cpp index 61fada7..a8e5f57 100644 --- a/source/gamestate_menu.cpp +++ b/source/gamestate_menu.cpp @@ -12,6 +12,26 @@ namespace gamestate draw::surface *cursor = nullptr; int exit = 0; + bool loop(); + + + void init() + { + exit = 0; + fondo = draw::loadSurface("menuprin.gif", true); + cursor = draw::loadSurface("cursor.gif"); + + if (audio::getMusicState() != audio::music_state::MUSIC_PLAYING) { + audio::playMusic(audio::loadMusic("mus3.ogg")); + } + + draw::setSource(fondo); + draw::draw(0,0,320,200,0,0); + draw::fadein(); + + game::setState(&gamestate::menu::loop); + } + bool loop() { if (exit) { @@ -57,21 +77,5 @@ namespace gamestate return true; } - void init() - { - exit = 0; - fondo = draw::loadSurface("menuprin.gif", true); - cursor = draw::loadSurface("cursor.gif"); - - if (audio::getMusicState() != audio::music_state::MUSIC_PLAYING) { - audio::playMusic(audio::loadMusic("mus3.ogg")); - } - - draw::setSource(fondo); - draw::draw(0,0,320,200,0,0); - draw::fadein(); - - game::setState(&gamestate::menu::loop); - } } } \ No newline at end of file diff --git a/source/gamestate_prefase.cpp b/source/gamestate_prefase.cpp index 1759163..dafc578 100644 --- a/source/gamestate_prefase.cpp +++ b/source/gamestate_prefase.cpp @@ -10,13 +10,65 @@ namespace gamestate { namespace prefase { + // Variables del gamestate draw::surface *fondo = nullptr; draw::surface *cursor = nullptr; draw::surface *font = nullptr; uint8_t num_arounders = 0; uint8_t arounders_necessaris = 0; - uint8_t fase = 0; + + // Mètodes del gamestate + void carregarMapa(); + std::string formatejar(const int numero); + void drawText(const int x, const int y, std::string text); + bool loop(); + + + void init() + { + fondo = draw::loadSurface("prefase.gif", true); + cursor = draw::loadSurface("cursor.gif"); + font = draw::loadSurface("fuente1.gif"); + + int size=0; + uint32_t *font_pal = draw::loadPalette("fuente1.gif", &size); + draw::setPalette(font_pal+1, 5, 80); + + carregarMapa(); + + draw::fadein(); + game::setState(gamestate::prefase::loop); + } + + bool loop() + { + const int x = input::mouseX(); + const int y = input::mouseY(); + + draw::setTrans(255); + draw::setSource(fondo); + draw::draw(0, 0, 320, 200, 0, 0); + + draw::setTrans(0); + draw::setSource(font); + + drawText(130, 60, "NIVELL"); + drawText(179, 60, formatejar(game::getConfig("fase")+1)); + + drawText(80, 100, formatejar(num_arounders)); + drawText(101, 100, "AROUNDERS DISPONIBLES"); + + drawText(80, 110, formatejar(arounders_necessaris)); + drawText(101, 110, "AROUNDERS NECESSARIS"); + + draw::setSource(cursor); + draw::draw(x, y, cursor->w, cursor->h, 0, 0); + + draw::render(); + + return true; + } void carregarMapa() { @@ -27,8 +79,7 @@ namespace gamestate num_arounders = *(punter++); arounders_necessaris = *punter; - fase = game::getConfig("fase")+1; - + free(buffer); } @@ -63,49 +114,5 @@ namespace gamestate for (int i=1;i<=5;++i) draw::restorecol(i); } - bool loop() - { - const int x = input::mouseX(); - const int y = input::mouseY(); - - draw::setTrans(255); - draw::setSource(fondo); - draw::draw(0, 0, 320, 200, 0, 0); - - draw::setTrans(0); - draw::setSource(font); - - drawText(130, 60, "NIVELL"); - drawText(179, 60, formatejar(fase)); - - drawText(80, 100, formatejar(num_arounders)); - drawText(101, 100, "AROUNDERS DISPONIBLES"); - - drawText(80, 110, formatejar(arounders_necessaris)); - drawText(101, 110, "AROUNDERS NECESSARIS"); - - draw::setSource(cursor); - draw::draw(x, y, cursor->w, cursor->h, 0, 0); - - draw::render(); - - return true; - } - - void init() - { - fondo = draw::loadSurface("prefase.gif", true); - cursor = draw::loadSurface("cursor.gif"); - font = draw::loadSurface("fuente1.gif"); - - int size=0; - uint32_t *font_pal = draw::loadPalette("fuente1.gif", &size); - draw::setPalette(font_pal+1, 5, 80); - - carregarMapa(); - - draw::fadein(); - game::setState(gamestate::prefase::loop); - } } } diff --git a/source/gamestate_sequence.cpp b/source/gamestate_sequence.cpp index 3f172eb..2f8b68a 100644 --- a/source/gamestate_sequence.cpp +++ b/source/gamestate_sequence.cpp @@ -22,37 +22,33 @@ namespace gamestate FILE *sequence_file = nullptr; uint32_t wait_until = 0; - void drawPic(std::string filename) - { - draw::surface *pic = draw::loadSurface(filename, true); - draw::setSource(pic); - draw::draw(0, 0, 320, 200, 0, 0); - draw::freeSurface(pic); - } + void drawPic(std::string filename); + void drawText(const int x, const int y, const uint8_t color, std::string text); + bool loop(); - void drawText(const int x, const int y, const uint8_t color, std::string text) + + void init() { - draw::surface *pic = draw::loadSurface("fuente2.gif"); - draw::setSource(pic); - draw::setTrans(0); - draw::setPaletteEntry(64, 255, 255, 255); - draw::setPaletteEntry(65, 255, 0, 0); - draw::setPaletteEntry(66, 0, 255, 0); - draw::setPaletteEntry(67, 0, 0, 255); - draw::setPaletteEntry(68, 0, 0, 0); - draw::swapcol(1, color+64); - draw::swapcol(2, 68); - const int len = text.length(); - for (int i=0;i