Fixed multithreaded atlas loading.

Sometimes I wonder what I was thinking.
This commit is contained in:
NathanSweet 2015-11-04 07:33:02 +01:00
parent 3b5109032c
commit 1a628a66f7

View File

@ -77,23 +77,18 @@ static void trim (Str* str) {
}
/* Tokenize string without modification. Returns 0 on failure. */
static int readLine (const char* begin, const char* end, Str* str) {
static const char* nextStart;
if (begin) {
nextStart = begin;
return 1;
}
if (nextStart == end) return 0;
str->begin = nextStart;
static int readLine (const char** begin, const char* end, Str* str) {
if (*begin == end) return 0;
str->begin = *begin;
/* Find next delimiter. */
while (nextStart != end && *nextStart != '\n')
nextStart++;
while (*begin != end && **begin != '\n')
(*begin)++;
str->end = nextStart;
str->end = *begin;
trim(str);
if (nextStart != end) nextStart++;
if (*begin != end) (*begin)++;
return 1;
}
@ -111,18 +106,18 @@ static int beginPast (Str* str, char c) {
}
/* Returns 0 on failure. */
static int readValue (const char* end, Str* str) {
readLine(0, end, str);
static int readValue (const char** begin, const char* end, Str* str) {
readLine(begin, end, str);
if (!beginPast(str, ':')) return 0;
trim(str);
return 1;
}
/* Returns the number of tuple values read (1, 2, 4, or 0 for failure). */
static int readTuple (const char* end, Str tuple[]) {
static int readTuple (const char** begin, const char* end, Str tuple[]) {
int i;
Str str = {NULL, NULL};
readLine(0, end, &str);
readLine(begin, end, &str);
if (!beginPast(&str, ':')) return 0;
for (i = 0; i < 3; ++i) {
@ -187,8 +182,7 @@ spAtlas* spAtlas_create (const char* begin, int length, const char* dir, void* r
self = NEW(spAtlas);
self->rendererObject = rendererObject;
readLine(begin, 0, 0);
while (readLine(0, end, &str)) {
while (readLine(&begin, end, &str)) {
if (str.end - str.begin == 0) {
page = 0;
} else if (!page) {
@ -206,21 +200,21 @@ spAtlas* spAtlas_create (const char* begin, int length, const char* dir, void* r
self->pages = page;
lastPage = page;
switch (readTuple(end, tuple)) {
switch (readTuple(&begin, end, tuple)) {
case 0:
return abortAtlas(self);
case 2: /* size is only optional for an atlas packed with an old TexturePacker. */
page->width = toInt(tuple);
page->height = toInt(tuple + 1);
if (!readTuple(end, tuple)) return abortAtlas(self);
if (!readTuple(&begin, end, tuple)) return abortAtlas(self);
}
page->format = (spAtlasFormat)indexOf(formatNames, 7, tuple);
if (!readTuple(end, tuple)) return abortAtlas(self);
if (!readTuple(&begin, end, tuple)) return abortAtlas(self);
page->minFilter = (spAtlasFilter)indexOf(textureFilterNames, 7, tuple);
page->magFilter = (spAtlasFilter)indexOf(textureFilterNames, 7, tuple + 1);
if (!readValue(end, &str)) return abortAtlas(self);
if (!readValue(&begin, end, &str)) return abortAtlas(self);
if (!equals(&str, "none")) {
page->uWrap = *str.begin == 'x' ? SP_ATLAS_REPEAT : (*str.begin == 'y' ? SP_ATLAS_CLAMPTOEDGE : SP_ATLAS_REPEAT);
page->vWrap = *str.begin == 'x' ? SP_ATLAS_CLAMPTOEDGE : (*str.begin == 'y' ? SP_ATLAS_REPEAT : SP_ATLAS_REPEAT);
@ -239,14 +233,14 @@ spAtlas* spAtlas_create (const char* begin, int length, const char* dir, void* r
region->page = page;
region->name = mallocString(&str);
if (!readValue(end, &str)) return abortAtlas(self);
if (!readValue(&begin, end, &str)) return abortAtlas(self);
region->rotate = equals(&str, "true");
if (readTuple(end, tuple) != 2) return abortAtlas(self);
if (readTuple(&begin, end, tuple) != 2) return abortAtlas(self);
region->x = toInt(tuple);
region->y = toInt(tuple + 1);
if (readTuple(end, tuple) != 2) return abortAtlas(self);
if (readTuple(&begin, end, tuple) != 2) return abortAtlas(self);
region->width = toInt(tuple);
region->height = toInt(tuple + 1);
@ -260,7 +254,7 @@ spAtlas* spAtlas_create (const char* begin, int length, const char* dir, void* r
region->v2 = (region->y + region->height) / (float)page->height;
}
if (!(count = readTuple(end, tuple))) return abortAtlas(self);
if (!(count = readTuple(&begin, end, tuple))) return abortAtlas(self);
if (count == 4) { /* split is optional */
region->splits = MALLOC(int, 4);
region->splits[0] = toInt(tuple);
@ -268,7 +262,7 @@ spAtlas* spAtlas_create (const char* begin, int length, const char* dir, void* r
region->splits[2] = toInt(tuple + 2);
region->splits[3] = toInt(tuple + 3);
if (!(count = readTuple(end, tuple))) return abortAtlas(self);
if (!(count = readTuple(&begin, end, tuple))) return abortAtlas(self);
if (count == 4) { /* pad is optional, but only present with splits */
region->pads = MALLOC(int, 4);
region->pads[0] = toInt(tuple);
@ -276,18 +270,18 @@ spAtlas* spAtlas_create (const char* begin, int length, const char* dir, void* r
region->pads[2] = toInt(tuple + 2);
region->pads[3] = toInt(tuple + 3);
if (!readTuple(end, tuple)) return abortAtlas(self);
if (!readTuple(&begin, end, tuple)) return abortAtlas(self);
}
}
region->originalWidth = toInt(tuple);
region->originalHeight = toInt(tuple + 1);
readTuple(end, tuple);
readTuple(&begin, end, tuple);
region->offsetX = toInt(tuple);
region->offsetY = toInt(tuple + 1);
if (!readValue(end, &str)) return abortAtlas(self);
if (!readValue(&begin, end, &str)) return abortAtlas(self);
region->index = toInt(&str);
}
}