texture.cpp
上传用户:garry_shen
上传日期:2015-04-15
资源大小:45647k
文件大小:16k
- #include "stdafx.h"
- #include "XMudClient.h"
- #include "d3dappi.h"
- #define MAGICBYTES 2
- void D3DAppIAddPathList(const char *path);
- void D3DAppIInitialisePathList();
- FILE * D3DAppIFindFile(const char *name, const char *mode);
- BOOL loadPPMHeader(FILE *fp, DWORD *width, DWORD *height, int *maxgrey);
- BOOL D3DAppILoadTextureSurf(int n, BOOL* bInVideo)
- {
- DDSURFACEDESC ddsd;
- LPDIRECTDRAWSURFACE lpSrcTextureSurf = NULL;
- LPDIRECT3DTEXTURE2 lpSrcTexture = NULL;
- LPDIRECTDRAWPALETTE lpDstPalette = NULL;
- PALETTEENTRY ppe[256];
- DWORD pcaps;
- RELEASE(d3dappi.lpTextureSurf[n]);
- lpSrcTextureSurf = D3DAppILoadSurface(d3dappi.lpDD, d3dappi.ImageFile[n],
- &d3dappi.ThisTextureFormat.ddsd,
- DDSCAPS_SYSTEMMEMORY);
- if (!lpSrcTextureSurf)
- goto exit_with_error;
- LastError = lpSrcTextureSurf->QueryInterface(IID_IDirect3DTexture2,
- (LPVOID*)&lpSrcTexture);
- if (LastError != DD_OK)
- {
- D3DAppISetErrorString("Failed to obtain D3D texture interface for a source texture.n%s", D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- LastError = D3DAppIGetSurfDesc(&ddsd, lpSrcTextureSurf);
- if (LastError != DD_OK) {
- D3DAppISetErrorString("Could not get the surface description of the source texture.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
- ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD;
- if (!d3dappi.ThisDriver.bIsHardware)
- ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
- LastError = D3DAppICreateSurface(&ddsd, &d3dappi.lpTextureSurf[n]);
- if (LastError != DD_OK) {
- D3DAppISetErrorString("Could not create the destination texture surface.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
- pcaps = DDPCAPS_8BIT | DDPCAPS_ALLOW256;
- } else if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED4) {
- pcaps = DDPCAPS_4BIT;
- } else {
- pcaps = 0;
- }
- if (pcaps) {
- memset(ppe, 0, sizeof(PALETTEENTRY) * 256);
- LastError = d3dappi.lpDD->CreatePalette(pcaps,
- ppe, &lpDstPalette, NULL);
- if (LastError != DD_OK)
- {
- D3DAppISetErrorString("Failed to create a palette for the destination texture.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- LastError = d3dappi.lpTextureSurf[n]->SetPalette(lpDstPalette);
- if (LastError != DD_OK)
- {
- D3DAppISetErrorString("Failed to set the destination texture's palette.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- lpDstPalette->Release( );
- }
- LastError = d3dappi.lpTextureSurf[n]->QueryInterface(IID_IDirect3DTexture2,
- (LPVOID*)&d3dappi.lpTexture[n]);
- if (LastError != DD_OK){
- D3DAppISetErrorString("Failed to obtain D3D texture interface for a destination texture.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- LastError = d3dappi.lpTexture[n]->Load(lpSrcTexture);
- if (LastError != DD_OK)
- {
- D3DAppISetErrorString("Could not load a source texture into a destination texture.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- RELEASE(lpSrcTexture);
- RELEASE(lpSrcTextureSurf);
- LastError = D3DAppIGetSurfDesc(&ddsd, d3dappi.lpTextureSurf[n]);
- if (LastError != DD_OK){
- D3DAppISetErrorString("Could not get the surface description of the loaded texture surface.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- if (ddsd.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
- *bInVideo = TRUE;
- else
- *bInVideo = FALSE;
-
-
- return TRUE;
-
- exit_with_error:
- RELEASE(lpSrcTexture);
- RELEASE(lpSrcTextureSurf);
- RELEASE(lpDstPalette);
- RELEASE(d3dappi.lpTexture[n]);
- RELEASE(d3dappi.lpTextureSurf[n]);
- return FALSE;
- }
- BOOL D3DAppIReloadTextureSurf(int n)
- {
- LPDIRECTDRAWSURFACE lpSrcTextureSurf = NULL;
- LPDIRECT3DTEXTURE2 lpSrcTexture = NULL;
-
- lpSrcTextureSurf = D3DAppILoadSurface(d3dappi.lpDD, d3dappi.ImageFile[n],
- &d3dappi.ThisTextureFormat.ddsd,
- DDSCAPS_SYSTEMMEMORY);
- if (!lpSrcTextureSurf)
- goto exit_with_error;
- LastError = lpSrcTextureSurf->QueryInterface(IID_IDirect3DTexture2,
- (LPVOID*)&lpSrcTexture);
- if (LastError != DD_OK) {
- D3DAppISetErrorString("Failed to obtain D3D texture interface for a source texture.n%s", D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- LastError = d3dappi.lpTexture[n]->Load(lpSrcTexture);
- if (LastError != DD_OK)
- {
- D3DAppISetErrorString("Could not load a source texture into a destination texture.n%s",
- D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- RELEASE(lpSrcTexture);
- RELEASE(lpSrcTextureSurf);
-
- return TRUE;
-
- exit_with_error:
- RELEASE(lpSrcTexture);
- RELEASE(lpSrcTextureSurf);
- return FALSE;
- }
- BOOL D3DAppIGetTextureHandle(int n)
- {
- LastError = d3dappi.lpTexture[n]->GetHandle(d3dappi.lpD3DDevice, &MasterTextureHandle[n]);
- if (LastError != DD_OK) {
- D3DAppISetErrorString("Could not get a handle to loaded texture %i.n%s",
- n, D3DAppErrorToString(LastError));
- goto exit_with_error;
- }
- if (!d3dappi.bTexturesDisabled){
- d3dappi.TextureHandle[n] = MasterTextureHandle[n];
- }else{
- d3dappi.TextureHandle[n] = 0;
- }
- return TRUE;
- exit_with_error:
- MasterTextureHandle[n] = 0;
- d3dappi.TextureHandle[n] = 0;
- return FALSE;
- }
- void
- D3DAppIReleaseTexture(int n)
- {
- RELEASE(d3dappi.lpTexture[n]);
- RELEASE(d3dappi.lpTextureSurf[n]);
- MasterTextureHandle[n] = 0;
- d3dappi.TextureHandle[n] = 0;
- }
- void
- D3DAppIReleaseAllTextures(void)
- {
- int i;
- for (i = 0; i < d3dappi.NumTextures; i++) {
- D3DAppIReleaseTexture(i);
- }
- }
- BOOL D3DAppILoadAllTextures(void)
- {
- int i;
- if (d3dappi.ThisDriver.bDoesTextures)
- {
- d3dappi.NumUsableTextures = 0;
- for (i = 0; i < d3dappi.NumTextures; i++)
- {
- BOOL bInVideo;
- ATTEMPT(D3DAppILoadTextureSurf(i, &bInVideo));
- if (!bInVideo && d3dappi.ThisDriver.bIsHardware)
- {
- D3DAppIReleaseTexture(i);
- break;
- }
- else
- {
- ++d3dappi.NumUsableTextures;
- }
- }
- for (i = 0; i < d3dappi.NumUsableTextures; i++)
- {
- ATTEMPT(D3DAppIGetTextureHandle(i));
- }
- }
- else
- {
- d3dappi.NumUsableTextures = 0;
- }
- return TRUE;
-
- exit_with_error:
- for (i = 0; i < d3dappi.NumTextures; i++)
- {
- D3DAppIReleaseTexture(i);
- }
- return FALSE;
- }
- LPDIRECTDRAWSURFACE D3DAppILoadSurface(LPDIRECTDRAW lpDD, LPCSTR lpName,
- LPDDSURFACEDESC lpFormat, DWORD memoryflag)
- {
- LPDIRECTDRAWSURFACE lpDDS;
- DDSURFACEDESC ddsd, format;
- D3DCOLOR colors[256];
- D3DCOLOR c;
- DWORD dwWidth, dwHeight;
- int i, j;
- FILE *fp;
- char *lpC;
- LPDIRECTDRAWPALETTE lpDDPal;
- PALETTEENTRY ppe[256];
- int psize;
- DWORD pcaps;
- int color_count;
- BOOL bQuant = FALSE;
- HRESULT ddrval;
- fp = D3DAppIFindFile(lpName, "rb");
- if (fp == NULL)
- {
- D3DAppISetErrorString("Cannot find %s.n", lpName);
- return NULL;
- }
- if (!loadPPMHeader(fp, &dwWidth, &dwHeight, &i))
- {
- fclose(fp);
- D3DAppISetErrorString("Could not load or parse PPM header in %s.n", lpName);
- return NULL;
- }
- memcpy(&format, lpFormat, sizeof(DDSURFACEDESC));
- if (format.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8)
- {
- bQuant = TRUE;
- psize = 256;
- pcaps = DDPCAPS_8BIT | DDPCAPS_ALLOW256;
- } else if (format.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED4)
- {
- bQuant = TRUE;
- psize = 16;
- pcaps = DDPCAPS_4BIT;
- }
- memcpy(&ddsd, &format, sizeof(ddsd));
- ddsd.dwSize = sizeof(ddsd);
- ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
- ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | memoryflag;
- ddsd.dwHeight = dwHeight;
- ddsd.dwWidth = dwWidth;
-
- ddrval = lpDD->CreateSurface(&ddsd, &lpDDS, NULL);
- if (ddrval != DD_OK) {
- D3DAppISetErrorString("CreateSurface for texture failed (loadtex).n%s",
- D3DAppErrorToString(ddrval));
- return NULL;
- }
- memset(&ddsd, 0, sizeof(ddsd));
- ddsd.dwSize = sizeof(ddsd);
- ddrval = lpDDS->Lock(NULL, &ddsd, 0, NULL);
- if (ddrval != DD_OK)
- {
- lpDDS->Release( );
- D3DAppISetErrorString("Lock failed while loading surface (loadtex).n%s",
- D3DAppErrorToString(ddrval));
- return NULL;
- }
- if (!bQuant)
- {
- unsigned long* lpLP;
- unsigned short* lpSP;
- unsigned char* lpCP;
- unsigned long m;
- int s;
- int red_shift, red_scale;
- int green_shift, green_scale;
- int blue_shift, blue_scale;
- for (s = 0, m = format.ddpfPixelFormat.dwRBitMask; !(m & 1);
- s++, m >>= 1);
- red_shift = s;
- red_scale = 255 / (format.ddpfPixelFormat.dwRBitMask >> s);
- for (s = 0, m = format.ddpfPixelFormat.dwGBitMask; !(m & 1);
- s++, m >>= 1);
- green_shift = s;
- green_scale = 255 / (format.ddpfPixelFormat.dwGBitMask >> s);
- for (s = 0, m = format.ddpfPixelFormat.dwBBitMask; !(m & 1);
- s++, m >>= 1);
- blue_shift = s;
- blue_scale = 255 / (format.ddpfPixelFormat.dwBBitMask >> s);
- switch (format.ddpfPixelFormat.dwRGBBitCount)
- {
- case 32 :
- for (j = 0; j < (int)dwHeight; j++)
- {
- lpLP = (unsigned long*)(((char*)ddsd.lpSurface) +
- ddsd.lPitch * j);
- for (i = 0; i < (int)dwWidth; i++)
- {
- int r, g, b;
- r = getc(fp) / red_scale;
- g = getc(fp) / green_scale;
- b = getc(fp) / blue_scale;
- *lpLP = (r << red_shift) | (g << green_shift) |
- (b << blue_shift);
- lpLP++;
- }
- }
- break;
- case 16 :
- for (j = 0; j < (int)dwHeight; j++) {
- lpSP = (unsigned short*)(((char*)ddsd.lpSurface) +
- ddsd.lPitch * j);
- for (i = 0; i < (int)dwWidth; i++) {
- int r, g, b;
- r = getc(fp) / red_scale;
- g = getc(fp) / green_scale;
- b = getc(fp) / blue_scale;
- *lpSP = (r << red_shift) | (g << green_shift) |
- (b << blue_shift);
- lpSP++;
- }
- }
- break;
- case 8:
- for (j = 0; j < (int)dwHeight; j++) {
- lpCP = (unsigned char*)(((char*)ddsd.lpSurface) +
- ddsd.lPitch * j);
- for (i = 0; i < (int)dwWidth; i++) {
- int r, g, b;
- r = getc(fp) / red_scale;
- g = getc(fp) / green_scale;
- b = getc(fp) / blue_scale;
- *lpCP = (r << red_shift) | (g << green_shift) |
- (b << blue_shift);
- lpCP++;
- }
- }
- break;
- default:
- lpDDS->Unlock(NULL);
- fclose(fp);
- lpDDS->Release( );
- D3DAppISetErrorString("Unknown pixel format (loadtex).");
- return NULL;
- }
- lpDDS->Unlock( NULL);
- fclose(fp);
- return (lpDDS);
- }
- color_count = 0;
- for (j = 0; j < (int)dwHeight; j++) {
- lpC = ((char*)ddsd.lpSurface) + ddsd.lPitch * j;
- for (i = 0; i < (int)dwWidth; i++)
- {
- int r, g, b, k;
- r = getc(fp);
- g = getc(fp);
- b = getc(fp);
- c = RGB_MAKE(r, g, b);
- for (k = 0; k < color_count; k++)
- if (c == colors[k]) break;
- if (k == color_count) {
- color_count++;
- if (color_count > psize) {
- color_count--;
- k = color_count - 1;
- }
- colors[k] = c;
- }
- if (psize == 16) {
- if ((i & 1) == 0)
- *lpC = k & 0xf;
- else {
- *lpC |= (k & 0xf) << 4;
- lpC++;
- }
- } else {
- *lpC = (char)k;
- lpC++;
- }
- }
- }
- fclose(fp);
- lpDDS->Unlock(NULL);
-
- if (color_count > psize) {
- lpDDS->Unlock(NULL);
- lpDDS->Release( );
- D3DAppISetErrorString("Palette burst. (loadtex).n");
- return (NULL);
- }
-
- memset(ppe, 0, sizeof(PALETTEENTRY) * 256);
- for (i = 0; i < color_count; i++) {
- ppe[i].peRed = (unsigned char)RGB_GETRED(colors[i]);
- ppe[i].peGreen = (unsigned char)RGB_GETGREEN(colors[i]);
- ppe[i].peBlue = (unsigned char)RGB_GETBLUE(colors[i]);
- }
- for (; i < 256; i++)
- ppe[i].peFlags = D3DPAL_RESERVED;
- ddrval = lpDD->CreatePalette(DDPCAPS_INITIALIZE | pcaps,
- ppe, &lpDDPal, NULL);
- if (ddrval != DD_OK){
- lpDDS->Release( );
- D3DAppISetErrorString("Create palette failed while loading surface (loadtex).n%s",
- D3DAppErrorToString(ddrval));
- return (NULL);
- }
- ddrval = lpDDS->SetPalette( lpDDPal);
- if (ddrval != DD_OK){
- lpDDS->Release( );
- lpDDPal->Release( );
- D3DAppISetErrorString("SetPalette failed while loading surface (loadtex).n%s",
- D3DAppErrorToString(ddrval));
- return (NULL);
- }
-
- lpDDPal->Release( );
-
- return lpDDS;
- }
- BOOL ppm_getbyte(FILE *fp, char *newByte)
- {
- char cchar;
- int cc;
- cchar = cc = getc(fp);
-
- if (cc == EOF) {
- return FALSE;
- }
-
- if (cchar == '#') {
- do {
- cchar = cc = getc(fp);
- if (cc == EOF)
- return FALSE;
- } while (cchar != 'n' && cchar != 'r');
- }
-
- *newByte = cchar;
-
- return TRUE;
- }
- BOOL ppm_getint(FILE *fp, DWORD *newInt)
- {
- DWORD cint;
- char cchar;
-
- do {
- if (!ppm_getbyte(fp, &cchar)) return FALSE;
- } while (isspace(cchar));
-
- if (!isdigit(cchar)) {
- return FALSE;
- }
-
- cint = 0;
-
- do {
- cint = (cint * 10) + (cchar - '0');
- if (!ppm_getbyte(fp, &cchar)) return FALSE;
- } while(isdigit(cchar));
-
- *newInt = cint;
-
- return TRUE;
- }
- BOOL loadPPMHeader(FILE *fp, DWORD *width, DWORD *height, int *maxgrey)
- {
- char magic[MAGICBYTES], cchar;
- if (fread(magic, MAGICBYTES, 1, fp) != 1)
- return FALSE;
- if (magic[0] != 'P' || magic[1] != '6')
- return FALSE;
- if (!ppm_getint(fp, width))
- return FALSE;
- if (!ppm_getint(fp, height))
- return FALSE;
- if (!ppm_getint(fp, (DWORD * )maxgrey))
- return FALSE;
- do {
- if (!ppm_getbyte(fp, &cchar))
- return FALSE;
- } while (cchar == ' ' || cchar == 't' || cchar == 'n' || cchar == 'r');
-
- fseek(fp, -1, SEEK_CUR);
-
- return TRUE;
- }
- #define MAXPATH 256
- #define PATHSEP ';'
- #define FILESEP '\'
- #define MAXCONTENTS 25
- #define RESPATH "Software\Microsoft\Direct3D"
- int PathListInitialised = FALSE;
- static struct {
- int count;
- char *contents[MAXCONTENTS];
- } PathList;
- void D3DAppIAddPathList(const char *path)
- {
- char *p;
- char *elt;
- int len;
-
- while (path) {
- p = strchr(path, PATHSEP);
- if (p)
- len = p - path;
- else
- len = lstrlen(path);
- elt = (char *) malloc(len + 1);
- if (elt == NULL)
- return;
- lstrcpyn(elt, path, len + 1);
- elt[len] = ' ';
- PathList.contents[PathList.count] = elt;
- PathList.count++;
- if (p)
- path = p + 1;
- else
- path = NULL;
- if (PathList.count == MAXCONTENTS)
- return;
- }
- return;
- }
- void D3DAppIInitialisePathList()
- {
- long result;
- HKEY key;
- DWORD type, size;
- static char buf[512];
- char* path;
-
- if (PathListInitialised)
- return;
- PathListInitialised = TRUE;
-
- PathList.count = 0;
- path = getenv("D3DPATH");
- D3DAppIAddPathList(".");
- if (path != NULL) {
- D3DAppIAddPathList(path);
- return;
- }
- result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, RESPATH, 0, KEY_READ, &key);
- if (result == ERROR_SUCCESS) {
- size = sizeof(buf);
- result = RegQueryValueEx(key, "D3D Path", NULL, &type, (LPBYTE) buf,
- &size);
- RegCloseKey(key);
- if (result == ERROR_SUCCESS && type == REG_SZ)
- D3DAppIAddPathList(buf);
- }
- }
- FILE* D3DAppIFindFile(const char *name, const char *mode)
- {
- FILE *fp;
- char buf[MAXPATH];
- static char filesep[] = {FILESEP, 0};
- int i;
-
- D3DAppIInitialisePathList();
-
- fp = fopen(name, mode);
- if (fp != NULL)
- return fp;
-
- for (i = 0; i < PathList.count; i++) {
- lstrcpy(buf, PathList.contents[i]);
- lstrcat(buf, filesep);
- lstrcat(buf, name);
- fp = fopen(buf, mode);
- if (fp)
- return fp;
- }
- return NULL;
- }
- void
- D3DAppIReleasePathList(void)
- {
- int i;
- for (i = 0; i < PathList.count; i++) {
- free(PathList.contents[i]);
- PathList.contents[i] = NULL;
- }
- PathList.count = 0;
- PathListInitialised = FALSE;
- }