1 /***************************************************************************
2                           sdltools.h  -  description
3                              -------------------
4     begin                : Fri Jul 21 2006
5     copyright            : (C) 2006 by Immi
6     email                : cuyo@pcpool.mathematik.uni-freiburg.de
7 
8 Modified 2006,2008,2010,2011 by the cuyo developers
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  *                                                                         *
14  *   This program is free software; you can redistribute it and/or modify  *
15  *   it under the terms of the GNU General Public License as published by  *
16  *   the Free Software Foundation; either version 2 of the License, or     *
17  *   (at your option) any later version.                                   *
18  *                                                                         *
19  ***************************************************************************/
20 
21 #ifndef SDLTOOLS_H
22 #define SDLTOOLS_H
23 
24 #include <SDL.h>
25 
26 #include <maske.h>
27 
28 #define scale_base 4
29 
30 class Str;
31 
32 
33 struct Color {
34   Uint8 mR, mG, mB;
35 
ColorColor36   Color() {}
ColorColor37   Color(Uint8 r, Uint8 g, Uint8 b): mR(r), mG(g), mB(b) {}
38 
39   /* Convert to SDL pixel value */
40   Uint32 getPixel(SDL_PixelFormat *fmt = SDL_GetVideoSurface()->format) const {
41     return SDL_MapRGB(fmt, mR, mG, mB);
42   }
43 
44   Uint8 & operator[](int i) {return i == 0 ? mR : (i == 1 ? mG : mB); }
45   Uint8 operator[](int i) const {return i == 0 ? mR : (i == 1 ? mG : mB); }
46 };
47 
48 
49 
50 
51 namespace SDLTools {
52 
53   /* opt_w, opt_h: size of window, as given by command line option;
54      or -1,-1 to automatically choose window size */
55   void initSDL(int opt_w, int opt_h);
56 
57   /* Call while cuyo is already running is not yet supported
58      (but not difficult to implement) */
59   void setFullscreen(bool fs);
60 
61   /* Change the size of the window from the view of the cuyo program.
62      Does *not* change the real window size; instead, scaling may
63      change.
64      I propose that this should only be used in such a way that when
65      the real window size is the preferred one (L_preferred_xxx),
66      then scaling should never change.
67      Note that changing the scaling always takes some time.
68   */
69   void setVirtualWindowSize(int w, int h);
70 
71   void setMainTitle();
72   void setLevelTitle(const Str & levelname);
73 
74   /* Convert Qt-Key into SDL-Key; don't use Qt constants: we don't want to
75      depend on Qt just to be able to read old .cuyo files. */
76   SDLKey qtKey2sdlKey(int qtk);
77 
78 
79   SDL_Rect rect(int x, int y, int w = 0, int h = 0);
80 
81   bool intersection(const SDL_Rect & a, const SDL_Rect & b, SDL_Rect & ret);
82 
83 
84   /* Creates a 32-bit-surface with alpha. After filling it with your
85      data, you should convert it to screen format */
86   SDL_Surface * createSurface32(int w, int h);
87 
88   /* Converts a surface to a 32-bit-surface with alpha. The original surface
89      is deleted. */
90   void convertSurface32(SDL_Surface *& s);
91 
92   /* Return a reference to the pixel at (x, y);
93      assumes that the surface is 32-Bit.
94      NOTE: The surface must be locked before calling this! */
95   Uint32 & getPixel32(SDL_Surface *surface, int x, int y);
96 
97   /* Converts the surface to a format suitable for fast blitting onto the
98      display surface. Contrary to SDL_DisplayFormat, transparency is
99      respected, at least where it is full transparency. Contrary to
100      SDL_DisplayFormatAlpha, it uses ColourKey for paletted surfaces.
101      The source surface is assumed to be 32-Bit. */
102   SDL_Surface * maskedDisplayFormat(SDL_Surface *);
103 
104   SDL_Surface * createMaskedDisplaySurface(int w, int h);
105 
106   /* Scales the surface according to our window size;
107      s must be 32 bit.
108      Warning: It is possible that a new surface is created and
109      returned, but is is also possible that just a pointer to s
110      is returned. */
111   SDL_Surface * scaleSurface(SDL_Surface * s);
112 
113   int getScale();
114 
115   /* Like SDL_PollEvent, but the event is already scaled.
116      And for resize events, the window and scaling is already
117      prepared. */
118   bool pollEvent(SDL_Event & evt);
119 }
120 
121 
122 /**
123   An area is a sub-rectangle of a surface. All drawing commands
124   take place inside the active area.
125   Calls to enter() (enter a sub-area) can be nested.
126   (This has not been tested yet.)
127   Right now,
128   - The surface is always the screen surface
129   - There exists only one global Area
130 
131   Right now, all calls to methods of Area (except init and destroy)
132   are expected to happen in subroutines of UI::allesAnzeigen()
133 */
134 namespace Area {
135   void destroy();
136 
137   void enter(SDL_Rect r);
138   void leave();
139 
140   void setClip(SDL_Rect r);
141   void noClip();
142 
143   void setBackground(const SDL_Rect &, SDL_Surface *);
144     /** Setting the background happens relative to the current area.
145         It defines the behaviour of subsequent calls to ...
146         leave()ing the current area unsets the background. */
147   void maskBackground(const Maske *, SDL_Rect, int x, int y);
148     /* Copies the background through the mask to the screen.
149        The SDL_Rect is relative to the mask in unscaled coordinates.
150        x and y are the destination on the screen relative to the current area.
151     */
152 
153   /* If the coordinates of srcrect are not a multiple of scale_base, then
154      rounding of the width and height of srcrect is done in such a way that
155      the result is correct in the *destination*, not in the source. */
156   void blitSurface(SDL_Surface *src, SDL_Rect srcrect, int dstx, int dsty);
157   void blitSurface(SDL_Surface *src, int dstx, int dsty);
158 
159   void fillRect(SDL_Rect dst, const Color & c);
160   void fillRect(int x, int y, int w, int h, const Color & c);
161 
162   /* Fills everything outside the current virtual window */
163   void fillBorder(const Color & c);
164 
165   /* You have to call the following methods to make your drawing operations
166      really visible on the screen. (However, the update will take place only
167      at the next call to doUpdate) */
168   void updateRect(SDL_Rect dst);
169   void updateRect(int x, int y, int w, int h);
170   /* Better than calling updateRect(bigRect): Stops collecting small
171      rectangles. All means really all, not only active area. */
172   void updateAll();
173 
174   /* To be called only by ui.cpp */
175   void doUpdate();
176 }
177 
178 
179 #endif
180 
181