From: ShadowFlare Date: Wed, 29 Mar 2006 21:00:09 +0000 (+0000) Subject: (no commit message) X-Git-Url: https://sfsrealm.hopto.org/projects/?a=commitdiff_plain;h=ecca31ff6c588a09592e9a0ca08180828ab8a5a7;p=grpapi.git --- diff --git a/LoadStorm/Storm.cpp b/LoadStorm/Storm.cpp index 264914d..c8e6366 100644 --- a/LoadStorm/Storm.cpp +++ b/LoadStorm/Storm.cpp @@ -9,6 +9,8 @@ struct STORMMODULE { ~STORMMODULE(); } Storm; +void FreeStorm(); + HINSTANCE hStorm = 0; BOOL AlreadyLoaded = FALSE; @@ -60,30 +62,12 @@ STORMMODULE::STORMMODULE() STORMMODULE::~STORMMODULE() { - SFileCloseArchive = 0; - SFileCloseFile = 0; - SFileDestroy = 0; - SFileGetFileArchive = 0; - SFileGetFileSize = 0; - SFileOpenArchive = 0; - SFileOpenFile = 0; - SFileOpenFileEx = 0; - SFileReadFile = 0; - SFileSetBasePath = 0; - SFileSetFilePointer = 0; - SFileSetLocale = 0; - SFileGetBasePath = 0; - SFileGetArchiveName = 0; - SFileGetFileName = 0; - - if (hStorm==0) return; - - if (AlreadyLoaded==FALSE) FreeLibrary(hStorm); - hStorm = 0; + FreeStorm(); } HINSTANCE LoadStorm(char * DllFileName) { + FreeStorm(); if (hStorm!=0) return 0; hStorm = GetModuleHandle(DllFileName); if (hStorm == NULL) hStorm = LoadLibrary(DllFileName); @@ -112,3 +96,26 @@ HINSTANCE LoadStorm(char * DllFileName) return hStorm; } +void FreeStorm() +{ + SFileCloseArchive = 0; + SFileCloseFile = 0; + SFileDestroy = 0; + SFileGetFileArchive = 0; + SFileGetFileSize = 0; + SFileOpenArchive = 0; + SFileOpenFile = 0; + SFileOpenFileEx = 0; + SFileReadFile = 0; + SFileSetBasePath = 0; + SFileSetFilePointer = 0; + SFileSetLocale = 0; + SFileGetBasePath = 0; + SFileGetArchiveName = 0; + SFileGetFileName = 0; + + if (hStorm==0) return; + + if (AlreadyLoaded==FALSE) FreeLibrary(hStorm); + hStorm = 0; +} diff --git a/grpapi/grpapi.cpp b/grpapi/grpapi.cpp index f746120..b5fb2ce 100644 --- a/grpapi/grpapi.cpp +++ b/grpapi/grpapi.cpp @@ -1,5 +1,5 @@ #include "grpapi.h" -#include "..\\LoadStorm\\storm.h" +#include "../LoadStorm/storm.h" #include #ifdef GRPAPI_STATIC @@ -34,6 +34,9 @@ typedef struct { DWORD Offset; } FRAMEHEADER; +GETPIXELPROC MyGetPixel = GetPixel; +SETPIXELPROC MySetPixel = (SETPIXELPROC)SetPixelV; + void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD dwFlags, DWORD dwAlpha); extern HINSTANCE hStorm; @@ -60,7 +63,7 @@ BOOL APIENTRY DllMain( HINSTANCE hInstDLL, BOOL GRPAPI WINAPI LoadGrpApi() { - if (hStorm==0) return FALSE; + if (!hStorm) return FALSE; else return TRUE; } @@ -68,14 +71,20 @@ void GRPAPI WINAPI FreeGrpApi() { } +BOOL GRPAPI WINAPI SetMpqDll(LPCSTR lpDllFileName) +{ + if (LoadStorm((char *)lpDllFileName)) return TRUE; + else return FALSE; +} + BOOL GRPAPI WINAPI LoadPalette(LPCSTR lpFileName, DWORD *dwPaletteBuffer) { - if (lpFileName==0 || dwPaletteBuffer==0) return FALSE; + if (!lpFileName || !dwPaletteBuffer) return FALSE; HANDLE hFile; - if (SFileOpenFileEx!=0 && SFileGetFileSize!=0 - && SFileSetFilePointer!=0 && SFileReadFile!=0 - && SFileCloseFile!=0) { - if (SFileOpenFileEx(0,lpFileName,1,&hFile)==0) return FALSE; + if (SFileOpenFileEx && SFileGetFileSize + && SFileSetFilePointer && SFileReadFile + && SFileCloseFile) { + if (!SFileOpenFileEx(0,lpFileName,1,&hFile)) return FALSE; DWORD fsz = SFileGetFileSize(hFile,0); SFileSetFilePointer(hFile,0,0,FILE_BEGIN); if (fsz>=1024) { @@ -126,20 +135,20 @@ BOOL GRPAPI WINAPI LoadPalette(LPCSTR lpFileName, DWORD *dwPaletteBuffer) HANDLE GRPAPI WINAPI LoadGrp(LPCSTR lpFileName) { - if (lpFileName==0) return (HANDLE)-1; + if (!lpFileName) return (HANDLE)-1; HANDLE hFile; char *GrpFile; - if (SFileOpenFileEx!=0 && SFileGetFileSize!=0 - && SFileSetFilePointer!=0 && SFileReadFile!=0 - && SFileCloseFile!=0) { - if (SFileOpenFileEx(0,lpFileName,1,&hFile)==0) return (HANDLE)-1; + if (SFileOpenFileEx && SFileGetFileSize + && SFileSetFilePointer && SFileReadFile + && SFileCloseFile) { + if (!SFileOpenFileEx(0,lpFileName,1,&hFile)) return (HANDLE)-1; DWORD fsz = SFileGetFileSize(hFile,0); if (fsz<6) { SFileCloseFile(hFile); return (HANDLE)-1; } GrpFile = (char *)VirtualAlloc(0,fsz,MEM_COMMIT,PAGE_READWRITE); - if (GrpFile!=0) { + if (GrpFile) { SFileSetFilePointer(hFile,0,0,FILE_BEGIN); SFileReadFile(hFile,GrpFile,fsz,0,0); } @@ -155,7 +164,7 @@ HANDLE GRPAPI WINAPI LoadGrp(LPCSTR lpFileName) return (HANDLE)-1; } GrpFile = (char *)VirtualAlloc(0,fsz,MEM_COMMIT,PAGE_READWRITE); - if (GrpFile!=0) { + if (GrpFile) { SetFilePointer(hFile,0,0,FILE_BEGIN); ReadFile(hFile,GrpFile,fsz,&tsz,0); } @@ -167,14 +176,14 @@ HANDLE GRPAPI WINAPI LoadGrp(LPCSTR lpFileName) BOOL GRPAPI WINAPI DestroyGrp(HANDLE hGrp) { - if (hGrp==0 || hGrp==INVALID_HANDLE_VALUE) return FALSE; + if (!hGrp || hGrp==INVALID_HANDLE_VALUE) return FALSE; VirtualFree(hGrp,0,MEM_RELEASE); return TRUE; } BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WORD nFrame, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha) { - if (hGrp==0 || hGrp==INVALID_HANDLE_VALUE || hdcDest==0 || dwPalette==0) return FALSE; + if (!hGrp || hGrp==INVALID_HANDLE_VALUE || hdcDest==0 || !dwPalette) return FALSE; GRPHEADER *GrpFile = (GRPHEADER *)hGrp; nFrame %= GrpFile->nFrames; FRAMEHEADER *GrpFrame = &((FRAMEHEADER *)(((char *)GrpFile)+6))[nFrame]; @@ -184,67 +193,119 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR nXDest += GrpFrame->Left; nYDest += GrpFrame->Top; WORD *GrpOffsets = ((WORD *)(((char *)GrpFile)+GrpFrame->Offset)); - for (DWORD y=0;yHeight;y++) { - BYTE *RowData = ((BYTE *)(((char *)GrpOffsets)+GrpOffsets[y])); - WORD x=0,ofs=0; - while (xWidth) { - if (RowData[ofs]<=64) { - if ((dwFlags&HORIZONTAL_FLIP)==0 && (dwFlags&VERTICAL_FLIP)==0) { - for (WORD i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,nXDest+x,nYDest+y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); - x++; + BYTE *RowData; + WORD x,ofs; + DWORD y; + WORD i; + if (!(dwFlags&HORIZONTAL_FLIP) && !(dwFlags&VERTICAL_FLIP)) { + for (y=0;yHeight;y++) { + RowData = ((BYTE *)(((char *)GrpOffsets)+GrpOffsets[y])); + x=0; ofs=0; + while (xWidth) { + if (!(RowData[ofs] & 0x80)) { + if (!(RowData[ofs] & 0x40)) { + for (i=1;i<=RowData[ofs] && xWidth;i++) { + SetPix(hdcDest,nXDest+x,nYDest+y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + x++; + } + ofs+=RowData[ofs]+1; } - } - else if (dwFlags&HORIZONTAL_FLIP && (dwFlags&VERTICAL_FLIP)==0) { - for (WORD i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,Right-x,nYDest+y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); - x++; + else { + for (i=0;iWidth;i++) { + SetPix(hdcDest,nXDest+x,nYDest+y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + x++; + } + ofs+=2; } } - else if ((dwFlags&HORIZONTAL_FLIP)==0 && dwFlags&VERTICAL_FLIP) { - for (WORD i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,nXDest+x,Bottom-y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); - x++; + else { + x+=RowData[ofs]-128; + ofs++; + } + } + } + } + else if (dwFlags&HORIZONTAL_FLIP && !(dwFlags&VERTICAL_FLIP)) { + for (y=0;yHeight;y++) { + RowData = ((BYTE *)(((char *)GrpOffsets)+GrpOffsets[y])); + x=0; ofs=0; + while (xWidth) { + if (!(RowData[ofs] & 0x80)) { + if (!(RowData[ofs] & 0x40)) { + for (i=1;i<=RowData[ofs] && xWidth;i++) { + SetPix(hdcDest,Right-x,nYDest+y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + x++; + } + ofs+=RowData[ofs]+1; + } + else { + for (i=0;iWidth;i++) { + SetPix(hdcDest,Right-x,nYDest+y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + x++; + } + ofs+=2; } } else { - for (WORD i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,Right-x,Bottom-y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); - x++; - } + x+=RowData[ofs]-128; + ofs++; } - ofs+=RowData[ofs]+1; } - else if (RowData[ofs]<=128) { - if ((dwFlags&HORIZONTAL_FLIP)==0 && (dwFlags&VERTICAL_FLIP)==0) { - for (WORD i=0;iWidth;i++) { - SetPix(hdcDest,nXDest+x,nYDest+y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); - x++; + } + } + else if (!(dwFlags&HORIZONTAL_FLIP) && dwFlags&VERTICAL_FLIP) { + for (y=0;yHeight;y++) { + RowData = ((BYTE *)(((char *)GrpOffsets)+GrpOffsets[y])); + x=0; ofs=0; + while (xWidth) { + if (!(RowData[ofs] & 0x80)) { + if (!(RowData[ofs] & 0x40)) { + for (i=1;i<=RowData[ofs] && xWidth;i++) { + SetPix(hdcDest,nXDest+x,Bottom-y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + x++; + } + ofs+=RowData[ofs]+1; } - } - else if (dwFlags&HORIZONTAL_FLIP && (dwFlags&VERTICAL_FLIP)==0) { - for (WORD i=0;iWidth;i++) { - SetPix(hdcDest,Right-x,nYDest+y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); - x++; + else { + for (i=0;iWidth;i++) { + SetPix(hdcDest,nXDest+x,Bottom-y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + x++; + } + ofs+=2; } } - else if ((dwFlags&HORIZONTAL_FLIP)==0 && dwFlags&VERTICAL_FLIP) { - for (WORD i=0;iWidth;i++) { - SetPix(hdcDest,nXDest+x,Bottom-y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); - x++; + else { + x+=RowData[ofs]-128; + ofs++; + } + } + } + } + else { + for (y=0;yHeight;y++) { + RowData = ((BYTE *)(((char *)GrpOffsets)+GrpOffsets[y])); + x=0; ofs=0; + while (xWidth) { + if (!(RowData[ofs] & 0x80)) { + if (!(RowData[ofs] & 0x40)) { + for (i=1;i<=RowData[ofs] && xWidth;i++) { + SetPix(hdcDest,Right-x,Bottom-y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + x++; + } + ofs+=RowData[ofs]+1; + } + else { + for (i=0;iWidth;i++) { + SetPix(hdcDest,Right-x,Bottom-y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + x++; + } + ofs+=2; } } else { - for (WORD i=0;iWidth;i++) { - SetPix(hdcDest,Right-x,Bottom-y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); - x++; - } + x+=RowData[ofs]-128; + ofs++; } - ofs+=2; - } - else { - x+=RowData[ofs]-128; - ofs++; } } } @@ -253,21 +314,28 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR BOOL GRPAPI WINAPI GetGrpInfo(HANDLE hGrp, GRPHEADER *GrpInfo) { - if (hGrp==0 || hGrp==INVALID_HANDLE_VALUE || GrpInfo==0) return FALSE; + if (!hGrp || hGrp==INVALID_HANDLE_VALUE || !GrpInfo) return FALSE; memcpy(GrpInfo,hGrp,6); return TRUE; } +void GRPAPI WINAPI SetFunctionGetPixel(GETPIXELPROC lpGetPixelProc) +{ + MyGetPixel = lpGetPixelProc; +} + +void GRPAPI WINAPI SetFunctionSetPixel(SETPIXELPROC lpSetPixelProc) +{ + MySetPixel = lpSetPixelProc; +} + void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD dwFlags, DWORD dwAlpha) { if (dwFlags&SHADOW_COLOR) { - ((BYTE *)&clrColor)[0] = ((BYTE *)&dwFlags)[1]; - ((BYTE *)&clrColor)[1] = ((BYTE *)&dwFlags)[2]; - ((BYTE *)&clrColor)[2] = ((BYTE *)&dwFlags)[3]; - ((BYTE *)&clrColor)[3] = 0; + clrColor = (dwFlags >> 8) & 0x00FFFFFF; } if (dwFlags&ALPHA_BLEND) { - DWORD dwColor = GetPixel(hDC,X,Y); + DWORD dwColor = MyGetPixel(hDC,X,Y); // Old alpha /*((BYTE *)&dwColor)[0]*=1-((float)((BYTE *)&dwAlpha)[0]/256); @@ -293,6 +361,5 @@ void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD dwFlags, DW ( ( ((BYTE *)&clrColor)[2] * ( ((BYTE *)&dwAlpha)[2] + 1 ) ) >> 8 ) + ( ( ((BYTE *)&dwColor)[2] * ( 256 - ((BYTE *)&dwAlpha)[2] ) ) >> 8 ); } - SetPixelV(hDC,X,Y,clrColor); + MySetPixel(hDC,X,Y,clrColor); } - diff --git a/grpapi/grpapi.def b/grpapi/grpapi.def index db8056e..131acb6 100644 --- a/grpapi/grpapi.def +++ b/grpapi/grpapi.def @@ -1,8 +1,11 @@ EXPORTS - LoadPalette @1 - LoadGrp @2 - DestroyGrp @3 - DrawGrp @4 - GetGrpInfo @5 - LoadGrpApi @6 - FreeGrpApi @7 + LoadPalette @1 + LoadGrp @2 + DestroyGrp @3 + DrawGrp @4 + GetGrpInfo @5 + LoadGrpApi @6 + FreeGrpApi @7 + SetFunctionGetPixel @8 + SetFunctionSetPixel @9 + SetMpqDll @10 diff --git a/grpapi/grpapi.h b/grpapi/grpapi.h index 8fc78cb..2f48cef 100644 --- a/grpapi/grpapi.h +++ b/grpapi/grpapi.h @@ -1,6 +1,6 @@ /* - ShadowFlare GRP Library. (c) ShadowFlare Software 2002 + ShadowFlare GRP Library. (c) ShadowFlare Software 2002-2006 Any comments or suggestions are accepted at blakflare@hotmail.com (ShadowFlare) */ @@ -55,6 +55,30 @@ BOOL GRPAPI WINAPI DestroyGrp(HANDLE hGrp); BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WORD nFrame, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha); BOOL GRPAPI WINAPI GetGrpInfo(HANDLE hGrp, GRPHEADER *GrpInfo); +typedef COLORREF (WINAPI* GETPIXELPROC)( + HDC hDC, // same value as hdcDest from DrawGrp, + // does not need to be used as an HDC, + // can be used for any other type of pointer + int X, // x-coordinate of pixel + int Y // y-coordinate of pixel +); +typedef void (WINAPI* SETPIXELPROC)( + HDC hDC, // same value as hdcDest from DrawGrp, + // does not need to be used as an HDC, + // can be used for any other type of pointer + int X, // x-coordinate of pixel + int Y, // y-coordinate of pixel + COLORREF clrColor // new pixel color +); + +// Call these to have DrawGrp use custom functions for reading and drawing pixels +// so that you can have it read from and write to a buffer, for example. +void GRPAPI WINAPI SetFunctionGetPixel(GETPIXELPROC lpGetPixelProc); +void GRPAPI WINAPI SetFunctionSetPixel(SETPIXELPROC lpSetPixelProc); + +// Call this to make a different Storm.dll-compatible MPQ library be used (like SFMPQ). +BOOL GRPAPI WINAPI SetMpqDll(LPCSTR lpDllFileName); + // These no longer need to be called BOOL GRPAPI WINAPI LoadGrpApi(); void GRPAPI WINAPI FreeGrpApi(); diff --git a/grpapi/grpapi.vcproj b/grpapi/grpapi.vcproj index 11fbbd9..6198eb0 100644 --- a/grpapi/grpapi.vcproj +++ b/grpapi/grpapi.vcproj @@ -86,7 +86,9 @@