1 // BlinkenSisters - Hunt for the Lost Pixels
2 //     Bringing back the fun of the 80s
3 //
4 // (C) 2005-07 Rene Schickbauer, Wolfgang Dautermann
5 //
6 // See License.txt for licensing information
7 //
8 
9 
10 #include "globals.h"
11 #include "drawprimitives.h"
12 #include "errorhandler.h"
13 #include <math.h>
14 #ifndef M_PI
15 #define M_PI	3.141592654
16 #endif
17 #include <SDL_endian.h> /* für einen 24-Bit-Modus unabhängig von der Bytereihenfolge */
18 
19 
drawcircle(const Sint32 x,const Sint32 y,const Sint32 radius,const Uint32 color)20 void drawcircle(const Sint32 x, const Sint32 y, const Sint32 radius, const Uint32 color)
21 {
22 	float i;
23 	Sint32 pixelx, pixely ;
24 	Uint8 r, g, b ;
25 	SDL_GetRGB(color, gScreen->format, &r, &g, &b);
26 	for (i=0; i<2*M_PI; i+=0.01) {
27 		pixelx = x + (Sint32)(radius*cos(i));
28 		pixely = y + (Sint32)(radius*sin(i));
29 		if (pixelx >=0 && pixelx < SCR_WIDTH && pixely >=0 && pixely < SCR_HEIGHT)
30 			DrawPixel(gScreen, pixelx, pixely, r, g, b );
31   }
32 }
33 
34 
35 // Draw a rectangle - this is just a convinience wrapper around SDL_FillRect
drawrect(const Sint32 x,const Sint32 y,const Sint32 width,const Sint32 height,const Uint32 color)36 void drawrect(const Sint32 x, const Sint32 y, const Sint32 width, const Sint32 height, const Uint32 color)
37 {
38 	SDL_Rect rectLocation = { x, y, width, height };
39 	SDL_FillRect(gScreen, &rectLocation, color);
40 }
41 
BS_IMG_Load_DisplayFormat(const char * filename,bool die_on_error)42 SDL_Surface * BS_IMG_Load_DisplayFormat(const char * filename, bool die_on_error)
43 {
44         SDL_Surface *temp = IMG_Load(filename);
45         if(!temp) {
46 		if(die_on_error == true) {
47                 	DIE(ERROR_IMAGE_READ, filename);
48 		} else {
49 			return NULL ;
50 		}
51         }
52 	SDL_Surface * convertedsurface = SDL_DisplayFormat(temp);
53 	SDL_FreeSurface(temp);
54 	return convertedsurface ;
55 }
56 
57 
58 /* DrawPixel example from http://www.libsdl.de/docs/sdl-intro/sdl-intro.htm */
DrawPixel(SDL_Surface * screen,int x,int y,Uint8 R,Uint8 G,Uint8 B)59 void DrawPixel(SDL_Surface *screen, int x, int y,Uint8 R, Uint8 G,Uint8 B)
60 {
61     Uint32 color = SDL_MapRGB(screen->format, R, G, B);
62 
63     if ( SDL_MUSTLOCK(screen) )
64     {
65         if ( SDL_LockSurface(screen) < 0 ) {
66             return;
67         }
68     }
69 
70     switch (screen->format->BytesPerPixel) {
71         case 1: { /* vermutlich 8 Bit */
72             Uint8 *bufp;
73 
74             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
75             *bufp = color;
76         }
77         break;
78 
79         case 2: { /* vermutlich 15 Bit oder 16 Bit */
80             Uint16 *bufp;
81 
82             bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
83             *bufp = color;
84         }
85         break;
86 
87         case 3: { /* langsamer 24-Bit-Modus, selten verwendet */
88             Uint8 *bufp;
89 
90             bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
91             if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
92                 bufp[0] = color;
93                 bufp[1] = color >> 8;
94                 bufp[2] = color >> 16;
95             } else {
96                 bufp[2] = color;
97                 bufp[1] = color >> 8;
98                 bufp[0] = color >> 16;
99             }
100         }
101         break;
102 
103         case 4: { /* vermutlich 32 Bit */
104             Uint32 *bufp;
105 
106             bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
107             *bufp = color;
108         }
109         break;
110     }
111     if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
112 }
113 
114