src/osd/sdl/sdlmain.c
| r9580 | r9581 | |
| 13 | 13 | #include <SDL/SDL.h> |
| 14 | 14 | #include <SDL/SDL_version.h> |
| 15 | 15 | |
| 16 | #ifdef SDLMAME_UNIX |
| 17 | #ifndef SDLMAME_MACOSX |
| 18 | #include <SDL_ttf.h> |
| 19 | #include <fontconfig/fontconfig.h> |
| 20 | #endif |
| 21 | #endif |
| 22 | |
| 16 | 23 | // standard includes |
| 17 | 24 | #ifdef MESS |
| 18 | 25 | #include <unistd.h> |
| r9580 | r9581 | |
| 280 | 287 | setvbuf(stdout, (char *) NULL, _IONBF, 0); |
| 281 | 288 | setvbuf(stderr, (char *) NULL, _IONBF, 0); |
| 282 | 289 | |
| 290 | #ifdef SDLMAME_UNIX |
| 291 | #ifndef SDLMAME_MACOSX |
| 292 | if (TTF_Init() == -1) |
| 293 | { |
| 294 | printf("SDL_ttf failed: %s\n", TTF_GetError()); |
| 295 | } |
| 296 | FcInit(); |
| 297 | #endif |
| 298 | #endif |
| 299 | |
| 283 | 300 | #ifdef SDLMAME_OS2 |
| 284 | 301 | MorphToPM(); |
| 285 | 302 | #endif |
| r9580 | r9581 | |
| 318 | 335 | // already called... |
| 319 | 336 | //SDL_Quit(); |
| 320 | 337 | |
| 338 | #ifdef SDLMAME_UNIX |
| 339 | #ifndef SDLMAME_MACOSX |
| 340 | TTF_Quit(); |
| 341 | FcFini(); |
| 342 | #endif |
| 343 | #endif |
| 344 | |
| 321 | 345 | exit(res); |
| 322 | 346 | |
| 323 | 347 | return res; |
| r9580 | r9581 | |
| 596 | 620 | SDL_EnableUNICODE(SDL_TRUE); |
| 597 | 621 | #endif |
| 598 | 622 | } |
| 623 | |
| 624 | #ifdef SDLMAME_UNIX |
| 625 | #ifdef SDLMAME_MACOSX |
| 626 | //------------------------------------------------- |
| 627 | // font_open - attempt to "open" a handle to the |
| 628 | // font with the given name |
| 629 | //------------------------------------------------- |
| 630 | |
| 631 | osd_font sdl_osd_interface::font_open(const char *_name, int &height) |
| 632 | { |
| 633 | return (osd_font)NULL; |
| 634 | } |
| 635 | |
| 636 | //------------------------------------------------- |
| 637 | // font_close - release resources associated with |
| 638 | // a given OSD font |
| 639 | //------------------------------------------------- |
| 640 | |
| 641 | void sdl_osd_interface::font_close(osd_font font) |
| 642 | { |
| 643 | } |
| 644 | |
| 645 | //------------------------------------------------- |
| 646 | // font_get_bitmap - allocate and populate a |
| 647 | // BITMAP_FORMAT_ARGB32 bitmap containing the |
| 648 | // pixel values MAKE_ARGB(0xff,0xff,0xff,0xff) |
| 649 | // or MAKE_ARGB(0x00,0xff,0xff,0xff) for each |
| 650 | // pixel of a black & white font |
| 651 | //------------------------------------------------- |
| 652 | |
| 653 | bitmap_t *sdl_osd_interface::font_get_bitmap(osd_font font, unicode_char chnum, INT32 &width, INT32 &xoffs, INT32 &yoffs) |
| 654 | { |
| 655 | return (bitmap_t *)NULL; |
| 656 | } |
| 657 | #else |
| 658 | //------------------------------------------------- |
| 659 | // font_open - attempt to "open" a handle to the |
| 660 | // font with the given name |
| 661 | //------------------------------------------------- |
| 662 | |
| 663 | osd_font sdl_osd_interface::font_open(const char *_name, int &height) |
| 664 | { |
| 665 | TTF_Font *font = (TTF_Font *)NULL; |
| 666 | int style = 0; |
| 667 | FcConfig *config; |
| 668 | FcPattern *pat; |
| 669 | FcObjectSet *os; |
| 670 | FcFontSet *fontset; |
| 671 | FcValue val; |
| 672 | bool bakedstyles = false; |
| 673 | |
| 674 | // accept qualifiers from the name |
| 675 | astring name(_name); |
| 676 | |
| 677 | if (name == "default") |
| 678 | { |
| 679 | name = "Liberation Sans"; |
| 680 | } |
| 681 | |
| 682 | bool bold = (name.replace(0, "[B]", "") + name.replace(0, "[b]", "") > 0); |
| 683 | bool italic = (name.replace(0, "[I]", "") + name.replace(0, "[i]", "") > 0); |
| 684 | bool underline = (name.replace(0, "[U]", "") + name.replace(0, "[u]", "") > 0); |
| 685 | bool strike = (name.replace(0, "[S]", "") + name.replace(0, "[s]", "") > 0); |
| 686 | |
| 687 | // first up, try it as a filename |
| 688 | font = TTF_OpenFont(name.cstr(), 120); |
| 689 | |
| 690 | // if that didn't work, crank up the FontConfig database |
| 691 | if (!font) |
| 692 | { |
| 693 | config = FcConfigGetCurrent(); |
| 694 | pat = FcPatternCreate(); |
| 695 | os = FcObjectSetCreate(); |
| 696 | FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *)name.cstr()); |
| 697 | |
| 698 | // try and get a font with the requested styles baked-in |
| 699 | if (bold) |
| 700 | { |
| 701 | if (italic) |
| 702 | { |
| 703 | FcPatternAddString(pat, FC_STYLE, (const FcChar8 *)"Bold Italic"); |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | FcPatternAddString(pat, FC_STYLE, (const FcChar8 *)"Bold"); |
| 708 | } |
| 709 | } |
| 710 | else if (italic) |
| 711 | { |
| 712 | FcPatternAddString(pat, FC_STYLE, (const FcChar8 *)"Italic"); |
| 713 | } |
| 714 | else |
| 715 | { |
| 716 | FcPatternAddString(pat, FC_STYLE, (const FcChar8 *)"Regular"); |
| 717 | } |
| 718 | |
| 719 | FcPatternAddString(pat, FC_FONTFORMAT, (const FcChar8 *)"TrueType"); |
| 720 | |
| 721 | FcObjectSetAdd(os, FC_FILE); |
| 722 | fontset = FcFontList(config, pat, os); |
| 723 | |
| 724 | for (int i = 0; i < fontset->nfont; i++) |
| 725 | { |
| 726 | if (FcPatternGet(fontset->fonts[i], FC_FILE, 0, &val) != FcResultMatch) |
| 727 | { |
| 728 | continue; |
| 729 | } |
| 730 | |
| 731 | if (val.type != FcTypeString) |
| 732 | { |
| 733 | continue; |
| 734 | } |
| 735 | |
| 736 | mame_printf_verbose("Matching font: %s\n", val.u.s); |
| 737 | font = TTF_OpenFont((const char*)val.u.s, 120); |
| 738 | |
| 739 | if (font) |
| 740 | { |
| 741 | bakedstyles = true; |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // didn't get a font above? try again with no baked-in styles |
| 747 | if (!font) |
| 748 | { |
| 749 | FcPatternDestroy(pat); |
| 750 | FcFontSetDestroy(fontset); |
| 751 | |
| 752 | pat = FcPatternCreate(); |
| 753 | FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *)name.cstr()); |
| 754 | FcPatternAddString(pat, FC_STYLE, (const FcChar8 *)"Regular"); |
| 755 | FcPatternAddString(pat, FC_FONTFORMAT, (const FcChar8 *)"TrueType"); |
| 756 | fontset = FcFontList(config, pat, os); |
| 757 | |
| 758 | for (int i = 0; i < fontset->nfont; i++) |
| 759 | { |
| 760 | if (FcPatternGet(fontset->fonts[i], FC_FILE, 0, &val) != FcResultMatch) |
| 761 | { |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | if (val.type != FcTypeString) |
| 766 | { |
| 767 | continue; |
| 768 | } |
| 769 | |
| 770 | mame_printf_verbose("Matching unstyled font: %s\n", val.u.s); |
| 771 | font = TTF_OpenFont((const char*)val.u.s, 120); |
| 772 | |
| 773 | if (font) |
| 774 | { |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | FcPatternDestroy(pat); |
| 781 | FcObjectSetDestroy(os); |
| 782 | FcFontSetDestroy(fontset); |
| 783 | } |
| 784 | |
| 785 | if (!font) |
| 786 | { |
| 787 | printf("WARNING: Couldn't find/open TrueType font %s, using MAME default\n", name.cstr()); |
| 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | // apply styles |
| 792 | if (!bakedstyles) |
| 793 | { |
| 794 | style |= bold ? TTF_STYLE_BOLD : 0; |
| 795 | style |= italic ? TTF_STYLE_ITALIC : 0; |
| 796 | } |
| 797 | style |= underline ? TTF_STYLE_UNDERLINE : 0; |
| 798 | style |= strike ? TTF_STYLE_STRIKETHROUGH : 0; |
| 799 | TTF_SetFontStyle(font, style); |
| 800 | |
| 801 | height = TTF_FontLineSkip(font); |
| 802 | |
| 803 | return (osd_font)font; |
| 804 | } |
| 805 | |
| 806 | //------------------------------------------------- |
| 807 | // font_close - release resources associated with |
| 808 | // a given OSD font |
| 809 | //------------------------------------------------- |
| 810 | |
| 811 | void sdl_osd_interface::font_close(osd_font font) |
| 812 | { |
| 813 | TTF_Font *ttffont; |
| 814 | |
| 815 | ttffont = (TTF_Font *)font; |
| 816 | |
| 817 | TTF_CloseFont(ttffont); |
| 818 | } |
| 819 | |
| 820 | //------------------------------------------------- |
| 821 | // font_get_bitmap - allocate and populate a |
| 822 | // BITMAP_FORMAT_ARGB32 bitmap containing the |
| 823 | // pixel values MAKE_ARGB(0xff,0xff,0xff,0xff) |
| 824 | // or MAKE_ARGB(0x00,0xff,0xff,0xff) for each |
| 825 | // pixel of a black & white font |
| 826 | //------------------------------------------------- |
| 827 | |
| 828 | bitmap_t *sdl_osd_interface::font_get_bitmap(osd_font font, unicode_char chnum, INT32 &width, INT32 &xoffs, INT32 &yoffs) |
| 829 | { |
| 830 | TTF_Font *ttffont; |
| 831 | bitmap_t *bitmap = (bitmap_t *)NULL; |
| 832 | SDL_Surface *drawsurf; |
| 833 | SDL_Color fcol = { 0xff, 0xff, 0xff }; |
| 834 | char ustr[16]; |
| 835 | |
| 836 | ttffont = (TTF_Font *)font; |
| 837 | |
| 838 | memset(ustr, 0, sizeof(ustr)); |
| 839 | utf8_from_uchar(ustr, 1, chnum); |
| 840 | drawsurf = TTF_RenderUTF8_Solid(ttffont, ustr, fcol); |
| 841 | |
| 842 | // was nothing returned? |
| 843 | if (drawsurf) |
| 844 | { |
| 845 | // allocate a MAME destination bitmap |
| 846 | bitmap = auto_alloc(&machine(), bitmap_t(drawsurf->w, drawsurf->h, BITMAP_FORMAT_ARGB32)); |
| 847 | |
| 848 | // copy the rendered character image into it |
| 849 | for (int y = 0; y < bitmap->height; y++) |
| 850 | { |
| 851 | UINT32 *dstrow = BITMAP_ADDR32(bitmap, y, 0); |
| 852 | UINT8 *srcrow = (UINT8 *)drawsurf->pixels; |
| 853 | |
| 854 | srcrow += (y * drawsurf->pitch); |
| 855 | |
| 856 | for (int x = 0; x < drawsurf->w; x++) |
| 857 | { |
| 858 | dstrow[x] = srcrow[x] ? MAKE_ARGB(0xff,0xff,0xff,0xff) : MAKE_ARGB(0x00,0xff,0xff,0xff); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | // what are these? |
| 863 | xoffs = yoffs = 0; |
| 864 | width = drawsurf->w; |
| 865 | |
| 866 | SDL_FreeSurface(drawsurf); |
| 867 | } |
| 868 | |
| 869 | return bitmap; |
| 870 | } |
| 871 | #endif // not OSX |
| 872 | #else // not UNIX |
| 873 | //------------------------------------------------- |
| 874 | // font_open - attempt to "open" a handle to the |
| 875 | // font with the given name |
| 876 | //------------------------------------------------- |
| 877 | |
| 878 | osd_font sdl_osd_interface::font_open(const char *_name, int &height) |
| 879 | { |
| 880 | return (osd_font)NULL; |
| 881 | } |
| 882 | |
| 883 | //------------------------------------------------- |
| 884 | // font_close - release resources associated with |
| 885 | // a given OSD font |
| 886 | //------------------------------------------------- |
| 887 | |
| 888 | void sdl_osd_interface::font_close(osd_font font) |
| 889 | { |
| 890 | } |
| 891 | |
| 892 | //------------------------------------------------- |
| 893 | // font_get_bitmap - allocate and populate a |
| 894 | // BITMAP_FORMAT_ARGB32 bitmap containing the |
| 895 | // pixel values MAKE_ARGB(0xff,0xff,0xff,0xff) |
| 896 | // or MAKE_ARGB(0x00,0xff,0xff,0xff) for each |
| 897 | // pixel of a black & white font |
| 898 | //------------------------------------------------- |
| 899 | |
| 900 | bitmap_t *sdl_osd_interface::font_get_bitmap(osd_font font, unicode_char chnum, INT32 &width, INT32 &xoffs, INT32 &yoffs) |
| 901 | { |
| 902 | return (bitmap_t *)NULL; |
| 903 | } |
| 904 | #endif |