-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDlgCreateAnimatedTexture.cpp
399 lines (315 loc) · 11.4 KB
/
DlgCreateAnimatedTexture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
// DlgCreateAnimatedTexture.cpp : implementation file
//
#include "StdH.h"
#include "DlgCreateAnimatedTexture.h"
#include <Engine/Templates/Stock_CTextureData.h>
#ifdef _DEBUG
#undef new
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CDlgCreateAnimatedTexture dialog
#define TEMPORARY_SCRIPT_NAME "Temp\\Temp.scr"
#define TEMPORARY_TEXTURE_NAME "Temp\\Temp.tex"
void CDlgCreateAnimatedTexture::ReleaseCreatedTexture(void)
{
// if there is texture obtained, release it
if (m_ptdCreated != NULL)
{
// free obtained texture
_pTextureStock->Release(m_ptdCreated);
m_ptdCreated = NULL;
m_wndViewCreatedTexture.m_toTexture.SetData(NULL);
}
}
void CDlgCreateAnimatedTexture::InitAnimationsCombo(void)
{
m_ctrlAnimationsCombo.ResetContent();
if (m_ptdCreated != NULL)
{
CAnimInfo aiInfo;
for (INDEX iAnim = 0; iAnim < m_ptdCreated->GetAnimsCt(); iAnim++)
{
m_ptdCreated->GetAnimInfo(iAnim, aiInfo);
m_ctrlAnimationsCombo.AddString(CString(aiInfo.ai_AnimName));
}
} else {
m_ctrlAnimationsCombo.AddString(_T("None"));
}
m_ctrlAnimationsCombo.SetCurSel(0);
}
void CDlgCreateAnimatedTexture::OnSelchangeTextureAnimations()
{
if (m_ptdCreated != NULL)
{
// set selected animation
INDEX iAnim = m_ctrlAnimationsCombo.GetCurSel();
m_wndViewCreatedTexture.m_toTexture.SetAnim(iAnim);
}
}
void CDlgCreateAnimatedTexture::RefreshTexture(void)
{
// refresh script string from edit control
UpdateData(TRUE);
// prepare names for temporary script and texture
CTFileName fnTempScript = CTString(TEMPORARY_SCRIPT_NAME);
CTFileName fnTemptexture = CTString(TEMPORARY_TEXTURE_NAME);
try {
// write context of edit ctrl to temporary script file
CTFileStream fileScript;
fileScript.Create_t(fnTempScript);
CTString strEditScript = MfcStringToCT(m_strEditScript);
char *pScript = (char *)AllocMemory(strlen(strEditScript) + 1);
strcpy(pScript, strEditScript);
fileScript.WriteRawChunk_t(pScript, strlen(strEditScript) + 1);
fileScript.Close();
FreeMemory(pScript);
// [Cecil] Patched script processing
P_ProcessTextureScript(fnTempScript);
// release old texture if it exists and obtain new texture
ReleaseCreatedTexture();
// obtain newly created texture
m_ptdCreated = _pTextureStock->Obtain_t(fnTemptexture);
m_ptdCreated->Reload();
// set texture data to texture preview window so it could display texture
m_wndViewCreatedTexture.m_toTexture.SetData(m_ptdCreated);
char achrSize[64];
sprintf(achrSize, "%d x %d",
m_ptdCreated->td_mexWidth >> m_ptdCreated->td_iFirstMipLevel,
m_ptdCreated->td_mexHeight >> m_ptdCreated->td_iFirstMipLevel);
m_strSizeInPixels = achrSize;
UpdateData(FALSE);
// init animations combo
InitAnimationsCombo();
} catch (char *err_str) {
AfxMessageBox(CString(err_str));
}
}
CDlgCreateAnimatedTexture::CDlgCreateAnimatedTexture(
CDynamicArray<CTFileName> &afnPictures, CWnd *pParent /*=NULL*/)
: CDialog(CDlgCreateAnimatedTexture::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgCreateAnimatedTexture)
m_strEditScript = _T("");
m_strSizeInPixels = _T("");
m_strCreatedTextureName = _T("");
//}}AFX_DATA_INIT
// remember array of selected frames
m_pafnPictures = &afnPictures;
// set first frame as input file name
afnPictures.Lock();
CTFileName fnInputFile = afnPictures[0];
afnPictures.Unlock();
if (fnInputFile != "" && (fnInputFile.FileExt() == ".tex" || fnInputFile.FileExt() == ".scr")) {
m_strCreatedTextureName = fnInputFile.FileDir() + fnInputFile.FileName() + ".tex";
} else {
m_strCreatedTextureName = "Unnamed";
}
m_bPreviewWindowsCreated = FALSE;
m_ptdCreated = NULL;
m_pixSourceWidth = -1;
m_pixSourceHeight = -1;
// remember source and destination file names
m_fnSourceFileName = fnInputFile;
m_fnCreatedFileName = fnInputFile.FileDir() + fnInputFile.FileName() + ".tex";
}
CDlgCreateAnimatedTexture::~CDlgCreateAnimatedTexture()
{
ReleaseCreatedTexture();
}
void CDlgCreateAnimatedTexture::DoDataExchange(CDataExchange *pDX)
{
CDialog::DoDataExchange(pDX);
// if dialog is recieving data
if (pDX->m_bSaveAndValidate == FALSE)
{
}
//{{AFX_DATA_MAP(CDlgCreateAnimatedTexture)
DDX_Control(pDX, IDC_CHEQUERED_ALPHA, m_ctrlCheckButton);
DDX_Control(pDX, IDC_TEXTURE_ANIMATIONS, m_ctrlAnimationsCombo);
DDX_Text(pDX, IDC_EDIT_SCRIPT, m_strEditScript);
DDX_Text(pDX, IDC_SIZE_IN_PIXELS, m_strSizeInPixels);
DDX_Text(pDX, IDC_TEXTURE_NAME, m_strCreatedTextureName);
//}}AFX_DATA_MAP
// if dialog is giving data
if (pDX->m_bSaveAndValidate != FALSE)
{
}
}
BEGIN_MESSAGE_MAP(CDlgCreateAnimatedTexture, CDialog)
//{{AFX_MSG_MAP(CDlgCreateAnimatedTexture)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_CHEQUERED_ALPHA, OnChequeredAlpha)
ON_BN_CLICKED(ID_CREATE_TEXTURE, OnCreateTexture)
ON_BN_CLICKED(ID_REFRESH_TEXTURE, OnRefreshTexture)
ON_CBN_SELCHANGE(IDC_TEXTURE_ANIMATIONS, OnSelchangeTextureAnimations)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CDlgCreateAnimatedTexture message handlers
void CDlgCreateAnimatedTexture::OnPaint()
{
CPaintDC dc(this); // device context for painting
// if texture preview windows are not yet created
if (!m_bPreviewWindowsCreated)
{
// ---------------- Create custom window that will show how created texture will look like
CWnd *pWndCreatedTexturePreview = GetDlgItem(IDC_TEXTURE_PREVIEW_WINDOW);
ASSERT(pWndCreatedTexturePreview != NULL);
CRect rectPreviewCreatedTextureWnd;
// get rectangle occupied by preview texture window
pWndCreatedTexturePreview->GetWindowRect(&rectPreviewCreatedTextureWnd);
ScreenToClient(&rectPreviewCreatedTextureWnd);
// create window for for showing created texture
m_wndViewCreatedTexture.Create(NULL, NULL, WS_BORDER | WS_VISIBLE, rectPreviewCreatedTextureWnd,
this, IDW_VIEW_CREATED_TEXTURE);
// mark that custom windows are created
m_bPreviewWindowsCreated = TRUE;
}
}
void CDlgCreateAnimatedTexture::OnChequeredAlpha()
{
// toggle chequered alpha on/off
m_wndViewCreatedTexture.m_bChequeredAlpha = !m_wndViewCreatedTexture.m_bChequeredAlpha;
}
void CDlgCreateAnimatedTexture::OnRefreshTexture()
{
RefreshTexture();
}
void CDlgCreateAnimatedTexture::OnCreateTexture()
{
// refresh (recreate) texture in temporary directory
RefreshTexture();
// prepare names for temporary script and texture
CTFileName fnFullTempTexture = IDir::AppPath() + CTString(TEMPORARY_TEXTURE_NAME);
CTFileName fnFullTempScript = IDir::AppPath() + CTString(TEMPORARY_SCRIPT_NAME);
// and for supposed final texture name
CTFileName fnFullFinalTexture = IDir::AppPath() + m_fnCreatedFileName;
CTFileName fnSaveName;
if (m_strCreatedTextureName == "Unnamed")
{
// extract last sub directory name
char achrLastSubDir[256];
strcpy(achrLastSubDir, m_fnSourceFileName.FileDir());
achrLastSubDir[strlen(achrLastSubDir) - 1] = 0; // remove last '\'
CTString strLastSubDir = CTFileName(CTString(achrLastSubDir)).FileName();
// call save texture requester
fnSaveName = _EngineGUI.BrowseTexture(
strLastSubDir + ".tex", // default name
KEY_NAME_CREATE_ANIMATED_TEXTURE_DIR, "Choose texture name",
FALSE /* bOpenFileRequester */);
if (fnSaveName == "") {
return;
}
} else {
fnSaveName = MfcStringToCT(m_strCreatedTextureName);
}
// set newly picked names for final script and texture
fnFullFinalTexture = IDir::AppPath() + fnSaveName;
CTFileName fnFullFinalScript = fnFullFinalTexture.FileDir() + fnFullFinalTexture.FileName() + ".scr";
// copy temporary script and texture files into real their place
CopyFileA(fnFullTempScript, fnFullFinalScript, FALSE);
CopyFileA(fnFullTempTexture, fnFullFinalTexture, FALSE);
m_fnCreatedFileName = fnSaveName;
// end dialog
EndDialog(IDOK);
}
BOOL CDlgCreateAnimatedTexture::OnInitDialog()
{
CDialog::OnInitDialog();
// if we received script as input
if (m_fnSourceFileName.FileExt() == ".scr") {
// load script file into edit control
try {
CTFileStream fileScript;
fileScript.Open_t(m_fnSourceFileName);
// get size of script file
ULONG ulScriptFileSize = fileScript.GetStreamSize();
char *pchrFile = new char[ulScriptFileSize + 1];
// set eol character
pchrFile[ulScriptFileSize] = 0;
fileScript.Read_t(pchrFile, ulScriptFileSize);
// copy script to edit ctrl
m_strEditScript = CTString(pchrFile);
delete[] pchrFile;
// catch errors
} catch (char *strError) {
// and do nothing
(void)strError;
}
// we will create temporary script
} else {
try {
// if can't get picture file information
CImageInfo iiImageInfo;
if (iiImageInfo.GetGfxFileInfo_t(m_fnSourceFileName) == UNSUPPORTED_FILE) {
// throw error
ThrowF_t("File '%s' has unsupported file format", (IDir::AppPath() + m_fnSourceFileName).str_String);
}
// get dimensions
m_pixSourceWidth = iiImageInfo.ii_Width;
m_pixSourceHeight = iiImageInfo.ii_Height;
} catch (char *err_str) {
AfxMessageBox(CString(err_str));
}
// allocate 16k for script
char achrDefaultScript[16384];
// default script into edit control
sprintf(achrDefaultScript,
";* Texture description\r\n"
"TEXTURE_WIDTH %.4f\r\n"
"TEXTURE_MIPMAPS 8\r\n"
"ANIM_START\r\n"
"DIRECTORY %s\r\n\r\n"
";* Animations\r\n"
"ANIMATION Default_Animation\r\n"
"SPEED 0.1\r\n"
"FRAMES %d\r\n",
METERS_MEX(m_pixSourceWidth * (1 << 5)),
(CTString &)m_fnCreatedFileName.FileDir(),
m_pafnPictures->Count());
// add name for each frame
FOREACHINDYNAMICARRAY(*m_pafnPictures, CTFileName, itPicture)
{
CTFileName &fn = *itPicture;
CTString strName = fn.FileName();
CTString strExt = fn.FileExt();
// add finishing part of script
sprintf(achrDefaultScript, "%s %s%s\r\n", achrDefaultScript, strName, strExt);
}
// add finishing part of script
sprintf(achrDefaultScript, "%sANIM_END\r\nEND\r\n", achrDefaultScript);
// copy default script into edit ctrl
m_strEditScript = achrDefaultScript;
}
CTFileName fnTexFileName = m_fnSourceFileName.FileDir() + m_fnSourceFileName.FileName() + ".tex";
// try to
try {
// obtain texture with the same name (if exists)
CTextureData *pTD = _pTextureStock->Obtain_t(fnTexFileName);
pTD->Reload();
// release texture
_pTextureStock->Release(pTD);
// if texture can't be obtained
} catch (char *err_str) {
// never mind
(void)err_str;
}
m_ctrlCheckButton.SetCheck(1);
// force edit script control to pick up default script string
UpdateData(FALSE);
// and refresh (recreate) texture in temporary directory
RefreshTexture();
return TRUE;
}