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 "nomlib/config.hpp"
37#include "nomlib/math/Color4.hpp"
38#include "nomlib/math/Rect.hpp"
39#include "nomlib/math/Size2.hpp"
40#include "nomlib/math/Point2.hpp"
41
42// FIXME: The following declaration is necessary in order to avoid a very
43// nasty compiling conflict that can happen under Windows anytime the
44// wingdi.h header file is included (commonly from windows.h), due to min and
45// max macros being declared there. This is why macros are evil.
46//
47// In this case, it's probably not a bad idea to rename our RGB functions...
48// seeing as they are terrible names for a function description, anyhow!
49//
50// http://support.microsoft.com/kb/143208
51// http://stackoverflow.com/questions/5004858/stdmin-gives-error
52#if defined( NOM_PLATFORM_WINDOWS )
53 #undef RGB
54#endif
55
56// Forward declarations
57struct _TTF_Font {};
58
59namespace nom {
60
61enum BlendMode
62{
65 BLEND_MODE_NONE = SDL_BLENDMODE_NONE,
66
70 BLEND_MODE_BLEND = SDL_BLENDMODE_BLEND,
71
75 BLEND_MODE_ADD = SDL_BLENDMODE_ADD,
76
80 BLEND_MODE_MOD = SDL_BLENDMODE_MOD
81};
82
84enum PixelFormat
85{
91 PIXEL_FORMAT_INVALID = SDL_PIXELFORMAT_UNKNOWN,
92
94 PIXEL_FORMAT_ARGB8888 = SDL_PIXELFORMAT_ARGB8888,
95
97 PIXEL_FORMAT_YV12 = SDL_PIXELFORMAT_YV12,
98
100 PIXEL_FORMAT_IYUV = SDL_PIXELFORMAT_IYUV,
101
103 PIXEL_FORMAT_UYVY = SDL_PIXELFORMAT_UYVY,
104};
105
110namespace SDL_WINDOW
111{
112 typedef std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> UniquePtr;
113 typedef SDL_Window* RawPtr;
114}
115
120{
121 typedef SDL_PixelFormat* RawPtr;
122}
123
128namespace SDL_SURFACE
129{
130 typedef std::unique_ptr<SDL_Surface, void(*) (SDL_Surface*)> UniquePtr;
131 typedef std::shared_ptr<SDL_Surface> SharedPtr;
132 typedef SDL_Surface* RawPtr;
133}
134
139namespace SDL_TEXTURE
140{
141 typedef std::shared_ptr<SDL_Texture> SharedPtr;
142 typedef SDL_Texture* RawPtr;
143}
144
147BlendMode blend_mode(SDL_BlendMode mode);
148
151SDL_BlendMode SDL_blend_mode(BlendMode mode);
152
156SDL_Rect SDL_RECT ( const IntRect& rectangle );
157
163SDL_Rect SDL_RECT ( const Point2i& pos, const Size2i& size );
164
168SDL_Point SDL_POINT ( const Point2i& );
169
173SDL_Color SDL_COLOR ( const Color4i& color );
174
178const Color4i pixel ( uint32 pixel, const SDL_PixelFormat* fmt );
179
183const Color4i alpha_pixel ( uint32 pixel, const SDL_PixelFormat* fmt );
184
185const Color4i pixel ( uint32 pixel, uint32 fmt );
186const Color4i alpha_pixel ( uint32 pixel, uint32 fmt );
187
194uint32 RGB ( const Color4i& color, const SDL_PixelFormat* fmt );
195
202uint32 RGB ( const Color4i& color, uint32 fmt );
203
210uint32 RGBA ( const Color4i& color, const SDL_PixelFormat* fmt );
211
218uint32 RGBA ( const Color4i& color, uint32 fmt );
219
227std::string hint(const std::string& name);
228
240bool set_hint(const std::string& name, const std::string& value);
241
242const std::string PIXEL_FORMAT_NAME ( uint32 format );
243
251bool save_png ( SDL_SURFACE::RawPtr, const std::string& filename );
252
259int available_render_driver(const std::string& driver);
260
261namespace priv {
262
264void FreeWindow ( SDL_Window* );
265
267void FreeRenderTarget ( SDL_Renderer* );
268
270void FreeTexture ( SDL_Texture* );
271
273void FreeSurface ( SDL_Surface* );
274
276void TTF_FreeFont ( _TTF_Font* );
277
278} // namespace priv
279} // namespace nom
280
282std::ostream& operator <<( std::ostream& os, const SDL_Point& pos );
283
285std::ostream& operator <<( std::ostream& os, const SDL_Rect& rect );
286
288std::ostream& operator <<( std::ostream& os, const SDL_Color& color );
289
290#endif // include guard defined
Convenience definitions for pointer types.
Convenience definitions for pointer types.
Convenience definitions for pointer types.
Convenience definitions for pointer types.