nomlib
Loading...
Searching...
No Matches
SDL_helpers.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. 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
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON 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
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SDL2_HELPERS_HPP
30#define NOMLIB_SDL2_HELPERS_HPP
31
32#include <memory>
33
34#include <SDL.h>
35
36#include "export.hpp"
37#include "nomlib/config.hpp"
38#include "nomlib/math/Color4.hpp"
39#include "nomlib/math/Rect.hpp"
40#include "nomlib/math/Size2.hpp"
41#include "nomlib/math/Point2.hpp"
42
43// FIXME: The following declaration is necessary in order to avoid a very
44// nasty compiling conflict that can happen under Windows anytime the
45// wingdi.h header file is included (commonly from windows.h), due to min and
46// max macros being declared there. This is why macros are evil.
47//
48// In this case, it's probably not a bad idea to rename our RGB functions...
49// seeing as they are terrible names for a function description, anyhow!
50//
51// http://support.microsoft.com/kb/143208
52// http://stackoverflow.com/questions/5004858/stdmin-gives-error
53#if defined( NOM_PLATFORM_WINDOWS )
54 #undef RGB
55#endif
56
57// SDL2_TTF forwards compatibility check
58//
59// This implementation code was written with the assumption of SDL2_ttf
60// v2.0.12 in mind -- since then, the public headers for the project has
61// altered their internal declaration of TTF_Font. Our original forward
62// declaration becomes invalid without the following version checks.
63//
64// SEE ALSO
65// 1. https://github.com/libsdl-org/SDL_ttf/issues/372
66#if defined(NOM_USE_SDL_TTF_MAJOR_VERSION) && defined(NOM_USE_SDL_TTF_MINOR_VERSION)
67 #if (NOM_USE_SDL_TTF_MAJOR_VERSION == 2) && (NOM_USE_SDL_TTF_MINOR_VERSION >= 23)
68 // Forward declarations for SDL2 TTF v2.23.0 and greater
69 typedef struct TTF_Font TTF_Font;
70 #else
71 // Forward declarations for SDL2_TTF v2.0.12
72 typedef struct _TTF_Font TTF_Font;
73 #endif
74#else // Assume latest API when no version info is available
75 // Forward declarations for SDL2 TTF v2.23.0 and greater
76 typedef struct TTF_Font TTF_Font;
77#endif
78
79namespace nom {
80
81enum BlendMode
82{
85 BLEND_MODE_NONE = SDL_BLENDMODE_NONE,
86
90 BLEND_MODE_BLEND = SDL_BLENDMODE_BLEND,
91
95 BLEND_MODE_ADD = SDL_BLENDMODE_ADD,
96
100 BLEND_MODE_MOD = SDL_BLENDMODE_MOD
101};
102
104enum PixelFormat
105{
111 PIXEL_FORMAT_INVALID = SDL_PIXELFORMAT_UNKNOWN,
112
114 PIXEL_FORMAT_ARGB8888 = SDL_PIXELFORMAT_ARGB8888,
115
117 PIXEL_FORMAT_YV12 = SDL_PIXELFORMAT_YV12,
118
120 PIXEL_FORMAT_IYUV = SDL_PIXELFORMAT_IYUV,
121
123 PIXEL_FORMAT_UYVY = SDL_PIXELFORMAT_UYVY,
124};
125
130namespace SDL_WINDOW
131{
132 typedef std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> UniquePtr;
133 typedef SDL_Window* RawPtr;
134}
135
140{
141 typedef SDL_PixelFormat* RawPtr;
142}
143
148namespace SDL_SURFACE
149{
150 typedef std::unique_ptr<SDL_Surface, void(*) (SDL_Surface*)> UniquePtr;
151 typedef std::shared_ptr<SDL_Surface> SharedPtr;
152 typedef SDL_Surface* RawPtr;
153}
154
159namespace SDL_TEXTURE
160{
161 typedef std::shared_ptr<SDL_Texture> SharedPtr;
162 typedef SDL_Texture* RawPtr;
163}
164
167NOM_EXPORT BlendMode blend_mode(SDL_BlendMode mode);
168
171NOM_EXPORT SDL_BlendMode SDL_blend_mode(BlendMode mode);
172
176NOM_EXPORT SDL_Rect SDL_RECT ( const IntRect& rectangle );
177
183NOM_EXPORT SDL_Rect SDL_RECT ( const Point2i& pos, const Size2i& size );
184
188NOM_EXPORT SDL_Point SDL_POINT ( const Point2i& );
189
193NOM_EXPORT SDL_Color SDL_COLOR ( const Color4i& color );
194
198NOM_EXPORT const Color4i pixel ( uint32 pixel, const SDL_PixelFormat* fmt );
199
203NOM_EXPORT
204const Color4i alpha_pixel ( uint32 pixel, const SDL_PixelFormat* fmt );
205
206NOM_EXPORT const Color4i pixel ( uint32 pixel, uint32 fmt );
207NOM_EXPORT const Color4i alpha_pixel ( uint32 pixel, uint32 fmt );
208
215NOM_EXPORT
216uint32 RGB ( const Color4i& color, const SDL_PixelFormat* fmt );
217
224NOM_EXPORT uint32 RGB ( const Color4i& color, uint32 fmt );
225
232NOM_EXPORT uint32 RGBA ( const Color4i& color, const SDL_PixelFormat* fmt );
233
240NOM_EXPORT uint32 RGBA ( const Color4i& color, uint32 fmt );
241
249NOM_EXPORT std::string hint(const std::string& name);
250
262NOM_EXPORT bool set_hint(const std::string& name, const std::string& value);
263
264NOM_EXPORT const std::string PIXEL_FORMAT_NAME ( uint32 format );
265
273NOM_EXPORT bool save_png ( SDL_SURFACE::RawPtr, const std::string& filename );
274
281NOM_EXPORT int available_render_driver(const std::string& driver);
282
283namespace priv {
284
286NOM_EXPORT void FreeWindow ( SDL_Window* );
287
289NOM_EXPORT void FreeRenderTarget ( SDL_Renderer* );
290
292NOM_EXPORT void FreeTexture ( SDL_Texture* );
293
295NOM_EXPORT void FreeSurface ( SDL_Surface* );
296
298NOM_EXPORT void TTF_FreeFont ( TTF_Font* );
299
300} // namespace priv
301} // namespace nom
302
304NOM_EXPORT
305std::ostream& operator <<( std::ostream& os, const SDL_Point& pos );
306
308NOM_EXPORT
309std::ostream& operator <<( std::ostream& os, const SDL_Rect& rect );
310
312NOM_EXPORT
313std::ostream& operator <<( std::ostream& os, const SDL_Color& color );
314
315#endif // include guard defined
Convenience definitions for pointer types.
Convenience definitions for pointer types.
Convenience definitions for pointer types.
Convenience definitions for pointer types.