]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CGraphics.cpp
1.17 Medal Support
[quix0rs-blobwars.git] / src / CGraphics.cpp
1 /*
2 Copyright (C) 2004 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "headers.h"
22
23 Graphics::Graphics()
24 {
25         for (int i = 0 ; i < MAX_TILES ; i++)
26         {
27                 tile[i] = NULL;
28         }
29
30         background = NULL;
31         infoMessage = NULL;
32
33         fontSize = 0;
34         
35         medalMessageTimer = 0;
36         medalType = 0;
37
38         currentLoading = 0;
39
40         screenShotNumber = 0;
41         takeRandomScreenShots = false;
42
43         waterAnim = 201;
44         slimeAnim = 208;
45         lavaAnim = 215;
46 }
47
48 void Graphics::free()
49 {
50         debug(("graphics.free: Background\n"));
51         if (background != NULL)
52         {
53                 SDL_FreeSurface(background);
54         }
55         debug(("graphics.free: Background - Done\n"));
56
57         background = NULL;
58
59         debug(("graphics.free: Tiles\n"));
60         for (int i = 0 ; i < MAX_TILES ; i++)
61         {
62                 if (tile[i] != NULL)
63                 {
64                         SDL_FreeSurface(tile[i]);
65                         tile[i] = NULL;
66                 }
67         }
68         debug(("graphics.free: Tiles - Done\n"));
69
70         debug(("graphics.free: Sprites\n"));
71         Sprite *sprite = (Sprite*)spriteList.getHead();
72         while (sprite->next != NULL)
73         {
74                 sprite = (Sprite*)sprite->next;
75                 //debug(("graphics.free: Sprites Sprite::Free - %s\n", sprite->name));
76                 sprite->free();
77         }
78         debug(("graphics.free: Sprites Clear()\n"));
79         spriteList.clear();
80         debug(("graphics.free: Sprites - Done\n"));
81 }
82
83 void Graphics::destroy()
84 {
85         free();
86
87         for (int i = 0 ; i < 5 ; i++)
88         {
89                 if (font[i])
90                 {
91                         TTF_CloseFont(font[i]);
92                 }
93         }
94         
95         if (medalMessage != NULL)
96         {
97                 SDL_FreeSurface(medalMessage);
98         }
99
100         if (fadeBlack)
101         {
102                 SDL_FreeSurface(fadeBlack);
103         }
104
105         if (infoBar)
106         {
107                 SDL_FreeSurface(infoBar);
108         }
109         
110         for (int i = 0 ; i < 4 ; i++)
111         {
112                 if (medal[i] != NULL)
113                 {
114                         SDL_FreeSurface(medal[i]);
115                         medal[i] = NULL;
116                 }
117         }
118 }
119
120 void Graphics::registerEngine(Engine *engine)
121 {
122         this->engine = engine;
123 }
124
125 void Graphics::mapColors()
126 {
127         red = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
128         yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);
129         green = SDL_MapRGB(screen->format, 0x00, 0xff, 0x00);
130         darkGreen = SDL_MapRGB(screen->format, 0x00, 0x77, 0x00);
131         skyBlue = SDL_MapRGB(screen->format, 0x66, 0x66, 0xff);
132         blue = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
133         cyan = SDL_MapRGB(screen->format, 0x00, 0x99, 0xff);
134         white = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
135         lightGrey = SDL_MapRGB(screen->format, 0xcc, 0xcc, 0xcc);
136         grey = SDL_MapRGB(screen->format, 0x88, 0x88, 0x88);
137         darkGrey = SDL_MapRGB(screen->format, 0x33, 0x33, 0x33);
138         black = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
139
140         fontForeground.r = fontForeground.g = fontForeground.b = 0xff;
141         fontBackground.r = fontBackground.g = fontBackground.b = 0x00;
142
143         fontForeground.unused = fontBackground.unused = 0;
144
145         fadeBlack = alphaRect(640, 480, 0x00, 0x00, 0x00);
146
147         infoBar = alphaRect(640, 25, 0x00, 0x00, 0x00);
148         
149         medalMessage = NULL;
150 }
151
152 Sprite *Graphics::getSpriteHead()
153 {
154         return (Sprite*)spriteList.getHead();
155 }
156
157 void Graphics::setTransparent(SDL_Surface *sprite)
158 {
159         SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(sprite->format, 0, 0, 0));
160 }
161
162 bool Graphics::canShowMedalMessage()
163 {
164         return (medalMessageTimer <= 0);
165 }
166
167 void Graphics::updateScreen()
168 {
169         if (medalMessageTimer > 0)
170         {
171                 int padding = 0;
172                 
173                 medalMessageTimer--;
174                 
175                 if (medalType >= 0)
176                 {
177                         padding = 18;
178                 }
179                 
180                 drawRect(screen->w - (medalMessage->w + 5 + padding), 5, medalMessage->w + padding - 2, 20, grey, screen);
181                 drawRect(screen->w - (medalMessage->w + 5 + padding - 1), 6, medalMessage->w + padding - 4, 18, black, screen);
182                 blit(medalMessage, screen->w - (medalMessage->w + 5), 7, screen, false);
183                 
184                 if (medalType >= 0)
185                 {
186                         blit(medal[medalType], screen->w - (medalMessage->w + 5 + 16), 7, screen, false);
187                 }
188         }
189         
190         SDL_Flip(screen);
191         SDL_Delay(1);
192
193         if (takeRandomScreenShots)
194         {
195                 if ((Math::prand() % 500) == 0)
196                 {
197                         sprintf(screenshot, "screenshots/screenshot%.3d.bmp", screenShotNumber);
198                         SDL_SaveBMP(screen, screenshot);
199                         screenShotNumber++;
200                 }
201
202                 SDL_Delay(16);
203         }
204
205         if (engine->keyState[SDLK_F12])
206         {
207                 sprintf(screenshot, "screenshots/screenshot%.3d.bmp", screenShotNumber);
208                 SDL_SaveBMP(screen, screenshot);
209                 screenShotNumber++;
210
211                 engine->keyState[SDLK_F12] = 0;
212         }
213
214         if ((engine->keyState[SDLK_F10]) || ((engine->keyState[SDLK_RETURN]) && (engine->keyState[SDLK_LALT])))
215         {
216                 SDL_WM_ToggleFullScreen(screen);
217                 engine->fullScreen = !engine->fullScreen;
218
219                 engine->keyState[SDLK_F10] = engine->keyState[SDLK_LALT] = engine->keyState[SDLK_RETURN] = 0;
220         }
221 }
222
223 void Graphics::delay(int time)
224 {
225         unsigned long then = SDL_GetTicks();
226
227         engine->keyState[SDLK_ESCAPE] = 0;
228
229         while (true)
230         {
231                 updateScreen();
232                 
233                 if (SDL_GetTicks() >= then + time)
234                 {
235                         break;
236                 }
237
238                 engine->getInput();
239                 
240                 /*
241                 if (engine->keyState[SDLK_ESCAPE])
242                 {
243                         break;
244                 }
245                 */
246         }
247 }
248
249 void Graphics::RGBtoHSV(float r, float g, float b, float *h, float *s, float *v)
250 {
251         float mn, mx, delta;
252         mn = min(min(r, g), b);
253         mx = max(max(r, g), b);
254         *v = mx;
255         delta = mx - mn;
256
257         if (mx != 0)
258         {
259                 *s = delta / mx;
260         }
261         else
262         {
263                 *s = 0;
264                 *h = -1;
265                 return;
266         }
267
268         if (r == mx)
269         {
270                 *h = (g - b) / delta;
271         }
272         else if (g == mx)
273         {
274                 *h = 2 + (b - r) / delta;
275         }
276         else
277         {
278                 *h = 4 + (r - g) / delta;
279         }
280
281         *h *= 60;
282
283         if (*h < 0)
284         {
285                 *h += 360;
286         }
287 }
288
289 void Graphics::HSVtoRGB(float *r, float *g, float *b, float h, float s, float v)
290 {
291         int i;
292         float f, p, q, t;
293         if (s == 0)
294         {
295                 *r = *g = *b = v;
296                 return;
297         }
298
299         h /= 60;
300         i = (int)(h);
301         f = h - i;
302         p = v * (1 - s);
303         q = v * (1 - s * f);
304         t = v * (1 - s * (1 - f));
305
306         switch (i)
307         {
308                 case 0:
309                         *r = v;
310                         *g = t;
311                         *b = p;
312                         break;
313
314                 case 1:
315                         *r = q;
316                         *g = v;
317                         *b = p;
318                         break;
319
320                 case 2:
321                         *r = p;
322                         *g = v;
323                         *b = t;
324                         break;
325
326                 case 3:
327                         *r = p;
328                         *g = q;
329                         *b = v;
330                         break;
331
332                 case 4:
333                         *r = t;
334                         *g = p;
335                         *b = v;
336                         break;
337
338                 default:
339                         *r = v;
340                         *g = p;
341                         *b = q;
342                         break;
343         }
344 }
345
346 SDL_Surface *Graphics::loadImage(const char *filename)
347 {
348         SDL_Surface *image, *newImage;
349
350         #if USEPAK
351                 if (!engine->unpack(filename, PAK_IMG))
352                         showErrorAndExit(ERR_FILE, filename);
353                 image = IMG_Load_RW(engine->sdlrw, 1);
354         #else
355                 image = IMG_Load(filename);
356         #endif
357
358         if (!image)
359                 showErrorAndExit(ERR_FILE, filename);
360
361         newImage = SDL_DisplayFormat(image);
362
363         if (newImage)
364         {
365                 SDL_FreeSurface(image);
366         }
367         else
368         {
369                 // This happens when we are loading the window icon image
370                 newImage = image;
371         }
372
373         setTransparent(newImage);
374
375         return newImage;
376 }
377
378 SDL_Surface *Graphics::loadImage(const char *filename, int hue, int sat, int value)
379 {
380         SDL_Surface *image, *newImage;
381
382         #if USEPAK
383                 if (!engine->unpack(filename, PAK_IMG))
384                         showErrorAndExit(ERR_FILE, filename);
385                 image = IMG_Load_RW(engine->sdlrw, 1);
386         #else
387                 image = IMG_Load(filename);
388         #endif
389
390         if (!image)
391                 showErrorAndExit(ERR_FILE, filename);
392
393         if ((hue != 0) || (sat != 0) || (value != 0))
394         {
395                 if (image->format->BitsPerPixel != 8)
396                 {
397                         debug(("WARNING: Could not set Hue for '%s'! Not an 8 bit image!\n", filename));
398                 }
399                 else
400                 {
401                         SDL_Color *color;
402                         float r, g, b, h, s, v;
403
404                         if (image->format->palette->colors != NULL)
405                         {
406                                 for (int i = 1 ; i < image->format->palette->ncolors ; i++)
407                                 {
408                                         color = &image->format->palette->colors[i];
409
410                                         r = (int)color->r;
411                                         g = (int)color->g;
412                                         b = (int)color->b;
413
414                                         RGBtoHSV(r, g, b, &h, &s, &v);
415
416                                         h += hue;
417                                         s += sat;
418                                         v += value;
419
420                                         HSVtoRGB(&r, &g, &b, h, s, v);
421
422                                         color->r = (int)r;
423                                         color->g = (int)g;
424                                         color->b = (int)b;
425
426                                 }
427                         }
428                 }
429         }
430
431         newImage = SDL_DisplayFormat(image);
432
433         if (newImage)
434         {
435                 SDL_FreeSurface(image);
436         }
437         else
438         {
439                 // This happens when we are loading the window icon image
440                 newImage = image;
441         }
442
443         setTransparent(newImage);
444
445         return newImage;
446 }
447
448 SDL_Surface *Graphics::quickSprite(const char *name, SDL_Surface *image)
449 {
450         Sprite *sprite = addSprite(name);
451         sprite->setFrame(0, image, 60);
452
453         return sprite->getCurrentFrame();
454 }
455
456 void Graphics::fade(int amount)
457 {
458         SDL_SetAlpha(fadeBlack, SDL_SRCALPHA|SDL_RLEACCEL, amount);
459         blit(fadeBlack, 0, 0, screen, false);
460 }
461
462 void Graphics::fadeToBlack()
463 {
464         int start = 0;
465
466         while (start < 50)
467         {
468                 SDL_SetAlpha(fadeBlack, SDL_SRCALPHA|SDL_RLEACCEL, start);
469                 blit(fadeBlack, 0, 0, screen, false);
470                 delay(60);
471                 start++;
472         }
473 }
474
475 void Graphics::loadMapTiles(const char *baseDir)
476 {
477         bool found, autoAlpha;
478         char filename[255];
479         strcpy(filename, "");
480
481         autoAlpha = false;
482         
483         if (strcmp(baseDir, "gfx/common") == 0)
484         {
485                 autoAlpha = true;
486         }
487
488         #if !USEPAK
489         FILE *fp;
490         #endif
491
492         for (int i = 1 ; i < MAX_TILES ; i++)
493         {
494                 found = true;
495
496                 sprintf(filename, "%s/%d.png", baseDir, i);
497
498                 #if USEPAK
499                 
500                 if (!engine->getPak()->fileExists(filename))
501                         continue;
502
503                 #else
504
505                 fp = fopen(filename, "rb");
506                 if (!fp)
507                         continue;
508                 fclose(fp);
509
510                 #endif
511
512                 if (found)
513                 {
514                         tile[i] = loadImage(filename);
515
516                         if (autoAlpha)
517                         {
518                                 if ((i < MAP_EXITSIGN) || (i >= MAP_WATERANIM))
519                                 {
520                                         SDL_SetAlpha(tile[i], SDL_SRCALPHA|SDL_RLEACCEL, 130);
521                                 }
522                         }
523                         else
524                         {
525                                 if (i < MAP_DECORATION)
526                                 {
527                                         SDL_SetColorKey(tile[i], 0, SDL_MapRGB(tile[i]->format, 0, 0, 0));
528                                 }
529                         }
530                 }
531         }
532 }
533
534 /*
535 Note : We need to search for the right >>> PIXEL SIZE <<< and NOT point size!!
536 If a user has a resolution other than approximately 72dpi then
537 they will get a small or larger font and this won't work. This might look
538 weird since we'll load and delete multiple fonts, but it works...
539 */
540 void Graphics::loadFont(int i, const char *filename, int pixelSize)
541 {
542         int minx, maxx, miny, maxy, advance;
543         
544         debug(("Attempting to load a font with pixel size of %d...\n", pixelSize));
545         
546         if (font[i])
547         {
548                 debug(("Freeing Font %d first...\n", i));
549                 TTF_CloseFont(font[i]);
550         }
551         
552         char tempPath[PATH_MAX];
553         
554         sprintf(tempPath, "%sfont.ttf", engine->userHomeDirectory);
555
556         bool found = false;
557         int size = 0;
558         
559         while (!found)
560         {
561                 if (font[i])
562                 {
563                         TTF_CloseFont(font[i]);
564                 }
565                 
566                 #if USEPAK
567                         font[i] = TTF_OpenFont(tempPath, ++size);
568                 #else
569                         font[i] = TTF_OpenFont("data/vera.ttf", ++size);
570                 #endif
571         
572                 if (!font[i])
573                 {
574                         engine->reportFontFailure();
575                 }
576                 
577                 TTF_GlyphMetrics(font[i], '8', &minx, &maxx, &miny, &maxy, &advance);
578                 
579                 // great! we have an exact match
580                 if (maxx == pixelSize)
581                 {
582                         break;
583                 }
584                 
585                 // we've overshot, so we'll use the previous size!
586                 if (maxx > pixelSize)
587                 {
588                         TTF_CloseFont(font[i]);
589                         
590                         #if USEPAK
591                                 font[i] = TTF_OpenFont(tempPath, size - 1);
592                         #else
593                                 font[i] = TTF_OpenFont("data/vera.ttf", size - 1);
594                         #endif
595                                         
596                         TTF_GlyphMetrics(font[i], '8', &minx, &maxx, &miny, &maxy, &advance);
597                         
598                         break;
599                 }
600                 
601                 if (size >= 100)
602                 {
603                         debug(("Pixel size has exceeded 99 pixels! I'm giving up!\n"));
604                         engine->reportFontFailure();
605                 }
606         }
607         
608         TTF_SetFontStyle(font[i], TTF_STYLE_NORMAL);
609         
610         debug(("Got a match for font size %d - Nearest = %d\n", pixelSize, maxx));
611 }
612
613 Sprite *Graphics::addSprite(const char *name)
614 {
615         Sprite *sprite = new Sprite;
616         strcpy(sprite->name, name);
617
618         spriteList.add(sprite);
619
620         return sprite;
621 }
622
623 Sprite *Graphics::getSprite(const char *name, bool required)
624 {
625         Sprite *sprite = (Sprite*)spriteList.getHead();
626
627         while (sprite->next != NULL)
628         {
629                 sprite = (Sprite*)sprite->next;
630                 
631                 if (strcmp(sprite->name, name) == 0)
632                 {
633                         return sprite;
634                 }
635         }
636
637         if (required)
638                 showErrorAndExit("The requested sprite '%s' does not exist", name);
639
640         return NULL;
641 }
642
643 void Graphics::animateSprites()
644 {
645         Sprite *sprite = (Sprite*)spriteList.getHead();
646
647         while (sprite->next != NULL)
648         {
649                 sprite = (Sprite*)sprite->next;
650
651                 sprite->animate();
652         }
653
654         if ((engine->getFrameLoop() % 8) == 0)
655         {
656                 Math::wrapInt(&(++waterAnim), 201, 204);
657                 Math::wrapInt(&(++slimeAnim), 207, 212);
658                 Math::wrapInt(&(++lavaAnim), 214, 220);
659         }
660 }
661
662 int Graphics::getWaterAnim()
663 {
664         return waterAnim;
665 }
666
667 int Graphics::getSlimeAnim()
668 {
669         return slimeAnim;
670 }
671
672 int Graphics::getLavaAnim()
673 {
674         return lavaAnim;
675 }
676
677 int Graphics::getLavaAnim(int current)
678 {
679         if ((engine->getFrameLoop() % 8) == 0)
680                 return Math::rrand(214, 220);
681
682         return current;
683 }
684
685 void Graphics::loadBackground(const char *filename)
686 {
687         if (background != NULL)
688                 SDL_FreeSurface(background);
689
690         if (strcmp(filename, "@none@") == 0)
691                 return;
692
693         background = loadImage(filename);
694
695         SDL_SetColorKey(background, 0, SDL_MapRGB(background->format, 0, 0, 0));
696 }
697
698 void Graphics::putPixel(int x, int y, Uint32 pixel, SDL_Surface *dest)
699 {
700         if ((x < 0) || (x > 639) || (y < 0) || (y > 479))
701                 return;
702
703         int bpp = dest->format->BytesPerPixel;
704         /* Here p is the address to the pixel we want to set */
705         Uint8 *p = (Uint8 *)dest->pixels + y * dest->pitch + x * bpp;
706
707         switch(bpp)
708         {
709                 case 1:
710                         *p = pixel;
711                         break;
712
713                 case 2:
714                         *(Uint16 *)p = pixel;
715                         break;
716
717                 case 3:
718                         if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
719                         {
720                                 p[0] = (pixel >> 16) & 0xff;
721                                 p[1] = (pixel >> 8) & 0xff;
722                                 p[2] = pixel & 0xff;
723                         }
724                         else
725                         {
726                                 p[0] = pixel & 0xff;
727                                 p[1] = (pixel >> 8) & 0xff;
728                                 p[2] = (pixel >> 16) & 0xff;
729                         }
730                         break;
731
732                 case 4:
733                         *(Uint32 *)p = pixel;
734                         break;
735         }
736 }
737
738 Uint32 Graphics::getPixel(SDL_Surface *surface, int x, int y)
739 {
740         if ((x < 0) || (x > (surface->w - 1)) || (y < 0) || (y > (surface->h - 1)))
741                 return 0;
742
743         int bpp = surface->format->BytesPerPixel;
744         Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
745
746         switch(bpp) {
747         case 1:
748                 return *p;
749
750         case 2:
751                 return *(Uint16 *)p;
752
753         case 3:
754                 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
755                                 return p[0] << 16 | p[1] << 8 | p[2];
756                 else
757                                 return p[0] | p[1] << 8 | p[2] << 16;
758
759         case 4:
760                 return *(Uint32 *)p;
761
762         default:
763                 return 0;       /* shouldn't happen, but avoids warnings */
764         }
765 }
766
767 void Graphics::drawLine(float startX, float startY, float endX, float endY, int color, SDL_Surface *dest)
768 {
769         lock(screen);
770         
771         float dx, dy;
772         
773         Math::calculateSlope(startX, startY, endX, endY, &dx, &dy);
774
775         while (true)
776         {
777                 putPixel((int)startX, (int)startY, color, dest);
778
779                 if ((int)startX == (int)endX)
780                         break;
781
782                 startX -= dx;
783                 startY -= dy;
784         }
785
786         unlock(screen);
787 }
788
789 void Graphics::blit(SDL_Surface *image, int x, int y, SDL_Surface *dest, bool centered)
790 {
791         if (!image)
792         {
793                 showErrorAndExit("graphics::blit() - NULL pointer", SDL_GetError());
794         }
795
796         if ((x < -image->w) || (x > 640 + image->w))
797                 return;
798
799         if ((y < -image->h) || (y > 480 + image->h))
800                 return;
801
802         // Set up a rectangle to draw to
803         gRect.x = x;
804         gRect.y = y;
805         if (centered)
806         {
807                 gRect.x -= (image->w / 2);
808                 gRect.y -= (image->h / 2);
809         }
810
811         gRect.w = image->w;
812         gRect.h = image->h;
813
814         /* Blit onto the screen surface */
815         if (SDL_BlitSurface(image, NULL, dest, &gRect) < 0)
816                 showErrorAndExit("graphics::blit() - %s", SDL_GetError());
817 }
818
819 void Graphics::drawBackground()
820 {
821         if (background != NULL)
822                 blit(background, 0, 0, screen, false);
823         else
824                 SDL_FillRect(screen, NULL, black);
825 }
826
827 void Graphics::drawBackground(SDL_Rect *r)
828 {
829         if (r->x < 0) r->x = 0;
830         if (r->y < 0) r->y = 0;
831         if (r->x + r->w > 639) r->w = 640 - r->x;
832         if (r->y + r->h > 639) r->h = 480 - r->y;
833
834         if (SDL_BlitSurface(background, r, screen, r) < 0)
835                 showErrorAndExit("graphics::blit() - %s", SDL_GetError());
836 }
837
838 void Graphics::drawRect(int x, int y, int w, int h, int color, SDL_Surface *dest)
839 {
840         gRect.x = x;
841         gRect.y = y;
842         gRect.w = w;
843         gRect.h = h;
844
845         SDL_FillRect(dest, &gRect, color);
846 }
847
848 void Graphics::drawRect(int x, int y, int w, int h, int color, int borderColor, SDL_Surface *dest)
849 {
850         drawRect(x - 1, y - 1, w + 2, h + 2, borderColor, dest);
851         drawRect(x, y, w, h, color, dest);
852 }
853
854 void Graphics::setFontColor(int red, int green, int blue, int red2, int green2, int blue2)
855 {
856         fontForeground.r = red;
857         fontForeground.g = green;
858         fontForeground.b = blue;
859
860         fontBackground.r = red2;
861         fontBackground.g = green2;
862         fontBackground.b = blue2;
863
864         fontForeground.unused = fontBackground.unused = 0;
865 }
866
867 void Graphics::setFontSize(int size)
868 {
869         fontSize = size;
870         Math::limitInt(&fontSize, 0, 4);
871 }
872
873 SDL_Surface *Graphics::getString(const char *in, bool transparent)
874 {
875         SDL_Surface *text = TTF_RenderUTF8_Shaded(font[fontSize], in, fontForeground, fontBackground);
876
877         if (!text)
878         {
879                 text = TTF_RenderUTF8_Shaded(font[fontSize], "FONT_ERROR", fontForeground, fontBackground);
880         }
881
882         if (transparent)
883                 setTransparent(text);
884
885         return text;
886 }
887
888 void Graphics::drawString(const char *in, int x, int y, int alignment, SDL_Surface *dest)
889 {
890         bool center = false;
891
892         SDL_Surface *text = TTF_RenderUTF8_Shaded(font[fontSize], in, fontForeground, fontBackground);
893
894         if (!text)
895         {
896                 text = TTF_RenderUTF8_Shaded(font[fontSize], "FONT_ERROR", fontForeground, fontBackground);
897         }
898
899         if (alignment == TXT_RIGHT) x -= text->w;
900         if (alignment == TXT_CENTERED) center = true;
901
902         setTransparent(text);
903         blit(text, x, y, dest, center);
904         SDL_FreeSurface(text);
905 }
906
907 void Graphics::clearChatString()
908 {
909         strcpy(chatString, "");
910 }
911
912 void Graphics::createChatString(const char *in)
913 {
914         sprintf(chatString, "%s %s", chatString, in);
915 }
916
917 void Graphics::drawChatString(SDL_Surface *surface, int y)
918 {
919         char *word = strtok(chatString, " ");
920         char wordWithSpace[100];
921         
922         int r, g, b;
923
924         int x = 10;
925         int surfaceWidth = surface->w - 10;
926
927         SDL_Surface *wordSurface;
928
929         while (word)
930         {
931                 if (strcmp(word, "<RGB>") == 0)
932                 {
933                         r = atoi(strtok(NULL, " "));
934                         g = atoi(strtok(NULL, " "));
935                         b = atoi(strtok(NULL, " "));
936
937                         if ((!r) && (!g) && (!b))
938                         {
939                                 debug(("Parse Error in Text Color (%d:%d:%d)!!\n", r, g, b));
940                                 exit(1);
941                         }
942
943                         setFontColor(r, g, b, 0, 0, 0);
944
945                         word = strtok(NULL, " ");
946
947                         continue;
948                 }
949
950                 sprintf(wordWithSpace, "%s ", word);
951
952                 wordSurface = getString(wordWithSpace, false);
953
954                 if (x + wordSurface->w > surfaceWidth)
955                 {
956                         y += (int)(wordSurface->h * 1.5) ;
957                         x = 10;
958                 }
959
960                 blit(wordSurface, x, y, surface, false);
961
962                 SDL_FreeSurface(wordSurface);
963
964                 x += wordSurface->w;
965
966                 word = strtok(NULL, " ");
967         }
968 }
969
970 void Graphics::showMedalMessage(int type, const char *in)
971 {
972         char message[1024];
973         
974         if (medalMessage != NULL)
975         {
976                 SDL_FreeSurface(medalMessage);
977         }
978         
979         setFontSize(0);
980         
981         switch (type)
982         {
983                 // Bronze
984                 case 1:
985                         setFontColor(0xA6, 0x7D, 0x3D, 0x00, 0x00, 0x00);
986                         break;
987                 
988                 // Silver
989                 case 2:
990                         setFontColor(0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00);
991                         break;
992                         
993                 // Gold
994                 case 3:
995                         setFontColor(0xFF, 0xCC, 0x33, 0x00, 0x00, 0x00);
996                         break;
997                         
998                 // Ruby
999                 case 4:
1000                         setFontColor(0xFF, 0x11, 0x55, 0x00, 0x00, 0x00);
1001                         break;
1002         }
1003         
1004         medalType = type - 1; // for indexing on the image
1005         if (type != -1)
1006         {
1007                 sprintf(message, "  Medal Earned - %s  ", in);
1008                 medalMessage = getString(message, true);
1009         }
1010         else
1011         {
1012                 sprintf(message, "  %s  ", in);
1013                 medalMessage = getString(message, true);
1014         }
1015         medalMessageTimer = (5 * 60);
1016 }
1017
1018 void Graphics::drawWidgetRect(int x, int y, int w, int h)
1019 {
1020         drawRect(x - 5, y - 4, w + 10, h + 8, white, screen);
1021         drawRect(x - 4, y - 3, w + 8, h + 6, black, screen);
1022         drawRect(x - 3, y - 2, w + 6, h + 4, green, screen);
1023 }
1024
1025 SDL_Surface *Graphics::createSurface(int width, int height)
1026 {
1027         SDL_Surface *surface, *newImage;
1028
1029         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
1030
1031         if (surface == NULL)
1032                 showErrorAndExit("CreateRGBSurface failed: %s\n", SDL_GetError());
1033
1034         newImage = SDL_DisplayFormat(surface);
1035
1036         SDL_FreeSurface(surface);
1037
1038         return newImage;
1039 }
1040
1041 SDL_Surface *Graphics::alphaRect(int width, int height, Uint8 red, Uint8 green, Uint8 blue)
1042 {
1043         SDL_Surface *surface = createSurface(width, height);
1044
1045         SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, red, green, blue));
1046
1047         SDL_SetAlpha(surface, SDL_SRCALPHA|SDL_RLEACCEL, 130);
1048
1049         return surface;
1050 }
1051
1052 void Graphics::colorize(SDL_Surface *image, int red, int green, int blue)
1053 {
1054         SDL_Surface *alpha = alphaRect(image->w, image->h, red, green, blue);
1055
1056         blit(alpha, 0, 0, image, false);
1057
1058         SDL_SetColorKey(image, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(image->format, red / 2, green / 2, blue / 2));
1059 }
1060
1061 void Graphics::lock(SDL_Surface *surface)
1062 {
1063         /* Lock the screen for direct access to the pixels */
1064         if (SDL_MUSTLOCK(surface))
1065         {
1066                 if (SDL_LockSurface(surface) < 0 )
1067                 {
1068                         showErrorAndExit("Could not lock surface", "");
1069                 }
1070         }
1071 }
1072
1073 void Graphics::unlock(SDL_Surface *surface)
1074 {
1075         if (SDL_MUSTLOCK(surface))
1076         {
1077                 SDL_UnlockSurface(surface);
1078         }
1079 }
1080
1081 void Graphics::resetLoading()
1082 {
1083         currentLoading = 0;
1084 }
1085
1086 void Graphics::showLoading(int amount, int max)
1087 {
1088         #if USEPAK
1089         max *= 4;
1090
1091         if (max > 398)
1092                 max = 398;
1093
1094         Math::limitInt(&(currentLoading += amount), 0, max);
1095
1096         drawRect(120, 420, 400, 10, black, white, screen);
1097         drawRect(121, 421, currentLoading, 8, red, screen);
1098
1099         SDL_UpdateRect(screen, 120, 420, 400, 10);
1100         #endif
1101 }
1102
1103 void Graphics::showLicenseErrorAndExit()
1104 {
1105         setFontSize(3); setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
1106         drawString("License Agreement Missing", 320, 50, true, screen);
1107         
1108         setFontSize(1); setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
1109
1110         drawString("The GNU General Public License was not found.", 320, 180, true, screen);
1111         drawString("It could either not be properly loaded or has been removed.", 320, 220, true, screen);
1112         drawString("Blob Wars : Metal Blob Solid will not run with the license missing.", 320, 260, true, screen);
1113         
1114         drawString("Blob Wars : Metal Blob Solid will now exit", 320, 420, true, screen);
1115         drawString("Press Escape to continue", 320, 450, true, screen);
1116
1117         engine->flushInput();
1118
1119         while (true)
1120         {
1121                 updateScreen();
1122                 engine->getInput();
1123                 if (engine->keyState[SDLK_ESCAPE])
1124                         exit(1);
1125                 SDL_Delay(16);
1126         }
1127 }
1128
1129 void Graphics::showErrorAndExit(const char *error, const char *param)
1130 {
1131         SDL_FillRect(screen, NULL, black);
1132
1133         if (strcmp(param, "LICENSE") == 0)
1134         {
1135                 showLicenseErrorAndExit();
1136         }
1137
1138         char message[256];
1139         sprintf(message, error, param);
1140
1141         setFontSize(3); setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
1142         drawString("An unforseen error has occurred", 320, 50, true, screen);
1143         setFontSize(1); setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
1144         drawString(message, 320, 90, true, screen);
1145
1146         drawString("You may wish to try the following,", 50, 150, false, screen);
1147
1148         setFontSize(0);
1149         drawString("1) Try reinstalling the game.", 75, 190, false, screen);
1150         drawString("2) Ensure you have SDL 1.2.5 or greater installed.", 75, 210, false, screen);
1151         drawString("3) Ensure you have the latest versions of additional required SDL libraries.", 75, 230, false, screen);
1152         drawString("4) Install using an RPM if you originally built the game from source", 75, 250, false, screen);
1153         drawString("or try building from source if you installed using an RPM.", 75, 270, false, screen);
1154         drawString("5) Visit http://www.parallelrealities.co.uk/blobWars.php and check for updates.", 75, 290, false, screen);
1155
1156         setFontSize(1);
1157
1158         drawString("If problems persist contact Parallel Realities. Please be aware however that we will not", 320, 360, true, screen);
1159         drawString("be able to assist in cases where the code or data has been modified.", 320, 380, true, screen);
1160
1161         drawString("Blob Wars : Metal Blob Solid will now exit", 320, 420, true, screen);
1162         drawString("Press Escape to continue", 320, 450, true, screen);
1163
1164         engine->flushInput();
1165
1166         while (true)
1167         {
1168                 updateScreen();
1169                 engine->getInput();
1170                 if (engine->keyState[SDLK_ESCAPE])
1171                 {
1172                         exit(1);
1173                 }
1174                 SDL_Delay(16);
1175         }
1176 }
1177
1178 void Graphics::showRootWarning()
1179 {
1180         setFontSize(3); setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
1181         drawString("CAUTION - RUNNING AS ROOT USER!", 320, 50, true, screen);
1182         
1183         setFontSize(1); setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
1184
1185         drawString("WARNING - You appear to be running the game as the root user!", 320, 180, true, screen);
1186         drawString("This is not recommended and is it strongly advised that you do not run", 320, 220, true, screen);
1187         drawString("the game as root. You may still continue but consider running as regular user in future!", 320, 260, true, screen);
1188         
1189         drawString("Press Space to Exit", 320, 420, true, screen);
1190         drawString("Press Escape to Continue", 320, 450, true, screen);
1191
1192         engine->flushInput();
1193
1194         while (true)
1195         {
1196                 updateScreen();
1197                 engine->getInput();
1198                 
1199                 if (engine->keyState[SDLK_ESCAPE])
1200                 {
1201                         return;
1202                 }
1203                 else if (engine->keyState[SDLK_SPACE])
1204                 {
1205                         exit(0);
1206                 }
1207
1208                 SDL_Delay(16);
1209         }
1210 }