1 /*
2 libSDL2pp - C++11 bindings/wrapper for SDL2
3 Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 #include <iostream>
23
24 #include <SDL.h>
25
26 #include <SDL2pp/SDL.hh>
27 #include <SDL2pp/Window.hh>
28 #include <SDL2pp/Renderer.hh>
29 #include <SDL2pp/Texture.hh>
30
31 using namespace SDL2pp;
32
33 #define RGBA(r, g, b, a) r, g, b, a
34 static const unsigned char pixels[4 * 4 * 4] = {
35 RGBA(0xff, 0x00, 0x00, 0xff), RGBA(0xff, 0x80, 0x00, 0xff), RGBA(0xff, 0xff, 0x00, 0xff), RGBA(0x80, 0xff, 0x00, 0xff),
36 RGBA(0xff, 0x00, 0x80, 0xff), RGBA(0xff, 0xff, 0xff, 0xff), RGBA(0x00, 0x00, 0x00, 0x00), RGBA(0x00, 0xff, 0x00, 0xff),
37 RGBA(0xff, 0x00, 0xff, 0xff), RGBA(0x00, 0x00, 0x00, 0x00), RGBA(0x00, 0x00, 0x00, 0xff), RGBA(0x00, 0xff, 0x80, 0xff),
38 RGBA(0x80, 0x00, 0xff, 0xff), RGBA(0x00, 0x00, 0xff, 0xff), RGBA(0x00, 0x80, 0xff, 0xff), RGBA(0x00, 0xff, 0xff, 0xff),
39 };
40
41 enum {
42 MY_SPRITE_SIZE = 4,
43 MY_SCREEN_WIDTH = 640,
44 MY_SCREEN_HEIGHT = 480,
45 MY_RENDERTARGET_SIZE = 512,
46 };
47
main(int,char * [])48 int main(int, char*[]) try {
49 SDL sdl(SDL_INIT_VIDEO);
50 Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, MY_SCREEN_WIDTH, MY_SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);
51 Renderer render(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
52 render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
53
54 // Necessary checks according to SDL docs
55 SDL_RendererInfo ri;
56 render.GetInfo(ri);
57
58 if (!(ri.flags & SDL_RENDERER_TARGETTEXTURE)) {
59 std::cerr << "Sorry, your renderer doesn't support texture targets" << std::endl;
60 return 1;
61 }
62
63 // Sprite data
64 Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, MY_SPRITE_SIZE, MY_SPRITE_SIZE);
65
66 sprite.Update(NullOpt, pixels, MY_SPRITE_SIZE * MY_SPRITE_SIZE);
67 sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
68
69 // Two render target textures
70 Texture target1(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, MY_RENDERTARGET_SIZE, MY_RENDERTARGET_SIZE);
71 target1.SetBlendMode(SDL_BLENDMODE_BLEND);
72
73 Texture target2(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, MY_RENDERTARGET_SIZE, MY_RENDERTARGET_SIZE);
74 target2.SetBlendMode(SDL_BLENDMODE_BLEND);
75
76 while (1) {
77 // Process input
78 SDL_Event event;
79 while (SDL_PollEvent(&event))
80 if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q)))
81 return 0;
82
83 // Note we fill with transparent color, not black
84 render.SetDrawColor(0, 0, 0, 0);
85
86 // Fill base texture with sprite texture
87 render.SetTarget(target1);
88 render.Clear();
89 render.Copy(sprite);
90
91 // Repeat several cycles of flip-flop tiling
92 for (int i = 0; i < 4; i++) {
93 render.SetTarget(target2);
94 render.Clear();
95 render.Copy(target1, NullOpt, Rect(0, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
96 render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
97 render.Copy(target1, NullOpt, Rect(0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
98 render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0);
99
100 // Swap textures to copy recursively
101 std::swap(target1, target2);
102 }
103
104 // Draw result to screen
105 render.SetTarget();
106 render.Clear();
107
108 render.Copy(target1, NullOpt, Rect((MY_SCREEN_WIDTH - MY_SCREEN_HEIGHT) / 2, 0, MY_SCREEN_HEIGHT, MY_SCREEN_HEIGHT), SDL_GetTicks() / 10000.0 * 360.0);
109
110 render.Present();
111
112 // Frame limiter
113 SDL_Delay(1);
114 }
115
116 return 0;
117 } catch (std::exception& e) {
118 std::cerr << "Error: " << e.what() << std::endl;
119 return 1;
120 }
121