190 lines
7.6 KiB
C++
190 lines
7.6 KiB
C++
/* Board libraries are scanned off the UI thread, with compact cached previews. */
|
|
#define BOARD_THUMB_SIZE 96
|
|
typedef struct {
|
|
char path[PATH_MAX], name[256];
|
|
unsigned char preview[BOARD_THUMB_SIZE * BOARD_THUMB_SIZE];
|
|
int resolution, population;
|
|
} BoardEntry;
|
|
typedef struct {
|
|
BoardEntry *items;
|
|
size_t count;
|
|
bool ready;
|
|
char message[100];
|
|
} BoardCatalog;
|
|
static BoardCatalog catalogs[2], catalogResult;
|
|
static pthread_t catalogThread;
|
|
static pthread_mutex_t catalogMutex = PTHREAD_MUTEX_INITIALIZER;
|
|
static bool catalogBusy, catalogDone;
|
|
static int catalogWhich, catalogPending = -1;
|
|
static char catalogDirectory[PATH_MAX];
|
|
|
|
static bool appendBoardEntry(const char *path, const char *name) {
|
|
EditSnapshot board;
|
|
char error[160];
|
|
if (!readBoardFile(path, &board, error)) {
|
|
snprintf(catalogResult.message, sizeof(catalogResult.message), "Some unreadable boards skipped.");
|
|
return true;
|
|
}
|
|
BoardEntry *items = realloc(catalogResult.items, (catalogResult.count + 1) * sizeof(*items));
|
|
if (!items) {
|
|
snprintf(catalogResult.message, sizeof(catalogResult.message), "Not enough memory for more boards.");
|
|
return false;
|
|
}
|
|
catalogResult.items = items;
|
|
BoardEntry *entry = &items[catalogResult.count++];
|
|
memset(entry, 0, sizeof(*entry));
|
|
snprintf(entry->path, sizeof(entry->path), "%s", path);
|
|
snprintf(entry->name, sizeof(entry->name), "%s", name);
|
|
entry->resolution = board.resolution;
|
|
entry->population = board.population;
|
|
/* Fit the occupied bounds so small formations remain legible in the gallery. */
|
|
int minX = board.resolution, minY = board.resolution, maxX = -1, maxY = -1;
|
|
for (int y = 0; y < board.resolution; ++y)
|
|
for (int x = 0; x < board.resolution; ++x)
|
|
if (board.cells[y * board.resolution + x]) {
|
|
if (x < minX) minX = x;
|
|
if (x > maxX) maxX = x;
|
|
if (y < minY) minY = y;
|
|
if (y > maxY) maxY = y;
|
|
}
|
|
int width = maxX - minX + 1, height = maxY - minY + 1;
|
|
int span = (width > height ? width : height) + 4;
|
|
if (span < 8) span = 8;
|
|
int originX = minX - (span - width) / 2, originY = minY - (span - height) / 2;
|
|
for (int y = minY; y <= maxY; ++y)
|
|
for (int x = minX; x <= maxX; ++x)
|
|
if (board.cells[y * board.resolution + x]) {
|
|
int x0 = (x - originX) * BOARD_THUMB_SIZE / span;
|
|
int y0 = (y - originY) * BOARD_THUMB_SIZE / span;
|
|
int x1 = (x + 1 - originX) * BOARD_THUMB_SIZE / span;
|
|
int y1 = (y + 1 - originY) * BOARD_THUMB_SIZE / span;
|
|
if (x1 <= x0) x1 = x0 + 1;
|
|
if (y1 <= y0) y1 = y0 + 1;
|
|
for (int py = y0; py < y1; ++py)
|
|
for (int px = x0; px < x1; ++px)
|
|
entry->preview[py * BOARD_THUMB_SIZE + px] = 1;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static int compareBoardEntries(const void *a, const void *b) {
|
|
return strcmp(((const BoardEntry *)a)->name, ((const BoardEntry *)b)->name);
|
|
}
|
|
|
|
static void *scanBoardCatalog(void *unused) {
|
|
(void)unused;
|
|
char path[PATH_MAX];
|
|
if (catalogWhich == 0) {
|
|
for (int i = 0; i < EXAMPLE_COUNT; ++i) {
|
|
if (snprintf(path, sizeof(path), "%s%s", catalogDirectory, exampleFiles[i] + 9) >= (int)sizeof(path)) continue;
|
|
if (!appendBoardEntry(path, exampleNames[i])) break;
|
|
}
|
|
} else {
|
|
DIR *directory = opendir(catalogDirectory);
|
|
if (directory) {
|
|
struct dirent *item;
|
|
while ((item = readdir(directory))) {
|
|
size_t length = strlen(item->d_name);
|
|
if (item->d_name[0] == '.' || length < 6 || strcmp(item->d_name + length - 5, ".life")) continue;
|
|
if (snprintf(path, sizeof(path), "%s%s", catalogDirectory, item->d_name) >= (int)sizeof(path)) continue;
|
|
struct stat info;
|
|
if (lstat(path, &info) != 0 || !S_ISREG(info.st_mode)) continue;
|
|
char name[256];
|
|
snprintf(name, sizeof(name), "%s", item->d_name);
|
|
name[length - 5] = '\0';
|
|
if (!appendBoardEntry(path, name)) break;
|
|
}
|
|
closedir(directory);
|
|
if (catalogResult.count > 1)
|
|
qsort(catalogResult.items, catalogResult.count, sizeof(BoardEntry), compareBoardEntries);
|
|
} else if (errno != ENOENT) {
|
|
snprintf(catalogResult.message, sizeof(catalogResult.message), "Cannot read the boards folder.");
|
|
}
|
|
}
|
|
catalogResult.ready = true;
|
|
pthread_mutex_lock(&catalogMutex);
|
|
catalogDone = true;
|
|
pthread_mutex_unlock(&catalogMutex);
|
|
return NULL;
|
|
}
|
|
|
|
static void requestBoardCatalog(int which) {
|
|
if (catalogBusy) { catalogPending = which; return; }
|
|
catalogWhich = which;
|
|
boardPath(catalogDirectory, which == 0 ? "examples/" : "");
|
|
memset(&catalogResult, 0, sizeof(catalogResult));
|
|
catalogDone = false;
|
|
if (pthread_create(&catalogThread, NULL, scanBoardCatalog, NULL) == 0) catalogBusy = true;
|
|
else {
|
|
catalogs[which].ready = true;
|
|
snprintf(catalogs[which].message, sizeof(catalogs[which].message), "Cannot load board previews.");
|
|
}
|
|
}
|
|
|
|
static void pollBoardCatalog(void) {
|
|
if (!catalogBusy) return;
|
|
pthread_mutex_lock(&catalogMutex);
|
|
bool done = catalogDone;
|
|
pthread_mutex_unlock(&catalogMutex);
|
|
if (!done) return;
|
|
pthread_join(catalogThread, NULL);
|
|
catalogBusy = false;
|
|
free(catalogs[catalogWhich].items);
|
|
catalogs[catalogWhich] = catalogResult;
|
|
memset(&catalogResult, 0, sizeof(catalogResult));
|
|
if (catalogPending >= 0) {
|
|
int which = catalogPending;
|
|
catalogPending = -1;
|
|
requestBoardCatalog(which);
|
|
}
|
|
}
|
|
|
|
static void closeBoardCatalogs(void) {
|
|
if (catalogBusy) { pthread_join(catalogThread, NULL); free(catalogResult.items); }
|
|
for (int i = 0; i < 2; ++i) free(catalogs[i].items);
|
|
}
|
|
|
|
static void changeSidebarPage(int page) {
|
|
finishLoopInput();
|
|
drawing = editPending = false;
|
|
activeSlider = -1;
|
|
previousWidgetCount = widgetCount;
|
|
memcpy(previousWidgets, widgets, sizeof(widgets));
|
|
pageDirection = page == PAGE_CONTROLS ? -1 : 1;
|
|
pageSlideStarted = notificationTime();
|
|
if (sidebarPage == PAGE_CONTROLS) returnScroll = panelScroll;
|
|
sidebarPage = page;
|
|
panelScroll = page == PAGE_CONTROLS ? returnScroll : 0;
|
|
if (!showControls) setControlsVisible(true);
|
|
if (page == PAGE_SAVE) {
|
|
paused = true;
|
|
boardName[0] = '\0';
|
|
confirmReplace = false;
|
|
}
|
|
if (page == PAGE_OPEN || page == PAGE_EXAMPLES) requestBoardCatalog(page == PAGE_OPEN);
|
|
}
|
|
|
|
/* A plain basename only: keep saves inside boards/, never in examples/. */
|
|
static bool namedBoardPath(char path[PATH_MAX]) {
|
|
char name[sizeof(boardName) + 6];
|
|
snprintf(name, sizeof(name), "%s", boardName);
|
|
size_t length = strlen(name);
|
|
while (length && name[length - 1] == ' ') name[--length] = '\0';
|
|
if (!length || name[0] == '.' || name[0] == ' ' || strchr(name, '/') || strchr(name, '\\')) return false;
|
|
if (length < 5 || strcmp(name + length - 5, ".life")) strcat(name, ".life");
|
|
boardPath(path, name);
|
|
return path[0] != '\0';
|
|
}
|
|
|
|
static void submitBoardName(void) {
|
|
char path[PATH_MAX];
|
|
if (!namedBoardPath(path)) { notifyBoard("Enter a board name without slashes or leading dots/spaces."); return; }
|
|
struct stat info;
|
|
if (lstat(path, &info) == 0 && !confirmReplace) {
|
|
confirmReplace = true;
|
|
notifyBoard("Name exists. Click Replace existing board to overwrite it.");
|
|
return;
|
|
}
|
|
startBoardPath(true, path, false, confirmReplace);
|
|
}
|