- [NEW] aux_textfile()

- Implementant la càrrega del nivell
This commit is contained in:
2023-10-18 16:18:57 +02:00
parent 3f203fee62
commit fdc604be3c
4 changed files with 112 additions and 21 deletions

57
source/aux_textfile.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "aux_textfile.h"
#include "jgame.h"
#include <stdlib.h>
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 (p<fsize && buffer[p]<=32 ) p++;
// Grab token
while (p<fsize && buffer[p]>32 ) token[tpos++]=buffer[p++];
token[tpos]=0;
return std::string(token);
}
const bool searchToken(std::string token)
{
while (p<fsize && getNextToken() != token) p++;
return p<fsize;
}
std::string getStringValue(std::string token)
{
textfile::searchToken(token);
textfile::searchToken("=");
return textfile::getNextToken();
}
const int getIntValue(std::string token)
{
return std::stoi(getStringValue(token));
}
}

18
source/aux_textfile.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include <string>
namespace textfile
{
const bool open(std::string filename);
void close();
std::string getNextToken();
const bool searchToken(std::string token);
std::string getStringValue(std::string token);
const int getIntValue(std::string token);
}

View File

@@ -1,6 +1,6 @@
#include "proc_mapa.h" #include "proc_mapa.h"
#include "jgame.h" #include "jgame.h"
#include <stdio.h> #include "aux_textfile.h"
namespace mapa namespace mapa
{ {
@@ -38,30 +38,30 @@ namespace mapa
void carregar() void carregar()
{ {
FILE *f = file::getFilePointer("mapes.txt"); textfile::open("mapes.txt");
//const int fase = game::getConfig("fase");
int val;
do { do {
fscanf(f, "LEVEL %i", &val); textfile::searchToken("LEVEL");
} while (val != game::getConfig("fase")); } while (std::stoi(textfile::getNextToken()) != game::getConfig("fase"));
int tileset=0; const int tileset = textfile::getIntValue("tileset");
fscanf(f, "tileset = %i", &tileset); arounders::orientacio_inicial = textfile::getIntValue("orientacio");
fscanf(f, "orientacio = %i", &arounders::orientacio_inicial); arounders::totals = textfile::getIntValue("arounders");
fscanf(f, "arounders = %i", &arounders::totals); arounders::necessaris = textfile::getIntValue("necessaris");
fscanf(f, "necessaris = %i", &arounders::necessaris);
fscanf(f, "parar = %i", &accions::parar); accions::parar = textfile::getIntValue("parar");
fscanf(f, "cavar = %i", &accions::cavar); accions::cavar = textfile::getIntValue("cavar");
fscanf(f, "escalar = %i", &accions::escalar); accions::escalar = textfile::getIntValue("escalar");
fscanf(f, "perforar = %i", &accions::perforar); accions::perforar = textfile::getIntValue("perforar");
fscanf(f, "escalera = %i", &accions::escalera); accions::escalera = textfile::getIntValue("escalera");
fscanf(f, "pasarela = %i", &accions::pasarela); accions::pasarela = textfile::getIntValue("pasarela");
fscanf(f, "corda = %i", &accions::corda); accions::corda = textfile::getIntValue("corda");
fscanf(f, "inici = %i %i", &ini_x, &ini_y); ini_x = textfile::getIntValue("inici");
fscanf(f, "final = %i %i", &fin_x, &fin_y); ini_y = std::stoi(textfile::getNextToken());
fin_x = textfile::getIntValue("final");
fin_y = std::stoi(textfile::getNextToken());
//int tilemap[200]; //int tilemap[200];
//for (int y=0; y<10; ++y) for (int x=0; x<10; ++x) fscanf(f, "%i", &tilemap[x+y*20]); //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); draw::freeSurface(tiles);
fclose(f); textfile::fclose();
draw::surface *marcador = draw::loadSurface("marcador.gif"); draw::surface *marcador = draw::loadSurface("marcador.gif");
draw::setSource(marcador); draw::setSource(marcador);

View File

@@ -78,6 +78,22 @@ int main(int argc, char *argv[])
printf("arounders = %i\n", textfile::getIntValue("arounders") ); printf("arounders = %i\n", textfile::getIntValue("arounders") );
printf("necessaris = %i\n", textfile::getIntValue("necessaris") ); 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(); textfile::close();
return 0; return 0;
} }