the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 307 lines 9.3 kB view raw
1#include "stdafx.h" 2#include "XUI_FontRenderer.h" 3#include "XUI_Font.h" 4#include "XUI_FontData.h" 5#include "..\..\..\Minecraft.World\StringHelpers.h" 6 7extern IDirect3DDevice9 *g_pD3DDevice; 8extern void GetRenderAndSamplerStates(IDirect3DDevice9 *pDevice,DWORD *RenderStateA,DWORD *SamplerStateA); 9extern void SetRenderAndSamplerStates(IDirect3DDevice9 *pDevice,DWORD *RenderStateA,DWORD *SamplerStateA); 10 11XUI_FontRenderer::XUI_FontRenderer() 12{ 13 ZeroMemory(m_loadedFontData, sizeof(XUI_FontData*) * eFontData_MAX); 14 15 //XuiFontSetRenderer(this); 16 17 //Minecraft *pMinecraft=Minecraft::GetInstance(); 18 19 //ScreenSizeCalculator ssc(pMinecraft->options, pMinecraft->width_phys, pMinecraft->height_phys); 20 //m_fScreenWidth=(float)pMinecraft->width_phys; 21 //m_fRawWidth=(float)ssc.rawWidth; 22 //m_fScreenHeight=(float)pMinecraft->height_phys; 23 //m_fRawHeight=(float)ssc.rawHeight; 24} 25 26HRESULT XUI_FontRenderer::Init( float fDpi ) 27{ 28 return( S_OK ); 29 30} 31 32VOID XUI_FontRenderer::Term() 33{ 34 return; 35} 36 37HRESULT XUI_FontRenderer::GetCaps( DWORD * pdwCaps ) 38{ 39 if( pdwCaps != NULL ) 40 { 41 // setting this means XUI calls the DrawCharsToDevice method 42 *pdwCaps = XUI_FONT_RENDERER_CAP_INTERNAL_GLYPH_CACHE | XUI_FONT_RENDERER_CAP_POINT_SIZE_RESPECTED | XUI_FONT_RENDERER_STYLE_DROPSHADOW; 43 } 44 return ( S_OK ); 45} 46 47HRESULT XUI_FontRenderer::CreateFont( const TypefaceDescriptor * pTypefaceDescriptor, float fPointSize, DWORD dwStyle, DWORD dwReserved, HFONTOBJ * phFont ) 48{ 49 float fXuiSize = fPointSize * ( 16.0f / 14.0f ); 50 //float fXuiSize = fPointSize * ( 16.0f / 16.0f ); 51 fXuiSize /= 4.0f; 52 fXuiSize = floor( fXuiSize ); 53 int xuiSize = (int)(fXuiSize * 4.0f); 54 if( xuiSize < 1 ) xuiSize = 8; 55 56 // 4J Stu - We have fonts based on multiples of 8 or 12 57 // We don't want to make the text larger as then it may not fit in the box specified 58 // so we decrease the size until we find one that will look ok 59 while( xuiSize%8!=0 && xuiSize%12!=0 ) xuiSize -= 2; 60 61 //app.DebugPrintf("point size is: %f, xuiSize is: %d\n", fPointSize, xuiSize); 62 63 XUI_Font *font = NULL; 64 XUI_FontData *fontData = NULL; 65 FLOAT scale = 1; 66 67 eFontData efontdata; 68 if( xuiSize%12==0 ) 69 { 70 scale = xuiSize/12; 71 efontdata = eFontData_Mojangles_11; 72 } 73 else 74 { 75 scale = xuiSize/8; 76 efontdata = eFontData_Mojangles_7; 77 } 78 79 font = m_loadedFonts[efontdata][scale]; 80 if (font == NULL) 81 { 82 fontData = m_loadedFontData[efontdata]; 83 if (fontData == NULL) 84 { 85 SFontData *sfontdata; 86 switch (efontdata) 87 { 88 case eFontData_Mojangles_7: sfontdata = &SFontData::Mojangles_7; break; 89 case eFontData_Mojangles_11: sfontdata = &SFontData::Mojangles_11; break; 90 default: sfontdata = NULL; break; 91 } 92 93 fontData = new XUI_FontData(); 94 fontData->Create(*sfontdata); 95 96 m_loadedFontData[efontdata] = fontData; 97 } 98 99 font = new XUI_Font( efontdata, scale, fontData ); 100 m_loadedFonts[efontdata][scale] = font; 101 } 102 font->IncRefCount(); 103 104 *phFont = (HFONTOBJ)font; 105 return S_OK; 106} 107 108VOID XUI_FontRenderer::ReleaseFont( HFONTOBJ hFont ) 109{ 110 XUI_Font *xuiFont = (XUI_Font*) hFont; 111 if (xuiFont != NULL) 112 { 113 xuiFont->DecRefCount(); 114 if (xuiFont->refCount <= 0) 115 { 116 AUTO_VAR(it, m_loadedFonts[xuiFont->m_iFontData].find(xuiFont->m_fScaleFactor) ); 117 if (it != m_loadedFonts[xuiFont->m_iFontData].end()) m_loadedFonts[xuiFont->m_iFontData].erase(it); 118 delete hFont; 119 } 120 } 121 return; 122} 123 124HRESULT XUI_FontRenderer::GetFontMetrics( HFONTOBJ hFont, XUIFontMetrics *pFontMetrics ) 125{ 126 if( hFont == 0 || pFontMetrics == 0 ) return E_INVALIDARG; 127 128 XUI_Font *font = (XUI_Font *)hFont; 129 130 pFontMetrics->fLineHeight = (font->m_fontData->getFontYAdvance() + 1) * font->m_fYScaleFactor; 131 pFontMetrics->fMaxAscent = font->m_fontData->getMaxAscent() * font->m_fYScaleFactor; 132 pFontMetrics->fMaxDescent = font->m_fontData->getMaxDescent() * font->m_fYScaleFactor; 133 pFontMetrics->fMaxHeight = font->m_fontData->getFontHeight() * font->m_fYScaleFactor; 134 pFontMetrics->fMaxWidth = font->m_fontData->getFontMaxWidth() * font->m_fXScaleFactor; 135 pFontMetrics->fMaxAdvance = font->m_fontData->getFontMaxWidth() * font->m_fXScaleFactor; 136 137 //*pFontMetrics = font->m_fontMetrics; // g_fontMetrics; 138 return( S_OK ); 139} 140 141HRESULT XUI_FontRenderer::GetCharMetrics( HFONTOBJ hFont, WCHAR wch, XUICharMetrics *pCharMetrics ) 142{ 143 if (hFont == 0 || pCharMetrics == 0) return E_INVALIDARG; 144 145 XUI_Font *font = (XUI_Font *)hFont; 146 XUI_FontData::SChar sChar = font->m_fontData->getChar(wch); 147 148 pCharMetrics->fMinX = sChar.getMinX() * font->m_fYScaleFactor; 149 pCharMetrics->fMinY = sChar.getMinY() * font->m_fYScaleFactor; 150 pCharMetrics->fMaxX = sChar.getMaxX() * font->m_fYScaleFactor; 151 pCharMetrics->fMaxY = sChar.getMaxY() * font->m_fYScaleFactor; 152 pCharMetrics->fAdvance = sChar.getAdvance() * font->m_fYScaleFactor; 153 154 return(S_OK); 155} 156 157HRESULT XUI_FontRenderer::DrawCharToTexture( HFONTOBJ hFont, WCHAR wch, HXUIDC hDC, IXuiTexture * pTexture, UINT x, UINT y, UINT width, UINT height, UINT insetX, UINT insetY ) 158{ 159 if( hFont==0 || pTexture==NULL ) return E_INVALIDARG; 160 return( S_OK ); 161} 162 163HRESULT XUI_FontRenderer::DrawCharsToDevice( HFONTOBJ hFont, CharData * pCharData, DWORD dwCount, RECT *pClipRect, HXUIDC hDC, D3DXMATRIX * pWorldViewProj ) 164{ 165 if( hFont == 0 ) return E_INVALIDARG; 166 if( dwCount == 0 ) return( S_OK ); 167 168 DWORD RenderStateA[8]; 169 DWORD SamplerStateA[5]; 170 XMVECTOR vconsts[20]; 171 XMVECTOR pconsts[20]; 172 XUI_Font *font = (XUI_Font *)hFont; 173 174 // 4J-PB - if we're in 480 Widescreen mode, we need to ensure that the font characters are aligned on an even boundary if they are a 2x multiple 175 if(!RenderManager.IsHiDef()) 176 { 177 if(RenderManager.IsWidescreen()) 178 { 179 float fScaleX, fScaleY; 180 font->GetScaleFactors(&fScaleX,&fScaleY); 181 int iScaleX=fScaleX; 182 int iScaleY=fScaleY; 183 184 if(iScaleX%2==0) 185 { 186 int iWorldX=pWorldViewProj->_41; 187 pWorldViewProj->_41 = (float)(iWorldX & -2); 188 } 189 if(iScaleY%2==0) 190 { 191 int iWorldY=pWorldViewProj->_42; 192 pWorldViewProj->_42 = (float)(iWorldY & -2); 193 } 194 } 195 else 196 { 197 // make x an even number for 480 4:3 198 int iWorldX=pWorldViewProj->_41; 199 pWorldViewProj->_41 = (float)(iWorldX & -2); 200 201 // 480 SD mode - y needs to be on a pixel boundary when multiplied by 1.5, so if it's an odd number, subtract 1/3 from it 202 int iWorldY=pWorldViewProj->_42; 203 if(iWorldY%2==1) 204 { 205 pWorldViewProj->_42-=1.0f/3.0f; 206 } 207 } 208 } 209 210 g_pD3DDevice->GetVertexShaderConstantF( 0, (float *)vconsts, 20 ); 211 g_pD3DDevice->GetPixelShaderConstantF( 0, (float *)pconsts, 20 ); 212 g_pD3DDevice->SetRenderState(D3DRS_HALFPIXELOFFSET, TRUE); 213 GetRenderAndSamplerStates(g_pD3DDevice, RenderStateA, SamplerStateA ); 214 glDisable(GL_CULL_FACE); 215 glDisable(GL_DEPTH_TEST); 216 217 RenderManager.Set_matrixDirty(); 218 glMatrixMode(GL_PROJECTION); 219 glLoadIdentity(); 220 glOrtho(0, 1280.0f, 720.0f, 0, 1000, 3000); 221 glMatrixMode(GL_MODELVIEW); 222 glLoadIdentity(); 223 glTranslatef(0, 0, -2000); 224 glColor4f(1.0f,1.0f,1.0f,1.0f); 225 float matrixCopy[16]; 226 memcpy(matrixCopy, pWorldViewProj, 64); 227 matrixCopy[11] = 0.0f; 228 matrixCopy[12] = floor(matrixCopy[12] + 0.5f); 229 matrixCopy[13] = floor(matrixCopy[13] + 0.5f); 230 matrixCopy[14] = floor(matrixCopy[14] + 0.5f); 231 matrixCopy[15] = 1.0f; 232 glMultMatrixf(matrixCopy); 233 234 235 float lineXPos = 0.0f; 236 float lineYPos = 0.0f; 237 DWORD colour = 0; 238 DWORD style = 0; 239#if 1 240 for( int i = 0; i < dwCount; i++ ) 241 { 242 wstring string; 243 string.push_back(pCharData[i].wch); 244 lineYPos = pCharData[i].y; 245 lineXPos = pCharData[i].x; 246 colour = pCharData[i].dwColor; 247 style = pCharData[i].dwStyle; 248 249 //if(pCharData[i].wch > font->m_fontData->getMaxGlyph()) 250 if ( !font->m_fontData->getChar(pCharData[i].wch).hasChar() ) 251 { 252 // Can't render this character, fallback to the default renderer 253 app.OverrideFontRenderer(false,false); 254 break; 255 } 256#else 257 DWORD i = 0; 258 while( i < dwCount ) 259 { 260 wstring string; 261 lineYPos = pCharData[i].y; 262 lineXPos = pCharData[i].x; 263 colour = pCharData[i].dwColor; 264 style = pCharData[i].dwStyle; 265 266 while(i < dwCount && pCharData[i].y == lineYPos) 267 { 268 string.push_back(pCharData[i].wch); 269 ++i; 270 } 271#endif 272 273 bool dropShadow = false; 274 if( (style & XUI_FONT_STYLE_DROPSHADOW) == XUI_FONT_STYLE_DROPSHADOW) dropShadow = true; 275 276 //int yPos = (int)pCharData[i].y + (int)(font->m_fontMetrics.fLineHeight - font->m_fontMetrics.fMaxAscent)/2; 277 //if( (pCharData[i].dwStyle & XUI_FONT_STYLE_VERTICAL_CENTER) == XUI_FONT_STYLE_VERTICAL_CENTER) 278 //{ 279 // yPos = (pClipRect->bottom - (int)font->m_fontMetrics.fLineHeight) / 2; 280 //} 281 282 if(dropShadow) 283 { 284 DWORD shadowColour; 285 XuiGetTextDropShadowColor(hDC, &shadowColour); 286 // 4J Stu - Shadow colour is currently ignored 287 font->DrawShadowText( lineXPos,lineYPos,colour,shadowColour,string.c_str() ); 288 //drawShadow(thisChar, (int)pCharData[i].x, yPos, pCharData[i].dwColor ); 289 } 290 else 291 { 292 font->DrawText( lineXPos,lineYPos,colour,string.c_str() ); 293 //draw(thisChar, (int)pCharData[i].x, yPos, pCharData[i].dwColor, false ); 294 } 295 } 296 297 g_pD3DDevice->SetVertexShaderConstantF( 0, (float *)vconsts, 20 ); 298 g_pD3DDevice->SetPixelShaderConstantF( 0, (float *)pconsts, 20 ); 299 SetRenderAndSamplerStates(g_pD3DDevice, RenderStateA, SamplerStateA ); 300 g_pD3DDevice->SetRenderState(D3DRS_HALFPIXELOFFSET, FALSE); 301 glEnable(GL_CULL_FACE); 302 glEnable(GL_DEPTH_TEST); 303 304 XuiRenderRestoreState(hDC); 305 306 return( S_OK ); 307}