puzzles: cut size for c200v2

- font caching is disabled
- font table is dynamically allocated
- side effect: tlsf isn't reset between runs anymore, memory leaks will have a bigger impact

Change-Id: I0b25c22665d956895e8007883d522256010d04ab
This commit is contained in:
Franklin Wei 2017-08-09 21:51:23 -04:00
parent 3e1c8cca92
commit ca228d3d87

View file

@ -38,7 +38,11 @@
/* how many ticks between timer callbacks */ /* how many ticks between timer callbacks */
#define TIMER_INTERVAL (HZ / 50) #define TIMER_INTERVAL (HZ / 50)
/* no c200v2 */
#if PLUGIN_BUFFER_SIZE > 0x14000
#define DEBUG_MENU #define DEBUG_MENU
#define FONT_CACHING
#endif
#define BG_R .9f /* very light gray */ #define BG_R .9f /* very light gray */
#define BG_G .9f #define BG_G .9f
@ -51,9 +55,6 @@
#ifdef COMBINED #ifdef COMBINED
#define SAVE_FILE PLUGIN_GAMES_DATA_DIR "/puzzles.sav" #define SAVE_FILE PLUGIN_GAMES_DATA_DIR "/puzzles.sav"
#else
static char save_file_path[MAX_PATH];
#define SAVE_FILE ((const char*)save_file_path)
#endif #endif
#define FONT_TABLE PLUGIN_GAMES_DATA_DIR "/.sgt-puzzles.fnttab" #define FONT_TABLE PLUGIN_GAMES_DATA_DIR "/.sgt-puzzles.fnttab"
@ -146,7 +147,7 @@ static void rb_color(int n)
static struct bundled_font { static struct bundled_font {
int status; /* -3 = never tried loading, or unloaded, -2 = failed to load, >= -1: loaded successfully */ int status; /* -3 = never tried loading, or unloaded, -2 = failed to load, >= -1: loaded successfully */
int last_use; int last_use;
} loaded_fonts[2*BUNDLE_COUNT]; /* monospace are first, then proportional */ } *loaded_fonts = NULL; /* monospace are first, then proportional */
static int n_fonts, access_counter = -1; static int n_fonts, access_counter = -1;
@ -166,6 +167,7 @@ static void unload_fonts(void)
static void init_fonttab(void) static void init_fonttab(void)
{ {
loaded_fonts = smalloc(2 * BUNDLE_COUNT * sizeof(struct bundled_font));
for(int i = 0; i < 2 * BUNDLE_COUNT; ++i) for(int i = 0; i < 2 * BUNDLE_COUNT; ++i)
loaded_fonts[i].status = -3; loaded_fonts[i].status = -3;
access_counter = 0; access_counter = 0;
@ -1095,7 +1097,7 @@ static bool do_configure_item(config_item *cfgs, int idx)
break; break;
} }
default: default:
fatal("bad type"); fatal("");
break; break;
} }
return false; return false;
@ -1871,7 +1873,7 @@ static void fix_size(void)
midend_size(me, &w, &h, TRUE); midend_size(me, &w, &h, TRUE);
} }
static void reset_tlsf(void) static void init_tlsf(void)
{ {
/* reset tlsf by nuking the signature */ /* reset tlsf by nuking the signature */
/* will make any already-allocated memory point to garbage */ /* will make any already-allocated memory point to garbage */
@ -1912,10 +1914,6 @@ static void init_colors(void)
static char *init_for_game(const game *gm, int load_fd, bool draw) static char *init_for_game(const game *gm, int load_fd, bool draw)
{ {
/* if we are loading a game tlsf has already been initialized */
if(load_fd < 0)
reset_tlsf();
me = midend_new(NULL, gm, &rb_drawing, NULL); me = midend_new(NULL, gm, &rb_drawing, NULL);
if(load_fd < 0) if(load_fd < 0)
@ -1937,12 +1935,20 @@ static char *init_for_game(const game *gm, int load_fd, bool draw)
{ {
clear_and_draw(); clear_and_draw();
} }
return NULL; return NULL;
} }
static void shutdown_tlsf(void)
{
memset(giant_buffer, 0, 4);
}
static void exit_handler(void) static void exit_handler(void)
{ {
unload_fonts(); unload_fonts();
shutdown_tlsf();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ #ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(false); rb->cpu_boost(false);
#endif #endif
@ -1950,6 +1956,7 @@ static void exit_handler(void)
#define MAX_LINE 128 #define MAX_LINE 128
#ifdef FONT_CACHING
/* try loading the fonts indicated in the on-disk font table */ /* try loading the fonts indicated in the on-disk font table */
static void load_fonts(void) static void load_fonts(void)
{ {
@ -2030,7 +2037,7 @@ static void save_fonts(void)
int outfd = rb->open(FONT_TABLE ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666); int outfd = rb->open(FONT_TABLE ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(outfd < 0) if(outfd < 0)
return; return;
uint64_t oldmask = 0; uint64_t oldmask = 0;
if(fd >= 0) if(fd >= 0)
@ -2073,20 +2080,25 @@ static void save_fonts(void)
final |= oldmask; final |= oldmask;
uint32_t left = final >> 31; uint32_t left = final >> 31;
uint32_t right = final & 0x7fffffff; uint32_t right = final & 0x7fffffff;
if(fd < 0)
rb->fdprintf(outfd, "# Please do not edit this file!\n");
rb->fdprintf(outfd, "%s:%u:%u\n", midend_which_game(me)->name, left, right); rb->fdprintf(outfd, "%s:%u:%u\n", midend_which_game(me)->name, left, right);
rb->close(outfd); rb->close(outfd);
rb->rename(FONT_TABLE ".tmp", FONT_TABLE); rb->rename(FONT_TABLE ".tmp", FONT_TABLE);
} }
} }
#endif
static void save_fname(char *buf)
{
rb->snprintf(buf, MAX_PATH, "%s/sgt-%s.sav", PLUGIN_GAMES_DATA_DIR, thegame.htmlhelp_topic);
}
/* expects a totally free me* pointer */ /* expects a totally free me* pointer */
static bool load_game(void) static bool load_game(void)
{ {
reset_tlsf(); char fname[MAX_PATH];
save_fname(fname);
int fd = rb->open(SAVE_FILE, O_RDONLY); int fd = rb->open(fname, O_RDONLY);
if(fd < 0) if(fd < 0)
return false; return false;
@ -2123,7 +2135,7 @@ static bool load_game(void)
return false; return false;
} }
rb->close(fd); rb->close(fd);
rb->remove(SAVE_FILE); rb->remove(fname);
return true; return true;
} }
} }
@ -2140,22 +2152,24 @@ static bool load_game(void)
rb->splash(HZ, ret); rb->splash(HZ, ret);
sfree(ret); sfree(ret);
rb->close(fd); rb->close(fd);
rb->remove(SAVE_FILE); rb->remove(fname);
return false; return false;
} }
rb->close(fd); rb->close(fd);
rb->remove(SAVE_FILE); rb->remove(fname);
#ifdef FONT_CACHING
load_fonts(); load_fonts();
#endif
/* success */ /* success */
return true; return true;
} }
rb->splashf(HZ, "Cannot load save game for %s!", game); rb->splashf(HZ, "Failed loading save for %s!", game);
/* clean up, even on failure */ /* clean up, even on failure */
rb->close(fd); rb->close(fd);
rb->remove(SAVE_FILE); rb->remove(fname);
return false; return false;
#endif #endif
@ -2166,12 +2180,17 @@ static void save_game(void)
{ {
rb->splash(0, "Saving..."); rb->splash(0, "Saving...");
char fname[MAX_PATH];
save_fname(fname);
/* save game */ /* save game */
int fd = rb->open(SAVE_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666); int fd = rb->open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
midend_serialize(me, write_wrapper, (void*) fd); midend_serialize(me, write_wrapper, (void*) fd);
rb->close(fd); rb->close(fd);
#ifdef FONT_CACHING
save_fonts(); save_fonts();
#endif
rb->lcd_update(); rb->lcd_update();
} }
@ -2235,13 +2254,16 @@ enum plugin_status plugin_start(const void *param)
#endif #endif
#ifndef COMBINED #ifndef COMBINED
rb->snprintf(save_file_path, sizeof(save_file_path), "%s/sgt-%s.sav", PLUGIN_GAMES_DATA_DIR, thegame.htmlhelp_topic);
#endif #endif
rb_atexit(exit_handler); rb_atexit(exit_handler);
init_tlsf();
if(fabs(sqrt(3)/2 - sin(PI/3)) > .01) if(fabs(sqrt(3)/2 - sin(PI/3)) > .01)
rb->splash(HZ, "WARNING: floating-point functions are being weird... report me!"); {
return PLUGIN_ERROR;
}
init_default_settings(); init_default_settings();