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 <cmath>
23 #include <iostream>
24 
25 #include <SDL.h>
26 #include <SDL_image.h>
27 
28 #include <SDL2pp/SDL.hh>
29 #include <SDL2pp/SDLImage.hh>
30 #include <SDL2pp/Window.hh>
31 #include <SDL2pp/Renderer.hh>
32 #include <SDL2pp/Texture.hh>
33 #include <SDL2pp/Surface.hh>
34 
35 using namespace SDL2pp;
36 
37 static const float pi = 3.14159265358979323846f;
38 
main(int,char * [])39 int main(int, char*[]) try {
40 	SDL sdl(SDL_INIT_VIDEO);
41 	SDLImage image(IMG_INIT_PNG); // optional
42 	Window window("libSDL2pp demo: loading", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
43 	Renderer render(window, -1, SDL_RENDERER_ACCELERATED);
44 
45 	// Load sprite texture; sprite1 and sprite2 are actually the same
46 	// however first one is loaded directly into texture, and second
47 	// one is loaded through an intermediary surface
48 	Surface surf(TESTDATA_DIR "/test.png");
49 
50 	Texture sprite1(render, TESTDATA_DIR "/test.png");
51 	Texture sprite2(render, surf);
52 
53 	sprite1.SetBlendMode(SDL_BLENDMODE_BLEND);
54 	sprite2.SetBlendMode(SDL_BLENDMODE_BLEND);
55 
56 	render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
57 
58 	while (1) {
59 		// Process input
60 		SDL_Event event;
61 		while (SDL_PollEvent(&event))
62 			if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q)))
63 				return 0;
64 
65 		// Clear screen
66 		render.SetDrawColor(255, 255, 255);
67 		render.Clear();
68 
69 		// Simple copy
70 		float angle = SDL_GetTicks() / 5000.0f * 2.0f * pi;
71 		render.Copy(sprite1, NullOpt, Rect(320 - 64, 240 - 64, 128, 128), angle / pi * 180.0f);
72 		render.Copy(sprite1, NullOpt, Rect(320 - 32 + (int)(std::sin(angle) * 40.0f), 240 - 32 + (int)(std::cos(angle) * 40.0f), 64, 64));
73 		render.Copy(sprite2, NullOpt, Rect(320 - 32 - (int)(std::sin(angle) * 40.0f), 240 - 32 - (int)(std::cos(angle) * 40.0f), 64, 64));
74 
75 		render.Present();
76 
77 		// Frame limiter
78 		SDL_Delay(1);
79 	}
80 
81 	return 0;
82 } catch (std::exception& e) {
83 	std::cerr << "Error: " << e.what() << std::endl;
84 	return 1;
85 }
86