X operations(XOPS)に非常に近いFPSゲームを制作・リメイクし、成果物をオープンソースとして公開することを目的としたプロジェクトです。
| Revision | 144 (tree) |
|---|---|
| Time | 2016-08-27 08:35:52 |
| Author | |
OpenGLにてシステムフォントによる文字列がうまく出ない問題を改善
| @@ -58,6 +58,7 @@ | ||
| 58 | 58 | now_SystemFontUStr[0] = NULL; |
| 59 | 59 | SystemFontListIdx = 0; |
| 60 | 60 | SystemFontListIdxSize = 0; |
| 61 | + SystemFont_posz = 1.0f; | |
| 61 | 62 | now_textureid = -1; |
| 62 | 63 | |
| 63 | 64 | camera_x = 0.0f; |
| @@ -1161,6 +1162,9 @@ | ||
| 1161 | 1162 | glAlphaFunc(GL_GREATER, 0.0f); |
| 1162 | 1163 | glEnable(GL_ALPHA_TEST); |
| 1163 | 1164 | |
| 1165 | + //2DシステムフォントZ座標初期化 | |
| 1166 | + SystemFont_posz = 1.0f; | |
| 1167 | + | |
| 1164 | 1168 | return 0; |
| 1165 | 1169 | } |
| 1166 | 1170 |
| @@ -1707,15 +1711,43 @@ | ||
| 1707 | 1711 | glDisableClientState(GL_COLOR_ARRAY); |
| 1708 | 1712 | } |
| 1709 | 1713 | |
| 1714 | +//! @brief 最も長い行の文字数を取得 | |
| 1715 | +//! @param str 文字列 (改行コード:可) | |
| 1716 | +//! @return 文字数 | |
| 1717 | +//! @attention マルチバイト文字は 1文字あたり 2 としてカウントされます。 | |
| 1718 | +int D3DGraphics::StrMaxLineLen(char *str) | |
| 1719 | +{ | |
| 1720 | + int maxlen = 0; | |
| 1721 | + int cnt = 0; | |
| 1722 | + | |
| 1723 | + for(int i=0; i<strlen(str); i++){ | |
| 1724 | + if( str[i] == '\n' ){ | |
| 1725 | + if( maxlen < cnt ){ | |
| 1726 | + maxlen = cnt; | |
| 1727 | + } | |
| 1728 | + cnt = 0; | |
| 1729 | + } | |
| 1730 | + else{ | |
| 1731 | + cnt += 1; | |
| 1732 | + } | |
| 1733 | + } | |
| 1734 | + | |
| 1735 | + if( maxlen < cnt ){ | |
| 1736 | + maxlen = cnt; | |
| 1737 | + } | |
| 1738 | + | |
| 1739 | + return maxlen; | |
| 1740 | +} | |
| 1741 | + | |
| 1710 | 1742 | //! @brief 文字を描画(システムフォント使用) |
| 1711 | 1743 | //! @param x x座標 |
| 1712 | 1744 | //! @param y y座標 |
| 1713 | 1745 | //! @param str 文字列 (改行コード:可) |
| 1714 | 1746 | //! @param color 色 |
| 1747 | +//! @warning 本関数は1フレーム間で100回までしか呼び出せません。(OpenGLコアのみ) | |
| 1715 | 1748 | //! @warning <b>描画は非常に低速です。</b>画面内で何度も呼び出すとパフォーマンスに影響します。 |
| 1716 | 1749 | //! @warning「改行コードを活用し一度に描画する」「日本語が必要ない文字はテクスチャフォントを活用する」などの対応を講じてください。 |
| 1717 | 1750 | //! @attention フォントの種類やサイズは固定です。 文字を二重に重ねて立体感を出さないと見にくくなります。 |
| 1718 | -//! @todo 文字を二重に重ねると、上下関係が正しく処理されない。 | |
| 1719 | 1751 | //! @todo 1文字目が欠ける場合がある。 |
| 1720 | 1752 | void D3DGraphics::Draw2DMSFontText(int x, int y, char *str, int color) |
| 1721 | 1753 | { |
| @@ -1722,7 +1754,10 @@ | ||
| 1722 | 1754 | int len = strlen(str); |
| 1723 | 1755 | WCHAR *ustr; |
| 1724 | 1756 | |
| 1757 | + y += 18; | |
| 1758 | + | |
| 1725 | 1759 | Start2DRender(); |
| 1760 | + glEnable(GL_DEPTH_TEST); | |
| 1726 | 1761 | |
| 1727 | 1762 | //テクスチャ無効 |
| 1728 | 1763 | glDisable(GL_TEXTURE_2D); |
| @@ -1764,7 +1799,7 @@ | ||
| 1764 | 1799 | |
| 1765 | 1800 | //座標と色を設定 |
| 1766 | 1801 | glBitmap(0, 0, 0, 0, 10, 0, NULL); |
| 1767 | - glRasterPos2i(x, y); | |
| 1802 | + glRasterPos3f((float)x, (float)y, SystemFont_posz); | |
| 1768 | 1803 | glColor4ub((color>>24)&0xFF, (color>>16)&0xFF, (color>>8)&0xFF, color&0xFF); |
| 1769 | 1804 | |
| 1770 | 1805 | for(int i=0; i<lstrlenW(ustr); i++){ |
| @@ -1771,7 +1806,7 @@ | ||
| 1771 | 1806 | if( ustr[i] == '\n' ){ |
| 1772 | 1807 | //改行する |
| 1773 | 1808 | y += 19; |
| 1774 | - glRasterPos2i(x, y); | |
| 1809 | + glRasterPos3f((float)x, (float)y, SystemFont_posz); | |
| 1775 | 1810 | } |
| 1776 | 1811 | else{ |
| 1777 | 1812 | //ディスプレイリスト描画 |
| @@ -1779,9 +1814,12 @@ | ||
| 1779 | 1814 | } |
| 1780 | 1815 | } |
| 1781 | 1816 | |
| 1817 | + SystemFont_posz -= 0.01f; | |
| 1818 | + | |
| 1782 | 1819 | //Unicode文字列の廃棄 |
| 1783 | 1820 | delete [] ustr; |
| 1784 | 1821 | |
| 1822 | + //glDisable(GL_DEPTH_TEST); | |
| 1785 | 1823 | End2DRender(); |
| 1786 | 1824 | } |
| 1787 | 1825 |
| @@ -1792,10 +1830,9 @@ | ||
| 1792 | 1830 | //! @param h 縦の大きさ |
| 1793 | 1831 | //! @param str 文字列 (改行コード:可) |
| 1794 | 1832 | //! @param color 色 |
| 1795 | -//! @warning <b>正しく中央揃えになりません。</b> | |
| 1796 | 1833 | void D3DGraphics::Draw2DMSFontTextCenter(int x, int y, int w, int h, char *str, int color) |
| 1797 | 1834 | { |
| 1798 | - Draw2DMSFontText(x, y, str, color); | |
| 1835 | + Draw2DMSFontText(x + (SCREEN_WIDTH/2 - (StrMaxLineLen(str)*9/2)), y, str, color); | |
| 1799 | 1836 | } |
| 1800 | 1837 | |
| 1801 | 1838 | //! @brief 2D描画用設定 |
| @@ -208,6 +208,7 @@ | ||
| 208 | 208 | WCHAR *now_SystemFontUStr; //!< 現在表示中のシステムフォントによる文字列(Unicode) |
| 209 | 209 | GLuint SystemFontListIdx; //!< システムフォントのディスプレイリスト |
| 210 | 210 | int SystemFontListIdxSize; //!< システムフォントのディスプレイリストのサイズ |
| 211 | + float SystemFont_posz; //!< システムフォントのZ座標 | |
| 211 | 212 | int now_textureid; //!< 現在設定中のテクスチャ番号 |
| 212 | 213 | |
| 213 | 214 | float camera_x; //!< カメラ座標 |
| @@ -236,6 +237,7 @@ | ||
| 236 | 237 | bool LoadJPEGTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture); |
| 237 | 238 | bool LoadPNGTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture); |
| 238 | 239 | void SetTexture(int TextureID); |
| 240 | + int StrMaxLineLen(char *str); | |
| 239 | 241 | void Start2DRender(); |
| 240 | 242 | void End2DRender(); |
| 241 | 243 |