Skip to content

Commit 3bba5f7

Browse files
committed
More organization
Properly destroy backbuffer bitmaps when quitting
1 parent ebd60fe commit 3bba5f7

20 files changed

+143
-125
lines changed

Editor/GUIEditorApp.cpp renamed to Editor/EditorApp.cpp

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
#include "GUIEditorApp.h"
2-
#include "GUIEditorUtil.h"
1+
#include "EditorApp.h"
2+
#include "EditorUtil.h"
33
#include "GUIButton.h"
44
#include "GUICheckbox.h"
55
#include "GUILabel.h"
66
#include "GUITextBox.h"
7-
#include "allegro.h"
87
#include "winalleg.h"
98

109
namespace RTEGUI {
1110

1211
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1312

14-
bool GUIEditorApp::Initialize() {
13+
bool EditorApp::Initialize() {
1514
set_color_depth(32);
1615
set_color_conversion(COLORCONV_MOST);
1716
set_window_title("Cortex Command GUI Editor");
@@ -25,6 +24,9 @@ namespace RTEGUI {
2524
m_BackBuffer = create_bitmap(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2625
clear_to_color(m_BackBuffer, 0);
2726

27+
m_ZoomBuffer = create_bitmap(m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
28+
clear_to_color(m_ZoomBuffer, 0);
29+
2830
m_Screen = std::make_unique<AllegroScreen>(m_BackBuffer);
2931
m_Input = std::make_unique<AllegroInput>(-1);
3032

@@ -43,7 +45,7 @@ namespace RTEGUI {
4345

4446
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4547

46-
void GUIEditorApp::CreateEditorLayout() {
48+
void EditorApp::CreateEditorLayout() {
4749
m_EditorBase.reset(dynamic_cast<GUICollectionBox *>(m_EditorManager->AddControl("EditorBase", "COLLECTIONBOX", nullptr, 0, 0, m_ResX, m_ResY)));
4850
m_EditorBase->SetDrawBackground(true);
4951
m_EditorBase->SetDrawColor(makecol(32, 32, 32));
@@ -142,7 +144,14 @@ namespace RTEGUI {
142144

143145
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
144146

145-
void GUIEditorApp::SelectActiveControlInList() const {
147+
void EditorApp::DestroyBackBuffers() {
148+
destroy_bitmap(m_BackBuffer);
149+
destroy_bitmap(m_ZoomBuffer);
150+
}
151+
152+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153+
154+
void EditorApp::SelectActiveControlInList() const {
146155
if (m_SelectionInfo.Control == nullptr) {
147156
m_CollectionBoxList->SetSelectedIndex(-1);
148157
return;
@@ -160,7 +169,7 @@ namespace RTEGUI {
160169

161170
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162171

163-
void GUIEditorApp::AddItemToCollectionBoxList(GUIControl *control, const std::string &indent) const {
172+
void EditorApp::AddItemToCollectionBoxList(GUIControl *control, const std::string &indent) const {
164173
m_CollectionBoxList->AddItem(indent + control->GetName());
165174
for (GUIControl *childControl : *control->GetChildren()) {
166175
if ((control = dynamic_cast<GUICollectionBox *>(childControl))) {
@@ -171,7 +180,7 @@ namespace RTEGUI {
171180

172181
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
173182

174-
void GUIEditorApp::PopulateCollectionBoxList() const {
183+
void EditorApp::PopulateCollectionBoxList() const {
175184
m_CollectionBoxList->ClearList();
176185
GUICollectionBox *collectionBox = nullptr;
177186

@@ -192,7 +201,7 @@ namespace RTEGUI {
192201

193202
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
194203

195-
void GUIEditorApp::PopulateCollectionBoxChildrenList(GUICollectionBox *collectionBox) const {
204+
void EditorApp::PopulateCollectionBoxChildrenList(GUICollectionBox *collectionBox) const {
196205
m_ControlsInActiveCollectionBoxList->ClearList();
197206

198207
std::vector<GUIControl *> controls = *collectionBox->GetChildren();
@@ -210,7 +219,7 @@ namespace RTEGUI {
210219

211220
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
212221

213-
GUIControl * GUIEditorApp::ControlUnderMouse(GUIControl *parent, int mousePosX, int mousePosY) {
222+
GUIControl * EditorApp::ControlUnderMouse(GUIControl *parent, int mousePosX, int mousePosY) {
214223
assert(parent);
215224

216225
// Clicked on the parent?
@@ -246,7 +255,7 @@ namespace RTEGUI {
246255

247256
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
248257

249-
int GUIEditorApp::HandleUnderMouse(GUIControl *control, int mousePosX, int mousePosY) const {
258+
int EditorApp::HandleUnderMouse(GUIControl *control, int mousePosX, int mousePosY) const {
250259
int xPos;
251260
int yPos;
252261
int width;
@@ -279,13 +288,13 @@ namespace RTEGUI {
279288

280289
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
281290

282-
bool GUIEditorApp::MouseInsideBox(int mousePosX, int mousePosY, int xPos, int yPos, int width, int height) const {
291+
bool EditorApp::MouseInsideBox(int mousePosX, int mousePosY, int xPos, int yPos, int width, int height) const {
283292
return (mousePosX >= xPos && mousePosX <= xPos + width && mousePosY >= yPos && mousePosY <= yPos + height) ? true : false;
284293
}
285294

286295
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
287296

288-
void GUIEditorApp::CalculateHandleResize(int mousePosX, int mousePosY, int *xPos, int *yPos, int *width, int *height) {
297+
void EditorApp::CalculateHandleResize(int mousePosX, int mousePosY, int *xPos, int *yPos, int *width, int *height) {
289298
int controlPosX;
290299
int controlPosY;
291300
int controlWidth;
@@ -353,7 +362,7 @@ namespace RTEGUI {
353362

354363
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
355364

356-
int GUIEditorApp::ProcessSnapCoord(int position) const {
365+
int EditorApp::ProcessSnapCoord(int position) const {
357366
if (m_SnapToGrid) {
358367
float unsnappedPosition = std::round(static_cast<float>(position) / static_cast<float>(m_GridSize));
359368
position = static_cast<int>(unsnappedPosition) * m_GridSize;
@@ -363,7 +372,7 @@ namespace RTEGUI {
363372

364373
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
365374

366-
void GUIEditorApp::AddNewControl(GUIEvent &editorEvent) {
375+
void EditorApp::AddNewControl(GUIEvent &editorEvent) {
367376
std::string controlClass = editorEvent.GetControl()->GetName().substr(2, std::string::npos);
368377
GUIControl *parent = m_RootControl;
369378

@@ -384,7 +393,7 @@ namespace RTEGUI {
384393

385394
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
386395

387-
std::string GUIEditorApp::GenerateControlName(std::string controlType) const {
396+
std::string EditorApp::GenerateControlName(std::string controlType) const {
388397
for (int i = 1; i < 1000; i++) {
389398
std::string controlName = controlType;
390399
std::transform(controlName.begin(), controlName.end(), controlName.begin(), tolower);
@@ -407,7 +416,7 @@ namespace RTEGUI {
407416

408417
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
409418

410-
void GUIEditorApp::UpdateCollectionBoxList() {
419+
void EditorApp::UpdateCollectionBoxList() {
411420
const GUIListPanel::Item *item = m_CollectionBoxList->GetSelected();
412421

413422
if (item) {
@@ -433,7 +442,7 @@ namespace RTEGUI {
433442

434443
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
435444

436-
void GUIEditorApp::UpdateGridSize(GUIEvent &editorEvent) {
445+
void EditorApp::UpdateGridSize(GUIEvent &editorEvent) {
437446
std::string newValue = dynamic_cast<GUITextBox *>(editorEvent.GetControl())->GetText();
438447
if (newValue.empty()) { newValue = "1"; }
439448

@@ -452,7 +461,7 @@ namespace RTEGUI {
452461

453462
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
454463

455-
void GUIEditorApp::UpdateControlProperties(GUIControl *control, bool setUnsavedChanges) {
464+
void EditorApp::UpdateControlProperties(GUIControl *control, bool setUnsavedChanges) {
456465
control->StoreProperties();
457466

458467
GUIProperties properties;
@@ -465,7 +474,7 @@ namespace RTEGUI {
465474

466475
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
467476

468-
void GUIEditorApp::UpdatePropertyPage(GUIEvent &editorEvent) {
477+
void EditorApp::UpdatePropertyPage(GUIEvent &editorEvent) {
469478
if (editorEvent.GetMsg() == GUIPropertyPage::Enter) {
470479
// Update the focused control properties
471480
GUIControl *control = m_SelectionInfo.Control;
@@ -481,7 +490,7 @@ namespace RTEGUI {
481490

482491
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
483492

484-
bool GUIEditorApp::Update() {
493+
bool EditorApp::Update() {
485494
m_EditorManager->Update();
486495
GUIEvent editorEvent;
487496
while (m_EditorManager->GetEvent(&editorEvent)) {
@@ -526,7 +535,7 @@ namespace RTEGUI {
526535

527536
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
528537

529-
void GUIEditorApp::ProcessMouseInput() {
538+
void EditorApp::ProcessMouseInput() {
530539
std::array<int, 3> mouseButtons;
531540
std::array<int, 3> mouseStates;
532541
int mousePosX;
@@ -627,7 +636,7 @@ namespace RTEGUI {
627636

628637
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
629638

630-
void GUIEditorApp::ProcessKeyboardInput() {
639+
void EditorApp::ProcessKeyboardInput() {
631640
// Handle keyboard input directly from Allegro instead of through AllegroInput to make life easier.
632641
for (int i = 0; i < KEY_MAX; ++i) {
633642
m_KeyStates.at(i) = key[i];
@@ -687,8 +696,8 @@ namespace RTEGUI {
687696

688697
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
689698

690-
void GUIEditorApp::DrawSelectionBox(GUIControl *control) {
691-
RTEAssert(control, "Trying to draw selection box for a null control!");
699+
void EditorApp::DrawSelectionBox(GUIControl *control) {
700+
assert(control);
692701

693702
int mousePosX;
694703
int mousePosY;
@@ -729,15 +738,15 @@ namespace RTEGUI {
729738

730739
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
731740

732-
void GUIEditorApp::DrawSelectionResizeBox(int xPos, int yPos) const {
741+
void EditorApp::DrawSelectionResizeBox(int xPos, int yPos) const {
733742
int boxSize = (m_Zoom) ? 5 : 7;
734743
m_Screen->GetBitmap()->DrawRectangle(xPos - boxSize / 2, yPos - boxSize / 2, boxSize, boxSize, 0x000000, true);
735744
m_Screen->GetBitmap()->DrawRectangle(xPos - boxSize / 2, yPos - boxSize / 2, boxSize, boxSize, 0xFFFFFF, false);
736745
}
737746

738747
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
739748

740-
void GUIEditorApp::DrawEditor() {
749+
void EditorApp::DrawEditor() {
741750
if (m_WindowResized) {
742751
acknowledge_resize();
743752
m_WindowResized = false;
@@ -752,19 +761,17 @@ namespace RTEGUI {
752761
m_EditorManager->DrawMouse();
753762

754763
if (m_Zoom) {
755-
BITMAP *tempBackbuffer = create_bitmap(m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
756-
stretch_blit(m_BackBuffer, tempBackbuffer, m_WorkspacePosX, m_WorkspacePosY, m_WorkspaceWidth, m_WorkspaceHeight, 0, 0, m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
757-
blit(tempBackbuffer, m_BackBuffer, 0, 0, m_WorkspacePosX, m_WorkspacePosY - 30, m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
758-
destroy_bitmap(tempBackbuffer);
764+
stretch_blit(m_BackBuffer, m_ZoomBuffer, m_WorkspacePosX, m_WorkspacePosY, m_WorkspaceWidth, m_WorkspaceHeight, 0, 0, m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
765+
blit(m_ZoomBuffer, m_BackBuffer, 0, 0, m_WorkspacePosX, m_WorkspacePosY - 30, m_WorkspaceWidth * 2, m_WorkspaceHeight * 2);
759766
}
760767
blit(m_BackBuffer, screen, 0, 0, 0, 0, screen->w, screen->h);
761768
}
762769

763770
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
764771

765-
void GUIEditorApp::OnLoadButton(bool addControls) {
772+
void EditorApp::OnLoadButton(bool addControls) {
766773
std::string newFilename;
767-
if (GUIEditorUtil::DisplayLoadGUIFile(&newFilename, win_get_window())) {
774+
if (EditorUtil::DisplayLoadGUIFile(&newFilename, win_get_window())) {
768775
m_ControlManager->Load(newFilename, addControls);
769776
m_Filename = newFilename;
770777

@@ -787,10 +794,10 @@ namespace RTEGUI {
787794

788795
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
789796

790-
void GUIEditorApp::OnSaveButton(bool saveAsNewFile) {
797+
void EditorApp::OnSaveButton(bool saveAsNewFile) {
791798
if (saveAsNewFile || m_Filename.empty()) {
792799
std::string newFilename;
793-
if (GUIEditorUtil::DisplaySaveGUIFile(&newFilename, win_get_window())) { m_Filename = newFilename; }
800+
if (EditorUtil::DisplaySaveGUIFile(&newFilename, win_get_window())) { m_Filename = newFilename; }
794801
}
795802
// Move the root object to top left corner before saving so it is displayed correctly in-game.
796803
m_RootControl->Move(0, 0);
@@ -805,18 +812,18 @@ namespace RTEGUI {
805812

806813
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
807814

808-
void GUIEditorApp::OnQuitButton() {
815+
void EditorApp::OnQuitButton() {
809816
int quitResult = 1;
810817
if (m_UnsavedChanges) {
811-
quitResult = GUIEditorUtil::QuitMessageBox("Save changes made?", win_get_window());
818+
quitResult = EditorUtil::QuitMessageBox("Save changes made?", win_get_window());
812819
if (quitResult == 1) { OnSaveButton(); }
813820
}
814821
m_Quit = (quitResult != 0) ? true : false;
815822
}
816823

817824
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
818825

819-
void GUIEditorApp::OnWindowResize(RESIZE_DISPLAY_EVENT *resizeInfo) {
826+
void EditorApp::OnWindowResize(RESIZE_DISPLAY_EVENT *resizeInfo) {
820827
m_EditorBase->Resize(resizeInfo->new_w, resizeInfo->new_h);
821828
m_LeftColumn->Resize(m_LeftColumn->GetWidth(), resizeInfo->new_h);
822829
m_RightColumn->Resize(m_RightColumn->GetWidth(), resizeInfo->new_h);

Editor/GUIEditorApp.h renamed to Editor/EditorApp.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#ifndef _GUIEDITORAPP_
2-
#define _GUIEDITORAPP_
1+
#ifndef _RTEGUIEDITORAPP_
2+
#define _RTEGUIEDITORAPP_
3+
4+
#include "allegro.h"
35

46
#include "GUI.h"
57
#include "GUIPropertyPage.h"
@@ -10,7 +12,7 @@
1012

1113
#include "Singleton.h"
1214

13-
#define g_GUIEditor GUIEditorApp::Instance()
15+
#define g_GUIEditor EditorApp::Instance()
1416

1517
using namespace RTE;
1618

@@ -19,15 +21,15 @@ namespace RTEGUI {
1921
/// <summary>
2022
/// GUI Editor Application class that handles the main editor app.
2123
/// </summary>
22-
class GUIEditorApp : public Singleton<GUIEditorApp> {
24+
class EditorApp : public Singleton<EditorApp> {
2325

2426
public:
2527

2628
#pragma region Creation
2729
/// <summary>
28-
/// Constructor method used to instantiate a GUIEditorApp object in system memory.
30+
/// Constructor method used to instantiate a EditorApp object in system memory.
2931
/// </summary>
30-
GUIEditorApp() = default;
32+
EditorApp() = default;
3133

3234
/// <summary>
3335
/// Initializes the editor app.
@@ -36,6 +38,13 @@ namespace RTEGUI {
3638
bool Initialize();
3739
#pragma endregion
3840

41+
#pragma region Destruction
42+
/// <summary>
43+
/// Frees any memory used by the editor's backbuffers, the rest will be handled by the default destructor.
44+
/// </summary>
45+
void DestroyBackBuffers();
46+
#pragma endregion
47+
3948
#pragma region Concrete Methods
4049
/// <summary>
4150
/// Updates the editor app.
@@ -113,6 +122,7 @@ namespace RTEGUI {
113122
int m_ResX = 1280;
114123
int m_ResY = 600;
115124
BITMAP *m_BackBuffer = nullptr;
125+
BITMAP *m_ZoomBuffer = nullptr;
116126
std::unique_ptr<AllegroScreen> m_Screen = nullptr;
117127
std::unique_ptr<AllegroInput> m_Input = nullptr;
118128
std::unique_ptr<GUIControlManager> m_ControlManager = nullptr;
@@ -285,8 +295,8 @@ namespace RTEGUI {
285295
#pragma endregion
286296

287297
// Disallow the use of some implicit methods.
288-
GUIEditorApp(const GUIEditorApp &reference) = delete;
289-
GUIEditorApp &operator=(const GUIEditorApp &rhs) = delete;
298+
EditorApp(const EditorApp &reference) = delete;
299+
EditorApp &operator=(const EditorApp &rhs) = delete;
290300
};
291301

292302
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Editor/GUIEditorUtil.cpp renamed to Editor/EditorUtil.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "GUIEditorUtil.h"
1+
#include "EditorUtil.h"
22

33
#include <Windows.h>
44
#include <commdlg.h>
@@ -8,7 +8,7 @@ namespace RTEGUI {
88

99
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1010

11-
int GUIEditorUtil::QuitMessageBox(const std::string &message, const HWND &windowHandle) {
11+
int EditorUtil::QuitMessageBox(const std::string &message, const HWND &windowHandle) {
1212
int result = MessageBox(windowHandle, message.c_str(), "Cortex Command GUI Editor", MB_YESNOCANCEL);
1313

1414
if (result == IDNO) { return -1; }
@@ -18,7 +18,7 @@ namespace RTEGUI {
1818

1919
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2020

21-
bool GUIEditorUtil::DisplayLoadGUIFile(std::string *filename, const HWND &windowHandle) {
21+
bool EditorUtil::DisplayLoadGUIFile(std::string *filename, const HWND &windowHandle) {
2222
OPENFILENAMEA dialogBox;
2323
std::string filenameToLoad(MAX_PATH, '\0'); // Make sure the string is initialized with the correct size and filled with null characters.
2424

@@ -52,7 +52,7 @@ namespace RTEGUI {
5252

5353
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5454

55-
bool GUIEditorUtil::DisplaySaveGUIFile(std::string *filename, const HWND &windowHandle) {
55+
bool EditorUtil::DisplaySaveGUIFile(std::string *filename, const HWND &windowHandle) {
5656
OPENFILENAMEA dialogBox;
5757
std::string filenameToSave(MAX_PATH, '\0'); // Make sure the string is initialized with the correct size and filled with null characters.
5858

0 commit comments

Comments
 (0)