File: shiptexturesdlg.cpp

package info (click to toggle)
freespace2 3.7.4%2Brepack-1.1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye
  • size: 22,268 kB
  • sloc: cpp: 393,535; ansic: 4,106; makefile: 1,091; xml: 181; sh: 137
file content (395 lines) | stat: -rw-r--r-- 9,011 bytes parent folder | download | duplicates (2)
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
// ShipTexturesDlg.cpp : implementation file
// Goober5000

#include "stdafx.h"
#include "fred.h"
#include "ShipTexturesDlg.h"
#include "model/model.h"
#include "bmpman/bmpman.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CShipTexturesDlg dialog


CShipTexturesDlg::CShipTexturesDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CShipTexturesDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CShipTexturesDlg)
	m_new_texture = _T("");
	m_old_texture_list = 0;
	//}}AFX_DATA_INIT

	self_ship = -1;
	active_texture_index = -1;
	modified = 0;
	texture_count = 0;
}


void CShipTexturesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

	//{{AFX_DATA_MAP(CShipTexturesDlg)
	DDX_Text(pDX, IDC_NEW_TEXTURE, m_new_texture);
	DDX_CBIndex(pDX, IDC_OLD_TEXTURE_LIST, m_old_texture_list);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CShipTexturesDlg, CDialog)
	//{{AFX_MSG_MAP(CShipTexturesDlg)
	ON_WM_CLOSE()
	ON_CBN_SELCHANGE(IDC_OLD_TEXTURE_LIST, OnSelchangeOldTextureList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShipTexturesDlg message handlers

void CShipTexturesDlg::OnOK() 
{
	int i, z, not_found, temp_bmp, temp_frames, temp_fps;
	CString missing_files, message;
	char buf[10];

	// update, in case of a last-minute edit
	OnSelchangeOldTextureList();

	// quick skip if nothing modified
	if (query_modified())
	{
		// sort according to new
		sort_textures(SORT_NEW);

		// check for filenames not found
		not_found = 0;
		missing_files = _T("");
		for (i=0; i<texture_count; i++)
		{
			// make sure we have a texture
			if (strlen(new_texture_name[i]))
			{
				// allow invisible textures without doing a file check
				if (!stricmp(new_texture_name[i], "invisible"))
					continue;

				// try loading the texture (bmpman should take care of eventually unloading it)
				temp_bmp = bm_load( new_texture_name[i] );

				// if PCX not found, look for ANI
				if (temp_bmp < 0)
				{					
					temp_bmp = bm_load_animation(new_texture_name[i],  &temp_frames, &temp_fps, NULL, 1);
				}

				// check if loaded
				if (temp_bmp < 0)
				{
					not_found++;
					missing_files += "   ";
					missing_files += new_texture_name[i];
					missing_files += '\n';
				}
			}
		}

		// alert user if any textures were not found
		if (not_found)
		{
			sprintf(buf, "%d", not_found);
			message = "FRED was unable to find ";
			message += buf;
			message += ((not_found > 1) ? " files:\n" : " file:\n");
			message += missing_files;
			message += "\nContinue anyway?";

			z = MessageBox(message, ((not_found > 1) ? "Some textures were not found." : "A texture was not found."), MB_OKCANCEL | MB_ICONEXCLAMATION | MB_DEFBUTTON2);
			if (z == IDCANCEL)
			{
				return;
			}
		}

		// re-sort according to old
		sort_textures();

		// overwrite all the old entries that refer to this ship
		SCP_vector<texture_replace>::iterator ii, end;
		end = Fred_texture_replacements.end();
		for (ii = Fred_texture_replacements.begin(); ii != end; ++ii)
		{
			if (!stricmp(ii->ship_name, Ships[self_ship].ship_name))
			{
				do {
					end--;
				} while (end != ii && !stricmp(end->ship_name, Ships[self_ship].ship_name));
				if (end == ii)
					break;
				texture_set(&(*ii), &(*end));
			}
		}

		if (end != Fred_texture_replacements.end())
			Fred_texture_replacements.erase(end, Fred_texture_replacements.end());

		// now put the new entries on the end of the list
		for (i=0; i<texture_count; i++)
		{
			// make sure there is an entry
			if (strlen(new_texture_name[i]))
			{
				texture_replace tr;

				strcpy_s(tr.old_texture, old_texture_name[i]);
				strcpy_s(tr.new_texture, new_texture_name[i]);
				strcpy_s(tr.ship_name, Ships[self_ship].ship_name);
				tr.new_texture_id = -1;

				// assign to global FRED array
				Fred_texture_replacements.push_back(tr);
			}
		}
	}	// skipped here if nothing modified

	CDialog::OnOK();
}

BOOL CShipTexturesDlg::OnInitDialog() 
{	
	int i, j, k, z, duplicate;
	char *p = NULL;
	char texture_file[MAX_FILENAME_LEN];
	CComboBox *box;

	// get our model
	polymodel *pm = model_get(Ship_info[Ships[self_ship].ship_info_index].model_num);

	// empty old and new fields
	texture_count = 0;
	for (i=0; i<MAX_REPLACEMENT_TEXTURES; i++)
	{
		*old_texture_name[i] = 0;
		*new_texture_name[i] = 0;
	}

	// set up pointer to combo box
	box = (CComboBox *) GetDlgItem(IDC_OLD_TEXTURE_LIST);
	box->ResetContent();

	// look for textures to populate the combo box
	for (i=0; i<pm->n_textures; i++)
	{
		for(j = 0; j < TM_NUM_TYPES; j++)
		{
			// get texture file name
			bm_get_filename(pm->maps[i].textures[j].GetOriginalTexture(), texture_file);

			// skip blank textures
			if (!strlen(texture_file))
				continue;

			// get rid of file extension
			p = strchr( texture_file, '.' );
			if ( p )
			{
				//mprintf(( "ignoring extension on file '%s'\n", texture_file ));
				*p = 0;
			}

			// check for duplicate textures in list
			duplicate = -1;
			for (k=0; k<texture_count; k++)
			{
				if (!stricmp(old_texture_name[k], texture_file))
				{
					duplicate = k;
					break;
				}
			}

			if (duplicate >= 0)
				continue;

			// make old texture lowercase
			strlwr(texture_file);

			// add it to the field
			strcpy_s(old_texture_name[texture_count], texture_file);

			// increment
			texture_count++;
		}
	}

	// now sort the filenames
	sort_textures();

	// and add them to the box
	for (i=0; i<texture_count; i++) {
		z = box->AddString(old_texture_name[i]);
	}

	// now look for new textures
	for (SCP_vector<texture_replace>::iterator ii = Fred_texture_replacements.begin(); ii != Fred_texture_replacements.end(); ++ii)
	{
		if (!stricmp(Ships[self_ship].ship_name, ii->ship_name))
		{
			// look for corresponding old texture
			for (i=0; i<texture_count; i++)
			{
				// if match
				if (!stricmp(old_texture_name[i], ii->old_texture))
				{
					// assign new texture
					strcpy_s(new_texture_name[i], ii->new_texture);

					// we found one, so no more to check
					break;
				}
			}
		}
	}
	// end of new texture check

	// set indexes and flags
	m_old_texture_list = 0;
	active_texture_index = 0;
	modified = 0;

	// display new texture, if we have one
	m_new_texture = CString(new_texture_name[0]);

	CDialog::OnInitDialog();
	UpdateData(FALSE);
 
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CShipTexturesDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{	
	return CDialog::Create(IDD, pParentWnd);
}

void CShipTexturesDlg::OnClose() 
{
	int z;

	if (query_modified()) {
		z = MessageBox("Do you want to keep your changes?", "Close", MB_ICONQUESTION | MB_YESNOCANCEL);
		if (z == IDCANCEL)
			return;

		if (z == IDYES) {
			OnOK();
			return;
		}
	}
	
	CDialog::OnClose();
}

int CShipTexturesDlg::query_modified()
{
	return modified;
}

void CShipTexturesDlg::OnSelchangeOldTextureList() 
{
	UpdateData(TRUE);

	char *p;

	// see if we edited anything
	if (stricmp(new_texture_name[active_texture_index], m_new_texture))
	{
		// assign it
		strcpy_s(new_texture_name[active_texture_index], m_new_texture);
	
		// make it lowercase
		strlwr(new_texture_name[active_texture_index]);

		// get rid of file extension
		p = strchr( new_texture_name[active_texture_index], '.' );
		if ( p )
		{
			mprintf(( "ignoring extension on file '%s'\n", new_texture_name[active_texture_index] ));
			*p = 0;
		}

		// set modified flag
		modified = 1;
	}

	// bring active texture index up to date
	active_texture_index = m_old_texture_list;

	// display appropriate texture
	m_new_texture = CString(new_texture_name[active_texture_index]);

	UpdateData(FALSE);
}

// bubble sort
void CShipTexturesDlg::sort_textures(int test)
{
	int i, j, str_check = 0;

	for (i = 0; i < texture_count; i++)
	{
		for (j = 0; j < i; j++)
		{
			switch(test)
			{
				case SORT_OLD:
					str_check = stricmp(old_texture_name[i], old_texture_name[j]);
					break;
				case SORT_NEW:
					str_check = stricmp(new_texture_name[i], new_texture_name[j]);
					break;
				default:
					Int3();
			}

			if (str_check < 0)
			{
				// swap old
				swap_strings(old_texture_name[i], old_texture_name[j]);

				// swap new
				swap_strings(new_texture_name[i], new_texture_name[j]);
			}
		}
	}
}

void CShipTexturesDlg::swap_strings(char *str1, char *str2)
{
/*
	char *temp;
	temp = str1;
	str1 = str2;
	str2 = temp;
*/

	char temp[256];
	strcpy_s(temp, str1);
	strcpy(str1, str2);
	strcpy(str2, temp);
}

texture_replace *CShipTexturesDlg::texture_set(texture_replace *dest, const texture_replace *src)
{
	dest->new_texture_id = src->new_texture_id;
	strcpy_s(dest->ship_name, src->ship_name);
	strcpy_s(dest->old_texture, src->old_texture);
	strcpy_s(dest->new_texture, src->new_texture);

	return dest;
}