Debian Codegraph @

editor/MegacellDialog.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // MegacellDialog.cpp : implementation file
20 //
21
22 #include <filesystem>
23
24 #include "stdafx.h"
25 #include "editor.h"
26 #include "MegacellDialog.h"
27 #include "WorldTexturesDialog.h"
28 #include "megapage.h"
29 #include "megacell.h"
30 #include "manage.h"
31 #include "gametexture.h"
32 #include "vclip.h"
33 #include "texpage.h"
34 #include "bitmap.h"
35 #include "ddio.h"
36 #include "mem.h"
37 #include "EditLineDialog.h"
38
39 #ifdef _DEBUG
40 #define new DEBUG_NEW
41 #undef THIS_FILE
42 static char THIS_FILE[] = __FILE__;
43 #endif
44
45 static megacell m_CopyMegacell;
46
47 /////////////////////////////////////////////////////////////////////////////
48 // CMegacellDialog dialog
49
50 CMegacellDialog::CMegacellDialog(CWnd *pParent /*=NULL*/) : CDialog(CMegacellDialog::IDD, pParent) {
51 //{{AFX_DATA_INIT(CMegacellDialog)
52 // NOTE: the ClassWizard will add member initialization here
53 //}}AFX_DATA_INIT
54
55 m_TexStart = 0;
56 m_TexListPos = 0;
57 m_RedrawListbox = 0;
58 m_TexPageLen = 0;
59 m_CheckForTextureList = 0;
60 m_CheckX = m_CheckY = 0;
61 memset(&m_CopyMegacell, 0, sizeof(megacell));
62 }
63
64 void CMegacellDialog::DoDataExchange(CDataExchange *pDX) {
65 CDialog::DoDataExchange(pDX);
66 //{{AFX_DATA_MAP(CMegacellDialog)
67 // NOTE: the ClassWizard will add DDX and DDV calls here
68 //}}AFX_DATA_MAP
69 }
70
71 BEGIN_MESSAGE_MAP(CMegacellDialog, CDialog)
72 //{{AFX_MSG_MAP(CMegacellDialog)
73 ON_BN_CLICKED(IDC_NEW_MEGACELL, OnNewMegacell)
74 ON_BN_CLICKED(IDC_LOCK_MEGACELL, OnLockMegacell)
75 ON_BN_CLICKED(IDC_CHECKIN_MEGACELL, OnCheckinMegacell)
76 ON_BN_CLICKED(IDC_DELETE_MEGACELL, OnDeleteMegacell)
77 ON_BN_CLICKED(IDC_NEXT_MEGACELL, OnNextMegacell)
78 ON_BN_CLICKED(IDC_PREVIOUS_MEGACELL, OnPreviousMegacell)
79 ON_CBN_SELENDOK(IDC_MEGACELL_PULLDOWN, OnSelendokMegacellPulldown)
80 ON_BN_CLICKED(IDC_MEGACELLS_OUT, OnMegacellsOut)
81 ON_EN_KILLFOCUS(IDC_MEGACELL_NAME_EDIT, OnKillfocusMegacellNameEdit)
82 ON_EN_KILLFOCUS(IDC_MEGA_WIDTH_EDIT, OnKillfocusMegaWidthEdit)
83 ON_EN_KILLFOCUS(IDC_MEGA_HEIGHT_EDIT, OnKillfocusMegaHeightEdit)
84 ON_WM_LBUTTONDOWN()
85 ON_WM_VSCROLL()
86 ON_WM_PAINT()
87 ON_BN_CLICKED(IDC_REMAP_MEGACELL, OnRemapMegacell)
88 ON_BN_CLICKED(IDC_ROTATE_MEGACELL, OnRotateMegacell)
89 ON_BN_CLICKED(IDC_COPY_MEGACELL, OnCopyMegacell)
90 ON_BN_CLICKED(IDC_PASTE_MEGACELL, OnPasteMegacell)
91 ON_BN_CLICKED(IDC_IMPORT_TINY, OnImportTiny)
92 ON_WM_HELPINFO()
93 ON_BN_CLICKED(IDC_IMPORT_SKY, OnImportSky)
94 ON_BN_CLICKED(IDC_IMPORT_SKY_BAND, OnImportSkyBand)
95 //}}AFX_MSG_MAP
96 END_MESSAGE_MAP()
97
98 /////////////////////////////////////////////////////////////////////////////
99 // CMegacellDialog message handlers
100
101 void CMegacellDialog::OnNewMegacell() {
102 char filename[255];
103 char cur_name[100];
104 int cell_handle;
105 int c = 1, finding_name = 1;
106
107 if (!Network_up) {
108 OutrageMessageBox("Sorry babe, the network is down. This action is a no-no.\n");
109 return;
110 }
111
112 // Get megacell name
113
114 CEditLineDialog dlg("Enter megacell name", this);
115 int ret = dlg.DoModal();
116
117 if (ret == IDCANCEL)
118 return;
119
120 strcpy(filename, (char *)dlg.GetText());
121
122 // Alloc a megacell and give a name not already taken by some other cell
123
124 cell_handle = AllocMegacell();
125
126 while (finding_name) {
127 sprintf(cur_name, "%s%d", filename, c);
128 if (FindMegacellName(cur_name) != -1)
129 c++;
130 else
131 finding_name = 0;
132 }
133
134 strcpy(Megacells[cell_handle].name, cur_name);
135
136 mng_AllocTrackLock(cur_name, PAGETYPE_MEGACELL);
137
138 D3EditState.current_megacell = cell_handle;
139
140 UpdateDialog();
141 }
142
143 void CMegacellDialog::OnLockMegacell() {
144 int n = D3EditState.current_megacell;
145 mngs_Pagelock temp_pl;
146 mngs_megacell_page megacellpage;
147 int r;
148
149 if (Num_megacells < 1)
150 return;
151
152 if (!mng_MakeLocker())
153 return;
154
155 // Make sure it can be locked
156 strcpy(temp_pl.name, Megacells[n].name);
157 temp_pl.pagetype = PAGETYPE_MEGACELL;
158
159 r = mng_CheckIfPageLocked(&temp_pl);
160 if (r == 2) {
161 int answer;
162 answer = OutrageMessageBox(MBOX_YESNO, "This page is not even in the table file, or the database maybe corrupt. "
163 "Override to 'Unlocked'? (Select NO if you don't know what you're doing)");
164 if (answer == IDYES) {
165 strcpy(temp_pl.holder, "UNLOCKED");
166 if (!mng_ReplacePagelock(temp_pl.name, &temp_pl))
167 MessageBox(ErrorString, "Error!");
168 }
169
170 } else if (r < 0)
171 OutrageMessageBox(ErrorString);
172 else if (r == 1)
173 OutrageMessageBox(InfoString);
174 else {
175 // Everything is ok. Tell the network we're locking it and get a copy to
176 // our local drive
177
178 strcpy(temp_pl.holder, TableUser);
179 if (!mng_ReplacePagelock(temp_pl.name, &temp_pl)) {
180 MessageBox(ErrorString, "Error!");
181 mng_EraseLocker();
182 return;
183 } else {
184 // Search thru the net pagefile and get a new copy in RAM in case anyone
185 // changed it since we started the editor
186 if (mng_FindSpecificMegacellPage(temp_pl.name, &megacellpage, 0)) {
187 if (mng_AssignMegacellPageToMegacell(&megacellpage, n)) {
188
189 if (!mng_ReplacePage(Megacells[n].name, Megacells[n].name, n, PAGETYPE_MEGACELL, 1)) {
190 OutrageMessageBox("There was problem writing that page locally!");
191 mng_EraseLocker();
192 return;
193 }
194 OutrageMessageBox("Megacell locked.");
195
196 } else
197 OutrageMessageBox("There was a problem loading this megacell. You might encounter problems in dealing with "
198 "it. Good luck!");
199
200 mng_AllocTrackLock(Megacells[n].name, PAGETYPE_MEGACELL);
201 UpdateDialog();
202 } else
203 OutrageMessageBox("Couldn't find that megacell in the table file!");
204 }
205 }
206 mng_EraseLocker();
207 }
208
209 void CMegacellDialog::OnCheckinMegacell() {
210 int n = D3EditState.current_megacell;
211 mngs_Pagelock temp_pl;
212 int r;
213 int i;
214
215 if (Num_megacells < 1)
216 return;
217
218 if (!mng_MakeLocker())
219 return;
220
221 // Make sure we own this megacell
222 strcpy(temp_pl.name, Megacells[n].name);
223 temp_pl.pagetype = PAGETYPE_MEGACELL;
224
225 r = mng_CheckIfPageOwned(&temp_pl, TableUser);
226 if (r < 0)
227 OutrageMessageBox(ErrorString);
228 else if (r == 0)
229 OutrageMessageBox(InfoString);
230 else {
231 // Change the pagelock state to UNLOCKED
232 strcpy(temp_pl.holder, "UNLOCKED");
233 if (!mng_ReplacePagelock(temp_pl.name, &temp_pl)) {
234 MessageBox(ErrorString, "Error!");
235 mng_EraseLocker();
236 return;
237 } else {
238 // Now actually replace the copy on the net with our local one
239
240 int answer = MessageBox("Do you wish to check in all the textures associated with this megacell (if any)?",
241 Megacells[n].name, MB_YESNO);
242
243 if (answer == IDYES) {
244 // Check in all these textures
245 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) {
246 int mx = i % MAX_MEGACELL_WIDTH;
247 int my = i / MAX_MEGACELL_WIDTH;
248
249 if (mx >= Megacells[n].width || my >= Megacells[n].height)
250 continue;
251
252 int index = Megacells[n].texture_handles[i];
253 if (!GameTextures[index].used || index == 0)
254 continue;
255 int tl;
256 if ((tl = mng_FindTrackLock(GameTextures[index].name, PAGETYPE_TEXTURE)) != -1)
257 CheckinTexture(index, tl);
258 }
259 }
260
261 if (!mng_ReplacePage(Megacells[n].name, Megacells[n].name, n, PAGETYPE_MEGACELL, 0))
262 OutrageMessageBox(ErrorString);
263 else {
264 OutrageMessageBox("Megacell checked in.");
265
266 // Delete it from local pagefile if its there
267 int dret = mng_DeletePage(Megacells[n].name, PAGETYPE_MEGACELL, 1);
268 ASSERT(dret == 1);
269 mng_EraseLocker();
270
271 // Free the tracklock
272 int p = mng_FindTrackLock(Megacells[n].name, PAGETYPE_MEGACELL);
273 ASSERT(p != -1);
274 mng_FreeTrackLock(p);
275 UpdateDialog();
276 }
277 }
278 }
279 }
280
281 extern int TlockState;
282 extern int AutoChecking;
283
284 void CMegacellDialog::OnDeleteMegacell() {
285 int answer, tl;
286 mngs_Pagelock pl;
287 CWorldTexturesDialog texdlg;
288
289 int n = D3EditState.current_megacell;
290 int i;
291
292 if (Num_megacells < 1)
293 return;
294
295 // Check to see if we even have it locked
296 if ((tl = mng_FindTrackLock(Megacells[n].name, PAGETYPE_MEGACELL)) == -1) {
297 OutrageMessageBox("This megacell is not yours to delete. Lock first.");
298 return;
299 }
300
301 // Make sure its to be deleted
302 answer = MessageBox("Are you sure you want to delete this megacell?", Megacells[n].name, MB_YESNO);
303 if (answer == IDNO)
304 return;
305
306 answer =
307 MessageBox("Do you wish to delete all the textures associated with this megacell?", Megacells[n].name, MB_YESNO);
308
309 if (!mng_MakeLocker())
310 return;
311
312 strcpy(pl.name, Megacells[n].name);
313 pl.pagetype = PAGETYPE_MEGACELL;
314
315 // Check to see if this is a local megacell only. If so, only delete it locally
316 if (mng_CheckIfPageOwned(&pl, TableUser) != 1) {
317 mng_FreeTrackLock(tl);
318 if (!mng_DeletePage(Megacells[n].name, PAGETYPE_MEGACELL, 1)) {
319 mprintf(0, ErrorString);
320 Int3();
321 }
322 } else // if its network, delete it from both the net and local drives
323 {
324
325 mng_FreeTrackLock(tl);
326
327 mng_DeletePage(Megacells[n].name, PAGETYPE_MEGACELL, 0);
328 mng_DeletePage(Megacells[n].name, PAGETYPE_MEGACELL, 1);
329
330 mng_DeletePagelock(Megacells[n].name, PAGETYPE_MEGACELL);
331 }
332
333 // Advance to the next megacell
334 D3EditState.current_megacell = GetNextMegacell(n);
335
336 // Now delete those textures if need be
337 if (answer == IDYES) {
338 uint8_t can_delete[MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT];
339
340 memset(can_delete, 0, MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT);
341
342 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) {
343 int mx = i % MAX_MEGACELL_WIDTH;
344 int my = i / MAX_MEGACELL_WIDTH;
345
346 if (mx >= Megacells[n].width || my >= Megacells[n].height)
347 continue;
348
349 int index = Megacells[n].texture_handles[i];
350
351 if (index == 0 || index == 1)
352 continue;
353
354 int tl;
355
356 tl = mng_FindTrackLock(GameTextures[index].name, PAGETYPE_TEXTURE);
357
358 if (tl == -1) {
359 // We need to lock this texture
360 AutoChecking = 2;
361 D3EditState.texdlg_texture = index;
362 texdlg.OnLock();
363 if (!TlockState)
364 continue;
365 tl = mng_FindTrackLock(GameTextures[index].name, PAGETYPE_TEXTURE);
366 can_delete[i] = 1;
367 } else {
368 can_delete[i] = 1;
369 }
370 }
371
372 int num_to_delete = 0;
373 char *texnames[MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT];
374 uint16_t texindices[MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT];
375
376 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++) {
377 texnames[i] = (char *)mem_malloc(PAGENAME_LEN);
378 ASSERT(texnames[i]);
379 if (!can_delete[i])
380 continue;
381
382 int index = Megacells[n].texture_handles[i];
383
384 int duplicate = 0;
385 for (int k = 0; k < num_to_delete && !duplicate; k++) {
386 if (texindices[k] == index)
387 duplicate = 1;
388 }
389
390 if (duplicate)
391 continue;
392
393 strcpy(texnames[num_to_delete], GameTextures[index].name);
394 texindices[num_to_delete] = index;
395 num_to_delete++;
396 }
397
398 mng_DeleteTexPageSeries(texnames, num_to_delete, 0);
399 mng_DeleteTexPageSeries(texnames, num_to_delete, 1);
400 mng_DeletePagelockSeries(texnames, num_to_delete, PAGETYPE_TEXTURE);
401
402 // Now free all pagelocks and textures
403 for (i = 0; i < num_to_delete; i++) {
404 tl = mng_FindTrackLock(GameTextures[texindices[i]].name, PAGETYPE_TEXTURE);
405
406 if (tl != -1)
407 mng_FreeTrackLock(tl);
408
409 if (GameTextures[texindices[i]].flags & TF_ANIMATED)
410 FreeVClip(GameTextures[texindices[i]].bm_handle);
411 else
412 bm_FreeBitmap(GameTextures[texindices[i]].bm_handle);
413
414 FreeTexture(texindices[i]);
415 }
416
417 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++)
418 mem_free(texnames[i]);
419 }
420
421 AutoChecking = 0;
422 FreeMegacell(n);
423 mng_EraseLocker();
424
425 OutrageMessageBox("Megacell deleted.");
426
427 UpdateDialog();
428 }
429
430 void CMegacellDialog::OnNextMegacell() {
431 D3EditState.current_megacell = GetNextMegacell(D3EditState.current_megacell);
432 UpdateDialog();
433 }
434
435 void CMegacellDialog::OnPreviousMegacell() {
436 D3EditState.current_megacell = GetPrevMegacell(D3EditState.current_megacell);
437 UpdateDialog();
438 }
439
440 void CMegacellDialog::OnSelendokMegacellPulldown() {
441 int i, cur;
442 char name[200];
443
444 cur = SendDlgItemMessage(IDC_MEGACELL_PULLDOWN, CB_GETCURSEL, 0, 0);
445 SendDlgItemMessage(IDC_MEGACELL_PULLDOWN, CB_GETLBTEXT, cur, (LPARAM)(LPCTSTR)name);
446
447 i = FindMegacellName(name);
448
449 if (i == -1) {
450 mprintf(0, "Possible corrupted megacell list, but probably nothing.");
451 UpdateDialog();
452 }
453
454 D3EditState.current_megacell = i;
455 UpdateDialog();
456 }
457
458 void CMegacellDialog::OnMegacellsOut() {}
459
460 void CMegacellDialog::OnKillfocusMegacellNameEdit() {
461 CEdit *ebox;
462 int n = D3EditState.current_megacell;
463 char name[PAGENAME_LEN];
464 mngs_Pagelock pl;
465
466 // Make sure we have this megacell locked, if not reset name and bail
467 int p = mng_FindTrackLock(Megacells[n].name, PAGETYPE_MEGACELL);
468 if (p == -1) {
469 OutrageMessageBox("You must lock this megacell if you wish to change its name.");
470 CEdit *ebox = (CEdit *)GetDlgItem(IDC_MEGACELL_NAME_EDIT);
471 ebox->SetWindowText(Megacells[n].name);
472 return;
473 }
474
475 // Get the new name
476 ebox = (CEdit *)GetDlgItem(IDC_MEGACELL_NAME_EDIT);
477 ebox->GetWindowText(name, PAGENAME_LEN);
478
479 // Check to see if there is already a cell with this name. If so, reset and bail
480 if (FindMegacellName(name) != -1) {
481 OutrageMessageBox("There already is a cell with that name...choose another name.");
482
483 CEdit *ebox = (CEdit *)GetDlgItem(IDC_MEGACELL_NAME_EDIT);
484 ebox->SetWindowText(Megacells[n].name);
485 return;
486 }
487
488 if (!mng_MakeLocker())
489 return;
490
491 // Check to see if this page exists on the network. If so, we have to
492 // rename it so we can prevent havoc
493 strcpy(pl.name, Megacells[n].name);
494 pl.pagetype = PAGETYPE_MEGACELL;
495
496 int ret = mng_CheckIfPageOwned(&pl, TableUser);
497 if (ret < 0)
498 OutrageMessageBox(ErrorString);
499 else if (ret == 1)
500 mng_RenamePage(Megacells[n].name, name, PAGETYPE_MEGACELL);
501 else if (ret == 2) {
502 // This page has never been checked in, replace only local copy
503
504 char oldname[PAGENAME_LEN];
505 strcpy(oldname, Megacells[n].name);
506 strcpy(Megacells[n].name, name);
507
508 mng_ReplacePage(oldname, Megacells[n].name, n, PAGETYPE_MEGACELL, 1);
509 } else if (ret == 0) {
510 OutrageMessageBox("You don't own this page. Get Jason now!");
511 mng_FreeTrackLock(p);
512 return;
513 }
514
515 // Finally, copy our new name to the appropriate arrays
516 strcpy(GlobalTrackLocks[p].name, name);
517 strcpy(Megacells[n].name, name);
518 mng_EraseLocker();
519
520 UpdateDialog();
521 }
522
523 void CMegacellDialog::OnKillfocusMegaWidthEdit() {
524 CEdit *ebox;
525 char name[20];
526 int n = D3EditState.current_megacell;
527 int width;
528
529 ebox = (CEdit *)GetDlgItem(IDC_MEGA_WIDTH_EDIT);
530 ebox->GetWindowText(name, 20);
531
532 width = atoi(name);
533
534 if (width < 0)
535 width = 2;
536 if (width > 8)
537 width = 8;
538 Megacells[n].width = width;
539
540 UpdateDialog();
541 }
542
543 void CMegacellDialog::OnKillfocusMegaHeightEdit() {
544 CEdit *ebox;
545 char name[20];
546 int n = D3EditState.current_megacell;
547 int height;
548
549 ebox = (CEdit *)GetDlgItem(IDC_MEGA_HEIGHT_EDIT);
550 ebox->GetWindowText(name, 20);
551
552 height = atoi(name);
553
554 if (height < 0)
555 height = 2;
556 if (height > 8)
557 height = 8;
558 Megacells[n].height = height;
559
560 UpdateDialog();
561 }
562
563 BOOL CMegacellDialog::DestroyWindow() {
564 SaveMegacellsOnClose();
565
566 return CDialog::DestroyWindow();
567 }
568
569 BOOL CMegacellDialog::OnInitDialog() {
570 static int first_time = 1;
571 CDialog::OnInitDialog();
572
573 mngs_megacell_page megacellpage;
574
575 // Remap all megacells
576 if (Network_up && first_time) {
577 for (int i = 0; i < MAX_MEGACELLS; i++) {
578 if (Megacells[i].used) {
579 if (Megacells[i].texture_handles[0] > 0)
580 continue;
581
582 if (mng_FindSpecificMegacellPage(Megacells[i].name, &megacellpage, 1)) {
583 if (!mng_AssignMegacellPageToMegacell(&megacellpage, i))
584 Int3(); // Get Jason
585 } else if (mng_FindSpecificMegacellPage(Megacells[i].name, &megacellpage, 0)) {
586 if (!mng_AssignMegacellPageToMegacell(&megacellpage, i))
587 Int3(); // Get Jason
588 }
589 }
590 }
591 }
592
593 first_time = 0;
594 UpdateDialog();
595
596 return TRUE; // return TRUE unless you set the focus to a control
597 // EXCEPTION: OCX Property Pages should return FALSE
598 }
599
600 void CMegacellDialog::SaveMegacellsOnClose() {
601 int i, t;
602 if (!Network_up)
603 return; // don't save a damn thing if the network is down
604
605 for (i = 0; i < MAX_TRACKLOCKS; i++) {
606 if (GlobalTrackLocks[i].used == 1 && GlobalTrackLocks[i].pagetype == PAGETYPE_TEXTURE) {
607 t = FindTextureName(GlobalTrackLocks[i].name);
608 ASSERT(t != -1);
609
610 mng_ReplacePage(GameTextures[t].name, GameTextures[t].name, t, PAGETYPE_TEXTURE, 1);
611 }
612
613 if (GlobalTrackLocks[i].used == 1 && GlobalTrackLocks[i].pagetype == PAGETYPE_MEGACELL) {
614 t = FindMegacellName(GlobalTrackLocks[i].name);
615
616 mng_DeletePage(Megacells[t].name, PAGETYPE_MEGACELL, 1);
617 ASSERT(t != -1);
618
619 mng_ReplacePage(Megacells[t].name, Megacells[t].name, t, PAGETYPE_MEGACELL, 1);
620 }
621 }
622 }
623
624 void CMegacellDialog::OnLButtonDown(UINT nFlags, CPoint point) {
625 m_CheckForTextureList = 1;
626 m_CheckX = point.x;
627 m_CheckY = point.y;
628
629 CDialog::OnLButtonDown(nFlags, point);
630
631 UpdateDialog();
632
633 CDialog::OnLButtonDown(nFlags, point);
634 }
635 void CMegacellDialog::UpdateDialog() {
636 CEdit *ebox;
637 CButton *bbox;
638 int n = D3EditState.current_megacell;
639 char str[50];
640
641 UpdateTextureView();
642
643 if (Num_megacells < 1) {
644 bbox = (CButton *)GetDlgItem(IDC_NEXT_MEGACELL);
645 bbox->EnableWindow(FALSE);
646 bbox = (CButton *)GetDlgItem(IDC_PREVIOUS_MEGACELL);
647 bbox->EnableWindow(FALSE);
648
649 m_CheckForTextureList = 0;
650
651 return;
652 } else {
653 bbox = (CButton *)GetDlgItem(IDC_NEXT_MEGACELL);
654 bbox->EnableWindow(TRUE);
655 bbox = (CButton *)GetDlgItem(IDC_PREVIOUS_MEGACELL);
656 bbox->EnableWindow(TRUE);
657 }
658
659 if (!Megacells[n].used)
660 n = D3EditState.current_megacell = GetNextMegacell(n);
661
662 ebox = (CEdit *)GetDlgItem(IDC_MEGACELL_NAME_EDIT);
663 ebox->SetWindowText(Megacells[n].name);
664
665 ebox = (CEdit *)GetDlgItem(IDC_MEGA_WIDTH_EDIT);
666 sprintf(str, "%d", Megacells[n].width);
667 ebox->SetWindowText(str);
668
669 ebox = (CEdit *)GetDlgItem(IDC_MEGA_HEIGHT_EDIT);
670 sprintf(str, "%d", Megacells[n].height);
671 ebox->SetWindowText(str);
672
673 bbox = (CButton *)GetDlgItem(IDC_CHECKIN_MEGACELL);
674 if (mng_FindTrackLock(Megacells[n].name, PAGETYPE_MEGACELL) == -1) {
675 bbox->EnableWindow(FALSE);
676 bbox = (CButton *)GetDlgItem(IDC_LOCK_MEGACELL);
677 bbox->EnableWindow(TRUE);
678 } else {
679 bbox->EnableWindow(TRUE);
680 bbox = (CButton *)GetDlgItem(IDC_LOCK_MEGACELL);
681 bbox->EnableWindow(FALSE);
682 }
683
684 SendDlgItemMessage(IDC_MEGACELL_PULLDOWN, CB_RESETCONTENT, 0, 0);
685
686 for (int i = 0; i < MAX_MEGACELLS; i++) {
687 if (Megacells[i].used)
688 SendDlgItemMessage(IDC_MEGACELL_PULLDOWN, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)Megacells[i].name);
689 }
690 SendDlgItemMessage(IDC_MEGACELL_PULLDOWN, CB_SELECTSTRING, 0, (LPARAM)(LPCTSTR)Megacells[n].name);
691
692 UpdateMegacellView();
693 }
694
695 #define TEXTURES_PER_ROW 4
696
697 void CMegacellDialog::UpdateTextureView() {
698 int n = D3EditState.texdlg_texture;
699 CWnd *texwnd;
700 RECT rect;
701 int x, y, bm_handle, w, h;
702 int update_listbox = 1;
703
704 Desktop_surf->attach_to_window((unsigned)m_hWnd);
705
706 if (1 || update_listbox) {
707
708 texwnd = GetDlgItem(IDC_TEXTURE_LISTBOX);
709 texwnd->GetWindowRect(&rect);
710 ScreenToClient(&rect);
711 w = rect.right - rect.left;
712 h = rect.bottom - rect.top;
713
714 int texwidth = (w - 1) / 4;
715 int texheight = (h - 1) / 8;
716
717 Desktop_surf->clear(rect.left, rect.top, w, h);
718
719 int texnum = m_TexStart;
720 int count = 0, done = 0;
721
722 while (!done) {
723 m_TextureSurf.create(texwidth, texheight, BPP_16);
724
725 bm_handle = GetTextureBitmap(texnum, 0);
726 m_TextureSurf.load(bm_handle);
727
728 x = 1 + rect.left + ((count % TEXTURES_PER_ROW) * texwidth);
729 y = 1 + rect.top + ((count / TEXTURES_PER_ROW) * texheight);
730
731 if (m_CheckForTextureList) {
732
733 if (m_CheckX >= x && m_CheckX <= (x + texwidth) && m_CheckY >= y && m_CheckY <= (y + texheight)) {
734 m_CheckForTextureList = 0;
735 D3EditState.texdlg_texture = texnum;
736 m_TextureSurf.free();
737 Desktop_surf->attach_to_window((unsigned)NULL);
738 UpdateDialog();
739 return;
740 }
741 }
742
743 if (texnum == n) // draw a white box if this is the current texture
744 {
745 grViewport *vport = new grViewport(&m_TextureSurf);
746
747 int x = 0, y = 0;
748
749 vport->lock();
750 vport->line(GR_RGB(255, 255, 255), x, y, x + vport->width() - 1, y);
751 vport->line(GR_RGB(255, 255, 255), x + vport->width() - 1, y, x + vport->width() - 1, y + vport->height() - 1);
752 vport->line(GR_RGB(255, 255, 255), x + vport->width() - 1, y + vport->height() - 1, x, y + vport->height() - 1);
753 vport->line(GR_RGB(255, 255, 255), x, y + vport->height() - 1, x, y);
754 vport->unlock();
755
756 delete vport;
757 }
758
759 Desktop_surf->blt(x, y, &m_TextureSurf);
760
761 texnum = GetNextTexture(texnum);
762 m_TextureSurf.free();
763 count++;
764 if (count == TEXTURES_PER_ROW * 8)
765 done = 1;
766 if (texnum <= m_TexStart)
767 done = 1;
768 }
769
770 // must define the vertical scroll bar dimensions
771 CScrollBar *bar = (CScrollBar *)GetDlgItem(IDC_TEXLIST_VERT);
772 SCROLLINFO si;
773
774 m_TexPageLen = (count / TEXTURES_PER_ROW) + ((count % TEXTURES_PER_ROW) ? 1 : 0);
775 m_TexListPos = bar->GetScrollPos();
776 si.cbSize = sizeof(si);
777 si.fMask = SIF_ALL;
778 si.nMin = 0;
779 si.nMax = (Num_textures / TEXTURES_PER_ROW) + ((Num_textures % TEXTURES_PER_ROW) ? 1 : 0) - 1;
780 si.nPos = m_TexListPos;
781 si.nPage = m_TexPageLen;
782 bar->SetScrollInfo(&si);
783 bar->ShowScrollBar(TRUE);
784 }
785
786 Desktop_surf->attach_to_window((unsigned)NULL);
787 }
788
789 void CMegacellDialog::TextureListUp(int rows) {
790 int old = m_TexStart;
791 int done = 0, count = 0;
792
793 while ((count < (TEXTURES_PER_ROW * rows)) && !done) {
794
795 m_TexStart = GetPreviousTexture(m_TexStart);
796
797 if (old <= m_TexStart) {
798 m_TexStart = old;
799 done = 1;
800 } else
801 count++;
802 }
803
804 m_RedrawListbox = 1;
805 UpdateDialog();
806 }
807
808 void CMegacellDialog::TextureListDown(int rows) {
809 int old = m_TexStart;
810 int done = 0, count = 0;
811
812 while ((count < (TEXTURES_PER_ROW * rows)) && !done) {
813 m_TexStart = GetNextTexture(m_TexStart);
814 if (old >= m_TexStart) {
815 m_TexStart = old;
816 done = 1;
817 } else
818 count++;
819 }
820
821 m_RedrawListbox = 1;
822 UpdateDialog();
823 }
824
825 void CMegacellDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar) {
826 int id = -1, min, max;
827 int new_pos = 0;
828
829 if (pScrollBar) {
830 id = pScrollBar->GetDlgCtrlID();
831 pScrollBar->GetScrollRange(&min, &max);
832 }
833
834 switch (id) {
835 case IDC_TEXLIST_VERT:
836 // vertically scroll the texture list box
837 switch (nSBCode) {
838 case SB_LINEDOWN:
839 new_pos = m_TexListPos + 1;
840 break;
841 case SB_LINEUP:
842 new_pos = m_TexListPos - 1;
843 break;
844 case SB_THUMBPOSITION:
845 new_pos = nPos;
846 break;
847 case SB_THUMBTRACK:
848 new_pos = nPos;
849 break;
850 case SB_PAGEDOWN:
851 new_pos = m_TexListPos + m_TexPageLen;
852 break;
853 case SB_PAGEUP:
854 new_pos = m_TexListPos - m_TexPageLen;
855 break;
856 default:
857 new_pos = m_TexListPos;
858 }
859
860 if (new_pos < 0)
861 new_pos = 0;
862 if (new_pos > (max - m_TexPageLen))
863 new_pos = (max - m_TexPageLen) + 1;
864 if (new_pos != m_TexListPos) {
865 // ok, new_pos will be an index into the listbox, NOT the texture list.
866 // so we will get the number of texture rows we went up or down and change
867 // tex_start accordingly.
868 mprintf(0, "scroll new_pos = %d. original_pos = %d\n", new_pos, m_TexListPos);
869 if (new_pos < m_TexListPos)
870 TextureListUp(abs(m_TexListPos - new_pos));
871 if (new_pos > m_TexListPos)
872 TextureListDown(abs(new_pos - m_TexListPos));
873
874 m_TexListPos = new_pos;
875
876 SCROLLINFO si;
877
878 si.cbSize = sizeof(si);
879 si.fMask = SIF_POS;
880 si.nPos = m_TexListPos;
881 pScrollBar->SetScrollInfo(&si);
882 }
883 break;
884
885 default:
886 Int3(); // WHAT THE FUCK!!
887 }
888
889 CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
890 }
891
892 void CMegacellDialog::UpdateMegacellView() {
893 int n = D3EditState.current_megacell;
894 CWnd *texwnd;
895 RECT rect;
896 int x, y, bm_handle, w, h;
897 int megawidth = Megacells[n].width;
898 int megaheight = Megacells[n].height;
899 int i, t;
900
901 Desktop_surf->attach_to_window((unsigned)m_hWnd);
902
903 texwnd = GetDlgItem(IDC_MEGACELL_VIEW);
904 texwnd->GetWindowRect(&rect);
905 ScreenToClient(&rect);
906 w = rect.right - rect.left;
907 h = rect.bottom - rect.top;
908
909 int texwidth = w / megawidth;
910 int texheight = h / megaheight;
911
912 // Don't have to clear if we're overwriting the whole area!
913 // if (megawidth<8 || megaheight<8)
914 // Desktop_surf->clear(rect.left,rect.top,w,h);
915
916 for (i = 0; i < megaheight; i++) {
917 for (t = 0; t < megawidth; t++) {
918 m_TextureSurf.create(texwidth, texheight, BPP_16);
919
920 bm_handle = GetTextureBitmap(Megacells[n].texture_handles[i * MAX_MEGACELL_WIDTH + t], 0);
921 m_TextureSurf.load(bm_handle);
922
923 x = 1 + rect.left + (t * texwidth);
924 y = 1 + rect.top + (i * texheight);
925
926 if (m_CheckForTextureList) {
927
928 if (m_CheckX >= x && m_CheckX <= (x + texwidth) && m_CheckY >= y && m_CheckY <= (y + texheight)) {
929 m_CheckForTextureList = 0;
930 Megacells[n].texture_handles[i * MAX_MEGACELL_WIDTH + t] = D3EditState.texdlg_texture;
931 m_TextureSurf.free();
932 Desktop_surf->attach_to_window((unsigned)NULL);
933 UpdateDialog();
934 return;
935 }
936 }
937
938 // Draw a separation line on the right and bottom side
939 {
940 grViewport *vport = new grViewport(&m_TextureSurf);
941
942 int x = 0, y = 0;
943
944 vport->lock();
945 vport->line(GR_RGB(0, 0, 0), 0, vport->height() - 1, vport->width() - 1, vport->height() - 1);
946 vport->line(GR_RGB(0, 0, 0), vport->width() - 1, 0, vport->width() - 1, vport->height() - 1);
947
948 vport->unlock();
949 delete vport;
950 }
951
952 Desktop_surf->blt(x, y, &m_TextureSurf);
953
954 m_TextureSurf.free();
955 }
956 }
957
958 m_CheckForTextureList = 0;
959 Desktop_surf->attach_to_window((unsigned)NULL);
960 }
961
962 void CMegacellDialog::OnPaint() {
963 CPaintDC dc(this); // device context for painting
964
965 UpdateDialog();
966 // Do not call CDialog::OnPaint() for painting messages
967 }
968
969 void CMegacellDialog::OnRemapMegacell() {
970 int n = D3EditState.current_megacell;
971 mngs_megacell_page megacellpage;
972
973 if (mng_FindSpecificMegacellPage(Megacells[n].name, &megacellpage, 1)) {
974 if (!mng_AssignMegacellPageToMegacell(&megacellpage, n))
975 Int3(); // Get Jason
976 } else if (mng_FindSpecificMegacellPage(Megacells[n].name, &megacellpage, 0)) {
977 if (!mng_AssignMegacellPageToMegacell(&megacellpage, n))
978 Int3(); // Get Jason
979 } else
980 OutrageMessageBox("This megacell was not found in the local table file.");
981
982 UpdateDialog();
983 }
984
985 void CMegacellDialog::OnRotateMegacell() {
986 int n = D3EditState.current_megacell;
987 int buffer[MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT];
988 int i;
989
990 if (!Megacells[n].used)
991 return;
992
993 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++)
994 buffer[i] = 0;
995
996 int oldwidth = Megacells[n].width;
997 int oldheight = Megacells[n].height;
998
999 for (i = 0; i < MAX_MEGACELL_HEIGHT; i++) {
1000 for (int t = 0; t < MAX_MEGACELL_WIDTH; t++) {
1001 buffer[((MAX_MEGACELL_HEIGHT - 1) - t) * MAX_MEGACELL_WIDTH + i] =
1002 Megacells[n].texture_handles[i * MAX_MEGACELL_WIDTH + t];
1003 }
1004 }
1005
1006 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++)
1007 Megacells[n].texture_handles[i] = buffer[i];
1008
1009 Megacells[n].width = oldheight;
1010 Megacells[n].height = oldwidth;
1011
1012 UpdateDialog();
1013 }
1014
1015 void CMegacellDialog::OnCopyMegacell() {
1016 int n = D3EditState.current_megacell;
1017 int i;
1018
1019 if (!Megacells[n].used)
1020 return;
1021
1022 m_CopyMegacell.width = Megacells[n].width;
1023 m_CopyMegacell.height = Megacells[n].height;
1024
1025 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++)
1026 m_CopyMegacell.texture_handles[i] = Megacells[n].texture_handles[i];
1027
1028 mprintf(0, "Megacell copied.\n");
1029 }
1030
1031 void CMegacellDialog::OnPasteMegacell() {
1032 int n = D3EditState.current_megacell;
1033 int i;
1034
1035 if (!Megacells[n].used)
1036 return;
1037
1038 Megacells[n].width = m_CopyMegacell.width;
1039 Megacells[n].height = m_CopyMegacell.height;
1040
1041 for (i = 0; i < MAX_MEGACELL_WIDTH * MAX_MEGACELL_HEIGHT; i++)
1042 Megacells[n].texture_handles[i] = m_CopyMegacell.texture_handles[i];
1043
1044 UpdateDialog();
1045 }
1046
1047 void CMegacellDialog::CheckinTexture(int n, int tracklock_num) {
1048 mngs_Pagelock temp_pl;
1049 int r;
1050
1051 if (!GameTextures[n].used) {
1052 mprintf(0, "Whoa, trying to save non-used texture %s!\n", GameTextures[n].name);
1053 return;
1054 }
1055
1056 strcpy(temp_pl.name, GameTextures[n].name);
1057 temp_pl.pagetype = PAGETYPE_TEXTURE;
1058
1059 r = mng_CheckIfPageOwned(&temp_pl, TableUser);
1060 if (r < 0)
1061 OutrageMessageBox(ErrorString);
1062 else if (r == 0)
1063 OutrageMessageBox(InfoString);
1064 else {
1065 strcpy(temp_pl.holder, "UNLOCKED");
1066 if (!mng_ReplacePagelock(temp_pl.name, &temp_pl)) {
1067 OutrageMessageBox(ErrorString, "Error!");
1068 mng_EraseLocker();
1069 return;
1070 } else {
1071 if (!mng_ReplacePage(GameTextures[n].name, GameTextures[n].name, n, PAGETYPE_TEXTURE, 0))
1072 OutrageMessageBox(ErrorString);
1073 else {
1074 // Save this textures bitmap to the network for all
1075
1076 std::filesystem::path fname;
1077
1078 if (GameTextures[n].flags & TF_ANIMATED) {
1079 fname = ManageGraphicsDir / GameVClips[GameTextures[n].bm_handle].name;
1080 SaveVClip(fname, GameTextures[n].bm_handle);
1081
1082 fname = LocalManageGraphicsDir / GameVClips[GameTextures[n].bm_handle].name;
1083 SaveVClip(fname, GameTextures[n].bm_handle);
1084 } else {
1085 fname = ManageGraphicsDir / GameBitmaps[GameTextures[n].bm_handle].name;
1086 bm_SaveFileBitmap(fname, GameTextures[n].bm_handle);
1087
1088 fname = LocalManageGraphicsDir / GameBitmaps[GameTextures[n].bm_handle].name;
1089 bm_SaveFileBitmap(fname, GameTextures[n].bm_handle);
1090 }
1091
1092 mprintf(0, "%s texture checked in.\n", GameTextures[n].name);
1093 int dret = mng_DeletePage(GameTextures[n].name, PAGETYPE_TEXTURE, 1);
1094
1095 ASSERT(dret == 1);
1096
1097 mng_FreeTrackLock(tracklock_num);
1098 }
1099 }
1100 }
1101 }
1102
1103 void CMegacellDialog::OnImportTiny() {
1104 char filename[255];
1105 int n = D3EditState.current_megacell;
1106 int megawidth = Megacells[n].width;
1107 int megaheight = Megacells[n].height;
1108 int src_handle;
1109 int count = 0;
1110 int i, t;
1111
1112 if (!Network_up) {
1113 OutrageMessageBox("Sorry babe, the network is down. This action is a no-no.\n");
1114 return;
1115 }
1116
1117 if (Num_megacells < 1) {
1118 OutrageMessageBox("You must first create a megacell before using this feature.");
1119 return;
1120 }
1121
1122 if ((mng_FindTrackLock(Megacells[n].name, PAGETYPE_MEGACELL)) == -1) {
1123 OutrageMessageBox("You must have this megacell locked before you can import!");
1124 return;
1125 }
1126
1127 CString filter = "Descent III files (*.tga,*.bbm,*.lbm,*.ogf,*.pcx)|*.pcx;*.tga;*.bbm;*.lbm;*.ogf;||";
1128
1129 if (!OpenFileDialog(this, (LPCSTR)filter, filename, Current_bitmap_dir, sizeof(Current_bitmap_dir)))
1130 return;
1131
1132 // Okay, we selected a file. Lets do what needs to be done here.
1133 src_handle = bm_AllocLoadFileBitmap(filename, 0);
1134
1135 if (src_handle < 0) {
1136 OutrageMessageBox("Couldn't read that bitmap!");
1137 return;
1138 }
1139
1140 int w = bm_w(src_handle, 0);
1141 int h = bm_h(src_handle, 0);
1142
1143 float width_per_cell = (float)w / (float)megawidth;
1144 float height_per_cell = (float)h / (float)megaheight;
1145
1146 for (i = 0; i < megaheight; i++) {
1147 for (t = 0; t < megawidth; t++) {
1148 float startx = width_per_cell * t;
1149 float starty = height_per_cell * i;
1150
1151 float u = startx;
1152 float v = starty;
1153
1154 float du = width_per_cell / 32.0;
1155 float dv = height_per_cell / 32.0;
1156
1157 int bm_handle = bm_AllocBitmap(32, 32, 0);
1158
1159 ASSERT(bm_handle >= 0);
1160
1161 uint16_t *dest_data = bm_data(bm_handle, 0);
1162 uint16_t *src_data = bm_data(src_handle, 0);
1163
1164 for (int ty = 0; ty < 32; ty++, u = startx, v += dv) {
1165 for (int tx = 0; tx < 32; tx++, u += du) {
1166 int cu = u;
1167 int cv = v;
1168
1169 uint16_t pix = src_data[cv * w + cu];
1170 dest_data[ty * 32 + tx] = pix;
1171 }
1172 }
1173
1174 // Alloc a texture and assign it this bitmap handle
1175 int tex_handle;
1176 tex_handle = AllocTexture();
1177 ASSERT(tex_handle > 0);
1178
1179 GameTextures[tex_handle].flags &= ~(TF_TEXTURE_TYPES);
1180 GameTextures[tex_handle].flags |= TF_TEXTURE_32;
1181 GameTextures[tex_handle].flags |= TF_TERRAIN;
1182
1183 GameTextures[tex_handle].bm_handle = bm_handle;
1184 Megacells[n].texture_handles[i * MAX_MEGACELL_WIDTH + t] = tex_handle;
1185
1186 // Find a unique name for this texture
1187
1188 int finding_name = 1;
1189 char cur_name[255];
1190
1191 while (finding_name) {
1192 sprintf(cur_name, "%s%d", Megacells[n].name, count);
1193 if (FindTextureName(cur_name) != -1)
1194 count++;
1195 else
1196 finding_name = 0;
1197 }
1198 strcpy(GameTextures[tex_handle].name, cur_name);
1199 bm_ChangeEndName(Megacells[n].name, filename);
1200 strcpy(GameBitmaps[bm_handle].name, filename);
1201
1202 // Save this textures image locally
1203 sprintf(filename, "%s\\%s", LocalManageGraphicsDir.u8string().c_str(),
1204 GameBitmaps[GameTextures[tex_handle].bm_handle].name);
1205 mprintf(0, "Saving bitmap %s from megacell!\n", GameBitmaps[bm_handle].name);
1206 bm_SaveFileBitmap(filename, GameTextures[tex_handle].bm_handle);
1207
1208 // Allocate a track lock for this texture
1209 mng_AllocTrackLock(cur_name, PAGETYPE_TEXTURE);
1210 }
1211 }
1212
1213 bm_FreeBitmap(src_handle);
1214
1215 UpdateDialog();
1216 }
1217
1218 BOOL CMegacellDialog::OnHelpInfo(HELPINFO *pHelpInfo) {
1219 // TODO: Add your message handler code here and/or call default
1220 WinHelp(HID_MEGACELLDLG, HELP_CONTEXT);
1221 return TRUE;
1222 // return CDialog::OnHelpInfo(pHelpInfo);
1223 }
1224
1225 #define SKY_TEXTURE_SIZE 128
1226 void CMegacellDialog::OnImportSky() {
1227 char filename[255];
1228 int n = D3EditState.current_megacell;
1229 int megawidth = Megacells[n].width;
1230 int megaheight = Megacells[n].height;
1231 int src_handle;
1232 int count = 0;
1233 int i, t;
1234
1235 if (!Network_up) {
1236 OutrageMessageBox("Sorry babe, the network is down. This action is a no-no.\n");
1237 return;
1238 }
1239
1240 if (Num_megacells < 1) {
1241 OutrageMessageBox("You must first create a megacell before using this feature.");
1242 return;
1243 }
1244
1245 if (megawidth != 2 || megaheight != 2) {
1246 OutrageMessageBox("All sky dome megacells must be 2x2 in dimensions!");
1247 return;
1248 }
1249
1250 if ((mng_FindTrackLock(Megacells[n].name, PAGETYPE_MEGACELL)) == -1) {
1251 OutrageMessageBox("You must have this megacell locked before you can import!");
1252 return;
1253 }
1254
1255 CString filter = "Descent III files (*.tga,*.bbm,*.lbm,*.ogf,*.pcx)|*.pcx;*.tga;*.bbm;*.lbm;*.ogf;||";
1256
1257 if (!OpenFileDialog(this, (LPCSTR)filter, filename, Current_bitmap_dir, sizeof(Current_bitmap_dir)))
1258 return;
1259
1260 // Okay, we selected a file. Lets do what needs to be done here.
1261 src_handle = bm_AllocLoadFileBitmap(filename, 0);
1262
1263 if (src_handle < 0) {
1264 OutrageMessageBox("Couldn't read that bitmap!");
1265 return;
1266 }
1267
1268 int w = bm_w(src_handle, 0);
1269 int h = bm_h(src_handle, 0);
1270
1271 float width_per_cell = (float)w / (float)megawidth;
1272 float height_per_cell = (float)h / (float)megaheight;
1273
1274 for (i = 0; i < megaheight; i++) {
1275 for (t = 0; t < megawidth; t++) {
1276 float startx = width_per_cell * t;
1277 float starty = height_per_cell * i;
1278
1279 float u = startx;
1280 float v = starty;
1281
1282 float du = width_per_cell / (float)SKY_TEXTURE_SIZE;
1283 float dv = height_per_cell / (float)SKY_TEXTURE_SIZE;
1284
1285 int bm_handle = bm_AllocBitmap(SKY_TEXTURE_SIZE, SKY_TEXTURE_SIZE, 0);
1286
1287 ASSERT(bm_handle >= 0);
1288
1289 uint16_t *dest_data = bm_data(bm_handle, 0);
1290 uint16_t *src_data = bm_data(src_handle, 0);
1291
1292 for (int ty = 0; ty < SKY_TEXTURE_SIZE; ty++, u = startx, v += dv) {
1293 for (int tx = 0; tx < SKY_TEXTURE_SIZE; tx++, u += du) {
1294 int cu = u;
1295 int cv = v;
1296
1297 uint16_t pix = src_data[cv * w + cu];
1298 dest_data[ty * SKY_TEXTURE_SIZE + tx] = pix;
1299 }
1300 }
1301
1302 // Alloc a texture and assign it this bitmap handle
1303 int tex_handle;
1304 tex_handle = AllocTexture();
1305 ASSERT(tex_handle > 0);
1306
1307 GameTextures[tex_handle].flags |= TF_TERRAIN;
1308
1309 GameTextures[tex_handle].bm_handle = bm_handle;
1310 Megacells[n].texture_handles[i * MAX_MEGACELL_WIDTH + t] = tex_handle;
1311
1312 // Find a unique name for this texture
1313
1314 int finding_name = 1;
1315 char cur_name[255];
1316
1317 while (finding_name) {
1318 sprintf(cur_name, "%s%d", Megacells[n].name, count);
1319 if (FindTextureName(cur_name) != -1)
1320 count++;
1321 else
1322 finding_name = 0;
1323 }
1324 strcpy(GameTextures[tex_handle].name, cur_name);
1325 bm_ChangeEndName(Megacells[n].name, filename);
1326 strcpy(GameBitmaps[bm_handle].name, filename);
1327
1328 // Save this textures image locally
1329 sprintf(filename, "%s\\%s", LocalManageGraphicsDir.u8string().c_str(),
1330 GameBitmaps[GameTextures[tex_handle].bm_handle].name);
1331 mprintf(0, "Saving bitmap %s from megacell!\n", GameBitmaps[bm_handle].name);
1332 bm_SaveFileBitmap(filename, GameTextures[tex_handle].bm_handle);
1333
1334 // Allocate a track lock for this texture
1335 mng_AllocTrackLock(cur_name, PAGETYPE_TEXTURE);
1336 }
1337 }
1338
1339 bm_FreeBitmap(src_handle);
1340
1341 UpdateDialog();
1342 }
1343
1344 #define SKY_BAND_SIZE 64
1345 void CMegacellDialog::OnImportSkyBand() {
1346 char filename[255];
1347 int n = D3EditState.current_megacell;
1348 int megawidth = Megacells[n].width;
1349 int megaheight = Megacells[n].height;
1350 int src_handle;
1351 int count = 0;
1352 int t;
1353
1354 if (!Network_up) {
1355 OutrageMessageBox("Sorry babe, the network is down. This action is a no-no.\n");
1356 return;
1357 }
1358
1359 if (Num_megacells < 1) {
1360 OutrageMessageBox("You must first create a megacell before using this feature.");
1361 return;
1362 }
1363
1364 if (megawidth != 8 || megaheight != 2) {
1365 OutrageMessageBox("All sky band megacells must be 8x2 in dimensions!");
1366 return;
1367 }
1368
1369 if ((mng_FindTrackLock(Megacells[n].name, PAGETYPE_MEGACELL)) == -1) {
1370 OutrageMessageBox("You must have this megacell locked before you can import!");
1371 return;
1372 }
1373
1374 CString filter = "Descent III files (*.tga,*.bbm,*.lbm,*.ogf,*.pcx)|*.pcx;*.tga;*.bbm;*.lbm;*.ogf;||";
1375
1376 if (!OpenFileDialog(this, (LPCSTR)filter, filename, Current_bitmap_dir, sizeof(Current_bitmap_dir)))
1377 return;
1378
1379 // Okay, we selected a file. Lets do what needs to be done here.
1380 src_handle = bm_AllocLoadFileBitmap(filename, 0);
1381
1382 if (src_handle < 0) {
1383 OutrageMessageBox("Couldn't read that bitmap!");
1384 return;
1385 }
1386
1387 int w = bm_w(src_handle, 0);
1388 int h = bm_h(src_handle, 0);
1389
1390 float width_per_cell = (float)w / (float)16;
1391 float height_per_cell = (float)h / (float)1;
1392
1393 for (t = 0; t < 16; t++) {
1394 float startx = width_per_cell * t;
1395 float starty = 0;
1396
1397 float u = startx;
1398 float v = starty;
1399
1400 float du = width_per_cell / (float)SKY_BAND_SIZE;
1401 float dv = height_per_cell / (float)SKY_BAND_SIZE;
1402
1403 int bm_handle = bm_AllocBitmap(SKY_BAND_SIZE, SKY_BAND_SIZE, 0);
1404
1405 ASSERT(bm_handle >= 0);
1406
1407 uint16_t *dest_data = bm_data(bm_handle, 0);
1408 uint16_t *src_data = bm_data(src_handle, 0);
1409
1410 for (int ty = 0; ty < SKY_BAND_SIZE; ty++, u = startx, v += dv) {
1411 for (int tx = 0; tx < SKY_BAND_SIZE; tx++, u += du) {
1412 int cu = u;
1413 int cv = v;
1414
1415 uint16_t pix = src_data[cv * w + cu];
1416 dest_data[ty * SKY_BAND_SIZE + tx] = pix;
1417 }
1418 }
1419
1420 // Alloc a texture and assign it this bitmap handle
1421 int tex_handle;
1422 tex_handle = AllocTexture();
1423 ASSERT(tex_handle > 0);
1424
1425 GameTextures[tex_handle].flags &= ~(TF_TEXTURE_TYPES);
1426 GameTextures[tex_handle].flags |= (TF_TERRAIN | TF_TEXTURE_64);
1427 GameTextures[tex_handle].bm_handle = bm_handle;
1428
1429 int row = t / MAX_MEGACELL_WIDTH;
1430 int column = t % MAX_MEGACELL_WIDTH;
1431
1432 Megacells[n].texture_handles[row * MAX_MEGACELL_WIDTH + column] = tex_handle;
1433
1434 // Find a unique name for this texture
1435
1436 int finding_name = 1;
1437 char cur_name[255];
1438
1439 while (finding_name) {
1440 sprintf(cur_name, "%s%d", Megacells[n].name, count);
1441 if (FindTextureName(cur_name) != -1)
1442 count++;
1443 else
1444 finding_name = 0;
1445 }
1446 strcpy(GameTextures[tex_handle].name, cur_name);
1447 bm_ChangeEndName(Megacells[n].name, filename);
1448 strcpy(GameBitmaps[bm_handle].name, filename);
1449
1450 // Save this textures image locally
1451 sprintf(filename, "%s\\%s", LocalManageGraphicsDir.u8string().c_str(),
1452 GameBitmaps[GameTextures[tex_handle].bm_handle].name);
1453 mprintf(0, "Saving bitmap %s from megacell!\n", GameBitmaps[bm_handle].name);
1454 bm_SaveFileBitmap(filename, GameTextures[tex_handle].bm_handle);
1455
1456 // Allocate a track lock for this texture
1457 mng_AllocTrackLock(cur_name, PAGETYPE_TEXTURE);
1458 }
1459
1460 bm_FreeBitmap(src_handle);
1461
1462 UpdateDialog();
1463 }