1111#include " callbacks.h"
1212#include " globals.h"
1313#include " lua/GameDataLoader.h"
14+ #include < libsiedler2/Archiv.h>
15+ #include < libsiedler2/ArchivItem_Bitmap.h>
16+ #include < libsiedler2/ArchivItem_Palette.h>
17+ #include < libsiedler2/ErrorCodes.h>
18+ #include < libsiedler2/libsiedler2.h>
1419#include < glad/glad.h>
1520#include < iostream>
1621
@@ -138,8 +143,6 @@ bool CGame::Init()
138143 std::cout << " failure" ;
139144 return false ;
140145 }
141- CFile::init ();
142-
143146 /* NOTE: its important to load a palette at first,
144147 * otherwise all images will be black (in exception of the LBM-Files, they have their own palette).
145148 * if its necessary to load pictures from a
@@ -153,20 +156,25 @@ bool CGame::Init()
153156 // load some pictures (after all the splash-screens)
154157 // at first GFX/PICS/SETUP997.LBM, cause this is the S2-loading picture
155158 std::cout << " \n Loading file: GFX/PICS/SETUP997.LBM..." ;
156- if (!CFile::open_file ( global::gameDataFilePath / " GFX/PICS/SETUP997.LBM" , LBM ))
159+ if (!global::loadArchive (ArchiveID::Setup997, global::gameDataFilePath / " GFX/PICS/SETUP997.LBM" , nullptr ))
157160 {
158161 std::cout << " failure" ;
159162 // if SETUP997.LBM doesn't exist, it's probably settlers2+mission cd and there we have SETUP998.LBM instead
160163 std::cout << " \n Try to load file: GFX/PICS/SETUP998.LBM instead..." ;
161- if (!CFile::open_file ( global::gameDataFilePath / " GFX/PICS/SETUP998.LBM" , LBM ))
164+ if (!global::loadArchive (ArchiveID::Setup997, global::gameDataFilePath / " GFX/PICS/SETUP998.LBM" , nullptr ))
162165 {
163166 std::cout << " failure" ;
164167 return false ;
165168 }
166169 }
167170
168171 // Create texture for splash background
169- splashBg_.load (global::bmpArray[SPLASHSCREEN_LOADING_S2SCREEN ].surface .get (), true );
172+ {
173+ auto & archiv = global::typedArchives[ArchiveID::Setup997];
174+ auto * bmp = dynamic_cast <const libsiedler2::baseArchivItem_Bitmap*>(archiv.get (0 ));
175+ if (bmp)
176+ splashBg_.load (*bmp, true );
177+ }
170178
171179 // std::cout << "\nShow loading screen...";
172180 showLoadScreen = true ;
@@ -182,125 +190,158 @@ bool CGame::Init()
182190 }
183191
184192 // continue loading pictures
185- for (const std::string file :
186- {" GFX/PICS/SETUP000.LBM" , " GFX/PICS/SETUP010.LBM" , " GFX/PICS/SETUP011.LBM" , " GFX/PICS/SETUP012.LBM" ,
187- " GFX/PICS/SETUP013.LBM" , " GFX/PICS/SETUP014.LBM" , " GFX/PICS/SETUP015.LBM" })
193+ const struct
188194 {
189- std::cout << " \n Loading file: " << file << " ..." ;
190- if (!CFile::open_file (global::gameDataFilePath / file, LBM ))
195+ ArchiveID id;
196+ const char * path;
197+ } setupFiles[] = {
198+ {ArchiveID::Setup000, " GFX/PICS/SETUP000.LBM" }, {ArchiveID::Setup010, " GFX/PICS/SETUP010.LBM" },
199+ {ArchiveID::Setup011, " GFX/PICS/SETUP011.LBM" }, {ArchiveID::Setup012, " GFX/PICS/SETUP012.LBM" },
200+ {ArchiveID::Setup013, " GFX/PICS/SETUP013.LBM" }, {ArchiveID::Setup014, " GFX/PICS/SETUP014.LBM" },
201+ {ArchiveID::Setup015, " GFX/PICS/SETUP015.LBM" },
202+ };
203+ for (const auto & sf : setupFiles)
204+ {
205+ std::cout << " \n Loading file: " << sf.path << " ..." ;
206+ if (!global::loadArchive (sf.id , global::gameDataFilePath / sf.path , nullptr ))
191207 {
192208 std::cout << " failure" ;
193209 // if it doesn't exist, it's probably settlers2+missioncd and we simply load SETUP010.LBM instead
194210 std::cout << " \n Loading file: GFX/PICS/SETUP010.LBM instead..." ;
195- if (!CFile::open_file ( global::gameDataFilePath / " GFX/PICS/SETUP010.LBM" , LBM ))
211+ if (!global::loadArchive (ArchiveID::Setup010, global::gameDataFilePath / " GFX/PICS/SETUP010.LBM" , nullptr ))
196212 {
197213 std::cout << " failure" ;
198214 return false ;
199215 }
200216 }
201217 }
202218
203- for (const std::string file :
204- {" GFX/PICS/SETUP666.LBM" , " GFX/PICS/SETUP667.LBM" , " GFX/PICS/SETUP801.LBM" , " GFX/PICS/SETUP802.LBM" ,
205- " GFX/PICS/SETUP803.LBM" , " GFX/PICS/SETUP804.LBM" , " GFX/PICS/SETUP805.LBM" , " GFX/PICS/SETUP806.LBM" ,
206- " GFX/PICS/SETUP810.LBM" , " GFX/PICS/SETUP811.LBM" , " GFX/PICS/SETUP895.LBM" , " GFX/PICS/SETUP896.LBM" })
207- {
208- std::cout << " \n Loading file: " << file << " ..." ;
209- if (!CFile::open_file (global::gameDataFilePath / file, LBM ))
219+ { // batch 2: SETUP666-896
220+ const struct
210221 {
211- std::cout << " failure" ;
212- return false ;
213- }
214- }
215-
216- for (const std::string file : {" GFX/PICS/SETUP897.LBM" , " GFX/PICS/SETUP898.LBM" })
217- {
218- std::cout << " \n Loading file: " << file << " ..." ;
219- if (!CFile::open_file (global::gameDataFilePath / file, LBM ))
222+ ArchiveID id;
223+ const char * path;
224+ } files[] = {
225+ {ArchiveID::Setup666, " GFX/PICS/SETUP666.LBM" }, {ArchiveID::Setup667, " GFX/PICS/SETUP667.LBM" },
226+ {ArchiveID::Setup801, " GFX/PICS/SETUP801.LBM" }, {ArchiveID::Setup802, " GFX/PICS/SETUP802.LBM" },
227+ {ArchiveID::Setup803, " GFX/PICS/SETUP803.LBM" }, {ArchiveID::Setup804, " GFX/PICS/SETUP804.LBM" },
228+ {ArchiveID::Setup805, " GFX/PICS/SETUP805.LBM" }, {ArchiveID::Setup806, " GFX/PICS/SETUP806.LBM" },
229+ {ArchiveID::Setup810, " GFX/PICS/SETUP810.LBM" }, {ArchiveID::Setup811, " GFX/PICS/SETUP811.LBM" },
230+ {ArchiveID::Setup895, " GFX/PICS/SETUP895.LBM" }, {ArchiveID::Setup896, " GFX/PICS/SETUP896.LBM" },
231+ };
232+ for (const auto & f : files)
220233 {
221- std::cout << " failure" ;
222- // if it doesn't exist, it's probably settlers2+missioncd and we simply load SETUP896.LBM instead
223- std::cout << " \n Loading file: GFX/PICS/SETUP896.LBM instead..." ;
224- if (!CFile::open_file (global::gameDataFilePath / " GFX/PICS/SETUP896.LBM" , LBM ))
234+ std::cout << " \n Loading file: " << f.path << " ..." ;
235+ if (!global::loadArchive (f.id , global::gameDataFilePath / f.path , nullptr ))
225236 {
226237 std::cout << " failure" ;
227238 return false ;
228239 }
229240 }
230241 }
231242
232- for (const std::string file :
233- {" GFX/PICS/SETUP899.LBM" , " GFX/PICS/SETUP990.LBM" , " GFX/PICS/WORLD.LBM" , " GFX/PICS/WORLDMSK.LBM" })
234- {
235- std::cout << " \n Loading file: " << file << " ..." ;
236- if (!CFile::open_file (global::gameDataFilePath / file, LBM ))
243+ { // batch 3: SETUP897-898 with fallback to SETUP896
244+ const struct
237245 {
238- std::cout << " failure" ;
239- return false ;
246+ ArchiveID id;
247+ const char * path;
248+ } files[] = {
249+ {ArchiveID::Setup897, " GFX/PICS/SETUP897.LBM" },
250+ {ArchiveID::Setup898, " GFX/PICS/SETUP898.LBM" },
251+ };
252+ for (const auto & f : files)
253+ {
254+ std::cout << " \n Loading file: " << f.path << " ..." ;
255+ if (!global::loadArchive (f.id , global::gameDataFilePath / f.path , nullptr ))
256+ {
257+ std::cout << " failure" ;
258+ std::cout << " \n Loading file: GFX/PICS/SETUP896.LBM instead..." ;
259+ if (!global::loadArchive (f.id , global::gameDataFilePath / " GFX/PICS/SETUP896.LBM" , nullptr ))
260+ {
261+ std::cout << " failure" ;
262+ return false ;
263+ }
264+ }
240265 }
241266 }
242267
243- // load gouraud data
244- for (const std::string file : {" DATA/TEXTURES/GOU5.DAT" , " DATA/TEXTURES/GOU6.DAT" , " DATA/TEXTURES/GOU7.DAT" })
245- {
246- std::cout << " \n Loading file: " << file << " ..." ;
247- if (!CFile::open_file (global::gameDataFilePath / file, GOU ))
268+ { // batch 4: remaining loading screens
269+ const struct
248270 {
249- std::cout << " failure" ;
250- return false ;
271+ ArchiveID id;
272+ const char * path;
273+ } files[] = {
274+ {ArchiveID::Setup899, " GFX/PICS/SETUP899.LBM" },
275+ {ArchiveID::Setup990, " GFX/PICS/SETUP990.LBM" },
276+ {ArchiveID::WorldLbm, " GFX/PICS/WORLD.LBM" },
277+ {ArchiveID::WorldMskLbm, " GFX/PICS/WORLDMSK.LBM" },
278+ };
279+ for (const auto & f : files)
280+ {
281+ std::cout << " \n Loading file: " << f.path << " ..." ;
282+ if (!global::loadArchive (f.id , global::gameDataFilePath / f.path , nullptr ))
283+ {
284+ std::cout << " failure" ;
285+ return false ;
286+ }
251287 }
252288 }
253289
254- // load only the palette at this time from editres.idx
255- std::cout << " \n Loading palette from file: DATA/EDITRES.IDX ..." ;
256- if (!CFile::open_file (global::gameDataFilePath / " DATA/EDITRES.IDX " , IDX , true ))
290+ // Load the default palette first so all subsequent loads have it available
291+ std::cout << " \n Loading default palette from file: GFX/PALETTE/PAL5.BBM ..." ;
292+ if (!global::loadPalette (global::gameDataFilePath / " GFX/PALETTE/PAL5.BBM " ))
257293 {
258294 std::cout << " failure" ;
259295 return false ;
260296 }
261- // set the right palette
262- CFile::set_palActual (CFile::get_palArray () - 1 );
263- std::cout << " \n Loading file: DATA/EDITRES.IDX..." ;
264- if (!CFile::open_file (global::gameDataFilePath / " DATA/EDITRES.IDX" , IDX ))
265- {
266- std::cout << " failure" ;
267- return false ;
268- }
269- // set back palette
270- CFile::set_palActual (CFile::get_palArray ());
271- // load only the palette at this time from editio.idx
272- std::cout << " \n Loading palette from file: DATA/IO/EDITIO.IDX..." ;
273- if (!CFile::open_file (global::gameDataFilePath / " DATA/IO/EDITIO.IDX" , IDX , true ))
297+
298+ // Load EDITRES.IDX as a complete bundle in typedArchives
274299 {
275- std::cout << " failure" ;
276- return false ;
300+ libsiedler2::Archiv editres;
301+ int ec = libsiedler2::Load (global::gameDataFilePath / " DATA/EDITRES.IDX" , editres, global::currentPalette);
302+ if (ec)
303+ {
304+ std::cout << " \n Error loading EDITRES.IDX: " << libsiedler2::getErrorString (ec) << std::endl;
305+ return false ;
306+ }
307+ // The palette in EDITRES (item 1) may override the default
308+ auto * pal = dynamic_cast <libsiedler2::ArchivItem_Palette*>(editres.get (1 ));
309+ if (pal)
310+ global::currentPalette = pal;
311+ // Store the complete Archiv in typedArchives — fonts stay inside for CFont to read directly
312+ // Indices match file positions: 0=Font, 1=Palette, 2=Font, 3=Font, 4-56=Bitmaps
313+ global::typedArchives[ArchiveID::Resource] = std::move (editres);
277314 }
278- // set the right palette
279- CFile::set_palActual (CFile::get_palArray () - 1 );
315+
280316 std::cout << " \n Loading file: DATA/IO/EDITIO.IDX..." ;
281- if (!CFile::open_file ( global::gameDataFilePath / " DATA/IO/EDITIO.IDX" , IDX ))
317+ if (!global::loadArchive (ArchiveID::Io, global::gameDataFilePath / " DATA/IO/EDITIO.IDX" , global::currentPalette ))
282318 {
283319 std::cout << " failure" ;
284320 return false ;
285321 }
286- // set back palette
287- CFile::set_palActual ( CFile::get_palArray ());
322+ std::cout << " done " ;
323+
288324 std::cout << " \n Loading file: DATA/EDITBOB.LST..." ;
289- if (!CFile::open_file (global::gameDataFilePath / " DATA/EDITBOB.LST" , LST ))
325+ if (!global::loadArchive (ArchiveID::EditorBob, global::gameDataFilePath / " DATA/EDITBOB.LST" ,
326+ global::currentPalette))
290327 {
291328 std::cout << " failure" ;
292329 return false ;
293330 }
331+ std::cout << " done" ;
294332
295333 // texture tilesets
296- for (const std::string file : {" GFX/TEXTURES/TEX5.LBM" , " GFX/TEXTURES/TEX6.LBM" , " GFX/TEXTURES/TEX7.LBM" })
334+ const ArchiveID texArchives[3 ] = {ArchiveID::Tex5, ArchiveID::Tex6, ArchiveID::Tex7};
335+ const std::string texFiles[3 ] = {" GFX/TEXTURES/TEX5.LBM" , " GFX/TEXTURES/TEX6.LBM" , " GFX/TEXTURES/TEX7.LBM" };
336+ for (unsigned ti = 0 ; ti < 3 ; ti++)
297337 {
298- std::cout << " \n Loading file: " << file << " ..." ;
299- if (!CFile::open_file ( global::gameDataFilePath / file, LBM ))
338+ std::cout << " \n Loading file: " << texFiles[ti] << " ..." ;
339+ if (!global::loadTileset (texArchives[ti], ti, global::gameDataFilePath / texFiles[ti] ))
300340 {
301341 std::cout << " failure" ;
302342 return false ;
303343 }
344+ std::cout << " done" ;
304345 }
305346
306347 /*
@@ -313,11 +354,14 @@ bool CGame::Init()
313354 */
314355
315356 // EVERY MISSION-FILE SHOULD BE LOADED SEPARATLY IF THE SPECIFIED MISSION GOES ON -- SO THIS IS TEMPORARY
316- for (const std::string file : {" DATA/MIS0BOBS.LST" , " DATA/MIS1BOBS.LST" , " DATA/MIS2BOBS.LST" , " DATA/MIS3BOBS.LST" ,
317- " DATA/MIS4BOBS.LST" , " DATA/MIS5BOBS.LST" })
357+ const ArchiveID misArchives[] = {ArchiveID::Mis0Bobs, ArchiveID::Mis1Bobs, ArchiveID::Mis2Bobs,
358+ ArchiveID::Mis3Bobs, ArchiveID::Mis4Bobs, ArchiveID::Mis5Bobs};
359+ const std::string misFiles[] = {" DATA/MIS0BOBS.LST" , " DATA/MIS1BOBS.LST" , " DATA/MIS2BOBS.LST" ,
360+ " DATA/MIS3BOBS.LST" , " DATA/MIS4BOBS.LST" , " DATA/MIS5BOBS.LST" };
361+ for (unsigned mi = 0 ; mi < 6 ; mi++)
318362 {
319- std::cout << " \n Loading file: " << file << " ..." ;
320- if (!CFile::open_file ( global::gameDataFilePath / file, LST ))
363+ std::cout << " \n Loading file: " << misFiles[mi] << " ..." ;
364+ if (!global::loadArchive (misArchives[mi], global::gameDataFilePath / misFiles[mi], global::currentPalette ))
321365 {
322366 std::cout << " failure" ;
323367 return false ;
@@ -327,10 +371,14 @@ bool CGame::Init()
327371 // create the mainmenu
328372 callback::mainmenu (INITIALIZING_CALL );
329373
330- // Create textures for cursor
331- cursor_.load (global::bmpArray[CURSOR ].surface .get ());
332- cursorClicked_.load (global::bmpArray[CURSOR_CLICKED ].surface .get ());
333- cross_.load (global::bmpArray[CROSS ].surface .get ());
374+ // Create textures for cursor (from typed archive)
375+ auto & resArchiv = global::typedArchives[ArchiveID::Resource];
376+ if (auto * bmp = dynamic_cast <const libsiedler2::baseArchivItem_Bitmap*>(resArchiv.get (CURSOR )))
377+ cursor_.load (*bmp);
378+ if (auto * bmp = dynamic_cast <const libsiedler2::baseArchivItem_Bitmap*>(resArchiv.get (CURSOR_CLICKED )))
379+ cursorClicked_.load (*bmp);
380+ if (auto * bmp = dynamic_cast <const libsiedler2::baseArchivItem_Bitmap*>(resArchiv.get (CROSS )))
381+ cross_.load (*bmp);
334382
335383 return true ;
336384}
0 commit comments