Current News
Archived News
Search News
Discussion Forum


Old Forum
Install Programs More Downloads...
Troubleshooting
Source Code
Format Specs.
Misc. Information
Non-SF Stuff
Links




1 // drawgrp.cpp : Defines the entry point for the console application.
2 //
3 /* License information for this code is in license.txt */
5 #define _CRT_RAND_S
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include "stdafx.h"
11 /*int qmain(int argc, char* argv[]);
13 int main(int argc, char* argv[])
14 {
15         HINSTANCE hStorm = LoadStorm("Storm.dll");
16         if (SFileDestroy!=0) SFileDestroy();
17         LoadGrpApi();
18         int RetVal = qmain(argc,argv);
19         FreeGrpApi();
20         if (hStorm!=0) FreeLibrary(hStorm);
21         return RetVal;
22 }*/
24 struct BufferInfo {
25         WORD nWidth;
26         WORD nHeight;
27         signed short *pBuffer;
28         WORD nFrame;
29 };
31 COLORREF WINAPI ReadPixelFromBuffer(BufferInfo *pBI, int X, int Y)
32 {
33         if (pBI->nFrame == 0xFFFF)
34                 return pBI->pBuffer[(Y * pBI->nWidth) + X];
35         else
36                 return pBI->pBuffer[(pBI->nFrame * pBI->nWidth * pBI->nHeight) + (Y * pBI->nWidth) + X];
37 }
39 void WINAPI WritePixelToBuffer(BufferInfo *pBI, int X, int Y, COLORREF clrColor)
40 {
41         if (pBI->nFrame == 0xFFFF)
42                 pBI->pBuffer[(Y * pBI->nWidth) + X] = (signed short)clrColor;
43         else
44                 //if (clrColor < 1 || clrColor > 13)
45                 //      pBI->pBuffer[(pBI->nFrame * pBI->nWidth * pBI->nHeight) + (Y * pBI->nWidth) + X] = (signed short)((BYTE)-clrColor);
46                 //else
47                         pBI->pBuffer[(pBI->nFrame * pBI->nWidth * pBI->nHeight) + (Y * pBI->nWidth) + X] = (signed short)clrColor;
48 }
50 int main(int argc, char* argv[])
51 {
52         HANDLE hMPQ=0,hMPQ2=0,hMPQ3=0;
53         DWORD *dwPalette = (DWORD *)_alloca(1024);
54         memset(dwPalette,0,1024);
55         LoadStorm("SFMpq.dll");
56         SetMpqDll("SFMpq.dll");
57         if (SFileOpenArchive!=0) {
58                 char *buffer = (char *)_alloca(13);
59                 memcpy(buffer,"StarDat.mpq",12);
60                 if (SFileOpenArchive(buffer,1000,0,&hMPQ)==0) return 0;
61                 memcpy(buffer,"BrooDat.mpq",12);
62                 SFileOpenArchive(buffer,2000,0,&hMPQ2);
63                 memcpy(buffer,"Patch_rt.mpq",13);
64                 SFileOpenArchive(buffer,3000,0,&hMPQ3);
65         }
66         BufferInfo BI;
67         LoadPalette("tileset\\Platform.wpe",dwPalette);
68         HANDLE hGrp, hGrp2;
69         if (argc>1)
70                 hGrp = LoadGrp(argv[1]);
71         else
72                 hGrp = LoadGrp("unit\\zerg\\zergling.grp");
73         HDC hDC = GetDC(0);
74         GRPHEADER GrpInfo;
75         if (GetGrpInfo(hGrp,&GrpInfo)==0) {GrpInfo.nFrames=0;GrpInfo.wMaxWidth=0;GrpInfo.wMaxHeight=0;}
76         BI.nWidth = GrpInfo.wMaxWidth;
77         BI.nHeight = GrpInfo.wMaxHeight;
78         //BI.nWidth = 255;
79         //BI.nHeight = 255;
80         BI.pBuffer = (signed short *)malloc(GrpInfo.nFrames * BI.nWidth * BI.nHeight * sizeof(short));
81         WORD i,x,y;
82         DWORD j, nGrpSize;
83         unsigned int u,v;
84         memset(BI.pBuffer, 0xFF, GrpInfo.nFrames * BI.nWidth * BI.nHeight * sizeof(short));
85         //for (DWORD j=0;j<16;j++){
86         /*for (WORD i=0;i<GrpInfo.nFrames;i++) {
87                 DrawGrp(hGrp,hDC,0,0,i,dwPalette,ALPHA_BLEND,0x401020);
88         }*/
89         signed short clrPixel;
90         RECT rect;
91         /*for (i=0;i<GrpInfo.nFrames;i++) {
92                 for (j=0;j<16;j++)
93                 {
94                         rand_s(&u);
95                         rand_s(&v);
96                         u = u % 800;
97                         v = v % 600;
98                         DrawGrp(hGrp,hDC,u,v,i,dwPalette,ALPHA_BLEND,0x404040);
99                 }
100         }*/
101         SetFunctionGetPixel((GETPIXELPROC)ReadPixelFromBuffer);
102         SetFunctionSetPixel((SETPIXELPROC)WritePixelToBuffer);
103         for (i=0;i<GrpInfo.nFrames;i++) {
104                 BI.nFrame = i;
105                 u = (BI.nWidth - GrpInfo.wMaxWidth) / 2;
106                 v = (BI.nHeight - GrpInfo.wMaxHeight) / 2;
107                 for (y = 0; y < BI.nHeight; y++) {
108                         for (x = 0; x < BI.nWidth; x++) {
109                                 WritePixelToBuffer(&BI, x, y, -1);
110                         }
111                 }
112                 DrawGrp(hGrp,(HDC)&BI,u,v,i,0,USE_INDEX,0);
113         }
114         hGrp2 = hGrp;
115         hGrp = CreateGrp(BI.pBuffer, GrpInfo.nFrames, BI.nWidth, BI.nHeight, FALSE, &nGrpSize);
116         /*HANDLE hFile;
117         hFile = CreateFile("generated zergling.grp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
118         if (hFile != INVALID_HANDLE_VALUE) {
119                 WriteFile(hFile, hGrp, nGrpSize, &j, 0);
120                 CloseHandle(hFile);
121         }*/
122         BI.nFrame = 0xFFFF;
123         j=0;
124         
125         for (i=0;i<GrpInfo.nFrames;i+=1) {
126                 rect.left = rect.top = 0;
127                 rect.right = rect.left + BI.nWidth;
128                 rect.bottom = rect.top + BI.nHeight;
129                 for (x = 0; x < BI.nWidth * BI.nHeight; x++)
130                         BI.pBuffer[x] = -1;
131                 //for (j=0;j<(1 * 1);j++) {
132 //                      DrawGrp(hGrp,(HDC)&BI,i,0,i % (17*8),dwPalette,ALPHA_BLEND,0x401020);
133                         //DrawGrp(hGrp2,(HDC)&BI,0,0,i,0,USE_INDEX,0x401020);
134                         //u = memcmp(BI.pBuffer, &BI.pBuffer[i * BI.nWidth * BI.nHeight], BI.nWidth * BI.nHeight * sizeof(short));
135                         DrawGrp(hGrp,(HDC)&BI,0,0,i,0,USE_INDEX,0x401020);
136                         u = memcmp(BI.pBuffer, &BI.pBuffer[i * BI.nWidth * BI.nHeight], BI.nWidth * BI.nHeight * sizeof(short));
137                         for (x = 0; x < BI.nWidth; x++)
138                                 for (y = 0; y < BI.nHeight; y++) {
139                                         clrPixel = BI.pBuffer[(y * BI.nWidth) + x];
140                                         if (clrPixel != -1) SetPixelV(hDC, x, y, dwPalette[clrPixel]);
141                                 }
142                 //}
143                 if (u) {
144                         printf("Output of re-encoded graphic for frame %d does not match original!  Total %d\n", i, ++j);
145                         //break;
146                 }
147                 FillRect(hDC, &rect, (HBRUSH) (COLOR_WINDOW+1));
148         }
149         for (i=4;i<400;i+=17) {
150                 rect.left = rect.top = 400-i;
151                 rect.right = rect.left + BI.nWidth;
152                 rect.bottom = rect.top + BI.nHeight;
153                 for (x = 0; x < BI.nWidth * BI.nHeight; x++)
154                         BI.pBuffer[x] = -1;
155                 for (j=0;j<32;j++) {
156 //                      DrawGrp(hGrp,(HDC)&BI,400-i,0,i % (17*8),dwPalette,HORIZONTAL_FLIP|ALPHA_BLEND,0x401020);
157                         DrawGrp(hGrp,(HDC)&BI,0,0,i % (17*8),0,HORIZONTAL_FLIP|USE_INDEX,0x401020);
158                         for (x = 0; x < BI.nWidth; x++)
159                                 for (y = 0; y < BI.nHeight; y++) {
160                                         clrPixel = BI.pBuffer[(y * BI.nWidth) + x];
161                                         if (clrPixel != -1) SetPixelV(hDC, 400-i + x, 400-i + y, dwPalette[clrPixel]);
162                                 }
163                 }
164                 FillRect(hDC, &rect, (HBRUSH) (COLOR_WINDOW+1));
165         }
166         for (i = 0; i < BI.nWidth; i++)
167                 for (j = 0; j < BI.nHeight; j++) {
168                         clrPixel = BI.pBuffer[(j * BI.nWidth) + i];
169                         if (clrPixel != -1) SetPixelV(hDC, 400 + i, 300 + j, dwPalette[clrPixel]);
170                 }
171         //}
172         
173         ReleaseDC(0,hDC);
174         free(BI.pBuffer);
175     DestroyGrp(hGrp);
176     DestroyGrp(hGrp2);
177         if (SFileCloseArchive!=0) {
178                 if (hMPQ3!=0) SFileCloseArchive(hMPQ3);
179                 if (hMPQ2!=0) SFileCloseArchive(hMPQ2);
180                 if (hMPQ!=0) SFileCloseArchive(hMPQ);
181         }
182         return 0;