1 /*=============================================================================
2 Blobby Volley 2
3 Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
4 Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 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 #pragma once
22 
23 #include <map>
24 #include <SDL2/SDL.h>
25 
26 #include "Vector.h"
27 #include "Global.h"
28 #include "BlobbyDebug.h"
29 
30 
31 // Text definitions
32 static const int FONT_WIDTH_NORMAL 	=	24;	// Height and width of the normal font.
33 static const int LINE_SPACER_NORMAL	=	 6;	// Extra space between 2 lines in a normal SelectBox.
34 static const int FONT_WIDTH_SMALL	=	12;	// Height and width of the small font.
35 static const int LINE_SPACER_SMALL	=	 3;	// Extra space between 2 lines in a small SelectBox.
36 
37 static const int FONT_INDEX_ASTERISK=	36; // M.W. : Currently a dot because there is no asterisk yet.
38 
39 
40 // Text flags (usable for the RenderManager::drawText() flag parameter)
41 // Just using one byte for now - up to 8 flags.
42 static const int TF_NORMAL		= 0x00; // 0 == false (backward compatibility for state modules)
43 static const int TF_HIGHLIGHT	= 0x01; // 1 == true (backward compatibility for state modules)
44 static const int TF_SMALL_FONT	= 0x02; // Draw a smaller font. (8px instead of 24px)
45 static const int TF_OBFUSCATE	= 0x04; // Obfuscate the text with asterisks. (for password Editboxes)
46 
47 // Text Alignment Flags
48 static const int TF_ALIGN_LEFT	= 0x00;	// Text aligned to the left (default)
49 static const int TF_ALIGN_CENTER= 0x08;	// Text centered
50 static const int TF_ALIGN_RIGHT = 0x10;	// Text aligned right
51 
52 
53 
54 /*! \struct BufferedImage
55 	\brief image data
56 	\details couples the raw image data with its size in a way that is
57 			independend of the used renderer.
58 */
59 struct BufferedImage : public ObjectCounter<BufferedImage>
60 {
61 	int w;
62 	int h;
63 	union
64 	{
65 		SDL_Texture* sdlImage;
66 		unsigned glHandle;
67 	};
68 };
69 
70 
71 /*! \class RenderManager
72 	\brief class for managing rendering
73 	\details
74 	This rendering class reduces all drawing stuff to a few calls
75 	to refresh the objects states. It also abstracts from specific
76 	graphics APIs.
77 	The following implementations are planned (ordered by importance)
78 
79 	RenderManagerSDL:
80 	 Uses standard SDL blits for drawing. It depends on precomputed
81 	 rotated sprites and colors the blobs manually.
82 	 Its fixed to the traditional resolution 800x600.
83 	RenderManagerGL2D:
84 	 This manager relies on OpenGL to accelerate 2D drawing on systems
85 	 like Linux/X11 where SDL acceleration is difficult. It rotates and
86 	 colors its sprites in realtime, but still uses 2D graphics.
87 	RenderManagerGL3D:
88      The GL3D is the top-end RenderManager. It uses newly created meshes
89      and therefore supports vertex morphing for the blobs. It makes use
90      of OpenGL to present special effects like per-pixel-lighting,
91      stencil shadows, motion blur, and much more. It will requiere
92      OpenGL 2.0 compliant graphics hardware.
93 	RenderManagerGP2X:
94 	 This manager is used to port Blobby Volley to the GP2X handheld.
95 	 It makes use of a fixed resolution at 320x240 and others for TV-Out.
96 	 It also uses highly optimised loading routines with raw image data.
97 	 In all other terms its similar to the RenderManagerSDL
98 
99 	\todo This classes need a complete rework! They include far too much information about the actual game.
100 */
101 class RenderManager : public ObjectCounter<RenderManager>
102 {
103 	public:
~RenderManager()104 		virtual ~RenderManager(){};
105 
106 		static RenderManager* createRenderManagerSDL();
107 		//static RenderManager* createRenderManagerGP2X();
108 #ifndef __ANDROID__
109 #ifdef __APPLE__
110 #if MAC_OS_X
111         static RenderManager* createRenderManagerGL2D();
112 #endif
113 #else
114         static RenderManager* createRenderManagerGL2D();
115 #endif
116 #endif
117 		static RenderManager* createRenderManagerNull();
118 
getSingleton()119 		static RenderManager& getSingleton()
120 		{
121 			return *mSingleton;
122 		}
123 
124 		Color getOscillationColor() const;
125 
126 		// Draws the stuff
127 		virtual void draw() = 0;
128 
129 		// This swaps the screen buffers and should be called
130 		// after all draw calls
refresh()131 		virtual void refresh() {};
132 
133 		// Init with the desired Resolution.
134 		// Note: It is not guaranteed that this resolution will be selected
init(int xResolution,int yResolution,bool fullscreen)135 		virtual void init(int xResolution, int yResolution, bool fullscreen) {};
136 
137 		// Frees all internal data
deinit()138 		virtual void deinit() {};
139 
140 		// Set a background image by filename
141 		// Note: There is a default, you dont need to do this
142 		// Returns true on success
setBackground(const std::string & filename)143 		virtual bool setBackground(const std::string& filename) { return true; };
144 
145 		// Colors the standard blob image, which are red and green by default
setBlobColor(int player,Color color)146 		virtual void setBlobColor(int player, Color color) {};
147 
showShadow(bool shadow)148 		virtual void showShadow(bool shadow) {};
149 
150 		// Takes the new balls position and its rotation in radians
setBall(const Vector2 & position,float rotation)151 		virtual void setBall(const Vector2& position, float rotation) {};
152 
153 		// Takes the new position and the animation state as a float,
154 		// because some renderers may interpolate the animation
setBlob(int player,const Vector2 & position,float animationState)155 		virtual void setBlob(int player, const Vector2& position,
156 				float animationState) {};
157 
158 		virtual void setMouseMarker(float position);
159 
160 		// This simply draws the given text with its top left corner at the
161 		// given position and doesn't care about line feeds.
162 		virtual void drawText(const std::string& text, Vector2 position, unsigned int flags = TF_NORMAL) {};
163 
164 		// This loads and draws an image by name
165 		// The according Surface is automatically colorkeyed
166 		// The image is centered around position
167 		virtual void drawImage(const std::string& filename, Vector2 position, Vector2 size = Vector2(0,0)) {};
168 
169 		// This draws a greyed-out area
170 		virtual void drawOverlay(float opacity, Vector2 pos1, Vector2 pos2, Color col = Color(0,0,0)) {}
171 
172 		//Draws a blob
drawBlob(const Vector2 & pos,const Color & col)173 		virtual void drawBlob(const Vector2& pos, const Color& col){};
174 
175 		// Enables particle drawing
startDrawParticles()176 		virtual void startDrawParticles() {};
177 		//Draw blood particle
drawParticle(const Vector2 & pos,int player)178 		virtual void drawParticle(const Vector2& pos, int player){};
179 		// Finishes drawing particles
endDrawParticles()180 		virtual void endDrawParticles() {};
181 
182 		// This forces a redraw of the background, for example
183 		// when the windows was minimized
184 		void redraw();
185 
186 		// This can disable the rendering of ingame graphics, for example for
187 		// the main menu
188 		void drawGame(bool draw);
189 
190 		// This function may be useful for displaying framerates
191 		void setTitle(const std::string& title);
192 
193 		// Returns the window
194 		SDL_Window* getWindow();
195 	protected:
196 		RenderManager();
197 		// Returns -1 on EOF
198 		// Returns index for ? on unknown char
199 		int getNextFontIndex(std::string::const_iterator& iter);
200 		SDL_Surface* highlightSurface(SDL_Surface* surface, int luminance);
201 		SDL_Surface* loadSurface(std::string filename);
202 		SDL_Surface* createEmptySurface(unsigned int width, unsigned int height);
203 
204 		SDL_Window* mWindow;
205 
206 		Vector2 blobShadowPosition(const Vector2& position);
207 		Vector2 ballShadowPosition(const Vector2& position);
208 
209 		SDL_Rect blobRect(const Vector2& position);
210 		SDL_Rect blobShadowRect(const Vector2& position);
211 		SDL_Rect ballRect(const Vector2& position);
212 		SDL_Rect ballShadowRect(const Vector2& position);
213 
214 		bool mDrawGame;
215 
216 		std::map<std::string, BufferedImage*> mImageMap;
217 
218 		float mMouseMarkerPosition;
219 		bool mNeedRedraw;
220 
221 	private:
222 		static RenderManager *mSingleton;
223 
224 };
225