many code-cleanups. merged leveleditor patch from Ricardo Cruz. Fixed bugs. many...
[supertux.git] / src / screen.c
1 /*
2   screen.c
3   
4   Super Tux - Screen Functions
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9   
10   April 11, 2000 - April 22, 2000
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <ctype.h>
25 #endif
26
27 #include "defines.h"
28 #include "globals.h"
29 #include "screen.h"
30 #include "setup.h"
31 #include "type.h"
32
33 /* Needed for line calculations */
34 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
35 #define ABS(x) ((x)>0 ? (x) : (-x))
36
37 /* --- CLEAR SCREEN --- */
38
39 void clearscreen(int r, int g, int b)
40 {
41 #ifndef NOOPENGL
42   if(use_gl)
43     {
44       glClearColor(r/256, g/256, b/256, 1.0);
45       glClear(GL_COLOR_BUFFER_BIT);
46     }
47   else
48 #endif
49
50     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
51
52 }
53
54 /* 'Stolen' from the SDL documentation.
55  * Set the pixel at (x, y) to the given value
56  * NOTE: The surface must be locked before calling this!
57  */
58 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
59 {
60   int bpp = surface->format->BytesPerPixel;
61   /* Here p is the address to the pixel we want to set */
62   Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
63
64   switch(bpp)
65     {
66     case 1:
67       *p = pixel;
68       break;
69
70     case 2:
71       *(Uint16 *)p = pixel;
72       break;
73
74     case 3:
75       if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
76         {
77           p[0] = (pixel >> 16) & 0xff;
78           p[1] = (pixel >> 8) & 0xff;
79           p[2] = pixel & 0xff;
80         }
81       else
82         {
83           p[0] = pixel & 0xff;
84           p[1] = (pixel >> 8) & 0xff;
85           p[2] = (pixel >> 16) & 0xff;
86         }
87       break;
88
89     case 4:
90       *(Uint32 *)p = pixel;
91       break;
92     }
93 }
94
95 /* Draw a single pixel on the screen. */
96 void drawpixel(int x, int y, Uint32 pixel)
97 {
98   /* Lock the screen for direct access to the pixels */
99   if ( SDL_MUSTLOCK(screen) )
100     {
101       if ( SDL_LockSurface(screen) < 0 )
102         {
103           fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
104           return;
105         }
106     }
107
108   if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
109     putpixel(screen, x, y, pixel);
110
111   if ( SDL_MUSTLOCK(screen) )
112     {
113       SDL_UnlockSurface(screen);
114     }
115   /* Update just the part of the display that we've changed */
116   SDL_UpdateRect(screen, x, y, 1, 1);
117 }
118
119 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
120 {
121 #ifndef NOOPENGL
122   if(use_gl)
123     {
124       glEnable(GL_BLEND);
125       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
126       glColor4ub(r, g, b,a);
127
128       glBegin(GL_LINES);
129       glVertex2f(x1, y1);
130       glVertex2f(x2, y2);
131       glEnd();
132       glDisable(GL_BLEND);
133     }
134   else
135     {
136 #endif
137
138       /* Basic unantialiased Bresenham line algorithm */
139       int lg_delta, sh_delta, cycle, lg_step, sh_step;
140       Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
141
142       lg_delta = x2 - x1;
143       sh_delta = y2 - y1;
144       lg_step = SGN(lg_delta);
145       lg_delta = ABS(lg_delta);
146       sh_step = SGN(sh_delta);
147       sh_delta = ABS(sh_delta);
148       if (sh_delta < lg_delta)
149         {
150           cycle = lg_delta >> 1;
151           while (x1 != x2)
152             {
153               drawpixel(x1, y1, color);
154               cycle += sh_delta;
155               if (cycle > lg_delta)
156                 {
157                   cycle -= lg_delta;
158                   y1 += sh_step;
159                 }
160               x1 += lg_step;
161             }
162           drawpixel(x1, y1, color);
163         }
164       cycle = sh_delta >> 1;
165       while (y1 != y2)
166         {
167           drawpixel(x1, y1, color);
168           cycle += lg_delta;
169           if (cycle > sh_delta)
170             {
171               cycle -= sh_delta;
172               x1 += lg_step;
173             }
174           y1 += sh_step;
175         }
176       drawpixel(x1, y1, color);
177     }
178 }
179
180 /* --- FILL A RECT --- */
181
182 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
183 {
184 if(w < 0)
185         {
186         x += w;
187         w = -w;
188         }
189 if(h < 0)
190         {
191         y += h;
192         h = -h;
193         }
194
195 #ifndef NOOPENGL
196   if(use_gl)
197     {
198       glEnable(GL_BLEND);
199       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
200       glColor4ub(r, g, b,a);
201
202       glBegin(GL_POLYGON);
203       glVertex2f(x, y);
204       glVertex2f(x+w, y);
205       glVertex2f(x+w, y+h);
206       glVertex2f(x, y+h);
207       glEnd();
208       glDisable(GL_BLEND);
209     }
210   else
211     {
212 #endif
213       SDL_Rect src, rect;
214       SDL_Surface *temp = NULL;
215
216       rect.x = (int)x;
217       rect.y = (int)y;
218       rect.w = (int)w;
219       rect.h = (int)h;
220
221       if(a != 255)
222         {
223           temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
224                                       screen->format->Rmask,
225                                       screen->format->Gmask,
226                                       screen->format->Bmask,
227                                       screen->format->Amask);
228
229
230           src.x = 0;
231           src.y = 0;
232           src.w = rect.w;
233           src.h = rect.h;
234
235           SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
236
237           SDL_SetAlpha(temp, SDL_SRCALPHA, a);
238
239           SDL_BlitSurface(temp,0,screen,&rect);
240
241           SDL_FreeSurface(temp);
242         }
243       else
244         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
245
246 #ifndef NOOPENGL
247
248     }
249 #endif
250 }
251
252
253 /* --- UPDATE SCREEN --- */
254
255 void updatescreen(void)
256 {
257   if(use_gl)  /*clearscreen(0,0,0);*/
258     SDL_GL_SwapBuffers();
259   else
260     SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
261 }
262
263 void flipscreen(void)
264 {
265   if(use_gl)
266     SDL_GL_SwapBuffers();
267   else
268     SDL_Flip(screen);
269 }
270
271 void update_rect(SDL_Surface *scr, Sint32 x, Sint32 y, Sint32 w, Sint32 h)
272 {
273   if(!use_gl)
274     SDL_UpdateRect(scr, x, y, w, h);
275 }
276