SDL_helpers.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/SDL_helpers.hpp"
30 
31 namespace nom {
32 
33 SDL_bool SDL_BOOL ( bool value )
34 {
35  if ( value )
36  {
37  return SDL_TRUE;
38  }
39  else
40  {
41  return SDL_FALSE;
42  }
43 }
44 
45 SDL_Rect SDL_RECT ( const Coords& coords )
46 {
47  SDL_Rect r;
48 
49  r.x = coords.x;
50  r.y = coords.y;
51  r.w = coords.width;
52  r.h = coords.height;
53 
54  return r;
55 }
56 
57 SDL_Color SDL_COLOR ( const Color& color )
58 {
59  SDL_Color c;
60 
61  c.r = color.red;
62  c.g = color.green;
63  c.b = color.blue;
64  c.a = color.alpha;
65 
66  return c;
67 }
68 
69 const Color pixel ( uint32 pixel, const SDL_PixelFormat* fmt )
70 {
71  Color c;
72 
73  SDL_GetRGB ( pixel, fmt, &c.red, &c.green, &c.blue );
74 
75  return c;
76 }
77 
78 const Color alpha_pixel ( uint32 pixel, const SDL_PixelFormat* fmt )
79 {
80  Color c;
81 
82  SDL_GetRGBA ( pixel, fmt, &c.red, &c.green, &c.blue, &c.alpha );
83 
84  return c;
85 }
86 
87 uint32 RGB ( const Color& color, const SDL_PixelFormat* fmt )
88 {
89  return SDL_MapRGB ( fmt, color.red, color.green, color.blue );
90 }
91 
92 uint32 RGB ( const Color& color, uint32 fmt )
93 {
94  return SDL_MapRGB ( SDL_AllocFormat(fmt), color.red, color.green, color.blue );
95 }
96 
97 uint32 RGBA ( const Color& color, const SDL_PixelFormat* fmt )
98 {
99  return SDL_MapRGBA ( fmt, color.red, color.green, color.blue, color.alpha );
100 }
101 
102 uint32 RGBA ( const Color& color, uint32 fmt )
103 {
104  return SDL_MapRGBA ( SDL_AllocFormat(fmt), color.red, color.green, color.blue, color.alpha );
105 }
106 
107 uint32 ticks ( void )
108 {
109  return SDL_GetTicks();
110 }
111 
112 const std::string hint ( const std::string& name )
113 {
114  std::string result;
115 
116  result = SDL_GetHint ( name.c_str() );
117  return result;
118 }
119 
120 bool set_hint ( const std::string& name, const std::string& value )
121 {
122  if ( SDL_SetHint ( name.c_str(), value.c_str() ) == false ) return false;
123 
124  return true;
125 }
126 
127 namespace priv {
128 
129 void FreeWindow ( SDL_Window* handle )
130 {
131  SDL_DestroyWindow ( handle );
132 }
133 
134 void FreeRenderTarget ( SDL_Renderer* handle )
135 {
136  SDL_DestroyRenderer ( handle );
137 }
138 
139 void FreeTexture ( SDL_Texture* video_buffer )
140 {
141  SDL_DestroyTexture ( video_buffer );
142 }
143 
144 void FreeSurface ( SDL_Surface* video_buffer )
145 {
146  SDL_FreeSurface ( video_buffer );
147 }
148 
149 void TTF_FreeFont ( TTF_Font* font )
150 {
151  TTF_CloseFont ( font );
152 }
153 
154 void Free_Joystick ( SDL_Joystick* joy )
155 {
156  if ( joy != nullptr )
157  {
158  if ( SDL_JoystickGetAttached ( joy ) )
159  {
160  SDL_JoystickClose ( joy );
161  joy = nullptr;
162  }
163  }
164 }
165 
166 } // namespace priv
167 } // namespace nom