commit a702c7cf3a161258c0ace235f4f34fa3513dac03 Author: Peter Wang Date: Mon Nov 15 20:13:50 2010 +1100 WIP: UTF-8 string literals with MSVC diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7f6ca74..a2be9d2 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -153,7 +153,7 @@ endif(SUPPORT_OPENGL AND NOT IPHONE) example(ex_font ${FONT} ${IMAGE}) example(ex_font_justify ex_font_justify.cpp ${NIHGUI} ${IMAGE} ${TTF}) example(ex_logo ${FONT} ${TTF} ${IMAGE} ${PRIM}) -example(ex_ttf ${TTF} ${PRIM}) +example(ex_ttf ex_ttf.cpp ${TTF} ${PRIM}) # Currently both FLAC and Vorbis are required because the examples # unconditionally initialise the addons. diff --git a/examples/ex_config.c b/examples/ex_config.c index 39991ca..040dd6c 100644 --- a/examples/ex_config.c +++ b/examples/ex_config.c @@ -22,6 +22,10 @@ do { \ } \ } while (0) +#define ADICAO "adi\xc3\xa7\xc3\xa3o" +#define EURO "\xe2\x82\xac" +#define HUH "\xc2\xbf" + int main(void) { ALLEGRO_CONFIG *cfg; diff --git a/examples/ex_font.c b/examples/ex_font.c index 2773699..a5e7ac3 100644 --- a/examples/ex_font.c +++ b/examples/ex_font.c @@ -4,6 +4,8 @@ #include "common.c" +#define EURO "\xe2\x82\xac" + static void wait_for_esc(ALLEGRO_DISPLAY *display) { ALLEGRO_EVENT_QUEUE *queue; @@ -92,7 +94,7 @@ int main(void) al_draw_textf(f, al_map_rgb(0, 255, 0), 10, 50, 0, "green"); /* Draw a unicode symbol */ - al_draw_textf(a4f, al_map_rgb(0, 0, 255), 10, 90, 0, "Mysha's 0.02€"); + al_draw_textf(a4f, al_map_rgb(0, 0, 255), 10, 90, 0, "Mysha's 0.02" EURO); al_flip_display(); diff --git a/examples/ex_ttf.c b/examples/ex_ttf.c deleted file mode 100644 index 972d6dc..0000000 --- a/examples/ex_ttf.c +++ /dev/null @@ -1,176 +0,0 @@ -#include -#include -#include -#include - -#include "common.c" - -struct Example -{ - double fps; - ALLEGRO_FONT *f1, *f2, *f3, *f4; -} ex; - -static void render(void) -{ - ALLEGRO_COLOR white = al_map_rgba_f(1, 1, 1, 1); - ALLEGRO_COLOR black = al_map_rgba_f(0, 0, 0, 1); - ALLEGRO_COLOR red = al_map_rgba_f(1, 0, 0, 1); - ALLEGRO_COLOR green = al_map_rgba_f(0, 0.5, 0, 1); - ALLEGRO_COLOR blue = al_map_rgba_f(0.1, 0.2, 1, 1); - int x, y, w, h, as, de, xpos, ypos; - int target_w, target_h; - ALLEGRO_USTR_INFO info, sub_info; - ALLEGRO_USTR *u; - - al_clear_to_color(white); - - al_hold_bitmap_drawing(true); - - al_draw_textf(ex.f1, black, 50, 50, 0, "Tulip (kerning)"); - al_draw_textf(ex.f2, black, 50, 100, 0, "Tulip (no kerning)"); - al_draw_textf(ex.f3, black, 50, 200, 0, "This font has a size of 12 pixels, " - "the one above has 48 pixels."); - - al_hold_bitmap_drawing(false); - al_hold_bitmap_drawing(true); - - al_draw_textf(ex.f3, red, 50, 220, 0, "The color can simply be changed."); - - al_hold_bitmap_drawing(false); - al_hold_bitmap_drawing(true); - - al_draw_textf(ex.f3, green, 50, 240, 0, "Some unicode symbols:"); - al_draw_textf(ex.f3, green, 50, 260, 0, "■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱"); - al_draw_textf(ex.f3, green, 50, 280, 0, "▲△▴▵▶▷▸▹►▻▼▽▾▿◀◁◂◃◄◅◆◇◈◉◊"); - al_draw_textf(ex.f3, green, 50, 300, 0, "○◌◍◎●◐◑◒◓◔◕◖◗◘◙"); - - #define OFF(x) al_ustr_offset(u, x) - #define SUB(x, y) al_ref_ustr(&sub_info, u, OFF(x), OFF(y)) - u = al_ref_cstr(&info, "«Thís»|you"); - al_draw_ustr(ex.f3, green, 50, 320, 0, SUB(0, 6)); - u = al_ref_cstr(&info, "should|‘ìş’"); - al_draw_ustr(ex.f3, green, 50, 340, 0, SUB(7, 11)); - u = al_ref_cstr(&info, "not|“cøünt”|see"); - al_draw_ustr(ex.f3, green, 50, 360, 0, SUB(4, 11)); - u = al_ref_cstr(&info, "réstrïçteđ…|this."); - al_draw_ustr(ex.f3, green, 50, 380, 0, SUB(0, 11)); - - al_hold_bitmap_drawing(false); - - target_w = al_get_bitmap_width(al_get_target_bitmap()); - target_h = al_get_bitmap_height(al_get_target_bitmap()); - - xpos = target_w - 10; - ypos = target_h - 10; - al_get_text_dimensions(ex.f4, "Allegro", &x, &y, &w, &h); - as = al_get_font_ascent(ex.f4); - de = al_get_font_descent(ex.f4); - xpos -= w; - ypos -= h; - x += xpos; - y += ypos; - - al_draw_rectangle(x, y, x + w, y + h, black, 0); - al_draw_line(x, y + as, x + w, y + as, black, 0); - al_draw_line(x, y + as + de, x + w, y + as + de, black, 0); - - al_hold_bitmap_drawing(true); - al_draw_textf(ex.f4, blue, xpos, ypos, 0, "Allegro"); - al_hold_bitmap_drawing(false); - - al_hold_bitmap_drawing(true); - - al_draw_textf(ex.f3, black, target_w, 0, ALLEGRO_ALIGN_RIGHT, - "%.1f FPS", ex.fps); - - al_hold_bitmap_drawing(false); -} - -int main(int argc, const char *argv[]) -{ - const char *font_file = "data/DejaVuSans.ttf"; - ALLEGRO_DISPLAY *display; - ALLEGRO_TIMER *timer; - ALLEGRO_EVENT_QUEUE *queue; - int redraw = 0; - double t = 0; - - if (!al_init()) { - abort_example("Could not init Allegro.\n"); - return 1; - } - - al_init_primitives_addon(); - al_install_mouse(); - al_init_font_addon(); - al_init_ttf_addon(); - - display = al_create_display(640, 480); - if (!display) { - abort_example("Could not create display.\n"); - return 1; - } - al_install_keyboard(); - - if (argc >= 2) { - font_file = argv[1]; - } - - ex.f1 = al_load_font(font_file, 48, 0); - ex.f2 = al_load_font(font_file, 48, ALLEGRO_TTF_NO_KERNING); - ex.f3 = al_load_font(font_file, 12, 0); - /* Specifying negative values means we specify the glyph height - * in pixels, not the usual font size. - */ - ex.f4 = al_load_font(font_file, -140, ALLEGRO_TTF_MONOCHROME); - - if (!ex.f1 || !ex.f2 || !ex.f3 || !ex.f4) { - fprintf(stderr, "Could not load font: %s\n", font_file); - return 1; - } - - timer = al_create_timer(1.0 / 60); - - queue = al_create_event_queue(); - al_register_event_source(queue, al_get_keyboard_event_source()); - al_register_event_source(queue, al_get_display_event_source(display)); - al_register_event_source(queue, al_get_timer_event_source(timer)); - - al_start_timer(timer); - while (true) { - ALLEGRO_EVENT event; - al_wait_for_event(queue, &event); - if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) - break; - if (event.type == ALLEGRO_EVENT_KEY_DOWN && - event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { - break; - } - if (event.type == ALLEGRO_EVENT_TIMER) - redraw++; - - while (redraw > 0 && al_is_event_queue_empty(queue)) { - double dt; - redraw--; - - dt = al_get_time(); - render(); - dt = al_get_time() - dt; - - t = 0.99 * t + 0.01 * dt; - - ex.fps = 1.0 / t; - al_flip_display(); - } - } - - al_destroy_font(ex.f1); - al_destroy_font(ex.f2); - al_destroy_font(ex.f3); - al_destroy_font(ex.f4); - - return 0; -} - -/* vim: set sts=4 sw=4 et: */ diff --git a/examples/ex_ttf.cpp b/examples/ex_ttf.cpp new file mode 100644 index 0000000..ec17f9c --- /dev/null +++ b/examples/ex_ttf.cpp @@ -0,0 +1,178 @@ + +#include +#include +#include +#include + +#include "common.c" +#include "utf8lit.cpp" + +struct Example +{ + double fps; + ALLEGRO_FONT *f1, *f2, *f3, *f4; +} ex; + +static void render(void) +{ + ALLEGRO_COLOR white = al_map_rgba_f(1, 1, 1, 1); + ALLEGRO_COLOR black = al_map_rgba_f(0, 0, 0, 1); + ALLEGRO_COLOR red = al_map_rgba_f(1, 0, 0, 1); + ALLEGRO_COLOR green = al_map_rgba_f(0, 0.5, 0, 1); + ALLEGRO_COLOR blue = al_map_rgba_f(0.1, 0.2, 1, 1); + int x, y, w, h, as, de, xpos, ypos; + int target_w, target_h; + ALLEGRO_USTR_INFO info, sub_info; + ALLEGRO_USTR *u; + + al_clear_to_color(white); + + al_hold_bitmap_drawing(true); + + al_draw_textf(ex.f1, black, 50, 50, 0, "Tulip (kerning)"); + al_draw_textf(ex.f2, black, 50, 100, 0, "Tulip (no kerning)"); + al_draw_textf(ex.f3, black, 50, 200, 0, "This font has a size of 12 pixels, " + "the one above has 48 pixels."); + + al_hold_bitmap_drawing(false); + al_hold_bitmap_drawing(true); + + al_draw_textf(ex.f3, red, 50, 220, 0, "The color can simply be changed."); + + al_hold_bitmap_drawing(false); + al_hold_bitmap_drawing(true); + + al_draw_textf(ex.f3, green, 50, 240, 0, "Some unicode symbols:"); + al_draw_textf(ex.f3, green, 50, 260, 0, U("■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱")); + al_draw_textf(ex.f3, green, 50, 280, 0, U("▲△▴▵▶▷▸▹►▻▼▽▾▿◀◁◂◃◄◅◆◇◈◉◊")); + al_draw_textf(ex.f3, green, 50, 300, 0, U("○◌◍◎●◐◑◒◓◔◕◖◗◘◙")); + + #define OFF(x) al_ustr_offset(u, x) + #define SUB(x, y) al_ref_ustr(&sub_info, u, OFF(x), OFF(y)) + u = al_ref_cstr(&info, U("«Thís»|you")); + al_draw_ustr(ex.f3, green, 50, 320, 0, SUB(0, 6)); + u = al_ref_cstr(&info, U("should|‘ìş’")); + al_draw_ustr(ex.f3, green, 50, 340, 0, SUB(7, 11)); + u = al_ref_cstr(&info, U("not|“cøünt”|see")); + al_draw_ustr(ex.f3, green, 50, 360, 0, SUB(4, 11)); + u = al_ref_cstr(&info, U("réstrïçteđ…|this.")); + al_draw_ustr(ex.f3, green, 50, 380, 0, SUB(0, 11)); + + al_hold_bitmap_drawing(false); + + target_w = al_get_bitmap_width(al_get_target_bitmap()); + target_h = al_get_bitmap_height(al_get_target_bitmap()); + + xpos = target_w - 10; + ypos = target_h - 10; + al_get_text_dimensions(ex.f4, "Allegro", &x, &y, &w, &h); + as = al_get_font_ascent(ex.f4); + de = al_get_font_descent(ex.f4); + xpos -= w; + ypos -= h; + x += xpos; + y += ypos; + + al_draw_rectangle(x, y, x + w, y + h, black, 0); + al_draw_line(x, y + as, x + w, y + as, black, 0); + al_draw_line(x, y + as + de, x + w, y + as + de, black, 0); + + al_hold_bitmap_drawing(true); + al_draw_textf(ex.f4, blue, xpos, ypos, 0, "Allegro"); + al_hold_bitmap_drawing(false); + + al_hold_bitmap_drawing(true); + + al_draw_textf(ex.f3, black, target_w, 0, ALLEGRO_ALIGN_RIGHT, + "%.1f FPS", ex.fps); + + al_hold_bitmap_drawing(false); +} + +int main(int argc, const char *argv[]) +{ + const char *font_file = "data/DejaVuSans.ttf"; + ALLEGRO_DISPLAY *display; + ALLEGRO_TIMER *timer; + ALLEGRO_EVENT_QUEUE *queue; + int redraw = 0; + double t = 0; + + if (!al_init()) { + abort_example("Could not init Allegro.\n"); + return 1; + } + + al_init_primitives_addon(); + al_install_mouse(); + al_init_font_addon(); + al_init_ttf_addon(); + + display = al_create_display(640, 480); + if (!display) { + abort_example("Could not create display.\n"); + return 1; + } + al_install_keyboard(); + + if (argc >= 2) { + font_file = argv[1]; + } + + ex.f1 = al_load_font(font_file, 48, 0); + ex.f2 = al_load_font(font_file, 48, ALLEGRO_TTF_NO_KERNING); + ex.f3 = al_load_font(font_file, 12, 0); + /* Specifying negative values means we specify the glyph height + * in pixels, not the usual font size. + */ + ex.f4 = al_load_font(font_file, -140, ALLEGRO_TTF_MONOCHROME); + + if (!ex.f1 || !ex.f2 || !ex.f3 || !ex.f4) { + fprintf(stderr, "Could not load font: %s\n", font_file); + return 1; + } + + timer = al_create_timer(1.0 / 60); + + queue = al_create_event_queue(); + al_register_event_source(queue, al_get_keyboard_event_source()); + al_register_event_source(queue, al_get_display_event_source(display)); + al_register_event_source(queue, al_get_timer_event_source(timer)); + + al_start_timer(timer); + while (true) { + ALLEGRO_EVENT event; + al_wait_for_event(queue, &event); + if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) + break; + if (event.type == ALLEGRO_EVENT_KEY_DOWN && + event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { + break; + } + if (event.type == ALLEGRO_EVENT_TIMER) + redraw++; + + while (redraw > 0 && al_is_event_queue_empty(queue)) { + double dt; + redraw--; + + dt = al_get_time(); + render(); + dt = al_get_time() - dt; + + t = 0.99 * t + 0.01 * dt; + + ex.fps = 1.0 / t; + al_flip_display(); + } + } + + al_destroy_font(ex.f1); + al_destroy_font(ex.f2); + al_destroy_font(ex.f3); + al_destroy_font(ex.f4); + + return 0; +} + +/* vim: set sts=4 sw=4 et: */ diff --git a/examples/utf8lit.cpp b/examples/utf8lit.cpp new file mode 100644 index 0000000..3f8f993 --- /dev/null +++ b/examples/utf8lit.cpp @@ -0,0 +1,21 @@ +#include +#include + +#define U(s) (to_utf8(L ## s)) + +static std::map cache; + +static const char *to_utf8(const wchar_t *ws) +{ + std::string & s = cache[ws]; + if (s == "") { + /* This assumes the string contains only code points within the BMP. */ + ALLEGRO_USTR *us = al_ustr_new(""); + for (int i = 0; ws[i] != 0; i++) { + al_ustr_append_chr(us, ws[i]); + } + cache[ws] = al_cstr(us); + al_ustr_free(us); + } + return s.c_str(); +}