| 1 | // SuperTux |
| 2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | #include "video/sdl/sdl_texture_renderer.hpp" |
| 18 | |
| 19 | #include <sstream> |
| 20 | |
| 21 | #include "supertux/globals.hpp" |
| 22 | #include "util/log.hpp" |
| 23 | #include "video/drawing_request.hpp" |
| 24 | #include "video/sdl/sdl_painter.hpp" |
| 25 | #include "video/sdl/sdl_screen_renderer.hpp" |
| 26 | #include "video/sdl/sdl_texture.hpp" |
| 27 | #include "video/sdl/sdl_video_system.hpp" |
| 28 | #include "video/video_system.hpp" |
| 29 | |
| 30 | SDLTextureRenderer::SDLTextureRenderer(SDLVideoSystem& video_system, SDL_Renderer* renderer, const Size& size, int downscale) : |
| 31 | m_video_system(video_system), |
| 32 | m_renderer(renderer), |
| 33 | m_painter(m_video_system, *this, m_renderer), |
| 34 | m_size(size), |
| 35 | m_downscale(downscale), |
| 36 | m_texture() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | SDLTextureRenderer::~SDLTextureRenderer() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | SDL_Texture* |
| 45 | SDLTextureRenderer::get_sdl_texture() const |
| 46 | { |
| 47 | return static_cast<SDLTexture*>(m_texture.get())->get_texture(); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | SDLTextureRenderer::start_draw() |
| 52 | { |
| 53 | if (!m_texture) |
| 54 | { |
| 55 | const int w = m_size.width / m_downscale; |
| 56 | const int h = m_size.height / m_downscale; |
| 57 | SDL_Texture* sdl_texture = SDL_CreateTexture(m_renderer, |
| 58 | SDL_PIXELFORMAT_RGB888, |
| 59 | SDL_TEXTUREACCESS_TARGET, |
| 60 | w, h); |
| 61 | if (!sdl_texture) |
| 62 | { |
| 63 | std::stringstream msg; |
| 64 | msg << "Couldn't create lightmap texture: " << SDL_GetError(); |
| 65 | throw std::runtime_error(msg.str()); |
| 66 | } |
| 67 | |
| 68 | m_texture = TexturePtr(new SDLTexture(sdl_texture, w, h, Sampler())); |
| 69 | } |
| 70 | |
| 71 | SDL_SetRenderTarget(m_renderer, get_sdl_texture()); |
| 72 | SDL_RenderSetScale(m_renderer, |
| 73 | 1.0f / static_cast<float>(m_downscale), |
| 74 | 1.0f / static_cast<float>(m_downscale)); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | SDLTextureRenderer::end_draw() |
| 79 | { |
| 80 | SDL_RenderSetScale(m_renderer, 1.0f, 1.0f); |
| 81 | SDL_SetRenderTarget(m_renderer, nullptr); |
| 82 | } |
| 83 | |
| 84 | Rect |
| 85 | SDLTextureRenderer::get_rect() const |
| 86 | { |
| 87 | return Rect(0, 0, |
| 88 | Size(m_size.width / m_downscale, |
| 89 | m_size.height / m_downscale)); |
| 90 | } |
| 91 | |
| 92 | Size |
| 93 | SDLTextureRenderer::get_logical_size() const |
| 94 | { |
| 95 | return m_size; |
| 96 | } |
| 97 | |
| 98 | TexturePtr |
| 99 | SDLTextureRenderer::get_texture() const |
| 100 | { |
| 101 | return m_texture; |
| 102 | } |
| 103 | |
| 104 | /* EOF */ |
| 105 | |