# HG changeset patch # User David Demelier # Date 1458682533 -3600 # Node ID 2418900a1cc583a51f77164323cd9b8d1dcd1b34 # Parent 6bc25027c198f3bd0ace772fa25a8e94de7d0041 Client: switch to bridge pattern (PIMPL) with clean headers Task: #431 diff -r 6bc25027c198 -r 2418900a1cc5 cmake/MalikaniaFunctions.cmake --- a/cmake/MalikaniaFunctions.cmake Tue Mar 22 21:15:41 2016 +0100 +++ b/cmake/MalikaniaFunctions.cmake Tue Mar 22 22:35:33 2016 +0100 @@ -109,6 +109,12 @@ if (${var}) target_include_directories(${target} PUBLIC ${${var}}) endif () + + target_include_directories( + ${target} + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/malikania/${WITH_BACKEND_DIRECTORY} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} + ) endfunction() function(apply_private_includes target var) diff -r 6bc25027c198 -r 2418900a1cc5 cmake/MalikaniaOptions.cmake --- a/cmake/MalikaniaOptions.cmake Tue Mar 22 21:15:41 2016 +0100 +++ b/cmake/MalikaniaOptions.cmake Tue Mar 22 22:35:33 2016 +0100 @@ -63,6 +63,7 @@ if (WITH_BACKEND MATCHES "SDL") set(WITH_BACKEND_SDL TRUE) + set(WITH_BACKEND_DIRECTORY "backend/sdl") else () message(FATAL_ERROR "Unknown backend selected: ${WITH_BACKEND}") endif () diff -r 6bc25027c198 -r 2418900a1cc5 libclient/CMakeLists.txt --- a/libclient/CMakeLists.txt Tue Mar 22 21:15:41 2016 +0100 +++ b/libclient/CMakeLists.txt Tue Mar 22 22:35:33 2016 +0100 @@ -31,6 +31,9 @@ ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Size.h ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Sprite.h ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Window.h + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/${WITH_BACKEND_DIRECTORY}/FontBackend.h + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/${WITH_BACKEND_DIRECTORY}/ImageBackend.h + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/${WITH_BACKEND_DIRECTORY}/WindowBackend.h ) set( @@ -38,29 +41,16 @@ ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Animator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/ClientResourcesLoader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Color.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Font.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Image.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Label.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Sprite.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/Window.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/${WITH_BACKEND_DIRECTORY}/FontBackend.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/${WITH_BACKEND_DIRECTORY}/ImageBackend.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/${WITH_BACKEND_DIRECTORY}/WindowBackend.cpp ) -if (WITH_BACKEND_SDL) - list( - APPEND - HEADERS - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/backend/sdl/ImageSdl.h - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/backend/sdl/WindowSdl.h - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/backend/sdl/FontSdl.h - ) - list( - APPEND - SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/backend/sdl/ImageSdl.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/backend/sdl/WindowSdl.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/malikania/backend/sdl/FontSdl.cpp - ) - list(APPEND DEFINITIONS SDL_MAIN_HANDLED) -endif () - find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) find_package(SDL2_ttf REQUIRED) @@ -70,7 +60,6 @@ TARGET libclient SOURCES ${HEADERS} ${SOURCES} PUBLIC_INCLUDES - ${CMAKE_CURRENT_SOURCE_DIR} ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS} diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/ClientResourcesLoader.cpp --- a/libclient/malikania/ClientResourcesLoader.cpp Tue Mar 22 21:15:41 2016 +0100 +++ b/libclient/malikania/ClientResourcesLoader.cpp Tue Mar 22 22:35:33 2016 +0100 @@ -18,11 +18,10 @@ #include -#include -#include -#include - +#include "Animation.h" #include "ClientResourcesLoader.h" +#include "Size.h" +#include "Sprite.h" namespace malikania { diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/Font.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/Font.cpp Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,40 @@ +/* + * Font.cpp -- font object + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "FontBackend.h" + +namespace malikania { + +Font::Font(std::string data, unsigned size) + : m_backend(std::make_unique(std::move(data), size)) + , m_size(size) +{ +} + +Font::Font(Font &&other) noexcept = default; + +Font::~Font() noexcept = default; + +Size Font::clip(const std::string &text) const +{ + return m_backend->clip(*this, text); +} + +Font &Font::operator=(Font &&other) noexcept = default; + +} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/Font.h --- a/libclient/malikania/Font.h Tue Mar 22 21:15:41 2016 +0100 +++ b/libclient/malikania/Font.h Tue Mar 22 22:35:33 2016 +0100 @@ -16,22 +16,17 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef _MALIKANIA_FONT_H_ -#define _MALIKANIA_FONT_H_ +#ifndef MALIKANIA_FONT_H +#define MALIKANIA_FONT_H /** * @file Font.h * @brief Fonts. */ +#include #include -#include - -#if defined(WITH_BACKEND_SDL) -# include "backend/sdl/FontSdl.h" -#endif - #include namespace malikania { @@ -42,7 +37,9 @@ */ class Font { private: - BackendFont m_backend; + class Backend; + + std::unique_ptr m_backend; unsigned m_size; public: @@ -52,11 +49,19 @@ * @param data the raw data * @param size the size */ - inline Font(std::string data, unsigned size) - : m_backend(std::move(data), size) - , m_size(size) - { - } + Font(std::string data, unsigned size); + + /** + * Default move constructor. + * + * @param other the other font + */ + Font(Font &&other) noexcept; + + /** + * Default destructor. + */ + virtual ~Font() noexcept; /** * Get the font size. @@ -73,9 +78,9 @@ * * @return the backend */ - inline BackendFont &backend() noexcept + inline const Backend &backend() const noexcept { - return m_backend; + return *m_backend; } /** @@ -83,9 +88,9 @@ * * @return the backend */ - inline const BackendFont &backend() const noexcept + inline Backend &backend() noexcept { - return m_backend; + return *m_backend; } /** @@ -94,12 +99,17 @@ * @param text the text to clip * @return the required size */ - inline Size clip(const std::string &text) const - { - return m_backend.clip(*this, text); - } + Size clip(const std::string &text) const; + + /** + * Default move assignment operator. + * + * @param other the other font + * @return this + */ + Font &operator=(Font &&other) noexcept; }; } // !malikania -#endif // _MALIKANIA_FONT_H_ +#endif // MALIKANIA_FONT_H diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/Image.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/Image.cpp Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,49 @@ +/* + * Image.h -- image object + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "ImageBackend.h" + +namespace malikania { + +Image::Image(Window &window, std::string data) + : m_backend(std::make_unique(*this, window, std::move(data))) +{ +} + +Image::Image(Image &&) noexcept = default; + +Image::~Image() noexcept = default; + +const Size &Image::size() const noexcept +{ + return m_backend->size(); +} + +void Image::draw(Window &window, const Point &position) +{ + m_backend->draw(window, position); +} + +void Image::draw(Window &window, const Rectangle &source, const Rectangle &target) +{ + m_backend->draw(window, source, target); +} + +Image &Image::operator=(Image &&) noexcept = default; + +} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/Image.h --- a/libclient/malikania/Image.h Tue Mar 22 21:15:41 2016 +0100 +++ b/libclient/malikania/Image.h Tue Mar 22 22:35:33 2016 +0100 @@ -16,8 +16,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef _MALIKANIA_IMAGE_H_ -#define _MALIKANIA_IMAGE_H_ +#ifndef MALIKANIA_IMAGE_H +#define MALIKANIA_IMAGE_H /** * @file Image.h @@ -27,16 +27,9 @@ #include #include -#include - -#if defined(WITH_BACKEND_SDL) -# include "backend/sdl/ImageSdl.h" -#endif - -#include -#include - #include "Point.h" +#include "Rectangle.h" +#include "Size.h" namespace malikania { @@ -48,7 +41,9 @@ */ class Image { private: - BackendImage m_backend; + class Backend; + + std::unique_ptr m_backend; public: /** @@ -57,9 +52,28 @@ * @param window the window * @param data the data */ - inline Image(Window &window, const std::string &data) - : m_backend(*this, window, data) + Image(Window &window, std::string data); + + /** + * Default move constructor. + * + * @param other the other image + */ + Image(Image &&other) noexcept; + + /** + * Default destructor. + */ + ~Image() noexcept; + + /** + * Overloaded function. + * + * @return the backend + */ + inline Backend &backend() noexcept { + return *m_backend; } /** @@ -67,9 +81,9 @@ * * @return the backend */ - inline BackendImage &backend() noexcept + inline const Backend &backend() const noexcept { - return m_backend; + return *m_backend; } /** @@ -77,10 +91,7 @@ * * @return the size */ - inline const Size &size() const noexcept - { - return m_backend.size(); - } + const Size &size() const noexcept; /** * Draw the image to the window. @@ -88,10 +99,7 @@ * @param window the window * @param position the position */ - inline void draw(Window &window, const Point &position = {0, 0}) - { - m_backend.draw(window, position); - } + void draw(Window &window, const Point &position = {0, 0}); /** * Overloaded function. @@ -100,12 +108,17 @@ * @param source the source to clip * @param target the target destination */ - inline void draw(Window &window, const Rectangle &source, const Rectangle &target) - { - m_backend.draw(window, source, target); - } + void draw(Window &window, const Rectangle &source, const Rectangle &target); + + /** + * Default move assignment operator. + * + * @param other the other image + * @return this + */ + Image &operator=(Image &&image) noexcept; }; } // !malikania -#endif // !_MALIKANIA_IMAGE_H_ +#endif // !MALIKANIA_IMAGE_H diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/Sprite.h --- a/libclient/malikania/Sprite.h Tue Mar 22 21:15:41 2016 +0100 +++ b/libclient/malikania/Sprite.h Tue Mar 22 22:35:33 2016 +0100 @@ -26,8 +26,6 @@ #include -#include - #include "Image.h" namespace malikania { diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/Window.cpp --- a/libclient/malikania/Window.cpp Tue Mar 22 21:15:41 2016 +0100 +++ b/libclient/malikania/Window.cpp Tue Mar 22 22:35:33 2016 +0100 @@ -1,9 +1,49 @@ #include -#include "Window.h" +#include "WindowBackend.h" namespace malikania { +Window::Window(unsigned width, unsigned height) + : m_backend(std::make_unique(*this, width, height)) +{ +} + +Window::Window(Window &&) noexcept = default; + +Window::~Window() noexcept = default; + +Size Window::getWindowResolution() +{ + return m_backend->resolution(); +} + +void Window::processEvent() +{ + m_backend->processEvents(*this); +} + +void Window::clear() +{ + m_backend->clear(); +} + +void Window::update() +{ + m_backend->update(); +} + +void Window::present() +{ + m_backend->present(); +} + +void Window::close() noexcept +{ + m_isOpen = false; + m_backend->close(); +} + void Window::setOnKeyUp(KeyUp function) { m_keyUpList.push_back(std::move(function)); @@ -24,4 +64,52 @@ m_refreshList.push_back(std::move(function)); } -}// !malikania +void Window::setDrawingColor(Color color) +{ + m_drawingColor = std::move(color); + m_backend->setDrawingColor(color); +} + +void Window::drawLine(const Line &line) +{ + m_backend->drawLine(line); +} + +void Window::drawLines(const std::vector &points) +{ + m_backend->drawLines(points); +} + +void Window::drawPoint(const Point &point) +{ + m_backend->drawPoint(point); +} + +void Window::drawPoints(const std::vector &points) +{ + m_backend->drawPoints(points); +} + +void Window::drawRectangle(const Rectangle &rectangle, bool filled, Color fillColor) +{ + m_backend->drawRectangle(rectangle, filled, fillColor); +} + +void Window::drawRectangles(const std::vector &rectangles, bool filled, std::vector fillColors) +{ + m_backend->drawRectangles(rectangles, filled, fillColors); +} + +void Window::drawText(const std::string &text, Font &font, const Rectangle &rectangle) +{ + m_backend->drawText(text, font, rectangle); +} + +void Window::drawText(const std::string &text, Font &font, const Point &point) +{ + m_backend->drawText(text, font, point); +} + +Window &Window::operator=(Window &&) noexcept = default; + +} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/Window.h --- a/libclient/malikania/Window.h Tue Mar 22 21:15:41 2016 +0100 +++ b/libclient/malikania/Window.h Tue Mar 22 22:35:33 2016 +0100 @@ -1,22 +1,19 @@ -#ifndef _MALIKANIA_WINDOW_H_ -#define _MALIKANIA_WINDOW_H_ +#ifndef MALIKANIA_WINDOW_H +#define MALIKANIA_WINDOW_H #include #include #include #include - -#include - -#if defined(WITH_BACKEND_SDL) -# include "backend/sdl/WindowSdl.h" -#endif +#include #include #include "Line.h" #include "Color.h" +#include "Font.h" #include "Point.h" +#include "Rectangle.h" namespace malikania { @@ -32,7 +29,10 @@ using RefreshList = std::vector; private: - BackendWindow m_backend; + class Backend; + + std::unique_ptr m_backend; + bool m_isOpen{true}; KeyUpList m_keyUpList; KeyDownList m_keyDownList; @@ -49,51 +49,38 @@ } public: - inline Window(unsigned width = 640, unsigned height = 480) - : m_backend(width, height) - { - } + Window(unsigned width = 640, unsigned height = 480); + + Window(Window &&) noexcept; - inline BackendWindow &backend() noexcept + virtual ~Window() noexcept; + + inline const Backend &backend() const noexcept { - return m_backend; - } - - inline Size getWindowResolution() - { - return m_backend.resolution(); + return *m_backend; } - inline void processEvent() + inline Backend &backend() noexcept { - m_backend.processEvents(*this); - } - - inline void clear() - { - m_backend.clear(); + return *m_backend; } - inline void update() - { - m_backend.update(); - } + Size getWindowResolution(); + + void processEvent(); - inline void present() - { - m_backend.present(); - } + void clear(); + + void update(); + + void present(); inline bool isOpen() noexcept { return m_isOpen; } - inline void close() noexcept - { - m_isOpen = false; - m_backend.close(); - } + void close() noexcept; void setOnKeyUp(KeyUp function); void setOnKeyDown(KeyDown function); @@ -120,52 +107,26 @@ return m_drawingColor; } - inline void setDrawingColor(Color color) - { - m_drawingColor = std::move(color); - m_backend.setDrawingColor(color); - } + void setDrawingColor(Color color); + + void drawLine(const Line &line); - inline void drawLine(const Line &line) - { - m_backend.drawLine(line); - } + void drawLines(const std::vector &points); - inline void drawLines(const std::vector &points) - { - m_backend.drawLines(points); - } + void drawPoint(const Point &point); - inline void drawPoint(const Point &point) - { - m_backend.drawPoint(point); - } + void drawPoints(const std::vector &points); - inline void drawPoints(const std::vector &points) - { - m_backend.drawPoints(points); - } + void drawRectangle(const Rectangle &rectangle, bool filled = false, Color fillColor = {255, 255, 255, 255}); - inline void drawRectangle(const Rectangle &rectangle, bool filled = false, Color fillColor = {255, 255, 255, 255}) - { - m_backend.drawRectangle(rectangle, filled, fillColor); - } + void drawRectangles(const std::vector &rectangles, bool filled = false + , std::vector fillColors = {{255, 255, 255, 255}}); - inline void drawRectangles(const std::vector &rectangles, bool filled = false - , std::vector fillColors = {{255, 255, 255, 255}}) - { - m_backend.drawRectangles(rectangles, filled, fillColors); - } + void drawText(const std::string &text, Font &font, const Rectangle &rectangle); - inline void drawText(const std::string &text, Font &font, const Rectangle &rectangle) - { - m_backend.drawText(text, font, rectangle); - } + void drawText(const std::string &text, Font &font, const Point &point); - inline void drawText(const std::string &text, Font &font, const Point &point) - { - m_backend.drawText(text, font, point); - } + Window &operator=(Window &&other) noexcept; }; }// !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/FontBackend.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/backend/sdl/FontBackend.cpp Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,57 @@ +/* + * FontSdl.cpp -- font object (SDL2 implementation) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include + +#include "FontBackend.h" + +using namespace std::string_literals; + +namespace malikania { + +Font::Backend::Backend(std::string data, unsigned size) + : m_font(nullptr, nullptr) +{ + auto rw = sdl::RWFromBinary(std::move(data)); + + if (rw == nullptr) { + throw std::runtime_error(SDL_GetError()); + } + + m_font = Handle(TTF_OpenFontRW(rw, true, size), TTF_CloseFont); + + if (m_font == NULL) { + throw std::runtime_error(TTF_GetError()); + } +} + +Size Font::Backend::clip(const Font &, const std::string &text) const +{ + int width, height; + + if (TTF_SizeUTF8(m_font.get(), text.c_str(), &width, &height) != 0) { + throw std::runtime_error(SDL_GetError()); + } + + return Size((unsigned)width, (unsigned)height); +} + +} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/FontBackend.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/backend/sdl/FontBackend.h Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,53 @@ +/* + * FontSdl.h -- font object (SDL2 implementation) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MALIKANIA_FONT_SDL_H +#define MALIKANIA_FONT_SDL_H + +#include + +#include +#include + +#include + +namespace malikania { + +class Font; +class Size; + +class Font::Backend { +private: + using Handle = std::unique_ptr; + + Handle m_font; + +public: + Backend(std::string data, unsigned size); + + inline TTF_Font *font() noexcept + { + return m_font.get(); + } + + Size clip(const Font &self, const std::string &text) const; +}; + +} // !malikania + +#endif // !MALIKANIA_FONT_SDL_H diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/FontSdl.cpp --- a/libclient/malikania/backend/sdl/FontSdl.cpp Tue Mar 22 21:15:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/* - * FontSdl.cpp -- font object (SDL2 implementation) - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include -#include - -using namespace std::string_literals; - -namespace malikania { - -FontSdl::FontSdl(std::string data, unsigned size) - : m_font(nullptr, nullptr) -{ - auto rw = sdl::RWFromBinary(std::move(data)); - - if (rw == nullptr) { - throw std::runtime_error(SDL_GetError()); - } - - m_font = Handle(TTF_OpenFontRW(rw, true, size), TTF_CloseFont); - - if (m_font == NULL) { - throw std::runtime_error(TTF_GetError()); - } -} - -Size FontSdl::clip(const Font &, const std::string &text) const -{ - int width, height; - - if (TTF_SizeUTF8(m_font.get(), text.c_str(), &width, &height) != 0) { - throw std::runtime_error(SDL_GetError()); - } - - return Size((unsigned)width, (unsigned)height); -} - -} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/FontSdl.h --- a/libclient/malikania/backend/sdl/FontSdl.h Tue Mar 22 21:15:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -/* - * FontSdl.h -- font object (SDL2 implementation) - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _MALIKANIA_FONT_SDL_H_ -#define _MALIKANIA_FONT_SDL_H_ - -#include - -#include - -#include "SDL_ttf.h" - -namespace malikania { - -class Font; -class Size; - -class FontSdl { -private: - using Handle = std::unique_ptr; - - Handle m_font; - -public: - FontSdl(std::string data, unsigned size); - - inline TTF_Font *font() noexcept - { - return m_font.get(); - } - - Size clip(const Font &self, const std::string &text) const; -}; - -/* - * "Export" the backend. - */ -using BackendFont = FontSdl; - -} // !malikania - -#endif // !_MALIKANIA_FONT_SDL_H_ diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/ImageBackend.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/backend/sdl/ImageBackend.cpp Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,99 @@ +/* + * ImageSdl.cpp-- image object (SDL2 implementation) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include + +#include "ImageBackend.h" +#include "WindowBackend.h" + +using namespace std::string_literals; + +namespace malikania { + +Image::Backend::Backend(Image &, Window &window, std::string data) + : m_texture(nullptr, nullptr) +{ + /* Initialize the texture */ + auto rw = sdl::RWFromBinary(std::move(data)); + + if (rw == nullptr) { + throw std::runtime_error(SDL_GetError()); + } + + m_texture = Handle(IMG_LoadTexture_RW(window.backend().renderer(), rw, true), SDL_DestroyTexture); + + if (m_texture == nullptr) { + throw std::runtime_error(SDL_GetError()); + } + + /* Store the size */ + int width, height; + + if (SDL_QueryTexture(m_texture.get(), nullptr, nullptr, &width, &height) < 0) { + throw std::runtime_error(SDL_GetError()); + } + + m_size = Size((unsigned)width, (unsigned)height); +} + +void Image::Backend::draw(Window &window, const Point &point) +{ + SDL_Rect target; + + target.x = (int)point.x(); + target.y = (int)point.y(); + target.w = (int)m_size.width(); + target.h = (int)m_size.height(); + + if (SDL_RenderCopy(window.backend().renderer(), m_texture.get(), nullptr, &target) < 0) { + throw std::runtime_error(SDL_GetError()); + } +} + +void Image::Backend::draw(Window &window, const Rectangle &source, const Rectangle &target) +{ + SDL_Rect sr, st; + + sr.x = source.x(); + sr.y = source.y(); + sr.w = (int)source.width(); + sr.h = (int)source.height(); + + st.x = target.x(); + st.y = target.y(); + st.w = (int)target.width(); + st.h = (int)target.height(); + + /* Readjust .w, .h if null */ + if (source.isNull()) { + sr.w = (int)m_size.width(); + sr.h = (int)m_size.height(); + } + if (target.isNull()) { + st.w = (int)m_size.width(); + st.h = (int)m_size.height(); + } + + if (SDL_RenderCopy(window.backend().renderer(), m_texture.get(), &sr, &st) < 0) { + throw std::runtime_error(SDL_GetError()); + } +} + +} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/ImageBackend.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/backend/sdl/ImageBackend.h Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,63 @@ +/* + * ImageSdl.h -- image object (SDL2 implementation) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MALIKANIA_IMAGE_SDL_H +#define MALIKANIA_IMAGE_SDL_H + +#include + +#include + +#include +#include + +namespace malikania { + +class Image; +class Point; +class Rectangle; +class Window; + +class Image::Backend { +private: + using Handle = std::unique_ptr; + + Handle m_texture; + Size m_size; + +public: + Backend(Image &self, Window &window, std::string data); + + inline SDL_Texture *texture() noexcept + { + return m_texture.get(); + } + + inline const Size &size() const noexcept + { + return m_size; + } + + void draw(Window &window, const Point &position); + + void draw(Window &window, const Rectangle &source, const Rectangle &target); +}; + +} // !malikania + +#endif // !MALIKANIA_IMAGE_SDL_H diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/ImageSdl.cpp --- a/libclient/malikania/backend/sdl/ImageSdl.cpp Tue Mar 22 21:15:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -/* - * ImageSdl.cpp-- image object (SDL2 implementation) - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include -#include - -using namespace std::string_literals; - -namespace malikania { - -ImageSdl::ImageSdl(Image &, Window &window, const std::string &data) - : m_texture(nullptr, nullptr) -{ - /* Initialize the texture */ - auto rw = SDL_RWFromMem(const_cast(data.c_str()), data.length()); - - if (rw == nullptr) { - throw std::runtime_error(SDL_GetError()); - } - - m_texture = Handle(IMG_LoadTexture_RW(window.backend().renderer(), rw, true), SDL_DestroyTexture); - - if (m_texture == nullptr) { - throw std::runtime_error(SDL_GetError()); - } - - /* Store the size */ - int width, height; - - if (SDL_QueryTexture(m_texture.get(), nullptr, nullptr, &width, &height) < 0) { - throw std::runtime_error(SDL_GetError()); - } - - m_size = Size((unsigned)width, (unsigned)height); -} - -void ImageSdl::draw(Window &window, const Point &point) -{ - SDL_Rect target; - - target.x = (int)point.x(); - target.y = (int)point.y(); - target.w = (int)m_size.width(); - target.h = (int)m_size.height(); - - if (SDL_RenderCopy(window.backend().renderer(), m_texture.get(), nullptr, &target) < 0) { - throw std::runtime_error(SDL_GetError()); - } -} - -void ImageSdl::draw(Window &window, const Rectangle &source, const Rectangle &target) -{ - SDL_Rect sr, st; - - sr.x = source.x(); - sr.y = source.y(); - sr.w = (int)source.width(); - sr.h = (int)source.height(); - - st.x = target.x(); - st.y = target.y(); - st.w = (int)target.width(); - st.h = (int)target.height(); - - /* Readjust .w, .h if null */ - if (source.isNull()) { - sr.w = (int)m_size.width(); - sr.h = (int)m_size.height(); - } - if (target.isNull()) { - st.w = (int)m_size.width(); - st.h = (int)m_size.height(); - } - - if (SDL_RenderCopy(window.backend().renderer(), m_texture.get(), &sr, &st) < 0) { - throw std::runtime_error(SDL_GetError()); - } -} - -} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/ImageSdl.h --- a/libclient/malikania/backend/sdl/ImageSdl.h Tue Mar 22 21:15:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -/* - * ImageSdl.h -- image object (SDL2 implementation) - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _MALIKANIA_IMAGE_SDL_H_ -#define _MALIKANIA_IMAGE_SDL_H_ - -#include - -#include - -#include - -namespace malikania { - -class Image; -class Point; -class Rectangle; -class Window; - -class ImageSdl { -private: - using Handle = std::unique_ptr; - - Handle m_texture; - Size m_size; - -public: - ImageSdl(Image &image, Window &window, const std::string &data); - - inline SDL_Texture *texture() noexcept - { - return m_texture.get(); - } - - inline const Size &size() const noexcept - { - return m_size; - } - - void draw(Window &window, const Point &position); - - void draw(Window &window, const Rectangle &source, const Rectangle &target); -}; - -/* - * "Export" the backend. - */ -using BackendImage = ImageSdl; - -} // !malikania - -#endif // !_MALIKANIA_IMAGE_SDL_H_ diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/WindowBackend.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/backend/sdl/WindowBackend.cpp Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,244 @@ +/* + * WindowSdl.cpp -- window object (SDL2 implementation) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include + +#include "FontBackend.h" +#include "WindowBackend.h" + +namespace malikania { + +Window::Backend::Backend(Window &, unsigned width, unsigned height) + : m_window(nullptr, nullptr) + , m_renderer(nullptr, nullptr) +{ + SDL_SetMainReady(); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); + + m_window = WindowHandle( + SDL_CreateWindow("Malikania", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL), + SDL_DestroyWindow + ); + + if (m_window == nullptr) { + throw std::runtime_error(SDL_GetError()); + } + + // Create renderer + m_renderer = RendererHandle( + SDL_CreateRenderer(m_window.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), + SDL_DestroyRenderer + ); + + if (m_renderer == nullptr) { + throw std::runtime_error(SDL_GetError()); + } + + if (TTF_Init() == -1) { + throw std::runtime_error(TTF_GetError()); + } +} + +void Window::Backend::processEvents(Window &window) +{ + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_KEYUP: + window.onKeyUp(event.key.keysym.sym); + break; + // TODO continue implemanting all event possible + case SDL_KEYDOWN: + window.onKeyDown(event.key.keysym.sym); + break; + case SDL_QUIT: + window.close(); + break; + default: + break; + } + } +} + +void Window::Backend::clear() +{ + SDL_RenderClear(m_renderer.get()); +} + +void Window::Backend::update() +{ + // TO AUDIT +#if 0 + for (Refresh function : m_refreshList) { + function(); + } +#endif +} + +void Window::Backend::present() +{ + SDL_RenderPresent(m_renderer.get()); +} + +Size Window::Backend::resolution() +{ + SDL_DisplayMode current; + int width = 0; + int height = 0; + for (int i = 0; i < SDL_GetNumVideoDisplays(); i++) { + int error = SDL_GetCurrentDisplayMode(i, ¤t); + if (error == 0) { + // Get the last one + // TODO test with only one display mode, but we have to test with more than that + width = current.w; + height = current.h; + } else { + throw std::runtime_error("Could not get display mode for video display" + std::string(SDL_GetError())); + } + } + + return Size((unsigned)width, (unsigned)height); +} + +void Window::Backend::setDrawingColor(const Color &color) +{ + int error = SDL_SetRenderDrawColor(m_renderer.get(), color.red(), color.green(), color.blue(), color.alpha()); + if (error != 0) { + throw std::runtime_error("Couldn't set drawing color" + std::string(SDL_GetError())); + } +} + +void Window::Backend::drawLine(const Line &line) +{ + if (SDL_RenderDrawLine(m_renderer.get(), line.x1(), line.y1(), line.x2(), line.y2()) != 0) { + throw std::runtime_error(SDL_GetError()); + } +} + +void Window::Backend::drawLines(const std::vector &points) +{ + std::vector sdlPoints(points.size()); + + for (unsigned i = 0; i < points.size(); ++i) { + sdlPoints[i] = SDL_Point{points[i].x(), points[i].y()}; + } + + if (SDL_RenderDrawLines(m_renderer.get(), sdlPoints.data(), sdlPoints.size()) != 0) { + throw std::runtime_error(SDL_GetError()); + } +} + +void Window::Backend::drawPoint(const Point &point) +{ + if (SDL_RenderDrawPoint(m_renderer.get(), point.x(), point.y()) != 0) { + throw std::runtime_error(SDL_GetError()); + } +} + +void Window::Backend::drawPoints(const std::vector &points) +{ + std::vector sdlPoints(points.size()); + + for (unsigned i = 0; i < points.size(); ++i) { + sdlPoints[i] = SDL_Point{points[i].x(), points[i].y()}; + } + + if (SDL_RenderDrawPoints(m_renderer.get(), sdlPoints.data(), sdlPoints.size()) != 0) { + throw std::runtime_error(SDL_GetError()); + } +} + +// TODO: not sure to keep this signature (add fillRect probably) +void Window::Backend::drawRectangle(const Rectangle &rectangle, bool filled, const malikania::Color &fillColor) +{ + SDL_Rect rect{rectangle.x(), rectangle.y(), (int)rectangle.width(), (int)rectangle.height()}; + int error = SDL_RenderDrawRect(m_renderer.get(), &rect); + if (error != 0) { + throw std::runtime_error(SDL_GetError()); + } + if (filled) { + this->setDrawingColor(fillColor); + error = SDL_RenderFillRect(m_renderer.get(), &rect); + if (error != 0) { + throw std::runtime_error(SDL_GetError()); + } + } +} + +// TODO: same as above +void Window::Backend::drawRectangles(const std::vector &rectangles, bool filled, std::vector fillColors) +{ + std::vector sdlRects(rectangles.size()); + + for (unsigned i = 0; i < rectangles.size(); ++i) { + sdlRects[i] = SDL_Rect{rectangles[i].x(), rectangles[i].y(), (int)rectangles[i].width(), (int)rectangles[i].height()}; + } + + if (SDL_RenderDrawRects(m_renderer.get(), sdlRects.data(), sdlRects.size()) != 0) { + throw std::runtime_error(SDL_GetError()); + } + + if (filled) { + if (rectangles.size() != fillColors.size()) { + throw std::runtime_error("Couldn't fill rectangles, rectangles size and fillColors size are not the same"); + } + + int j = 0; + for (Color fillColor : fillColors) { + this->setDrawingColor(fillColor); + + if (SDL_RenderFillRect(m_renderer.get(), &sdlRects[j++]) != 0) { + throw std::runtime_error("Couldn't fill rectangle" + std::string(SDL_GetError())); + } + } + } +} + +// TODO: merge this into only one function and test results. +void Window::Backend::drawText(const std::string &text, Font &font, const Rectangle &rectangle) +{ + SDL_Color color = {0, 0, 0, 255}; + SDL_Surface* message = TTF_RenderUTF8_Blended(font.backend().font(), text.c_str(), color); + SDL_Texture* texture = SDL_CreateTextureFromSurface(m_renderer.get(), message); + SDL_Rect rect{rectangle.x(), rectangle.y(), (int)rectangle.width(), (int)rectangle.height()}; + + SDL_RenderCopy(m_renderer.get(), texture, nullptr, &rect); + SDL_FreeSurface(message); +} + +void Window::Backend::drawText(const std::string &text, Font &font, const Point &point) +{ + SDL_Color color = {0, 0, 0, 0}; + SDL_GetRenderDrawColor(m_renderer.get(), &color.r, &color.g, &color.b, &color.a); + SDL_Surface* message = TTF_RenderUTF8_Blended(font.backend().font(), text.c_str(), color); + SDL_Texture* textTexture = SDL_CreateTextureFromSurface(m_renderer.get(), message); + SDL_Rect rect{point.x(), point.y(), message->w, message->h}; + + SDL_RenderCopy(m_renderer.get(), textTexture, nullptr, &rect); + SDL_FreeSurface(message); +} + +void Window::Backend::close() +{ +} + +} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/WindowBackend.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/backend/sdl/WindowBackend.h Tue Mar 22 22:35:33 2016 +0100 @@ -0,0 +1,76 @@ +/* + * WindowSdl.h -- window object (SDL2 implementation) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MALIKANIA_WINDOW_SDL_H +#define MALIKANIA_WINDOW_SDL_H + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace malikania { + +class Window; + +class Window::Backend { +private: + using WindowHandle = std::unique_ptr; + using RendererHandle = std::unique_ptr; + + WindowHandle m_window; + RendererHandle m_renderer; + +public: + Backend(Window &self, unsigned width, unsigned height); + + void close(); + void processEvents(Window &window); + void clear(); + void update(); + void present(); + Size resolution(); + + // Drawing functions + void setDrawingColor(const Color &color); + void drawLine(const Line &line); + void drawLines(const std::vector &points); + void drawPoint(const Point &point); + void drawPoints(const std::vector &points); + void drawRectangle(const Rectangle &rectangle, bool filled, const Color &fillColor); + void drawRectangles(const std::vector &rectangles, bool filled, std::vector fillColors); + void drawText(const std::string &text, Font &font, const Rectangle &rectangle); + void drawText(const std::string &text, Font &font, const Point &point); + + inline SDL_Renderer *renderer() noexcept + { + return m_renderer.get(); + } +}; + +} // !malikania + +#endif // !MALIKANIA_WINDOW_SDL_H diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/WindowSdl.cpp --- a/libclient/malikania/backend/sdl/WindowSdl.cpp Tue Mar 22 21:15:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,240 +0,0 @@ -/* - * WindowSdl.cpp -- window object (SDL2 implementation) - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include - -namespace malikania { - -WindowSdl::WindowSdl(unsigned width, unsigned height) - : m_window(nullptr, nullptr) - , m_renderer(nullptr, nullptr) -{ - SDL_SetMainReady(); - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); - - m_window = WindowHandle( - SDL_CreateWindow("Malikania", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL), - SDL_DestroyWindow - ); - - if (m_window == nullptr) { - throw std::runtime_error(SDL_GetError()); - } - - // Create renderer - m_renderer = RendererHandle( - SDL_CreateRenderer(m_window.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), - SDL_DestroyRenderer - ); - - if (m_renderer == nullptr) { - throw std::runtime_error(SDL_GetError()); - } - - if (TTF_Init() == -1) { - throw std::runtime_error(TTF_GetError()); - } -} - -void WindowSdl::processEvents(Window &window) -{ - SDL_Event event; - - while (SDL_PollEvent(&event)) { - switch (event.type) { - case SDL_KEYUP: - window.onKeyUp(event.key.keysym.sym); - break; - // TODO continue implemanting all event possible - case SDL_KEYDOWN: - window.onKeyDown(event.key.keysym.sym); - break; - case SDL_QUIT: - window.close(); - break; - default: - break; - } - } -} - -void WindowSdl::clear() -{ - SDL_RenderClear(m_renderer.get()); -} - -void WindowSdl::update() -{ - // TO AUDIT -#if 0 - for (Refresh function : m_refreshList) { - function(); - } -#endif -} - -void WindowSdl::present() -{ - SDL_RenderPresent(m_renderer.get()); -} - -Size WindowSdl::resolution() -{ - SDL_DisplayMode current; - int width = 0; - int height = 0; - for (int i = 0; i < SDL_GetNumVideoDisplays(); i++) { - int error = SDL_GetCurrentDisplayMode(i, ¤t); - if (error == 0) { - // Get the last one - // TODO test with only one display mode, but we have to test with more than that - width = current.w; - height = current.h; - } else { - throw std::runtime_error("Could not get display mode for video display" + std::string(SDL_GetError())); - } - } - - return Size((unsigned)width, (unsigned)height); -} - -void WindowSdl::setDrawingColor(const Color &color) -{ - int error = SDL_SetRenderDrawColor(m_renderer.get(), color.red(), color.green(), color.blue(), color.alpha()); - if (error != 0) { - throw std::runtime_error("Couldn't set drawing color" + std::string(SDL_GetError())); - } -} - -void WindowSdl::drawLine(const Line &line) -{ - if (SDL_RenderDrawLine(m_renderer.get(), line.x1(), line.y1(), line.x2(), line.y2()) != 0) { - throw std::runtime_error(SDL_GetError()); - } -} - -void WindowSdl::drawLines(const std::vector &points) -{ - std::vector sdlPoints(points.size()); - - for (unsigned i = 0; i < points.size(); ++i) { - sdlPoints[i] = SDL_Point{points[i].x(), points[i].y()}; - } - - if (SDL_RenderDrawLines(m_renderer.get(), sdlPoints.data(), sdlPoints.size()) != 0) { - throw std::runtime_error(SDL_GetError()); - } -} - -void WindowSdl::drawPoint(const Point &point) -{ - if (SDL_RenderDrawPoint(m_renderer.get(), point.x(), point.y()) != 0) { - throw std::runtime_error(SDL_GetError()); - } -} - -void WindowSdl::drawPoints(const std::vector &points) -{ - std::vector sdlPoints(points.size()); - - for (unsigned i = 0; i < points.size(); ++i) { - sdlPoints[i] = SDL_Point{points[i].x(), points[i].y()}; - } - - if (SDL_RenderDrawPoints(m_renderer.get(), sdlPoints.data(), sdlPoints.size()) != 0) { - throw std::runtime_error(SDL_GetError()); - } -} - -// TODO: not sure to keep this signature (add fillRect probably) -void WindowSdl::drawRectangle(const Rectangle &rectangle, bool filled, const malikania::Color &fillColor) -{ - SDL_Rect rect{rectangle.x(), rectangle.y(), (int)rectangle.width(), (int)rectangle.height()}; - int error = SDL_RenderDrawRect(m_renderer.get(), &rect); - if (error != 0) { - throw std::runtime_error(SDL_GetError()); - } - if (filled) { - this->setDrawingColor(fillColor); - error = SDL_RenderFillRect(m_renderer.get(), &rect); - if (error != 0) { - throw std::runtime_error(SDL_GetError()); - } - } -} - -// TODO: same as above -void WindowSdl::drawRectangles(const std::vector &rectangles, bool filled, std::vector fillColors) -{ - std::vector sdlRects(rectangles.size()); - - for (unsigned i = 0; i < rectangles.size(); ++i) { - sdlRects[i] = SDL_Rect{rectangles[i].x(), rectangles[i].y(), (int)rectangles[i].width(), (int)rectangles[i].height()}; - } - - if (SDL_RenderDrawRects(m_renderer.get(), sdlRects.data(), sdlRects.size()) != 0) { - throw std::runtime_error(SDL_GetError()); - } - - if (filled) { - if (rectangles.size() != fillColors.size()) { - throw std::runtime_error("Couldn't fill rectangles, rectangles size and fillColors size are not the same"); - } - - int j = 0; - for (Color fillColor : fillColors) { - this->setDrawingColor(fillColor); - - if (SDL_RenderFillRect(m_renderer.get(), &sdlRects[j++]) != 0) { - throw std::runtime_error("Couldn't fill rectangle" + std::string(SDL_GetError())); - } - } - } -} - -// TODO: merge this into only one function and test results. -void WindowSdl::drawText(const std::string &text, Font &font, const Rectangle &rectangle) -{ - SDL_Color color = {0, 0, 0, 255}; - SDL_Surface* message = TTF_RenderUTF8_Blended(font.backend().font(), text.c_str(), color); - SDL_Texture* texture = SDL_CreateTextureFromSurface(m_renderer.get(), message); - SDL_Rect rect{rectangle.x(), rectangle.y(), (int)rectangle.width(), (int)rectangle.height()}; - - SDL_RenderCopy(m_renderer.get(), texture, nullptr, &rect); - SDL_FreeSurface(message); -} - -void WindowSdl::drawText(const std::string &text, Font &font, const Point &point) -{ - SDL_Color color = {0, 0, 0, 0}; - SDL_GetRenderDrawColor(m_renderer.get(), &color.r, &color.g, &color.b, &color.a); - SDL_Surface* message = TTF_RenderUTF8_Blended(font.backend().font(), text.c_str(), color); - SDL_Texture* textTexture = SDL_CreateTextureFromSurface(m_renderer.get(), message); - SDL_Rect rect{point.x(), point.y(), message->w, message->h}; - - SDL_RenderCopy(m_renderer.get(), textTexture, nullptr, &rect); - SDL_FreeSurface(message); -} - -void WindowSdl::close() -{ -} - -} // !malikania diff -r 6bc25027c198 -r 2418900a1cc5 libclient/malikania/backend/sdl/WindowSdl.h --- a/libclient/malikania/backend/sdl/WindowSdl.h Tue Mar 22 21:15:41 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* - * WindowSdl.h -- window object (SDL2 implementation) - * - * Copyright (c) 2013-2016 Malikania Authors - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _MALIKANIA_WINDOW_SDL_H_ -#define _MALIKANIA_WINDOW_SDL_H_ - -#include -#include - -#include - -#include -#include -#include -#include -#include -#include - -namespace malikania { - -class Window; - -class WindowSdl { -private: - using WindowHandle = std::unique_ptr; - using RendererHandle = std::unique_ptr; - - WindowHandle m_window; - RendererHandle m_renderer; - -public: - WindowSdl(unsigned width, unsigned height); - - void close(); - void processEvents(Window &window); - void clear(); - void update(); - void present(); - Size resolution(); - - // Drawing functions - void setDrawingColor(const Color &color); - void drawLine(const Line &line); - void drawLines(const std::vector &points); - void drawPoint(const Point &point); - void drawPoints(const std::vector &points); - void drawRectangle(const Rectangle &rectangle, bool filled, const Color &fillColor); - void drawRectangles(const std::vector &rectangles, bool filled, std::vector fillColors); - void drawText(const std::string &text, Font &font, const Rectangle &rectangle); - void drawText(const std::string &text, Font &font, const Point &point); - - inline SDL_Renderer *renderer() noexcept - { - return m_renderer.get(); - } -}; - -/* - * "Export" the backend. - */ -using BackendWindow = WindowSdl; - -} // !malikania - -#endif // !_MALIKANIA_WINDOW_SDL_H_