forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic_loader.cpp
880 lines (743 loc) · 24 KB
/
pic_loader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
// This file is part of openCaesar3.
//
// openCaesar3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2012-2013 Gregoire Athanase, gathanase@gmail.com
#include "pic_loader.hpp"
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
#include <memory>
#include <iomanip>
#include <archive.h>
#include <archive_entry.h>
#include <sys/stat.h>
#include <map>
#include <SDL.h>
#include <SDL_image.h>
#include "oc3_positioni.h"
#include "exception.hpp"
#include "sdl_facade.hpp"
namespace {
static const char* rcCartsGroup = "carts";
static const char* rcCommerceGroup = "commerce";
static const char* rcEntertaimentGroup = "entertainment";
static const char* rcWarehouseGroup = "warehouse";
}
PicMetaData* PicMetaData::_instance = NULL;
PicMetaData& PicMetaData::instance()
{
if (_instance == NULL)
{
_instance = new PicMetaData();
if (_instance == NULL) THROW("Memory error, cannot instantiate object");
}
return *_instance;
}
PicMetaData::PicMetaData()
{
_dummy_data.xoffset = 0;
_dummy_data.yoffset = 0;
// tiles
PicInfo info;
info.xoffset = -1;
info.yoffset = -1;
setRange("land1a", 1, 303, info);
setRange("oc3_land", 1, 2, info);
setRange("land2a", 1, 151, info);
setRange("land3a", 47, 92, info);
setRange("plateau", 1, 44, info);
setRange(rcCommerceGroup, 1, 167, info);
setRange("transport", 1, 93, info);
setRange("security", 1, 61, info);
setRange(rcEntertaimentGroup, 1, 116, info);
setRange("housng1a", 1, 51, info);
setRange(rcWarehouseGroup, 19, 83, info);
setRange("utilitya", 1, 42, info);
setRange("govt", 1, 10, info);
setOne(rcEntertaimentGroup, 12, 37, 62); // amphitheater
setOne(rcEntertaimentGroup, 35, 34, 37); // theater
setOne(rcEntertaimentGroup, 50, 70, 105); // collosseum
// animations
info.xoffset = 42;
info.yoffset = 34;
setRange(rcCommerceGroup, 2, 11, info); // market poor
info.xoffset = 66;
info.yoffset = 44;
setRange(rcCommerceGroup, 44, 53, info); // marble
info.xoffset = 45;
info.yoffset = 18;
setRange(rcCommerceGroup, 55, 60, info); // iron
info.xoffset = 15;
info.yoffset = 32;
setRange(rcCommerceGroup, 62, 71, info); // clay
info.xoffset = 35;
info.yoffset = 6;
setRange(rcCommerceGroup, 73, 82, info); // timber
info.xoffset = 14;
info.yoffset = 36;
setRange(rcCommerceGroup, 87, 98, info); // wine
info.xoffset = 0;
info.yoffset = 45;
setRange(rcCommerceGroup, 100, 107, info); // oil
info.xoffset = 42;
info.yoffset = 36;
setRange(rcCommerceGroup, 109, 116, info); // weapons
info.xoffset = 38;
info.yoffset = 39;
setRange(rcCommerceGroup, 118, 131, info); // furniture
info.xoffset = 65;
info.yoffset = 42;
setRange(rcCommerceGroup, 133, 139, info); // pottery
info.xoffset = 65;
info.yoffset = 42;
setRange(rcCommerceGroup, 159, 167, info); // market rich
// stock of input good
setOne(rcCommerceGroup, 153, 45, -8); // grapes
setOne(rcCommerceGroup, 154, 37, -2); // olive
setOne(rcCommerceGroup, 155, 48, -4); // timber
setOne(rcCommerceGroup, 156, 47, -11); // iron
setOne(rcCommerceGroup, 157, 47, -9); // clay
// warehouse
setOne(rcWarehouseGroup, 1, 60, 56);
setOne(rcWarehouseGroup, 18, 56, 93);
info.xoffset = 55;
info.yoffset = 75;
setRange(rcWarehouseGroup, 2, 17, info);
info.xoffset = 79;
info.yoffset = 108;
setRange(rcWarehouseGroup, 84, 91, info);
// granary
setOne(rcCommerceGroup, 141, 28, 109);
setOne(rcCommerceGroup, 142, 33, 75);
setOne(rcCommerceGroup, 143, 56, 65);
setOne(rcCommerceGroup, 144, 92, 65);
setOne(rcCommerceGroup, 145, 118, 76);
setOne(rcCommerceGroup, 146, 78, 69);
setOne(rcCommerceGroup, 147, 78, 69);
setOne(rcCommerceGroup, 148, 78, 69);
setOne(rcCommerceGroup, 149, 78, 69);
setOne(rcCommerceGroup, 150, 78, 69);
setOne(rcCommerceGroup, 151, 78, 69);
setOne(rcCommerceGroup, 152, 78, 69);
// walkers
info.xoffset = -2;
info.yoffset = -2;
setRange("citizen01", 1, 1240, info);
setRange("citizen02", 1, 1030, info);
setRange("citizen03", 1, 1128, info);
setRange("citizen04", 1, 577, info);
setRange("citizen05", 1, 184, info);
}
void PicMetaData::setRange(const std::string &preffix, const int first, const int last, PicInfo &data)
{
for (int i = first; i<=last; ++i)
{
setOne(preffix, i, data);
}
}
void PicMetaData::setOne(const std::string &preffix, const int index, PicInfo &data)
{
std::ostringstream oss;
oss << preffix << "_" << std::setw(5) << std::setfill('0') << index;
std::string resource_name = oss.str();
_data[resource_name] = data;
}
void PicMetaData::setOne(const std::string &preffix, const int index, const int xoffset, const int yoffset)
{
std::ostringstream oss;
oss << preffix << "_" << std::setw(5) << std::setfill('0') << index;
std::string resource_name = oss.str();
PicInfo data;
data.xoffset = xoffset;
data.yoffset = yoffset;
_data[resource_name] = data;
}
PicInfo& PicMetaData::get_data(const std::string &resource_name)
{
std::map<std::string, PicInfo>::iterator it = _data.find(resource_name);
if (it == _data.end())
{
return _dummy_data;
// THROW("Invalid resource name: " << resource_name);
}
return (*it).second;
}
PicLoader* PicLoader::_instance = NULL;
PicLoader& PicLoader::instance()
{
if (_instance == NULL)
{
_instance = new PicLoader();
if (_instance == NULL) THROW("Memory error, cannot instantiate object");
}
return *_instance;
}
PicLoader::PicLoader() { }
void PicLoader::set_picture(const std::string &name, SDL_Surface &surface)
{
// first: we deallocate the current picture, if any
std::map<std::string, Picture>::iterator it = _resources.find(name);
if (it != _resources.end())
{
Picture &pic = it->second;
SDL_FreeSurface(pic.get_surface());
}
_resources[name] = make_picture(&surface, name);
}
Picture& PicLoader::get_picture(const std::string &name)
{
std::map<std::string, Picture>::iterator it = _resources.find(name);
if (it == _resources.end()) THROW("Unknown resource " << name);
return it->second;
}
Picture& PicLoader::get_picture(const std::string &prefix, const int idx)
{
std::stringstream ss;
ss << prefix << "_" << std::setw(5) << std::setfill('0') << idx << ".png";
std::string resource_name = ss.str();
return get_picture(resource_name);
}
Picture& PicLoader::get_picture_good(const GoodType goodType)
{
int pic_index;
switch (goodType)
{
case G_WHEAT:
pic_index = 317;
break;
case G_VEGETABLE:
pic_index = 318;
break;
case G_FRUIT:
pic_index = 319;
break;
case G_OLIVE:
pic_index = 320;
break;
case G_GRAPE:
pic_index = 321;
break;
case G_MEAT:
pic_index = 322;
break;
case G_WINE:
pic_index = 323;
break;
case G_OIL:
pic_index = 324;
break;
case G_IRON:
pic_index = 325;
break;
case G_TIMBER:
pic_index = 326;
break;
case G_CLAY:
pic_index = 327;
break;
case G_MARBLE:
pic_index = 328;
break;
case G_WEAPON:
pic_index = 329;
break;
case G_FURNITURE:
pic_index = 330;
break;
case G_POTTERY:
pic_index = 331;
break;
case G_FISH:
pic_index = 333;
break;
default:
THROW("This good type has no picture:" << goodType);
}
return get_picture("paneling", pic_index);
}
std::list<Picture*> PicLoader::get_pictures()
{
std::list<Picture*> pictures;
for (std::map<std::string, Picture>::iterator it = _resources.begin(); it != _resources.end(); ++it)
{
// for every resource
Picture& pic = (*it).second;
pictures.push_back(&pic);
}
return pictures;
}
void PicLoader::load_wait()
{
std::string aPath = "resources/pics/";
load_archive(aPath+"pics_wait.zip");
std::cout << "number of images loaded: " << _resources.size() << std::endl;
}
void PicLoader::load_all()
{
std::string aPath = "resources/pics/";
load_archive(aPath+"pics.zip");
load_archive(aPath+"pics_oc3.zip");
std::cout << "number of images loaded: " << _resources.size() << std::endl;
}
void PicLoader::load_archive(const std::string &filename)
{
std::cout << "reading image archive: " << filename << std::endl;
struct archive *a;
struct archive_entry *entry;
int rc;
a = archive_read_new();
archive_read_support_compression_all(a);
archive_read_support_format_all(a);
rc = archive_read_open_filename(a, filename.c_str(), 16384); // block size
if (rc != ARCHIVE_OK) THROW("Cannot open archive " << filename);
SDL_Surface *surface;
SDL_RWops *rw;
const Uint32 bufferSize = 10000000; // allocated buffer
std::auto_ptr< Uint8 > buffer( new Uint8[ bufferSize ] );
if( buffer.get() == 0 )
THROW("Memory error, cannot allocate buffer size " << bufferSize);
std::string entryname;
while (archive_read_next_header(a, &entry) == ARCHIVE_OK)
{
// for all entries in archive
entryname = archive_entry_pathname(entry);
if ((archive_entry_stat(entry)->st_mode & S_IFREG) == 0)
{
// not a regular file (maybe a directory). skip it.
continue;
}
if (archive_entry_stat(entry)->st_size >= bufferSize)
THROW("Cannot load archive: file is too big " << entryname << " in archive " << filename);
int size = archive_read_data(a, buffer.get(), bufferSize); // read data into buffer
rw = SDL_RWFromMem(buffer.get(), size);
surface = IMG_Load_RW(rw, 0);
set_picture(entryname, *surface);
SDL_FreeRW(rw);
}
rc = archive_read_finish(a);
if (rc != ARCHIVE_OK) THROW("Error while reading archive " << filename);
}
Picture PicLoader::make_picture(SDL_Surface *surface, const std::string& resource_name) const
{
int xoffset = 0;
int yoffset = 0;
// decode the picture name => to set the offset manually
// resource_name = "buildings/Govt_00005.png"
int dot_pos = resource_name.find('.');
std::string filename = resource_name.substr(0, dot_pos);
PicInfo& pic_info = PicMetaData::instance().get_data(filename);
if (pic_info.xoffset == -1 && pic_info.yoffset == -1)
{
// this is a tiled picture=> automatic offset correction
yoffset= surface->h-15*((surface->w+2)/60); // (w+2)/60 is the size of the tile: (1x1, 2x2, 3x3, ...)
}
else if (pic_info.xoffset == -2 && pic_info.yoffset == -2)
{
// this is a walker picture=> automatic offset correction
xoffset= -surface->w/2;
yoffset= int(surface->h*3./4.);
}
else
{
xoffset= pic_info.xoffset;
yoffset= pic_info.yoffset;
}
Picture pic;
pic.init(surface, xoffset, yoffset);
pic.set_name(filename);
return pic;
}
int PicLoader::get_pic_id_by_name(std::string &pic_name)
{
// example: for land1a_00004.png, return 244+4=248
std::string res_pfx; // resource name prefix = land1a
int res_id; // idx of resource = 4
// extract the name and idx from name (ex: [land1a, 4])
int pos = pic_name.find("_");
res_pfx = pic_name.substr(0, pos);
std::stringstream ss(pic_name.substr(pos+1));
ss >> res_id;
if (res_pfx == "plateau")
{
res_id += 200;
}
else if (res_pfx == "land1a")
{
res_id += 244;
}
else if (res_pfx == "land2a")
{
res_id += 547;
}
else if (res_pfx == "land3a")
{
res_id += 778;
}
else
{
THROW("Unknown image " << pic_name);
}
return res_id;
}
Picture& PicLoader::get_pic_by_id(const int imgId)
{
// example: for land1a_00004.png, pfx=land1a and id=4
std::string res_pfx; // resource name prefix
int res_id = imgId; // id of resource
if (201<=imgId && imgId <245)
{
res_pfx = "plateau";
res_id = imgId-200;
}
else if (245<=imgId && imgId <548)
{
res_pfx = "land1a";
res_id = imgId-244;
}
else if (548<=imgId && imgId <779)
{
res_pfx = "land2a";
res_id = imgId-547;
}
else if (779<=imgId && imgId <871)
{
res_pfx = "land3a";
res_id = imgId-778;
}
else
{
res_pfx = "land1a";
res_id = 1;
// THROW("Unknown image Id " << imgId);
}
return get_picture(res_pfx, res_id);
}
AnimLoader::AnimLoader(PicLoader &loader)
{
_loader = &loader;
}
void AnimLoader::fill_animation(Animation &oAnim, const std::string &prefix, const int start, const int number, const int step)
{
std::vector<Picture*> pictures;
for (int i = 0; i < number; ++i)
{
Picture &pic = _loader->get_picture(prefix, start+i*step);
pictures.push_back(&pic);
}
oAnim.init(pictures);
}
void AnimLoader::fill_animation_reverse(Animation &oAnim, const std::string &prefix, const int start, const int number, const int step)
{
std::vector<Picture*> pictures;
for (int i = 0; i < number; ++i)
{
Picture &pic = _loader->get_picture(prefix, start - i * step);
pictures.push_back(&pic);
}
oAnim.init(pictures);
}
void AnimLoader::change_offset(Animation &ioAnim, const int xoffset, const int yoffset)
{
std::vector<Picture*>::iterator it;
for (it = ioAnim.get_pictures().begin(); it != ioAnim.get_pictures().end(); ++it)
{
Picture &pic = **it;
pic.set_offset(xoffset, yoffset);
}
}
bool operator<(const WalkerAction &v1, const WalkerAction &v2)
{
if (v1._action!=v2._action)
{
return v1._action < v2._action;
}
else
{
return v1._direction < v2._direction;
}
}
WalkerLoader* WalkerLoader::_instance = NULL;
WalkerLoader& WalkerLoader::instance()
{
if (_instance == NULL)
{
_instance = new WalkerLoader();
if (_instance == NULL) THROW("Memory error, cannot instantiate object");
}
return *_instance;
}
WalkerLoader::WalkerLoader() { }
void WalkerLoader::loadAll()
{
_animations.resize(30); // number of walker types
std::map<WalkerAction, Animation> map;
map.clear();
fillWalk(map, "citizen01", 1, 12);
_animations[WG_POOR] = map;
map.clear();
fillWalk(map, "citizen01", 105, 12);
_animations[WG_BATH] = map;
map.clear();
fillWalk(map, "citizen01", 209, 12);
_animations[WG_PRIEST] = map;
map.clear();
fillWalk(map, "citizen01", 313, 12);
_animations[WG_ACTOR] = map;
map.clear();
fillWalk(map, "citizen01", 417, 12);
_animations[WG_TAMER] = map;
map.clear();
fillWalk(map, "citizen01", 617, 12);
_animations[WG_TAX] = map;
map.clear();
fillWalk(map, "citizen01", 721, 12);
_animations[WG_CHILD] = map;
map.clear();
fillWalk(map, "citizen01", 825, 12);
_animations[WG_MARKETLADY] = map;
map.clear();
fillWalk(map, "citizen01", 929, 12);
_animations[WG_PUSHER] = map;
map.clear();
fillWalk(map, "citizen01", 1033, 12);
_animations[WG_PUSHER2] = map;
map.clear();
fillWalk(map, "citizen01", 1137, 12);
_animations[WG_ENGINEER] = map;
map.clear();
fillWalk(map, "citizen02", 1, 12);
_animations[WG_GLADIATOR] = map;
map.clear();
fillWalk(map, "citizen02", 199, 12);
_animations[WG_GLADIATOR2] = map;
map.clear();
fillWalk(map, "citizen02", 351, 12);
_animations[WG_RIOTER] = map;
map.clear();
fillWalk(map, "citizen02", 463, 12);
_animations[WG_BARBER] = map;
map.clear();
fillWalk(map, "citizen02", 615, 12);
_animations[WG_PREFECT] = map;
map.clear();
fillWalk(map, "citizen02", 911, 12);
_animations[WG_HOMELESS] = map;
map.clear();
fillWalk(map, "citizen03", 713, 12);
_animations[WG_RICH] = map;
map.clear();
fillWalk(map, "citizen03", 817, 12);
_animations[WG_DOCTOR] = map;
map.clear();
fillWalk(map, "citizen03", 921, 12);
_animations[WG_RICH2] = map;
map.clear();
fillWalk(map, "citizen03", 1025, 12);
_animations[WG_LIBRARIAN] = map;
map.clear();
fillWalk(map, "citizen03", 553, 12);
_animations[WG_SOLDIER] = map;
map.clear();
fillWalk(map, "citizen03", 241, 12);
_animations[WG_JAVELINEER] = map;
map.clear();
fillWalk(map, "citizen04", 1, 12);
_animations[WG_HORSEMAN] = map;
map.clear();
fillWalk(map, rcCartsGroup, 145, 12);
_animations[WG_HORSE_CARAVAN] = map;
map.clear();
fillWalk(map, rcCartsGroup, 273, 12);
_animations[WG_CAMEL_CARAVAN] = map;
map.clear();
fillWalk(map, rcCartsGroup, 369, 12);
_animations[WG_LITTLE_HELPER] = map;
}
void WalkerLoader::fillWalk(std::map<WalkerAction, Animation> &ioMap, const std::string &prefix, const int start, const int size)
{
AnimLoader animLoader(PicLoader::instance());
WalkerAction action;
action._action = WA_MOVE;
action._direction = D_NORTH;
animLoader.fill_animation(ioMap[action], prefix, start , size, 8);
action._direction = D_NORTH_EAST;
animLoader.fill_animation(ioMap[action], prefix, start+1, size, 8);
action._direction = D_EAST;
animLoader.fill_animation(ioMap[action], prefix, start+2, size, 8);
action._direction = D_SOUTH_EAST;
animLoader.fill_animation(ioMap[action], prefix, start+3, size, 8);
action._direction = D_SOUTH;
animLoader.fill_animation(ioMap[action], prefix, start+4, size, 8);
action._direction = D_SOUTH_WEST;
animLoader.fill_animation(ioMap[action], prefix, start+5, size, 8);
action._direction = D_WEST;
animLoader.fill_animation(ioMap[action], prefix, start+6, size, 8);
action._direction = D_NORTH_WEST;
animLoader.fill_animation(ioMap[action], prefix, start+7, size, 8);
}
const std::map<WalkerAction, Animation>& WalkerLoader::getAnimationMap(const WalkerGraphicType walkerGraphic)
{
return _animations[walkerGraphic];
}
CartLoader* CartLoader::_instance = NULL;
CartLoader& CartLoader::instance()
{
if (_instance == NULL)
{
_instance = new CartLoader();
if (_instance == NULL) THROW("Memory error, cannot instantiate object");
}
return *_instance;
}
CartLoader::CartLoader()
{
loadAll();
}
void CartLoader::loadAll()
{
std::cout << "Loading cart graphics" << std::endl;
// FIX: size of std::vector to match number of carts with goods + emmigrants + immigrants
// _carts.resize(G_MAX);
_carts.resize(CT_MAX);
std::vector<Picture*> cart; // key=direction
bool frontCart = false;
fillCart(cart, rcCartsGroup, 1, frontCart);
_carts[G_NONE] = cart;
fillCart(cart, rcCartsGroup, 9, frontCart);
_carts[G_WHEAT] = cart;
fillCart(cart, rcCartsGroup, 17, frontCart);
_carts[G_VEGETABLE] = cart;
fillCart(cart, rcCartsGroup, 25, frontCart);
_carts[G_FRUIT] = cart;
fillCart(cart, rcCartsGroup, 33, frontCart);
_carts[G_OLIVE] = cart;
fillCart(cart, rcCartsGroup, 41, frontCart);
_carts[G_GRAPE] = cart;
fillCart(cart, rcCartsGroup, 49, frontCart);
_carts[G_MEAT] = cart;
fillCart(cart, rcCartsGroup, 57, frontCart);
_carts[G_WINE] = cart;
fillCart(cart, rcCartsGroup, 65, frontCart);
_carts[G_OIL] = cart;
fillCart(cart, rcCartsGroup, 73, frontCart);
_carts[G_IRON] = cart;
fillCart(cart, rcCartsGroup, 81, frontCart);
_carts[G_TIMBER] = cart;
fillCart(cart, rcCartsGroup, 89, frontCart);
_carts[G_CLAY] = cart;
fillCart(cart, rcCartsGroup, 97, frontCart);
_carts[G_MARBLE] = cart;
fillCart(cart, rcCartsGroup, 105, frontCart);
_carts[G_WEAPON] = cart;
fillCart(cart, rcCartsGroup, 113, frontCart);
_carts[G_FURNITURE] = cart;
fillCart(cart, rcCartsGroup, 121, frontCart);
_carts[G_POTTERY] = cart;
fillCart(cart, rcCartsGroup, 129, !frontCart);
_carts[G_SCARB1] = cart;
fillCart(cart, rcCartsGroup, 137, !frontCart);
_carts[G_SCARB2] = cart;
fillCart(cart, rcCartsGroup, 697, frontCart);
_carts[G_FISH] = cart;
}
namespace{
static const Point frontCartOffsetSouth = /*Point( -33, 35 )*/Point(-33,22);
static const Point frontCartOffsetWest = Point( -31, 35 );
static const Point frontCartOffsetNorth = Point( -5, 37 );
static const Point frontCartOffsetEast = Point( -5, 22 );
static const Point backCartOffsetSouth = Point( -5, 40 );
static const Point backCartOffsetWest = Point( -5, 22 );
static const Point backCartOffsetNorth = Point( -33, 22 );
static const Point backCartOffsetEast = Point( -31, 35 );
}
void CartLoader::fillCart(std::vector<Picture*> &ioCart, const std::string &prefix, const int start, bool back )
{
PicLoader &picLoader = PicLoader::instance();
ioCart.clear();
ioCart.resize(D_MAX);
ioCart[D_NORTH] = &picLoader.get_picture(rcCartsGroup, start);
ioCart[D_NORTH_EAST] = &picLoader.get_picture(rcCartsGroup, start + 1);
ioCart[D_EAST] = &picLoader.get_picture(rcCartsGroup, start + 2);
ioCart[D_SOUTH_EAST] = &picLoader.get_picture(rcCartsGroup, start + 3);
ioCart[D_SOUTH] = &picLoader.get_picture(rcCartsGroup, start + 4);
ioCart[D_SOUTH_WEST] = &picLoader.get_picture(rcCartsGroup, start + 5);
ioCart[D_WEST] = &picLoader.get_picture(rcCartsGroup, start + 6);
ioCart[D_NORTH_WEST] = &picLoader.get_picture(rcCartsGroup, start + 7);
ioCart[D_SOUTH]->set_offset( back ? backCartOffsetSouth : frontCartOffsetSouth);
ioCart[D_WEST]->set_offset ( back ? backCartOffsetWest : frontCartOffsetWest );
ioCart[D_NORTH]->set_offset( back ? backCartOffsetNorth : frontCartOffsetNorth);
ioCart[D_EAST]->set_offset ( back ? backCartOffsetEast : frontCartOffsetEast );
}
Picture& CartLoader::getCart(const GoodStock &stock, const DirectionType &direction)
{
Picture *res = NULL;
if (stock._currentQty == 0)
{
res = _carts.at( G_NONE ).at(direction);
}
else
{
res = _carts.at(stock._goodType).at(direction);
}
return *res;
}
Picture& CartLoader::getCart(GoodType cart, const DirectionType &direction)
{
Picture *res = NULL;
res = _carts.at( cart ).at( direction );
return *res;
}
Picture& CartLoader::getCart(CartTypes cart, const DirectionType &direction)
{
Picture *res = NULL;
res = _carts.at( cart ).at( direction );
return *res;
}
FontLoader::FontLoader()
{ }
void FontLoader::load_all()
{
std::string full_font_path = "FreeSerif.ttf";
TTF_Font *ttf;
SDL_Color black = {0, 0, 0, 0};
SDL_Color red = {160, 0, 0, 0}; // dim red
SDL_Color white = {215, 215, 215, 0}; // dim white
SDL_Color yellow = {160, 160, 0, 0};
ttf = TTF_OpenFont(full_font_path.c_str(), 12);
if (ttf == NULL) THROW("Cannot load font file:" << full_font_path << ", error:" << TTF_GetError());
Font font0(*ttf, black);
FontCollection::instance().setFont(FONT_0, font0);
ttf = TTF_OpenFont(full_font_path.c_str(), 18);
if (ttf == NULL) THROW("Cannot load font file:" << full_font_path << ", error:" << TTF_GetError());
Font font2(*ttf, black);
FontCollection::instance().setFont(FONT_2, font2);
ttf = TTF_OpenFont(full_font_path.c_str(), 18);
if (ttf == NULL) THROW("Cannot load font file:" << full_font_path << ", error:" << TTF_GetError());
Font font2red(*ttf, red);
FontCollection::instance().setFont(FONT_2_RED, font2red);
ttf = TTF_OpenFont(full_font_path.c_str(), 18);
if (ttf == NULL) THROW("Cannot load font file:" << full_font_path << ", error:" << TTF_GetError());
Font font2white(*ttf, white);
FontCollection::instance().setFont(FONT_2_WHITE, font2white);
ttf = TTF_OpenFont(full_font_path.c_str(), 18);
if (ttf == NULL) THROW("Cannot load font file:" << full_font_path << ", error:" << TTF_GetError());
Font font2yellow(*ttf, yellow);
FontCollection::instance().setFont(FONT_2_YELLOW, font2yellow);
ttf = TTF_OpenFont(full_font_path.c_str(), 28);
if (ttf == NULL) THROW("Cannot load font file:" << full_font_path << ", error:" << TTF_GetError());
Font font3(*ttf, black);
FontCollection::instance().setFont(FONT_3, font3);
}