1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Replace FILE by SDL_RWops for android assets support

This commit is contained in:
Mathieu Schroeter 2018-06-09 16:06:23 +02:00
parent 0e8f8e8bef
commit 671b2f32e9
3 changed files with 63 additions and 63 deletions

View File

@ -109,7 +109,7 @@ bool
CDecor::Write (Sint32 rank, bool bUser, Sint32 world, Sint32 time, Sint32 total) CDecor::Write (Sint32 rank, bool bUser, Sint32 world, Sint32 time, Sint32 total)
{ {
std::string filename; std::string filename;
FILE * file = nullptr; SDL_RWops * file = nullptr;
DescFile * pBuffer = nullptr; DescFile * pBuffer = nullptr;
Sint32 i; Sint32 i;
size_t nb; size_t nb;
@ -125,7 +125,7 @@ CDecor::Write (Sint32 rank, bool bUser, Sint32 world, Sint32 time, Sint32 total)
AddUserPath (filename); AddUserPath (filename);
} }
file = fopen (filename.c_str (), "wb"); file = SDL_RWFromFile (filename.c_str (), "wb");
if (file == nullptr) if (file == nullptr)
goto error; goto error;
@ -157,35 +157,35 @@ CDecor::Write (Sint32 rank, bool bUser, Sint32 world, Sint32 time, Sint32 total)
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
pBuffer->memoPos[i] = m_memoPos[i]; pBuffer->memoPos[i] = m_memoPos[i];
nb = fwrite (pBuffer, sizeof (DescFile), 1, file); nb = SDL_RWwrite (file, pBuffer, sizeof (DescFile), 1);
if (nb < 1) if (nb < 1)
goto error; goto error;
nb = fwrite (m_decor, sizeof (Cellule), MAXCELX * MAXCELY / 4, file); nb = SDL_RWwrite (file, m_decor, sizeof (Cellule), MAXCELX * MAXCELY / 4);
if (nb < MAXCELX * MAXCELY / 4) if (nb < MAXCELX * MAXCELY / 4)
goto error; goto error;
nb = fwrite (m_blupi, sizeof (Blupi), MAXBLUPI, file); nb = SDL_RWwrite (file, m_blupi, sizeof (Blupi), MAXBLUPI);
if (nb < MAXBLUPI) if (nb < MAXBLUPI)
goto error; goto error;
nb = fwrite (m_move, sizeof (Move), MAXMOVE, file); nb = SDL_RWwrite (file, m_move, sizeof (Move), MAXMOVE);
if (nb < MAXMOVE) if (nb < MAXMOVE)
goto error; goto error;
nb = fwrite (m_lastDrapeau, sizeof (Point), MAXLASTDRAPEAU, file); nb = SDL_RWwrite (file, m_lastDrapeau, sizeof (Point), MAXLASTDRAPEAU);
if (nb < MAXLASTDRAPEAU) if (nb < MAXLASTDRAPEAU)
goto error; goto error;
free (pBuffer); free (pBuffer);
fclose (file); SDL_RWclose (file);
return true; return true;
error: error:
if (pBuffer != nullptr) if (pBuffer != nullptr)
free (pBuffer); free (pBuffer);
if (file != nullptr) if (file != nullptr)
fclose (file); SDL_RWclose (file);
return false; return false;
} }
@ -196,7 +196,7 @@ CDecor::Read (
Sint32 rank, bool bUser, Sint32 & world, Sint32 & time, Sint32 & total) Sint32 rank, bool bUser, Sint32 & world, Sint32 & time, Sint32 & total)
{ {
std::string filename; std::string filename;
FILE * file = nullptr; SDL_RWops * file = nullptr;
DescFile * pBuffer = nullptr; DescFile * pBuffer = nullptr;
Sint32 majRev, minRev; Sint32 majRev, minRev;
Sint32 i, x, y; Sint32 i, x, y;
@ -218,7 +218,7 @@ CDecor::Read (
else else
filename = string_format (GetBaseDir () + "data/world%.3d.blp", rank); filename = string_format (GetBaseDir () + "data/world%.3d.blp", rank);
file = fopen (filename.c_str (), "rb"); file = SDL_RWFromFile (filename.c_str (), "rb");
if (file == nullptr) if (file == nullptr)
goto error; goto error;
@ -226,7 +226,7 @@ CDecor::Read (
if (pBuffer == nullptr) if (pBuffer == nullptr)
goto error; goto error;
nb = fread (pBuffer, sizeof (DescFile), 1, file); nb = SDL_RWread (file, pBuffer, sizeof (DescFile), 1);
if (nb < 1) if (nb < 1)
goto error; goto error;
@ -276,7 +276,7 @@ CDecor::Read (
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
m_memoPos[i] = pBuffer->memoPos[i]; m_memoPos[i] = pBuffer->memoPos[i];
nb = fread (m_decor, sizeof (Cellule), MAXCELX * MAXCELY / 4, file); nb = SDL_RWread (file, m_decor, sizeof (Cellule), MAXCELX * MAXCELY / 4);
if (nb < MAXCELX * MAXCELY / 4) if (nb < MAXCELX * MAXCELY / 4)
goto error; goto error;
if (majRev == 1 && minRev < 5) if (majRev == 1 && minRev < 5)
@ -296,7 +296,7 @@ CDecor::Read (
memset (m_blupi, 0, sizeof (Blupi) * MAXBLUPI); memset (m_blupi, 0, sizeof (Blupi) * MAXBLUPI);
for (i = 0; i < MAXBLUPI; i++) for (i = 0; i < MAXBLUPI; i++)
{ {
nb = fread (&oldBlupi, sizeof (OldBlupi), 1, file); nb = SDL_RWread (file, &oldBlupi, sizeof (OldBlupi), 1);
if (nb != 1) if (nb != 1)
goto error; goto error;
memcpy (m_blupi + i, &oldBlupi, sizeof (OldBlupi)); memcpy (m_blupi + i, &oldBlupi, sizeof (OldBlupi));
@ -305,30 +305,30 @@ CDecor::Read (
} }
else else
{ {
nb = fread (m_blupi, sizeof (Blupi), MAXBLUPI, file); nb = SDL_RWread (file, m_blupi, sizeof (Blupi), MAXBLUPI);
if (nb < MAXBLUPI) if (nb < MAXBLUPI)
goto error; goto error;
} }
nb = fread (m_move, sizeof (Move), MAXMOVE, file); nb = SDL_RWread (file, m_move, sizeof (Move), MAXMOVE);
if (nb < MAXMOVE) if (nb < MAXMOVE)
goto error; goto error;
nb = fread (m_lastDrapeau, sizeof (Point), MAXLASTDRAPEAU, file); nb = SDL_RWread (file, m_lastDrapeau, sizeof (Point), MAXLASTDRAPEAU);
if (nb < MAXLASTDRAPEAU) if (nb < MAXLASTDRAPEAU)
InitDrapeau (); InitDrapeau ();
BlupiDeselect (); // désélectionne tous les blupi BlupiDeselect (); // désélectionne tous les blupi
free (pBuffer); free (pBuffer);
fclose (file); SDL_RWclose (file);
return true; return true;
error: error:
if (pBuffer != nullptr) if (pBuffer != nullptr)
free (pBuffer); free (pBuffer);
if (file != nullptr) if (file != nullptr)
fclose (file); SDL_RWclose (file);
Flush (); // initialise un décor neutre Flush (); // initialise un décor neutre
return false; return false;
@ -341,7 +341,7 @@ CDecor::FileExist (
Sint32 rank, bool bUser, Sint32 & world, Sint32 & time, Sint32 & total) Sint32 rank, bool bUser, Sint32 & world, Sint32 & time, Sint32 & total)
{ {
std::string filename; std::string filename;
FILE * file = nullptr; SDL_RWops * file = nullptr;
DescFile * pBuffer = nullptr; DescFile * pBuffer = nullptr;
Sint32 majRev, minRev; Sint32 majRev, minRev;
size_t nb; size_t nb;
@ -359,7 +359,7 @@ CDecor::FileExist (
else else
filename = string_format (GetBaseDir () + "data/world%.3d.blp", rank); filename = string_format (GetBaseDir () + "data/world%.3d.blp", rank);
file = fopen (filename.c_str (), "rb"); file = SDL_RWFromFile (filename.c_str (), "rb");
if (file == nullptr) if (file == nullptr)
goto error; goto error;
@ -367,7 +367,7 @@ CDecor::FileExist (
if (pBuffer == nullptr) if (pBuffer == nullptr)
goto error; goto error;
nb = fread (pBuffer, sizeof (DescFile), 1, file); nb = SDL_RWread (file, pBuffer, sizeof (DescFile), 1);
if (nb < 1) if (nb < 1)
goto error; goto error;
@ -401,14 +401,14 @@ CDecor::FileExist (
total = pBuffer->totalTime; total = pBuffer->totalTime;
free (pBuffer); free (pBuffer);
fclose (file); SDL_RWclose (file);
return true; return true;
error: error:
if (pBuffer != nullptr) if (pBuffer != nullptr)
free (pBuffer); free (pBuffer);
if (file != nullptr) if (file != nullptr)
fclose (file); SDL_RWclose (file);
return false; return false;
} }

View File

@ -4651,13 +4651,13 @@ CEvent::PrivateLibelle ()
bool bool
CEvent::ReadLibelle (Sint32 world, bool bSchool, bool bHelp) CEvent::ReadLibelle (Sint32 world, bool bSchool, bool bHelp)
{ {
FILE * file = nullptr; SDL_RWops * file = nullptr;
char * pBuffer = nullptr; char * pBuffer = nullptr;
char * pText; char * pText;
char * pDest; char * pDest;
char indic; char indic;
Sint32 h1, h2; Sint32 h1, h2;
size_t nb; size_t nb;
if (bSchool) if (bSchool)
indic = '$'; indic = '$';
@ -4674,17 +4674,17 @@ CEvent::ReadLibelle (Sint32 world, bool bSchool, bool bHelp)
memset (pBuffer, 0, sizeof (char) * 50000); memset (pBuffer, 0, sizeof (char) * 50000);
file = fopen (stories.c_str (), "rb"); file = SDL_RWFromFile (stories.c_str (), "rb");
if (file == nullptr) if (file == nullptr)
{ {
/* Try with the fallback locale */ /* Try with the fallback locale */
stories = GetBaseDir () + "data/en/stories.blp"; stories = GetBaseDir () + "data/en/stories.blp";
file = fopen (stories.c_str (), "rb"); file = SDL_RWFromFile (stories.c_str (), "rb");
if (!file) if (!file)
goto error; goto error;
} }
nb = fread (pBuffer, sizeof (char), 50000 - 1, file); nb = SDL_RWread (file, pBuffer, sizeof (char), 50000 - 1);
pBuffer[nb] = 0; pBuffer[nb] = 0;
pText = pBuffer; pText = pBuffer;
@ -4713,14 +4713,14 @@ CEvent::ReadLibelle (Sint32 world, bool bSchool, bool bHelp)
m_pDecor->SetInfoHeight (POSDRAWY + h1 + 10); m_pDecor->SetInfoHeight (POSDRAWY + h1 + 10);
free (pBuffer); free (pBuffer);
fclose (file); SDL_RWclose (file);
return true; return true;
error: error:
if (pBuffer != nullptr) if (pBuffer != nullptr)
free (pBuffer); free (pBuffer);
if (file != nullptr) if (file != nullptr)
fclose (file); SDL_RWclose (file);
return false; return false;
} }
@ -4730,14 +4730,14 @@ bool
CEvent::WriteInfo () CEvent::WriteInfo ()
{ {
std::string filename; std::string filename;
FILE * file = nullptr; SDL_RWops * file = nullptr;
DescInfo info = {0}; DescInfo info = {0};
size_t nb; size_t nb;
filename = "data/info.blp"; filename = "data/info.blp";
AddUserPath (filename); AddUserPath (filename);
file = fopen (filename.c_str (), "wb"); file = SDL_RWFromFile (filename.c_str (), "wb");
if (file == nullptr) if (file == nullptr)
goto error; goto error;
@ -4765,16 +4765,16 @@ CEvent::WriteInfo ()
info.fullScreen = g_bFullScreen; info.fullScreen = g_bFullScreen;
info.zoom = g_zoom; info.zoom = g_zoom;
nb = fwrite (&info, sizeof (info), 1, file); nb = SDL_RWwrite (file, &info, sizeof (info), 1);
if (nb < 1) if (nb < 1)
goto error; goto error;
fclose (file); SDL_RWclose (file);
return true; return true;
error: error:
if (file != nullptr) if (file != nullptr)
fclose (file); SDL_RWclose (file);
return false; return false;
} }
@ -4784,20 +4784,20 @@ bool
CEvent::ReadInfo () CEvent::ReadInfo ()
{ {
std::string filename; std::string filename;
FILE * file = nullptr; SDL_RWops * file = nullptr;
DescInfo info; DescInfo info;
size_t nb; size_t nb;
filename = "data/info.blp"; filename = "data/info.blp";
AddUserPath (filename); AddUserPath (filename);
file = fopen (filename.c_str (), "rb"); file = SDL_RWFromFile (filename.c_str (), "rb");
if (file == nullptr) if (file == nullptr)
goto error; goto error;
SDL_memset (&info, 0, sizeof (info)); SDL_memset (&info, 0, sizeof (info));
nb = fread (&info, sizeof (DescInfo), 1, file); nb = SDL_RWread (file, &info, sizeof (DescInfo), 1);
if (nb < 1) if (nb < 1)
goto error; goto error;
@ -4832,12 +4832,12 @@ CEvent::ReadInfo ()
g_zoom = info.zoom; g_zoom = info.zoom;
} }
fclose (file); SDL_RWclose (file);
return true; return true;
error: error:
if (file != nullptr) if (file != nullptr)
fclose (file); SDL_RWclose (file);
return false; return false;
} }
@ -4895,8 +4895,8 @@ CEvent::DemoRecStart ()
void void
CEvent::DemoRecStop () CEvent::DemoRecStop ()
{ {
FILE * file = nullptr; SDL_RWops * file = nullptr;
DemoHeader header; DemoHeader header;
if (m_bDemoPlay || !m_bDemoRec) if (m_bDemoPlay || !m_bDemoRec)
return; return;
@ -4907,7 +4907,7 @@ CEvent::DemoRecStop ()
AddUserPath (demoPath); AddUserPath (demoPath);
unlink (demoPath.c_str ()); unlink (demoPath.c_str ());
file = fopen (demoPath.c_str (), "wb"); file = SDL_RWFromFile (demoPath.c_str (), "wb");
if (file) if (file)
{ {
memset (&header, 0, sizeof (DemoHeader)); memset (&header, 0, sizeof (DemoHeader));
@ -4918,10 +4918,10 @@ CEvent::DemoRecStop ()
header.world = GetPhysicalWorld (); header.world = GetPhysicalWorld ();
header.skill = m_pDecor->GetSkill (); header.skill = m_pDecor->GetSkill ();
fwrite (&header, sizeof (DemoHeader), 1, file); SDL_RWwrite (file, &header, sizeof (DemoHeader), 1);
for (const auto buffer : m_pDemoSDLBuffer) for (const auto buffer : m_pDemoSDLBuffer)
fwrite (&buffer, sizeof (DemoSDLEvent), 1, file); SDL_RWwrite (file, &buffer, sizeof (DemoSDLEvent), 1);
fclose (file); SDL_RWclose (file);
} }
m_pDemoSDLBuffer.clear (); m_pDemoSDLBuffer.clear ();
@ -4937,7 +4937,7 @@ bool
CEvent::DemoPlayStart (const std::string * demoFile) CEvent::DemoPlayStart (const std::string * demoFile)
{ {
std::string filename; std::string filename;
FILE * file = nullptr; SDL_RWops * file = nullptr;
DemoHeader header; DemoHeader header;
Sint32 world, time, total; Sint32 world, time, total;
size_t nb; size_t nb;
@ -4946,17 +4946,17 @@ CEvent::DemoPlayStart (const std::string * demoFile)
demoFile demoFile
? *demoFile ? *demoFile
: string_format (GetBaseDir () + "data/demo%.3d.blp", m_demoNumber); : string_format (GetBaseDir () + "data/demo%.3d.blp", m_demoNumber);
file = fopen (filename.c_str (), "rb"); file = SDL_RWFromFile (filename.c_str (), "rb");
if (file == nullptr) if (file == nullptr)
{ {
DemoPlayStop (); DemoPlayStop ();
return false; return false;
} }
nb = fread (&header, sizeof (DemoHeader), 1, file); nb = SDL_RWread (file, &header, sizeof (DemoHeader), 1);
if (nb < 1) if (nb < 1)
{ {
fclose (file); SDL_RWclose (file);
DemoPlayStop (); DemoPlayStop ();
return false; return false;
} }
@ -4966,7 +4966,7 @@ CEvent::DemoPlayStart (const std::string * demoFile)
m_pDemoBuffer = (DemoEvent *) malloc (MAXDEMO * sizeof (DemoEvent)); m_pDemoBuffer = (DemoEvent *) malloc (MAXDEMO * sizeof (DemoEvent));
if (m_pDemoBuffer == nullptr) if (m_pDemoBuffer == nullptr)
{ {
fclose (file); SDL_RWclose (file);
DemoPlayStop (); DemoPlayStop ();
return false; return false;
} }
@ -4978,14 +4978,14 @@ CEvent::DemoPlayStart (const std::string * demoFile)
m_pDecor->SetSkill (header.skill); m_pDecor->SetSkill (header.skill);
if (header.majRev == 1) if (header.majRev == 1)
m_demoEnd = fread (m_pDemoBuffer, sizeof (DemoEvent), MAXDEMO, file); m_demoEnd = SDL_RWread (file, m_pDemoBuffer, sizeof (DemoEvent), MAXDEMO);
else if (header.majRev == 2) else if (header.majRev == 2)
{ {
DemoSDLEvent demoEvent; DemoSDLEvent demoEvent;
for (;;) for (;;)
{ {
auto res = fread (&demoEvent, sizeof (DemoSDLEvent), 1, file); auto res = SDL_RWread (file, &demoEvent, sizeof (DemoSDLEvent), 1);
if (res != 1) if (res != 1)
break; break;
@ -4994,7 +4994,7 @@ CEvent::DemoPlayStart (const std::string * demoFile)
m_demoEnd = m_pDemoSDLBuffer.size (); m_demoEnd = m_pDemoSDLBuffer.size ();
} }
fclose (file); SDL_RWclose (file);
m_demoTime = 0; m_demoTime = 0;
m_demoIndex = 0; m_demoIndex = 0;

View File

@ -164,7 +164,7 @@ FileExists (
const std::string & filename, std::string & absolute, enum Location location) const std::string & filename, std::string & absolute, enum Location location)
{ {
absolute = filename; absolute = filename;
FILE * file; SDL_RWops * file;
switch (location) switch (location)
{ {
@ -181,10 +181,10 @@ FileExists (
break; break;
} }
file = fopen (absolute.c_str (), "rb"); file = SDL_RWFromFile (absolute.c_str (), "rb");
if (file == nullptr) if (file == nullptr)
return false; return false;
fclose (file); SDL_RWclose (file);
return true; return true;
} }