-
Notifications
You must be signed in to change notification settings - Fork 6
/
graphics.c
1578 lines (1311 loc) · 48.2 KB
/
graphics.c
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 1994, 2002, 2003 Johannes Prix
* Copyright (c) 1994, 2002, 2003 Reinhard Prix
*
*
* This file is part of Freedroid
*
* Freedroid 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 2 of the License, or
* (at your option) any later version.
*
* Freedroid 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 Freedroid; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
/*----------------------------------------------------------------------
*
* Desc: Graphics primitived, such as functions to load LBM or PCX images,
* to change the vga color table, to activate or deachtivate monitor
* signal, to set video modes etc.
*
*----------------------------------------------------------------------*/
#define _graphics_c
#include "system.h"
#include "defs.h"
#include "struct.h"
#include "global.h"
#include "proto.h"
#include "map.h"
#include "text.h"
#include "takeover.h"
const SDL_VideoInfo *vid_info; /* info about current video mode */
int vid_bpp;
int fonts_loaded = FALSE;
char *portrait_raw_mem[NUM_DROIDS];
// local prototypes
void PutPixel (SDL_Surface * surface, int x, int y, Uint32 pixel);
int Load_Fonts (void);
SDL_Surface *Load_Block (char *fpath, int line, int col, SDL_Rect * block, int flags);
SDL_RWops *load_raw_pic (const char *fpath, char **raw_mem );
BFont_Info *Duplicate_Font ( const BFont_Info * in_font );
void LoadThemeConfigurationFile(void);
/* XPM */
static const char *crosshair_xpm[] = {
/* width height num_colors chars_per_pixel */
" 32 32 3 1",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
" ",
" ",
" XXXX ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" XXXX ",
" ",
" XXXXXXXXXXX XXXXXXXXXX ",
" X.........X X........X ",
" X.........X X........X ",
" XXXXXXXXXXX XXXXXXXXXX ",
" ",
" XXXX ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" X..X ",
" XXXX ",
" ",
" ",
"0,0"
};
/* XPM */
static const char *arrow_xpm[] = {
/* width height num_colors chars_per_pixel */
" 32 32 3 1",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
"X ",
"XX ",
"X.X ",
"X..X ",
"X...X ",
"X....X ",
"X.....X ",
"X......X ",
"X.......X ",
"X........X ",
"X.....XXXXX ",
"X..X..X ",
"X.X X..X ",
"XX X..X ",
"X X..X ",
" X..X ",
" X..X ",
" X..X ",
" XX ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"0,0"
};
/* ----------------------------------------------------------------------
* This function applies a color filter to a given surface
* ---------------------------------------------------------------------- */
int
ApplyFilter (SDL_Surface *surf, float fred, float fgreen, float fblue)
{
int x , y ; // for processing through the surface...
Uint8 red, green, blue, alpha;
//--------------------
// First we check for null surfaces given...
//
if ( surf == NULL )
{
DebugPrintf (0 , "\nERROR: ApplyFilter called with NULL pointer\n" );
return (ERR);
}
//--------------------
// Now we start to process through the whole surface and examine each
// pixel.
//
for ( y = 0 ; y < surf -> h ; y ++ )
{
for ( x = 0 ; x < surf -> w ; x ++ )
{
GetRGBA (surf, x, y, &red, &green, &blue, &alpha);
if (alpha == SDL_ALPHA_TRANSPARENT)
continue;
red *= fred;
green *= fgreen;
blue *= fblue;
putpixel (surf, x, y, SDL_MapRGBA (surf->format, red, green, blue, alpha) ) ;
}
}
return (OK);
} // Apply_Filter
/* ----------------------------------------------------------------------
* This function gives the green component of a pixel, using a value of
* 255 for the most green pixel and 0 for the least green pixel.
* ---------------------------------------------------------------------- */
void
GetRGBA ( SDL_Surface* surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
{
SDL_PixelFormat *fmt;
Uint32 pixel;
//--------------------
// First we extract the pixel itself and the
// format information we need.
//
fmt = surface -> format ;
pixel = * ( ( ( Uint32* ) surface -> pixels ) + x + y * surface->w ) ;
SDL_GetRGBA (pixel, fmt, r, g, b, a);
}; // int GetRGBA
/*----------------------------------------------------------------------
This function was taken directly from the example in the SDL docu.
Even there they say they have stolen if from the mailing list.
Anyway it should create a new mouse cursor from an XPM.
The XPM is defined above and not read in from disk or something.
----------------------------------------------------------------------*/
static SDL_Cursor *init_system_cursor(const char *image[])
{
int i, row, col;
Uint8 data[4*32];
Uint8 mask[4*32];
int hot_x, hot_y;
i = -1;
for ( row=0; row<32; ++row ) {
for ( col=0; col<32; ++col ) {
if ( col % 8 ) {
data[i] <<= 1;
mask[i] <<= 1;
} else {
++i;
data[i] = mask[i] = 0;
}
switch (image[4+row][col]) {
case 'X':
data[i] |= 0x01;
mask[i] |= 0x01;
break;
case '.':
mask[i] |= 0x01;
break;
case ' ':
break;
}
}
}
sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
};
/*
----------------------------------------------------------------------
----------------------------------------------------------------------
*/
void
DrawLineBetweenTiles( float x1 , float y1 , float x2 , float y2 , int linecolor )
{
int i;
int pixx;
int pixy;
float tmp;
float slope;
if ( (x1 == x2) && (y1 == y2) ) return; // nothing is to be done here
if (x1 == x2) // infinite slope!! special case, that must be caught!
{
if (y1 > y2) // in this case, just interchange 1 and 2
{
tmp = y1;
y1=y2;
y2=tmp;
}
for ( i=0 ; i < (y2 - y1) * Block_Rect.w ; i++ )
{
pixx = User_Rect.x + User_Rect.w/2 - Block_Rect.w * (Me.pos.x - x1 );
pixy = UserCenter_y - Block_Rect.h * (Me.pos.y - y1 ) + i ;
if ( (pixx <= User_Rect.x) ||
(pixx >= User_Rect.x + User_Rect.w -1) ||
(pixy <= User_Rect.y ) ||
(pixy >= User_Rect.y + User_Rect.h -1) ) continue;
putpixel( ne_screen , pixx , pixy , linecolor );
putpixel( ne_screen , pixx-1 , pixy , linecolor );
}
return;
}
if (x1 > x2) // in this case, just interchange 1 and 2
{
tmp = x1;
x1=x2;
x2=tmp;
tmp = y1;
y1=y2;
y2=tmp;
}
//--------------------
// Now we start the drawing process
//
// SDL_LockSurface( ne_screen );
slope = ( y2 - y1 ) / (x2 - x1) ;
for ( i=0 ; i<(x2-x1)*Block_Rect.w ; i++ )
{
pixx=User_Rect.x + User_Rect.w/2 - Block_Rect.w * (Me.pos.x - x1 ) + i;
pixy= UserCenter_y - Block_Rect.h * (Me.pos.y - y1 ) + i * slope ;
if ( (pixx <= User_Rect.x) ||
(pixx >= User_Rect.x + User_Rect.w -1) ||
(pixy <= User_Rect.y ) ||
(pixy >= User_Rect.y + User_Rect.h -1) ) continue;
putpixel( ne_screen , pixx , pixy , linecolor );
putpixel( ne_screen , pixx , pixy -1 , linecolor );
}
// SDL_UnlockSurface( ne_screen );
} // void DrawLineBetweenTiles
/*-----------------------------------------------------------------
* This function saves a screenshot to disk.
* The screenshots are names "Screenshot_XX.bmp" where XX is a
* running number.
*
* NOTE: This function does NOT check for existing screenshots,
* but will silently overwrite them. No problem in most
* cases I think.
*
*-----------------------------------------------------------------*/
void
TakeScreenshot(void)
{
static int Number_Of_Screenshot=0;
char Screenshoot_Filename[100];
Activate_Conservative_Frame_Computation();
sprintf( Screenshoot_Filename , "Screenshot_%d.bmp", Number_Of_Screenshot );
SDL_SaveBMP( ne_screen , Screenshoot_Filename );
Number_Of_Screenshot++;
DisplayBanner ("Screenshot", NULL, BANNER_NO_SDL_UPDATE | BANNER_FORCE_UPDATE );
MakeGridOnScreen(NULL);
SDL_Flip (ne_screen);
Play_Sound (SCREENSHOT_SOUND);
while (cmd_is_active(CMD_SCREENSHOT)) SDL_Delay(1);
DisplayBanner (NULL, NULL, BANNER_FORCE_UPDATE );
return;
} // void TakeScreenshot(void)
/*
----------------------------------------------------------------------
@Desc: This function draws a "grid" on the screen, that means every
"second" pixel is blacked out, thereby generation a fading
effect. This function was created to fade the background of the
Escape menu and its submenus.
@Ret: none
----------------------------------------------------------------------
*/
void
MakeGridOnScreen( SDL_Rect* Grid_Rectangle )
{
int x,y;
if ( Grid_Rectangle == NULL ) Grid_Rectangle = & User_Rect ;
DebugPrintf (2, "\nvoid MakeGridOnScreen(...): real function call confirmed.");
SDL_LockSurface( ne_screen );
for ( y = Grid_Rectangle->y ; y < (Grid_Rectangle->h + Grid_Rectangle->y) ; y++)
{
for ( x = Grid_Rectangle->x ; x < (Grid_Rectangle->x + Grid_Rectangle->w) ; x++ )
{
if ((x+y)%2 == 0)
{
putpixel( ne_screen, x, y, 0 );
}
}
}
SDL_UnlockSurface( ne_screen );
DebugPrintf (2, "\nvoid MakeGridOnScreen(...): end of function reached.");
} // void MakeGridOnSchreen(void)
/*----------------------------------------------------------------------
* This function load an image and displays it directly to the ne_screen
* but without updating it.
* This might be very handy, especially in the Title() function to
* display the title image and perhaps also for displaying the ship
* and that.
*
----------------------------------------------------------------------*/
void
DisplayImage(char *datafile)
{
SDL_Surface *image;
image = IMG_Load(datafile);
if ( image == NULL ) {
DebugPrintf(0, "ERROR: Couldn't load image %s: %s\n", datafile, IMG_GetError());
Terminate(ERR);
}
if (GameConfig.scale != 1.0)
ScalePic (&image, GameConfig.scale);
SDL_BlitSurface(image, NULL, ne_screen, NULL);
SDL_FreeSurface(image);
return;
} // DisplayImage()
/*----------------------------------------------------------------------
* This function resizes all blocks and structures involved in assembling
* the combat picture to a new scale. The new scale is relative to the
* standard scale with means scale=1 is 64x64 tile size.
*
* in the first call we assume the Block_Rect to be the original game-size
* and store this value for future rescalings
----------------------------------------------------------------------*/
void
SetCombatScaleTo(float scale)
{
int i, j;
static SDL_Rect origBlock;
static bool firstcall = TRUE;
SDL_Surface *tmp;
if (firstcall) Copy_Rect (Block_Rect, origBlock); // keep that as a backup
firstcall = FALSE;
for ( j=0 ; j < NUM_COLORS ; j++ )
for ( i = 0 ; i < NUM_MAP_BLOCKS ; i++ )
{
// if there's already a rescaled version, free it
if (MapBlockSurfacePointer[j][i] != OrigMapBlockSurfacePointer[j][i])
SDL_FreeSurface (MapBlockSurfacePointer[j][i]);
// then zoom..
tmp = zoomSurface(OrigMapBlockSurfacePointer[j][i], scale, scale, 0);
if ( tmp == NULL ) {
DebugPrintf (0, "ERROR: zoomSurface() failed for scale = %g.\n", scale);
Terminate (ERR);
}
// and optimize
MapBlockSurfacePointer[j][i]=SDL_DisplayFormat (tmp);
SDL_FreeSurface(tmp); // free the old surface
}
Copy_Rect (origBlock, Block_Rect); // always scale with respect to original size!
ScaleRect(Block_Rect, scale);
return;
} // void SetCombatScaleTo(float new_scale);
/*
----------------------------------------------------------------------
----------------------------------------------------------------------
*/
void
LoadThemeConfigurationFile(void)
{
char *Data;
char *ReadPointer;
char *fpath;
int BulletIndex;
#define END_OF_THEME_DATA_STRING "**** End of theme data section ****"
#define END_OF_THEME_BLAST_DATA_STRING "*** End of themes blast data section ***"
#define END_OF_THEME_BULLET_DATA_STRING "*** End of themes bullet data section ***"
#define END_OF_THEME_DIGIT_DATA_STRING "*** End of themes digit data section ***"
fpath = find_file ("config.theme", GRAPHICS_DIR, USE_THEME, CRITICAL);
Data = ReadAndMallocAndTerminateFile( fpath , END_OF_THEME_DATA_STRING ) ;
//--------------------
// Now the file is read in entirely and
// we can start to analyze its content,
//
#define BLAST_ONE_NUMBER_OF_PHASES_STRING "How many phases in Blast one :"
#define BLAST_TWO_NUMBER_OF_PHASES_STRING "How many phases in Blast two :"
ReadValueFromString (Data, BLAST_ONE_NUMBER_OF_PHASES_STRING, "%d", &Blastmap[0].phases);
ReadValueFromString (Data, BLAST_TWO_NUMBER_OF_PHASES_STRING, "%d", &Blastmap[1].phases);
//--------------------
// Next we read in the number of phases that are to be used for each bullet type
ReadPointer = Data ;
while ( ( ReadPointer = strstr ( ReadPointer , "For Bullettype Nr.=" ) ) != NULL )
{
ReadValueFromString (ReadPointer, "For Bullettype Nr.=", "%d", &BulletIndex);
if ( BulletIndex >= Number_Of_Bullet_Types )
{
DebugPrintf (0, "\n\n\
----------------------------------------------------------------------\n\
Freedroid has encountered a problem:\n\
In function 'char* LoadThemeConfigurationFile ( ... ):\n\
\n\
There was a specification for the number of phases in a bullet type\n\
that does not at all exist in the ruleset.\n\
\n\
This might indicate that either the ruleset file is corrupt or the \n\
theme.config configuration file is corrupt or (less likely) that there\n\
is a severe bug in the reading function.\n\
\n\
Please check that your theme and ruleset files are properly set up.\n\
\n\
Please also don't forget, that you might have to run 'make install'\n\
again after you've made modifications to the data files in the source tree.\n\
\n\
Freedroid will terminate now to draw attention to the data problem it could\n\
not resolve.... Sorry, if that interrupts a major game of yours.....\n\
----------------------------------------------------------------------\n\
\n" );
Terminate(ERR);
}
ReadValueFromString (ReadPointer, "we will use number of phases=", "%d", &Bulletmap[BulletIndex].phases);
ReadValueFromString (ReadPointer, "and number of phase changes per second=", "%f",
&Bulletmap[BulletIndex].phase_changes_per_second);
ReadPointer++;
}
// --------------------
// Also decidable from the theme is where in the robot to
// display the digits. This must also be read from the configuration
// file of the theme
//
#define DIGIT_ONE_POSITION_X_STRING "First digit x :"
#define DIGIT_ONE_POSITION_Y_STRING "First digit y :"
#define DIGIT_TWO_POSITION_X_STRING "Second digit x :"
#define DIGIT_TWO_POSITION_Y_STRING "Second digit y :"
#define DIGIT_THREE_POSITION_X_STRING "Third digit x :"
#define DIGIT_THREE_POSITION_Y_STRING "Third digit y :"
ReadValueFromString (Data, DIGIT_ONE_POSITION_X_STRING, "%hd", &FirstDigit_Rect.x);
ReadValueFromString (Data, DIGIT_ONE_POSITION_Y_STRING, "%hd", &FirstDigit_Rect.y);
ReadValueFromString (Data, DIGIT_TWO_POSITION_X_STRING, "%hd", &SecondDigit_Rect.x);
ReadValueFromString (Data, DIGIT_TWO_POSITION_Y_STRING, "%hd", &SecondDigit_Rect.y);
ReadValueFromString (Data, DIGIT_THREE_POSITION_X_STRING, "%hd", &ThirdDigit_Rect.x);
ReadValueFromString (Data, DIGIT_THREE_POSITION_Y_STRING, "%hd", &ThirdDigit_Rect.y);
free (Data);
return;
}; // void LoadThemeConfigurationFile ( void )
/*-----------------------------------------------------------------
* @Desc: get the pics for: druids, bullets, blasts
*
* reads all blocks and puts the right pointers into
* the various structs
*
* @Ret: TRUE/FALSE
*
*-----------------------------------------------------------------*/
int
InitPictures (void)
{
static bool first_call= TRUE;
char *fpath;
int line, col, i;
BFont_Info *oldfont;
SDL_Surface *tmp;
char fname[500];
// Loading all these pictures might take a while...
// and we do not want do deal with huge frametimes, which
// could box the influencer out of the ship....
Activate_Conservative_Frame_Computation();
oldfont = GetCurrentFont ();
if (!fonts_loaded)
Load_Fonts ();
SetCurrentFont (Font0_BFont);
init_progress ("Loading pictures");
LoadThemeConfigurationFile();
update_progress (15);
//---------- get Map blocks
fpath = find_file (MAP_BLOCK_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL);
Load_Block (fpath, 0, 0, NULL, INIT_ONLY); /* init function */
for (line = 0; line < NUM_COLORS; line ++)
for (col = 0; col < NUM_MAP_BLOCKS; col ++)
{
FreeIfUsed (OrigMapBlockSurfacePointer[line][col]);
OrigMapBlockSurfacePointer[line][col] = Load_Block (NULL, line, col, &OrigBlock_Rect,0);
MapBlockSurfacePointer[line][col] = OrigMapBlockSurfacePointer[line][col];
}
update_progress (20);
//---------- get Droid-model blocks
fpath = find_file (DROID_BLOCK_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL);
Load_Block (fpath, 0, 0, NULL, INIT_ONLY);
for (col = 0; col < DROID_PHASES; col ++)
{
FreeIfUsed (InfluencerSurfacePointer[col]);
FreeIfUsed (EnemySurfacePointer[col]);
InfluencerSurfacePointer[col] = Load_Block (NULL, 0, col, &OrigBlock_Rect, 0);
EnemySurfacePointer[col] = Load_Block (NULL, 1, col, &OrigBlock_Rect, 0);
/* Droid pics are only used in _internal_ blits ==> clear per-surf alpha */
SDL_SetAlpha (InfluencerSurfacePointer[col], 0, 0);
SDL_SetAlpha (EnemySurfacePointer[col], 0, 0);
}
// SDL_SetAlpha( Me.pic, SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
update_progress (30);
//---------- get Bullet blocks
fpath = find_file (BULLET_BLOCK_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL);
Load_Block (fpath, 0, 0, NULL, INIT_ONLY);
for (line = 0; line < Number_Of_Bullet_Types; line ++)
for (col = 0; col < Bulletmap[line].phases; col ++)
{
FreeIfUsed (Bulletmap[line].SurfacePointer[col]);
Bulletmap[line].SurfacePointer[col] = Load_Block (NULL, line, col, &OrigBlock_Rect, 0);
}
update_progress (35);
//---------- get Blast blocks
fpath = find_file (BLAST_BLOCK_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL);
Load_Block (fpath, 0, 0, NULL, INIT_ONLY);
for (line = 0; line < ALLBLASTTYPES; line ++)
for (col = 0; col < Blastmap[line].phases; col ++)
{
FreeIfUsed (Blastmap[line].SurfacePointer[col]);
Blastmap[line].SurfacePointer[col] = Load_Block (NULL, line, col, &OrigBlock_Rect, 0);
}
update_progress (45);
//---------- get Digit blocks
fpath = find_file (DIGIT_BLOCK_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL);
Load_Block (fpath, 0, 0, NULL, INIT_ONLY);
for (col = 0; col < 10; col++)
{
FreeIfUsed (InfluDigitSurfacePointer[col]);
InfluDigitSurfacePointer[col] = Load_Block (NULL, 0, col, &OrigDigit_Rect, 0);
FreeIfUsed (EnemyDigitSurfacePointer[col]);
EnemyDigitSurfacePointer[col] = Load_Block (NULL, 0, col + 10, &OrigDigit_Rect, 0);
}
update_progress(50);
//---------- get Takeover pics
FreeIfUsed(to_blocks); /* this happens when we do theme-switching */
fpath = find_file (TO_BLOCK_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL);
to_blocks = Load_Block (fpath, 0, 0, NULL, 0);
update_progress (60);
FreeIfUsed(ship_on_pic);
ship_on_pic = IMG_Load (find_file (SHIP_ON_PIC_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL));
FreeIfUsed(ship_off_pic);
ship_off_pic= IMG_Load (find_file (SHIP_OFF_PIC_FILE, GRAPHICS_DIR, USE_THEME, CRITICAL));
// the following are not theme-specific and are therefore only loaded once!
if (first_call)
{
// create the tmp block-build storage
tmp = SDL_CreateRGBSurface( 0 , Block_Rect.w, Block_Rect.h, vid_bpp, 0, 0, 0, 0);
BuildBlock = SDL_DisplayFormatAlpha (tmp);
SDL_FreeSurface (tmp);
// takeover background pics
fpath = find_file (TAKEOVER_BG_PIC_FILE, GRAPHICS_DIR, NO_THEME, CRITICAL);
takeover_bg_pic = Load_Block (fpath, 0, 0, NULL, 0);
set_takeover_rects (); // setup takeover rectangles
// cursor shapes
arrow_cursor = init_system_cursor (arrow_xpm);
crosshair_cursor = init_system_cursor (crosshair_xpm);
//---------- get Console pictures
fpath = find_file (CONSOLE_PIC_FILE, GRAPHICS_DIR, NO_THEME, CRITICAL);
console_pic = Load_Block (fpath, 0, 0, NULL, 0);
fpath = find_file (CONSOLE_BG_PIC1_FILE, GRAPHICS_DIR, NO_THEME, CRITICAL);
console_bg_pic1 = Load_Block (fpath, 0, 0, NULL, 0);
fpath = find_file (CONSOLE_BG_PIC2_FILE, GRAPHICS_DIR, NO_THEME, CRITICAL);
console_bg_pic2 = Load_Block (fpath, 0, 0, NULL, 0);
update_progress (80);
arrow_up = IMG_Load (find_file ("arrow_up.png", GRAPHICS_DIR, NO_THEME, CRITICAL) );
arrow_down = IMG_Load (find_file ("arrow_down.png", GRAPHICS_DIR, NO_THEME, CRITICAL) );
arrow_right = IMG_Load (find_file ("arrow_right.png", GRAPHICS_DIR, NO_THEME, CRITICAL) );
arrow_left = IMG_Load (find_file ("arrow_left.png", GRAPHICS_DIR, NO_THEME, CRITICAL) );
//---------- get Banner
fpath = find_file (BANNER_BLOCK_FILE, GRAPHICS_DIR, NO_THEME, CRITICAL);
banner_pic = Load_Block (fpath, 0, 0, NULL, 0);
update_progress (90);
//---------- get Droid images ----------
for (i=0; i<NUM_DROIDS; i++)
{
// first check if we find a file with rotation-frames: first try .jpg
strcpy( fname, Druidmap[i].druidname );
strcat( fname , ".jpg" );
fpath = find_file (fname, GRAPHICS_DIR, NO_THEME, IGNORE);
// then try with .png
if (!fpath)
{
strcpy( fname, Druidmap[i].druidname );
strcat( fname , ".png" );
fpath = find_file (fname, GRAPHICS_DIR, NO_THEME, CRITICAL);
}
packed_portraits[i] = load_raw_pic (fpath, &portrait_raw_mem[i] );
}
update_progress (95);
// we need the 999.png in any case for transparency!
strcpy( fname, Druidmap[DRUID999].druidname );
strcat( fname , ".png" );
fpath = find_file (fname, GRAPHICS_DIR, NO_THEME, CRITICAL);
pic999 = Load_Block (fpath, 0, 0, NULL, 0);
// get the Ashes pics
strcpy (fname, "Ashes.png");
fpath = find_file (fname, GRAPHICS_DIR, NO_THEME, WARNONLY);
if (!fpath)
{
DebugPrintf (0, "WARNING: deactivated display of droid-decals\n");
GameConfig.ShowDecals = FALSE;
}
else
{
Load_Block (fpath, 0, 0, NULL, INIT_ONLY);
Decal_pics[0] = Load_Block (NULL, 0, 0, &OrigBlock_Rect, 0);
Decal_pics[1] = Load_Block (NULL, 0, 1, &OrigBlock_Rect, 0);
}
} // if first_call
update_progress (96);
// if scale != 1 then we need to rescale everything now
ScaleGraphics (GameConfig.scale);
update_progress (98);
// make sure bullet-surfaces get re-generated!
for ( i = 0 ; i < MAXBULLETS ; i++ )
AllBullets[i].Surfaces_were_generated = FALSE ;
SetCurrentFont (oldfont);
first_call = FALSE;
return (TRUE);
} // InitPictures
/*----------------------------------------------------------------------
* load a pic into memory and return the SDL_RWops pointer to it
*----------------------------------------------------------------------*/
SDL_RWops *
load_raw_pic (const char *fpath, char **raw_mem )
{
FILE *fp;
size_t size;
if ( raw_mem == NULL || (*raw_mem) != NULL ) {
DebugPrintf ( 0, "Invalid input 'raw_mem': must be pointing to NULL pointer\n");
Terminate ( ERR );
}
// sanity check
if (!fpath)
{
DebugPrintf (0, "ERROR: load_raw_pic() called with NULL argument!\n");
Terminate (ERR);
}
fp = fopen (fpath, "rb");
if (!fp)
{
DebugPrintf (0, "ERROR: could not open file %s. Giving up\n", fpath);
Terminate (ERR);
}
size = FS_filelength (fp);
(*raw_mem) = MyMalloc (size);
if (fread ( (*raw_mem), 1, size, fp) != size)
{
DebugPrintf (0, "ERROR reading file %s. Giving up...\n", fpath);
Terminate (ERR);
}
fclose (fp);
return (SDL_RWFromMem( (*raw_mem), size) );
}
/*------------------------------------------------------------
* General block-reading routine: get block from pic-file
*
* fpath: full pathname of picture-file; if NULL: use previous SDL-surf
* line, col: block-position in pic-file to read block from
* block: dimension of blocks to consider: if NULL: copy whole pic
* NOTE: only w and h of block are used!!
*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* NOTE: to avoid memory-leaks, use (flags | INIT_ONLY) if you only
* call this function to set up a new pic-file to be read.
* This will avoid copying & mallocing a new pic, NULL will be returned
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*------------------------------------------------------------*/
SDL_Surface *
Load_Block (char *fpath, int line, int col, SDL_Rect * block, int flags)
{
static SDL_Surface *pic = NULL;
SDL_Surface *tmp;
SDL_Rect src, dim;
SDL_Surface *ret;
int usealpha;
if (!fpath && !pic) /* we need some info.. */
return (NULL);
if ( (pic != NULL) && (flags == FREE_ONLY) ) {
SDL_FreeSurface (pic);
return NULL;
}
if (fpath) // initialize: read & malloc new pic, dont' return a copy!!
{
if (pic) // previous pic?
SDL_FreeSurface (pic);
pic = IMG_Load (fpath);
}
if ( (flags & INIT_ONLY) != FALSE )
return (NULL); // that's it guys, only initialzing...
if (!block)
{
Set_Rect (dim, 0, 0, pic->w, pic->h);
}
else
{
Set_Rect (dim, 0, 0, block->w, block->h);
}
if (pic->format->Amask != 0)
usealpha = TRUE;
else
usealpha = FALSE;
if (usealpha)
SDL_SetAlpha (pic, 0, 0); /* clear per-surf alpha for internal blit */
tmp = SDL_CreateRGBSurface (0, dim.w, dim.h, vid_bpp, 0, 0, 0, 0);
if (usealpha)
ret = SDL_DisplayFormatAlpha (tmp);
else
ret = SDL_DisplayFormat (tmp);
SDL_FreeSurface (tmp);
Set_Rect (src, col * (dim.w + 2), line * (dim.h + 2), dim.w, dim.h);
SDL_BlitSurface (pic, &src, ret, NULL);
if (usealpha)
SDL_SetAlpha (ret, SDL_SRCALPHA | SDL_RLEACCEL, SDL_ALPHA_OPAQUE);
return (ret);
} // Load_Block()
/*-----------------------------------------------------------------
* Initialise the Video display and graphics engine
*
*
*-----------------------------------------------------------------*/
void
Init_Video (void)
{
char vid_driver[81];
Uint32 vid_flags; /* flags for SDL video mode */
char *fpath;
const char *YN[2] = {"no", "yes"};
/* Initialize the SDL library */
// if ( SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1 )
if ( SDL_Init (SDL_INIT_VIDEO) == -1 )
{
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
Terminate(ERR);
} else
DebugPrintf(1, "\nSDL Video initialisation successful.\n");
// Now SDL_TIMER is initialized here:
if ( SDL_InitSubSystem ( SDL_INIT_TIMER ) == -1 )
{
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
Terminate(ERR);
} else
DebugPrintf(1, "\nSDL Timer initialisation successful.\n");
/* clean up on exit */
atexit (SDL_Quit);
vid_info = SDL_GetVideoInfo (); /* just curious */
SDL_VideoDriverName (vid_driver, 80);
#ifdef ANDROID
vid_bpp = 16; // Hardcoded Android default
#else
vid_bpp = vid_info->vfmt->BitsPerPixel;
#endif
DebugPrintf (0, "Video info summary from SDL:\n");
DebugPrintf (0, "----------------------------------------------------------------------\n");
DebugPrintf (0, "Is it possible to create hardware surfaces: %s\n" , YN[vid_info->hw_available]);
DebugPrintf (0, "Is there a window manager available: %s\n", YN[vid_info->wm_available]);
DebugPrintf (0, "Are hardware to hardware blits accelerated: %s\n", YN[vid_info->blit_hw]);
DebugPrintf (0, "Are hardware to hardware colorkey blits accelerated: %s\n", YN[vid_info->blit_hw_CC]);
DebugPrintf (0, "Are hardware to hardware alpha blits accelerated: %s\n", YN[vid_info->blit_hw_A]);
DebugPrintf (0, "Are software to hardware blits accelerated: %s\n", YN[vid_info->blit_sw]);
DebugPrintf (0, "Are software to hardware colorkey blits accelerated: %s\n", YN[vid_info->blit_sw_CC]);
DebugPrintf (0, "Are software to hardware alpha blits accelerated: %s\n", YN[vid_info->blit_sw_A]);
DebugPrintf (0, "Are color fills accelerated: %s\n", YN[vid_info->blit_fill]);
DebugPrintf (0, "Total amount of video memory in Kilobytes: %d\n", vid_info->video_mem);
DebugPrintf (0, "Pixel format of the video device: bpp = %d, bytes/pixel = %d\n",
vid_bpp, vid_info->vfmt->BytesPerPixel);
DebugPrintf (0, "Video Driver Name: %s\n", vid_driver);
DebugPrintf (0, "----------------------------------------------------------------------\n");
// flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
vid_flags = 0;
if (GameConfig.UseFullscreen) vid_flags |= SDL_FULLSCREEN;
if (vid_info->wm_available) /* if there's a window-manager */
{
SDL_WM_SetCaption ("Freedroid", "");
fpath = find_file (ICON_FILE, GRAPHICS_DIR, NO_THEME, WARNONLY);
if ( fpath == NULL ) {
DebugPrintf ( 0, "Could not find icon file '%s'\n", ICON_FILE );
} else {
SDL_Surface *img = IMG_Load (fpath);
if ( img == NULL ) {
DebugPrintf ( 0, "IMG_Load failed for icon file '%s'\n", fpath );
} else {
SDL_WM_SetIcon( img, NULL);
SDL_FreeSurface ( img );
}
}
}
if( !(ne_screen = SDL_SetVideoMode ( Screen_Rect.w, Screen_Rect.h , 0 , vid_flags)) )
{
DebugPrintf (0, "ERORR: Couldn't set %d x %d video mode. SDL: %s\n",
Screen_Rect.w, Screen_Rect.h, SDL_GetError());
exit(-1);
}
vid_info = SDL_GetVideoInfo (); /* info about current video mode */
DebugPrintf(1, "Got video mode: ");
SDL_SetGamma( 1 , 1 , 1 );
GameConfig.Current_Gamma_Correction=1;
return;
} /* InitVideo () */
/*@Function============================================================
@Desc:
@Ret:
@Int: