1 /*
2  * Copyright (C) 2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
3  *
4  * This file is part of hoverboard-sdl.
5  *
6  * hoverboard-sdl is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * hoverboard-sdl is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with hoverboard-sdl.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "tile.hh"
21 
22 #include <SDL2/SDL_render.h>
23 
24 #include <SDL2pp/Renderer.hh>
25 #include <SDL2pp/Texture.hh>
26 
~VisualData()27 Tile::VisualData::~VisualData() {
28 }
29 
NeedsUpgrade() const30 bool Tile::VisualData::NeedsUpgrade() const {
31 	return false;
32 }
33 
Upgrade(SDL2pp::Renderer &) const34 std::unique_ptr<Tile::TextureVisual> Tile::VisualData::Upgrade(SDL2pp::Renderer&) const {
35 	return nullptr;
36 }
37 
~NoVisual()38 Tile::NoVisual::~NoVisual() {
39 }
40 
Render(SDL2pp::Renderer &,const SDL2pp::Point &)41 void Tile::NoVisual::Render(SDL2pp::Renderer&, const SDL2pp::Point&) {
42 }
43 
SolidVisual(const SDL_Color & color)44 Tile::SolidVisual::SolidVisual(const SDL_Color& color) : color_(color) {
45 }
46 
~SolidVisual()47 Tile::SolidVisual::~SolidVisual() {
48 }
49 
Render(SDL2pp::Renderer & renderer,const SDL2pp::Point & offset)50 void Tile::SolidVisual::Render(SDL2pp::Renderer& renderer, const SDL2pp::Point& offset) {
51 	renderer.SetDrawColor(color_.r, color_.g, color_.b, color_.a);
52 	renderer.FillRect(SDL2pp::Rect(offset.x, offset.y, tile_size_, tile_size_));
53 }
54 
PixelVisual(PixelVisual::PixelData && pixels)55 Tile::PixelVisual::PixelVisual(PixelVisual::PixelData&& pixels) : pixels_(std::move(pixels)) {
56 }
57 
~PixelVisual()58 Tile::PixelVisual::~PixelVisual() {
59 }
60 
NeedsUpgrade() const61 bool Tile::PixelVisual::NeedsUpgrade() const {
62 	return true;
63 }
64 
Upgrade(SDL2pp::Renderer & renderer) const65 std::unique_ptr<Tile::TextureVisual> Tile::PixelVisual::Upgrade(SDL2pp::Renderer& renderer) const {
66 	return std::unique_ptr<Tile::TextureVisual>(new TextureVisual(renderer, pixels_));
67 }
68 
Render(SDL2pp::Renderer &,const SDL2pp::Point &)69 void Tile::PixelVisual::Render(SDL2pp::Renderer&, const SDL2pp::Point&) {
70 }
71 
TextureVisual(SDL2pp::Renderer & renderer,const Tile::PixelVisual::PixelData & pixels)72 Tile::TextureVisual::TextureVisual(SDL2pp::Renderer& renderer, const Tile::PixelVisual::PixelData& pixels)
73 	: texture_(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, tile_size_, tile_size_) {
74 	texture_.Update(SDL2pp::NullOpt, pixels.data(), tile_size_ * 4);
75 }
76 
~TextureVisual()77 Tile::TextureVisual::~TextureVisual() {
78 }
79 
Render(SDL2pp::Renderer & renderer,const SDL2pp::Point & offset)80 void Tile::TextureVisual::Render(SDL2pp::Renderer& renderer, const SDL2pp::Point& offset) {
81 	renderer.Copy(texture_, SDL2pp::NullOpt, offset);
82 }
83