1- // ////////////////////////////////////////////////////////////////////////////////////////
2- // File: ContentFile.cpp
3- // ////////////////////////////////////////////////////////////////////////////////////////
4- // Description: Source file for the ContentFile class.
5- // Project: Retro Terrain Engine
6- // Author(s): Daniel Tabar
7- // data@datarealms.com
8- // http://www.datarealms.com
9-
10-
11- // ////////////////////////////////////////////////////////////////////////////////////////
12- // Inclusions of header files
13-
141#include " ContentFile.h"
152#include " RTEError.h"
163
4+ namespace RTE {
175
18- using namespace std ;
19-
20- namespace RTE
21- {
22-
23- const string ContentFile::m_ClassName = " ContentFile" ;
24- map<string, BITMAP *> ContentFile::m_sLoadedBitmaps;
25-
26-
27- // ////////////////////////////////////////////////////////////////////////////////////////
28- // Method: Clear
29- // ////////////////////////////////////////////////////////////////////////////////////////
30- // Description: Clears all the member variables of this ContentFile, effectively
31- // resetting the members of this abstraction level only.
32-
33- void ContentFile::Clear ()
34- {
35- m_DataPath.erase ();
36- m_DataModuleID = 0 ;
37- }
38-
39- // ////////////////////////////////////////////////////////////////////////////////////////
40- // Method: Create
41- // ////////////////////////////////////////////////////////////////////////////////////////
42- // Description: Makes the ContentFile object ready for use.
43-
44- int ContentFile::Create (const char *filePath)
45- {
46- m_DataPath = filePath;
47-
48- return 0 ;
49- }
6+ const string ContentFile::m_ClassName = " ContentFile" ;
7+ std::unordered_map<std::string, BITMAP *> ContentFile::m_LoadedBitmaps;
508
9+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5110
52- // ////////////////////////////////////////////////////////////////////////////////////////
53- // Method: Create
54- // ////////////////////////////////////////////////////////////////////////////////////////
55- // Description: Creates a ContentFile to be identical to another, by deep copy.
11+ int ContentFile::Create ( const char *filePath) {
12+ m_DataPath = filePath;
13+ return 0 ;
14+ }
5615
57- int ContentFile::Create (const ContentFile &reference)
58- {
59- // Entity::Create(reference);
16+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6017
61- m_DataPath = reference.m_DataPath ;
62- m_DataModuleID = reference.m_DataModuleID ;
18+ int ContentFile::Create (const ContentFile &reference) {
19+ m_DataPath = reference.m_DataPath ;
20+ return 0 ;
21+ }
6322
64- return 0 ;
65- }
23+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6624
25+ void ContentFile::FreeAllLoaded () {
26+ for (const std::pair<std::string, BITMAP *> &loadedBitmap : m_LoadedBitmaps) {
27+ destroy_bitmap (loadedBitmap.second );
28+ }
29+ }
6730
31+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6832
69- // ////////////////////////////////////////////////////////////////////////////////////////
70- // Method: Destroy
71- // ////////////////////////////////////////////////////////////////////////////////////////
72- // Description: Destroys and resets (through Clear()) the ContentFile object.
33+ BITMAP * ContentFile::GetAsBitmap (int conversionMode) {
34+ if (m_DataPath.empty ()) {
35+ return nullptr ;
36+ }
37+ BITMAP *returnBitmap = nullptr ;
7338
74- void ContentFile::Destroy (bool notInherited)
75- {
76- Clear ();
77- }
39+ /* Iterator crashes for god knows what reason
40+ std::unordered_map<std::string, BITMAP *>::iterator foundBitmap = m_LoadedBitmaps.find(m_DataPath);
41+ if (foundBitmap != m_LoadedBitmaps.end()) {
42+ returnBitmap = (*foundBitmap).second;
43+ } else {
44+ returnBitmap = LoadAndReleaseBitmap(conversionMode);
45+ m_LoadedBitmaps.insert({ m_DataPath, returnBitmap });
46+ }
47+ */
7848
49+ returnBitmap = LoadAndReleaseBitmap (conversionMode);
7950
80- // ////////////////////////////////////////////////////////////////////////////////////////
81- // Static method: FreeAllLoaded
82- // ////////////////////////////////////////////////////////////////////////////////////////
83- // Description: Frees all loaded data used by all ContentFile instances. This should
84- // ONLY be done when quitting the app, or after everything else is
85- // completly destroyed
51+ return returnBitmap;
52+ }
8653
87- void ContentFile::FreeAllLoaded ()
88- {
89- for (map<string, BITMAP *>::iterator lbItr = m_sLoadedBitmaps.begin (); lbItr != m_sLoadedBitmaps.end (); ++lbItr)
90- destroy_bitmap ((*lbItr).second );
91- }
54+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9255
56+ BITMAP * ContentFile::LoadAndReleaseBitmap (int conversionMode) {
57+ if (m_DataPath.empty ()) {
58+ return nullptr ;
59+ }
60+ set_color_conversion (conversionMode == 0 ? COLORCONV_MOST : conversionMode);
9361
94- // ////////////////////////////////////////////////////////////////////////////////////////
95- // Virtual method: SetDataPath
96- // ////////////////////////////////////////////////////////////////////////////////////////
97- // Description: Sets the file path of the content file represented by this ContentFile
98- // object.
99-
100- void ContentFile::SetDataPath (std::string newDataPath)
101- {
102- m_DataPath = newDataPath;
103- };
104-
105-
106- // ////////////////////////////////////////////////////////////////////////////////////////
107- // Method: GetDataModuleID
108- // ////////////////////////////////////////////////////////////////////////////////////////
109- // Description: Gets the ID of the Data Module this file is inside.
110- // Arguments: None.
111- // Return value: The ID of the Data Module containing this' file.
112-
113- int ContentFile::GetDataModuleID ()
114- {
115- return m_DataModuleID;
116- }
117-
118-
119- // ////////////////////////////////////////////////////////////////////////////////////////
120- // Method: GetAsBitmap
121- // ////////////////////////////////////////////////////////////////////////////////////////
122- // Description: Loads and gets the data represtented by this ConentFile object as an
123- // Allegro BITMAP. Note that ownership of the BITMAP IS NOT TRANSFERRED!
124-
125- BITMAP * ContentFile::GetAsBitmap (int conversionMode)
126- {
127- if (m_DataPath.empty ())
128- return 0 ;
129-
130- BITMAP *pReturnBitmap = 0 ;
131-
132- // Check if this file has already been read and loaded from disk.
133- map<string, BITMAP *>::iterator itr = m_sLoadedBitmaps.find (m_DataPath);
134- if (itr != m_sLoadedBitmaps.end ())
135- {
136- // Yes, has been loaded previously, then use that data from memory.
137- pReturnBitmap = (*itr).second ;
138- }
139- // Hasn't been loaded previously, so go ahead and do so now.
140- else
141- {
142- // Load the BITMAP from file and take ownership of it
143- pReturnBitmap = LoadAndReleaseBitmap (conversionMode);
144-
145- // Now when loaded for the first time, enter into the map, PASSING OVER OWNERSHIP OF THE LOADED DATAFILE
146- m_sLoadedBitmaps.insert (pair<string, BITMAP *>(m_DataPath, pReturnBitmap));
147- }
148-
149- // Return without transferring ownership
150- return pReturnBitmap;
151- }
152-
153-
154-
155- // ////////////////////////////////////////////////////////////////////////////////////////
156- // Virtual method: LoadAndReleaseBitmap
157- // ////////////////////////////////////////////////////////////////////////////////////////
158- // Description: Loads and transfers the data represtented by this ConentFile object as
159- // an Allegro BITMAP. Note that ownership of the BITMAP IS TRANSFERRED!
160- // Also, this is relatively slow since it reads the data from disk each time.
161-
162- BITMAP * ContentFile::LoadAndReleaseBitmap (int conversionMode)
163- {
164- if (m_DataPath.empty ())
165- return 0 ;
166-
167- BITMAP *pReturnBitmap = 0 ;
168- // Set the conversion mode, default to COLORCONV_MOST if none was set
169- set_color_conversion (conversionMode == 0 ? COLORCONV_MOST : conversionMode);
170-
171- // Find where the '#' denoting the divider between the datafile and the datafile object's name is
172- int separatorPos = m_DataPath.rfind (' #' );
173-
174- // If there is none, that means we're told to load an exposed file outside of a .dat datafile.
175- if (separatorPos == -1 )
176- {
177- PACKFILE *pFile = pack_fopen (m_DataPath.c_str (), F_READ );
178- // Make sure we opened properly, or try to add 000 before the extension if it's part of an animation naming
179- if (!pFile)
180- {
181- // Truncate away the extension
182- int extensionPos = m_DataPath.rfind (' .' );
183- m_DataPath.resize (extensionPos);
184- pFile = pack_fopen ((m_DataPath + " 000.bmp" ).c_str (), F_READ );
185-
186- }
187-
188- // Get the current color palette; the load_bmp function needs it
189- PALETTE currentPalette;
190- get_palette (currentPalette);
191-
192- // Now load the actual BITMAP
193- pReturnBitmap = load_bmp_pf (pFile, (RGB *)currentPalette);
194-
195- // Close the file stream
196- pack_fclose (pFile);
197- }
198- return pReturnBitmap;
199- }
62+ PALETTE currentPalette;
63+ get_palette (currentPalette);
64+ return load_bmp (m_DataPath.c_str (), currentPalette);
65+ }
20066}
0 commit comments