mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-04 03:15:59 -04:00
Split demo into multiple programs to keep them simple and clear
This commit is contained in:
parent
3d57457a33
commit
21c7e3c73c
@ -35,13 +35,16 @@ IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
|
|||||||
ADD_LIBRARY(SDL2pp SHARED ${LIBRARY_SOURCES})
|
ADD_LIBRARY(SDL2pp SHARED ${LIBRARY_SOURCES})
|
||||||
TARGET_LINK_LIBRARIES(SDL2pp ${SDL2_LIBRARY})
|
TARGET_LINK_LIBRARIES(SDL2pp ${SDL2_LIBRARY})
|
||||||
|
|
||||||
# demo
|
# demos
|
||||||
SET(DEMO_SOURCES
|
SET(DEMOS
|
||||||
demo/demo.cc
|
sprites
|
||||||
|
lines
|
||||||
)
|
)
|
||||||
|
|
||||||
ADD_EXECUTABLE(demo ${DEMO_SOURCES})
|
FOREACH(DEMO ${DEMOS})
|
||||||
TARGET_LINK_LIBRARIES(demo SDL2pp)
|
ADD_EXECUTABLE(demo_${DEMO} demo/${DEMO}.cc)
|
||||||
|
TARGET_LINK_LIBRARIES(demo_${DEMO} SDL2pp)
|
||||||
|
ENDFOREACH(DEMO ${DEMOS})
|
||||||
ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
|
ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
|
||||||
MESSAGE(STATUS "libSDL2pp bundled build")
|
MESSAGE(STATUS "libSDL2pp bundled build")
|
||||||
|
|
||||||
|
91
demo/lines.cc
Normal file
91
demo/lines.cc
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
libSDL2pp - C++ wrapper for libSDL2
|
||||||
|
Copyright (C) 2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#include <SDL2pp/SDL2pp.hh>
|
||||||
|
|
||||||
|
using namespace SDL2pp;
|
||||||
|
|
||||||
|
int Run() {
|
||||||
|
SDL sdl(SDL_INIT_VIDEO);
|
||||||
|
Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
|
||||||
|
Renderer render(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
|
||||||
|
|
||||||
|
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
// Process input
|
||||||
|
SDL_Event event;
|
||||||
|
while (SDL_PollEvent(&event))
|
||||||
|
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q)))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Clear screen
|
||||||
|
render.SetDrawColor(0, 32, 32);
|
||||||
|
render.Clear();
|
||||||
|
|
||||||
|
// Render lines
|
||||||
|
render.SetDrawColor(255, 0, 0);
|
||||||
|
render.DrawLine(10, 10, 630, 10);
|
||||||
|
render.SetDrawColor(0, 255, 0);
|
||||||
|
render.DrawLine(630, 10, 630, 470);
|
||||||
|
render.SetDrawColor(0, 0, 255);
|
||||||
|
render.DrawLine(630, 470, 10, 470);
|
||||||
|
render.SetDrawColor(255, 255, 255);
|
||||||
|
render.DrawLine(10, 470, 10, 10);
|
||||||
|
|
||||||
|
render.SetDrawColor(255, 255, 255, 127);
|
||||||
|
render.FillRect(0, 0, 20, 20);
|
||||||
|
render.SetDrawColor(255, 255, 255);
|
||||||
|
render.DrawRect(0, 0, 20, 20);
|
||||||
|
|
||||||
|
// Pixel-perfectness test
|
||||||
|
render.SetDrawColor(192, 192, 192);
|
||||||
|
render.DrawLine(6, 2, 6, 10);
|
||||||
|
render.DrawLine(2, 6, 10, 6);
|
||||||
|
|
||||||
|
render.SetDrawColor(255, 255, 255);
|
||||||
|
render.DrawRect(5, 5, 7, 7);
|
||||||
|
render.DrawRect(3, 3, 9, 9);
|
||||||
|
|
||||||
|
render.Present();
|
||||||
|
|
||||||
|
// Frame limiter
|
||||||
|
SDL_Delay(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
try {
|
||||||
|
return Run();
|
||||||
|
} catch (Exception& e) {
|
||||||
|
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
@ -37,61 +37,45 @@ unsigned char pixels[4 * 4 * 4] = {
|
|||||||
|
|
||||||
int Run() {
|
int Run() {
|
||||||
SDL sdl(SDL_INIT_VIDEO);
|
SDL sdl(SDL_INIT_VIDEO);
|
||||||
Window window("libSDL2pp demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
|
Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
|
||||||
Renderer render(window, -1, SDL_RENDERER_ACCELERATED);
|
Renderer render(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
|
||||||
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4);
|
|
||||||
|
|
||||||
|
// Load sprite texture
|
||||||
|
Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4);
|
||||||
sprite.Update(Rect::Null(), pixels, 4 * 4);
|
sprite.Update(Rect::Null(), pixels, 4 * 4);
|
||||||
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
sprite.SetBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
|
render.SetDrawBlendMode(SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// Process events
|
// Process input
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event))
|
||||||
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE))
|
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q)))
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
// Render
|
// Clear screen
|
||||||
render.SetDrawColor(0, 0, 0);
|
render.SetDrawColor(0, 32, 32);
|
||||||
render.Clear();
|
render.Clear();
|
||||||
|
|
||||||
// Render lines
|
// Simple copy
|
||||||
render.SetDrawColor(255, 0, 0);
|
render.Copy(sprite, Rect::Null(), Rect(80, 0, 240, 240));
|
||||||
render.DrawLine(10, 10, 630, 10);
|
|
||||||
render.SetDrawColor(0, 255, 0);
|
|
||||||
render.DrawLine(630, 10, 630, 470);
|
|
||||||
render.SetDrawColor(0, 0, 255);
|
|
||||||
render.DrawLine(630, 470, 10, 470);
|
|
||||||
render.SetDrawColor(255, 255, 255);
|
|
||||||
render.DrawLine(10, 470, 10, 10);
|
|
||||||
|
|
||||||
render.SetDrawColor(255, 255, 255, 127);
|
// Copy with modulation
|
||||||
render.FillRect(0, 0, 20, 20);
|
render.Copy(sprite, Rect::Null(), Rect(400, 0, 120, 120));
|
||||||
render.SetDrawColor(255, 255, 255);
|
sprite.SetAlphaMod(92);
|
||||||
render.DrawRect(0, 0, 20, 20);
|
render.Copy(sprite, Rect::Null(), Rect(400 + 120, 0, 120, 120));
|
||||||
|
sprite.SetColorMod(255, 0, 0);
|
||||||
|
render.Copy(sprite, Rect::Null(), Rect(400, 0 + 120, 120, 120));
|
||||||
|
sprite.SetAlphaMod();
|
||||||
|
render.Copy(sprite, Rect::Null(), Rect(400 + 120, 0 + 120, 120, 120));
|
||||||
|
sprite.SetColorMod();
|
||||||
|
|
||||||
// Pixel-perfectness test
|
// Copy with rotation
|
||||||
render.SetDrawColor(192, 192, 192);
|
render.Copy(sprite, Rect::Null(), Rect(80, 240, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
|
||||||
render.DrawLine(6, 2, 6, 10);
|
|
||||||
render.DrawLine(2, 6, 10, 6);
|
|
||||||
|
|
||||||
render.SetDrawColor(255, 255, 255);
|
// Rotation around another point
|
||||||
render.DrawRect(5, 5, 7, 7);
|
render.Copy(sprite, Rect::Null(), Rect(520, 360, 120, 120), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point(0, 0), SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
|
||||||
render.DrawRect(3, 3, 9, 9);
|
|
||||||
|
|
||||||
// Render 4 smaller squares
|
|
||||||
sprite.SetAlphaMod(0xff);
|
|
||||||
render.Copy(sprite, Rect::Null(), Rect(80, 0, 240, 240), SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
|
|
||||||
render.Copy(sprite, Rect::Null(), Rect(80, 360, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
|
|
||||||
render.Copy(sprite, Rect::Null(), Rect(400, 0, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
|
|
||||||
render.Copy(sprite, Rect::Null(), Rect(400, 360, 240, 240), SDL_GetTicks() / 5000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
|
|
||||||
|
|
||||||
// Render transparent bigger square
|
|
||||||
sprite.SetAlphaMod(0x80);
|
|
||||||
render.Copy(sprite, Rect::Null(), Rect(80, 0, 480, 480), SDL_GetTicks() / 10000.0 * 360.0, Point::Null(), SDL_FLIP_NONE);
|
|
||||||
|
|
||||||
render.Present();
|
render.Present();
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user