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