MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Drawing.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Drawing.h SDL_Surface rendering toolkit
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library 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. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef DRAWING_H
29 #define DRAWING_H
30 
31 #include "AssetManager.h"
32 #include "itype.h"
33 #include "SDL.h" /* For SDL_Surface, SDL_Color */
34 #include "SDL_ttf.h" /* For TTF_Font, at least */
35 #include "Rect.h"
36 #include "Tile.h"
37 
38 namespace MFM {
39 
44  class Drawing
45  {
49  Rect m_rect;
50 
54  u32 m_bgColor;
55 
59  u32 m_fgColor;
60 
64  SDL_Surface * m_dest;
65 
69  TTF_Font* m_font;
70 
71  static void SetSDLColor(SDL_Color & set, const u32 from) ;
72  static void GetSDLColor(const SDL_Color & from, u32 & to) ;
73 
74  public:
75 
76  static const int WHITE = 0xffffffff;
77  static const int BLACK = 0xff000000;
78  static const int RED = 0xffff0000;
79  static const int GREEN = 0xff00ff00;
80  static const int BLUE = 0xff0000ff;
81  static const int CYAN = 0xff00ffff;
82  static const int MAGENTA = 0xffff00ff;
83  static const int YELLOW = 0xffffff00;
84 
85  static const int GREY05 = 0xff0c0c0c;
86  static const int GREY10 = 0xff191919;
87  static const int GREY20 = 0xff333333;
88  static const int GREY30 = 0xff4c4c4c;
89  static const int GREY40 = 0xff666666;
90  static const int GREY50 = 0xff7f7f7f;
91  static const int GREY60 = 0xff999999;
92  static const int GREY70 = 0xffb2b2b2;
93  static const int GREY80 = 0xffcccccc;
94  static const int GREY90 = 0xffe5e5e5;
95  static const int GREY95 = 0xfff2f2f2;
96 
97  static const int GREY = GREY50;
98  static const int DARK_PURPLE = 0xff200020;
99  static const int ORANGE = 0xffff6600;
100 
101  static u32 HalfColor(u32 input)
102  {
103  u8 r, g, b;
104  r = (input & 0xff0000) >> 17;
105  g = (input & 0x00ff00) >> 9;
106  b = (input & 0xff) >> 1;
107 
108  return 0xff000000 | (r << 16) | (g << 8) | b;
109  }
110 
111  Drawing(SDL_Surface * dest = 0, TTF_Font * font = 0) ;
112 
122  static u32 InterpolateColors(const u32 color1, const u32 color2, u32 percentOfColor1);
123 
130  void TransformWindow(const Rect & rect) ;
131 
137  static void TransformWindow(const Rect & existing, const Rect & newTransform, Rect & result) ;
138 
142  void GetWindow(Rect & rect) const ;
143 
147  void SetWindow(const Rect & rect) ;
148 
152  void Reset(SDL_Surface * dest, TTF_Font * font);
153 
157  void Clear();
158 
162  u32 GetBackground() const
163  {
164  return m_bgColor;
165  }
166 
170  u32 SetBackground(const u32 color)
171  {
172  u32 old = m_bgColor;
173  m_bgColor = color;
174  return old;
175  }
176 
180  u32 GetForeground() const
181  {
182  return m_fgColor;
183  }
184 
188  u32 SetForeground(const u32 color)
189  {
190  u32 old = m_fgColor;
191  m_fgColor = color;
192  return old;
193  }
194 
199  TTF_Font* GetFont() const
200  {
201  return m_font;
202  }
203 
209  TTF_Font* SetFont(TTF_Font * newFont)
210  {
211  TTF_Font* old = m_font;
212  m_font = newFont;
213  return old;
214  }
215 
225  TTF_Font* SetFont(FontAsset asset)
226  {
227  return SetFont(AssetManager::Get(asset));
228  }
229 
233  void DrawHLine(int y, int startX, int endX) const;
234 
238  void DrawVLine(int x, int startY, int endY) const;
239 
244  void DrawRectangle(const Rect & rect) const;
245 
249  void FillRect(int x, int y, int w, int h) const;
250 
255  void FillRect(int x, int y, int w, int h, u32 color) const;
256 
260  void FillRect(const Rect & rect) const
261  {
262  FillRect(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight());
263  }
264 
269  void FillCircle(int x, int y, int w, int h, int radius) const;
270 
271 
276  void BlitText(const char* message, UPoint loc, UPoint size) const;
277 
283  void BlitBackedText(const char* message, UPoint loc, UPoint size);
284 
288  void BlitBackedTextCentered(const char* message, UPoint loc, UPoint size);
289 
294  SPoint GetTextSize(const char* message) ;
295 
300  SPoint GetTextSizeInFont(const char* message, TTF_Font * thisFont) ;
301 
305  void BlitImage(SDL_Surface* image, UPoint loc, UPoint maxSize) const;
306 
310  void BlitAsset(Asset asset, UPoint loc, UPoint maxSize) const;
311 
312 
313  static void Convert(const Rect & rect, SDL_Rect & toFill) ;
314 
315  private:
316  // Let's try to deprecate SetPixel. Bounds-checking every pixel is
317  // slow, and SDL_FillRect is (like SSE) fast and also clips against
318  // the surface cliprect so it's bounds-checking anyway?
319  static inline void SetPixel(SDL_Surface* dest,
320  u32 x, u32 y,
321  u32 color)
322  {
323  if(x >= 0 && y >= 0 && x < (u32)dest->w && y < (u32)dest->h)
324  {
325  ((u32*)dest->pixels)[x + y * dest->w] = color;
326  }
327  }
328  };
329 } /* namespace MFM */
330 
331 #endif /*DRAWING_H*/
Definition: Rect.h:40
s32 GetX() const
Definition: Rect.h:147
static u32 InterpolateColors(const u32 color1, const u32 color2, u32 percentOfColor1)
Definition: Drawing.cpp:41
void FillCircle(int x, int y, int w, int h, int radius) const
Definition: Drawing.cpp:135
void BlitBackedTextCentered(const char *message, UPoint loc, UPoint size)
Definition: Drawing.cpp:220
TTF_Font * GetFont() const
Definition: Drawing.h:199
void GetWindow(Rect &rect) const
Definition: Drawing.cpp:36
void SetWindow(const Rect &rect)
Definition: Drawing.cpp:31
void BlitAsset(Asset asset, UPoint loc, UPoint maxSize) const
Definition: Drawing.cpp:167
u32 GetWidth() const
Definition: Rect.h:209
u32 SetForeground(const u32 color)
Definition: Drawing.h:188
void FillRect(const Rect &rect) const
Definition: Drawing.h:260
void Clear()
Definition: Drawing.cpp:91
void BlitImage(SDL_Surface *image, UPoint loc, UPoint maxSize) const
Definition: Drawing.cpp:147
SPoint GetTextSizeInFont(const char *message, TTF_Font *thisFont)
Definition: Drawing.cpp:206
void BlitBackedText(const char *message, UPoint loc, UPoint size)
Definition: Drawing.cpp:233
SPoint GetTextSize(const char *message)
Definition: Drawing.cpp:201
u32 SetBackground(const u32 color)
Definition: Drawing.h:170
void DrawRectangle(const Rect &rect) const
Definition: Drawing.cpp:106
void DrawHLine(int y, int startX, int endX) const
Definition: Drawing.cpp:96
u32 GetForeground() const
Definition: Drawing.h:180
void TransformWindow(const Rect &rect)
Definition: Drawing.cpp:16
void FillRect(int x, int y, int w, int h) const
Definition: Drawing.cpp:115
void DrawVLine(int x, int startY, int endY) const
Definition: Drawing.cpp:101
TTF_Font * SetFont(FontAsset asset)
Definition: Drawing.h:225
void Reset(SDL_Surface *dest, TTF_Font *font)
Definition: Drawing.cpp:75
u32 GetBackground() const
Definition: Drawing.h:162
TTF_Font * SetFont(TTF_Font *newFont)
Definition: Drawing.h:209
void BlitText(const char *message, UPoint loc, UPoint size) const
Definition: Drawing.cpp:172
u32 GetHeight() const
Definition: Rect.h:219
s32 GetY() const
Definition: Rect.h:157
Definition: Drawing.h:44