all repos — mgba @ 7e74cba49a7ed6408897f3a946319899a43a3f12

mGBA Game Boy Advance Emulator

GUI: Start moving state out of locals
Jeffrey Pfau jeffrey@endrift.com
Sat, 29 Aug 2015 23:21:41 -0700
commit

7e74cba49a7ed6408897f3a946319899a43a3f12

parent

dd3b56eb7a3d6fc30f59267bb3b444b611380f8b

4 files changed, 48 insertions(+), 24 deletions(-)

jump to
A src/util/gui.c

@@ -0,0 +1,27 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "gui.h" + +void GUIPollInput(struct GUIParams* params, int* newInputOut, int* heldInput) { + int input = params->pollInput(); + int newInput = 0; + for (int i = 0; i < GUI_INPUT_MAX; ++i) { + if (input & (1 << i)) { + ++params->inputHistory[i]; + } else { + params->inputHistory[i] = -1; + } + if (!params->inputHistory[i] || (params->inputHistory[i] >= 30 && !(params->inputHistory[i] % 6))) { + newInput |= (1 << i); + } + } + if (newInputOut) { + *newInputOut = newInput; + } + if (heldInput) { + *heldInput = input; + } +}
M src/util/gui.hsrc/util/gui.h

@@ -32,6 +32,11 @@

void (*drawStart)(void); void (*drawEnd)(void); int (*pollInput)(void); + + // State + int inputHistory[GUI_INPUT_MAX]; }; + +void GUIPollInput(struct GUIParams* params, int* newInput, int* heldInput); #endif
M src/util/gui/file-select.csrc/util/gui/file-select.c

@@ -46,7 +46,7 @@ static int _strpcmp(const void* a, const void* b) {

return strcmp(*(const char**) a, *(const char**) b); } -static bool _refreshDirectory(const struct GUIParams* params, const char* currentPath, struct FileList* currentFiles, bool (*filter)(struct VFile*)) { +static bool _refreshDirectory(struct GUIParams* params, const char* currentPath, struct FileList* currentFiles, bool (*filter)(struct VFile*)) { _cleanFiles(currentFiles); struct VDir* dir = VDirOpen(currentPath);

@@ -59,7 +59,8 @@ struct VDirEntry* de;

while ((de = dir->listNext(dir))) { ++i; if (i % SCANNING_THRESHOLD == SCANNING_THRESHOLD - 1) { - int input = params->pollInput(); + int input = 0; + GUIPollInput(params, &input, 0); if (input & (1 << GUI_INPUT_CANCEL)) { return false; }

@@ -90,7 +91,7 @@ qsort(FileListGetPointer(currentFiles, 1), FileListSize(currentFiles) - 1, sizeof(char*), _strpcmp);

return true; } -bool selectFile(const struct GUIParams* params, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)) { +bool selectFile(struct GUIParams* params, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)) { if (!currentPath[0]) { strncpy(currentPath, basePath, outLen); }

@@ -107,21 +108,9 @@ struct FileList currentFiles;

FileListInit(&currentFiles, 0); _refreshDirectory(params, currentPath, &currentFiles, filter); - int inputHistory[GUI_INPUT_MAX] = { 0 }; - while (true) { - int input = params->pollInput(); int newInput = 0; - for (int i = 0; i < GUI_INPUT_MAX; ++i) { - if (input & (1 << i)) { - ++inputHistory[i]; - } else { - inputHistory[i] = -1; - } - if (!inputHistory[i] || (inputHistory[i] >= 30 && !(inputHistory[i] % 6))) { - newInput |= (1 << i); - } - } + GUIPollInput(params, &newInput, 0); if (newInput & (1 << GUI_INPUT_UP) && fileIndex > 0) { --fileIndex;

@@ -167,21 +156,24 @@ sep = "";

} snprintf(outPath, outLen, "%s%s%s", currentPath, sep, *FileListGetPointer(&currentFiles, fileIndex)); if (!_refreshDirectory(params, outPath, &currentFiles, filter)) { - struct VFile* vf = VFileOpen(currentPath, O_RDONLY); + struct VFile* vf = VFileOpen(outPath, O_RDONLY); if (!vf) { - break; + if (!_refreshDirectory(params, currentPath, &currentFiles, filter)) { + break; + } + continue; } if (!filter || filter(vf)) { vf->close(vf); + _cleanFiles(&currentFiles); + FileListDeinit(&currentFiles); return true; } vf->close(vf); - _upDirectory(currentPath); - if (!_refreshDirectory(params, currentPath, &currentFiles, filter)) { - break; - } + break; + } else { + strncpy(currentPath, outPath, outLen); } - strncpy(currentPath, outPath, outLen); } fileIndex = 0; }
M src/util/gui/file-select.hsrc/util/gui/file-select.h

@@ -10,6 +10,6 @@ #include "util/gui.h"

struct VFile; -bool selectFile(const struct GUIParams*, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)); +bool selectFile(struct GUIParams*, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)); #endif