the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at master 395 lines 11 kB view raw
1#include "stdafx.h" 2#include "..\..\stubs.h" 3#include "..\..\Minecraft.h" 4#include "..\..\Textures.h" 5#include "XUI_FontData.h" 6#include "..\..\..\Minecraft.World\StringHelpers.h" 7 8 9#define USE_NEW 0 10 11 12extern IDirect3DDevice9 *g_pD3DDevice; 13 14int XUI_FontData::getMaxGlyph() 15{ 16 return m_fontData->getFontData()->m_uiGlyphCount; 17} 18 19float XUI_FontData::getFontHeight() 20{ 21 return m_fontData->getFontData()->m_uiGlyphHeight; 22} 23 24float XUI_FontData::getFontTopPadding() 25{ 26 return 0; 27} 28 29float XUI_FontData::getFontBottomPadding() 30{ 31 return 0; 32} 33 34float XUI_FontData::getFontYAdvance() 35{ 36 return m_fontData->getFontData()->m_uiGlyphHeight - 1; 37} 38 39float XUI_FontData::getFontMaxWidth() 40{ 41 return m_fontData->getFontData()->m_uiGlyphWidth; 42} 43 44float XUI_FontData::getMaxDescent() 45{ 46 return 0; 47} 48 49float XUI_FontData::getMaxAscent() 50{ 51 return m_fontData->getFontData()->m_uiGlyphHeight; 52} 53 54int XUI_FontData::getImageWidth() 55{ 56 return m_fontData->getFontData()->m_uiGlyphMapX; 57} 58 59int XUI_FontData::getImageHeight() 60{ 61 return m_fontData->getFontData()->m_uiGlyphMapY; 62} 63 64float XUI_FontData::SChar::getMinX() 65{ 66 return 0.0f; 67} 68 69float XUI_FontData::SChar::getMaxX() 70{ 71 return (float) m_parent->m_fontData->getFontData()->m_uiGlyphWidth; 72} 73 74float XUI_FontData::SChar::getMinY() 75{ 76 return 0.0f; 77} 78 79float XUI_FontData::SChar::getMaxY() 80{ 81 return 0.0f; //m_parent->m_fontData->getFontData()->m_uiGlyphHeight; 82} 83 84float XUI_FontData::SChar::getAdvance() 85{ 86 return (float) m_parent->m_fontData->getWidth(m_glyphId); 87} 88 89int XUI_FontData::SChar::getGlyphId() 90{ 91 return m_glyphId; 92} 93 94#define USE_NEW_UV 1 95 96int XUI_FontData::SChar::tu1() 97{ 98#if USE_NEW_UV 99 int row = 0, col = 0; 100 m_parent->m_fontData->getPos(m_glyphId, row, col); 101 return col * m_parent->m_fontData->getFontData()->m_uiGlyphWidth; 102#else 103 return m_parent->m_Glyphs[m_glyphId].tu1; 104#endif 105} 106 107int XUI_FontData::SChar::tu2() 108{ 109#if USE_NEW_UV 110 return tu1() + m_parent->m_fontData->getFontData()->m_uiGlyphWidth; 111#else 112 return m_parent->m_Glyphs[m_glyphId].tu2; 113#endif 114} 115 116int XUI_FontData::SChar::tv1() 117{ 118#if USE_NEW_UV 119 int row = 0, col = 0; 120 m_parent->m_fontData->getPos(m_glyphId, row, col); 121 return row * m_parent->m_fontData->getFontData()->m_uiGlyphHeight + 1; 122#else 123 return m_parent->m_Glyphs[m_glyphId].tv1; 124#endif 125} 126 127int XUI_FontData::SChar::tv2() 128{ 129#if USE_NEW_UV 130 return tv1() + m_parent->m_fontData->getFontData()->m_uiGlyphHeight; 131#else 132 return m_parent->m_Glyphs[m_glyphId].tv2; 133#endif 134} 135 136short XUI_FontData::SChar::getOffset() 137{ 138 return 0; 139} 140 141short XUI_FontData::SChar::getWAdvance() 142{ 143 return 0; 144} 145 146XUI_FontData::SChar XUI_FontData::getChar(const wchar_t strChar) 147{ 148 SChar out; 149 out.m_glyphId = m_fontData->getGlyphId((unsigned int) strChar); 150 out.m_parent = this; 151 return out; 152} 153 154//-------------------------------------------------------------------------------------- 155// Name: XUI_FontData() 156// Desc: Constructor 157//-------------------------------------------------------------------------------------- 158XUI_FontData::XUI_FontData() 159{ 160 m_pFontTexture = NULL; 161 m_iFontTexture = -1; 162 163 m_dwNumGlyphs = 0L; 164 m_Glyphs = NULL; 165 166 m_cMaxGlyph = 0; 167 168 m_dwNestedBeginCount = 0L; 169} 170 171 172//-------------------------------------------------------------------------------------- 173// Name: ~XUI_FontData() 174// Desc: Destructor 175//-------------------------------------------------------------------------------------- 176XUI_FontData::~XUI_FontData() 177{ 178 Destroy(); 179} 180 181 182//-------------------------------------------------------------------------------------- 183// Name: Create() 184// Desc: Create the font's internal objects (texture and array of glyph info) 185// using the XPR packed resource file 186//-------------------------------------------------------------------------------------- 187HRESULT XUI_FontData::Create( SFontData &sfontdata ) 188{ 189#ifndef _CONTENT_PACKAGE 190 app.DebugPrintf("Attempting to load font data for font: '%s'\n", sfontdata.m_strFontName.c_str()); 191#endif 192 193 BufferedImage *img = new BufferedImage(sfontdata.m_wstrFilename, false, true); 194 195 m_iFontTexture = Minecraft::GetInstance()->textures->getTexture(img, C4JRender::TEXTURE_FORMAT_RxGyBzAw, false); 196 197 int imgWidth = img->getWidth(), imgHeight = img->getHeight(); 198 intArray rawPixels(imgWidth * imgHeight); 199 img->getRGB(0, 0, imgWidth, imgHeight, rawPixels, 0, imgWidth); 200 delete img; 201 202 m_fontData = new CFontData( sfontdata, rawPixels.data ); 203 204 if (rawPixels.data != NULL) delete [] rawPixels.data; 205 206#if 0 207 { // 4J-JEV: Load in FontData (ABC) file, and initialize member variables from it. 208 209 const ULONG_PTR c_ModuleHandle = (ULONG_PTR)GetModuleHandle(NULL); 210 211 //wsprintfW(szResourceLocator,L"section://%X,%s#%s",c_ModuleHandle,L"media", L"media/font/Mojangles_10.abc"); 212 wsprintfW(szResourceLocator,L"section://%X,%s#%s%s%s",c_ModuleHandle,L"media", L"media/font/",strFontFileName.c_str(),L".abc"); 213 214 BYTE *buffer; 215 UINT bufferSize; 216 hr = XuiResourceLoadAllNoLoc( 217 szResourceLocator, 218 &buffer, 219 &bufferSize 220 ); 221 if( FAILED(hr) ) app.FatalLoadError(); 222 223 //return Create( tex, buffer ); 224 hr = Create( m_iFontTexture, buffer ); 225 XuiFree(buffer); 226 } 227 228 // The ABC's are wrong, so recalc 229 // TODO 4J Stu - This isn't going to change every time we run the app, so really the FontMaker tool needs 230 // changed, or at the very least the .abc files need pre-processed to store the values we want 231 int rowV = 0; 232 int rowXOffset = 0; 233 234 235 for (unsigned int i = 0; i < 299; i++) 236 { 237 // Translate unprintable characters 238 GLYPH_ATTR* pGlyph; 239 240 DWORD letter = m_fontData->getGlyphId(i); 241 if( letter == 0 || letter >= 280 ) continue; 242 pGlyph = (GLYPH_ATTR*)&m_Glyphs[letter]; // Get the requested glyph 243 244 // 4J Stu - The original ABC's were generated for a font height that is 1 pixel higher than our cleaned up version 245 // We adjust for 1 pixel padding in the y at the top of each box. 246 pGlyph->tv1++; 247 248 if( pGlyph->tv1 != rowV ) 249 { 250 rowV = pGlyph->tv1; 251 rowXOffset = 0; 252 } 253 if( pGlyph->wOffset > 0 ) 254 { 255 rowXOffset += pGlyph->wOffset; 256 pGlyph->wOffset = 0; 257 } 258 259 pGlyph->tu1 -= rowXOffset; 260 pGlyph->tu2 -= rowXOffset; 261 262 int x = pGlyph->tu2-1; 263 int emptyColumnX = x; 264 for (; x >= pGlyph->tu1; x--) 265 { 266 bool emptyColumn = true; 267 for (int y = pGlyph->tv1; y < pGlyph->tv2; y++) 268 { 269 int rawPix = rawPixels[x + (y*imgWidth)]; 270 DWORD pixel = rawPixels[x + (y*imgWidth)] & 0xff000000; 271 if (pixel > 0) emptyColumn = false; 272 } 273 274 if (!emptyColumn && emptyColumnX == pGlyph->tu2-1) 275 { 276 emptyColumnX = x; 277 } 278 } 279 if(emptyColumnX != pGlyph->tu2-1) 280 { 281 pGlyph->wWidth = emptyColumnX-pGlyph->tu1; 282 pGlyph->wAdvance = pGlyph->wWidth + 1; 283 } 284 } 285#endif 286 287 return S_OK; 288} 289 290 291//-------------------------------------------------------------------------------------- 292// Name: Create() 293// Desc: Create the font's internal objects (texture and array of glyph info) 294//-------------------------------------------------------------------------------------- 295//HRESULT XUI_FontData::Create( D3DTexture* pFontTexture, const VOID* pFontData ) 296HRESULT XUI_FontData::Create( int iFontTexture, const VOID* pFontData ) 297{ 298 // Save a copy of the texture 299 //m_pFontTexture = pFontTexture; 300#if 0 301 m_iFontTexture = iFontTexture; 302 303 // Check version of file (to make sure it matches up with the FontMaker tool) 304 const BYTE* pData = static_cast<const BYTE*>(pFontData); 305 DWORD dwFileVersion = reinterpret_cast<const FontFileHeaderImage_t *>(pData)->m_dwFileVersion; 306 307 if( dwFileVersion == ATGFONTFILEVERSION ) 308 { 309 //m_fFontHeight = reinterpret_cast<const FontFileHeaderImage_t *>(pData)->m_fFontHeight; 310 //m_fFontTopPadding = reinterpret_cast<const FontFileHeaderImage_t *>(pData)->m_fFontTopPadding; 311 //m_fFontBottomPadding = reinterpret_cast<const FontFileHeaderImage_t *>(pData)->m_fFontBottomPadding; 312 //m_fFontYAdvance = reinterpret_cast<const FontFileHeaderImage_t *>(pData)->m_fFontYAdvance; 313 314 // Point to the translator string which immediately follows the 4 floats 315 m_cMaxGlyph = reinterpret_cast<const FontFileHeaderImage_t *>(pData)->m_cMaxGlyph; 316 317 WCHAR* translatorTable = const_cast<FontFileHeaderImage_t*>(reinterpret_cast<const FontFileHeaderImage_t *>(pData))->m_TranslatorTable; 318 319 // 4J Stu - This map saves us some memory because the translatorTable is largely empty 320 // If we were ever to use >50% of the table then we should store it and use directly rather than the map 321 for( unsigned short i = 0; i < m_cMaxGlyph + 1; ++i ) 322 { 323 if( translatorTable[i] == 0 ) continue; 324 m_TranslatorMap.insert( unordered_map<wchar_t, unsigned short>::value_type(i, translatorTable[i]) ); 325 } 326 327 pData += ATGCALCFONTFILEHEADERSIZE( m_cMaxGlyph + 1 ); 328 329 // Read the glyph attributes from the file 330 m_dwNumGlyphs = reinterpret_cast<const FontFileStrikesImage_t *>(pData)->m_dwNumGlyphs; 331 m_Glyphs = new GLYPH_ATTR[m_dwNumGlyphs]; 332 memcpy(m_Glyphs, reinterpret_cast<const FontFileStrikesImage_t *>(pData)->m_Glyphs, sizeof(GLYPH_ATTR) * m_dwNumGlyphs); 333 334 //m_dwNumGlyphs = m_fontData->getFontData()->m_uiGlyphCount; 335 } 336 else 337 { 338 app.DebugPrintf( "Incorrect version number on font file!\n" ); 339 return E_FAIL; 340 } 341#endif 342 return S_OK; 343} 344 345 346//-------------------------------------------------------------------------------------- 347// Name: Destroy() 348// Desc: Destroy the font object 349//-------------------------------------------------------------------------------------- 350VOID XUI_FontData::Destroy() 351{ 352 if(m_pFontTexture!=NULL) 353 { 354 m_pFontTexture->Release(); 355 delete m_pFontTexture; 356 } 357 358 m_fontData->release(); 359 360 m_pFontTexture = NULL; 361 m_dwNumGlyphs = 0L; 362 m_cMaxGlyph = 0; 363 364 m_dwNestedBeginCount = 0L; 365} 366 367/* 368FLOAT XUI_FontData::GetCharAdvance( const WCHAR* strChar ) 369{ 370 unsigned int uiChar = (unsigned int) *strChar; 371 return 0.0f;// m_fontData.getAdvance(m_fontData.getGlyphId(uiChar)); 372} 373 374FLOAT XUI_FontData::GetCharWidth( const WCHAR* strChar ) 375{ 376 return 0.0f; 377} 378 379void XUI_FontData::GetCharMetrics( const WCHAR* strChar, XUICharMetrics *xuiMetrics) 380{ 381 unsigned int uiChar = (unsigned int) *strChar; 382 unsigned short usGlyph = m_fontData->getGlyphId(uiChar); 383 384 xuiMetrics->fAdvance = m_fontData->getWidth(usGlyph); //.getAdvance(usGlyph) * (float) m_fontData.getFontData()->m_uiGlyphHeight; 385 xuiMetrics->fMaxX = (float) m_fontData->getFontData()->m_uiGlyphWidth; 386 xuiMetrics->fMinX = 0.0f; 387 xuiMetrics->fMaxY = 0;// m_fontData.getFontData()->m_fAscent * (float) m_fontData.getFontData()->m_uiGlyphHeight; 388 xuiMetrics->fMinY = 0;//m_fontData.getFontData()->m_fDescent * (float) m_fontData.getFontData()->m_uiGlyphHeight; 389} 390 391unsigned short XUI_FontData::getGlyphId(wchar_t character) 392 { 393 return m_fontData->getGlyphId( (unsigned int) character ); 394} 395*/