- Actualitzem tots els gamestate per a usar els nous mòduls.

This commit is contained in:
2023-10-17 18:49:41 +02:00
parent bcd38a5a06
commit 8918c25b4e
5 changed files with 68 additions and 189 deletions

View File

@@ -11,7 +11,6 @@ namespace gamestate
bool loop(); bool loop();
void init() void init()
{ {
exit = 0; exit = 0;
@@ -22,8 +21,6 @@ namespace gamestate
audio::playMusic(audio::loadMusic("mus3.ogg")); audio::playMusic(audio::loadMusic("mus3.ogg"));
} }
draw::setSource(fondo);
draw::draw(0,0,320,200,0,0);
draw::fadein(); draw::fadein();
game::setState(&gamestate::menu::loop); game::setState(&gamestate::menu::loop);

View File

@@ -1,28 +1,34 @@
#include "gamestates.h" #include "gamestates.h"
#include "jgame.h" #include "jgame.h"
#include <string> #include <string>
#include "aux_font.h"
#define POSTFASE_INITIAL 0
#define POSTFASE_VICTORIA 1
#define POSTFASE_PASSWORD 2
namespace gamestate namespace gamestate
{ {
namespace postfase namespace postfase
{ {
namespace state
{
const int initial = 0;
const int victoria = 1;
const int password = 2;
}
// Variables del gamestate // Variables del gamestate
static int sub_state = POSTFASE_INITIAL; static int sub_state = postfase::state::initial;
void init_victoria(); void initVictoria();
void init_password(); void initPassword();
bool loop(); bool loop();
void drawText(const int x, const int y, const uint8_t color, std::string text); std::string getPassword();
char *ObtenerPasswordDeFase();
void init() void init()
{ {
sub_state = POSTFASE_INITIAL; sub_state = postfase::state::initial;
font::selectFont(font::type::colored);
font::setColor(font::color::red);
if (game::getConfig("fase") == 30) { if (game::getConfig("fase") == 30) {
gamestate::sequence::init(); gamestate::sequence::init();
@@ -33,34 +39,21 @@ namespace gamestate
} }
if (game::getConfig("fase") % 5 == 0) { if (game::getConfig("fase") % 5 == 0) {
gamestate::postfase::init_victoria(); gamestate::postfase::initVictoria();
} else { } else {
gamestate::postfase::init_password(); gamestate::postfase::initPassword();
} }
} }
void init_victoria() void initVictoria()
{ {
sub_state = POSTFASE_VICTORIA; sub_state = postfase::state::victoria;
draw::surface *fondo = nullptr; draw::surface *fondo = nullptr;
switch (game::getConfig("fase")) { char filename[12] = "final00.GIF";
case 5: filename[6]= game::getConfig("fase") / 5;
fondo = draw::loadSurface("final01.GIF", true); fondo = draw::loadSurface(filename, true);
break;
case 10:
fondo = draw::loadSurface("final02.GIF", true);
break;
case 15:
fondo = draw::loadSurface("final03.GIF", true);
break;
case 20:
fondo = draw::loadSurface("final04.GIF", true);
break;
case 25:
fondo = draw::loadSurface("final05.GIF", true);
break;
}
draw::setSource(fondo); draw::setSource(fondo);
draw::draw(0,0,320,200,0,0); draw::draw(0,0,320,200,0,0);
draw::freeSurface(fondo); draw::freeSurface(fondo);
@@ -69,19 +62,18 @@ namespace gamestate
game::setState(gamestate::postfase::loop); game::setState(gamestate::postfase::loop);
} }
void init_password() void initPassword()
{ {
sub_state = POSTFASE_PASSWORD; sub_state = postfase::state::password;
draw::surface *fondo = draw::loadSurface("postfase.gif", true); draw::surface *fondo = draw::loadSurface("postfase.gif", true);
char *password = ObtenerPasswordDeFase(); std::string password = getPassword();
draw::setSource(fondo); draw::setSource(fondo);
draw::draw(0,0,320,200,0,0); draw::draw(0,0,320,200,0,0);
draw::freeSurface(fondo); draw::freeSurface(fondo);
drawText(175, 166, 1, password); font::print(175, 166, password);
free(password);
draw::fadein(); draw::fadein();
game::setState(gamestate::postfase::loop); game::setState(gamestate::postfase::loop);
@@ -98,8 +90,8 @@ namespace gamestate
if (salir) if (salir)
{ {
salir = false; salir = false;
if (sub_state == POSTFASE_VICTORIA) { if (sub_state == postfase::state::victoria) {
gamestate::postfase::init_password(); gamestate::postfase::initPassword();
} else { } else {
gamestate::sequence::init(); gamestate::sequence::init();
} }
@@ -115,41 +107,14 @@ namespace gamestate
return true; return true;
} }
void drawText(const int x, const int y, const uint8_t color, std::string text) std::string getPassword()
{ {
draw::surface *pic = draw::loadSurface("fuente2.gif"); char *buffer = file::getFileBuffer("offsets.bal");
draw::setSource(pic);
draw::setTrans(0);
switch (color) {
case 0: draw::setPaletteEntry(63, 255, 255, 255); break;
case 1: draw::setPaletteEntry(63, 255, 0, 0); break;
case 2: draw::setPaletteEntry(63, 0, 255, 0); break;
case 3: draw::setPaletteEntry(63, 0, 0, 255); break;
}
const int len = text.length();
for (int i=0;i<len;++i)
{
char chr = text[i];
draw::draw(x+i*7, y, 6, 6, (int(chr)-32)*7, 0);
}
draw::setTrans(255);
draw::freeSurface(pic);
draw::render();
}
char *ObtenerPasswordDeFase()
{
int filesize = 0;
const char *buffer = file::getFileBuffer("offsets.bal", &filesize);
int punter = (game::getConfig("fase")-1)*11; int punter = (game::getConfig("fase")-1)*11;
char passFile[11];
char *passFile = (char*)malloc(11); for (int i=0;i<10;i++) passFile[i] = uint8_t(buffer[++punter]) - (101+i);
free(buffer);
for (int i=0;i<10;i++) {
punter++;
passFile[i] = uint8_t(buffer[punter]) - (101+i);
}
return passFile; return passFile;
} }

View File

@@ -1,6 +1,8 @@
#include "gamestates.h" #include "gamestates.h"
#include "jgame.h" #include "jgame.h"
#include <string> #include <string>
#include "aux_font.h"
#include "proc_mapa.h"
namespace gamestate namespace gamestate
{ {
@@ -9,30 +11,35 @@ namespace gamestate
// Variables del gamestate // Variables del gamestate
draw::surface *fondo = nullptr; draw::surface *fondo = nullptr;
draw::surface *cursor = nullptr; draw::surface *cursor = nullptr;
draw::surface *font = nullptr;
uint8_t num_arounders = 0;
uint8_t arounders_necessaris = 0;
// Mètodes del gamestate // 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(); bool loop();
void init() void init()
{ {
// Carrega el gif del fondo
fondo = draw::loadSurface("prefase.gif", true); fondo = draw::loadSurface("prefase.gif", true);
cursor = draw::loadSurface("cursor.gif"); cursor = draw::loadSurface("cursor.gif");
font = draw::loadSurface("fuente1.gif");
//int size=0; // Carrega el mapa
//uint32_t *font_pal = draw::loadPalette("fuente1.gif", &size); mapa::carregar();
//draw::setPalette(font_pal+1, 5, 80);
carregarMapa(); // Pinta el text en el fondo
draw::setDestination(fondo);
font::selectFont(font::type::normal);
font::print(130, 60, "NIVELL");
font::print(179, 60, game::getConfig("fase")+1);
font::print(80, 100, mapa::arounders::totals);
font::print(101, 100, "AROUNDERS DISPONIBLES");
font::print(80, 110, mapa::arounders::necessaris);
font::print(101, 110, "AROUNDERS NECESSARIS");
draw::setDestination(nullptr);
// Comencem el bucle
draw::fadein(); draw::fadein();
game::setState(gamestate::prefase::loop); game::setState(gamestate::prefase::loop);
} }
@@ -41,27 +48,12 @@ namespace gamestate
{ {
static bool salir = false; static bool salir = false;
const int x = input::mouseX();
const int y = input::mouseY();
draw::setTrans(255); draw::setTrans(255);
draw::setSource(fondo); draw::setSource(fondo);
draw::draw(0, 0, 320, 200, 0, 0); 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::setSource(cursor);
draw::draw(x, y, cursor->w, cursor->h, 0, 0); draw::draw(input::mouseX(), input::mouseY(), cursor->w, cursor->h, 0, 0);
draw::render(); draw::render();
@@ -83,49 +75,5 @@ namespace gamestate
return true; return true;
} }
void carregarMapa()
{
int filesize = 0;
char *buffer = file::getFileBuffer("MAPES.BAL", &filesize);
char *punter = buffer + (game::getConfig("fase") * 212) + 3;
num_arounders = *(punter++);
arounders_necessaris = *punter;
free(buffer);
}
std::string formatejar(const int numero)
{
char resultat[3];
if (numero > 9) {
resultat[0] = (numero / 10) + 48;
resultat[1] = (numero % 10) + 48;
} else {
resultat[0] = 48;
resultat[1] = (numero % 10) + 48;
}
resultat[2] = '\0';
return std::string(resultat);
}
void drawText(const int x, const int y, std::string text)
{
draw::setSource(font);
draw::setTrans(0);
//for (int i=1;i<=5;++i) draw::swapcol(i, 79+i);
const int len = text.length();
for (int i=0;i<len;++i)
{
char chr = text[i];
draw::draw(x+i*7, y, 5, 5, (int(chr)-32)*7, 0);
}
//for (int i=1;i<=5;++i) draw::restorecol(i);
}
} }
} }

View File

@@ -2,14 +2,7 @@
#include "jgame.h" #include "jgame.h"
#include <string> #include <string>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "aux_font.h"
#define DIAPO_ESPERAR 0
#define DIAPO_FADEIN 1
#define DIAPO_SHOW 2
#define DIAPO_PRINT 3
#define DIAPO_MUSICA 4
#define DIAPO_FADEOUT 5
#define DIAPO_FADEMUSIC 6
namespace gamestate namespace gamestate
{ {
@@ -19,17 +12,16 @@ namespace gamestate
uint32_t wait_until = 0; uint32_t wait_until = 0;
void drawPic(std::string filename); void drawPic(std::string filename);
void drawText(const int x, const int y, const uint8_t color, std::string text);
bool loop(); bool loop();
void init() void init()
{ {
draw::setTrans(255); draw::setTrans(255);
const int fase = game::getConfig("fase"); font::selectFont(font::type::colored);
std::string filename; std::string filename;
switch (fase) switch (game::getConfig("fase"))
{ {
case -1: filename = "seqIN.txt"; break; case -1: filename = "seqIN.txt"; break;
case 0: filename = "seq00.txt"; break; case 0: filename = "seq00.txt"; break;
@@ -39,11 +31,10 @@ namespace gamestate
case 20: filename = "seq20.txt"; break; case 20: filename = "seq20.txt"; break;
case 25: filename = "seq25.txt"; break; case 25: filename = "seq25.txt"; break;
case 30: filename = "seq30.txt"; break; case 30: filename = "seq30.txt"; break;
default: gamestate::prefase::init(); default: gamestate::prefase::init(); return; break;
} }
int filesize; sequence_file = file::getFilePointer(filename);
sequence_file = file::getFilePointer(filename, &filesize);
game::setState(&gamestate::sequence::loop); game::setState(&gamestate::sequence::loop);
} }
@@ -54,6 +45,7 @@ namespace gamestate
draw::render(); draw::render();
return true; return true;
} }
if ( (wait_until > 0) && (SDL_GetTicks() < wait_until) ) if ( (wait_until > 0) && (SDL_GetTicks() < wait_until) )
{ {
if (input::anyKeyPressed() || input::mouseBtn(1)) { if (input::anyKeyPressed() || input::mouseBtn(1)) {
@@ -87,30 +79,26 @@ namespace gamestate
std::string command(text); std::string command(text);
if (command=="ESPERAR") { if (command=="ESPERAR") {
//int res =
fscanf(sequence_file, "%i", &val); fscanf(sequence_file, "%i", &val);
printf("ESPERAR %i\n", val); printf("ESPERAR %i\n", val);
wait_until = SDL_GetTicks() + val; wait_until = SDL_GetTicks() + val;
} else if (command=="FADEIN") { } else if (command=="FADEIN") {
//int res =
fscanf(sequence_file, " '%[^']'", text); fscanf(sequence_file, " '%[^']'", text);
drawPic(text); drawPic(text);
draw::fadein(); draw::fadein();
} else if (command=="SHOW") { } else if (command=="SHOW") {
//int res =
fscanf(sequence_file, " '%[^']'", text); fscanf(sequence_file, " '%[^']'", text);
drawPic(text); drawPic(text);
draw::render(); draw::render();
} else if (command=="PRINT") { } else if (command=="PRINT") {
//int res =
fscanf(sequence_file, " %i %i %i '%[^']'", &x, &y, &val, text); fscanf(sequence_file, " %i %i %i '%[^']'", &x, &y, &val, text);
drawText(x, y, val, text); font::setColor(val);
font::print(x, y, text);
} else if (command=="PLAYMUSIC") { } else if (command=="PLAYMUSIC") {
//int res =
fscanf(sequence_file, " '%[^']'", text); fscanf(sequence_file, " '%[^']'", text);
audio::loadMusic(text); audio::loadMusic(text);
audio::playMusic(); audio::playMusic();
@@ -135,27 +123,5 @@ namespace gamestate
draw::freeSurface(pic); draw::freeSurface(pic);
} }
void drawText(const int x, const int y, const uint8_t color, std::string text)
{
draw::surface *pic = draw::loadSurface("fuente2.gif");
draw::setSource(pic);
draw::setTrans(0);
switch (color) {
case 0: draw::setPaletteEntry(63, 255, 255, 255); break;
case 1: draw::setPaletteEntry(63, 255, 0, 0); break;
case 2: draw::setPaletteEntry(63, 0, 255, 0); break;
case 3: draw::setPaletteEntry(63, 0, 0, 255); break;
}
const int len = text.length();
for (int i=0;i<len;++i)
{
char chr = text[i];
draw::draw(x+i*7, y, 6, 6, (int(chr)-32)*7, 0);
}
draw::setTrans(255);
draw::freeSurface(pic);
draw::render();
}
} }
} }

View File

@@ -1,5 +1,6 @@
#include "jgame.h" #include "jgame.h"
#include "gamestates.h" #include "gamestates.h"
#include "aux_font.h"
void game::init() void game::init()
{ {
@@ -7,6 +8,8 @@ void game::init()
input::init(3); input::init(3);
audio::init(); audio::init();
font::init();
game::setUpdateTicks(16); game::setUpdateTicks(16);
game::setConfig("fase", -1); game::setConfig("fase", -1);