Skip to content

Commit e923049

Browse files
committed
Texture.h: Added version handling in Texture::load().
* Thus making it future-incompatible. * Then we will merge this into feature/termin8or-256-colors and add compatibility to version 2. * Heads up for future merge conflict with said branch.
1 parent b3fc0ba commit e923049

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

include/Termin8or/drawing/Texture.h

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../screen/Color.h"
1212
#include "../screen/Styles.h"
1313
#include <Core/TextIO.h>
14+
#include <Core/StringHelper.h>
1415
#include <sstream>
1516

1617

@@ -55,6 +56,7 @@ namespace t8
5556

5657
struct Texture
5758
{
59+
int ver = 0;
5860
RC size;
5961
int area = 0;
6062
std::vector<char> characters;
@@ -251,15 +253,49 @@ namespace t8
251253
{
252254
if (section == 0)
253255
{
254-
std::istringstream iss(l);
255-
iss >> size.r >> size.c;
256-
area = size.r * size.c;
257-
characters.resize(area, ' ');
258-
fg_colors.resize(area, Color::Default);
259-
bg_colors.resize(area, Color::Transparent2);
260-
materials.resize(area, -1);
261-
section = 1;
262-
r = 0;
256+
if (!l.empty())
257+
{
258+
if (l.starts_with("VER"))
259+
{
260+
auto tokens = str::tokenize(l, { ' ' });
261+
if (tokens.size() == 2)
262+
{
263+
// Absence of VER line means it is version 1.
264+
int ver_parsed = std::stoi(tokens[1]);
265+
// Change from 1 to current version for making sure it is future-incompatible.
266+
static const int compatible_version_until_and_including = 1;
267+
if (ver_parsed <= compatible_version_until_and_including)
268+
ver = ver_parsed;
269+
else
270+
{
271+
std::cerr << "ERROR in Texture::load() : Incompatible texture version: version = " + tokens[1] << '\n';
272+
return false;
273+
}
274+
}
275+
else
276+
ver = 1; // Absence of VER line means it is version 1.
277+
}
278+
else
279+
{
280+
std::istringstream iss(l);
281+
iss >> size.r >> size.c;
282+
area = size.r * size.c;
283+
characters.resize(area, ' ');
284+
fg_colors.resize(area, Color::Default);
285+
bg_colors.resize(area, Color::Transparent2);
286+
materials.resize(area, -1);
287+
}
288+
r++;
289+
}
290+
else if (r == 1 || r == 2) // 2 if there is a VER line.
291+
{
292+
section = 1;
293+
r = 0;
294+
}
295+
else if (r > 2)
296+
{
297+
std::cerr << "ERROR in Texture::parse() : Incorrect number of header lines.\n";
298+
}
263299
}
264300
else if (section == 1)
265301
{

0 commit comments

Comments
 (0)