FS2_Open
Open source remastering of the Freespace 2 engine
shiptexturesdlg.cpp
Go to the documentation of this file.
1 // ShipTexturesDlg.cpp : implementation file
2 // Goober5000
3 
4 #include "stdafx.h"
5 #include "fred.h"
6 #include "ShipTexturesDlg.h"
7 #include "model/model.h"
8 #include "bmpman/bmpman.h"
9 
10 #ifdef _DEBUG
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
14 
16 // CShipTexturesDlg dialog
17 
18 
19 CShipTexturesDlg::CShipTexturesDlg(CWnd* pParent /*=NULL*/)
20  : CDialog(CShipTexturesDlg::IDD, pParent)
21 {
22  //{{AFX_DATA_INIT(CShipTexturesDlg)
23  m_new_texture = _T("");
25  //}}AFX_DATA_INIT
26 
27  self_ship = -1;
29  modified = 0;
30  texture_count = 0;
31 }
32 
33 
34 void CShipTexturesDlg::DoDataExchange(CDataExchange* pDX)
35 {
36  CDialog::DoDataExchange(pDX);
37 
38  //{{AFX_DATA_MAP(CShipTexturesDlg)
39  DDX_Text(pDX, IDC_NEW_TEXTURE, m_new_texture);
40  DDX_CBIndex(pDX, IDC_OLD_TEXTURE_LIST, m_old_texture_list);
41  //}}AFX_DATA_MAP
42 }
43 
44 
45 BEGIN_MESSAGE_MAP(CShipTexturesDlg, CDialog)
46  //{{AFX_MSG_MAP(CShipTexturesDlg)
47  ON_WM_CLOSE()
48  ON_CBN_SELCHANGE(IDC_OLD_TEXTURE_LIST, OnSelchangeOldTextureList)
49  //}}AFX_MSG_MAP
50 END_MESSAGE_MAP()
51 
53 // CShipTexturesDlg message handlers
54 
55 void CShipTexturesDlg::OnOK()
56 {
57  int i, z, not_found, temp_bmp, temp_frames, temp_fps;
58  CString missing_files, message;
59  char buf[10];
60 
61  // update, in case of a last-minute edit
62  OnSelchangeOldTextureList();
63 
64  // quick skip if nothing modified
65  if (query_modified())
66  {
67  // sort according to new
68  sort_textures(SORT_NEW);
69 
70  // check for filenames not found
71  not_found = 0;
72  missing_files = _T("");
73  for (i=0; i<texture_count; i++)
74  {
75  // make sure we have a texture
76  if (strlen(new_texture_name[i]))
77  {
78  // allow invisible textures without doing a file check
79  if (!stricmp(new_texture_name[i], "invisible"))
80  continue;
81 
82  // try loading the texture (bmpman should take care of eventually unloading it)
83  temp_bmp = bm_load( new_texture_name[i] );
84 
85  // if PCX not found, look for ANI
86  if (temp_bmp < 0)
87  {
88  temp_bmp = bm_load_animation(new_texture_name[i], &temp_frames, &temp_fps, NULL, 1);
89  }
90 
91  // check if loaded
92  if (temp_bmp < 0)
93  {
94  not_found++;
95  missing_files += " ";
96  missing_files += new_texture_name[i];
97  missing_files += '\n';
98  }
99  }
100  }
101 
102  // alert user if any textures were not found
103  if (not_found)
104  {
105  sprintf(buf, "%d", not_found);
106  message = "FRED was unable to find ";
107  message += buf;
108  message += ((not_found > 1) ? " files:\n" : " file:\n");
109  message += missing_files;
110  message += "\nContinue anyway?";
111 
112  z = MessageBox(message, ((not_found > 1) ? "Some textures were not found." : "A texture was not found."), MB_OKCANCEL | MB_ICONEXCLAMATION | MB_DEFBUTTON2);
113  if (z == IDCANCEL)
114  {
115  return;
116  }
117  }
118 
119  // re-sort according to old
120  sort_textures();
121 
122  // overwrite all the old entries that refer to this ship
124  end = Fred_texture_replacements.end();
125  for (ii = Fred_texture_replacements.begin(); ii != end; ++ii)
126  {
127  if (!stricmp(ii->ship_name, Ships[self_ship].ship_name))
128  {
129  do {
130  end--;
131  } while (end != ii && !stricmp(end->ship_name, Ships[self_ship].ship_name));
132  if (end == ii)
133  break;
134  texture_set(&(*ii), &(*end));
135  }
136  }
137 
138  if (end != Fred_texture_replacements.end())
140 
141  // now put the new entries on the end of the list
142  for (i=0; i<texture_count; i++)
143  {
144  // make sure there is an entry
145  if (strlen(new_texture_name[i]))
146  {
147  texture_replace tr;
148 
149  strcpy_s(tr.old_texture, old_texture_name[i]);
150  strcpy_s(tr.new_texture, new_texture_name[i]);
151  strcpy_s(tr.ship_name, Ships[self_ship].ship_name);
152  tr.new_texture_id = -1;
153 
154  // assign to global FRED array
155  Fred_texture_replacements.push_back(tr);
156  }
157  }
158  } // skipped here if nothing modified
159 
160  CDialog::OnOK();
161 }
162 
164 {
165  int i, j, k, z, duplicate;
166  char *p = NULL;
167  char texture_file[MAX_FILENAME_LEN];
168  CComboBox *box;
169 
170  // get our model
171  polymodel *pm = model_get(Ship_info[Ships[self_ship].ship_info_index].model_num);
172 
173  // empty old and new fields
174  texture_count = 0;
175  for (i=0; i<MAX_REPLACEMENT_TEXTURES; i++)
176  {
177  *old_texture_name[i] = 0;
178  *new_texture_name[i] = 0;
179  }
180 
181  // set up pointer to combo box
182  box = (CComboBox *) GetDlgItem(IDC_OLD_TEXTURE_LIST);
183  box->ResetContent();
184 
185  // look for textures to populate the combo box
186  for (i=0; i<pm->n_textures; i++)
187  {
188  for(j = 0; j < TM_NUM_TYPES; j++)
189  {
190  // get texture file name
191  bm_get_filename(pm->maps[i].textures[j].GetOriginalTexture(), texture_file);
192 
193  // skip blank textures
194  if (!strlen(texture_file))
195  continue;
196 
197  // get rid of file extension
198  p = strchr( texture_file, '.' );
199  if ( p )
200  {
201  //mprintf(( "ignoring extension on file '%s'\n", texture_file ));
202  *p = 0;
203  }
204 
205  // check for duplicate textures in list
206  duplicate = -1;
207  for (k=0; k<texture_count; k++)
208  {
209  if (!stricmp(old_texture_name[k], texture_file))
210  {
211  duplicate = k;
212  break;
213  }
214  }
215 
216  if (duplicate >= 0)
217  continue;
218 
219  // make old texture lowercase
220  strlwr(texture_file);
221 
222  // add it to the field
223  strcpy_s(old_texture_name[texture_count], texture_file);
224 
225  // increment
226  texture_count++;
227  }
228  }
229 
230  // now sort the filenames
231  sort_textures();
232 
233  // and add them to the box
234  for (i=0; i<texture_count; i++) {
235  z = box->AddString(old_texture_name[i]);
236  }
237 
238  // now look for new textures
240  {
241  if (!stricmp(Ships[self_ship].ship_name, ii->ship_name))
242  {
243  // look for corresponding old texture
244  for (i=0; i<texture_count; i++)
245  {
246  // if match
247  if (!stricmp(old_texture_name[i], ii->old_texture))
248  {
249  // assign new texture
250  strcpy_s(new_texture_name[i], ii->new_texture);
251 
252  // we found one, so no more to check
253  break;
254  }
255  }
256  }
257  }
258  // end of new texture check
259 
260  // set indexes and flags
261  m_old_texture_list = 0;
263  modified = 0;
264 
265  // display new texture, if we have one
266  m_new_texture = CString(new_texture_name[0]);
267 
268  CDialog::OnInitDialog();
269  UpdateData(FALSE);
270 
271  return TRUE; // return TRUE unless you set the focus to a control
272  // EXCEPTION: OCX Property Pages should return FALSE
273 }
274 
275 BOOL CShipTexturesDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
276 {
277  return CDialog::Create(IDD, pParentWnd);
278 }
279 
281 {
282  int z;
283 
284  if (query_modified()) {
285  z = MessageBox("Do you want to keep your changes?", "Close", MB_ICONQUESTION | MB_YESNOCANCEL);
286  if (z == IDCANCEL)
287  return;
288 
289  if (z == IDYES) {
290  OnOK();
291  return;
292  }
293  }
294 
295  CDialog::OnClose();
296 }
297 
298 int CShipTexturesDlg::query_modified()
299 {
300  return modified;
301 }
302 
304 {
305  UpdateData(TRUE);
306 
307  char *p;
308 
309  // see if we edited anything
310  if (stricmp(new_texture_name[active_texture_index], m_new_texture))
311  {
312  // assign it
313  strcpy_s(new_texture_name[active_texture_index], m_new_texture);
314 
315  // make it lowercase
316  strlwr(new_texture_name[active_texture_index]);
317 
318  // get rid of file extension
319  p = strchr( new_texture_name[active_texture_index], '.' );
320  if ( p )
321  {
322  mprintf(( "ignoring extension on file '%s'\n", new_texture_name[active_texture_index] ));
323  *p = 0;
324  }
325 
326  // set modified flag
327  modified = 1;
328  }
329 
330  // bring active texture index up to date
331  active_texture_index = m_old_texture_list;
332 
333  // display appropriate texture
334  m_new_texture = CString(new_texture_name[active_texture_index]);
335 
336  UpdateData(FALSE);
337 }
338 
339 // bubble sort
340 void CShipTexturesDlg::sort_textures(int test)
341 {
342  int i, j, str_check = 0;
343 
344  for (i = 0; i < texture_count; i++)
345  {
346  for (j = 0; j < i; j++)
347  {
348  switch(test)
349  {
350  case SORT_OLD:
351  str_check = stricmp(old_texture_name[i], old_texture_name[j]);
352  break;
353  case SORT_NEW:
354  str_check = stricmp(new_texture_name[i], new_texture_name[j]);
355  break;
356  default:
357  Int3();
358  }
359 
360  if (str_check < 0)
361  {
362  // swap old
363  swap_strings(old_texture_name[i], old_texture_name[j]);
364 
365  // swap new
366  swap_strings(new_texture_name[i], new_texture_name[j]);
367  }
368  }
369  }
370 }
371 
372 void CShipTexturesDlg::swap_strings(char *str1, char *str2)
373 {
374 /*
375  char *temp;
376  temp = str1;
377  str1 = str2;
378  str2 = temp;
379 */
380 
381  char temp[256];
382  strcpy_s(temp, str1);
383  strcpy(str1, str2);
384  strcpy(str2, temp);
385 }
386 
387 texture_replace *CShipTexturesDlg::texture_set(texture_replace *dest, const texture_replace *src)
388 {
389  dest->new_texture_id = src->new_texture_id;
390  strcpy_s(dest->ship_name, src->ship_name);
391  strcpy_s(dest->old_texture, src->old_texture);
392  strcpy_s(dest->new_texture, src->new_texture);
393 
394  return dest;
395 }
#define MAX_FILENAME_LEN
Definition: pstypes.h:324
#define MB_OKCANCEL
Definition: config.h:180
int i
Definition: multi_pxo.cpp:466
int n_textures
Definition: model.h:761
afx_msg void OnSelchangeOldTextureList()
afx_msg void OnClose()
#define SORT_NEW
polymodel * model_get(int model_num)
Definition: modelread.cpp:3134
char ship_name[NAME_LENGTH]
Definition: missionparse.h:324
CShipTexturesDlg(CWnd *pParent=NULL)
#define IDC_NEW_TEXTURE
Definition: resource.h:1065
#define mprintf(args)
Definition: pstypes.h:238
#define MB_ICONQUESTION
Definition: config.h:188
#define TRUE
Definition: pstypes.h:399
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: Glext.h:5156
#define MB_YESNOCANCEL
Definition: config.h:183
unsigned int UINT
Definition: config.h:82
#define Int3()
Definition: pstypes.h:292
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: Glext.h:7308
#define TM_NUM_TYPES
Definition: model.h:663
int bm_load_animation(const char *real_filename, int *nframes, int *fps, int *keyframe, int can_drop_frames, int dir_type)
Loads a bitmap sequance so we can draw with it.
Definition: bmpman.cpp:1420
#define SORT_OLD
virtual void DoDataExchange(CDataExchange *pDX)
#define MB_DEFBUTTON2
Definition: config.h:193
texture_map maps[MAX_MODEL_TEXTURES]
Definition: model.h:762
virtual void OnOK()
sprintf(buf,"(%f,%f,%f)", v3->xyz.x, v3->xyz.y, v3->xyz.z)
const char * bm_get_filename(int handle)
Gets the filename of the bitmap indexed by handle, which must exist.
Definition: bmpman.cpp:727
GLdouble GLdouble z
Definition: Glext.h:5451
unsigned long DWORD
Definition: config.h:90
int GetOriginalTexture()
int BOOL
Definition: config.h:80
int bm_load(const char *real_filename)
Loads a bitmap so we can draw with it later.
Definition: bmpman.cpp:1119
ship Ships[MAX_SHIPS]
Definition: ship.cpp:122
#define MAX_REPLACEMENT_TEXTURES
Definition: model.h:690
#define MB_ICONEXCLAMATION
Definition: config.h:184
#define IDC_OLD_TEXTURE_LIST
Definition: resource.h:1067
char old_texture[MAX_FILENAME_LEN]
Definition: missionparse.h:325
GLfloat GLfloat p
Definition: Glext.h:8373
texture_info textures[TM_NUM_TYPES]
Definition: model.h:671
GLenum src
Definition: Glext.h:5917
SCP_vector< ship_info > Ship_info
Definition: ship.cpp:164
int MessageBox(HWND h, const char *s1, const char *s2, int i)
virtual BOOL OnInitDialog()
int temp
Definition: lua.cpp:4996
polymodel * pm
Definition: lua.cpp:1598
SCP_vector< texture_replace > Fred_texture_replacements
int model_num
Definition: lua.cpp:4996
void strlwr(char *s)
#define FALSE
Definition: pstypes.h:400
#define stricmp(s1, s2)
Definition: config.h:271
char ship_name[NAME_LENGTH]
Definition: ship.h:604
GLuint GLuint end
Definition: Gl.h:1502
char new_texture[MAX_FILENAME_LEN]
Definition: missionparse.h:326
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID, CCreateContext *pContext=NULL)
#define strcpy_s(...)
Definition: safe_strings.h:67