From fdc604be3c6c9685999d87e592f9f5be7ea02825 Mon Sep 17 00:00:00 2001 From: JailDoctor Date: Wed, 18 Oct 2023 16:18:57 +0200 Subject: [PATCH] =?UTF-8?q?-=20[NEW]=20aux=5Ftextfile()=20-=20Implementant?= =?UTF-8?q?=20la=20c=C3=A0rrega=20del=20nivell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/aux_textfile.cpp | 57 +++++++++++++++++++++++++++++++++++++++++ source/aux_textfile.h | 18 +++++++++++++ source/proc_mapa.cpp | 42 +++++++++++++++--------------- tools/level_reader.cpp | 16 ++++++++++++ 4 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 source/aux_textfile.cpp create mode 100644 source/aux_textfile.h diff --git a/source/aux_textfile.cpp b/source/aux_textfile.cpp new file mode 100644 index 0000000..1db7cf5 --- /dev/null +++ b/source/aux_textfile.cpp @@ -0,0 +1,57 @@ +#include "aux_textfile.h" + +#include "jgame.h" +#include + +namespace textfile +{ + char *buffer = nullptr; + int fsize = 0; + int p = 0; + + const bool open(std::string filename) + { + buffer = file::getFileBuffer(filename.c_str()); + p = 0; + + return true; + } + + void close() + { + free(buffer); + } + + std::string getNextToken() + { + char token[255]; + int tpos = 0; + + // Ignore whitespace + while (p32 ) token[tpos++]=buffer[p++]; + token[tpos]=0; + + return std::string(token); + } + + const bool searchToken(std::string token) + { + while (p +#include "aux_textfile.h" namespace mapa { @@ -38,30 +38,30 @@ namespace mapa void carregar() { - FILE *f = file::getFilePointer("mapes.txt"); + textfile::open("mapes.txt"); - //const int fase = game::getConfig("fase"); - int val; do { - fscanf(f, "LEVEL %i", &val); - } while (val != game::getConfig("fase")); + textfile::searchToken("LEVEL"); + } while (std::stoi(textfile::getNextToken()) != game::getConfig("fase")); - int tileset=0; - fscanf(f, "tileset = %i", &tileset); - fscanf(f, "orientacio = %i", &arounders::orientacio_inicial); - fscanf(f, "arounders = %i", &arounders::totals); - fscanf(f, "necessaris = %i", &arounders::necessaris); + const int tileset = textfile::getIntValue("tileset"); + arounders::orientacio_inicial = textfile::getIntValue("orientacio"); + arounders::totals = textfile::getIntValue("arounders"); + arounders::necessaris = textfile::getIntValue("necessaris"); - fscanf(f, "parar = %i", &accions::parar); - fscanf(f, "cavar = %i", &accions::cavar); - fscanf(f, "escalar = %i", &accions::escalar); - fscanf(f, "perforar = %i", &accions::perforar); - fscanf(f, "escalera = %i", &accions::escalera); - fscanf(f, "pasarela = %i", &accions::pasarela); - fscanf(f, "corda = %i", &accions::corda); + accions::parar = textfile::getIntValue("parar"); + accions::cavar = textfile::getIntValue("cavar"); + accions::escalar = textfile::getIntValue("escalar"); + accions::perforar = textfile::getIntValue("perforar"); + accions::escalera = textfile::getIntValue("escalera"); + accions::pasarela = textfile::getIntValue("pasarela"); + accions::corda = textfile::getIntValue("corda"); - fscanf(f, "inici = %i %i", &ini_x, &ini_y); - fscanf(f, "final = %i %i", &fin_x, &fin_y); + ini_x = textfile::getIntValue("inici"); + ini_y = std::stoi(textfile::getNextToken()); + + fin_x = textfile::getIntValue("final"); + fin_y = std::stoi(textfile::getNextToken()); //int tilemap[200]; //for (int y=0; y<10; ++y) for (int x=0; x<10; ++x) fscanf(f, "%i", &tilemap[x+y*20]); @@ -82,7 +82,7 @@ namespace mapa draw::freeSurface(tiles); - fclose(f); + textfile::fclose(); draw::surface *marcador = draw::loadSurface("marcador.gif"); draw::setSource(marcador); diff --git a/tools/level_reader.cpp b/tools/level_reader.cpp index ad12dc1..787afb3 100644 --- a/tools/level_reader.cpp +++ b/tools/level_reader.cpp @@ -78,6 +78,22 @@ int main(int argc, char *argv[]) printf("arounders = %i\n", textfile::getIntValue("arounders") ); printf("necessaris = %i\n", textfile::getIntValue("necessaris") ); + printf("parar = %i\n", textfile::getIntValue("parar") ); + printf("cavar = %i\n", textfile::getIntValue("cavar") ); + printf("escalar = %i\n", textfile::getIntValue("escalar") ); + printf("perforar = %i\n", textfile::getIntValue("perforar") ); + printf("escalera = %i\n", textfile::getIntValue("escalera") ); + printf("pasarela = %i\n", textfile::getIntValue("pasarela") ); + printf("corda = %i\n", textfile::getIntValue("corda") ); + + int a = textfile::getIntValue("inici"); + int b = std::stoi(textfile::getNextToken()); + printf("inici = %i, %i\n", a, b ); + + a = textfile::getIntValue("final"); + b = std::stoi(textfile::getNextToken()); + printf("final = %i, %i\n", a, b ); + textfile::close(); return 0; } \ No newline at end of file