summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2017-08-09 21:51:23 -0400
committerFranklin Wei <git@fwei.tk>2017-08-09 21:51:23 -0400
commitca228d3d8781876e58aecf9434b7760fd2bb4b93 (patch)
tree1c8921fa5aae9e39e00bfcb29279eeb8afbb4b0a
parent3e1c8cca9265c80c12653fd9573871298ea162ca (diff)
downloadrockbox-ca228d3d8781876e58aecf9434b7760fd2bb4b93.tar.gz
rockbox-ca228d3d8781876e58aecf9434b7760fd2bb4b93.zip
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
-rw-r--r--apps/plugins/puzzles/rockbox.c68
1 files changed, 45 insertions, 23 deletions
diff --git a/apps/plugins/puzzles/rockbox.c b/apps/plugins/puzzles/rockbox.c
index 511c1eef86..5a00c47a61 100644
--- a/apps/plugins/puzzles/rockbox.c
+++ b/apps/plugins/puzzles/rockbox.c
@@ -38,7 +38,11 @@
38/* how many ticks between timer callbacks */ 38/* how many ticks between timer callbacks */
39#define TIMER_INTERVAL (HZ / 50) 39#define TIMER_INTERVAL (HZ / 50)
40 40
41/* no c200v2 */
42#if PLUGIN_BUFFER_SIZE > 0x14000
41#define DEBUG_MENU 43#define DEBUG_MENU
44#define FONT_CACHING
45#endif
42 46
43#define BG_R .9f /* very light gray */ 47#define BG_R .9f /* very light gray */
44#define BG_G .9f 48#define BG_G .9f
@@ -51,9 +55,6 @@
51 55
52#ifdef COMBINED 56#ifdef COMBINED
53#define SAVE_FILE PLUGIN_GAMES_DATA_DIR "/puzzles.sav" 57#define SAVE_FILE PLUGIN_GAMES_DATA_DIR "/puzzles.sav"
54#else
55static char save_file_path[MAX_PATH];
56#define SAVE_FILE ((const char*)save_file_path)
57#endif 58#endif
58 59
59#define FONT_TABLE PLUGIN_GAMES_DATA_DIR "/.sgt-puzzles.fnttab" 60#define FONT_TABLE PLUGIN_GAMES_DATA_DIR "/.sgt-puzzles.fnttab"
@@ -146,7 +147,7 @@ static void rb_color(int n)
146static struct bundled_font { 147static struct bundled_font {
147 int status; /* -3 = never tried loading, or unloaded, -2 = failed to load, >= -1: loaded successfully */ 148 int status; /* -3 = never tried loading, or unloaded, -2 = failed to load, >= -1: loaded successfully */
148 int last_use; 149 int last_use;
149} loaded_fonts[2*BUNDLE_COUNT]; /* monospace are first, then proportional */ 150} *loaded_fonts = NULL; /* monospace are first, then proportional */
150 151
151static int n_fonts, access_counter = -1; 152static int n_fonts, access_counter = -1;
152 153
@@ -166,6 +167,7 @@ static void unload_fonts(void)
166 167
167static void init_fonttab(void) 168static void init_fonttab(void)
168{ 169{
170 loaded_fonts = smalloc(2 * BUNDLE_COUNT * sizeof(struct bundled_font));
169 for(int i = 0; i < 2 * BUNDLE_COUNT; ++i) 171 for(int i = 0; i < 2 * BUNDLE_COUNT; ++i)
170 loaded_fonts[i].status = -3; 172 loaded_fonts[i].status = -3;
171 access_counter = 0; 173 access_counter = 0;
@@ -1095,7 +1097,7 @@ static bool do_configure_item(config_item *cfgs, int idx)
1095 break; 1097 break;
1096 } 1098 }
1097 default: 1099 default:
1098 fatal("bad type"); 1100 fatal("");
1099 break; 1101 break;
1100 } 1102 }
1101 return false; 1103 return false;
@@ -1871,7 +1873,7 @@ static void fix_size(void)
1871 midend_size(me, &w, &h, TRUE); 1873 midend_size(me, &w, &h, TRUE);
1872} 1874}
1873 1875
1874static void reset_tlsf(void) 1876static void init_tlsf(void)
1875{ 1877{
1876 /* reset tlsf by nuking the signature */ 1878 /* reset tlsf by nuking the signature */
1877 /* will make any already-allocated memory point to garbage */ 1879 /* will make any already-allocated memory point to garbage */
@@ -1912,10 +1914,6 @@ static void init_colors(void)
1912 1914
1913static char *init_for_game(const game *gm, int load_fd, bool draw) 1915static char *init_for_game(const game *gm, int load_fd, bool draw)
1914{ 1916{
1915 /* if we are loading a game tlsf has already been initialized */
1916 if(load_fd < 0)
1917 reset_tlsf();
1918
1919 me = midend_new(NULL, gm, &rb_drawing, NULL); 1917 me = midend_new(NULL, gm, &rb_drawing, NULL);
1920 1918
1921 if(load_fd < 0) 1919 if(load_fd < 0)
@@ -1937,12 +1935,20 @@ static char *init_for_game(const game *gm, int load_fd, bool draw)
1937 { 1935 {
1938 clear_and_draw(); 1936 clear_and_draw();
1939 } 1937 }
1938
1940 return NULL; 1939 return NULL;
1941} 1940}
1942 1941
1942static void shutdown_tlsf(void)
1943{
1944 memset(giant_buffer, 0, 4);
1945}
1946
1943static void exit_handler(void) 1947static void exit_handler(void)
1944{ 1948{
1945 unload_fonts(); 1949 unload_fonts();
1950 shutdown_tlsf();
1951
1946#ifdef HAVE_ADJUSTABLE_CPU_FREQ 1952#ifdef HAVE_ADJUSTABLE_CPU_FREQ
1947 rb->cpu_boost(false); 1953 rb->cpu_boost(false);
1948#endif 1954#endif
@@ -1950,6 +1956,7 @@ static void exit_handler(void)
1950 1956
1951#define MAX_LINE 128 1957#define MAX_LINE 128
1952 1958
1959#ifdef FONT_CACHING
1953/* try loading the fonts indicated in the on-disk font table */ 1960/* try loading the fonts indicated in the on-disk font table */
1954static void load_fonts(void) 1961static void load_fonts(void)
1955{ 1962{
@@ -2030,7 +2037,7 @@ static void save_fonts(void)
2030 int outfd = rb->open(FONT_TABLE ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666); 2037 int outfd = rb->open(FONT_TABLE ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666);
2031 if(outfd < 0) 2038 if(outfd < 0)
2032 return; 2039 return;
2033 2040
2034 uint64_t oldmask = 0; 2041 uint64_t oldmask = 0;
2035 2042
2036 if(fd >= 0) 2043 if(fd >= 0)
@@ -2073,20 +2080,25 @@ static void save_fonts(void)
2073 final |= oldmask; 2080 final |= oldmask;
2074 uint32_t left = final >> 31; 2081 uint32_t left = final >> 31;
2075 uint32_t right = final & 0x7fffffff; 2082 uint32_t right = final & 0x7fffffff;
2076 if(fd < 0)
2077 rb->fdprintf(outfd, "# Please do not edit this file!\n");
2078 rb->fdprintf(outfd, "%s:%u:%u\n", midend_which_game(me)->name, left, right); 2083 rb->fdprintf(outfd, "%s:%u:%u\n", midend_which_game(me)->name, left, right);
2079 rb->close(outfd); 2084 rb->close(outfd);
2080 rb->rename(FONT_TABLE ".tmp", FONT_TABLE); 2085 rb->rename(FONT_TABLE ".tmp", FONT_TABLE);
2081 } 2086 }
2082} 2087}
2088#endif
2089
2090static void save_fname(char *buf)
2091{
2092 rb->snprintf(buf, MAX_PATH, "%s/sgt-%s.sav", PLUGIN_GAMES_DATA_DIR, thegame.htmlhelp_topic);
2093}
2083 2094
2084/* expects a totally free me* pointer */ 2095/* expects a totally free me* pointer */
2085static bool load_game(void) 2096static bool load_game(void)
2086{ 2097{
2087 reset_tlsf(); 2098 char fname[MAX_PATH];
2099 save_fname(fname);
2088 2100
2089 int fd = rb->open(SAVE_FILE, O_RDONLY); 2101 int fd = rb->open(fname, O_RDONLY);
2090 if(fd < 0) 2102 if(fd < 0)
2091 return false; 2103 return false;
2092 2104
@@ -2123,7 +2135,7 @@ static bool load_game(void)
2123 return false; 2135 return false;
2124 } 2136 }
2125 rb->close(fd); 2137 rb->close(fd);
2126 rb->remove(SAVE_FILE); 2138 rb->remove(fname);
2127 return true; 2139 return true;
2128 } 2140 }
2129 } 2141 }
@@ -2140,22 +2152,24 @@ static bool load_game(void)
2140 rb->splash(HZ, ret); 2152 rb->splash(HZ, ret);
2141 sfree(ret); 2153 sfree(ret);
2142 rb->close(fd); 2154 rb->close(fd);
2143 rb->remove(SAVE_FILE); 2155 rb->remove(fname);
2144 return false; 2156 return false;
2145 } 2157 }
2146 rb->close(fd); 2158 rb->close(fd);
2147 rb->remove(SAVE_FILE); 2159 rb->remove(fname);
2148 2160
2161#ifdef FONT_CACHING
2149 load_fonts(); 2162 load_fonts();
2163#endif
2150 2164
2151 /* success */ 2165 /* success */
2152 return true; 2166 return true;
2153 } 2167 }
2154 rb->splashf(HZ, "Cannot load save game for %s!", game); 2168 rb->splashf(HZ, "Failed loading save for %s!", game);
2155 2169
2156 /* clean up, even on failure */ 2170 /* clean up, even on failure */
2157 rb->close(fd); 2171 rb->close(fd);
2158 rb->remove(SAVE_FILE); 2172 rb->remove(fname);
2159 2173
2160 return false; 2174 return false;
2161#endif 2175#endif
@@ -2166,12 +2180,17 @@ static void save_game(void)
2166{ 2180{
2167 rb->splash(0, "Saving..."); 2181 rb->splash(0, "Saving...");
2168 2182
2183 char fname[MAX_PATH];
2184 save_fname(fname);
2185
2169 /* save game */ 2186 /* save game */
2170 int fd = rb->open(SAVE_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666); 2187 int fd = rb->open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2171 midend_serialize(me, write_wrapper, (void*) fd); 2188 midend_serialize(me, write_wrapper, (void*) fd);
2172 rb->close(fd); 2189 rb->close(fd);
2173 2190
2191#ifdef FONT_CACHING
2174 save_fonts(); 2192 save_fonts();
2193#endif
2175 2194
2176 rb->lcd_update(); 2195 rb->lcd_update();
2177} 2196}
@@ -2235,13 +2254,16 @@ enum plugin_status plugin_start(const void *param)
2235#endif 2254#endif
2236 2255
2237#ifndef COMBINED 2256#ifndef COMBINED
2238 rb->snprintf(save_file_path, sizeof(save_file_path), "%s/sgt-%s.sav", PLUGIN_GAMES_DATA_DIR, thegame.htmlhelp_topic);
2239#endif 2257#endif
2240 2258
2241 rb_atexit(exit_handler); 2259 rb_atexit(exit_handler);
2242 2260
2261 init_tlsf();
2262
2243 if(fabs(sqrt(3)/2 - sin(PI/3)) > .01) 2263 if(fabs(sqrt(3)/2 - sin(PI/3)) > .01)
2244 rb->splash(HZ, "WARNING: floating-point functions are being weird... report me!"); 2264 {
2265 return PLUGIN_ERROR;
2266 }
2245 2267
2246 init_default_settings(); 2268 init_default_settings();
2247 2269