the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 400 lines 8.3 kB view raw
1#include "stdafx.h" 2#include "..\Minecraft.World\StringHelpers.h" 3#include "Textures.h" 4#include "..\Minecraft.World\ArrayWithLength.h" 5#include "BufferedImage.h" 6 7#ifdef _XBOX 8typedef struct 9{ 10 unsigned int filesz; 11 unsigned short creator1; 12 unsigned short creator2; 13 unsigned int bmp_offset; 14 unsigned int header_sz; 15 unsigned int width; 16 unsigned int height; 17 unsigned short nplanes; 18 unsigned short bitspp; 19 unsigned int compress_type; 20 unsigned int bmp_bytesz; 21 int hres; 22 int vres; 23 unsigned int ncolors; 24 unsigned int nimpcolors; 25} BITMAPINFOHEADER; 26#endif 27 28BufferedImage::BufferedImage(int width,int height,int type) 29{ 30 data[0] = new int[width*height]; 31 32 for( int i = 1 ; i < 10; i++ ) 33 { 34 data[i] = NULL; 35 } 36 this->width = width; 37 this->height = height; 38} 39 40void BufferedImage::ByteFlip4(unsigned int &data) 41{ 42 data = ( data >> 24 ) | 43 ( ( data >> 8 ) & 0x0000ff00 ) | 44 ( ( data << 8 ) & 0x00ff0000 ) | 45 ( data << 24 ); 46} 47// Loads a bitmap into a buffered image - only currently supports the 2 types of 32-bit image that we've made so far 48// and determines which of these is which by the compression method. Compression method 3 is a 32-bit image with only 49// 24-bits used (ie no alpha channel) whereas method 0 is a full 32-bit image with a valid alpha channel. 50BufferedImage::BufferedImage(const wstring& File, bool filenameHasExtension /*=false*/, bool bTitleUpdateTexture /*=false*/, const wstring &drive /*=L""*/) 51{ 52 HRESULT hr; 53 wstring wDrive; 54 wstring filePath; 55 filePath = File; 56 57 wDrive = drive; 58 if(wDrive.empty()) 59 { 60#ifdef _XBOX 61 if(bTitleUpdateTexture) 62 { 63 // Make the content package point to to the UPDATE: drive is needed 64#ifdef _TU_BUILD 65 wDrive=L"UPDATE:\\"; 66#else 67 68 wDrive=L"GAME:\\res\\TitleUpdate\\"; 69#endif 70 } 71 else 72 { 73 wDrive=L"GAME:\\"; 74 } 75#else 76 77#ifdef __PS3__ 78 79 char *pchUsrDir; 80 if(app.GetBootedFromDiscPatch()) 81 { 82 const char *pchTextureName=wstringtofilename(File); 83 pchUsrDir = app.GetBDUsrDirPath(pchTextureName); 84 } 85 else 86 { 87 pchUsrDir=getUsrDirPath(); 88 } 89 90 wstring wstr (pchUsrDir, pchUsrDir+strlen(pchUsrDir)); 91 92 if(bTitleUpdateTexture) 93 { 94 // Make the content package point to to the UPDATE: drive is needed 95 wDrive= wstr + L"\\Common\\res\\TitleUpdate\\"; 96 } 97 else 98 { 99 wDrive= wstr + L"/Common/"; 100 } 101#elif __PSVITA__ 102 103 /*char *pchUsrDir=getUsrDirPath(); 104 105 wstring wstr (pchUsrDir, pchUsrDir+strlen(pchUsrDir)); 106 107 if(bTitleUpdateTexture) 108 { 109 // Make the content package point to to the UPDATE: drive is needed 110 wDrive= wstr + L"\\Common\\res\\TitleUpdate\\"; 111 } 112 else 113 { 114 wDrive= wstr + L"/Common/"; 115 }*/ 116 117 if(bTitleUpdateTexture) 118 { 119 // Make the content package point to to the UPDATE: drive is needed 120 wDrive= L"Common\\res\\TitleUpdate\\"; 121 } 122 else 123 { 124 wDrive= L"Common/"; 125 } 126#else 127 if(bTitleUpdateTexture) 128 { 129 // Make the content package point to to the UPDATE: drive is needed 130 wDrive= L"Common\\res\\TitleUpdate\\"; 131 } 132 else 133 { 134 wDrive= L"Common/"; 135 } 136#endif 137 138#endif 139 } 140 141 for( int l = 0 ; l < 10; l++ ) 142 { 143 data[l] = NULL; 144 } 145 146 for( int l = 0; l < 10; l++ ) 147 { 148 wstring name; 149 wstring mipMapPath = L""; 150 if( l != 0 ) 151 { 152 mipMapPath = L"MipMapLevel" + _toString<int>(l+1); 153 } 154 if( filenameHasExtension ) 155 { 156 name = wDrive + L"res" + filePath.substr(0,filePath.length()); 157 } 158 else 159 { 160 name = wDrive + L"res" + filePath.substr(0,filePath.length()-4) + mipMapPath + L".png"; 161 } 162 163 const char *pchTextureName=wstringtofilename(name); 164 165#ifndef _CONTENT_PACKAGE 166 app.DebugPrintf("\n--- Loading TEXTURE - %s\n\n",pchTextureName); 167#endif 168 169 D3DXIMAGE_INFO ImageInfo; 170 ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO)); 171 hr=RenderManager.LoadTextureData(pchTextureName,&ImageInfo,&data[l]); 172 173 174 if(hr!=ERROR_SUCCESS) 175 { 176 // 4J - If we haven't loaded the non-mipmap version then exit the game 177 if( l == 0 ) 178 { 179 app.FatalLoadError(); 180 } 181 return; 182 } 183 184 if( l == 0 ) 185 { 186 width=ImageInfo.Width; 187 height=ImageInfo.Height; 188 } 189 } 190} 191 192BufferedImage::BufferedImage(DLCPack *dlcPack, const wstring& File, bool filenameHasExtension /*= false*/ ) 193{ 194 HRESULT hr; 195 wstring filePath = File; 196 BYTE *pbData = NULL; 197 DWORD dwBytes = 0; 198 199 for( int l = 0 ; l < 10; l++ ) 200 { 201 data[l] = NULL; 202 } 203 204 for( int l = 0; l < 10; l++ ) 205 { 206 wstring name; 207 wstring mipMapPath = L""; 208 if( l != 0 ) 209 { 210 mipMapPath = L"MipMapLevel" + _toString<int>(l+1); 211 } 212 if( filenameHasExtension ) 213 { 214 name = L"res" + filePath.substr(0,filePath.length()); 215 } 216 else 217 { 218 name = L"res" + filePath.substr(0,filePath.length()-4) + mipMapPath + L".png"; 219 } 220 221 if(!dlcPack->doesPackContainFile(DLCManager::e_DLCType_All, name)) 222 { 223 // 4J - If we haven't loaded the non-mipmap version then exit the game 224 if( l == 0 ) 225 { 226 app.FatalLoadError(); 227 } 228 return; 229 } 230 231 DLCFile *dlcFile = dlcPack->getFile(DLCManager::e_DLCType_All, name); 232 pbData = dlcFile->getData(dwBytes); 233 if(pbData == NULL || dwBytes == 0) 234 { 235 // 4J - If we haven't loaded the non-mipmap version then exit the game 236 if( l == 0 ) 237 { 238 app.FatalLoadError(); 239 } 240 return; 241 } 242 243 D3DXIMAGE_INFO ImageInfo; 244 ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO)); 245 hr=RenderManager.LoadTextureData(pbData,dwBytes,&ImageInfo,&data[l]); 246 247 248 if(hr!=ERROR_SUCCESS) 249 { 250 // 4J - If we haven't loaded the non-mipmap version then exit the game 251 if( l == 0 ) 252 { 253 app.FatalLoadError(); 254 } 255 return; 256 } 257 258 if( l == 0 ) 259 { 260 width=ImageInfo.Width; 261 height=ImageInfo.Height; 262 } 263 } 264} 265 266 267BufferedImage::BufferedImage(BYTE *pbData, DWORD dwBytes) 268{ 269 int iCurrentByte=0; 270 for( int l = 0 ; l < 10; l++ ) 271 { 272 data[l] = NULL; 273 } 274 275 D3DXIMAGE_INFO ImageInfo; 276 ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO)); 277 HRESULT hr=RenderManager.LoadTextureData(pbData,dwBytes,&ImageInfo,&data[0]); 278 279 if(hr==ERROR_SUCCESS) 280 { 281 width=ImageInfo.Width; 282 height=ImageInfo.Height; 283 } 284 else 285 { 286 app.FatalLoadError(); 287 } 288} 289 290BufferedImage::~BufferedImage() 291{ 292 for(int i = 0; i < 10; i++ ) 293 { 294 delete[] data[i]; 295 } 296} 297 298int BufferedImage::getWidth() 299{ 300 return width; 301} 302 303int BufferedImage::getHeight() 304{ 305 return height; 306} 307 308void BufferedImage::getRGB(int startX, int startY, int w, int h, intArray out,int offset,int scansize, int level) 309{ 310 int ww = width >> level; 311 for( int y = 0; y < h; y++ ) 312 { 313 for( int x = 0; x < w; x++ ) 314 { 315 out[ y * scansize + offset + x] = data[level][ startX + x + ww * ( startY + y ) ]; 316 } 317 } 318} 319 320int *BufferedImage::getData() 321{ 322 return data[0]; 323} 324 325int *BufferedImage::getData(int level) 326{ 327 return data[level]; 328} 329 330Graphics *BufferedImage::getGraphics() 331{ 332 return NULL; 333} 334 335//Returns the transparency. Returns either OPAQUE, BITMASK, or TRANSLUCENT. 336//Specified by: 337//getTransparency in interface Transparency 338//Returns: 339//the transparency of this BufferedImage. 340int BufferedImage::getTransparency() 341{ 342 // TODO - 4J Implement? 343 return 0; 344} 345 346//Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image. 347//Parameters: 348//x, y - the coordinates of the upper-left corner of the specified rectangular region 349//w - the width of the specified rectangular region 350//h - the height of the specified rectangular region 351//Returns: 352//a BufferedImage that is the subimage of this BufferedImage. 353BufferedImage *BufferedImage::getSubimage(int x ,int y, int w, int h) 354{ 355 // TODO - 4J Implement 356 357 BufferedImage *img = new BufferedImage(w,h,0); 358 intArray arrayWrapper(img->data[0], w*h); 359 this->getRGB(x, y, w, h, arrayWrapper,0,w); 360 361 int level = 1; 362 while(getData(level) != NULL) 363 { 364 int ww = w >> level; 365 int hh = h >> level; 366 int xx = x >> level; 367 int yy = y >> level; 368 img->data[level] = new int[ww*hh]; 369 intArray arrayWrapper(img->data[level], ww*hh); 370 this->getRGB(xx, yy, ww, hh, arrayWrapper,0,ww,level); 371 372 ++level; 373 } 374 375 return img; 376} 377 378 379void BufferedImage::preMultiplyAlpha() 380{ 381 int *curData = data[0]; 382 383 int cur = 0; 384 int alpha = 0; 385 int r = 0; 386 int g = 0; 387 int b = 0; 388 389 int total = width * height; 390 for(unsigned int i = 0; i < total; ++i) 391 { 392 cur = curData[i]; 393 alpha = (cur >> 24) & 0xff; 394 r = ((cur >> 16) & 0xff) * (float)alpha/255; 395 g = ((cur >> 8) & 0xff) * (float)alpha/255; 396 b = (cur & 0xff) * (float)alpha/255; 397 398 curData[i] = (r << 16) | (g << 8) | (b ) | (alpha << 24); 399 } 400}