diff --git a/libee/ee.c b/libee/ee.c index a9185a4..6028c3a 100644 --- a/libee/ee.c +++ b/libee/ee.c @@ -45,27 +45,6 @@ #include "ee.h" #include "ee_internals.h" -/* Global array with color names */ -const char *ee_color_names[16] = -{ - "black", - "blue", - "green", - "cyan", - "red", - "magenta", - "brown", - "lightgray", - "darkgray", - "lightblue", - "lightgreen", - "lightcyan", - "lightred", - "lightmagenta", - "yellow", - "white", -}; - static unsigned int _ee_delay; static unsigned int _ee_rendertime; char *_ee_empty_line; @@ -227,6 +206,34 @@ unsigned int ee_get_rendertime(void) return _ee_rendertime; } +const char *ee_get_color_name(unsigned int color) +{ + static const char *color_names[16] = + { + "black", + "blue", + "green", + "cyan", + "red", + "magenta", + "brown", + "light gray", + "dark gray", + "light blue", + "light green", + "light cyan", + "light red", + "light magenta", + "yellow", + "white", + }; + + if(color < 0 || color > 15) + return "unknown color"; + + return color_names[color]; +} + static unsigned int _ee_getticks(void) { static unsigned int last_sec = 0, last_usec = 0; @@ -249,6 +256,7 @@ static unsigned int _ee_getticks(void) void ee_refresh(void) { +#define IDLE_USEC 10000 static unsigned int lastticks = 0; unsigned int ticks = lastticks + _ee_getticks(); @@ -265,8 +273,9 @@ void ee_refresh(void) #endif /* Wait until _ee_delay + time of last call */ - for(ticks += _ee_getticks(); ticks < _ee_delay; ticks += _ee_getticks()) - usleep(10000); + ticks += _ee_getticks(); + for(; ticks < _ee_delay - IDLE_USEC; ticks += _ee_getticks()) + usleep(IDLE_USEC); /* Update the sliding mean of the render time */ _ee_rendertime = (7 * _ee_rendertime + ticks) / 8; diff --git a/libee/ee.h b/libee/ee.h index 5481568..bc6f462 100644 --- a/libee/ee.h +++ b/libee/ee.h @@ -51,8 +51,6 @@ enum ee_colors EE_WHITE = 15 }; -extern const char *ee_color_names[16]; - /* * Types */ @@ -66,6 +64,7 @@ void ee_set_delay(unsigned int); unsigned int ee_get_rendertime(void); unsigned int ee_get_width(void); unsigned int ee_get_height(void); +const char *ee_get_color_name(unsigned int); void ee_refresh(void); void ee_end(void); @@ -75,6 +74,7 @@ void ee_set_color(int); int ee_get_color(void); void ee_putchar(int, int, char); void ee_putstr(int, int, const char *); +void ee_printf(int, int, const char *, ...); void ee_clear(void); void ee_draw_line(int, int, int, int, char); diff --git a/libee/graphics.c b/libee/graphics.c index ef0399e..ca86644 100644 --- a/libee/graphics.c +++ b/libee/graphics.c @@ -34,18 +34,19 @@ #include #include +#include #include "ee.h" #include "ee_internals.h" -static int ee_color = 0; +static int _ee_color = 0; void ee_set_color(int color) { if(color < 0 || color > 15) return; - ee_color = color; + _ee_color = color; #if defined(USE_SLANG) SLsmg_set_color(color + 1); #elif defined(USE_NCURSES) @@ -57,7 +58,7 @@ void ee_set_color(int color) int ee_get_color(void) { - return ee_color; + return _ee_color; } void ee_putchar(int x, int y, char c) @@ -73,7 +74,7 @@ void ee_putchar(int x, int y, char c) addch(c); #elif defined(USE_CONIO) _ee_screen[2 * (x + y * ee_get_width())] = c; - _ee_screen[2 * (x + y * ee_get_width()) + 1] = ee_color; + _ee_screen[2 * (x + y * ee_get_width()) + 1] = _ee_color; // gotoxy(x + 1, y + 1); // putch(c); #endif @@ -115,13 +116,36 @@ void ee_putstr(int x, int y, const char *s) while(*s) { *buf++ = *s++; - *buf++ = ee_color; + *buf++ = _ee_color; } // gotoxy(x + 1, y + 1); // cputs(s); #endif } +void ee_printf(int x, int y, const char *format, ...) +{ + char tmp[BUFSIZ]; + char *buf = tmp; + va_list args; + + if(y < 0 || y >= ee_get_height() || x >= ee_get_width()) + return; + + if(ee_get_width() - x + 1 > BUFSIZ) + buf = malloc(ee_get_width() - x + 1); + + va_start(args, format); + vsnprintf(buf, ee_get_width() - x + 1, format, args); + buf[ee_get_width() - x] = '\0'; + va_end(args); + + ee_putstr(x, y, buf); + + if(buf != tmp) + free(buf); +} + void ee_clear(void) { /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */ diff --git a/test/demo.c b/test/demo.c index 910474a..7123dcc 100644 --- a/test/demo.c +++ b/test/demo.c @@ -40,9 +40,8 @@ static void demo_ellipses(void); static void demo_triangles(void); static void demo_sprites(void); -int force_clipping = 0; +int bounds = 0; int outline = 0; -int thin = 0; struct ee_sprite *sprite = NULL; int main(int argc, char **argv) @@ -62,79 +61,88 @@ int main(int argc, char **argv) /* Main menu */ display_menu(); + ee_refresh(); /* Go ! */ while(!quit) { char key = ee_get_key(); + if(key && demo) { display_menu(); + ee_refresh(); demo = NULL; } else if(key) { + handle_key: switch(key) { case 'q': + case 'Q': demo = NULL; quit = 1; break; + case 'o': + case 'O': + outline = (outline + 1) % 3; + display_menu(); + break; + case 'b': + case 'B': + bounds = (bounds + 1) % 2; + display_menu(); + break; case 'c': - ee_clear(); demo = demo_color; break; - case '0': - ee_clear(); + case 'f': + case 'F': demo = demo_all; break; case '1': - ee_clear(); demo = demo_dots; break; case '2': - ee_clear(); demo = demo_lines; - thin = 0; break; case '3': - ee_clear(); - demo = demo_lines; - thin = 1; + demo = demo_boxes; break; case '4': - ee_clear(); - demo = demo_boxes; - outline = 0; + demo = demo_triangles; break; case '5': - ee_clear(); - demo = demo_boxes; - outline = 1; - break; - case '6': - ee_clear(); demo = demo_ellipses; break; - case '7': - ee_clear(); - demo = demo_triangles; - outline = 0; - break; - case '8': - ee_clear(); - demo = demo_triangles; - outline = 1; - break; - case '9': - ee_clear(); + case 's': + case 'S': demo = demo_sprites; break; } + + if(demo) + ee_clear(); + + key = ee_get_key(); + if(key) + goto handle_key; + + ee_refresh(); } if(demo) + { demo(); + + ee_set_color(EE_WHITE); + ee_draw_thin_box(1, 1, ee_get_width() - 2, ee_get_height() - 2); + ee_printf(4, 1, "[%i.%i fps]----", + 1000000 / ee_get_rendertime(), + (10000000 / ee_get_rendertime()) % 10); + ee_refresh(); + } } /* Clean up */ @@ -156,20 +164,23 @@ static void display_menu(void) ee_putstr((xo - strlen("libee demo")) / 2, 3, "libee demo"); ee_putstr((xo - strlen("============")) / 2, 4, "============"); - ee_putstr(4, 6, "0: complete demo"); - ee_putstr(4, 7, "1: dots demo"); - ee_putstr(4, 8, "2: lines demo"); - ee_putstr(4, 9, "3: thin lines demo"); - ee_putstr(4, 10, "4: boxes demo"); - ee_putstr(4, 11, "5: outlined boxes demo"); - ee_putstr(4, 12, "6: ellipses demo"); - ee_putstr(4, 13, "7: triangles demo"); - ee_putstr(4, 14, "8: outlined triangles demo"); - ee_putstr(4, 15, "9: sprites demo"); - - ee_putstr(4, yo - 2, "q: quit"); - - ee_refresh(); + ee_putstr(4, 6, "demos:"); + ee_putstr(4, 7, "'f': full"); + ee_putstr(4, 8, "'1': dots"); + ee_putstr(4, 9, "'2': lines"); + ee_putstr(4, 10, "'3': boxes"); + ee_putstr(4, 11, "'4': triangles"); + ee_putstr(4, 12, "'5': ellipses"); + ee_putstr(4, 13, "'s': sprites"); + ee_putstr(4, 14, "'c': color"); + + ee_putstr(4, 16, "settings:"); + ee_printf(4, 17, "'o': outline: %s", + outline == 0 ? "none" : outline == 1 ? "solid" : "thin"); + ee_printf(4, 18, "'b': drawing boundaries: %s", + bounds == 0 ? "screen" : "infinite"); + + ee_putstr(4, yo - 2, "'q': quit"); } static void demo_all(void) @@ -267,8 +278,6 @@ static void demo_all(void) ee_draw_sprite(ee_get_width() / 2 + cos(0.02*i) * ee_get_width() / 4, ee_get_height() / 2 + sin(0.02*i) * ee_get_height() / 3, sprite, 0); - - ee_refresh(); } static void demo_dots(void) @@ -283,7 +292,6 @@ static void demo_dots(void) ee_set_color(ee_rand(0, 15)); ee_putchar(ee_rand(0, xmax), ee_rand(0, ymax), '#'); } - ee_refresh(); } static void demo_color(void) @@ -294,13 +302,12 @@ static void demo_color(void) ee_clear(); for(i = 0; i < 16; i++) { - sprintf(buf, "'%c': %i (%s)", 'a' + i, i, ee_color_names[i]); + sprintf(buf, "'%c': %i (%s)", 'a' + i, i, ee_get_color_name(i)); ee_set_color(EE_WHITE); ee_putstr(4, i + 3, buf); ee_set_color(i); ee_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------"); } - ee_refresh(); } static void demo_lines(void) @@ -309,7 +316,7 @@ static void demo_lines(void) int h = ee_get_height(); int xa, ya, xb, yb; - if(force_clipping) + if(bounds) { xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h); xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h); @@ -321,12 +328,10 @@ static void demo_lines(void) } ee_set_color(ee_rand(0, 15)); - if(thin) + if(outline > 1) ee_draw_thin_line(xa, ya, xb, yb); else ee_draw_line(xa, ya, xb, yb, '#'); - - ee_refresh(); } static void demo_boxes(void) @@ -335,7 +340,7 @@ static void demo_boxes(void) int h = ee_get_height(); int xa, ya, xb, yb; - if(force_clipping) + if(bounds) { xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h); xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h); @@ -349,13 +354,11 @@ static void demo_boxes(void) ee_set_color(ee_rand(0, 15)); ee_fill_box(xa, ya, xb, yb, '#'); - if(outline) - { - ee_set_color(ee_rand(0, 15)); + ee_set_color(ee_rand(0, 15)); + if(outline == 2) ee_draw_thin_box(xa, ya, xb, yb); - } - - ee_refresh(); + else if(outline == 1) + ee_draw_box(xa, ya, xb, yb, '#'); } static void demo_ellipses(void) @@ -364,7 +367,7 @@ static void demo_ellipses(void) int h = ee_get_height(); int x, y, a, b; - if(force_clipping) + if(bounds) { x = ee_rand(- w, 2 * w); y = ee_rand(- h, 2 * h); a = ee_rand(0, w); b = ee_rand(0, h); @@ -382,13 +385,11 @@ static void demo_ellipses(void) ee_set_color(ee_rand(0, 15)); ee_fill_ellipse(x, y, a, b, '#'); - if(outline) - { - ee_set_color(ee_rand(0, 15)); + ee_set_color(ee_rand(0, 15)); + if(outline == 2) ee_draw_thin_ellipse(x, y, a, b); - } - - ee_refresh(); + else if(outline == 1) + ee_draw_ellipse(x, y, a, b, '#'); } static void demo_triangles(void) @@ -397,7 +398,7 @@ static void demo_triangles(void) int h = ee_get_height(); int xa, ya, xb, yb, xc, yc; - if(force_clipping) + if(bounds) { xa = ee_rand(- w, 2 * w); ya = ee_rand(- h, 2 * h); xb = ee_rand(- w, 2 * w); yb = ee_rand(- h, 2 * h); @@ -414,19 +415,16 @@ static void demo_triangles(void) ee_set_color(ee_rand(0, 15)); ee_fill_triangle(xa, ya, xb, yb, xc, yc, '#'); - if(outline) - { - ee_set_color(ee_rand(0, 15)); + ee_set_color(ee_rand(0, 15)); + if(outline == 2) ee_draw_thin_triangle(xa, ya, xb, yb, xc, yc); - } - - ee_refresh(); + else if(outline == 1) + ee_draw_triangle(xa, ya, xb, yb, xc, yc, '#'); } static void demo_sprites(void) { ee_draw_sprite(ee_rand(0, ee_get_width() - 1), ee_rand(0, ee_get_height() - 1), sprite, 0); - ee_refresh(); }