├── .codedocs ├── .gitignore ├── .gitmodules ├── .travis.yml ├── CMakeLists.txt ├── CONTRIBUTORS.md ├── LICENSE ├── README.md ├── TODO ├── doc └── Doxyfile ├── include └── gk │ ├── core.hpp │ ├── core │ ├── ApplicationState.hpp │ ├── ApplicationStateStack.hpp │ ├── ArgumentParser.hpp │ ├── Box.hpp │ ├── Config.hpp │ ├── CoreApplication.hpp │ ├── Debug.hpp │ ├── EventHandler.hpp │ ├── EventListenerList.hpp │ ├── Exception.hpp │ ├── Filesystem.hpp │ ├── GameClock.hpp │ ├── ISerializable.hpp │ ├── IntTypes.hpp │ ├── LogStream.hpp │ ├── Logger.hpp │ ├── LoggerHandler.hpp │ ├── LoggerUtils.hpp │ ├── Mouse.hpp │ ├── Rect.hpp │ ├── SDLHeaders.hpp │ ├── SDLLoader.hpp │ ├── Timer.hpp │ ├── Utils.hpp │ ├── Vector2.hpp │ ├── Vector3.hpp │ ├── Vector4.hpp │ ├── Window.hpp │ ├── XMLFile.hpp │ └── input │ │ ├── GamePad.hpp │ │ ├── InputHandler.hpp │ │ └── KeyboardHandler.hpp │ ├── gl │ ├── Camera.hpp │ ├── Drawable.hpp │ ├── GLCheck.hpp │ ├── OpenGL.hpp │ ├── RenderStates.hpp │ ├── RenderTarget.hpp │ ├── Shader.hpp │ ├── Texture.hpp │ ├── Transform.hpp │ ├── Transformable.hpp │ ├── Vertex.hpp │ ├── VertexArray.hpp │ ├── VertexBuffer.hpp │ ├── VertexBufferLayout.hpp │ └── View.hpp │ ├── graphics.hpp │ ├── graphics │ ├── BoxShape.hpp │ ├── Color.hpp │ ├── Image.hpp │ ├── RectangleShape.hpp │ ├── Sprite.hpp │ ├── SpriteAnimation.hpp │ ├── TiledImage.hpp │ ├── Tilemap.hpp │ ├── Tileset.hpp │ └── tilemap │ │ ├── TilemapAnimator.hpp │ │ └── TilemapRenderer.hpp │ ├── math │ └── Math.hpp │ ├── resource.hpp │ ├── resource │ ├── IResourceLoader.hpp │ ├── ResourceHandler.hpp │ ├── TextureLoader.hpp │ ├── TilemapLoader.hpp │ └── TilesetLoader.hpp │ ├── scene.hpp │ ├── scene │ ├── CollisionHelper.hpp │ ├── Scene.hpp │ ├── SceneObject.hpp │ ├── SceneObjectList.hpp │ ├── behaviour │ │ ├── Behaviour.hpp │ │ └── EasyBehaviour.hpp │ ├── component │ │ ├── BehaviourComponent.hpp │ │ ├── CollisionComponent.hpp │ │ ├── HealthComponent.hpp │ │ ├── HitboxComponent.hpp │ │ ├── LifetimeComponent.hpp │ │ ├── MovementComponent.hpp │ │ └── PositionComponent.hpp │ ├── controller │ │ ├── AbstractController.hpp │ │ ├── BehaviourController.hpp │ │ ├── EasyController.hpp │ │ ├── LifetimeController.hpp │ │ └── MovementController.hpp │ ├── movement │ │ ├── EasyMovement.hpp │ │ ├── GamePadMovement.hpp │ │ └── Movement.hpp │ └── view │ │ ├── AbstractView.hpp │ │ ├── EasyView.hpp │ │ ├── HitboxView.hpp │ │ └── SpriteView.hpp │ └── utils │ └── NonCopyable.hpp ├── misc ├── header_cpp.txt └── update_headers.sh ├── source ├── CMakeLists.txt ├── core │ ├── ApplicationStateStack.cpp │ ├── ArgumentParser.cpp │ ├── CoreApplication.cpp │ ├── Filesystem.cpp │ ├── GameClock.cpp │ ├── ISerializable.cpp │ ├── Logger.cpp │ ├── LoggerHandler.cpp │ ├── LoggerUtils.cpp │ ├── Mouse.cpp │ ├── SDLLoader.cpp │ ├── Timer.cpp │ ├── Utils.cpp │ ├── Window.cpp │ ├── XMLFile.cpp │ └── input │ │ ├── GamePad.cpp │ │ ├── InputHandler.cpp │ │ └── KeyboardHandler.cpp ├── gl │ ├── Camera.cpp │ ├── GLCheck.cpp │ ├── RenderTarget.cpp │ ├── Shader.cpp │ ├── Texture.cpp │ ├── Transform.cpp │ ├── Transformable.cpp │ ├── VertexArray.cpp │ ├── VertexBuffer.cpp │ ├── VertexBufferLayout.cpp │ └── View.cpp ├── graphics │ ├── BoxShape.cpp │ ├── Color.cpp │ ├── Image.cpp │ ├── RectangleShape.cpp │ ├── Sprite.cpp │ ├── SpriteAnimation.cpp │ ├── TiledImage.cpp │ ├── Tilemap.cpp │ ├── Tileset.cpp │ └── tilemap │ │ ├── TilemapAnimator.cpp │ │ └── TilemapRenderer.cpp ├── resource │ ├── ResourceHandler.cpp │ ├── TextureLoader.cpp │ ├── TilemapLoader.cpp │ └── TilesetLoader.cpp └── scene │ ├── CollisionHelper.cpp │ ├── Scene.cpp │ ├── controller │ ├── BehaviourController.cpp │ ├── LifetimeController.cpp │ └── MovementController.cpp │ ├── movement │ └── GamePadMovement.cpp │ └── view │ ├── HitboxView.cpp │ └── SpriteView.cpp └── tests ├── ColorTests.hpp ├── RectTests.hpp ├── TransformTests.hpp └── Vector2Tests.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Temporary editor files 2 | \#*# 3 | .#* 4 | *~ 5 | .ycm_extra_conf.py 6 | 7 | # Compiled files 8 | *.o 9 | *.a 10 | *.d 11 | *.so 12 | a.out 13 | 14 | # Debug stuff 15 | vgcore.* 16 | .gdb_history 17 | .valgrind_suppressions 18 | 19 | # CMake temporary files 20 | CMakeFiles 21 | CMakeCache.txt 22 | CTestTestfile.cmake 23 | cmake_install.cmake 24 | compile_commands.json 25 | install_manifest.txt 26 | Makefile 27 | 28 | # CMake temporary files on Windows 29 | *.sln 30 | *.vcxproj 31 | *.vcxproj.filters 32 | *.vcxproj.user 33 | gamekit.dir 34 | Win32 35 | Debug 36 | Release 37 | RelWithDebInfo 38 | 39 | # Visual Studio 40 | .vs 41 | 42 | # Doxygen generated files 43 | doc/html 44 | doc/latex 45 | 46 | # Binaries 47 | build 48 | examples/sample/sample 49 | *.exe 50 | 51 | # Misc 52 | *.zip 53 | 54 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "external/tinyxml2"] 2 | path = external/tinyxml2 3 | url = https://github.com/Unarelith/tinyxml2 4 | [submodule "external/glm"] 5 | path = external/glm 6 | url = https://github.com/g-truc/glm 7 | [submodule "external/glew"] 8 | path = external/glew 9 | url = https://github.com/Perlmint/glew-cmake 10 | [submodule "external/SDL2"] 11 | path = external/SDL2 12 | url = https://github.com/libsdl-org/SDL 13 | [submodule "external/SDL2_image"] 14 | path = external/SDL2_image 15 | url = https://github.com/libsdl-org/SDL_image.git 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: linux 2 | dist: trusty 3 | language: cpp 4 | 5 | compiler: 6 | - clang 7 | 8 | addons: 9 | apt: 10 | sources: 11 | - sourceline: 'ppa:ubuntu-toolchain-r/test' 12 | packages: 13 | - libopenal-dev 14 | - libjpeg-dev 15 | - libudev-dev 16 | - libxrandr-dev 17 | - libfreetype6-dev 18 | - libvorbis-dev 19 | - libflac-dev 20 | - libegl1-mesa-dev 21 | - libgles2-mesa-dev 22 | - g++-8 23 | - cmake 24 | - cxxtest 25 | 26 | script: 27 | - mkdir build 28 | - cd build 29 | - cmake -DGK_BUILD_TESTS:BOOL=ON .. 30 | - make -j8 31 | - ./gamekit_tests 32 | 33 | notifications: 34 | email: false 35 | 36 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | The following people have contributed code to this project and hold the copyright on some portions (see the commit history for details): 2 | * Pedro Gimeno Fortea (pgimeno) \ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GameKit 2 | 3 | [![Build Status](https://travis-ci.com/Unarelith/GameKit.svg?branch=master)](https://travis-ci.com/Unarelith/GameKit) 4 | [![Documentation](https://codedocs.xyz/Unarelith/GameKit.svg)](https://codedocs.xyz/Unarelith/GameKit/) 5 | [![License](https://img.shields.io/badge/license-LGPLv2.1%2B-blue.svg)](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html) 6 | 7 | ## Description 8 | 9 | GameKit is a library I made to avoid duplicating code in my projets. 10 | 11 | It's a small game engine built on top of SDL2 and OpenGL. 12 | 13 | ## Documentation 14 | 15 | - [API Documentation](https://codedocs.xyz/Unarelith/GameKit) 16 | 17 | ## Linux packages 18 | 19 | - **ArchLinux:** `gamekit-git` (AUR) 20 | 21 | ## How to compile 22 | 23 | - Dependencies: 24 | - A compiler with C++17 support 25 | - `git` and [CMake](http://www.cmake.org/download/) 26 | - OpenGL >= 2.1 27 | - _Linux users: Check your distribution repositories for packages._ 28 | - Run `cmake -B build . && cmake --build build -j8 && sudo cmake --install build` 29 | - Or `mkdir build && cd build && cmake .. && make -j8 && sudo make install` (for old CMake versions) 30 | 31 | ## License 32 | 33 | The code is licensed under LGPL v2.1. 34 | 35 | -------------------------------------------------------------------------------- /include/gk/core.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_CORE_HPP_ 28 | #define GK_CORE_HPP_ 29 | 30 | #include "gk/core/ApplicationStateStack.hpp" 31 | #include "gk/core/Box.hpp" 32 | #include "gk/core/CoreApplication.hpp" 33 | #include "gk/core/Exception.hpp" 34 | #include "gk/core/Filesystem.hpp" 35 | #include "gk/core/GameClock.hpp" 36 | #include "gk/core/input/GamePad.hpp" 37 | #include "gk/core/Mouse.hpp" 38 | #include "gk/core/Rect.hpp" 39 | #include "gk/core/Timer.hpp" 40 | #include "gk/core/Vector2.hpp" 41 | #include "gk/core/Vector3.hpp" 42 | #include "gk/core/XMLFile.hpp" 43 | 44 | #endif // GK_CORE_HPP_ 45 | 46 | //////////////////////////////////////////////////////////// 47 | /// \defgroup core 48 | /// \brief Core engine features 49 | /// 50 | //////////////////////////////////////////////////////////// 51 | -------------------------------------------------------------------------------- /include/gk/core/ArgumentParser.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_ARGUMENTPARSER_HPP_ 28 | #define GK_ARGUMENTPARSER_HPP_ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace gk { 35 | 36 | struct Argument { 37 | Argument(const std::string &_shortName, const std::string &_longName, const std::string &_desc) 38 | : shortName(_shortName), longName(_longName), desc(_desc), hasParameter(false) {} 39 | Argument(const std::string &_shortName, const std::string &_longName, const std::string &_desc, const std::string &_paramName) 40 | : shortName(_shortName), longName(_longName), desc(_desc), paramName(_paramName), hasParameter(true) {} 41 | 42 | std::string shortName; 43 | std::string longName; 44 | std::string desc; 45 | std::string paramName; 46 | bool hasParameter = false; 47 | std::string parameter = ""; 48 | bool isFound = false; 49 | }; 50 | 51 | class ArgumentParser { 52 | public: 53 | ArgumentParser(); 54 | ArgumentParser(int argc, char **argv); 55 | 56 | void parse(); 57 | 58 | void printHelp(); 59 | void debug(); 60 | 61 | void addArgument(const std::string &name, const Argument &argument) { m_arguments.emplace(name, argument); } 62 | const Argument &getArgument(const std::string &name) { return m_arguments.at(name); } 63 | 64 | private: 65 | std::vector m_argv; 66 | std::map m_arguments; 67 | }; 68 | 69 | } // namespace gk 70 | 71 | #endif // GK_ARGUMENTPARSER_HPP_ 72 | -------------------------------------------------------------------------------- /include/gk/core/Config.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_CONFIG_HPP_ 28 | #define GK_CONFIG_HPP_ 29 | 30 | //////////////////////////////////////////////////////////// 31 | // Identify the operating system 32 | // see http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system 33 | // and SFML/Config.hpp 34 | //////////////////////////////////////////////////////////// 35 | #if defined(_WIN32) 36 | // Windows 37 | #define GK_SYSTEM_WINDOWS 38 | #elif defined(__APPLE__) && defined(__MACH__) 39 | // Apple platform, see which one it is 40 | #include "TargetConditionals.h" 41 | 42 | #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR 43 | // iOS 44 | #define GK_SYSTEM_IOS 45 | #elif TARGET_OS_MAC 46 | // MacOS 47 | #define GK_SYSTEM_MACOS 48 | #else 49 | // Unsupported Apple system 50 | #error This Apple operating system is not supported by GameKit library 51 | #endif 52 | #elif defined(__unix__) 53 | // UNIX system, see which one it is 54 | #if defined(__ANDROID__) 55 | // Android 56 | #define GK_SYSTEM_ANDROID 57 | #elif defined(__linux__) 58 | // Linux 59 | #define GK_SYSTEM_LINUX 60 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 61 | // FreeBSD 62 | #define GK_SYSTEM_FREEBSD 63 | #elif defined(__OpenBSD__) 64 | // OpenBSD 65 | #define GK_SYSTEM_OPENBSD 66 | #else 67 | // Unsupported UNIX system 68 | #error This UNIX operating system is not supported by GameKit library 69 | #endif 70 | #else 71 | // Unsupported system 72 | #error This operating system is not supported by GameKit library 73 | #endif 74 | 75 | #endif // GK_CONFIG_HPP_ 76 | -------------------------------------------------------------------------------- /include/gk/core/Debug.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_DEBUG_HPP_ 28 | #define GK_DEBUG_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/LoggerHandler.hpp" 33 | 34 | #define _FILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) 35 | 36 | #define gkDebug() (gk::LoggerHandler::getInstance().print(gk::Debug, _FILE, __LINE__)) 37 | #define gkInfo() (gk::LoggerHandler::getInstance().print(gk::Info, _FILE, __LINE__)) 38 | #define gkWarning() (gk::LoggerHandler::getInstance().print(gk::Warning, _FILE, __LINE__)) 39 | #define gkError() (gk::LoggerHandler::getInstance().print(gk::Error, _FILE, __LINE__)) 40 | 41 | #define gkTrace(s) do { gkInfo() << "Function called: " #s; s } while (false); 42 | 43 | #endif // GK_DEBUG_HPP_ 44 | -------------------------------------------------------------------------------- /include/gk/core/EventListenerList.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_EVENTLISTENERLIST_HPP_ 28 | #define GK_EVENTLISTENERLIST_HPP_ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "gk/core/IntTypes.hpp" 35 | 36 | namespace gk { 37 | 38 | class IEventListenerList { 39 | public: 40 | virtual ~IEventListenerList() = default; 41 | 42 | virtual void processEvents() = 0; 43 | 44 | virtual void removeListeners(void *instance) = 0; 45 | }; 46 | 47 | template 48 | class EventListenerList : public IEventListenerList { 49 | public: 50 | void addListener(std::function &&function) { 51 | m_listeners.emplace_back(function, nullptr); 52 | } 53 | 54 | void addListener(std::function &&function, void *instance) { 55 | m_listeners.emplace_back(function, instance); 56 | } 57 | 58 | void removeListeners(void *instance) override { 59 | for (auto it = m_listeners.begin() ; it != m_listeners.end() ;) { 60 | if (it->second == instance) 61 | it = m_listeners.erase(it); 62 | else 63 | ++it; 64 | } 65 | } 66 | 67 | void pushEvent(const T &event) { 68 | m_events.emplace(event); 69 | } 70 | 71 | template 72 | void emplaceEvent(Args &&...args) { 73 | m_events.emplace(std::forward(args)...); 74 | } 75 | 76 | void processEvents() override { 77 | while (!m_events.empty()) { 78 | const T &event = m_events.front(); 79 | for (auto &listener : m_listeners) { 80 | listener.first(event); 81 | } 82 | m_events.pop(); 83 | } 84 | } 85 | 86 | private: 87 | std::queue m_events; 88 | std::vector, void *>> m_listeners; 89 | }; 90 | 91 | } // namespace gk 92 | 93 | #endif // GK_EVENTLISTENERLIST_HPP_ 94 | -------------------------------------------------------------------------------- /include/gk/core/Exception.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_EXCEPTION_HPP_ 28 | #define GK_EXCEPTION_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/core/Debug.hpp" 34 | #include "gk/core/Utils.hpp" 35 | 36 | #define EXCEPTION(...) (gk::Exception(__LINE__, _FILE, __VA_ARGS__)) 37 | 38 | namespace gk { 39 | 40 | class Exception { 41 | public: 42 | template 43 | Exception(u16 line, const std::string &filename, Args... args) noexcept { 44 | m_errorMsg = Logger::textColor(LoggerColor::Red, true); 45 | m_errorMsg += "at " + filename + ":" + std::to_string(line) + ": "; 46 | m_errorMsg += makeString(std::forward(args)...); 47 | m_errorMsg += Logger::textColor(); 48 | } 49 | 50 | virtual ~Exception() = default; 51 | 52 | virtual const char *what() const noexcept { 53 | return m_errorMsg.c_str(); 54 | } 55 | 56 | private: 57 | std::string m_errorMsg; 58 | }; 59 | 60 | } // namespace gk 61 | 62 | #endif // GK_EXCEPTION_HPP_ 63 | -------------------------------------------------------------------------------- /include/gk/core/Filesystem.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_FILESYSTEM_HPP_ 28 | #define GK_FILESYSTEM_HPP_ 29 | 30 | #include 31 | 32 | namespace gk { 33 | 34 | class Filesystem { 35 | public: 36 | static bool fileExists(const std::string &filename); 37 | }; 38 | 39 | } // namespace gk 40 | 41 | #endif // GK_FILESYSTEM_HPP_ 42 | -------------------------------------------------------------------------------- /include/gk/core/GameClock.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_GAMECLOCK_HPP_ 28 | #define GK_GAMECLOCK_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/core/Timer.hpp" 34 | 35 | namespace gk { 36 | 37 | class GameClock { 38 | public: 39 | u32 getTicks(bool realTime = false); 40 | u16 getFpsAverage() const { return m_fps; } 41 | 42 | void updateGame(const std::function &updateFunc); 43 | void drawGame(const std::function &drawFunc); 44 | 45 | void waitForNextFrame(); 46 | 47 | void startFpsTimer() { 48 | m_fpsTimer = getTicks(true); 49 | } 50 | 51 | void setTimestep(u8 timestep) { 52 | std::lock_guard lock(m_mutex); 53 | m_timestep = timestep; 54 | } 55 | 56 | static GameClock &getInstance() { return *s_instance; } 57 | static void setInstance(GameClock &clock) { s_instance = &clock; } 58 | 59 | private: 60 | void measureLastFrameDuration(); 61 | void computeFramesPerSecond(); 62 | 63 | static GameClock *s_instance; 64 | 65 | u32 m_ticks = 0; 66 | u16 m_fps = 0; 67 | 68 | std::mutex m_mutex; 69 | 70 | u32 m_lastFrameDate = 0; 71 | u32 m_lag = 0; 72 | u32 m_timeDropped = 0; 73 | 74 | u8 m_timestep = 6; 75 | u8 m_numUpdates = 0; 76 | 77 | u32 m_fpsTimer = 0; 78 | u16 m_frames = 0; 79 | }; 80 | 81 | } // namespace gk 82 | 83 | #endif // GK_GAMECLOCK_HPP_ 84 | -------------------------------------------------------------------------------- /include/gk/core/ISerializable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_ISERIALIZABLE_HPP_ 28 | #define GK_ISERIALIZABLE_HPP_ 29 | 30 | namespace sf { class Packet; } 31 | 32 | namespace gk { 33 | 34 | class ISerializable { 35 | public: 36 | virtual ~ISerializable() = default; 37 | 38 | virtual void serialize(sf::Packet &) const {}; 39 | virtual void deserialize(sf::Packet &) {}; 40 | }; 41 | 42 | } // namespace gk 43 | 44 | sf::Packet &operator<<(sf::Packet &packet, const gk::ISerializable &s); 45 | sf::Packet &operator>>(sf::Packet &packet, gk::ISerializable &s); 46 | 47 | #endif // GK_ISERIALIZABLE_HPP_ 48 | -------------------------------------------------------------------------------- /include/gk/core/IntTypes.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_INTTYPES_HPP_ 28 | #define GK_INTTYPES_HPP_ 29 | 30 | #include 31 | 32 | using s8 = int8_t; 33 | using s16 = int16_t; 34 | using s32 = int32_t; 35 | using s64 = int64_t; 36 | 37 | using u8 = uint8_t; 38 | using u16 = uint16_t; 39 | using u32 = uint32_t; 40 | using u64 = uint64_t; 41 | 42 | using s8f = int_fast8_t; 43 | using s16f = int_fast16_t; 44 | using s32f = int_fast32_t; 45 | using s64f = int_fast64_t; 46 | 47 | using u8f = uint_fast8_t; 48 | using u16f = uint_fast16_t; 49 | using u32f = uint_fast32_t; 50 | using u64f = uint_fast64_t; 51 | 52 | #endif // GK_INTTYPES_HPP_ 53 | -------------------------------------------------------------------------------- /include/gk/core/LogStream.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_LOGSTREAM_HPP_ 28 | #define GK_LOGSTREAM_HPP_ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace gk::priv { 35 | 36 | class LogStream { 37 | public: 38 | void openFile(const std::string &filename) { 39 | m_file.open(filename, std::ofstream::out | std::ofstream::trunc); 40 | if (!m_file.is_open()) 41 | std::cerr << "Can't open log file: '" << filename << "'" << std::endl; 42 | } 43 | 44 | LogStream &operator<<(const std::string &str) { 45 | std::cout << str; 46 | if (m_file.is_open()) 47 | m_file << str; 48 | return *this; 49 | } 50 | 51 | LogStream &operator<<(int i) { 52 | std::cout << i; 53 | if (m_file.is_open()) 54 | m_file << i; 55 | return *this; 56 | } 57 | 58 | LogStream &operator<<(char c) { 59 | std::cout << c; 60 | if (m_file.is_open()) 61 | m_file << c; 62 | return *this; 63 | } 64 | 65 | LogStream &operator<<(std::ostream &(*f)(std::ostream &)) { 66 | f(std::cout); 67 | if (m_file.is_open()) 68 | f(m_file); 69 | return *this; 70 | } 71 | 72 | private: 73 | std::ofstream m_file; 74 | }; 75 | 76 | } // namespace gk::priv 77 | 78 | #endif // GK_LOGSTREAM_HPP_ 79 | -------------------------------------------------------------------------------- /include/gk/core/LoggerHandler.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_LOGGERHANDLER_HPP_ 28 | #define GK_LOGGERHANDLER_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/core/Logger.hpp" 34 | #include "gk/core/LogStream.hpp" 35 | 36 | namespace gk { 37 | 38 | class LoggerHandler { 39 | using InstanceMap = std::unordered_map; 40 | 41 | public: 42 | LoggerHandler() = default; 43 | 44 | Logger print(LogLevel level, const char *file, int line); 45 | 46 | LogLevel maxLevel() const { return m_maxLevel; } 47 | void setMaxLevel(LogLevel maxLevel) { m_maxLevel = maxLevel; } 48 | 49 | void setName(const std::string &name) { m_name = name; } 50 | 51 | void openLogFile(const std::string &filename) { m_stream.openFile(filename); } 52 | 53 | static LoggerHandler &getInstance(); 54 | static void setInstance(LoggerHandler &instance); 55 | 56 | private: 57 | static InstanceMap s_instanceMap; 58 | 59 | std::string m_name; 60 | 61 | LogLevel m_maxLevel = LogLevel::Debug; 62 | 63 | priv::LogStream m_stream; 64 | }; 65 | 66 | } // namespace gk 67 | 68 | #endif // GK_LOGGERHANDLER_HPP_ 69 | -------------------------------------------------------------------------------- /include/gk/core/Mouse.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_MOUSE_HPP_ 28 | #define GK_MOUSE_HPP_ 29 | 30 | #include "gk/core/Rect.hpp" 31 | #include "gk/core/Window.hpp" 32 | 33 | namespace gk { 34 | 35 | class Mouse { 36 | public: 37 | static void resetToWindowCenter(); 38 | 39 | static void setWindow(Window *window) { s_window = window; } 40 | 41 | static void setCursorVisible(bool isVisible); 42 | static void setCursorGrabbed(bool isGrabbed); 43 | 44 | static Vector2i getPosition(); 45 | 46 | static bool isInRect(const IntRect &rect); 47 | 48 | private: 49 | static Window *s_window; 50 | }; 51 | 52 | } // namespace gk 53 | 54 | #endif // GK_MOUSE_HPP_ 55 | -------------------------------------------------------------------------------- /include/gk/core/SDLHeaders.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_SDLHEADERS_HPP_ 28 | #define GK_SDLHEADERS_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #endif // GK_SDLHEADERS_HPP_ 34 | -------------------------------------------------------------------------------- /include/gk/core/SDLLoader.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_SDLLOADER_HPP_ 28 | #define GK_SDLLOADER_HPP_ 29 | 30 | namespace gk { 31 | 32 | class SDLLoader { 33 | public: 34 | SDLLoader() = default; 35 | SDLLoader(const SDLLoader &) = delete; 36 | SDLLoader(SDLLoader &&) = delete; 37 | ~SDLLoader(); 38 | 39 | SDLLoader& operator=(const SDLLoader &) = delete; 40 | SDLLoader& operator=(SDLLoader &&) = delete; 41 | 42 | void load(); 43 | 44 | private: 45 | bool m_sdlInitialized = false; 46 | bool m_imgInitialized = false; 47 | // bool m_ttfInitialized = false; 48 | // bool m_mixInitialized = false; 49 | }; 50 | 51 | } 52 | 53 | #endif // GK_SDLLOADER_HPP_ 54 | -------------------------------------------------------------------------------- /include/gk/core/Utils.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_UTILS_HPP_ 28 | #define GK_UTILS_HPP_ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | struct SDL_Surface; 35 | 36 | namespace gk { 37 | 38 | template 39 | std::string toString(const T value, const int n = 6) { 40 | std::ostringstream out; 41 | out.precision(n); 42 | out << std::fixed << value; 43 | return out.str(); 44 | } 45 | 46 | bool regexMatch(const std::string &str, const std::string ®ex); 47 | 48 | template 49 | std::string makeString(Args &&...args) { 50 | std::ostringstream stream; 51 | std::vector tmp{0, ((void)(stream << args << " "), 0)...}; 52 | 53 | return stream.str(); 54 | } 55 | 56 | std::string getCurrentTime(const char *format); 57 | 58 | SDL_Surface *flipSDLSurface(SDL_Surface *surface) noexcept; 59 | 60 | } // namespace gk 61 | 62 | #endif // GK_UTILS_HPP_ 63 | -------------------------------------------------------------------------------- /include/gk/core/XMLFile.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_XMLFILE_HPP_ 28 | #define GK_XMLFILE_HPP_ 29 | 30 | #include 31 | 32 | #include 33 | 34 | namespace gk { 35 | 36 | class XMLFile { 37 | public: 38 | XMLFile() = default; 39 | XMLFile(const std::string &filename); 40 | 41 | void load(const std::string &filename); 42 | 43 | tinyxml2::XMLHandle FirstChildElement(const char *element) { return m_doc.FirstChildElement(element); } 44 | 45 | private: 46 | tinyxml2::XMLDocument m_xml; 47 | tinyxml2::XMLHandle m_doc{m_xml}; 48 | }; 49 | 50 | } // namespace gk 51 | 52 | #endif // GK_XMLFILE_HPP_ 53 | -------------------------------------------------------------------------------- /include/gk/core/input/KeyboardHandler.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_KEYBOARDHANDLER_HPP_ 28 | #define GK_KEYBOARDHANDLER_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/core/input/InputHandler.hpp" 34 | #include "gk/core/SDLHeaders.hpp" 35 | 36 | namespace gk { 37 | 38 | class KeyboardHandler : public InputHandler { 39 | public: 40 | void loadKeysFromFile(const std::string &filename); 41 | 42 | bool isKeyPressed(GameKey key); 43 | 44 | SDL_Scancode getScancode(GameKey key) { return SDL_GetScancodeFromKey(m_keys[key]); } 45 | SDL_Keycode getKeycode(GameKey key) { return m_keys[key]; } 46 | std::string getKeyName(GameKey key) { return SDL_GetKeyName(m_keys[key]); } 47 | void setKeycode(GameKey key, SDL_Keycode keycode) { m_keys[key] = keycode; } 48 | 49 | protected: 50 | std::map m_keys; 51 | }; 52 | 53 | } // namespace gk 54 | 55 | #endif // GK_KEYBOARDHANDLER_HPP_ 56 | -------------------------------------------------------------------------------- /include/gk/gl/GLCheck.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_GLCHECK_HPP_ 28 | #define GK_GLCHECK_HPP_ 29 | 30 | #ifdef GK_DEBUG 31 | #define glCheck(expr) do { expr; gk::priv::glCheckError(__FILE__, __LINE__, #expr); } while (false) 32 | #else 33 | #define glCheck(expr) (expr) 34 | #endif 35 | 36 | namespace gk { 37 | namespace priv { 38 | void glCheckError(const char* file, unsigned int line, const char* expression); 39 | } 40 | } 41 | 42 | #endif // GK_GLCHECK_HPP_ 43 | -------------------------------------------------------------------------------- /include/gk/gl/OpenGL.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_OPENGL_HPP_ 28 | #define GK_OPENGL_HPP_ 29 | 30 | #define GL_GLEXT_PROTOTYPES 31 | #define GL3_PROTOTYPES 1 32 | 33 | #ifdef __APPLE__ 34 | #include 35 | #else 36 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__MINGW32__) 37 | #ifdef USE_GLAD 38 | #include 39 | #else 40 | #include 41 | #endif 42 | #else 43 | #include 44 | #endif 45 | #endif 46 | 47 | #endif // GK_OPENGL_HPP_ 48 | -------------------------------------------------------------------------------- /include/gk/gl/RenderStates.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_RENDERSTATES_HPP_ 28 | #define GK_RENDERSTATES_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/IntTypes.hpp" 33 | #include "gk/gl/Transform.hpp" 34 | 35 | namespace gk { 36 | 37 | class Shader; 38 | class Texture; 39 | 40 | struct RenderStates { 41 | Transform projectionMatrix; 42 | Transform viewMatrix; 43 | Transform transform; 44 | 45 | const Texture *texture = nullptr; 46 | const Shader *shader = nullptr; 47 | 48 | static const RenderStates Default; // Defined in RenderTarget.cpp 49 | }; 50 | 51 | } // namespace gk 52 | 53 | #endif // GK_RENDERSTATES_HPP_ 54 | -------------------------------------------------------------------------------- /include/gk/gl/RenderTarget.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_RENDERTARGET_HPP_ 28 | #define GK_RENDERTARGET_HPP_ 29 | 30 | #include "gk/core/Rect.hpp" 31 | #include "gk/gl/OpenGL.hpp" 32 | #include "gk/gl/RenderStates.hpp" 33 | #include "gk/gl/View.hpp" 34 | 35 | namespace gk { 36 | 37 | class Drawable; 38 | class VertexBuffer; 39 | 40 | class RenderTarget { 41 | public: 42 | virtual ~RenderTarget() = default; 43 | 44 | void draw(const Drawable &drawable, const RenderStates &states = RenderStates::Default); 45 | void draw(const VertexBuffer &vertexBuffer, GLenum mode, GLint firstVertex, GLsizei vertexCount, const RenderStates &states = RenderStates::Default); 46 | void drawElements(const VertexBuffer &vertexBuffer, GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, const RenderStates &states = RenderStates::Default); 47 | 48 | void drawArrays(GLenum mode, GLint firstVertex, GLsizei vertexCount); 49 | 50 | void beginDrawing(const RenderStates &states); 51 | 52 | virtual Vector2u getSize() const = 0; 53 | 54 | virtual const View &getDefaultView() const = 0; 55 | 56 | View *getView() { return m_view; } 57 | const View *getView() const { return m_view; } 58 | // FIXME: const_cast shouldn't be used here but it's required for OpenMiner 59 | void setView(const View &view) { m_view = const_cast(&view); m_viewChanged = true; } 60 | void disableView() { m_view = nullptr; } 61 | 62 | private: 63 | IntRect getViewport(const View &view) const; 64 | 65 | void applyCurrentView(const RenderStates &states); 66 | 67 | bool m_viewChanged = false; 68 | View *m_view = nullptr; 69 | 70 | IntRect m_previousViewport; 71 | }; 72 | 73 | } // namespace gk 74 | 75 | #endif // GK_RENDERTARGET_HPP_ 76 | -------------------------------------------------------------------------------- /include/gk/gl/Transform.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TRANSFORM_HPP_ 28 | #define GK_TRANSFORM_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/Box.hpp" 33 | 34 | namespace gk { 35 | 36 | class Transform { 37 | public: 38 | Transform() = default; 39 | Transform(const glm::mat4 &matrix) : m_matrix(matrix) {} 40 | 41 | Transform& combine(const Transform& transform); 42 | 43 | Transform& translate(float x, float y, float z = 0); 44 | Transform& translate(const Vector3f& offset) { return translate(offset.x, offset.y, offset.z); } 45 | 46 | // Intrinsic rotation (right multiplication) 47 | Transform& rotate(float angle) { return rotate(angle, {0, 0, 1}); } 48 | Transform& rotate(float angle, const Vector3f& axis); 49 | Transform& rotateX(float angle); 50 | Transform& rotateY(float angle); 51 | Transform& rotateZ(float angle); 52 | // Extrinsic rotation (left multiplication) 53 | Transform& lrotateX(float angle); 54 | Transform& lrotateY(float angle); 55 | Transform& lrotateZ(float angle); 56 | 57 | Transform& scale(float scaleX, float scaleY, float scaleZ = 1); 58 | Transform& scale(const Vector3f& factors) { return scale(factors.x, factors.y, factors.z); } 59 | 60 | const float* getRawMatrix() const { return glm::value_ptr(m_matrix); } 61 | const glm::mat4 &getMatrix() const { return m_matrix; } 62 | glm::mat4 &getMatrix() { return m_matrix; } 63 | 64 | // Transform getInverse() const { return glm::inverse(m_matrix); } 65 | // Transform getTranspose() const { return glm::transpose(m_matrix); } 66 | 67 | static const Transform Identity; 68 | 69 | private: 70 | glm::mat4 m_matrix{1}; 71 | }; 72 | 73 | Transform operator*(const Transform& left, const Transform& right); 74 | Transform& operator*=(Transform& left, const Transform& right); 75 | 76 | } // namespace gk 77 | 78 | #endif // GK_TRANSFORM_HPP_ 79 | -------------------------------------------------------------------------------- /include/gk/gl/Vertex.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_VERTEX_HPP_ 28 | #define GK_VERTEX_HPP_ 29 | 30 | #include "OpenGL.hpp" 31 | 32 | namespace gk { 33 | 34 | struct Vertex { 35 | GLfloat coord3d[4] = {0, 0, 0, 1}; 36 | GLfloat texCoord[2] = {-1, -1}; 37 | GLfloat color[4] = {0, 0, 0, 1}; 38 | }; 39 | 40 | } // namespace gk 41 | 42 | #endif // GK_VERTEX_HPP_ 43 | -------------------------------------------------------------------------------- /include/gk/gl/VertexArray.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_VERTEXARRAY_HPP_ 28 | #define GK_VERTEXARRAY_HPP_ 29 | 30 | #include "gk/gl/OpenGL.hpp" 31 | 32 | namespace gk { 33 | 34 | class VertexArray { 35 | public: 36 | VertexArray(); 37 | VertexArray(VertexArray &&); 38 | ~VertexArray() noexcept; 39 | 40 | VertexArray &operator=(VertexArray &&); 41 | 42 | static void bind(const VertexArray *vertexArray); 43 | 44 | private: 45 | GLuint m_id = 0; 46 | }; 47 | 48 | } // namespace gk 49 | 50 | #endif // GK_VERTEXARRAY_HPP_ 51 | -------------------------------------------------------------------------------- /include/gk/gl/VertexBuffer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_VERTEXBUFFER_HPP_ 28 | #define GK_VERTEXBUFFER_HPP_ 29 | 30 | #include "gk/gl/VertexBufferLayout.hpp" 31 | #include "gk/utils/NonCopyable.hpp" 32 | 33 | namespace gk { 34 | 35 | class VertexBuffer : public NonCopyable { 36 | public: 37 | VertexBuffer(); 38 | VertexBuffer(VertexBuffer &&); 39 | ~VertexBuffer() noexcept; 40 | 41 | VertexBuffer &operator=(VertexBuffer &&); 42 | 43 | void setData(GLsizeiptr size, const GLvoid *data, GLenum usage) const; 44 | void updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data) const; 45 | 46 | static void bind(const VertexBuffer *vertexBuffer); 47 | 48 | VertexBufferLayout &layout() { return m_layout; } 49 | const VertexBufferLayout &layout() const { return m_layout; } 50 | 51 | private: 52 | GLuint m_id = 0; 53 | 54 | VertexBufferLayout m_layout; 55 | }; 56 | 57 | } // namespace gk 58 | 59 | #endif // GK_VERTEXBUFFER_HPP_ 60 | -------------------------------------------------------------------------------- /include/gk/gl/VertexBufferLayout.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_VERTEXBUFFERLAYOUT_HPP_ 28 | #define GK_VERTEXBUFFERLAYOUT_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/core/IntTypes.hpp" 34 | #include "gk/gl/OpenGL.hpp" 35 | 36 | namespace gk { 37 | 38 | struct VertexAttributeDef { 39 | VertexAttributeDef(u16 _id, const std::string &_name, GLint _size, GLenum _type, 40 | GLboolean _normalized, GLsizei _stride, const void *_offset) 41 | : id(_id), name(_name), size(_size), type(_type), 42 | normalized(_normalized), stride(_stride), offset(_offset) {} 43 | 44 | u16 id; 45 | std::string name; 46 | GLint size; 47 | GLenum type; 48 | GLboolean normalized; 49 | GLsizei stride; 50 | const void *offset; 51 | }; 52 | 53 | class VertexBufferLayout { 54 | using AttributeVector = std::vector; 55 | 56 | using iterator = AttributeVector::iterator; 57 | using const_iterator = AttributeVector::const_iterator; 58 | 59 | public: 60 | template 61 | void addAttribute(Args &&...args) { 62 | m_attributes.emplace_back(std::forward(args)...); 63 | } 64 | 65 | void setupDefaultLayout(); 66 | 67 | void enableLayout() const; 68 | void disableLayout() const; 69 | 70 | iterator begin() { return m_attributes.begin(); } 71 | iterator end() { return m_attributes.end(); } 72 | const_iterator begin() const { return m_attributes.begin(); } 73 | const_iterator end() const { return m_attributes.end(); } 74 | 75 | private: 76 | AttributeVector m_attributes; 77 | }; 78 | 79 | } // namespace gk 80 | 81 | #endif // GK_VERTEXBUFFERLAYOUT_HPP_ 82 | -------------------------------------------------------------------------------- /include/gk/graphics.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_GRAPHICS_HPP_ 28 | #define GK_GRAPHICS_HPP_ 29 | 30 | #include "gk/gl/Camera.hpp" 31 | #include "gk/gl/GLCheck.hpp" 32 | #include "gk/gl/IDrawable.hpp" 33 | #include "gk/gl/Shader.hpp" 34 | #include "gk/gl/Transformable.hpp" 35 | #include "gk/gl/Vertex.hpp" 36 | #include "gk/gl/VertexBuffer.hpp" 37 | #include "gk/gl/View.hpp" 38 | 39 | #include "gk/graphics/Font.hpp" 40 | #include "gk/graphics/Image.hpp" 41 | #include "gk/graphics/RectangleShape.hpp" 42 | #include "gk/graphics/Sprite.hpp" 43 | #include "gk/graphics/Text.hpp" 44 | #include "gk/graphics/Tilemap.hpp" 45 | 46 | #endif // GK_GRAPHICS_HPP_ 47 | 48 | //////////////////////////////////////////////////////////// 49 | /// \defgroup graphics 50 | /// \brief 2D/3D graphics module (sprite, text, camera, vbo...) 51 | /// 52 | //////////////////////////////////////////////////////////// 53 | -------------------------------------------------------------------------------- /include/gk/graphics/BoxShape.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_BOXSHAPE_HPP_ 28 | #define GK_BOXSHAPE_HPP_ 29 | 30 | #include "gk/gl/Drawable.hpp" 31 | #include "gk/gl/Transformable.hpp" 32 | #include "gk/gl/VertexBuffer.hpp" 33 | #include "gk/graphics/Color.hpp" 34 | 35 | namespace gk { 36 | 37 | class BoxShape : public Drawable, public Transformable { 38 | public: 39 | BoxShape(); 40 | BoxShape(float sizeX, float sizeY, float sizeZ, const Color &color = Color::White) 41 | : m_size(sizeX, sizeY, sizeZ), m_color(color) {} 42 | 43 | const gk::Vector3f &getSize() const { return m_size; } 44 | 45 | void resize(float sizeX, float sizeY, float sizeZ) { m_size.x = sizeX; m_size.y = sizeY; m_size.z = sizeZ; m_isVboInitialized = false; } 46 | 47 | void setColor(const Color &color) { m_color = color; m_isVboInitialized = false; } 48 | 49 | private: 50 | void updateVertexBuffer() const; 51 | 52 | void draw(RenderTarget &target, RenderStates states) const override; 53 | 54 | gk::Vector3f m_size{1, 1, 1}; 55 | 56 | VertexBuffer m_vbo; 57 | 58 | Color m_color = Color::White; 59 | 60 | mutable bool m_isVboInitialized = false; 61 | }; 62 | 63 | } // namespace gk 64 | 65 | #endif // GK_BOXSHAPE_HPP_ 66 | -------------------------------------------------------------------------------- /include/gk/graphics/Image.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_IMAGE_HPP_ 28 | #define GK_IMAGE_HPP_ 29 | 30 | #include "gk/graphics/Color.hpp" 31 | #include "gk/core/Rect.hpp" 32 | #include "gk/gl/Drawable.hpp" 33 | #include "gk/gl/Texture.hpp" 34 | #include "gk/gl/Transformable.hpp" 35 | #include "gk/gl/VertexBuffer.hpp" 36 | 37 | namespace gk { 38 | 39 | class Image : public Drawable, public Transformable { 40 | public: 41 | Image(); 42 | Image(const std::string &textureName); 43 | Image(const Texture &texture); 44 | 45 | void load(const Image &image); 46 | void load(const std::string &textureName); 47 | void load(const Texture &texture); 48 | 49 | const Texture *texture() const { return m_texture; } 50 | void setTexture(const std::string &textureName); 51 | 52 | const FloatRect &clipRect() const { return m_clipRect; } 53 | void setClipRect(float x, float y, u16 width, u16 height); 54 | 55 | const FloatRect &posRect() const { return m_posRect; } 56 | void setPosRect(float x, float y, u16 width, u16 height); 57 | 58 | u16 width() const { return u16(m_width * getScale().x); } 59 | u16 height() const { return u16(m_height * getScale().y); } 60 | 61 | void setColor(const Color &color) { m_color = color; updateVertexBuffer(); } 62 | void setAlphaMod(u8 alpha) { m_color.a = alpha / 255.0f; updateVertexBuffer(); } 63 | void setFlip(bool isFlipped) { m_isFlipped = isFlipped; } 64 | 65 | protected: 66 | void updateVertexBuffer() const; 67 | 68 | void draw(RenderTarget &target, RenderStates states) const override; 69 | 70 | const Texture *m_texture = nullptr; 71 | 72 | VertexBuffer m_vbo; 73 | 74 | private: 75 | u16 m_width = 0; 76 | u16 m_height = 0; 77 | 78 | FloatRect m_clipRect; 79 | FloatRect m_posRect; 80 | 81 | Color m_color; 82 | 83 | bool m_isFlipped = false; 84 | }; 85 | 86 | } // namespace gk 87 | 88 | #endif // GK_IMAGE_HPP_ 89 | -------------------------------------------------------------------------------- /include/gk/graphics/Sprite.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_SPRITE_HPP_ 28 | #define GK_SPRITE_HPP_ 29 | 30 | #include "gk/graphics/Image.hpp" 31 | #include "gk/graphics/SpriteAnimation.hpp" 32 | 33 | namespace gk { 34 | 35 | class Sprite : public Image { 36 | public: 37 | Sprite() : Image() {} 38 | Sprite(const std::string &textureName, u16 frameWidth, u16 frameHeight, bool isAnimated = false); 39 | 40 | void load(const Sprite &sprite); 41 | void load(const std::string &textureName, u16 frameWidth, u16 frameHeight, bool isAnimated = false); 42 | 43 | void updateAnimations(); 44 | 45 | void addAnimation(const SpriteAnimation &animation) { m_animations.emplace_back(animation); } 46 | 47 | u16 currentFrame() const { return m_currentFrame; } 48 | 49 | u16 frameWidth() const { return m_frameWidth; } 50 | u16 frameHeight() const { return m_frameHeight; } 51 | 52 | bool hasAnimations() { return m_animations.size() != 0; } 53 | 54 | SpriteAnimation ¤tAnimation() { return m_animations.at(m_currentAnimation); } 55 | const SpriteAnimation ¤tAnimation() const { return m_animations.at(m_currentAnimation); } 56 | 57 | SpriteAnimation &getAnimation(std::size_t i) { return m_animations.at(i); } 58 | const SpriteAnimation &getAnimation(std::size_t i) const { return m_animations.at(i); } 59 | 60 | void setCurrentFrame(u16 currentFrame); 61 | void setCurrentAnimation(u16 currentAnimation); 62 | 63 | bool isAnimated() const { return m_isAnimated; } 64 | void setAnimated(bool isAnimated) { m_isAnimated = isAnimated; } 65 | 66 | private: 67 | std::vector m_animations; 68 | 69 | u16 m_currentFrame = 0; 70 | u16 m_currentAnimation = 0; 71 | u16 m_previousAnimation = 0; 72 | 73 | u16 m_frameWidth = 0; 74 | u16 m_frameHeight = 0; 75 | 76 | bool m_isAnimated = false; 77 | }; 78 | 79 | } // namespace gk 80 | 81 | #endif // GK_SPRITE_HPP_ 82 | -------------------------------------------------------------------------------- /include/gk/graphics/SpriteAnimation.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_SPRITEANIMATION_HPP_ 28 | #define GK_SPRITEANIMATION_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/Timer.hpp" 33 | 34 | namespace gk { 35 | 36 | class SpriteAnimation { 37 | public: 38 | SpriteAnimation(u16 delay, bool isRepeated = true) 39 | : m_delay(delay), m_isRepeated(isRepeated) {} 40 | 41 | void reset(u16 frameID = 0); 42 | 43 | void start(); 44 | void stop(); 45 | 46 | void play(); 47 | 48 | void addFrame(u16 frameID) { m_frames.emplace_back(frameID); } 49 | u16 getFrame(u16 frameID) const; 50 | u16 currentFrame() const; 51 | u16 displayedFramesAmount() const; 52 | 53 | bool isPlaying() const { return m_timer.isStarted() && !m_isPaused; } 54 | bool isFinished() const { return displayedFramesAmount() >= m_frames.size(); } 55 | 56 | u16 size() const { return (u16)m_frames.size(); } 57 | 58 | u16 delay() const { return m_delay; } 59 | 60 | void setRepeated(bool isRepeated) { m_isRepeated = isRepeated; } 61 | 62 | private: 63 | std::vector m_frames; 64 | 65 | Timer m_timer; 66 | 67 | u16 m_delay = 0; 68 | 69 | bool m_isPaused = false; 70 | bool m_isRepeated = true; 71 | }; 72 | 73 | } // namespace gk 74 | 75 | #endif // GK_SPRITEANIMATION_HPP_ 76 | -------------------------------------------------------------------------------- /include/gk/graphics/TiledImage.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TILEDIMAGE_HPP_ 28 | #define GK_TILEDIMAGE_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/graphics/Image.hpp" 34 | 35 | namespace gk { 36 | 37 | class TiledImage : public Drawable, public Transformable { 38 | public: 39 | TiledImage() = default; 40 | TiledImage(const std::string &textureName); 41 | 42 | void load(const std::string &textureName); 43 | 44 | void setTile(u16 id, float x, float y, u16 width, u16 height, float clipX, float clipY, u16 clipWidth, u16 clipHeight, const gk::Color &color = gk::Color::White); 45 | void setTilePosRect(u16 id, float x, float y, u16 width, u16 height); 46 | void setTileClipRect(u16 id, float x, float y, u16 clipWidth, u16 clipHeight); 47 | void setTileColor(u16 id, const gk::Color &color); 48 | 49 | void setTileCount(u16 tileCount); 50 | 51 | protected: 52 | void draw(gk::RenderTarget &target, gk::RenderStates states) const override; 53 | 54 | private: 55 | std::vector m_tiles; 56 | 57 | std::string m_textureName; 58 | }; 59 | 60 | } // namespace gk 61 | 62 | #endif // GK_TILEDIMAGE_HPP_ 63 | -------------------------------------------------------------------------------- /include/gk/graphics/Tilemap.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TILEMAP_HPP_ 28 | #define GK_TILEMAP_HPP_ 29 | 30 | #include "gk/gl/Transformable.hpp" 31 | #include "gk/graphics/tilemap/TilemapAnimator.hpp" 32 | #include "gk/graphics/tilemap/TilemapRenderer.hpp" 33 | #include "gk/graphics/Tileset.hpp" 34 | 35 | namespace gk { 36 | 37 | class Tilemap : public Drawable, public Transformable { 38 | public: 39 | Tilemap(u16 width, u16 height, Tileset &tileset, const std::vector> &data); 40 | 41 | void reset(); 42 | 43 | void update(); 44 | 45 | void updateTiles(); 46 | 47 | u16 getTile(u16 tileX, u16 tileY, u8 layer = 0); 48 | void setTile(u16 tileX, u16 tileY, u16 id, bool write = true, bool persistent = false); 49 | 50 | bool inTile(float x, float y, u16 tileID); 51 | 52 | u16 width() const { return m_width; } 53 | u16 height() const { return m_height; } 54 | 55 | u8 layerCount() const { return (u8)m_data.size(); } 56 | 57 | Tileset &tileset() { return m_tileset; } 58 | void setTilesetOffset(u16 tilesetOffset) { m_tilesetOffset = tilesetOffset; } 59 | 60 | protected: 61 | void draw(RenderTarget &target, RenderStates states) const override; 62 | 63 | private: 64 | Tileset &m_tileset; 65 | u16 m_tilesetOffset = 0; // FIXME 66 | 67 | u16 m_width = 0; 68 | u16 m_height = 0; 69 | 70 | std::vector> m_baseData; 71 | std::vector> m_data; 72 | 73 | TilemapAnimator m_animator; 74 | TilemapRenderer m_renderer; 75 | }; 76 | 77 | } // namespace gk 78 | 79 | #endif // GK_TILEMAP_HPP_ 80 | -------------------------------------------------------------------------------- /include/gk/graphics/Tileset.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TILESET_HPP_ 28 | #define GK_TILESET_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/IntTypes.hpp" 33 | #include "gk/gl/Texture.hpp" 34 | 35 | namespace gk { 36 | 37 | class Tile { 38 | public: 39 | Tile(u16 type = 0) : m_type(type) {} 40 | 41 | struct AnimationFrame { 42 | u16 tileID; 43 | u16 duration; 44 | }; 45 | 46 | u16 getFrameCount() const { return (u16)m_animation.size(); } 47 | const AnimationFrame &getFrame(u16 id) const { return m_animation.at(id); } 48 | void addAnimationFrame(u16 tileID, u16 duration) { m_animation.emplace_back(AnimationFrame{tileID, duration}); } 49 | 50 | u16 type() const { return m_type; } 51 | 52 | private: 53 | std::vector m_animation; 54 | 55 | u16 m_type = 0; 56 | }; 57 | 58 | class Tileset : public Texture { 59 | public: 60 | Tileset() = default; 61 | Tileset(const Tileset &) = delete; 62 | Tileset(Tileset &&tileset) = default; 63 | Tileset(const std::string &filename, const std::string &configFile); 64 | 65 | void load(const std::string &filename, const std::string &configFile); 66 | 67 | const std::vector &info() const { return m_info; } 68 | 69 | const Tile &getTile(u16 id) const { return m_tiles.at(id); } 70 | void setTile(u16 id, Tile &tile) { m_tiles.at(id) = tile; } 71 | 72 | u16 tileWidth() const { return m_tileWidth; } 73 | u16 tileHeight() const { return m_tileHeight; } 74 | u16 tileCount() const { return (u16)m_tiles.size(); } 75 | 76 | private: 77 | std::vector m_info; 78 | 79 | u16 m_tileWidth; 80 | u16 m_tileHeight; 81 | 82 | std::vector m_tiles; 83 | }; 84 | 85 | } // namespace gk 86 | 87 | #endif // GK_TILESET_HPP_ 88 | -------------------------------------------------------------------------------- /include/gk/graphics/tilemap/TilemapAnimator.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TILEMAPANIMATOR_HPP_ 28 | #define GK_TILEMAPANIMATOR_HPP_ 29 | 30 | #include "gk/core/Timer.hpp" 31 | #include "gk/graphics/Tileset.hpp" 32 | 33 | namespace gk { 34 | 35 | class Tilemap; 36 | 37 | class TilemapAnimator { 38 | public: 39 | void init(Tilemap &map); 40 | 41 | void animateTiles(Tilemap &map); 42 | 43 | private: 44 | struct TileAnimation { 45 | Timer timer; 46 | u16 currentFrame = 0; 47 | }; 48 | 49 | std::vector m_tileAnimations; 50 | }; 51 | 52 | } // namespace gk 53 | 54 | #endif // GK_TILEMAPANIMATOR_HPP_ 55 | -------------------------------------------------------------------------------- /include/gk/graphics/tilemap/TilemapRenderer.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TILEMAPRENDERER_HPP_ 28 | #define GK_TILEMAPRENDERER_HPP_ 29 | 30 | #include "gk/gl/Drawable.hpp" 31 | #include "gk/gl/VertexBuffer.hpp" 32 | #include "gk/graphics/Tileset.hpp" 33 | 34 | namespace gk { 35 | 36 | class Tilemap; 37 | 38 | class TilemapRenderer : public Drawable { 39 | public: 40 | TilemapRenderer(); 41 | 42 | void init(Tilemap *map, u16 mapWidth, u16 mapHeight, u8 mapLayers); 43 | 44 | void updateTile(u8 layer, u16 tileX, u16 tileY, u16 id, Tilemap &map); 45 | 46 | private: 47 | void draw(RenderTarget &target, RenderStates states) const override; 48 | 49 | VertexBuffer m_vbo; 50 | 51 | Tilemap *m_map = nullptr; 52 | }; 53 | 54 | } // namespace gk 55 | 56 | #endif // GK_TILEMAPRENDERER_HPP_ 57 | -------------------------------------------------------------------------------- /include/gk/resource.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_RESOURCE_HPP_ 28 | #define GK_RESOURCE_HPP_ 29 | 30 | #include "gk/resource/IResourceLoader.hpp" 31 | #include "gk/resource/ResourceHandler.hpp" 32 | #include "gk/resource/TextureLoader.hpp" 33 | 34 | #endif // GK_RESOURCE_HPP_ 35 | 36 | //////////////////////////////////////////////////////////// 37 | /// \defgroup resource 38 | /// \brief Resource loading and management 39 | /// 40 | //////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /include/gk/resource/IResourceLoader.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_IRESOURCELOADER_HPP_ 28 | #define GK_IRESOURCELOADER_HPP_ 29 | 30 | #include 31 | 32 | namespace gk { 33 | 34 | class ResourceHandler; 35 | 36 | class IResourceLoader { 37 | public: 38 | virtual ~IResourceLoader() = default; 39 | 40 | virtual void load(const char *xmlFilename, ResourceHandler &handler) = 0; 41 | }; 42 | 43 | } // namespace gk 44 | 45 | #endif // GK_RESOURCELOADER_HPP_ 46 | -------------------------------------------------------------------------------- /include/gk/resource/ResourceHandler.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_RESOURCEHANDLER_HPP_ 28 | #define GK_RESOURCEHANDLER_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/core/XMLFile.hpp" 34 | #include "gk/resource/IResourceLoader.hpp" 35 | #include "gk/core/Exception.hpp" 36 | 37 | namespace gk { 38 | 39 | class ResourceHandler { 40 | public: 41 | template 42 | T &add(const std::string &name, Args &&...args) { 43 | if(has(name)) { 44 | throw EXCEPTION("A resource of type", typeid(T).name(), "already exists with name:", name); 45 | } 46 | 47 | m_resources.emplace(name, std::make_shared(std::forward(args)...)); 48 | 49 | return get(name); 50 | } 51 | 52 | bool has(const std::string &name) { 53 | return m_resources.count(name) == 1; 54 | } 55 | 56 | template 57 | T &get(const std::string &name) { 58 | auto it = m_resources.find(name); 59 | if(it == m_resources.end()) { 60 | throw EXCEPTION("Unable to find resource with name:", name); 61 | } 62 | 63 | return *std::static_pointer_cast(it->second); 64 | } 65 | 66 | void clear() { m_resources.clear(); } 67 | 68 | template 69 | static auto loadConfigFile(const char *xmlFilename) -> typename std::enable_if::value>::type { 70 | ResourceLoader loader; 71 | loader.load(xmlFilename, getInstance()); 72 | } 73 | 74 | static ResourceHandler &getInstance(); 75 | 76 | static void setInstance(ResourceHandler &handler); 77 | 78 | private: 79 | static ResourceHandler *instance; 80 | 81 | std::map> m_resources; 82 | }; 83 | 84 | } // namespace gk 85 | 86 | #endif // GK_RESOURCEHANDLER_HPP_ 87 | -------------------------------------------------------------------------------- /include/gk/resource/TextureLoader.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TEXTURELOADER_HPP_ 28 | #define GK_TEXTURELOADER_HPP_ 29 | 30 | #include "gk/resource/IResourceLoader.hpp" 31 | 32 | namespace gk { 33 | 34 | class TextureLoader : public IResourceLoader { 35 | public: 36 | void load(const char *xmlFilename, ResourceHandler &handler); 37 | }; 38 | 39 | } // namespace gk 40 | 41 | #endif // GK_TEXTURELOADER_HPP_ 42 | -------------------------------------------------------------------------------- /include/gk/resource/TilemapLoader.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TILEMAPLOADER_HPP_ 28 | #define GK_TILEMAPLOADER_HPP_ 29 | 30 | #include "gk/resource/ResourceHandler.hpp" 31 | 32 | namespace gk { 33 | 34 | class Tileset; 35 | 36 | class TilemapLoader : public IResourceLoader { 37 | public: 38 | void load(const char *xmlFilename, ResourceHandler &handler) override; 39 | 40 | private: 41 | void loadMap(const std::string &name, Tileset &tileset, ResourceHandler &handler); 42 | }; 43 | 44 | } // namespace gk 45 | 46 | #endif // GK_TILEMAPLOADER_HPP_ 47 | -------------------------------------------------------------------------------- /include/gk/resource/TilesetLoader.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_TILESETLOADER_HPP_ 28 | #define GK_TILESETLOADER_HPP_ 29 | 30 | #include "gk/resource/IResourceLoader.hpp" 31 | 32 | namespace gk { 33 | 34 | class TilesetLoader : public IResourceLoader { 35 | public: 36 | void load(const char *xmlFilename, ResourceHandler &handler); 37 | }; 38 | 39 | } // namespace gk 40 | 41 | #endif // GK_TILESETLOADER_HPP_ 42 | -------------------------------------------------------------------------------- /include/gk/scene.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_SCENE_HPP_ 28 | #define GK_SCENE_HPP_ 29 | 30 | #include "gk/scene/Scene.hpp" 31 | 32 | // Behaviours 33 | #include "gk/scene/behaviour/EasyBehaviour.hpp" 34 | 35 | // Controllers 36 | #include "gk/scene/controller/BehaviourController.hpp" 37 | #include "gk/scene/controller/LifetimeController.hpp" 38 | #include "gk/scene/controller/MovementController.hpp" 39 | 40 | // Components 41 | #include "gk/scene/component/BehaviourComponent.hpp" 42 | #include "gk/scene/component/CollisionComponent.hpp" 43 | #include "gk/scene/component/HealthComponent.hpp" 44 | #include "gk/scene/component/HitboxComponent.hpp" 45 | #include "gk/scene/component/LifetimeComponent.hpp" 46 | #include "gk/scene/component/MovementComponent.hpp" 47 | #include "gk/scene/component/PositionComponent.hpp" 48 | 49 | // Movements 50 | #include "gk/scene/movement/EasyMovement.hpp" 51 | #include "gk/scene/movement/GamePadMovement.hpp" 52 | 53 | // Views 54 | #include "gk/scene/view/HitboxView.hpp" 55 | #include "gk/scene/view/SpriteView.hpp" 56 | 57 | #endif // GK_SCENE_HPP_ 58 | 59 | /////////////////////////////////////////////////////////// 60 | /// \defgroup scene 61 | /// \brief Game scene and entity management 62 | /// 63 | //////////////////////////////////////////////////////////// 64 | -------------------------------------------------------------------------------- /include/gk/scene/CollisionHelper.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_COLLISIONHELPER_HPP_ 28 | #define GK_COLLISIONHELPER_HPP_ 29 | 30 | #include "gk/scene/SceneObject.hpp" 31 | 32 | namespace gk { 33 | 34 | class CollisionHelper { 35 | public: 36 | virtual ~CollisionHelper() = default; 37 | 38 | void checkCollision(SceneObject &object1, SceneObject &object2); 39 | 40 | virtual bool inCollision(SceneObject &object1, SceneObject &object2); 41 | }; 42 | 43 | } // namespace gk 44 | 45 | #endif // GK_COLLISIONHELPER_HPP_ 46 | -------------------------------------------------------------------------------- /include/gk/scene/SceneObjectList.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_SCENEOBJECTLIST_HPP_ 28 | #define GK_SCENEOBJECTLIST_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/scene/SceneObject.hpp" 34 | 35 | namespace gk { 36 | 37 | class SceneObjectList { 38 | using iterator = std::deque::iterator; 39 | using const_iterator = std::deque::const_iterator; 40 | 41 | public: 42 | SceneObject &addObject(SceneObject &&object) { 43 | m_objects.emplace_back(std::move(object)); 44 | return m_objects.back(); 45 | } 46 | 47 | SceneObject *findByName(const std::string &name) { 48 | auto it = std::find_if(m_objects.begin(), m_objects.end(), [name] (SceneObject &object) { return object.name() == name; }); 49 | if (it == m_objects.end()) 50 | return nullptr; 51 | else 52 | return &*it; 53 | } 54 | 55 | void removeByName(const std::string &name) { 56 | auto it = std::find_if(m_objects.begin(), m_objects.end(), [name] (SceneObject &object) { return object.name() == name; }); 57 | if (it != m_objects.end()) 58 | m_objects.erase(it); 59 | } 60 | 61 | void pop() { m_objects.pop_back(); } 62 | 63 | SceneObject &operator[](size_t n) { return m_objects[n]; } 64 | 65 | void remove(size_t n) { m_objects.erase(m_objects.begin() + n); } 66 | 67 | iterator begin() noexcept { return m_objects.begin(); } 68 | iterator end() noexcept { return m_objects.end(); } 69 | 70 | const_iterator begin() const noexcept { return m_objects.begin(); } 71 | const_iterator end() const noexcept { return m_objects.end(); } 72 | 73 | size_t size() const { return m_objects.size(); } 74 | 75 | private: 76 | std::deque m_objects; 77 | }; 78 | 79 | } // namespace gk 80 | 81 | #endif // GK_SCENEOBJECTLIST_HPP_ 82 | -------------------------------------------------------------------------------- /include/gk/scene/behaviour/Behaviour.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_BEHAVIOUR_HPP_ 28 | #define GK_BEHAVIOUR_HPP_ 29 | 30 | #include "gk/scene/SceneObject.hpp" 31 | 32 | namespace gk { 33 | 34 | class Behaviour { 35 | public: 36 | virtual ~Behaviour() = default; 37 | 38 | virtual void update(SceneObject &object) = 0; 39 | }; 40 | 41 | } // namespace gk 42 | 43 | #endif // GK_BEHAVIOUR_HPP_ 44 | -------------------------------------------------------------------------------- /include/gk/scene/behaviour/EasyBehaviour.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_EASYBEHAVIOUR_HPP_ 28 | #define GK_EASYBEHAVIOUR_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/scene/behaviour/Behaviour.hpp" 33 | 34 | namespace gk { 35 | 36 | class EasyBehaviour : public Behaviour { 37 | public: 38 | EasyBehaviour(const std::function &func) : m_func(func) {} 39 | 40 | void update(SceneObject &object) override { 41 | m_func(object); 42 | } 43 | 44 | private: 45 | std::function m_func; 46 | }; 47 | 48 | } // namespace gk 49 | 50 | #endif // GK_EASYBEHAVIOUR_HPP_ 51 | -------------------------------------------------------------------------------- /include/gk/scene/component/BehaviourComponent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_BEHAVIOURCOMPONENT_HPP_ 28 | #define GK_BEHAVIOURCOMPONENT_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/scene/behaviour/Behaviour.hpp" 33 | 34 | namespace gk { 35 | 36 | class BehaviourComponent { 37 | public: 38 | template 39 | T &addBehaviour(const char *name, Args &&...args) { 40 | T *t = new T(std::forward(args)...); 41 | m_behaviours.emplace(name, t); 42 | return *t; 43 | } 44 | 45 | void update(SceneObject &object) { 46 | for (auto &it : m_behaviours) { 47 | it.second->update(object); 48 | } 49 | } 50 | 51 | private: 52 | std::unordered_map> m_behaviours; 53 | }; 54 | 55 | } // namespace gk 56 | 57 | #endif // GK_BEHAVIOURCOMPONENT_HPP_ 58 | -------------------------------------------------------------------------------- /include/gk/scene/component/CollisionComponent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_COLLISIONCOMPONENT_HPP_ 28 | #define GK_COLLISIONCOMPONENT_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/scene/SceneObject.hpp" 34 | 35 | namespace gk { 36 | 37 | class CollisionComponent { 38 | using CollisionChecker = std::function; 39 | using CollisionAction = std::function; 40 | 41 | public: 42 | void checkCollisions(SceneObject &object) { 43 | for(auto &it : m_checkers) { 44 | it(object); 45 | } 46 | } 47 | 48 | void collisionActions(SceneObject &object1, SceneObject &object2, bool inCollision) { 49 | for(auto &it : m_actions) { 50 | it(object1, object2, inCollision); 51 | } 52 | } 53 | 54 | void addChecker(CollisionChecker checker) { 55 | m_checkers.push_back(checker); 56 | } 57 | 58 | void addAction(CollisionAction action) { 59 | m_actions.push_back(action); 60 | } 61 | 62 | private: 63 | std::vector m_checkers; 64 | 65 | std::vector m_actions; 66 | }; 67 | 68 | } // namespace gk 69 | 70 | #endif // GK_COLLISIONCOMPONENT_HPP_ 71 | -------------------------------------------------------------------------------- /include/gk/scene/component/HealthComponent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_HEALTHCOMPONENT_HPP_ 28 | #define GK_HEALTHCOMPONENT_HPP_ 29 | 30 | #include "gk/core/Timer.hpp" 31 | 32 | namespace gk { 33 | 34 | class HealthComponent { 35 | public: 36 | HealthComponent(const u16 maxLife, const u16 life = 0, const u16 hurtTime = 0) 37 | : m_maxLife(maxLife), m_life(life ? life : maxLife), m_hurtTime(hurtTime) {} 38 | 39 | void setLife(const u16 newLife) { m_life = (newLife > m_maxLife) ? m_maxLife : newLife; } 40 | void addLife(const u16 lifeAdded) { setLife(m_life + lifeAdded); } 41 | void removeLife(const u16 lifeRemoved) { 42 | if (!m_hurtTime || !m_hurtTimer.isStarted() || m_hurtTimer.time() > m_hurtTime) { 43 | (lifeRemoved > m_life) ? setLife(0) : setLife(m_life - lifeRemoved); 44 | 45 | if (m_hurtTime) { 46 | m_hurtTimer.reset(); 47 | m_hurtTimer.start(); 48 | } 49 | } 50 | } 51 | 52 | u16 life() const { return m_life; } 53 | u16 maxLife() const { return m_maxLife; } 54 | 55 | Timer hurtTimer() { return m_hurtTimer; } 56 | 57 | private: 58 | const u16 m_maxLife = 0; 59 | u16 m_life = 0; 60 | 61 | const u16 m_hurtTime = 0; 62 | 63 | Timer m_hurtTimer; 64 | }; 65 | 66 | } // namespace gk 67 | 68 | #endif // GK_HEALTHCOMPONENT_HPP_ 69 | -------------------------------------------------------------------------------- /include/gk/scene/component/HitboxComponent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_HITBOXCOMPONENT_HPP_ 28 | #define GK_HITBOXCOMPONENT_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/Rect.hpp" 33 | #include "gk/core/Exception.hpp" 34 | 35 | namespace gk { 36 | 37 | class HitboxComponent { 38 | public: 39 | HitboxComponent(s8 currentHitboxID = 0) 40 | : m_currentHitboxID(currentHitboxID) {} 41 | 42 | HitboxComponent(s16 x, s16 y, u16 width, u16 height) : HitboxComponent(0) { 43 | addHitbox(x, y, width, height); 44 | } 45 | 46 | void addHitbox(s16 x, s16 y, u16 width, u16 height) { 47 | m_hitboxes.emplace_back(x, y, width, height); 48 | } 49 | 50 | const FloatRect *currentHitbox() const { 51 | if(m_currentHitboxID >= 0 && m_currentHitboxID < (s16)m_hitboxes.size()) { 52 | return &m_hitboxes[m_currentHitboxID]; 53 | } else { 54 | return nullptr; 55 | } 56 | } 57 | 58 | void setCurrentHitbox(u8 id) { 59 | if(id < m_hitboxes.size()) { 60 | m_currentHitboxID = id; 61 | } else { 62 | throw EXCEPTION("Hitbox ID out of range:", (s16)id, "| Array size:", m_hitboxes.size()); 63 | } 64 | } 65 | 66 | void resetCurrentHitbox() { m_currentHitboxID = -1; } 67 | 68 | private: 69 | s8 m_currentHitboxID = -1; 70 | 71 | std::vector m_hitboxes; 72 | }; 73 | 74 | } // namespace gk 75 | 76 | #endif // GK_HITBOXCOMPONENT_HPP_ 77 | -------------------------------------------------------------------------------- /include/gk/scene/component/LifetimeComponent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_LIFETIMECOMPONENT_HPP_ 28 | #define GK_LIFETIMECOMPONENT_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/Timer.hpp" 33 | #include "gk/scene/SceneObject.hpp" 34 | 35 | namespace gk { 36 | 37 | class LifetimeComponent { 38 | using DeathChecker = std::function; 39 | 40 | public: 41 | LifetimeComponent() = default; 42 | LifetimeComponent(u32 lifetime) : m_lifetime(lifetime) { m_timer.start(); } 43 | LifetimeComponent(DeathChecker deathChecker) : m_deathChecker(deathChecker) {} 44 | 45 | void kill() { m_dead = true; } 46 | 47 | bool almostDead() { return m_lifetime != 0 && m_timer.time() > m_lifetime / 4 * 3; } 48 | 49 | bool dead(const SceneObject &object) const { 50 | return m_dead 51 | || (m_lifetime != 0 && m_timer.time() > m_lifetime) 52 | || (m_deathChecker && m_deathChecker(object)); 53 | } 54 | 55 | u32 aliveTime() { return m_timer.time(); } 56 | 57 | bool areClientsNotified() const { return m_areClientsNotified; } 58 | 59 | void setDeathChecker(DeathChecker deathChecker) { m_deathChecker = deathChecker; } 60 | void setClientsNotified(bool areClientsNotified) { m_areClientsNotified = areClientsNotified; } 61 | 62 | private: 63 | Timer m_timer; 64 | 65 | bool m_dead = false; 66 | bool m_areClientsNotified = false; 67 | 68 | u32 m_lifetime = 0; 69 | 70 | DeathChecker m_deathChecker; 71 | }; 72 | 73 | } // namespace gk 74 | 75 | #endif // GK_LIFETIMECOMPONENT_HPP_ 76 | -------------------------------------------------------------------------------- /include/gk/scene/component/MovementComponent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_MOVEMENTCOMPONENT_HPP_ 28 | #define GK_MOVEMENTCOMPONENT_HPP_ 29 | 30 | #include 31 | #include 32 | 33 | #include "gk/core/Vector2.hpp" 34 | #include "gk/scene/movement/Movement.hpp" 35 | 36 | namespace gk { 37 | 38 | class MovementStack { 39 | public: 40 | template 41 | bool push(Args &&...args) { 42 | if(size() > 0 && top() && !top()->isFinished()) { 43 | return false; 44 | } else { 45 | m_movements.emplace(args...); 46 | return true; 47 | } 48 | } 49 | 50 | void pop() { m_movements.pop(); } 51 | 52 | std::unique_ptr &top() { return m_movements.top(); } 53 | 54 | size_t size() const { return m_movements.size(); } 55 | 56 | private: 57 | std::stack> m_movements; 58 | }; 59 | 60 | class MovementComponent { 61 | public: 62 | MovementComponent(Movement *_movement) { 63 | movements.push(_movement); 64 | } 65 | 66 | Vector2f v{0, 0}; 67 | 68 | float speed = 1.0f; 69 | 70 | bool isMoving = false; 71 | bool isDirectionLocked = false; 72 | 73 | Vector2 isBlocked; 74 | 75 | MovementStack movements; 76 | }; 77 | 78 | } // namespace gk 79 | 80 | #endif // GK_MOVEMENTCOMPONENT_HPP_ 81 | -------------------------------------------------------------------------------- /include/gk/scene/component/PositionComponent.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_POSITIONCOMPONENT_HPP_ 28 | #define GK_POSITIONCOMPONENT_HPP_ 29 | 30 | #include "gk/core/Vector2.hpp" 31 | 32 | namespace gk { 33 | 34 | class PositionComponent : public Vector2f { 35 | public: 36 | PositionComponent(float x, float y) : Vector2f(x, y) {} 37 | PositionComponent(const Vector2f &pos) : Vector2f(pos) {} 38 | }; 39 | 40 | } // namespace gk 41 | 42 | #endif // GK_POSITIONCOMPONENT_HPP_ 43 | -------------------------------------------------------------------------------- /include/gk/scene/controller/AbstractController.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_ABSTRACTCONTROLLER_HPP_ 28 | #define GK_ABSTRACTCONTROLLER_HPP_ 29 | 30 | #include "gk/scene/SceneObjectList.hpp" 31 | 32 | namespace gk { 33 | 34 | class AbstractController { 35 | public: 36 | virtual ~AbstractController() = default; 37 | 38 | virtual void reset(SceneObject &) {} 39 | virtual void update(SceneObject &object) = 0; 40 | 41 | virtual void reset(SceneObjectList &objectList) { for(auto &object : objectList) reset(object); } 42 | virtual void update(SceneObjectList &objectList) { for(auto &object : objectList) update(object); } 43 | 44 | virtual bool isGlobal() const { return false; } 45 | }; 46 | 47 | } // namespace gk 48 | 49 | #endif // GK_ABSTRACTCONTROLLER_HPP_ 50 | -------------------------------------------------------------------------------- /include/gk/scene/controller/BehaviourController.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_BEHAVIOURCONTROLLER_HPP_ 28 | #define GK_BEHAVIOURCONTROLLER_HPP_ 29 | 30 | #include "gk/scene/controller/AbstractController.hpp" 31 | 32 | namespace gk { 33 | 34 | class BehaviourController : public AbstractController { 35 | public: 36 | void update(SceneObject &object) override; 37 | }; 38 | 39 | } // namespace gk 40 | 41 | #endif // GK_BEHAVIOURCONTROLLER_HPP_ 42 | -------------------------------------------------------------------------------- /include/gk/scene/controller/EasyController.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_EASYCONTROLLER_HPP_ 28 | #define GK_EASYCONTROLLER_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/scene/controller/AbstractController.hpp" 33 | 34 | namespace gk { 35 | 36 | class EasyController : public AbstractController { 37 | using Function = std::function; 38 | 39 | public: 40 | EasyController(const Function &func) : m_func(func) {} 41 | 42 | void update(SceneObject &object) override { 43 | m_func(object); 44 | } 45 | 46 | private: 47 | Function m_func; 48 | }; 49 | 50 | } // namespace gk 51 | 52 | #endif // GK_EASYCONTROLLER_HPP_ 53 | -------------------------------------------------------------------------------- /include/gk/scene/controller/LifetimeController.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_LIFETIMECONTROLLER_HPP_ 28 | #define GK_LIFETIMECONTROLLER_HPP_ 29 | 30 | #include "gk/scene/controller/AbstractController.hpp" 31 | 32 | namespace gk { 33 | 34 | class LifetimeController : public AbstractController { 35 | public: 36 | void update(SceneObjectList &objectList) override; 37 | void update(SceneObject &) override {} 38 | 39 | bool isGlobal() const override { return true; } 40 | }; 41 | 42 | } // namespace gk 43 | 44 | #endif // GK_LIFETIMECONTROLLER_HPP_ 45 | -------------------------------------------------------------------------------- /include/gk/scene/controller/MovementController.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_MOVEMENTCONTROLLER_HPP_ 28 | #define GK_MOVEMENTCONTROLLER_HPP_ 29 | 30 | #include "gk/scene/controller/AbstractController.hpp" 31 | 32 | namespace gk { 33 | 34 | class MovementController : public AbstractController { 35 | public: 36 | void update(SceneObject &object) override; 37 | }; 38 | 39 | } // namespace gk 40 | 41 | #endif // GK_MOVEMENTCONTROLLER_HPP_ 42 | -------------------------------------------------------------------------------- /include/gk/scene/movement/EasyMovement.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_EASYMOVEMENT_HPP_ 28 | #define GK_EASYMOVEMENT_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/scene/movement/Movement.hpp" 33 | 34 | namespace gk { 35 | 36 | class EasyMovement : public Movement { 37 | public: 38 | EasyMovement(const std::function &func) : m_func(func) {} 39 | 40 | void process(SceneObject &object) override { 41 | m_func(object); 42 | } 43 | 44 | private: 45 | std::function m_func; 46 | }; 47 | 48 | } // namespace gk 49 | 50 | #endif // EASYMOVEMENT_HPP_ 51 | -------------------------------------------------------------------------------- /include/gk/scene/movement/GamePadMovement.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_GAMEPADMOVEMENT_HPP_ 28 | #define GK_GAMEPADMOVEMENT_HPP_ 29 | 30 | #include "gk/core/input/InputHandler.hpp" 31 | #include "gk/scene/movement/Movement.hpp" 32 | 33 | namespace gk { 34 | 35 | class GamePadMovement : public Movement { 36 | public: 37 | void process(SceneObject &object) override; 38 | 39 | void setKeys(GameKey left, GameKey right, GameKey up, GameKey down) { 40 | m_left = left; 41 | m_right = right; 42 | m_up = up; 43 | m_down = down; 44 | } 45 | 46 | private: 47 | GameKey m_left; 48 | GameKey m_right; 49 | GameKey m_up; 50 | GameKey m_down; 51 | }; 52 | 53 | } // namespace gk 54 | 55 | #endif // GK_GAMEPADMOVEMENT_HPP_ 56 | -------------------------------------------------------------------------------- /include/gk/scene/movement/Movement.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_MOVEMENT_HPP_ 28 | #define GK_MOVEMENT_HPP_ 29 | 30 | #include "gk/scene/SceneObject.hpp" 31 | 32 | namespace gk { 33 | 34 | class Movement { 35 | public: 36 | virtual ~Movement() = default; 37 | 38 | bool isFinished() const { return m_isFinished; } 39 | 40 | virtual void process(SceneObject &object) = 0; 41 | 42 | protected: 43 | bool m_isFinished = false; 44 | }; 45 | 46 | } // namespace gk 47 | 48 | #endif // GK_MOVEMENT_HPP_ 49 | -------------------------------------------------------------------------------- /include/gk/scene/view/AbstractView.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_ABSTRACTVIEW_HPP_ 28 | #define GK_ABSTRACTVIEW_HPP_ 29 | 30 | #include "gk/gl/RenderTarget.hpp" 31 | #include "gk/scene/SceneObjectList.hpp" 32 | 33 | namespace gk { 34 | 35 | class AbstractView { 36 | public: 37 | virtual ~AbstractView() = default; 38 | 39 | virtual void draw(const SceneObject &object, RenderTarget &target, RenderStates states) = 0; 40 | 41 | virtual void draw(const SceneObjectList &objectList, RenderTarget &target, RenderStates states) { 42 | for(auto &object : objectList) { 43 | draw(object, target, states); 44 | 45 | if (object.has()) 46 | draw(object.get(), target, states); 47 | } 48 | } 49 | }; 50 | 51 | } // namespace gk 52 | 53 | #endif // GK_ABSTRACTVIEW_HPP_ 54 | -------------------------------------------------------------------------------- /include/gk/scene/view/EasyView.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_EASYVIEW_HPP_ 28 | #define GK_EASYVIEW_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/scene/view/AbstractView.hpp" 33 | 34 | namespace gk { 35 | 36 | class EasyView : public AbstractView { 37 | using Function = std::function; 38 | 39 | public: 40 | EasyView(const Function &func) : m_func(func) {} 41 | 42 | void draw(const SceneObject &object, gk::RenderTarget &target, gk::RenderStates states) override { 43 | m_func(object, target, states); 44 | } 45 | 46 | private: 47 | Function m_func; 48 | }; 49 | 50 | } // namespace gk 51 | 52 | #endif // GK_EASYVIEW_HPP_ 53 | -------------------------------------------------------------------------------- /include/gk/scene/view/HitboxView.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_HITBOXVIEW_HPP_ 28 | #define GK_HITBOXVIEW_HPP_ 29 | 30 | #include "gk/scene/view/AbstractView.hpp" 31 | 32 | namespace gk { 33 | 34 | class HitboxView : public AbstractView { 35 | public: 36 | void draw(const SceneObject &object, RenderTarget &target, RenderStates states); 37 | }; 38 | 39 | } // namespace gk 40 | 41 | #endif // GK_HITBOXVIEW_HPP_ 42 | -------------------------------------------------------------------------------- /include/gk/scene/view/SpriteView.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_SPRITEVIEW_HPP_ 28 | #define GK_SPRITEVIEW_HPP_ 29 | 30 | #include "gk/scene/view/AbstractView.hpp" 31 | 32 | namespace gk { 33 | 34 | class SpriteView : public AbstractView { 35 | public: 36 | void draw(const SceneObject &object, RenderTarget &target, RenderStates states) override; 37 | }; 38 | 39 | } // namespace gk 40 | 41 | #endif // GK_SPRITEVIEW_HPP_ 42 | -------------------------------------------------------------------------------- /include/gk/utils/NonCopyable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef GK_NONCOPYABLE_HPP_ 28 | #define GK_NONCOPYABLE_HPP_ 29 | 30 | namespace gk { 31 | 32 | class NonCopyable { 33 | protected: 34 | NonCopyable() = default; 35 | NonCopyable(const NonCopyable &) = delete; 36 | NonCopyable(NonCopyable &&) = default; 37 | 38 | NonCopyable &operator=(const NonCopyable &) = delete; 39 | NonCopyable &operator=(NonCopyable &&) = default; 40 | }; 41 | 42 | } 43 | 44 | #endif // GK_NONCOPYABLE_HPP_ 45 | -------------------------------------------------------------------------------- /misc/header_cpp.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | -------------------------------------------------------------------------------- /misc/update_headers.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! -f "misc/header_cpp.txt" ]; 4 | then 5 | echo "Failed to find header file. Aborting." 6 | exit 7 | fi 8 | 9 | for file in $(find include source examples -name "*.*pp"); 10 | do 11 | sed -i '1,26d' $file 12 | cat misc/header_cpp.txt $file > $file.new && mv $file.new $file 13 | done 14 | 15 | -------------------------------------------------------------------------------- /source/core/ApplicationStateStack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/ApplicationStateStack.hpp" 28 | 29 | namespace gk { 30 | 31 | ApplicationStateStack *ApplicationStateStack::s_instance = nullptr; 32 | 33 | void ApplicationStateStack::pop() { 34 | m_trash.push(m_states.top()); 35 | m_states.pop(); 36 | } 37 | 38 | void ApplicationStateStack::clearDeletedStates() { 39 | while (!m_trash.empty()) 40 | m_trash.pop(); 41 | } 42 | 43 | } // namespace gk 44 | 45 | -------------------------------------------------------------------------------- /source/core/Filesystem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include 28 | #include 29 | 30 | #include "gk/core/Filesystem.hpp" 31 | 32 | namespace gk { 33 | 34 | bool Filesystem::fileExists(const std::string &filename) { 35 | struct stat info; 36 | 37 | return !stat(filename.c_str(), &info); 38 | } 39 | 40 | } // namespace gk 41 | 42 | -------------------------------------------------------------------------------- /source/core/ISerializable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/ISerializable.hpp" 28 | 29 | sf::Packet &operator<<(sf::Packet &packet, const gk::ISerializable &s) { 30 | s.serialize(packet); 31 | return packet; 32 | } 33 | 34 | sf::Packet &operator>>(sf::Packet &packet, gk::ISerializable &s) { 35 | s.deserialize(packet); 36 | return packet; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /source/core/Logger.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/Logger.hpp" 28 | #include "gk/core/LogStream.hpp" 29 | 30 | namespace gk { 31 | 32 | bool Logger::isEnabled = true; 33 | bool Logger::printFileAndLine = false; 34 | bool Logger::printWithColor = false; 35 | 36 | std::string Logger::textColor(LoggerColor color, bool bold) { 37 | return (!printWithColor) ? "" 38 | : std::string("\33[0;") + ((u8(color) < 10) ? "0" : "") + std::to_string(u8(color)) + ";0" + ((bold) ? "1" : "0") + "m"; 39 | } 40 | 41 | void Logger::print() { 42 | if (!isEnabled || m_level == LogLevel::None) return; 43 | 44 | m_outStream << textColor(m_color, m_isBold); 45 | 46 | char levels[4] = {'D', 'I', 'W', 'E'}; 47 | m_outStream << "[" + getCurrentTime("%H:%M:%S") + "] [" << levels[m_level] << "] "; 48 | 49 | if (printFileAndLine) 50 | m_outStream << m_file << ":" << m_line << ": "; 51 | 52 | if (!m_sourceName.empty()) 53 | m_outStream << "[" + m_sourceName + "] "; 54 | 55 | m_outStream << m_stream.str() << std::endl; 56 | } 57 | 58 | } // namespace gk 59 | 60 | -------------------------------------------------------------------------------- /source/core/LoggerHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/Exception.hpp" 28 | #include "gk/core/LoggerHandler.hpp" 29 | 30 | namespace gk { 31 | 32 | LoggerHandler::InstanceMap LoggerHandler::s_instanceMap; 33 | 34 | Logger LoggerHandler::print(LogLevel level, const char *file, int line) { 35 | return {m_stream, level >= m_maxLevel ? level : LogLevel::None, file, line, m_name}; 36 | } 37 | 38 | LoggerHandler &LoggerHandler::getInstance() { 39 | if (s_instanceMap.empty()) 40 | throw EXCEPTION("LoggerHandler is not initialized"); 41 | 42 | return *s_instanceMap.at(std::this_thread::get_id()); 43 | } 44 | 45 | void LoggerHandler::setInstance(LoggerHandler &instance) { 46 | s_instanceMap.emplace(std::this_thread::get_id(), &instance); 47 | } 48 | 49 | } // namespace gk 50 | 51 | -------------------------------------------------------------------------------- /source/core/LoggerUtils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/Logger.hpp" 28 | #include "gk/core/LoggerUtils.hpp" 29 | 30 | std::ostream &operator<<(std::ostream &stream, gk::LoggerColor color) { 31 | return stream << gk::Logger::textColor(color); 32 | } 33 | 34 | std::ostream &operator<<(std::ostream &stream, const gk::Color &color) { 35 | return stream << "gk::Color(" << color.r * 255 << ", " << color.g * 255 << ", " << color.b * 255 << ", " << color.a * 255 << ")"; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /source/core/Mouse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/Mouse.hpp" 28 | #include "gk/core/SDLHeaders.hpp" 29 | 30 | namespace gk { 31 | 32 | Window *Mouse::s_window = nullptr; 33 | 34 | void Mouse::resetToWindowCenter() { 35 | SDL_WarpMouseInWindow(s_window->window(), s_window->getSize().x / 2, s_window->getSize().y / 2); 36 | } 37 | 38 | void Mouse::setCursorGrabbed(bool grabbed) { 39 | SDL_SetRelativeMouseMode((SDL_bool)grabbed); 40 | } 41 | 42 | void Mouse::setCursorVisible(bool visible) { 43 | SDL_ShowCursor(visible); 44 | } 45 | 46 | Vector2i Mouse::getPosition() { 47 | Vector2i pos; 48 | SDL_GetMouseState(&pos.x, &pos.y); 49 | return pos; 50 | } 51 | 52 | bool Mouse::isInRect(const IntRect &rect) { 53 | return rect.contains(getPosition()); 54 | } 55 | 56 | } // namespace gk 57 | 58 | -------------------------------------------------------------------------------- /source/core/SDLLoader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/Exception.hpp" 28 | #include "gk/core/SDLHeaders.hpp" 29 | #include "gk/core/SDLLoader.hpp" 30 | 31 | namespace gk { 32 | 33 | SDLLoader::~SDLLoader() { 34 | // if(m_mixInitialized) Mix_CloseAudio(); 35 | // if(m_ttfInitialized) TTF_Quit(); 36 | if(m_imgInitialized) IMG_Quit(); 37 | if(m_sdlInitialized) SDL_Quit(); 38 | } 39 | 40 | void SDLLoader::load() { 41 | if(SDL_Init(SDL_INIT_VIDEO) < 0) { 42 | throw EXCEPTION("SDL init error:", SDL_GetError()); 43 | } else { 44 | m_sdlInitialized = true; 45 | } 46 | 47 | int imgFlags = IMG_INIT_PNG; 48 | if((!IMG_Init(imgFlags)) & imgFlags) { 49 | throw EXCEPTION("SDL_image init error:", IMG_GetError()); 50 | } else { 51 | m_imgInitialized = true; 52 | } 53 | 54 | // if(TTF_Init() < 0) { 55 | // throw EXCEPTION("SDL_ttf init error:", TTF_GetError()); 56 | // } else { 57 | // m_ttfInitialized = true; 58 | // } 59 | // 60 | // if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) { 61 | // throw EXCEPTION("SDL_mixer init error:", Mix_GetError()); 62 | // } else { 63 | // m_mixInitialized = true; 64 | // } 65 | // 66 | // Mix_AllocateChannels(32); 67 | // 68 | // Mix_VolumeMusic(MIX_MAX_VOLUME / 3); 69 | // Mix_Volume(-1, MIX_MAX_VOLUME); 70 | } 71 | 72 | } // namespace gk 73 | 74 | -------------------------------------------------------------------------------- /source/core/Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/Timer.hpp" 28 | #include "gk/core/GameClock.hpp" 29 | 30 | namespace gk { 31 | 32 | Timer::Timer(bool useRealTime) { 33 | m_useRealTime = useRealTime; 34 | m_t = GameClock::getInstance().getTicks(m_useRealTime); 35 | } 36 | 37 | void Timer::stop() { 38 | if(m_isStarted) { 39 | m_isStarted = false; 40 | m_tick = GameClock::getInstance().getTicks(m_useRealTime) - m_t; 41 | } 42 | } 43 | 44 | void Timer::start() { 45 | if(!m_isStarted) { 46 | m_isStarted = true; 47 | m_t = GameClock::getInstance().getTicks(m_useRealTime) - m_tick; 48 | } 49 | } 50 | 51 | void Timer::reset() { 52 | m_isStarted = false; 53 | m_t = GameClock::getInstance().getTicks(m_useRealTime); 54 | m_tick = 0; 55 | } 56 | 57 | u32 Timer::time() const { 58 | if(m_isStarted) { 59 | return GameClock::getInstance().getTicks(m_useRealTime) - m_t; 60 | } else { 61 | return m_tick; 62 | } 63 | } 64 | 65 | void Timer::setTime(u32 time) { 66 | if(m_isStarted) { 67 | m_t = GameClock::getInstance().getTicks(m_useRealTime) - time; 68 | } else { 69 | m_tick = time; 70 | } 71 | } 72 | 73 | } // namespace gk 74 | 75 | -------------------------------------------------------------------------------- /source/core/Utils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include 28 | #include 29 | #include 30 | 31 | #include "gk/core/IntTypes.hpp" 32 | #include "gk/core/SDLHeaders.hpp" 33 | #include "gk/core/Utils.hpp" 34 | 35 | namespace gk { 36 | 37 | bool regexMatch(const std::string &str, const std::string ®ex) { 38 | std::regex re(regex); 39 | std::cmatch m; 40 | 41 | return std::regex_match(str.c_str(), m, re); 42 | } 43 | 44 | std::string getCurrentTime(const char *format) { 45 | std::time_t t = std::time(nullptr); 46 | std::tm tm = *std::localtime(&t); 47 | 48 | std::stringstream sstream; 49 | sstream << std::put_time(&tm, format); 50 | 51 | return sstream.str(); 52 | } 53 | 54 | // TODO: A class similar to `sf::Image` should be created around SDL_Surface and SDL2_image 55 | 56 | // From: https://stackoverflow.com/a/5867170/1392477 57 | SDL_Surface *flipSDLSurface(SDL_Surface *sfc) noexcept { 58 | SDL_Surface *result = SDL_CreateRGBSurface(sfc->flags, sfc->w, sfc->h, 59 | sfc->format->BytesPerPixel * 8, sfc->format->Rmask, sfc->format->Gmask, 60 | sfc->format->Bmask, sfc->format->Amask); 61 | 62 | u8 *pixels = (u8 *)sfc->pixels; 63 | u8 *rpixels = (u8 *)result->pixels; 64 | 65 | u32 pitch = sfc->pitch; 66 | u32 pxlength = pitch * sfc->h; 67 | 68 | assert(result != nullptr); 69 | 70 | for (u32 line = 0; line < (u32)sfc->h; ++line) { 71 | u32 pos = line * pitch; 72 | for (u32 i = 0 ; i <= pitch ; ++i) { 73 | rpixels[pos + i] = pixels[pxlength - pos - (pitch - i)]; 74 | } 75 | } 76 | 77 | return result; 78 | } 79 | 80 | } // namespace gk 81 | 82 | -------------------------------------------------------------------------------- /source/core/input/GamePad.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/input/GamePad.hpp" 28 | 29 | namespace gk { 30 | 31 | InputHandler *GamePad::inputHandler = nullptr; 32 | 33 | bool GamePad::isKeyPressed(GameKey key) { 34 | return (inputHandler) ? inputHandler->isKeyPressed(key) : false; 35 | } 36 | 37 | bool GamePad::isKeyPressedOnce(GameKey key) { 38 | return (inputHandler) ? inputHandler->isKeyPressedOnce(key) : false; 39 | } 40 | 41 | bool GamePad::isKeyPressedWithDelay(GameKey key, u16 delay) { 42 | return (inputHandler) ? inputHandler->isKeyPressedWithDelay(key, delay) : false; 43 | } 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /source/core/input/InputHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/input/InputHandler.hpp" 28 | #include "gk/core/GameClock.hpp" 29 | 30 | namespace gk { 31 | 32 | bool InputHandler::isKeyPressedOnce(GameKey key) { 33 | if(isKeyPressed(key)) { 34 | if(!m_keysPressedOnce[key]) { 35 | m_keysPressedOnce[key] = true; 36 | return true; 37 | } else { 38 | return false; 39 | } 40 | } else { 41 | m_keysPressedOnce[key] = false; 42 | return false; 43 | } 44 | } 45 | 46 | bool InputHandler::isKeyPressedWithDelay(GameKey key, u16 delay) { 47 | if(isKeyPressed(key) && GameClock::getInstance().getTicks() - m_lastTimePressed[key] > delay) { 48 | m_lastTimePressed[key] = GameClock::getInstance().getTicks(); 49 | return true; 50 | } else { 51 | if(!isKeyPressed(key)) m_lastTimePressed[key] = 0; 52 | return false; 53 | } 54 | } 55 | 56 | void InputHandler::addKey(GameKey key) { 57 | m_keysPressed[key] = false; 58 | m_keysPressedOnce[key] = false; 59 | m_lastTimePressed[key] = 0; 60 | } 61 | 62 | } // namespace gk 63 | 64 | -------------------------------------------------------------------------------- /source/core/input/KeyboardHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/input/KeyboardHandler.hpp" 28 | #include "gk/core/Debug.hpp" 29 | #include "gk/core/IntTypes.hpp" 30 | #include "gk/core/XMLFile.hpp" 31 | 32 | namespace gk { 33 | 34 | void KeyboardHandler::loadKeysFromFile(const std::string &filename) { 35 | XMLFile doc(filename); 36 | 37 | tinyxml2::XMLElement *keys = doc.FirstChildElement("keys").ToElement(); 38 | if (keys) { 39 | GameKey key = 0; 40 | tinyxml2::XMLElement *keyElement = keys->FirstChildElement("key"); 41 | while (keyElement) { 42 | const char *keyName; 43 | if ((keyName = keyElement->Attribute("scancode"))) 44 | m_keys[key] = SDL_GetKeyFromScancode(SDL_GetScancodeFromName(keyName)); 45 | else if ((keyName = keyElement->Attribute("keycode"))) 46 | m_keys[key] = SDL_GetKeyFromName(keyName); 47 | else 48 | gkWarning() << "Key '" << keyElement->Attribute("name") << "' is invalid"; 49 | 50 | if(m_keys[key] == SDLK_UNKNOWN) { 51 | gkWarning() << "Key '" << keyName << "' not recognized"; 52 | } 53 | 54 | InputHandler::addKey(key); 55 | 56 | ++key; 57 | keyElement = keyElement->NextSiblingElement("key"); 58 | } 59 | } 60 | } 61 | 62 | bool KeyboardHandler::isKeyPressed(GameKey key) { 63 | const u8 *keyboardState = SDL_GetKeyboardState(nullptr); 64 | SDL_Keycode keyScancode = m_keys[key]; 65 | 66 | m_keysPressed[key] = keyboardState[SDL_GetScancodeFromKey(keyScancode)]; 67 | 68 | return m_keysPressed[key]; 69 | } 70 | 71 | } // namespace gk 72 | 73 | -------------------------------------------------------------------------------- /source/gl/VertexArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/gl/GLCheck.hpp" 28 | #include "gk/gl/VertexArray.hpp" 29 | 30 | namespace gk { 31 | 32 | VertexArray::VertexArray() { 33 | glCheck(glGenVertexArrays(1, &m_id)); 34 | } 35 | 36 | VertexArray::VertexArray(VertexArray &&vertexArray) { 37 | m_id = vertexArray.m_id; 38 | vertexArray.m_id = 0; 39 | } 40 | 41 | VertexArray::~VertexArray() noexcept { 42 | if (m_id != 0) 43 | glCheck(glDeleteVertexArrays(1, &m_id)); 44 | } 45 | 46 | VertexArray &VertexArray::operator=(VertexArray &&vertexArray) { 47 | m_id = vertexArray.m_id; 48 | vertexArray.m_id = 0; 49 | 50 | return *this; 51 | } 52 | 53 | void VertexArray::bind(const VertexArray *vertexArray) { 54 | if(vertexArray) { 55 | glCheck(glBindVertexArray(vertexArray->m_id)); 56 | } else { 57 | glCheck(glBindVertexArray(0)); 58 | } 59 | } 60 | 61 | } // namespace gk 62 | 63 | -------------------------------------------------------------------------------- /source/gl/VertexBuffer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/gl/GLCheck.hpp" 28 | #include "gk/gl/VertexBuffer.hpp" 29 | 30 | namespace gk { 31 | 32 | VertexBuffer::VertexBuffer() { 33 | glCheck(glGenBuffers(1, &m_id)); 34 | } 35 | 36 | VertexBuffer::VertexBuffer(VertexBuffer &&vertexBuffer) { 37 | m_id = vertexBuffer.m_id; 38 | vertexBuffer.m_id = 0; 39 | 40 | m_layout = std::move(vertexBuffer.m_layout); 41 | } 42 | 43 | VertexBuffer::~VertexBuffer() noexcept { 44 | if (m_id != 0) 45 | glCheck(glDeleteBuffers(1, &m_id)); 46 | } 47 | 48 | VertexBuffer &VertexBuffer::operator=(VertexBuffer &&vertexBuffer) { 49 | m_id = vertexBuffer.m_id; 50 | vertexBuffer.m_id = 0; 51 | 52 | m_layout = std::move(vertexBuffer.m_layout); 53 | 54 | return *this; 55 | } 56 | 57 | void VertexBuffer::setData(GLsizeiptr size, const GLvoid *data, GLenum usage) const { 58 | glCheck(glBufferData(GL_ARRAY_BUFFER, size, data, usage)); 59 | } 60 | 61 | void VertexBuffer::updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data) const { 62 | glCheck(glBufferSubData(GL_ARRAY_BUFFER, offset, size, data)); 63 | } 64 | 65 | void VertexBuffer::bind(const VertexBuffer *vertexBuffer) { 66 | if(vertexBuffer) { 67 | glCheck(glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->m_id)); 68 | } else { 69 | glCheck(glBindBuffer(GL_ARRAY_BUFFER, 0)); 70 | } 71 | } 72 | 73 | } // namespace gk 74 | 75 | -------------------------------------------------------------------------------- /source/gl/VertexBufferLayout.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/gl/GLCheck.hpp" 28 | #include "gk/gl/RenderStates.hpp" 29 | #include "gk/gl/Vertex.hpp" 30 | #include "gk/gl/VertexBufferLayout.hpp" 31 | 32 | namespace gk { 33 | 34 | void VertexBufferLayout::setupDefaultLayout() { 35 | addAttribute(0, "coord3d", 4, GL_FLOAT, GL_FALSE, (GLsizei)sizeof(Vertex), reinterpret_cast(offsetof(Vertex, coord3d))); 36 | addAttribute(1, "texCoord", 2, GL_FLOAT, GL_FALSE, (GLsizei)sizeof(Vertex), reinterpret_cast(offsetof(Vertex, texCoord))); 37 | addAttribute(2, "color", 4, GL_FLOAT, GL_FALSE, (GLsizei)sizeof(Vertex), reinterpret_cast(offsetof(Vertex, color))); 38 | } 39 | 40 | void VertexBufferLayout::enableLayout() const { 41 | assert(!m_attributes.empty()); 42 | 43 | for (auto &attr : m_attributes) { 44 | glCheck(glEnableVertexAttribArray(attr.id)); 45 | glCheck(glVertexAttribPointer(attr.id, attr.size, attr.type, attr.normalized, attr.stride, attr.offset)); 46 | } 47 | } 48 | 49 | void VertexBufferLayout::disableLayout() const { 50 | for (auto &attr : m_attributes) 51 | glCheck(glDisableVertexAttribArray(attr.id)); 52 | } 53 | 54 | } // namespace gk 55 | 56 | -------------------------------------------------------------------------------- /source/graphics/Color.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include 28 | 29 | #include "gk/core/Debug.hpp" 30 | #include "gk/graphics/Color.hpp" 31 | #include "gk/math/Math.hpp" 32 | 33 | namespace gk { 34 | 35 | const Color Color::Black = Color::fromRGBA32(0, 0, 0); 36 | const Color Color::White = Color::fromRGBA32(255, 255, 255); 37 | const Color Color::Red = Color::fromRGBA32(255, 0, 0); 38 | const Color Color::Green = Color::fromRGBA32(0, 255, 0); 39 | const Color Color::Blue = Color::fromRGBA32(0, 0, 255); 40 | const Color Color::Yellow = Color::fromRGBA32(255, 255, 0); 41 | const Color Color::Magenta = Color::fromRGBA32(255, 0, 255); 42 | const Color Color::Cyan = Color::fromRGBA32(0, 255, 255); 43 | const Color Color::Transparent = Color::fromRGBA32(0, 0, 0, 0); 44 | 45 | Color::Color(float r, float g, float b, float a) { 46 | assert(r <= 1 && g <= 1 && b <= 1 && a <= 1); 47 | 48 | this->r = r; 49 | this->g = g; 50 | this->b = b; 51 | this->a = a; 52 | } 53 | 54 | Color Color::mix(const Color &other, float ratio) { 55 | Color c; 56 | c.r = lerpf(r, other.r, ratio); 57 | c.g = lerpf(g, other.g, ratio); 58 | c.b = lerpf(b, other.b, ratio); 59 | c.a = lerpf(a, other.a, ratio); 60 | return c; 61 | } 62 | 63 | Color Color::fromRGBA32(u8 r, u8 g, u8 b, u8 a) { 64 | return Color{ 65 | (float)r / 255.0f, 66 | (float)g / 255.0f, 67 | (float)b / 255.0f, 68 | (float)a / 255.0f, 69 | }; 70 | } 71 | 72 | } // namespace gk 73 | 74 | -------------------------------------------------------------------------------- /source/graphics/SpriteAnimation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/graphics/SpriteAnimation.hpp" 28 | 29 | namespace gk { 30 | 31 | void SpriteAnimation::reset(u16 frameID) { 32 | m_timer.reset(); 33 | m_timer.setTime(frameID * m_delay); 34 | } 35 | 36 | void SpriteAnimation::start() { 37 | m_timer.start(); 38 | 39 | // if(!Sprite::pause) m_isPaused = false; 40 | } 41 | 42 | void SpriteAnimation::stop() { 43 | m_timer.stop(); 44 | 45 | // if(!Sprite::pause) m_isPaused = true; 46 | } 47 | 48 | void SpriteAnimation::play() { 49 | if(/* Sprite::pause || */m_isPaused) { 50 | stop(); 51 | } else { 52 | start(); 53 | } 54 | 55 | if(isFinished()) { 56 | if(m_isRepeated) { 57 | reset(); 58 | start(); 59 | } else { 60 | stop(); 61 | } 62 | } 63 | } 64 | 65 | u16 SpriteAnimation::getFrame(u16 frameID) const { 66 | return m_frames[frameID]; 67 | } 68 | 69 | u16 SpriteAnimation::currentFrame() const { 70 | u16 frameID = displayedFramesAmount(); 71 | if(frameID >= m_frames.size()) { 72 | return getFrame((u16)m_frames.size() - 1); 73 | } else { 74 | return getFrame(frameID); 75 | } 76 | } 77 | 78 | u16 SpriteAnimation::displayedFramesAmount() const { 79 | if (m_delay == 0) 80 | return 0; 81 | 82 | return u16(m_timer.time() / m_delay); 83 | } 84 | 85 | } 86 | 87 | -------------------------------------------------------------------------------- /source/graphics/TiledImage.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/graphics/TiledImage.hpp" 28 | 29 | namespace gk { 30 | 31 | TiledImage::TiledImage(const std::string &textureName) { 32 | load(textureName); 33 | } 34 | 35 | void TiledImage::load(const std::string &textureName) { 36 | m_textureName = textureName; 37 | } 38 | 39 | void TiledImage::setTile(u16 id, float x, float y, u16 width, u16 height, float clipX, float clipY, u16 clipWidth, u16 clipHeight, const gk::Color &color) { 40 | auto &tile = m_tiles.at(id); 41 | tile.setClipRect(clipX, clipY, clipWidth, clipHeight); 42 | tile.setPosRect(x, y, width, height); 43 | tile.setColor(color); 44 | } 45 | 46 | void TiledImage::setTilePosRect(u16 id, float x, float y, u16 width, u16 height) { 47 | m_tiles.at(id).setPosRect(x, y, width, height); 48 | } 49 | 50 | void TiledImage::setTileClipRect(u16 id, float x, float y, u16 clipWidth, u16 clipHeight) { 51 | m_tiles.at(id).setClipRect(x, y, clipWidth, clipHeight); 52 | } 53 | 54 | void TiledImage::setTileColor(u16 id, const gk::Color &color) { 55 | m_tiles.at(id).setColor(color); 56 | } 57 | 58 | void TiledImage::setTileCount(u16 tileCount) { 59 | m_tiles.resize(tileCount); 60 | 61 | for (auto &it : m_tiles) 62 | it.load(m_textureName); 63 | } 64 | 65 | void TiledImage::draw(gk::RenderTarget &target, gk::RenderStates states) const { 66 | states.transform *= getTransform(); 67 | 68 | for (auto &it : m_tiles) 69 | target.draw(it, states); 70 | } 71 | 72 | } // namespace gk 73 | 74 | -------------------------------------------------------------------------------- /source/graphics/tilemap/TilemapAnimator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/graphics/tilemap/TilemapAnimator.hpp" 28 | #include "gk/graphics/Tilemap.hpp" 29 | 30 | namespace gk { 31 | 32 | void TilemapAnimator::init(Tilemap &map) { 33 | m_tileAnimations.assign(map.width() * map.height(), {}); 34 | } 35 | 36 | void TilemapAnimator::animateTiles(Tilemap &map) { 37 | for (u16 x = 0 ; x < map.width() ; ++x) { 38 | for (u16 y = 0 ; y < map.height() ; ++y) { 39 | const Tile &tile = map.tileset().getTile(map.getTile(x, y)); 40 | if (tile.getFrameCount()) { 41 | TileAnimation &tileAnimation = m_tileAnimations.at(x + y * map.width()); 42 | if (!tileAnimation.timer.isStarted()) 43 | tileAnimation.timer.start(); 44 | 45 | if (tileAnimation.timer.time() > tile.getFrame(tileAnimation.currentFrame).duration) { 46 | tileAnimation.currentFrame++; 47 | tileAnimation.currentFrame %= tile.getFrameCount(); 48 | 49 | map.setTile(x, y, tile.getFrame(tileAnimation.currentFrame).tileID, false); 50 | 51 | tileAnimation.timer.reset(); 52 | tileAnimation.timer.start(); 53 | } 54 | } 55 | } 56 | } 57 | } 58 | 59 | } // namespace gk 60 | 61 | -------------------------------------------------------------------------------- /source/resource/ResourceHandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/resource/ResourceHandler.hpp" 28 | 29 | namespace gk { 30 | 31 | ResourceHandler *ResourceHandler::instance = nullptr; 32 | 33 | ResourceHandler &ResourceHandler::getInstance() { 34 | return *instance; 35 | } 36 | 37 | void ResourceHandler::setInstance(ResourceHandler &handler) { 38 | instance = &handler; 39 | } 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /source/resource/TextureLoader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/XMLFile.hpp" 28 | #include "gk/gl/Texture.hpp" 29 | #include "gk/resource/ResourceHandler.hpp" 30 | #include "gk/resource/TextureLoader.hpp" 31 | 32 | namespace gk { 33 | 34 | void TextureLoader::load(const char *xmlFilename, ResourceHandler &handler) { 35 | XMLFile doc(xmlFilename); 36 | 37 | tinyxml2::XMLElement *textureElement = doc.FirstChildElement("textures").FirstChildElement("texture").ToElement(); 38 | while (textureElement) { 39 | std::string name = textureElement->Attribute("name"); 40 | std::string path = textureElement->Attribute("path"); 41 | 42 | auto &texture = handler.add("texture-" + name); 43 | texture.loadFromFile(path); 44 | 45 | textureElement = textureElement->NextSiblingElement("texture"); 46 | } 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /source/resource/TilemapLoader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/graphics/Tilemap.hpp" 28 | #include "gk/resource/TilemapLoader.hpp" 29 | 30 | namespace gk { 31 | 32 | void TilemapLoader::load(const char *xmlFilename, ResourceHandler &handler) { 33 | XMLFile doc(xmlFilename); 34 | 35 | tinyxml2::XMLElement *mapElement = doc.FirstChildElement("maps").FirstChildElement("map").ToElement(); 36 | while(mapElement) { 37 | std::string name = mapElement->Attribute("name"); 38 | std::string tilesetName = mapElement->Attribute("tileset"); 39 | 40 | Tileset &tileset = handler.get(tilesetName); 41 | 42 | loadMap(name, tileset, handler); 43 | 44 | mapElement = mapElement->NextSiblingElement("map"); 45 | } 46 | } 47 | 48 | void TilemapLoader::loadMap(const std::string &name, Tileset &tileset, ResourceHandler &handler) { 49 | XMLFile doc("resources/maps/" + name + ".tmx"); 50 | 51 | tinyxml2::XMLElement *mapElement = doc.FirstChildElement("map").ToElement(); 52 | 53 | u16 width = (u16)mapElement->UnsignedAttribute("width"); 54 | u16 height = (u16)mapElement->UnsignedAttribute("height"); 55 | 56 | std::vector> data; 57 | tinyxml2::XMLElement *layerElement = mapElement->FirstChildElement("layer"); 58 | while (layerElement) { 59 | tinyxml2::XMLElement *tileElement = layerElement->FirstChildElement("data")->FirstChildElement("tile"); 60 | 61 | std::vector layer; 62 | while(tileElement) { 63 | s16 tileID = s16(tileElement->IntAttribute("gid") - 1); 64 | 65 | layer.push_back((tileID >= 0) ? tileID : 0); 66 | 67 | tileElement = tileElement->NextSiblingElement("tile"); 68 | } 69 | 70 | data.emplace_back(std::move(layer)); 71 | 72 | layerElement = layerElement->NextSiblingElement("layer"); 73 | } 74 | 75 | handler.add("map-" + name, width, height, tileset, data); 76 | } 77 | 78 | } // namespace gk 79 | 80 | -------------------------------------------------------------------------------- /source/resource/TilesetLoader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/XMLFile.hpp" 28 | #include "gk/graphics/Tileset.hpp" 29 | #include "gk/resource/ResourceHandler.hpp" 30 | #include "gk/resource/TilesetLoader.hpp" 31 | 32 | namespace gk { 33 | 34 | void TilesetLoader::load(const char *xmlFilename, ResourceHandler &handler) { 35 | XMLFile doc(xmlFilename); 36 | 37 | tinyxml2::XMLElement *tilesetElement = doc.FirstChildElement("tilesets").FirstChildElement("tileset").ToElement(); 38 | while(tilesetElement) { 39 | std::string name = tilesetElement->Attribute("name"); 40 | std::string filename = "resources/graphics/tilesets/" + name + ".png"; 41 | 42 | std::string configFile = "resources/tilesets/" + name + ".tsx"; 43 | 44 | handler.add(name, filename, configFile); 45 | 46 | tilesetElement = tilesetElement->NextSiblingElement("tileset"); 47 | } 48 | } 49 | 50 | } // namespace gk 51 | 52 | -------------------------------------------------------------------------------- /source/scene/controller/BehaviourController.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/scene/component/BehaviourComponent.hpp" 28 | #include "gk/scene/component/LifetimeComponent.hpp" 29 | #include "gk/scene/controller/BehaviourController.hpp" 30 | 31 | namespace gk { 32 | 33 | void BehaviourController::update(SceneObject &object) { 34 | if(object.has() && (!object.has() || !object.get().dead(object))) { 35 | object.get().update(object); 36 | } 37 | } 38 | 39 | } // namespace gk 40 | 41 | -------------------------------------------------------------------------------- /source/scene/controller/LifetimeController.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/scene/component/LifetimeComponent.hpp" 28 | #include "gk/scene/controller/LifetimeController.hpp" 29 | 30 | namespace gk { 31 | 32 | void LifetimeController::update(SceneObjectList &objects) { 33 | for(size_t i = 0 ; i < objects.size() ; i++) { 34 | if (objects[i].has()) 35 | update(objects[i].get()); 36 | 37 | if(objects[i].has()) { 38 | auto &lifetimeComponent = objects[i].get(); 39 | if (lifetimeComponent.dead(objects[i]) && lifetimeComponent.areClientsNotified()) { 40 | bool canDelete = true; 41 | if (objects[i].has()) { 42 | for (SceneObject &object : objects[i].get()) { 43 | if (object.has() && !object.get().dead(object)) { 44 | canDelete = false; 45 | break; 46 | } 47 | } 48 | } 49 | 50 | if (canDelete) 51 | objects.remove(i--); 52 | } 53 | } 54 | } 55 | } 56 | 57 | } // namespace gk 58 | 59 | -------------------------------------------------------------------------------- /source/scene/controller/MovementController.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/scene/controller/MovementController.hpp" 28 | 29 | #include "gk/scene/component/CollisionComponent.hpp" 30 | #include "gk/scene/component/MovementComponent.hpp" 31 | #include "gk/scene/component/PositionComponent.hpp" 32 | 33 | namespace gk { 34 | 35 | void MovementController::update(SceneObject &object) { 36 | if(object.has()) { 37 | auto &movement = object.get(); 38 | 39 | if(movement.movements.size() != 0 && movement.movements.top()) { 40 | movement.movements.top()->process(object); 41 | } 42 | 43 | movement.isBlocked.x = false; 44 | movement.isBlocked.y = false; 45 | } 46 | 47 | if(object.has()) { 48 | object.get().checkCollisions(object); 49 | } 50 | 51 | if(object.has()) { 52 | auto &movement = object.get(); 53 | 54 | movement.isMoving = movement.v.x || movement.v.y; 55 | 56 | object.get() += movement.v * movement.speed; 57 | } 58 | } 59 | 60 | } // namespace gk 61 | 62 | -------------------------------------------------------------------------------- /source/scene/movement/GamePadMovement.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/core/input/GamePad.hpp" 28 | #include "gk/scene/movement/GamePadMovement.hpp" 29 | #include "gk/scene/component/MovementComponent.hpp" 30 | 31 | namespace gk { 32 | 33 | void GamePadMovement::process(SceneObject &object) { 34 | auto &movementComponent = object.get(); 35 | 36 | movementComponent.v.x = 0; 37 | movementComponent.v.y = 0; 38 | 39 | if(GamePad::isKeyPressed(m_left)) { 40 | movementComponent.v.x = -1; 41 | } 42 | else if(GamePad::isKeyPressed(m_right)) { 43 | movementComponent.v.x = 1; 44 | } 45 | 46 | if(GamePad::isKeyPressed(m_up)) { 47 | movementComponent.v.y = -1; 48 | } 49 | else if(GamePad::isKeyPressed(m_down)) { 50 | movementComponent.v.y = 1; 51 | } 52 | 53 | m_isFinished = true; 54 | } 55 | 56 | } // namespace gk 57 | 58 | -------------------------------------------------------------------------------- /source/scene/view/HitboxView.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/graphics/RectangleShape.hpp" 28 | #include "gk/scene/component/HitboxComponent.hpp" 29 | #include "gk/scene/view/HitboxView.hpp" 30 | #include "gk/scene/component/LifetimeComponent.hpp" 31 | #include "gk/scene/component/PositionComponent.hpp" 32 | 33 | namespace gk { 34 | 35 | void HitboxView::draw(const SceneObject &object, RenderTarget &target, RenderStates states) { 36 | if (object.has() && object.get().dead(object)) 37 | return; 38 | 39 | if (object.has()) { 40 | states.transform.translate(object.get().x, object.get().y); 41 | } 42 | 43 | if (object.has()) { 44 | const FloatRect *hitbox = object.get().currentHitbox(); 45 | if(hitbox) { 46 | RectangleShape rect; 47 | rect.setPosition(hitbox->x, hitbox->y); 48 | rect.setSize(hitbox->sizeX, hitbox->sizeY); 49 | rect.setWireframeMode(true); // FIXME 50 | rect.setFillColor(Color::White); 51 | 52 | target.draw(rect, states); 53 | } 54 | } 55 | } 56 | 57 | } // namespace gk 58 | 59 | -------------------------------------------------------------------------------- /source/scene/view/SpriteView.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #include "gk/graphics/Image.hpp" 28 | #include "gk/graphics/RectangleShape.hpp" 29 | #include "gk/graphics/Sprite.hpp" 30 | #include "gk/scene/component/HitboxComponent.hpp" 31 | #include "gk/scene/component/LifetimeComponent.hpp" 32 | #include "gk/scene/component/PositionComponent.hpp" 33 | #include "gk/scene/view/SpriteView.hpp" 34 | 35 | namespace gk { 36 | 37 | void SpriteView::draw(const SceneObject &object, RenderTarget &target, RenderStates states) { 38 | if (object.has() && object.get().dead(object)) 39 | return; 40 | 41 | if (object.has()) 42 | states.transform.translate({object.get(), 0.f}); 43 | 44 | if(object.has()) { 45 | target.draw(object.get(), states); 46 | } 47 | 48 | if(object.has()) { 49 | target.draw(object.get(), states); 50 | } 51 | } 52 | 53 | } // namespace gk 54 | 55 | -------------------------------------------------------------------------------- /tests/ColorTests.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef COLORTESTS_HPP_ 28 | #define COLORTESTS_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/graphics/Color.hpp" 33 | 34 | using namespace gk; 35 | 36 | class ColorTests : public CxxTest::TestSuite { 37 | public: 38 | void testMix() { 39 | Color c1 = Color::fromRGBA32(7, 15, 225, 123); 40 | Color c2 = Color::fromRGBA32(192, 192, 192, 192); 41 | Color c3; 42 | TS_ASSERT_EQUALS(c1.mix(c2, 0.0f), c1); 43 | TS_ASSERT_EQUALS(c1.mix(c2, 1.0f), c2); 44 | c3.r = ( 7/255.f+192/255.f)*0.5f; 45 | c3.g = ( 15/255.f+192/255.f)*0.5f; 46 | c3.b = (225/255.f+192/255.f)*0.5f; 47 | c3.a = (123/255.f+192/255.f)*0.5f; 48 | TS_ASSERT_EQUALS((c1.mix(c2, 0.5f)), c3); 49 | } 50 | 51 | // Currently disabled because the current code is buggy 52 | void xtestAddition() { 53 | Color c1 = Color::fromRGBA32(7, 15, 225, 123); 54 | Color c2 = Color::fromRGBA32(192, 192, 192, 192); 55 | Color c3; 56 | // Should addition clamp? 57 | c3.r = ( 7/255.f+192/255.f); 58 | c3.g = ( 15/255.f+192/255.f); 59 | c3.b = (225/255.f+192/255.f); 60 | c3.a = (123/255.f+192/255.f); 61 | TS_ASSERT_EQUALS(c1 + c2, c3); 62 | } 63 | 64 | void xtestSubtraction() { 65 | Color c1 = Color::fromRGBA32(7, 15, 225, 123); 66 | Color c2 = Color::fromRGBA32(192, 192, 192, 192); 67 | Color c3; 68 | // Should subtraction clamp? 69 | c3.r = ( 7/255.f-192/255.f); 70 | c3.g = ( 15/255.f-192/255.f); 71 | c3.b = (225/255.f-192/255.f); 72 | c3.a = (123/255.f-192/255.f); 73 | TS_ASSERT_EQUALS(c1 - c2, c3); 74 | } 75 | }; 76 | 77 | #endif // COLORTESTS_HPP_ 78 | -------------------------------------------------------------------------------- /tests/Vector2Tests.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * GameKit 5 | * 6 | * Copyright (C) 2018-2020 Unarelith, Quentin Bazin 7 | * Copyright (C) 2020 the GameKit contributors (see CONTRIBUTORS.md) 8 | * 9 | * This file is part of GameKit. 10 | * 11 | * GameKit is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * GameKit is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with GameKit; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 | * 25 | * ===================================================================================== 26 | */ 27 | #ifndef VECTOR2TESTS_HPP_ 28 | #define VECTOR2TESTS_HPP_ 29 | 30 | #include 31 | 32 | #include "gk/core/Vector2.hpp" 33 | 34 | using namespace gk; 35 | 36 | class Vector2Tests : public CxxTest::TestSuite { 37 | public: 38 | void testEqual() { 39 | Vector2i v1{5, 4}, v2{2, 6}, v3{5, 4}; 40 | 41 | TS_ASSERT(v1 != v2); 42 | TS_ASSERT(v1 == v1); 43 | TS_ASSERT(v1 == v3); 44 | } 45 | 46 | void testAssignement() { 47 | Vector2i v1, v2; 48 | 49 | v1 = {2, 6}; 50 | v2 = 0; 51 | 52 | TS_ASSERT_EQUALS(v1, (Vector2i{2, 6})); 53 | TS_ASSERT_EQUALS(v2, (Vector2i{0, 0})); 54 | } 55 | 56 | void testAddition() { 57 | Vector2i v1{5, 4}, v2{2, 6}, v3{2, 3}, v4{4, 8}; 58 | 59 | TS_ASSERT_EQUALS(v1 + v2, (Vector2i{ 7, 10})); 60 | TS_ASSERT_EQUALS(v2 + v3, (Vector2i{ 4, 9})); 61 | TS_ASSERT_EQUALS(v1 + v1, (Vector2i{10, 8})); 62 | 63 | v4 += v2; 64 | 65 | TS_ASSERT_EQUALS(v4, (Vector2i{ 6, 14})); 66 | } 67 | 68 | void testSubtraction() { 69 | Vector2i v1{5, 4}, v2{2, 6}, v3{2, 3}, v4{4, 8}; 70 | 71 | TS_ASSERT_EQUALS(v1 - v2, (Vector2i{ 3, -2})); 72 | TS_ASSERT_EQUALS(v2 - v3, (Vector2i{ 0, 3})); 73 | TS_ASSERT_EQUALS(v1 - v1, (Vector2i{ 0, 0})); 74 | TS_ASSERT_EQUALS(v4 - v2, (Vector2i{ 2, 2})); 75 | } 76 | 77 | void testMultiplication() { 78 | Vector2i v1{5, -4}; 79 | 80 | TS_ASSERT_EQUALS(v1 * -4, (Vector2i{-20, 16})); 81 | TS_ASSERT_EQUALS( 8 * v1, (Vector2i{40, -32})); 82 | } 83 | 84 | void testDivision() { 85 | Vector2i v1{4, -4}; 86 | 87 | TS_ASSERT_EQUALS(v1 / -4, (Vector2i{-1, 1})); 88 | TS_ASSERT_THROWS_ANYTHING(v1 / 0); 89 | } 90 | }; 91 | 92 | #endif // VECTOR2TESTS_HPP_ 93 | --------------------------------------------------------------------------------