diff options
author | Rafaël Carré <rafael.carre@gmail.com> | 2010-08-28 21:46:32 +0000 |
---|---|---|
committer | Rafaël Carré <rafael.carre@gmail.com> | 2010-08-28 21:46:32 +0000 |
commit | db92d58b020a38db884f785ed17988a93c6fdd9a (patch) | |
tree | 6911d943c5a3c7bdb767bd4e44302738a6f4e412 /apps/plugins | |
parent | bc2b74b0fb1fca37b95a28ad89bae12bd95ed951 (diff) | |
download | rockbox-db92d58b020a38db884f785ed17988a93c6fdd9a.tar.gz rockbox-db92d58b020a38db884f785ed17988a93c6fdd9a.zip |
wormlet: remove some text buffers
use lcd_putsxyf()
rework a bit DEBUG_WORMLET to remove a buffer (doesn't compile anwyay)
also use a table to get worm state, instead of switch/case
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27924 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins')
-rw-r--r-- | apps/plugins/wormlet.c | 111 |
1 files changed, 36 insertions, 75 deletions
diff --git a/apps/plugins/wormlet.c b/apps/plugins/wormlet.c index 191c995ad6..443ef4367e 100644 --- a/apps/plugins/wormlet.c +++ b/apps/plugins/wormlet.c | |||
@@ -23,7 +23,9 @@ | |||
23 | #include "lib/helper.h" | 23 | #include "lib/helper.h" |
24 | #include "lib/playback_control.h" | 24 | #include "lib/playback_control.h" |
25 | 25 | ||
26 | 26 | #ifdef DEBUG_WORMLET | |
27 | static long max_cycle; | ||
28 | #endif | ||
27 | 29 | ||
28 | /* size of the field the worm lives in */ | 30 | /* size of the field the worm lives in */ |
29 | #define FIELD_RECT_X 1 | 31 | #define FIELD_RECT_X 1 |
@@ -437,11 +439,6 @@ static int worm_food = WORM_PER_FOOD; | |||
437 | 439 | ||
438 | /* End additional variables */ | 440 | /* End additional variables */ |
439 | 441 | ||
440 | #ifdef DEBUG_WORMLET | ||
441 | /* just a buffer used for debug output */ | ||
442 | static char debugout[15]; | ||
443 | #endif | ||
444 | |||
445 | /* the number of active worms (dead or alive) */ | 442 | /* the number of active worms (dead or alive) */ |
446 | static int worm_count = MAX_WORMS; | 443 | static int worm_count = MAX_WORMS; |
447 | 444 | ||
@@ -456,6 +453,14 @@ static bool use_remote = false; | |||
456 | #define COLLISION_ARGH 3 | 453 | #define COLLISION_ARGH 3 |
457 | #define COLLISION_FIELD 4 | 454 | #define COLLISION_FIELD 4 |
458 | 455 | ||
456 | static const char *const state_desc[] = { | ||
457 | [COLLISION_NONE] = NULL, | ||
458 | [COLLISION_WORM] = "Wormed", | ||
459 | [COLLISION_FOOD] = "Growing", | ||
460 | [COLLISION_ARGH] = "Argh", | ||
461 | [COLLISION_FIELD] = "Crashed", | ||
462 | }; | ||
463 | |||
459 | /* constants for use as directions. | 464 | /* constants for use as directions. |
460 | Note that the values are ordered clockwise. | 465 | Note that the values are ordered clockwise. |
461 | Thus increasing / decreasing the values | 466 | Thus increasing / decreasing the values |
@@ -492,12 +497,6 @@ static struct configdata config[] = | |||
492 | {TYPE_INT, 0, 15, { .int_p = &worm_food }, "Worm Growth Per Food", NULL} | 497 | {TYPE_INT, 0, 15, { .int_p = &worm_food }, "Worm Growth Per Food", NULL} |
493 | }; | 498 | }; |
494 | 499 | ||
495 | #ifdef DEBUG_WORMLET | ||
496 | static void set_debug_out(char *str){ | ||
497 | strcpy(debugout, str); | ||
498 | } | ||
499 | #endif | ||
500 | |||
501 | /** | 500 | /** |
502 | * Returns the direction id in which the worm | 501 | * Returns the direction id in which the worm |
503 | * currently is creeping. | 502 | * currently is creeping. |
@@ -1547,8 +1546,6 @@ static void virtual_player(struct worm *w) | |||
1547 | */ | 1546 | */ |
1548 | static void score_board(void) | 1547 | static void score_board(void) |
1549 | { | 1548 | { |
1550 | char buf[15]; | ||
1551 | char* buf2 = NULL; | ||
1552 | int i; | 1549 | int i; |
1553 | int y = 0; | 1550 | int y = 0; |
1554 | rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); | 1551 | rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); |
@@ -1557,6 +1554,8 @@ static void score_board(void) | |||
1557 | rb->lcd_set_drawmode(DRMODE_SOLID); | 1554 | rb->lcd_set_drawmode(DRMODE_SOLID); |
1558 | for (i = 0; i < worm_count; i++) { | 1555 | for (i = 0; i < worm_count; i++) { |
1559 | int score = get_score(&worms[i]); | 1556 | int score = get_score(&worms[i]); |
1557 | int collision = check_collision(&worms[i]); | ||
1558 | const char *state_str; | ||
1560 | 1559 | ||
1561 | /* high score */ | 1560 | /* high score */ |
1562 | if (worms[i].fetch_worm_direction != virtual_player){ | 1561 | if (worms[i].fetch_worm_direction != virtual_player){ |
@@ -1565,40 +1564,20 @@ static void score_board(void) | |||
1565 | } | 1564 | } |
1566 | } | 1565 | } |
1567 | 1566 | ||
1568 | /* length */ | ||
1569 | rb->snprintf(buf, sizeof (buf),"Len:%d", score); | ||
1570 | |||
1571 | /* worm state */ | 1567 | /* worm state */ |
1572 | switch (check_collision(&worms[i])) { | 1568 | if (collision == COLLISION_NONE) { |
1573 | case COLLISION_NONE: | 1569 | if (worms[i].growing > 0) |
1574 | if (worms[i].growing > 0) | 1570 | state_str = "Growing"; |
1575 | buf2 = "Growing"; | 1571 | else { |
1576 | else { | 1572 | state_str = worms[i].alive ? "Hungry" : "Wormed"; |
1577 | if (worms[i].alive) | 1573 | } |
1578 | buf2 = "Hungry"; | 1574 | } else { |
1579 | else | 1575 | state_str = state_desc[collision]; |
1580 | buf2 = "Wormed"; | ||
1581 | } | ||
1582 | break; | ||
1583 | |||
1584 | case COLLISION_WORM: | ||
1585 | buf2 = "Wormed"; | ||
1586 | break; | ||
1587 | |||
1588 | case COLLISION_FOOD: | ||
1589 | buf2 = "Growing"; | ||
1590 | break; | ||
1591 | |||
1592 | case COLLISION_ARGH: | ||
1593 | buf2 = "Argh"; | ||
1594 | break; | ||
1595 | |||
1596 | case COLLISION_FIELD: | ||
1597 | buf2 = "Crashed"; | ||
1598 | break; | ||
1599 | } | 1576 | } |
1600 | rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y , buf); | 1577 | |
1601 | rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, y+8, buf2); | 1578 | /* length */ |
1579 | rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, y , "Len:%d", score); | ||
1580 | rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, y+8, state_str); | ||
1602 | 1581 | ||
1603 | if (!worms[i].alive){ | 1582 | if (!worms[i].alive){ |
1604 | rb->lcd_set_drawmode(DRMODE_COMPLEMENT); | 1583 | rb->lcd_set_drawmode(DRMODE_COMPLEMENT); |
@@ -1608,11 +1587,10 @@ static void score_board(void) | |||
1608 | } | 1587 | } |
1609 | y += 19; | 1588 | y += 19; |
1610 | } | 1589 | } |
1611 | rb->snprintf(buf , sizeof(buf), "Hs: %d", highscore); | 1590 | #ifdef DEBUG_WORMLET |
1612 | #ifndef DEBUG_WORMLET | 1591 | rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, "ticks %d", max_cycle); |
1613 | rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, buf); | ||
1614 | #else | 1592 | #else |
1615 | rb->lcd_putsxy(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, debugout); | 1593 | rb->lcd_putsxyf(FIELD_RECT_WIDTH + 3, LCD_HEIGHT - 8, "Hs: %d", highscore); |
1616 | #endif | 1594 | #endif |
1617 | } | 1595 | } |
1618 | 1596 | ||
@@ -1684,8 +1662,7 @@ static int run(void) | |||
1684 | long cycle_start = 0, cycle_end = 0; | 1662 | long cycle_start = 0, cycle_end = 0; |
1685 | #ifdef DEBUG_WORMLET | 1663 | #ifdef DEBUG_WORMLET |
1686 | int ticks_to_max_cycle_reset = 20; | 1664 | int ticks_to_max_cycle_reset = 20; |
1687 | long max_cycle = 0; | 1665 | max_cycle = 0; |
1688 | char buf[20]; | ||
1689 | #endif | 1666 | #endif |
1690 | 1667 | ||
1691 | /* initialize the board and so on */ | 1668 | /* initialize the board and so on */ |
@@ -1811,8 +1788,6 @@ static int run(void) | |||
1811 | max_cycle = cycle_duration; | 1788 | max_cycle = cycle_duration; |
1812 | ticks_to_max_cycle_reset = 20; | 1789 | ticks_to_max_cycle_reset = 20; |
1813 | } | 1790 | } |
1814 | rb->snprintf(buf, sizeof buf, "ticks %d", max_cycle); | ||
1815 | set_debug_out(buf); | ||
1816 | #endif | 1791 | #endif |
1817 | } | 1792 | } |
1818 | /* adjust the number of ticks to wait for a button. | 1793 | /* adjust the number of ticks to wait for a button. |
@@ -1855,7 +1830,6 @@ static void test_worm_food_collision(void) | |||
1855 | foodx[0] = 15; | 1830 | foodx[0] = 15; |
1856 | foody[0] = 12; | 1831 | foody[0] = 12; |
1857 | for (foody[0] = 20; foody[0] > 0; foody[0] --) { | 1832 | for (foody[0] = 20; foody[0] > 0; foody[0] --) { |
1858 | char buf[20]; | ||
1859 | bool collision; | 1833 | bool collision; |
1860 | draw_worm(&worms[0]); | 1834 | draw_worm(&worms[0]); |
1861 | draw_food(0); | 1835 | draw_food(0); |
@@ -1863,8 +1837,7 @@ static void test_worm_food_collision(void) | |||
1863 | if (collision) { | 1837 | if (collision) { |
1864 | collision_count++; | 1838 | collision_count++; |
1865 | } | 1839 | } |
1866 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); | 1840 | rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count); |
1867 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); | ||
1868 | rb->lcd_update(); | 1841 | rb->lcd_update(); |
1869 | } | 1842 | } |
1870 | if (collision_count != food_size) { | 1843 | if (collision_count != food_size) { |
@@ -1874,7 +1847,6 @@ static void test_worm_food_collision(void) | |||
1874 | 1847 | ||
1875 | foody[0] = 15; | 1848 | foody[0] = 15; |
1876 | for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) { | 1849 | for (foodx[0] = 30; foodx[0] > 0; foodx[0] --) { |
1877 | char buf[20]; | ||
1878 | bool collision; | 1850 | bool collision; |
1879 | draw_worm(&worms[0]); | 1851 | draw_worm(&worms[0]); |
1880 | draw_food(0); | 1852 | draw_food(0); |
@@ -1882,8 +1854,7 @@ static void test_worm_food_collision(void) | |||
1882 | if (collision) { | 1854 | if (collision) { |
1883 | collision_count ++; | 1855 | collision_count ++; |
1884 | } | 1856 | } |
1885 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); | 1857 | rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count); |
1886 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); | ||
1887 | rb->lcd_update(); | 1858 | rb->lcd_update(); |
1888 | } | 1859 | } |
1889 | if (collision_count != food_size * 2) { | 1860 | if (collision_count != food_size * 2) { |
@@ -1924,15 +1895,13 @@ static void test_worm_argh_collision(void) | |||
1924 | 1895 | ||
1925 | arghx[0] = 12; | 1896 | arghx[0] = 12; |
1926 | for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){ | 1897 | for (arghy[0] = 0; arghy[0] < FIELD_RECT_HEIGHT - argh_size; arghy[0]++){ |
1927 | char buf[20]; | ||
1928 | bool collision; | 1898 | bool collision; |
1929 | draw_argh(0); | 1899 | draw_argh(0); |
1930 | collision = worm_argh_collision(&worms[0], 0); | 1900 | collision = worm_argh_collision(&worms[0], 0); |
1931 | if (collision) { | 1901 | if (collision) { |
1932 | collision_count ++; | 1902 | collision_count ++; |
1933 | } | 1903 | } |
1934 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); | 1904 | rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count); |
1935 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); | ||
1936 | rb->lcd_update(); | 1905 | rb->lcd_update(); |
1937 | } | 1906 | } |
1938 | if (collision_count != argh_size * 2) { | 1907 | if (collision_count != argh_size * 2) { |
@@ -1941,15 +1910,13 @@ static void test_worm_argh_collision(void) | |||
1941 | 1910 | ||
1942 | arghy[0] = 12; | 1911 | arghy[0] = 12; |
1943 | for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){ | 1912 | for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - argh_size; arghx[0]++){ |
1944 | char buf[20]; | ||
1945 | bool collision; | 1913 | bool collision; |
1946 | draw_argh(0); | 1914 | draw_argh(0); |
1947 | collision = worm_argh_collision(&worms[0], 0); | 1915 | collision = worm_argh_collision(&worms[0], 0); |
1948 | if (collision) { | 1916 | if (collision) { |
1949 | collision_count ++; | 1917 | collision_count ++; |
1950 | } | 1918 | } |
1951 | rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); | 1919 | rb->lcd_putsxyf(0, LCD_HEIGHT -8, "collisions: %d", collision_count); |
1952 | rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); | ||
1953 | rb->lcd_update(); | 1920 | rb->lcd_update(); |
1954 | } | 1921 | } |
1955 | if (collision_count != argh_size * 4) { | 1922 | if (collision_count != argh_size * 4) { |
@@ -2173,7 +2140,6 @@ static int test_specific_worm_collision(void) | |||
2173 | int dir; | 2140 | int dir; |
2174 | int x = 0; | 2141 | int x = 0; |
2175 | int y = 0; | 2142 | int y = 0; |
2176 | char buf[20]; | ||
2177 | rb->lcd_clear_display(); | 2143 | rb->lcd_clear_display(); |
2178 | init_worm(&worms[0], 10, 20); | 2144 | init_worm(&worms[0], 10, 20); |
2179 | add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH); | 2145 | add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH); |
@@ -2195,8 +2161,7 @@ static int test_specific_worm_collision(void) | |||
2195 | collisions ++; | 2161 | collisions ++; |
2196 | } | 2162 | } |
2197 | rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y); | 2163 | rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y); |
2198 | rb->snprintf(buf, sizeof buf, "collisions %d", collisions); | 2164 | rb->lcd_putsxyf(0, LCD_HEIGHT - 8, "collisions %d", collisions); |
2199 | rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf); | ||
2200 | rb->lcd_update(); | 2165 | rb->lcd_update(); |
2201 | } | 2166 | } |
2202 | } | 2167 | } |
@@ -2237,7 +2202,6 @@ static void test_make_argh(void) | |||
2237 | rb->lcd_update(); | 2202 | rb->lcd_update(); |
2238 | 2203 | ||
2239 | for (seed = 0; hit < 20; seed += 2) { | 2204 | for (seed = 0; hit < 20; seed += 2) { |
2240 | char buf[20]; | ||
2241 | int x, y; | 2205 | int x, y; |
2242 | rb->srand(seed); | 2206 | rb->srand(seed); |
2243 | x = rb->rand() % (FIELD_RECT_WIDTH - argh_size); | 2207 | x = rb->rand() % (FIELD_RECT_WIDTH - argh_size); |
@@ -2253,9 +2217,8 @@ static void test_make_argh(void) | |||
2253 | failures ++; | 2217 | failures ++; |
2254 | } | 2218 | } |
2255 | 2219 | ||
2256 | rb->snprintf(buf, sizeof buf, "(%d;%d) fail%d try%d", | 2220 | rb->lcd_putsxyf(0, LCD_HEIGHT - 8, "(%d;%d) fail%d try%d", |
2257 | x, y, failures, tries); | 2221 | x, y, failures, tries); |
2258 | rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf); | ||
2259 | rb->lcd_update(); | 2222 | rb->lcd_update(); |
2260 | rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y, | 2223 | rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y, |
2261 | argh_size, argh_size); | 2224 | argh_size, argh_size); |
@@ -2289,14 +2252,12 @@ static void test_worm_argh_collision_in_moves(void) { | |||
2289 | 2252 | ||
2290 | set_worm_dir(&worms[0], EAST); | 2253 | set_worm_dir(&worms[0], EAST); |
2291 | for (i = 0; i < 20; i++) { | 2254 | for (i = 0; i < 20; i++) { |
2292 | char buf[20]; | ||
2293 | move_worm(&worms[0]); | 2255 | move_worm(&worms[0]); |
2294 | draw_worm(&worms[0]); | 2256 | draw_worm(&worms[0]); |
2295 | if (worm_argh_collision_in_moves(&worms[0], 0, 5)){ | 2257 | if (worm_argh_collision_in_moves(&worms[0], 0, 5)){ |
2296 | hit_count ++; | 2258 | hit_count ++; |
2297 | } | 2259 | } |
2298 | rb->snprintf(buf, sizeof buf, "in 5 moves hits: %d", hit_count); | 2260 | rb->lcd_putsxyf(0, LCD_HEIGHT - 8, "in 5 moves hits: %d", hit_count); |
2299 | rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf); | ||
2300 | rb->lcd_update(); | 2261 | rb->lcd_update(); |
2301 | } | 2262 | } |
2302 | if (hit_count != argh_size + 5) { | 2263 | if (hit_count != argh_size + 5) { |