├── .editorconfig ├── .gitattributes ├── .gitignore ├── .gitmodules ├── .travis.yml ├── CMakeLists.txt ├── CODESTYLE.md ├── CONTRIBUTING.md ├── CTestTestfile.cmake ├── README.md ├── Report20170614-0125.diagsession ├── UPDATES.md ├── deps ├── Lua │ └── src │ │ ├── CMakeLists.txt │ │ ├── lapi.c │ │ ├── lapi.h │ │ ├── lauxlib.c │ │ ├── lauxlib.h │ │ ├── lbaselib.c │ │ ├── lbitlib.c │ │ ├── lcode.c │ │ ├── lcode.h │ │ ├── lcorolib.c │ │ ├── lctype.c │ │ ├── lctype.h │ │ ├── ldblib.c │ │ ├── ldebug.c │ │ ├── ldebug.h │ │ ├── ldo.c │ │ ├── ldo.h │ │ ├── ldump.c │ │ ├── lfunc.c │ │ ├── lfunc.h │ │ ├── lgc.c │ │ ├── lgc.h │ │ ├── linit.c │ │ ├── liolib.c │ │ ├── llex.c │ │ ├── llex.h │ │ ├── llimits.h │ │ ├── lmathlib.c │ │ ├── lmem.c │ │ ├── lmem.h │ │ ├── loadlib.c │ │ ├── lobject.c │ │ ├── lobject.h │ │ ├── lopcodes.c │ │ ├── lopcodes.h │ │ ├── loslib.c │ │ ├── lparser.c │ │ ├── lparser.h │ │ ├── lprefix.h │ │ ├── lstate.c │ │ ├── lstate.h │ │ ├── lstring.c │ │ ├── lstring.h │ │ ├── lstrlib.c │ │ ├── ltable.c │ │ ├── ltable.h │ │ ├── ltablib.c │ │ ├── ltm.c │ │ ├── ltm.h │ │ ├── lua.c │ │ ├── lua.h │ │ ├── lua.hpp │ │ ├── luac.c │ │ ├── luaconf.h │ │ ├── lualib.h │ │ ├── lundump.c │ │ ├── lundump.h │ │ ├── lutf8lib.c │ │ ├── lvm.c │ │ ├── lvm.h │ │ ├── lzio.c │ │ └── lzio.h └── LuaBridge │ ├── LuaBridge.h │ ├── RefCountedObject.h │ ├── RefCountedPtr.h │ └── detail │ ├── CFunctions.h │ ├── ClassInfo.h │ ├── Constructor.h │ ├── FuncTraits.h │ ├── Iterator.h │ ├── LuaException.h │ ├── LuaHelpers.h │ ├── LuaRef.h │ ├── Namespace.h │ ├── Stack.h │ ├── TypeList.h │ ├── TypeTraits.h │ ├── Userdata.h │ └── dump.h ├── res ├── entities │ ├── Lantern.json │ ├── Player.json │ ├── Projectile.json │ ├── enemy │ │ └── Zombie.json │ └── scripts │ │ ├── player.lua │ │ └── zombie.lua ├── fonts │ ├── Inconsolata-Bold.ttf │ ├── Inconsolata-Regular.ttf │ └── SourceCodePro-Regular.ttf ├── settings │ ├── Control List.txt │ └── Controls.json ├── shaders │ └── transparency.frag ├── sound │ ├── music │ │ ├── Challenge.ogg │ │ ├── LunarStreams.ogg │ │ └── Menu.ogg │ └── sfx │ │ ├── explosion.ogg │ │ ├── hit.ogg │ │ ├── jump.ogg │ │ ├── pickup.ogg │ │ ├── powerup.ogg │ │ └── shoot.ogg ├── textures │ ├── LICENSE.md │ ├── entity │ │ ├── dinolantern.png │ │ ├── enemy │ │ │ ├── rose.png │ │ │ └── zombie.png │ │ ├── player_modelDefault.png │ │ └── temp │ │ │ └── k1.png │ ├── gui │ │ ├── button.png │ │ ├── dev │ │ │ ├── menu_background.png │ │ │ └── window.png │ │ └── slider.png │ ├── items │ │ ├── branch.png │ │ ├── gunne.png │ │ ├── luger.png │ │ ├── rusty_dagger.png │ │ └── slingshot.png │ └── tile_atlas.png ├── tiles │ ├── Void.json │ └── dungeon │ │ ├── BrickFloor.json │ │ └── BrickWall.json └── worlds │ └── default │ ├── obligatory.json │ ├── random.json │ └── world.json ├── src ├── Common.h ├── Types.h ├── app │ ├── Application.cpp │ ├── Application.h │ ├── Cursor.cpp │ ├── Cursor.h │ ├── WindowSettings.h │ ├── input │ │ ├── Input.cpp │ │ ├── Input.h │ │ ├── InputScheme.cpp │ │ └── InputScheme.h │ └── states │ │ ├── StateBase.h │ │ ├── StateMenu.cpp │ │ ├── StateMenu.h │ │ ├── StatePlaying.cpp │ │ └── StatePlaying.h ├── components │ ├── Component.h │ ├── Components.h │ ├── InventoryComponent.cpp │ ├── InventoryComponent.h │ ├── LifeComponent.cpp │ ├── LifeComponent.h │ ├── LightComponent.cpp │ ├── LightComponent.h │ ├── PhysicsComponent.cpp │ ├── PhysicsComponent.h │ ├── ScriptComponent.cpp │ ├── ScriptComponent.h │ ├── SpriteComponent.cpp │ ├── SpriteComponent.h │ ├── StatsComponent.cpp │ └── StatsComponent.h ├── debug │ ├── DebugConsole.cpp │ ├── DebugConsole.h │ ├── DebugMenu.cpp │ └── DebugMenu.h ├── entity │ ├── AStar.cpp │ ├── AStar.h │ ├── Damage.h │ ├── Entity.cpp │ ├── Entity.h │ ├── EntityFactory.cpp │ ├── EntityFactory.h │ └── system │ │ ├── System.cpp │ │ └── System.h ├── events │ ├── Event.cpp │ ├── Event.h │ ├── EventDispatcher.h │ ├── Events.h │ ├── IEventListener.h │ ├── KeyEvent.cpp │ ├── KeyEvent.h │ ├── MouseEvent.cpp │ └── MouseEvent.h ├── graphics │ ├── Animator.cpp │ ├── Animator.h │ ├── Label.cpp │ ├── Label.h │ ├── Renderable2D.h │ ├── Renderer2D.cpp │ ├── Renderer2D.h │ └── gui │ │ ├── Button.cpp │ │ ├── Button.h │ │ ├── Panel.cpp │ │ ├── Panel.h │ │ ├── Slider.cpp │ │ ├── Slider.h │ │ ├── Widget.cpp │ │ ├── Widget.h │ │ ├── Window.cpp │ │ └── Window.h ├── item │ ├── Item.cpp │ ├── Item.h │ ├── ItemFactory.cpp │ ├── ItemFactory.h │ └── system │ │ ├── DrawingSystems.cpp │ │ ├── DrawingSystems.h │ │ └── System.h ├── level │ ├── Level.cpp │ ├── Level.h │ ├── LightMap.cpp │ ├── LightMap.h │ ├── gen │ │ ├── Leaf.cpp │ │ ├── Leaf.h │ │ ├── WorldGenerator.cpp │ │ └── WorldGenerator.h │ └── tile │ │ ├── Tile.cpp │ │ ├── Tile.h │ │ ├── TileCollision.cpp │ │ ├── TileCollision.h │ │ ├── TileDatabase.cpp │ │ ├── TileDatabase.h │ │ ├── TileFlooding.cpp │ │ ├── TileFlooding.h │ │ ├── TileMap.cpp │ │ └── TileMap.h ├── main.cpp ├── maths │ ├── Color.h │ ├── Rectangle.cpp │ ├── Rectangle.h │ ├── Vec2.h │ ├── maths.h │ └── maths_func.h ├── resources │ ├── ResourceHolder.h │ └── ResourceManager.h ├── sound │ ├── Music.cpp │ ├── Music.h │ ├── SoundFX.cpp │ └── SoundFX.h └── util │ ├── FileUtil.cpp │ ├── FileUtil.h │ ├── Log.h │ ├── PriorityQueue.h │ ├── Random.cpp │ ├── Random.h │ ├── Timestep.h │ ├── json.hpp │ └── sfKeyToString.txt └── tests ├── CMakeLists.txt └── catch └── catch.hpp /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | charset = utf-8 7 | 8 | [*.{cpp, hpp, h}] 9 | indent_style = tab 10 | indent_size = 4 11 | 12 | [*.json] 13 | indent_style = tab 14 | indent_size = 2 15 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.layout 2 | *.depend 3 | *.cbp 4 | 5 | # User-specific files 6 | *.suo 7 | *.user 8 | *.userosscache 9 | *.sln 10 | *.vcxproj 11 | *.vcxproj.filters 12 | *.sln.docstates 13 | 14 | # Build results 15 | [Dd]ebug/ 16 | [Dd]ebugPublic/ 17 | [Rr]elease/ 18 | [Rr]eleases/ 19 | x64/ 20 | x86/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | [Ll]og/ 25 | [Dd]ependencies/ 26 | 27 | # CMake build 28 | CMakeFiles/ 29 | CMakeCache.txt 30 | cmake_install.cmake 31 | 32 | # Visual Studio 2017 cmake settings 33 | CMakeSettings.json 34 | 35 | # Visual Studio 2015 cache/options directory 36 | .vs/ 37 | 38 | Debug/ 39 | Release/ 40 | Win32/ 41 | Community-Game.dir/ 42 | build/ 43 | 44 | .out 45 | 46 | # Repertoi-e precompiled header .cpp 47 | /src/Common.cpp 48 | 49 | # Visual Studio profiler 50 | *.psess 51 | *.vsp 52 | *.vspx 53 | *.sap 54 | 55 | 56 | *.dll 57 | *.a 58 | *.zip 59 | *.7z 60 | *.exe 61 | *.o 62 | *.obj 63 | *.pdb 64 | *.ilk 65 | 66 | *.log 67 | *.iobj 68 | *.tlog 69 | *.idb 70 | *.ipch 71 | *.db 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opendb 78 | *.opensdf 79 | *.sdf 80 | *.cachefile 81 | *.VC.db 82 | *.VC.VC.opendb 83 | *.vspscc 84 | *.vssscc 85 | 86 | # Visual Studio Code 87 | .vscode/ 88 | 89 | # Builds 90 | dev_build.bat 91 | 92 | #CLion 93 | .idea 94 | cmake-build-debug/ 95 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "deps/SFML"] 2 | path = deps/SFML 3 | url = https://github.com/SFML/SFML.git 4 | branch = 2.4.x 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | before_install: 4 | - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y 5 | - sudo apt-get update -qq 6 | - sudo apt-get install g++-6 7 | - export CXX="g++-6" CC="gcc-6" 8 | 9 | before_script: 10 | - sudo apt-get install -y libudev-dev libjpeg62-dev libsndfile1-dev libglew1.5 libglew1.5-dev libfreetype6 libjpeg-turbo8 libjpeg8 libopenal-data libopenal1 libopenal-dev libxrandr2 libxrender1 libsoil1 11 | 12 | script: mkdir build && cd build && cmake .. && make -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Specify the minimum CMAKE version required 2 | cmake_minimum_required(VERSION 2.8) 3 | 4 | # Your project's name 5 | project(Community-Game) 6 | 7 | # Make your header file accessible to the compiler 8 | include_directories(include) 9 | 10 | # Add all files from the source/ folder to CMake 11 | file(GLOB_RECURSE SRC 12 | "src/*.cpp" 13 | "src/*.h" 14 | "src/*.hpp" 15 | ) 16 | 17 | # Define the executable 18 | add_executable(${PROJECT_NAME} ${SRC}) 19 | 20 | include_directories(deps/SFML/include) 21 | add_definitions(-DSFML_STATIC) 22 | if(WIN32) 23 | add_definitions( -D_CRT_SECURE_NO_WARNINGS ) 24 | endif() 25 | 26 | enable_testing() 27 | 28 | set(SFML_BUILD_DOC OFF CACHE BOOL "" FORCE) 29 | set(SFML_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) 30 | set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) 31 | set(CMAKE_BUILD_TYPE Debug) 32 | add_subdirectory(deps/SFML) 33 | 34 | #Lua include 35 | set(LUA_SOURCE_DIR "deps/Lua/src") 36 | include_directories(${LUA_SOURCE_DIR}) 37 | 38 | #LuaBridge include 39 | include_directories(deps/LuaBridge) 40 | 41 | #Lua build 42 | add_subdirectory(${LUA_SOURCE_DIR}) 43 | 44 | # Copy static files 45 | # Tired of manually copying it 46 | file(COPY res/ DESTINATION Debug/res/) 47 | 48 | target_link_libraries(${PROJECT_NAME} lualib sfml-network sfml-graphics sfml-window sfml-system sfml-audio) 49 | 50 | # Enable C++ 14 51 | if (NOT UNIX) 52 | set_target_properties(${PROJECT_NAME} PROPERTIES 53 | CXX_STANDARD 14 54 | CXX_STANDARD_REQUIRED ON 55 | CXX_EXTENSIONS OFF 56 | ) 57 | else() 58 | add_definitions(-std=c++14) 59 | endif() 60 | 61 | -------------------------------------------------------------------------------- /CODESTYLE.md: -------------------------------------------------------------------------------- 1 | # Code Style 2 | 3 | ## Naming conventions 4 | 5 | * File names: PascalCase 6 | * Folder names: underscore_lower_case 7 | 8 | * Class names: PascalCase 9 | * Struct names: PascalCase 10 | * Class members: m_camelCase 11 | 12 | * Functions: camelCase 13 | * Local variables: camelCase 14 | 15 | * Namespaces: PascalCase 16 | 17 | * Constants: SCREAMING_SNAKE_CASE 18 | 19 | ## Class Style 20 | 21 | ```C++ 22 | class Foo 23 | { 24 | public: 25 | Foo(); 26 | 27 | void foo(); 28 | void bar(); 29 | 30 | protected: 31 | void barFoo(); 32 | 33 | private: 34 | void fooBar(); 35 | 36 | int m_number; 37 | }; 38 | ``` 39 | 40 | * Public functions and members at top, protected in middle, private at bottom 41 | * Notice a space between the final class member/function and the next accessor! 42 | * Private members must be prefixed with "_m" 43 | 44 | * Initilzation lists: 45 | 46 | ```C++ 47 | Foo::Foo(int x, int y) 48 | : m_x (x) 49 | , m_y (y) { } 50 | ``` 51 | 52 | ## Namespaces 53 | 54 | * NO `using namespace std;` Your pull request will be denied if that is included. 55 | * Namespaces should be PascalCase 56 | * Nested namespaces: 57 | 58 | ```C++ 59 | namespace Foo { 60 | namespace Bar 61 | { 62 | void codeGoesHere() 63 | { 64 | 65 | } 66 | }} 67 | ``` 68 | 69 | ## Includes 70 | * All headers should have header guards (#pragma once) 71 | 72 | * Includes in files should be in the following order: 73 | * "h file corresponding to this cpp file (if applicable)", 74 | * "headers from the same component", 75 | * "headers from other components", 76 | * "headers from SFML" 77 | * "system headers and standard lib headers" 78 | 79 | * Example: 80 | ``` 81 | #include "System.h" 82 | 83 | #include "../component/Components.h" 84 | #include "../Entity.h" 85 | 86 | #include "../../maths/MathFunctions.h" 87 | #include "../../util/json.hpp" 88 | 89 | #include 90 | 91 | #include 92 | #include 93 | ``` 94 | 95 | ## Constants 96 | * Do not use C-Style "defines" for constants. 97 | * Use constexpr! It is compile-time determined just like #define is, no excuses! 98 | * Functions can be marked as "constexpr" as well. 99 | 100 | ## Functions 101 | 102 | * Primitives can be passed by value, or reference 103 | * Objects pass as either const reference (or reference), and NEVER BY VALUE 104 | * Indent style: 105 | 106 | ```C++ 107 | bool functionName(int arg1, const std::string& arg2) 108 | { 109 | if (arg1 > 5) 110 | { 111 | std::cout << arg2 << "\n"; 112 | return true; 113 | } 114 | return false; 115 | } 116 | ``` 117 | 118 | * For setter functions, use R-Value references and move scematics eg 119 | 120 | ```C++ 121 | void Foo:setString(std::string&& str) 122 | { 123 | m_string = std::move(str); 124 | } 125 | ``` 126 | 127 | ## Slash 128 | 129 | * Don't use the `\`, it can cause errors on Linux. Correct use: 130 | ```C++ 131 | #include 132 | ``` 133 | 134 | * This goes for strings as file paths too! 135 | 136 | ```C++ 137 | std::string filePath = "forward/slashes/so/it/works/cross/platform.png" 138 | ``` 139 | 140 | ## Pointers 141 | 142 | * Please prefer unique pointers to raw owning pointers 143 | ```C++ 144 | int* x = new int(5); //No! 145 | auto y = std::make_unique(5) //Yes! 146 | ``` 147 | 148 | * If you have to use "new", then you are probably doing something wrong. 149 | * Only case raw pointers are fine is if they are a pointer to a variable (So a non-owning pointer) 150 | * Class members pointers must have a "mp_" prefix 151 | 152 | ## Enums 153 | 154 | * Use enum class, not regular enums! 155 | -------------------------------------------------------------------------------- /CTestTestfile.cmake: -------------------------------------------------------------------------------- 1 | # CMake generated Testfile for 2 | # Source directory: C:/Users/faris/Community-Game 3 | # Build directory: C:/Users/faris/Community-Game 4 | # 5 | # This file includes the relevant testing commands required for 6 | # testing this directory and lists subdirectories to be tested as well. 7 | subdirs("deps/SFML") 8 | -------------------------------------------------------------------------------- /Report20170614-0125.diagsession: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/Report20170614-0125.diagsession -------------------------------------------------------------------------------- /UPDATES.md: -------------------------------------------------------------------------------- 1 | This is the list of recent announcements and updates planned. This should be a complete copy of the Discord channel #Announcements. 2 | 3 | > **Anna - Project Manager** 4 | > If any pull request are a try-out, not meant to be merged, or anything alike, please leave a comment on your request. We can't see what someone is thinking and this might cause conflicts. 5 | -------------------------------------------------------------------------------- /deps/Lua/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.6) 2 | 3 | project (lua) # project here actually means solution in premake 4 | 5 | if(WIN32) 6 | add_definitions( -D_CRT_SECURE_NO_WARNINGS ) 7 | endif() 8 | 9 | # 1. lua static library 10 | # how to rename library name? 11 | add_library (lualib STATIC lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c lauxlib.c lbaselib.c lbitlib.c lcorolib.c ldblib.c liolib.c lmathlib.c loslib.c lstrlib.c ltablib.c loadlib.c linit.c lutf8lib.c) 12 | set_target_properties(lualib PROPERTIES OUTPUT_NAME "lua") # rename the library output name 13 | 14 | # 2. lua interpreter 15 | link_directories (LUA_SOURCE_DIR) 16 | add_executable (lua lua.c) 17 | target_link_libraries (lua lualib) 18 | 19 | if(UNIX) 20 | target_link_libraries( lua m ) 21 | endif() -------------------------------------------------------------------------------- /deps/Lua/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /deps/Lua/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 28 | OPR_DIV, 29 | OPR_IDIV, 30 | OPR_BAND, OPR_BOR, OPR_BXOR, 31 | OPR_SHL, OPR_SHR, 32 | OPR_CONCAT, 33 | OPR_EQ, OPR_LT, OPR_LE, 34 | OPR_NE, OPR_GT, OPR_GE, 35 | OPR_AND, OPR_OR, 36 | OPR_NOBINOPR 37 | } BinOpr; 38 | 39 | 40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 41 | 42 | 43 | /* get (pointer to) instruction of given 'expdesc' */ 44 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) 45 | 46 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 47 | 48 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 49 | 50 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 51 | 52 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 53 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 54 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 55 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 56 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 57 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 58 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 59 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 60 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); 61 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 62 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 63 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 64 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 66 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 67 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 68 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 69 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 70 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 71 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 72 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 73 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 74 | LUAI_FUNC int luaK_jump (FuncState *fs); 75 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 76 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 77 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 78 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 79 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 80 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 81 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 82 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 83 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 84 | expdesc *v2, int line); 85 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 86 | 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /deps/Lua/src/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /deps/Lua/src/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /deps/Lua/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 24 | const TValue *p2); 25 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 26 | const TValue *p2, 27 | const char *msg); 28 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 29 | const TValue *p2); 30 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 31 | const TValue *p2); 32 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 33 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, 34 | TString *src, int line); 35 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 36 | LUAI_FUNC void luaG_traceexec (lua_State *L); 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /deps/Lua/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | /* 17 | ** Macro to check stack size and grow stack if needed. Parameters 18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the 19 | ** stack across reallocations, doing the work only when needed. 20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation 21 | ** at every check. 22 | */ 23 | #define luaD_checkstackaux(L,n,pre,pos) \ 24 | if (L->stack_last - L->top <= (n)) \ 25 | { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } 26 | 27 | /* In general, 'pre'/'pos' are empty (nothing to save) */ 28 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) 29 | 30 | 31 | 32 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 33 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 34 | 35 | 36 | /* type of protected functions, to be ran by 'runprotected' */ 37 | typedef void (*Pfunc) (lua_State *L, void *ud); 38 | 39 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 40 | const char *mode); 41 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); 45 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 46 | ptrdiff_t oldtop, ptrdiff_t ef); 47 | LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, 48 | int nres); 49 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 50 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 51 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 52 | LUAI_FUNC void luaD_inctop (lua_State *L); 53 | 54 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 55 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /deps/Lua/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** maximum number of upvalues in a closure (both C and Lua). (Value 27 | ** must fit in a VM register.) 28 | */ 29 | #define MAXUPVAL 255 30 | 31 | 32 | /* 33 | ** Upvalues for Lua closures 34 | */ 35 | struct UpVal { 36 | TValue *v; /* points to stack or to its own value */ 37 | lu_mem refcount; /* reference counter */ 38 | union { 39 | struct { /* (when open) */ 40 | UpVal *next; /* linked list */ 41 | int touched; /* mark to avoid cycles with dead threads */ 42 | } open; 43 | TValue value; /* the value (when closed) */ 44 | } u; 45 | }; 46 | 47 | #define upisopen(up) ((up)->v != &(up)->u.value) 48 | 49 | 50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 55 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 56 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 57 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 58 | int pc); 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /deps/Lua/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = { 43 | {"_G", luaopen_base}, 44 | {LUA_LOADLIBNAME, luaopen_package}, 45 | {LUA_COLIBNAME, luaopen_coroutine}, 46 | {LUA_TABLIBNAME, luaopen_table}, 47 | {LUA_IOLIBNAME, luaopen_io}, 48 | {LUA_OSLIBNAME, luaopen_os}, 49 | {LUA_STRLIBNAME, luaopen_string}, 50 | {LUA_MATHLIBNAME, luaopen_math}, 51 | {LUA_UTF8LIBNAME, luaopen_utf8}, 52 | {LUA_DBLIBNAME, luaopen_debug}, 53 | #if defined(LUA_COMPAT_BITLIB) 54 | {LUA_BITLIBNAME, luaopen_bit32}, 55 | #endif 56 | {NULL, NULL} 57 | }; 58 | 59 | 60 | LUALIB_API void luaL_openlibs (lua_State *L) { 61 | const luaL_Reg *lib; 62 | /* "require" functions from 'loadedlibs' and set results to global table */ 63 | for (lib = loadedlibs; lib->func; lib++) { 64 | luaL_requiref(L, lib->name, lib->func, 1); 65 | lua_pop(L, 1); /* remove lib */ 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /deps/Lua/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_AND = FIRST_RESERVED, TK_BREAK, 29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 32 | /* other terminal symbols */ 33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 34 | TK_SHL, TK_SHR, 35 | TK_DBCOLON, TK_EOS, 36 | TK_FLT, TK_INT, TK_NAME, TK_STRING 37 | }; 38 | 39 | /* number of reserved words */ 40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | lua_Integer i; 46 | TString *ts; 47 | } SemInfo; /* semantics information */ 48 | 49 | 50 | typedef struct Token { 51 | int token; 52 | SemInfo seminfo; 53 | } Token; 54 | 55 | 56 | /* state of the lexer plus state of the parser when shared by all 57 | functions */ 58 | typedef struct LexState { 59 | int current; /* current character (charint) */ 60 | int linenumber; /* input line counter */ 61 | int lastline; /* line of last token 'consumed' */ 62 | Token t; /* current token */ 63 | Token lookahead; /* look ahead token */ 64 | struct FuncState *fs; /* current function (parser) */ 65 | struct lua_State *L; 66 | ZIO *z; /* input stream */ 67 | Mbuffer *buff; /* buffer for tokens */ 68 | Table *h; /* to avoid collection/reuse strings */ 69 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 70 | TString *source; /* current source name */ 71 | TString *envn; /* environment variable name */ 72 | } LexState; 73 | 74 | 75 | LUAI_FUNC void luaX_init (lua_State *L); 76 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 77 | TString *source, int firstchar); 78 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 79 | LUAI_FUNC void luaX_next (LexState *ls); 80 | LUAI_FUNC int luaX_lookahead (LexState *ls); 81 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 82 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 83 | 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /deps/Lua/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** ('osize' is the old size, 'nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block 'p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ISO C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) { 50 | void *newblock; 51 | int newsize; 52 | if (*size >= limit/2) { /* cannot double it? */ 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else { 58 | newsize = (*size)*2; 59 | if (newsize < MINSIZEARRAY) 60 | newsize = MINSIZEARRAY; /* minimum size */ 61 | } 62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 63 | *size = newsize; /* update only when everything else is OK */ 64 | return newblock; 65 | } 66 | 67 | 68 | l_noret luaM_toobig (lua_State *L) { 69 | luaG_runerror(L, "memory allocation error: block too big"); 70 | } 71 | 72 | 73 | 74 | /* 75 | ** generic allocation routine. 76 | */ 77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 78 | void *newblock; 79 | global_State *g = G(L); 80 | size_t realosize = (block) ? osize : 0; 81 | lua_assert((realosize == 0) == (block == NULL)); 82 | #if defined(HARDMEMTESTS) 83 | if (nsize > realosize && g->gcrunning) 84 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 85 | #endif 86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 87 | if (newblock == NULL && nsize > 0) { 88 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ 89 | if (g->version) { /* is state fully built? */ 90 | luaC_fullgc(L, 1); /* try to free some memory... */ 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 92 | } 93 | if (newblock == NULL) 94 | luaD_throw(L, LUA_ERRMEM); 95 | } 96 | lua_assert((nsize == 0) == (newblock == NULL)); 97 | g->GCdebt = (g->GCdebt + nsize) - realosize; 98 | return newblock; 99 | } 100 | 101 | -------------------------------------------------------------------------------- /deps/Lua/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19 | ** each element has size 'e'. In case of arithmetic overflow of the 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22 | ** 23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24 | ** comparison avoids a runtime comparison when overflow cannot occur. 25 | ** The compiler should be able to optimize the real test by itself, but 26 | ** when it does it, it may give a warning about "comparison is always 27 | ** false due to limited range of data type"; the +1 tricks the compiler, 28 | ** avoiding this warning but also this optimization.) 29 | */ 30 | #define luaM_reallocv(L,b,on,n,e) \ 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32 | ? luaM_toobig(L) : cast_void(0)) , \ 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34 | 35 | /* 36 | ** Arrays of chars do not need any test 37 | */ 38 | #define luaM_reallocvchar(L,b,on,n) \ 39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40 | 41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44 | 45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47 | #define luaM_newvector(L,n,t) \ 48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49 | 50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51 | 52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53 | if ((nelems)+1 > (size)) \ 54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55 | 56 | #define luaM_reallocvector(L, v,oldn,n,t) \ 57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58 | 59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 60 | 61 | /* not to be called directly */ 62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 63 | size_t size); 64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 65 | size_t size_elem, int limit, 66 | const char *what); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /deps/Lua/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 21 | "MOVE", 22 | "LOADK", 23 | "LOADKX", 24 | "LOADBOOL", 25 | "LOADNIL", 26 | "GETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "SETTABUP", 30 | "SETUPVAL", 31 | "SETTABLE", 32 | "NEWTABLE", 33 | "SELF", 34 | "ADD", 35 | "SUB", 36 | "MUL", 37 | "MOD", 38 | "POW", 39 | "DIV", 40 | "IDIV", 41 | "BAND", 42 | "BOR", 43 | "BXOR", 44 | "SHL", 45 | "SHR", 46 | "UNM", 47 | "BNOT", 48 | "NOT", 49 | "LEN", 50 | "CONCAT", 51 | "JMP", 52 | "EQ", 53 | "LT", 54 | "LE", 55 | "TEST", 56 | "TESTSET", 57 | "CALL", 58 | "TAILCALL", 59 | "RETURN", 60 | "FORLOOP", 61 | "FORPREP", 62 | "TFORCALL", 63 | "TFORLOOP", 64 | "SETLIST", 65 | "CLOSURE", 66 | "VARARG", 67 | "EXTRAARG", 68 | NULL 69 | }; 70 | 71 | 72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 73 | 74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 75 | /* T A B C mode opcode */ 76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ 96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ 97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ 98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ 99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ 100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ 103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 123 | }; 124 | 125 | -------------------------------------------------------------------------------- /deps/Lua/src/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /deps/Lua/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 16 | 17 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 18 | #define sizeudata(u) sizeludata((u)->len) 19 | 20 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 21 | (sizeof(s)/sizeof(char))-1)) 22 | 23 | 24 | /* 25 | ** test whether a string is a reserved word 26 | */ 27 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 28 | 29 | 30 | /* 31 | ** equality for short strings, which are always internalized 32 | */ 33 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 34 | 35 | 36 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 37 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 40 | LUAI_FUNC void luaS_clearcache (global_State *g); 41 | LUAI_FUNC void luaS_init (lua_State *L); 42 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 43 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 44 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 45 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 46 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /deps/Lua/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->i_key.nk.next) 16 | 17 | 18 | /* 'const' to avoid wrong writings that can mess up field 'next' */ 19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) 20 | 21 | /* 22 | ** writable version of 'gkey'; allows updates to individual fields, 23 | ** but not to the whole (which has incompatible type) 24 | */ 25 | #define wgkey(n) (&(n)->i_key.nk) 26 | 27 | #define invalidateTMcache(t) ((t)->flags = 0) 28 | 29 | 30 | /* true when 't' is using 'dummynode' as its hash part */ 31 | #define isdummy(t) ((t)->lastfree == NULL) 32 | 33 | 34 | /* allocated size for hash nodes */ 35 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) 36 | 37 | 38 | /* returns the key, given the value of a table entry */ 39 | #define keyfromval(v) \ 40 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 41 | 42 | 43 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 44 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 45 | TValue *value); 46 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); 47 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 48 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 49 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 50 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 51 | LUAI_FUNC Table *luaH_new (lua_State *L); 52 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 53 | unsigned int nhsize); 54 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 55 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 56 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 57 | LUAI_FUNC int luaH_getn (Table *t); 58 | 59 | 60 | #if defined(LUA_DEBUG) 61 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 62 | LUAI_FUNC int luaH_isdummy (const Table *t); 63 | #endif 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /deps/Lua/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | 55 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 56 | 57 | 58 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); 59 | 60 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 61 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 62 | TMS event); 63 | LUAI_FUNC void luaT_init (lua_State *L); 64 | 65 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 66 | const TValue *p2, TValue *p3, int hasres); 67 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 68 | StkId res, TMS event); 69 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 70 | StkId res, TMS event); 71 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 72 | const TValue *p2, TMS event); 73 | 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /deps/Lua/src/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /deps/Lua/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* version suffix for environment variable names */ 15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR 16 | 17 | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); 19 | 20 | #define LUA_COLIBNAME "coroutine" 21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 22 | 23 | #define LUA_TABLIBNAME "table" 24 | LUAMOD_API int (luaopen_table) (lua_State *L); 25 | 26 | #define LUA_IOLIBNAME "io" 27 | LUAMOD_API int (luaopen_io) (lua_State *L); 28 | 29 | #define LUA_OSLIBNAME "os" 30 | LUAMOD_API int (luaopen_os) (lua_State *L); 31 | 32 | #define LUA_STRLIBNAME "string" 33 | LUAMOD_API int (luaopen_string) (lua_State *L); 34 | 35 | #define LUA_UTF8LIBNAME "utf8" 36 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 37 | 38 | #define LUA_BITLIBNAME "bit32" 39 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 40 | 41 | #define LUA_MATHLIBNAME "math" 42 | LUAMOD_API int (luaopen_math) (lua_State *L); 43 | 44 | #define LUA_DBLIBNAME "debug" 45 | LUAMOD_API int (luaopen_debug) (lua_State *L); 46 | 47 | #define LUA_LOADLIBNAME "package" 48 | LUAMOD_API int (luaopen_package) (lua_State *L); 49 | 50 | 51 | /* open all previous libraries */ 52 | LUALIB_API void (luaL_openlibs) (lua_State *L); 53 | 54 | 55 | 56 | #if !defined(lua_assert) 57 | #define lua_assert(x) ((void)0) 58 | #endif 59 | 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /deps/Lua/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.45 2015/09/08 15:41:05 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 27 | 28 | /* dump one chunk; from ldump.c */ 29 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 30 | void* data, int strip); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /deps/Lua/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | /* 31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers 32 | ** by flooring them (instead of raising an error if they are not 33 | ** integral values) 34 | */ 35 | #if !defined(LUA_FLOORN2I) 36 | #define LUA_FLOORN2I 0 37 | #endif 38 | 39 | 40 | #define tonumber(o,n) \ 41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 42 | 43 | #define tointeger(o,i) \ 44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) 45 | 46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 47 | 48 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 49 | 50 | 51 | /* 52 | ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, 53 | ** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, 54 | ** return 0 (meaning it will have to check metamethod) with 'slot' 55 | ** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). 56 | ** 'f' is the raw get function to use. 57 | */ 58 | #define luaV_fastget(L,t,k,slot,f) \ 59 | (!ttistable(t) \ 60 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ 61 | : (slot = f(hvalue(t), k), /* else, do raw access */ \ 62 | !ttisnil(slot))) /* result not nil? */ 63 | 64 | /* 65 | ** standard implementation for 'gettable' 66 | */ 67 | #define luaV_gettable(L,t,k,v) { const TValue *slot; \ 68 | if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ 69 | else luaV_finishget(L,t,k,v,slot); } 70 | 71 | 72 | /* 73 | ** Fast track for set table. If 't' is a table and 't[k]' is not nil, 74 | ** call GC barrier, do a raw 't[k]=v', and return true; otherwise, 75 | ** return false with 'slot' equal to NULL (if 't' is not a table) or 76 | ** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro 77 | ** returns true, there is no need to 'invalidateTMcache', because the 78 | ** call is not creating a new entry. 79 | */ 80 | #define luaV_fastset(L,t,k,slot,f,v) \ 81 | (!ttistable(t) \ 82 | ? (slot = NULL, 0) \ 83 | : (slot = f(hvalue(t), k), \ 84 | ttisnil(slot) ? 0 \ 85 | : (luaC_barrierback(L, hvalue(t), v), \ 86 | setobj2t(L, cast(TValue *,slot), v), \ 87 | 1))) 88 | 89 | 90 | #define luaV_settable(L,t,k,v) { const TValue *slot; \ 91 | if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ 92 | luaV_finishset(L,t,k,v,slot); } 93 | 94 | 95 | 96 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 97 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 98 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 99 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 100 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); 101 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, 102 | StkId val, const TValue *slot); 103 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 104 | StkId val, const TValue *slot); 105 | LUAI_FUNC void luaV_finishOp (lua_State *L); 106 | LUAI_FUNC void luaV_execute (lua_State *L); 107 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 108 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); 109 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 110 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 111 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /deps/Lua/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.37 2015/09/08 15:41:05 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /deps/Lua/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.31 2015/09/08 15:41:05 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 50 | 51 | 52 | 53 | /* --------- Private Part ------------------ */ 54 | 55 | struct Zio { 56 | size_t n; /* bytes still unread */ 57 | const char *p; /* current position in buffer */ 58 | lua_Reader reader; /* reader function */ 59 | void *data; /* additional data */ 60 | lua_State *L; /* Lua state (for reader) */ 61 | }; 62 | 63 | 64 | LUAI_FUNC int luaZ_fill (ZIO *z); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /deps/LuaBridge/LuaBridge.h: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | /* 3 | https://github.com/vinniefalco/LuaBridge 4 | 5 | Copyright 2012, Vinnie Falco 6 | Copyright 2007, Nathan Reed 7 | 8 | License: The MIT License (http://www.opensource.org/licenses/mit-license.php) 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | */ 28 | //============================================================================== 29 | 30 | #ifndef LUABRIDGE_LUABRIDGE_HEADER 31 | #define LUABRIDGE_LUABRIDGE_HEADER 32 | 33 | // All #include dependencies are listed here 34 | // instead of in the individual header files. 35 | // 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #define LUABRIDGE_MAJOR_VERSION 2 43 | #define LUABRIDGE_MINOR_VERSION 0 44 | #define LUABRIDGE_VERSION 200 45 | 46 | namespace luabridge 47 | { 48 | 49 | // Forward declaration 50 | // 51 | template 52 | struct Stack; 53 | 54 | #include "detail/LuaHelpers.h" 55 | 56 | #include "detail/TypeTraits.h" 57 | #include "detail/TypeList.h" 58 | #include "detail/FuncTraits.h" 59 | #include "detail/Constructor.h" 60 | #include "detail/Stack.h" 61 | #include "detail/ClassInfo.h" 62 | 63 | class LuaRef; 64 | 65 | #include "detail/LuaException.h" 66 | #include "detail/LuaRef.h" 67 | #include "detail/Iterator.h" 68 | 69 | //------------------------------------------------------------------------------ 70 | /** 71 | security options. 72 | */ 73 | class Security 74 | { 75 | public: 76 | static bool hideMetatables() 77 | { 78 | return getSettings().hideMetatables; 79 | } 80 | 81 | static void setHideMetatables(bool shouldHide) 82 | { 83 | getSettings().hideMetatables = shouldHide; 84 | } 85 | 86 | private: 87 | struct Settings 88 | { 89 | Settings() : hideMetatables(true) 90 | { 91 | } 92 | 93 | bool hideMetatables; 94 | }; 95 | 96 | static Settings& getSettings() 97 | { 98 | static Settings settings; 99 | return settings; 100 | } 101 | }; 102 | 103 | #include "detail/Userdata.h" 104 | #include "detail/CFunctions.h" 105 | #include "detail/Namespace.h" 106 | 107 | //------------------------------------------------------------------------------ 108 | /** 109 | Push an object onto the Lua stack. 110 | */ 111 | template 112 | inline void push(lua_State* L, T t) 113 | { 114 | Stack ::push(L, t); 115 | } 116 | 117 | //------------------------------------------------------------------------------ 118 | /** 119 | Set a global value in the lua_State. 120 | 121 | @note This works on any type specialized by `Stack`, including `LuaRef` and 122 | its table proxies. 123 | */ 124 | template 125 | inline void setGlobal(lua_State* L, T t, char const* name) 126 | { 127 | push(L, t); 128 | lua_setglobal(L, name); 129 | } 130 | 131 | //------------------------------------------------------------------------------ 132 | /** 133 | Change whether or not metatables are hidden (on by default). 134 | */ 135 | inline void setHideMetatables(bool shouldHide) 136 | { 137 | Security::setHideMetatables(shouldHide); 138 | } 139 | 140 | } 141 | 142 | #endif -------------------------------------------------------------------------------- /deps/LuaBridge/detail/ClassInfo.h: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | /* 3 | https://github.com/vinniefalco/LuaBridge 4 | 5 | Copyright 2012, Vinnie Falco 6 | 7 | License: The MIT License (http://www.opensource.org/licenses/mit-license.php) 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | */ 27 | //============================================================================== 28 | 29 | /** Unique Lua registry keys for a class. 30 | 31 | Each registered class inserts three keys into the registry, whose 32 | values are the corresponding static, class, and const metatables. This 33 | allows a quick and reliable lookup for a metatable from a template type. 34 | */ 35 | template 36 | class ClassInfo 37 | { 38 | public: 39 | /** Get the key for the static table. 40 | 41 | The static table holds the static data members, static properties, and 42 | static member functions for a class. 43 | */ 44 | static void const* getStaticKey () 45 | { 46 | static char value; 47 | return &value; 48 | } 49 | 50 | /** Get the key for the class table. 51 | 52 | The class table holds the data members, properties, and member functions 53 | of a class. Read-only data and properties, and const member functions are 54 | also placed here (to save a lookup in the const table). 55 | */ 56 | static void const* getClassKey () 57 | { 58 | static char value; 59 | return &value; 60 | } 61 | 62 | /** Get the key for the const table. 63 | 64 | The const table holds read-only data members and properties, and const 65 | member functions of a class. 66 | */ 67 | static void const* getConstKey () 68 | { 69 | static char value; 70 | return &value; 71 | } 72 | }; 73 | 74 | -------------------------------------------------------------------------------- /deps/LuaBridge/detail/Iterator.h: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | /* 3 | https://github.com/vinniefalco/LuaBridge 4 | 5 | Copyright 2012, Vinnie Falco 6 | 7 | License: The MIT License (http://www.opensource.org/licenses/mit-license.php) 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | */ 27 | //============================================================================== 28 | 29 | /** Allows table iteration. 30 | */ 31 | class Iterator 32 | { 33 | private: 34 | lua_State* m_L; 35 | LuaRef m_table; 36 | LuaRef m_key; 37 | LuaRef m_value; 38 | 39 | void next () 40 | { 41 | m_table.push(m_L); 42 | m_key.push (m_L); 43 | if (lua_next (m_L, -2)) 44 | { 45 | m_value.pop (m_L); 46 | m_key.pop (m_L); 47 | } 48 | else 49 | { 50 | m_key = Nil(); 51 | m_value = Nil(); 52 | } 53 | lua_pop(m_L, 1); 54 | } 55 | 56 | public: 57 | explicit Iterator (LuaRef table) 58 | : m_L (table.state ()) 59 | , m_table (table) 60 | , m_key (table.state ()) // m_key is nil 61 | , m_value (table.state ()) // m_value is nil 62 | { 63 | next (); // get the first (key, value) pair from table 64 | } 65 | 66 | lua_State* state () const 67 | { 68 | return m_L; 69 | } 70 | 71 | LuaRef operator* () const 72 | { 73 | return m_value; 74 | } 75 | 76 | LuaRef operator-> () const 77 | { 78 | return m_value; 79 | } 80 | 81 | Iterator& operator++ () 82 | { 83 | if (isNil()) 84 | { 85 | // if the iterator reaches the end, do nothing 86 | return *this; 87 | } 88 | else 89 | { 90 | next(); 91 | return *this; 92 | } 93 | } 94 | 95 | inline bool isNil () const 96 | { 97 | return m_key.isNil (); 98 | } 99 | 100 | inline LuaRef key () const 101 | { 102 | return m_key; 103 | } 104 | 105 | inline LuaRef value () const 106 | { 107 | return m_value; 108 | } 109 | 110 | private: 111 | // Don't use postfix increment, it is less efficient 112 | Iterator operator++ (int); 113 | }; 114 | 115 | -------------------------------------------------------------------------------- /deps/LuaBridge/detail/LuaException.h: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | /* 3 | https://github.com/vinniefalco/LuaBridge 4 | 5 | Copyright 2012, Vinnie Falco 6 | Copyright 2008, Nigel Atkinson 7 | 8 | License: The MIT License (http://www.opensource.org/licenses/mit-license.php) 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | */ 28 | //============================================================================== 29 | 30 | class LuaException : public std::exception 31 | { 32 | private: 33 | lua_State* m_L; 34 | std::string m_what; 35 | 36 | public: 37 | //---------------------------------------------------------------------------- 38 | /** 39 | Construct a LuaException after a lua_pcall(). 40 | */ 41 | LuaException (lua_State* L, int /*code*/) 42 | : m_L (L) 43 | { 44 | whatFromStack (); 45 | } 46 | 47 | //---------------------------------------------------------------------------- 48 | 49 | LuaException (lua_State *L, 50 | char const*, 51 | char const*, 52 | long) 53 | : m_L (L) 54 | { 55 | whatFromStack (); 56 | } 57 | 58 | //---------------------------------------------------------------------------- 59 | 60 | ~LuaException() throw () 61 | { 62 | } 63 | 64 | //---------------------------------------------------------------------------- 65 | 66 | char const* what() const throw () 67 | { 68 | return m_what.c_str(); 69 | } 70 | 71 | //============================================================================ 72 | /** 73 | Throw an exception. 74 | 75 | This centralizes all the exceptions thrown, so that we can set 76 | breakpoints before the stack is unwound, or otherwise customize the 77 | behavior. 78 | */ 79 | template 80 | static void Throw (Exception e) 81 | { 82 | throw e; 83 | } 84 | 85 | //---------------------------------------------------------------------------- 86 | /** 87 | Wrapper for lua_pcall that throws. 88 | */ 89 | static void pcall (lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0) 90 | { 91 | int code = lua_pcall (L, nargs, nresults, msgh); 92 | 93 | if (code != LUABRIDGE_LUA_OK) 94 | Throw (LuaException (L, code)); 95 | } 96 | 97 | //---------------------------------------------------------------------------- 98 | 99 | protected: 100 | void whatFromStack () 101 | { 102 | if (lua_gettop (m_L) > 0) 103 | { 104 | char const* s = lua_tostring (m_L, -1); 105 | m_what = s ? s : ""; 106 | } 107 | else 108 | { 109 | // stack is empty 110 | m_what = "missing error"; 111 | } 112 | } 113 | }; 114 | -------------------------------------------------------------------------------- /deps/LuaBridge/detail/TypeTraits.h: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | /* 3 | https://github.com/vinniefalco/LuaBridge 4 | 5 | Copyright 2012, Vinnie Falco 6 | 7 | License: The MIT License (http://www.opensource.org/licenses/mit-license.php) 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | */ 27 | //============================================================================== 28 | 29 | #ifndef LUABRIDGE_TYPEINFO_HEADER 30 | #define LUABRIDGE_TYPEINFO_HEADER 31 | 32 | //------------------------------------------------------------------------------ 33 | /** 34 | Container traits. 35 | 36 | Unspecialized ContainerTraits has the isNotContainer typedef for SFINAE. 37 | All user defined containers must supply an appropriate specialization for 38 | ContinerTraits (without the typedef isNotContainer). The containers that 39 | come with LuaBridge also come with the appropriate ContainerTraits 40 | specialization. See the corresponding declaration for details. 41 | 42 | A specialization of ContainerTraits for some generic type ContainerType 43 | looks like this: 44 | 45 | template 46 | struct ContainerTraits > 47 | { 48 | typedef typename T Type; 49 | 50 | static T* get (ContainerType const& c) 51 | { 52 | return c.get (); // Implementation-dependent on ContainerType 53 | } 54 | }; 55 | */ 56 | template 57 | struct ContainerTraits 58 | { 59 | typedef bool isNotContainer; 60 | }; 61 | 62 | //------------------------------------------------------------------------------ 63 | /** 64 | Type traits. 65 | 66 | Specializations return information about a type. 67 | */ 68 | struct TypeTraits 69 | { 70 | /** Determine if type T is a container. 71 | 72 | To be considered a container, there must be a specialization of 73 | ContainerTraits with the required fields. 74 | */ 75 | template 76 | class isContainer 77 | { 78 | private: 79 | typedef char yes[1]; // sizeof (yes) == 1 80 | typedef char no [2]; // sizeof (no) == 2 81 | 82 | template 83 | static no& test (typename C::isNotContainer*); 84 | 85 | template 86 | static yes& test (...); 87 | 88 | public: 89 | static const bool value = sizeof (test >(0)) == sizeof (yes); 90 | }; 91 | 92 | /** Determine if T is const qualified. 93 | */ 94 | /** @{ */ 95 | template 96 | struct isConst 97 | { 98 | static bool const value = false; 99 | }; 100 | 101 | template 102 | struct isConst 103 | { 104 | static bool const value = true; 105 | }; 106 | /** @} */ 107 | 108 | /** Remove the const qualifier from T. 109 | */ 110 | /** @{ */ 111 | template 112 | struct removeConst 113 | { 114 | typedef T Type; 115 | }; 116 | 117 | template 118 | struct removeConst 119 | { 120 | typedef T Type; 121 | }; 122 | /**@}*/ 123 | }; 124 | 125 | #endif 126 | -------------------------------------------------------------------------------- /deps/LuaBridge/detail/dump.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | std::string dumpLuaState(lua_State *L) { 5 | std::stringstream ostr; 6 | int i; 7 | int top = lua_gettop(L); 8 | ostr << "top=" << top << ":\n"; 9 | for (i = 1; i <= top; ++i) { 10 | int t = lua_type(L, i); 11 | switch(t) { 12 | case LUA_TSTRING: 13 | ostr << " " << i << ": '" << lua_tostring(L, i) << "'\n"; 14 | break; 15 | case LUA_TBOOLEAN: 16 | ostr << " " << i << ": " << 17 | (lua_toboolean(L, i) ? "true" : "false") << "\n"; 18 | break; 19 | case LUA_TNUMBER: 20 | ostr << " " << i << ": " << lua_tonumber(L, i) << "\n"; 21 | break; 22 | default: 23 | ostr << " " << i << ": TYPE=" << lua_typename(L, t) << "\n"; 24 | break; 25 | } 26 | } 27 | return ostr.str(); 28 | } 29 | -------------------------------------------------------------------------------- /res/entities/Lantern.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Lantern", 3 | "components": [ 4 | { 5 | "componentType": "Sprite", 6 | "src": "entity/dinolantern", 7 | "flipOnVelocity": false, 8 | "origin": { 9 | "x": 16, 10 | "y": 48 11 | }, 12 | "rect": { 13 | "left": 0, 14 | "top": 0, 15 | "width": 32, 16 | "height": 48 17 | } 18 | }, 19 | { 20 | "componentType": "Light", 21 | "intensity": 80, 22 | "color": [ 255, 190, 255 ] 23 | }, 24 | { 25 | "componentType": "Physics", 26 | "collisionBox": { 27 | "x": 0, 28 | "y": 0, 29 | "width": 0, 30 | "height": 0 31 | }, 32 | "movespeed": 0 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /res/entities/Player.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Player", 3 | "components": [ 4 | { 5 | "componentType": "Script", 6 | "src": "player.lua" 7 | }, 8 | { 9 | "componentType": "Sprite", 10 | "src": "entity/player_modelDefault", 11 | "origin": { 12 | "x": 15, 13 | "y": 32 14 | }, 15 | "rect": { 16 | "left": 0, 17 | "top": 0, 18 | "width": 32, 19 | "height": 64 20 | }, 21 | "animations": [ 22 | { 23 | "name": "idle", 24 | "positionX": 0, 25 | "positionY": 0, 26 | "stride": 32, 27 | "length": 8, 28 | "fps": 7 29 | }, 30 | { 31 | "name": "run", 32 | "positionX": 0, 33 | "positionY": 64, 34 | "stride": 32, 35 | "length": 8, 36 | "fps": 14 37 | } 38 | ] 39 | }, 40 | { 41 | "componentType": "Physics", 42 | "sortOffset": 32, 43 | "collisionBox": { 44 | "x": -16, 45 | "y": 0, 46 | "width": 32, 47 | "height": 32 48 | }, 49 | "movespeed": 150 50 | }, 51 | { 52 | "componentType": "Stats", 53 | "base": { 54 | "max_health": 150, 55 | "health_regen": 5, 56 | "armor": 0, 57 | "magic_resist": 0 58 | } 59 | } 60 | ] 61 | } 62 | -------------------------------------------------------------------------------- /res/entities/Projectile.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Projectile", 3 | "components": [ 4 | { 5 | "componentType": "Sprite", 6 | "src": "entity/temp/k1", 7 | "origin": { 8 | "x": 8, 9 | "y": 8 10 | }, 11 | "rect": { 12 | "left": 0, 13 | "top": 0, 14 | "width": 16, 15 | "height": 16 16 | } 17 | }, 18 | { 19 | "componentType": "Move", 20 | "type": "Sine-wave" 21 | }, 22 | { 23 | "componentType": "Physics", 24 | "collisionBox": { 25 | "x": 0, 26 | "y": 0, 27 | "width": 0, 28 | "height": 0 29 | }, 30 | "movespeed": 0 31 | } 32 | ] 33 | } -------------------------------------------------------------------------------- /res/entities/enemy/Zombie.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Zombie", 3 | "components": [ 4 | 5 | { 6 | "componentType": "Hostile" 7 | }, 8 | { 9 | "componentType": "Sprite", 10 | "src": "entity/enemy/zombie", 11 | "origin": { 12 | "x": 16, 13 | "y": 48 14 | }, 15 | "rect": { 16 | "left": 0, 17 | "top": 0, 18 | "width": 64, 19 | "height": 64 20 | }, 21 | "animations": [ 22 | { 23 | "name": "idle", 24 | "positionX": 16, 25 | "positionY": 0, 26 | "stride": 64, 27 | "length": 4, 28 | "fps": 7 29 | }, 30 | { 31 | "name": "run", 32 | "positionX": 16, 33 | "positionY": 64, 34 | "stride": 64, 35 | "length": 4, 36 | "fps": 10 37 | } 38 | ] 39 | }, 40 | { 41 | "componentType": "Physics", 42 | "sortOffset": 16, 43 | "collisionBox": { 44 | "x": -16, 45 | "y": -16, 46 | "width": 32, 47 | "height": 32 48 | }, 49 | "movespeed": 45 50 | }, 51 | { 52 | "componentType": "Stats", 53 | "base": { 54 | "max_health": 50, 55 | "health_regen": 0, 56 | "armor": 10, 57 | "magic_resist": 0 58 | } 59 | } 60 | ] 61 | } -------------------------------------------------------------------------------- /res/entities/scripts/player.lua: -------------------------------------------------------------------------------- 1 |  2 | -- This method gets called from the code constantly. Each entity script should start here 3 | function update(entity, app) 4 | 5 | if entity:isMoving() then -- If the player is moving 6 | entity:setAnimation("run") -- set animation to run 7 | else 8 | entity:setAnimation("idle") -- otherwise set to idle 9 | end 10 | 11 | -- Declare variables that 12 | -- will control player movement 13 | xa = 0 14 | ya = 0 15 | 16 | -- Increment xa, ya depending on keyboard input 17 | if app:keyPressed("MOVE_UP") then 18 | ya = ya - 1 19 | end 20 | if app:keyPressed("MOVE_DOWN") then 21 | ya = ya + 1 22 | end 23 | if app:keyPressed("MOVE_LEFT") then 24 | xa = xa - 1 25 | end 26 | if app:keyPressed("MOVE_RIGHT") then 27 | xa = xa + 1 28 | end 29 | 30 | -- If atleast one of the vars (xa or ya) is not 0 31 | -- then move the player 32 | if xa ~= 0 or ya ~= 0 then 33 | entity:setVelocity(xa, ya) -- setVelocity changes 'moving' to true (this controls animation) 34 | else 35 | entity:setMoving(false) -- Otherwise set player 'moving' to false (this controls animation) 36 | end 37 | 38 | -- Check if mousePos.x < screen width 39 | -- This makes player 'look' at mouse 40 | if app:mouseX() < app:screenWidth() / 2 then 41 | entity:setSpriteFlip(true) 42 | else 43 | entity:setSpriteFlip(false) 44 | end 45 | 46 | -- Face the player the direction he is moving 47 | if xa > 0 then 48 | entity:setSpriteFlip(false) 49 | end 50 | if xa < 0 then 51 | entity:setSpriteFlip(true) 52 | end 53 | end -------------------------------------------------------------------------------- /res/entities/scripts/zombie.lua: -------------------------------------------------------------------------------- 1 | -- Some vars that get used in the AI 2 | local track_distance = 16 3 | local target = false 4 | 5 | -- Holding a LuaPathFind helper 6 | local pathFind = nil 7 | 8 | -- Used in calculating the path, making sure it's 9 | -- only updated ~0.6 times a second 10 | local g_Timer = 0 11 | 12 | function update(entity, app) 13 | -- x, y vars to hold entity position 14 | -- during this update cycle 15 | local x = entity:getX() 16 | local y = entity:getY() 17 | 18 | -- Init pathfind only once instead of every frame 19 | if pathFind == nil then 20 | pathFind = entity:getPathFind() 21 | end 22 | 23 | if entity:isMoving() then -- If the zombie is moving 24 | entity:setAnimation("run") -- set animation to run 25 | else 26 | entity:setAnimation("idle") -- otherwise set to idle 27 | end 28 | 29 | if target then 30 | -- Update the path to the player 31 | if g_Timer == 0 then 32 | pathFind:updatePath(x, y, entity:getPlayer():getX(), entity:getPlayer():getY()) 33 | end 34 | 35 | -- Maintain the timer 36 | g_Timer = g_Timer + 1 37 | if g_Timer > 1 then 38 | g_Timer = 0 39 | end 40 | 41 | if pathFind:getPathSize() > track_distance * 1.5 then 42 | g_Timer = 0 43 | target = false 44 | end 45 | 46 | -- Get the next step of the path 47 | local nextX = pathFind:getNextX() 48 | local nextY = pathFind:getNextY() 49 | 50 | -- Declare variables that 51 | -- will control zombie movement 52 | local xa = 0 53 | local ya = 0 54 | 55 | -- Increment xa, ya depending on path 56 | if (entity:getX() < nextX) then 57 | xa = xa + 1 58 | end 59 | if (entity:getX() > nextX) then 60 | xa = xa - 1 61 | end 62 | if (entity:getY() < nextY) then 63 | ya = ya + 1 64 | end 65 | if (entity:getY() > nextY) then 66 | ya = ya - 1 67 | end 68 | 69 | -- If atleast one of the vars (xa or ya) is not 0 70 | -- then move the zombie 71 | if xa ~= 0 or ya ~= 0 then 72 | entity:setVelocity(xa, ya) -- setVelocity changes 'moving' to true (this controls animation) 73 | else 74 | entity:setMoving(false) -- Otherwise set zombie 'moving' to false (this controls animation) 75 | end 76 | 77 | if xa > 0 then 78 | entity:setSpriteFlip(false) 79 | end 80 | if xa < 0 then 81 | entity:setSpriteFlip(true) 82 | end 83 | else 84 | --@TODO: Tile flood instead of this 85 | if distance(entity:getPlayer():getX(), entity:getPlayer():getY(), x, y) <= track_distance * Const.TileSize then 86 | target = true 87 | else 88 | entity:setMoving(false) 89 | end 90 | end 91 | end 92 | 93 | -- Function used to determine distance between 2 points 94 | -- in this script it is used to calculate distance between zombie and player 95 | function distance(x1, y1, x2, y2) 96 | dx = x1 - x2 97 | dy = y1 - y2 98 | 99 | return math.sqrt(dx * dx + dy * dy) 100 | end -------------------------------------------------------------------------------- /res/fonts/Inconsolata-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/fonts/Inconsolata-Bold.ttf -------------------------------------------------------------------------------- /res/fonts/Inconsolata-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/fonts/Inconsolata-Regular.ttf -------------------------------------------------------------------------------- /res/fonts/SourceCodePro-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/fonts/SourceCodePro-Regular.ttf -------------------------------------------------------------------------------- /res/settings/Control List.txt: -------------------------------------------------------------------------------- 1 | The second value must be one of; 2 | A 3 | B 4 | C 5 | D 6 | E 7 | F 8 | G 9 | H 10 | I 11 | J 12 | K 13 | L 14 | M 15 | N 16 | O 17 | P 18 | Q 19 | R 20 | S 21 | T 22 | U 23 | V 24 | W 25 | X 26 | Y 27 | Z 28 | Num0 29 | Num1 30 | Num2 31 | Num3 32 | Num4 33 | Num5 34 | Num6 35 | Num7 36 | Num8 37 | Num9 38 | Escape 39 | LControl 40 | LShift 41 | LAlt 42 | LSystem 43 | RControl 44 | RShift 45 | RAlt 46 | RSystem 47 | Menu 48 | LBracket 49 | RBracket 50 | SemiColon 51 | Comma 52 | Period 53 | Quote 54 | Slash 55 | BackSlash 56 | Tilde 57 | Equal 58 | Dash 59 | Space 60 | Return 61 | BackSpace 62 | Tab 63 | PageUp 64 | PageDown 65 | End 66 | Home 67 | Insert 68 | Delete 69 | Add 70 | Subtract 71 | Multiply 72 | Divide 73 | Left 74 | Right 75 | Up 76 | Down 77 | Numpad0 78 | Numpad1 79 | Numpad2 80 | Numpad3 81 | Numpad4 82 | Numpad5 83 | Numpad6 84 | Numpad7 85 | Numpad8 86 | Numpad9 87 | F1 88 | F2 89 | F3 90 | F4 91 | F5 92 | F6 93 | F7 94 | F8 95 | F9 96 | F10 97 | F11 98 | F12 99 | F13 100 | F14 101 | F15 102 | Pause -------------------------------------------------------------------------------- /res/settings/Controls.json: -------------------------------------------------------------------------------- 1 | { 2 | "MOVE_UP": "W", 3 | "MOVE_DOWN": "S", 4 | "MOVE_LEFT": "A", 5 | "MOVE_RIGHT": "D", 6 | "SHOW_CONSOLE": "F1" 7 | } 8 | -------------------------------------------------------------------------------- /res/shaders/transparency.frag: -------------------------------------------------------------------------------- 1 | uniform float u_Opacity; 2 | 3 | uniform sampler2D u_Texture; 4 | 5 | void main() 6 | { 7 | vec4 color = texture2D(u_Texture, gl_TexCoord[0].xy); 8 | color.a *= u_Opacity; 9 | gl_FragColor = color; 10 | } -------------------------------------------------------------------------------- /res/sound/music/Challenge.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/music/Challenge.ogg -------------------------------------------------------------------------------- /res/sound/music/LunarStreams.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/music/LunarStreams.ogg -------------------------------------------------------------------------------- /res/sound/music/Menu.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/music/Menu.ogg -------------------------------------------------------------------------------- /res/sound/sfx/explosion.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/sfx/explosion.ogg -------------------------------------------------------------------------------- /res/sound/sfx/hit.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/sfx/hit.ogg -------------------------------------------------------------------------------- /res/sound/sfx/jump.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/sfx/jump.ogg -------------------------------------------------------------------------------- /res/sound/sfx/pickup.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/sfx/pickup.ogg -------------------------------------------------------------------------------- /res/sound/sfx/powerup.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/sfx/powerup.ogg -------------------------------------------------------------------------------- /res/sound/sfx/shoot.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/sound/sfx/shoot.ogg -------------------------------------------------------------------------------- /res/textures/LICENSE.md: -------------------------------------------------------------------------------- 1 | The copyright for the following textures (even the modified versions) belong to RagingRabbit. 2 | You are not allowed to use or redistribute them in any way other than with this project. 3 | 4 | Copyrighted textures: 5 | player_modelDefault.png 6 | zombie.png 7 | rose.png 8 | tile_atlas.png 9 | 10 | For more info write an e-mail to ragingrab@gmail.com 11 | -------------------------------------------------------------------------------- /res/textures/entity/dinolantern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/entity/dinolantern.png -------------------------------------------------------------------------------- /res/textures/entity/enemy/rose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/entity/enemy/rose.png -------------------------------------------------------------------------------- /res/textures/entity/enemy/zombie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/entity/enemy/zombie.png -------------------------------------------------------------------------------- /res/textures/entity/player_modelDefault.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/entity/player_modelDefault.png -------------------------------------------------------------------------------- /res/textures/entity/temp/k1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/entity/temp/k1.png -------------------------------------------------------------------------------- /res/textures/gui/button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/gui/button.png -------------------------------------------------------------------------------- /res/textures/gui/dev/menu_background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/gui/dev/menu_background.png -------------------------------------------------------------------------------- /res/textures/gui/dev/window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/gui/dev/window.png -------------------------------------------------------------------------------- /res/textures/gui/slider.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/gui/slider.png -------------------------------------------------------------------------------- /res/textures/items/branch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/items/branch.png -------------------------------------------------------------------------------- /res/textures/items/gunne.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/items/gunne.png -------------------------------------------------------------------------------- /res/textures/items/luger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/items/luger.png -------------------------------------------------------------------------------- /res/textures/items/rusty_dagger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/items/rusty_dagger.png -------------------------------------------------------------------------------- /res/textures/items/slingshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/items/slingshot.png -------------------------------------------------------------------------------- /res/textures/tile_atlas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HopsonCommunity/Community-Game/95165eaaa2a9675de9a7b7bd9444fd448f4757a0/res/textures/tile_atlas.png -------------------------------------------------------------------------------- /res/tiles/Void.json: -------------------------------------------------------------------------------- 1 | { 2 | "Passable": true, 3 | "Breakable": false, 4 | "Textures": [ [ 0, 0 ] ] 5 | } -------------------------------------------------------------------------------- /res/tiles/dungeon/BrickFloor.json: -------------------------------------------------------------------------------- 1 | { 2 | "Passable": true, 3 | "Breakable": false, 4 | "Textures": [ [ 1, 4 ] ] 5 | } -------------------------------------------------------------------------------- /res/tiles/dungeon/BrickWall.json: -------------------------------------------------------------------------------- 1 | { 2 | "Passable": false, 3 | "Breakable": false, 4 | "Textures": [ 5 | [ 0, 6 ], 6 | [ 0, 5 ] 7 | ], 8 | "CollisionBox": { 9 | "x": 0, 10 | "y": 0, 11 | "width": 32, 12 | "height": 32 13 | } 14 | } -------------------------------------------------------------------------------- /res/worlds/default/obligatory.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "min": 1, 4 | "max": 2, 5 | "wall": 0, 6 | "floor": 0, 7 | "entities": [ 8 | ] 9 | } 10 | ] 11 | -------------------------------------------------------------------------------- /res/worlds/default/random.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "chance" : 20, 4 | "wall" : 1, 5 | "floor" : 2, 6 | "entities" : [ 7 | { 8 | "entity" : "Lantern", 9 | "placement" : "center", 10 | "min" : 2, 11 | "max" : 3 12 | } 13 | ] 14 | }, 15 | { 16 | "chance" : 100, 17 | "wall" : 1, 18 | "floor" : 2, 19 | "entities" : [] 20 | } 21 | ] 22 | -------------------------------------------------------------------------------- /res/worlds/default/world.json: -------------------------------------------------------------------------------- 1 | { 2 | "seed" : "random", 3 | "width" : 100, 4 | "height" : 100, 5 | "minLeafSize": 15, 6 | "maxLeafSize" : 30, 7 | "minHallWidth" : 2, 8 | "maxHallWidth" : 2, 9 | "defaultFloor" : 2, 10 | "defaultWall" : 1 11 | } 12 | -------------------------------------------------------------------------------- /src/Common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /// Includes 4 | 5 | #include "Types.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | /// Constants 25 | 26 | constexpr float TILE_SIZE = 32.f; 27 | 28 | constexpr int32 LIGHT_MAX_LIGHTLEVEL = 25; 29 | constexpr float LIGHT_ABSOLUTE = 80.f; 30 | 31 | /// Macros 32 | 33 | #define BIT(x) (1 << x) 34 | 35 | #define METHOD(x) std::bind(x, this) 36 | #define _METHOD(x) std::bind(x, this, std::placeholders::_1) 37 | 38 | // Only usable in TileMap 39 | #define FOR_EACH_TILE(action) for (uint x = 0; x < width; x++) { for (uint y = 0; y < height; y++) { action; } } -------------------------------------------------------------------------------- /src/Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | typedef int8_t int8; 6 | typedef int16_t int16; 7 | typedef int32_t int32; 8 | typedef int64_t int64; 9 | 10 | typedef uint8_t uint8; 11 | typedef uint16_t uint16; 12 | typedef uint32_t uint32; 13 | typedef uint64_t uint64; 14 | 15 | typedef uint32 uint; 16 | typedef uint8 byte; -------------------------------------------------------------------------------- /src/app/Application.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "WindowSettings.h" 6 | #include "states/StateBase.h" 7 | #include "input/InputScheme.h" 8 | #include "input/Input.h" 9 | 10 | #include "../util/Timestep.h" 11 | #include "../sound/Music.h" 12 | 13 | #include "../resources/ResourceHolder.h" 14 | 15 | #include "../graphics/Label.h" 16 | #include "../graphics/gui/Panel.h" 17 | 18 | #include "../debug/DebugConsole.h" 19 | 20 | #include "../events/Events.h" 21 | 22 | constexpr bool VSYNC_ENABLED = true; 23 | constexpr bool VSYNC_DISABLED = false; 24 | 25 | class Application : public Events::IEventListener 26 | { 27 | private: 28 | static Application* s_instance; 29 | public: 30 | Application(std::string&& name, const WindowSettings& settings); 31 | 32 | // Running 33 | void start(); 34 | void onEvent(Events::Event& event) override; 35 | void render(); 36 | 37 | // States 38 | void pushState(std::unique_ptr state); 39 | void popState(); 40 | GUI::Panel* pushPanel(GUI::Panel* panel); 41 | void popPanel(GUI::Panel* panel); 42 | 43 | // Input 44 | Input::Input* getInputManager() const { return m_inputManager; } 45 | float mouseX() { return (float)m_inputManager->getMouse().x; } 46 | float mouseY() { return (float)m_inputManager->getMouse().y; } 47 | 48 | // Getters 49 | Vec2 uiMousePos() { return Vec2(m_inputManager->getMouse(m_uiView)); } 50 | bool inputPressed(std::string action) { return m_inputManager->isInput(action) && m_window.hasFocus(); } 51 | uint getFPS() const { return m_framesPerSecond; } 52 | uint getUPS() const { return m_updatesPerSecond; } 53 | float getFrameTime() const { return m_frameTime; } 54 | float screenWidth() { return (float)getWindow().getSize().x; } 55 | float screenHeight() { return (float)getWindow().getSize().y; } 56 | const WindowSettings& getSettings() const { return m_windowSettings; } 57 | ResourceHolder& getResources() { return *m_resources; } 58 | sf::RenderWindow& getWindow() { return m_window; } 59 | bool isConsoleActive() { return m_console->isActive(); } 60 | const sf::Font& getFont(const std::string& name) { return m_resources->fonts.get(name); } 61 | const sf::Texture& getTexture(const std::string& name) { return m_resources->textures.get(name); } 62 | 63 | //Setters 64 | void setVSync(bool enabled); 65 | void setConsoleActive(bool active) { m_console->setActive(active); } 66 | 67 | static Application& get() { return *s_instance; } 68 | 69 | private: 70 | std::string m_title; 71 | 72 | uint frames = 0; 73 | uint m_framesPerSecond, m_updatesPerSecond; 74 | float m_frameTime; 75 | 76 | sf::View m_uiView; 77 | Graphics::Label* m_fpsLabel; 78 | Graphics::Label* m_frameTimeLabel; 79 | 80 | Debug::Console* m_console; 81 | 82 | Input::Input* m_inputManager; 83 | 84 | ResourceHolder* m_resources; 85 | Sound::Music m_backgroundMusic; 86 | 87 | WindowSettings m_windowSettings; 88 | sf::RenderWindow m_window; 89 | std::vector> m_states; 90 | std::vector m_panels; 91 | }; 92 | -------------------------------------------------------------------------------- /src/app/Cursor.cpp: -------------------------------------------------------------------------------- 1 | #include "Cursor.h" 2 | 3 | #ifdef SFML_SYSTEM_WINDOWS 4 | 5 | StandardCursor::StandardCursor(const TYPE t) 6 | { 7 | switch (t) 8 | { 9 | case StandardCursor::WAIT: 10 | Cursor = LoadCursor(NULL, IDC_WAIT); 11 | break; 12 | case StandardCursor::HAND: 13 | Cursor = LoadCursor(NULL, IDC_HAND); 14 | break; 15 | case StandardCursor::NORMAL: 16 | Cursor = LoadCursor(NULL, IDC_ARROW); 17 | break; 18 | case StandardCursor::TEXT: 19 | Cursor = LoadCursor(NULL, IDC_IBEAM); 20 | break; 21 | case StandardCursor::SIZENWSE: 22 | Cursor = LoadCursor(NULL, IDC_SIZENWSE); 23 | break; 24 | } 25 | } 26 | 27 | void StandardCursor::set(const sf::WindowHandle& aWindowHandle) const 28 | { 29 | SetClassLongPtr(aWindowHandle, GCLP_HCURSOR, reinterpret_cast(Cursor)); 30 | } 31 | 32 | StandardCursor::~StandardCursor() 33 | { 34 | } 35 | 36 | #elif defined(SFML_SYSTEM_LINUX) 37 | 38 | StandardCursor::StandardCursor(const TYPE t) 39 | { 40 | display = XOpenDisplay(NULL); 41 | 42 | switch (t) 43 | { 44 | case StandardCursor::WAIT: 45 | Cursor = XCreateFontCursor(display, XC_watch); 46 | break; 47 | case StandardCursor::HAND: 48 | Cursor = XCreateFontCursor(display, XC_hand1); 49 | break; 50 | case StandardCursor::NORMAL: 51 | Cursor = XCreateFontCursor(display, XC_left_ptr); 52 | break; 53 | case StandardCursor::TEXT: 54 | Cursor = XCreateFontCursor(display, XC_xterm); 55 | break; 56 | case StandardCursor::SIZENWSE: 57 | Cursor = XCreateFontCursor(display, XC_bottom_right_corner); 58 | break; 59 | // For more cursor options on Linux go here: 60 | // http://www.tronche.com/gui/x/xlib/appendix/b/ 61 | } 62 | } 63 | 64 | void StandardCursor::set(const sf::WindowHandle& aWindowHandle) const 65 | { 66 | XDefineCursor(display, aWindowHandle, Cursor); 67 | XFlush(display); 68 | } 69 | 70 | StandardCursor::~StandardCursor() 71 | { 72 | XFreeCursor(display, Cursor); 73 | delete display; 74 | display = NULL; 75 | } 76 | 77 | #else 78 | #error This OS is not yet supported by the cursor library. 79 | #endif -------------------------------------------------------------------------------- /src/app/Cursor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #ifdef SFML_SYSTEM_WINDOWS 7 | #include 8 | #elif defined(SFML_SYSTEM_LINUX) 9 | #include 10 | #include 11 | #else 12 | #error This OS is not yet supported by the cursor library. 13 | #endif 14 | 15 | class StandardCursor 16 | { 17 | private: 18 | #ifdef SFML_SYSTEM_WINDOWS 19 | HCURSOR Cursor; 20 | #else 21 | XID Cursor; 22 | Display* display; 23 | #endif 24 | public: 25 | enum TYPE 26 | { 27 | WAIT, TEXT, NORMAL, HAND, SIZENWSE 28 | }; 29 | 30 | StandardCursor(const TYPE t); 31 | ~StandardCursor(); 32 | 33 | void set(const sf::WindowHandle& aWindowHandle) const; 34 | }; 35 | -------------------------------------------------------------------------------- /src/app/WindowSettings.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Types.h" 4 | 5 | struct WindowSettings 6 | { 7 | uint width; 8 | uint height; 9 | bool fullscreen; 10 | bool vsync; 11 | }; 12 | -------------------------------------------------------------------------------- /src/app/input/Input.cpp: -------------------------------------------------------------------------------- 1 | #include "Input.h" 2 | 3 | #include "../Application.h" 4 | 5 | /* 6 | How input works. 7 | 8 | Input "actions" are defined in the 'Controls.json' file. 9 | Actions are just std::strings that are mapped to a sf::Keyboard:Key enum. 10 | We query actions by using isInput(actionName) where actionName is the name 11 | of the action as defined in 'Controls.json'. 12 | */ 13 | 14 | namespace Input 15 | { 16 | Input::Input(InputScheme* inputScheme, Events::IEventListener* eventCallback, sf::RenderWindow* window) 17 | : m_inputScheme(inputScheme) 18 | , m_eventCallback(eventCallback) 19 | , m_window(window) 20 | { 21 | m_keyState.fill(false); 22 | } 23 | 24 | bool Input::isInput(std::string action) 25 | { 26 | return sf::Keyboard::isKeyPressed(m_inputScheme->getKeyForAction(action)); 27 | } 28 | 29 | void Input::update() 30 | { 31 | Vec2i mouse = sf::Mouse::getPosition(*m_window); 32 | if (m_mouse != mouse) 33 | { 34 | m_mouse = mouse; 35 | 36 | Events::Event&& event = Events::MouseMovedEvent((float)mouse.x, (float)mouse.y, sf::Mouse::isButtonPressed(sf::Mouse::Left)); 37 | m_eventCallback->onEvent(event); 38 | } 39 | 40 | sf::Event e; 41 | while (m_window->pollEvent(e)) 42 | { 43 | if (e.type == sf::Event::Closed) 44 | m_window->close(); 45 | 46 | if (e.type == sf::Event::KeyPressed && e.key.code > -1) 47 | { 48 | int32 repeat = m_keyState[e.key.code] ? 1 : 0; 49 | int32 mods = 0; 50 | if (e.key.shift) 51 | mods = mods | (int32)Events::KeyModifier::SHIFT; 52 | if (e.key.control) 53 | mods = mods | (int32)Events::KeyModifier::CONTROL; 54 | if (e.key.alt) 55 | mods = mods | (int32)Events::KeyModifier::ALT; 56 | 57 | if (e.key.code == sf::Keyboard::Tilde && !repeat) 58 | Application::get().setConsoleActive(!Application::get().isConsoleActive()); 59 | 60 | Events::Event&& event = Events::KeyPressedEvent(e.key.code, repeat, mods); 61 | m_eventCallback->onEvent(event); 62 | 63 | m_keyState[e.key.code] = true; 64 | } 65 | 66 | if (e.type == sf::Event::KeyReleased && e.key.code > -1) 67 | { 68 | Events::Event&& event = Events::KeyReleasedEvent(e.key.code); 69 | m_eventCallback->onEvent(event); 70 | 71 | m_keyState[e.key.code] = false; 72 | } 73 | 74 | if (e.type == sf::Event::MouseButtonPressed) 75 | { 76 | Events::Event&& event = Events::MousePressedEvent(e.mouseButton.button, (float)e.mouseButton.x, (float)e.mouseButton.y); 77 | m_eventCallback->onEvent(event); 78 | } 79 | 80 | if (e.type == sf::Event::MouseButtonReleased) 81 | { 82 | Events::Event&& event = Events::MouseReleasedEvent(e.mouseButton.button, (float)e.mouseButton.x, (float)e.mouseButton.y); 83 | m_eventCallback->onEvent(event); 84 | } 85 | } 86 | } 87 | 88 | Vec2i Input::getMouse() 89 | { 90 | return m_mouse; 91 | } 92 | 93 | Vec2i Input::getMouse(const sf::View& view) 94 | { 95 | return Vec2i(m_window->mapPixelToCoords(sf::Mouse::getPosition(*m_window), view)); 96 | } 97 | } -------------------------------------------------------------------------------- /src/app/input/Input.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "InputScheme.h" 4 | 5 | #include "../../events/Events.h" 6 | 7 | #include 8 | 9 | namespace Input 10 | { 11 | class Input 12 | { 13 | public: 14 | Input(InputScheme* inputScheme, Events::IEventListener* eventCallback, sf::RenderWindow* window); 15 | 16 | bool isInput(std::string action); 17 | 18 | void update(); 19 | 20 | Vec2i getMouse(); 21 | Vec2i getMouse(const sf::View& view); 22 | private: 23 | InputScheme* m_inputScheme; 24 | 25 | Vec2i m_mouse; 26 | 27 | std::array m_keyState; 28 | 29 | sf::RenderWindow* m_window; 30 | Events::IEventListener* m_eventCallback; 31 | }; 32 | } -------------------------------------------------------------------------------- /src/app/input/InputScheme.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../Types.h" 4 | #include "../../util/FileUtil.h" 5 | #include "../../util/json.hpp" 6 | 7 | #include 8 | 9 | namespace Input 10 | { 11 | class InputScheme 12 | { 13 | public: 14 | InputScheme(std::string fileName); 15 | 16 | sf::Keyboard::Key getKeyForAction(std::string actionName); 17 | private: 18 | void initMap(); 19 | 20 | std::map m_nameMap; 21 | std::map m_actionMap; 22 | }; 23 | } -------------------------------------------------------------------------------- /src/app/states/StateBase.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Application; 4 | 5 | #include "../../util/Timestep.h" 6 | 7 | #include "../../events/Events.h" 8 | 9 | namespace sf 10 | { 11 | class RenderWindow; 12 | class Event; 13 | } 14 | 15 | namespace State 16 | { 17 | class Base 18 | { 19 | public: 20 | Base(Application* app) 21 | : m_pApplication(app) 22 | { } 23 | 24 | virtual ~Base() = default; 25 | 26 | Base(Base& other) = delete; 27 | Base& operator= (Base& other) = delete; 28 | 29 | virtual void onEvent(Events::Event& event) = 0; 30 | virtual void update(const Timestep& ts) = 0; 31 | virtual void render(sf::RenderWindow& window) = 0; 32 | virtual void tick() = 0; 33 | 34 | protected: 35 | Application* m_pApplication; 36 | }; 37 | } 38 | -------------------------------------------------------------------------------- /src/app/states/StateMenu.cpp: -------------------------------------------------------------------------------- 1 | #include "StateMenu.h" 2 | 3 | #include "StatePlaying.h" 4 | #include "../Application.h" 5 | 6 | #include "../../Common.h" 7 | #include "../../util/Random.h" 8 | 9 | namespace State 10 | { 11 | Menu::Menu(Application* app, sf::RenderWindow* window) 12 | : Base(app) 13 | , m_backGround(Vec2((float)app->screenWidth(), (float)app->screenHeight())) 14 | , m_devBuildLabel("", app->getFont("SourceCodePro-Regular"), 30, Graphics::Label::Alignment::LEFT) 15 | , m_singleplayerClicked(false) 16 | { 17 | using namespace Graphics; 18 | using namespace GUI; 19 | 20 | m_backGround.setTexture(&app->getTexture("gui/dev/menu_background")); 21 | 22 | m_devBuildLabel.setPosition(Vec2(18.f, static_cast(app->screenHeight()) - 40.f)); 23 | m_devBuildLabel.getText().setFillColor(sf::Color(255, 235, 0, 255)); 24 | 25 | Label* singleplayer = new Label("Singleplayer", app->getFont("SourceCodePro-Regular"), 18); 26 | Label* multiplayer = new Label("Multiplayer", app->getFont("SourceCodePro-Regular"), 18); 27 | Label* credits = new Label("Credits", app->getFont("SourceCodePro-Regular"), 18); 28 | 29 | m_panel = new Panel(); 30 | m_panel->add(new Button(singleplayer, { app->screenWidth() / 2 - 75, app->screenHeight() / 2 - 30, 150, 50 }, [&]() { m_singleplayerClicked = true; })); 31 | m_panel->add(new Button(multiplayer, { app->screenWidth() / 2 - 75, app->screenHeight() / 2 + 45, 150, 50 }, [&]() {})); 32 | m_panel->add(new Button(credits, { app->screenWidth() / 2 - 75, app->screenHeight() / 2 + 120, 150, 50 }, [&]() {})); 33 | } 34 | 35 | void Menu::onEvent(Events::Event& event) 36 | { 37 | } 38 | 39 | void Menu::update(const Timestep& ts) 40 | { 41 | std::stringstream m; 42 | m << Random::getRandomChar() << " DEV BUILD " << Random::getRandomChar(); 43 | m_devBuildLabel.setText(m.str()); 44 | 45 | if (m_singleplayerClicked) 46 | { 47 | Application::get().popPanel(m_panel); 48 | Application::get().pushState(std::make_unique(&Application::get(), &Application::get().getWindow())); 49 | } 50 | } 51 | 52 | void Menu::render(sf::RenderWindow& window) 53 | { 54 | window.draw(m_backGround); 55 | m_devBuildLabel.render(window); 56 | } 57 | 58 | void Menu::tick() 59 | { 60 | } 61 | } -------------------------------------------------------------------------------- /src/app/states/StateMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "StateBase.h" 4 | 5 | #include "../../graphics/gui/Panel.h" 6 | #include "../../graphics/gui/Button.h" 7 | #include "../../graphics/gui/Slider.h" 8 | 9 | #include "../../graphics/gui/Window.h" 10 | 11 | namespace State 12 | { 13 | class Menu : public Base 14 | { 15 | public: 16 | Menu(Application* app, sf::RenderWindow* window); 17 | 18 | void onEvent(Events::Event& event) override; 19 | void update(const Timestep& ts) override; 20 | void render(sf::RenderWindow& window) override; 21 | void tick() override; 22 | 23 | private: 24 | sf::RectangleShape m_backGround; 25 | Graphics::Label m_devBuildLabel; 26 | GUI::Panel* m_panel; 27 | 28 | bool m_singleplayerClicked; 29 | }; 30 | } -------------------------------------------------------------------------------- /src/app/states/StatePlaying.cpp: -------------------------------------------------------------------------------- 1 | #include "StatePlaying.h" 2 | 3 | #include "../../level/tile/TileDatabase.h" 4 | #include "../../level/gen/WorldGenerator.h" 5 | 6 | #include "../Application.h" 7 | #include "../../Common.h" 8 | #include "../../util/Random.h" 9 | #include "../../util/Log.h" 10 | #include "../../entity/EntityFactory.h" 11 | #include "../../components/PhysicsComponent.h" 12 | #include "../../components/LightComponent.h" 13 | 14 | namespace State 15 | { 16 | Playing* Playing::s_instance; 17 | Entity::EntityFactory* Playing::entityFactory; 18 | Item::ItemFactory* Playing::itemFactory; 19 | 20 | Playing::Playing(Application* app, sf::RenderWindow* window) 21 | : Base(app) 22 | , m_level(WORLD_SIZE, WORLD_SIZE) 23 | { 24 | s_instance = this; 25 | 26 | entityFactory = new Entity::EntityFactory(); 27 | 28 | std::unique_ptr player = entityFactory->createEntity("Player.json"); 29 | LOG_INFO("Player ID: ", player.get()->getID()); 30 | 31 | m_level.player = player.get(); 32 | m_level.addEntity(std::move(player)); 33 | 34 | m_level.player->getComponent()->pos = { m_level.player_spawn.x * 32.0f, m_level.player_spawn.y * 32.0f }; 35 | 36 | std::unique_ptr lantern2 = entityFactory->createEntity("Lantern.json"); 37 | lantern2->getComponent()->pos = { m_level.player_spawn.x * 32.0f - 20, m_level.player_spawn.y * 32.0f - 50 }; 38 | 39 | m_level.addEntity(std::move(lantern2)); 40 | 41 | std::unique_ptr zombie = entityFactory->createEntity("enemy/Zombie.json"); 42 | zombie->getComponent()->pos = { m_level.player_spawn.x * 32.0f, m_level.player_spawn.y * 32.0f }; 43 | m_level.addEntity(std::move(zombie)); 44 | } 45 | 46 | void Playing::onEvent(Events::Event& event) 47 | { 48 | //if (event.type == sf::Event::Resized) 49 | // m_level.windowResize({ static_cast(event.size.width), static_cast(event.size.height) }); 50 | } 51 | 52 | void Playing::update(const Timestep& ts) 53 | { 54 | m_level.update(ts); 55 | } 56 | 57 | void Playing::render(sf::RenderWindow& window) 58 | { 59 | m_level.render(window); 60 | } 61 | 62 | void Playing::tick() 63 | { 64 | } 65 | bool Playing::isTilePassable(byte layer, uint x, uint y) 66 | { 67 | Level::TileNode* tile = getLevel().getTiles().getTile(layer, x, y); 68 | return tile ? Level::TileDatabase::get().getTileData(byte(tile->id)).flags & int32(Level::TileFlags::PASSABLE) : true; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/app/states/StatePlaying.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../level/Level.h" 4 | #include "../../entity/EntityFactory.h" 5 | #include "StateBase.h" 6 | #include "../../item/ItemFactory.h" 7 | 8 | namespace State 9 | { 10 | class Playing : public Base 11 | { 12 | private: 13 | static Playing* s_instance; 14 | 15 | public: 16 | static Entity::EntityFactory* entityFactory; 17 | static Item::ItemFactory* itemFactory; 18 | 19 | public: 20 | Playing(Application* app, sf::RenderWindow* window); 21 | 22 | void onEvent(Events::Event& event) override; 23 | void update(const Timestep& ts) override; 24 | void render(sf::RenderWindow& window) override; 25 | void tick() override; 26 | 27 | bool isTilePassable(byte layer, uint x, uint y); 28 | 29 | Level::Level& getLevel() { return m_level; } 30 | 31 | static Playing& get() { return *s_instance; } 32 | 33 | private: 34 | Level::Level m_level; 35 | 36 | void singleplayerCallback() {} 37 | void multiplayerCallback() {} 38 | void creditsCallback() {} 39 | }; 40 | } 41 | -------------------------------------------------------------------------------- /src/components/Component.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "../maths/maths.h" 6 | #include "../util/json.hpp" 7 | 8 | namespace Components 9 | { 10 | namespace ComponentID 11 | { 12 | enum ComponentID : uint 13 | { 14 | AI = BIT(0), 15 | Inventory = BIT(1), 16 | Life = BIT(2), 17 | Light = BIT(3), 18 | Physics = BIT(4), 19 | Sprite = BIT(5), 20 | Stats = BIT(6), 21 | Script = BIT(7) 22 | }; 23 | } 24 | 25 | class Component 26 | { 27 | public: 28 | Component() {}; 29 | virtual ~Component() {}; 30 | 31 | virtual std::unique_ptr clone() { return std::make_unique(*this); }; 32 | }; 33 | } -------------------------------------------------------------------------------- /src/components/Components.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // All components headers 4 | 5 | #include "PhysicsComponent.h" 6 | #include "LifeComponent.h" 7 | #include "SpriteComponent.h" 8 | #include "StatsComponent.h" 9 | #include "LightComponent.h" 10 | #include "ScriptComponent.h" 11 | #include "InventoryComponent.h" 12 | -------------------------------------------------------------------------------- /src/components/InventoryComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "InventoryComponent.h" 2 | 3 | namespace Components 4 | { 5 | InventoryComponent::InventoryComponent() 6 | { 7 | } 8 | 9 | InventoryComponent::InventoryComponent(nlohmann::json json) 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/components/InventoryComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Component.h" 4 | 5 | #include "../item/Item.h" 6 | 7 | namespace Components 8 | { 9 | class InventoryComponent : public Component 10 | { 11 | public: 12 | std::vector> items; 13 | 14 | public: 15 | InventoryComponent(); 16 | InventoryComponent(nlohmann::json); 17 | 18 | static const uint ID = ComponentID::Inventory; 19 | }; 20 | } 21 | -------------------------------------------------------------------------------- /src/components/LifeComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "LifeComponent.h" 2 | 3 | #include "../util/Log.h" 4 | 5 | namespace Components 6 | { 7 | LifeComponent::LifeComponent(float life) 8 | : life(life) 9 | {} 10 | 11 | LifeComponent::LifeComponent(nlohmann::json json) 12 | : life(20) 13 | { 14 | if (json.find("duration") == json.end()) 15 | LOG_WARN("[JSON] Entity with LifeComponent no duration! Set to default (", life, ")"); 16 | else 17 | life = json["duration"].get(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/components/LifeComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Component.h" 4 | 5 | namespace Components 6 | { 7 | class LifeComponent : public Component 8 | { 9 | public: 10 | float life; 11 | bool done; 12 | 13 | public: 14 | LifeComponent(float life); 15 | LifeComponent(nlohmann::json json); 16 | 17 | std::unique_ptr clone() override { return std::make_unique(*this); } 18 | 19 | static const uint ID = ComponentID::Life; 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /src/components/LightComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "LightComponent.h" 2 | 3 | namespace Components 4 | { 5 | LightComponent::LightComponent(nlohmann::json json) 6 | : added(false) 7 | { 8 | sf::Color light_color = sf::Color::White; 9 | if (json.find("color") == json.end()) 10 | LOG_WARN("[JSON] Entity with LightComponent but without color! Setting to ", light_color," as default."); 11 | else 12 | { 13 | std::vector rgb = json["color"]; 14 | light_color = sf::Color(rgb[0], rgb[1], rgb[2], 255); 15 | } 16 | 17 | byte intensity = 30; 18 | 19 | if (json.find("intensity") == json.end()) 20 | LOG_WARN("[JSON] Entity with LightComponent but without intensity! Setting to \"", intensity ,"\" as default."); 21 | else 22 | intensity = json["intensity"]; 23 | 24 | light = Level::StaticLight{0, 0, light_color, intensity }; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/components/LightComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Component.h" 4 | 5 | #include "../maths/Color.h" 6 | #include "../level/LightMap.h" 7 | 8 | namespace Components 9 | { 10 | class LightComponent : public Component 11 | { 12 | public: 13 | Level::StaticLight light; 14 | bool added; 15 | 16 | public: 17 | LightComponent(nlohmann::json json); 18 | 19 | std::unique_ptr clone() override { return std::make_unique(*this); } 20 | 21 | static const uint ID = ComponentID::Light; 22 | }; 23 | } 24 | -------------------------------------------------------------------------------- /src/components/PhysicsComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "PhysicsComponent.h" 2 | 3 | #include "../util/Log.h" 4 | 5 | namespace Components 6 | { 7 | void PhysicsComponent::setVelocity(float xa, float ya) 8 | { 9 | if (xa == 0 && ya == 0) 10 | return; 11 | velocity = { xa, ya }; 12 | 13 | normalize(velocity); 14 | velocity *= movespeed; 15 | 16 | moving = true; 17 | } 18 | 19 | PhysicsComponent::PhysicsComponent(const FloatRectangle& obj) 20 | : bounds(obj), moving(false), movespeed(0), sortOffset(0) 21 | { 22 | } 23 | 24 | PhysicsComponent::PhysicsComponent(nlohmann::json json) 25 | : moving(false), movespeed(0) 26 | { 27 | bounds = { {0, 0}, {0, 0} }; 28 | if (json.find("collisionBox") == json.end()) 29 | LOG_WARN("[JSON] Entity with PhysicsComponent but without FloatRectangle! Setting to ", bounds, " as default."); 30 | else 31 | bounds = { { json["collisionBox"]["x"], json["collisionBox"]["y"] },{ json["collisionBox"]["width"], json["collisionBox"]["height"] } }; 32 | 33 | if (json.find("movespeed") == json.end()) 34 | LOG_WARN("[JSON] Entity with PhysicsComponent but without movespeed! Setting to \"", movespeed, "\" as default."); 35 | else 36 | movespeed = json["movespeed"]; 37 | 38 | if (json.find("sortOffset") != json.end()) 39 | sortOffset = json["sortOffset"]; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/components/PhysicsComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Component.h" 4 | 5 | #include "../maths/Rectangle.h" 6 | 7 | namespace Components 8 | { 9 | class PhysicsComponent : public Component 10 | { 11 | public: 12 | FloatRectangle bounds; 13 | Vec2 pos, velocity; 14 | bool moving; 15 | float movespeed; 16 | float sortOffset; 17 | 18 | public: 19 | PhysicsComponent(const FloatRectangle& hitbox); 20 | PhysicsComponent(nlohmann::json json); 21 | 22 | void setVelocity(float xa, float ya); 23 | 24 | std::unique_ptr clone() override { return std::make_unique(*this); } 25 | 26 | static const uint ID = ComponentID::Physics; 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /src/components/ScriptComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Component.h" 4 | 5 | #include 6 | extern "C" 7 | { 8 | #include "lua.h" 9 | #include "lauxlib.h" 10 | #include "lualib.h" 11 | } 12 | 13 | #include "../entity/AStar.h" 14 | 15 | namespace Entity 16 | { 17 | class Entity; 18 | } 19 | 20 | namespace Components 21 | { 22 | class LuaPathFind 23 | { 24 | public: 25 | LuaPathFind() 26 | : m_nextStepX(0) 27 | , m_nextStepY(0) 28 | , m_pathSize(0) 29 | { 30 | } 31 | 32 | void updatePath(float x, float y, float tx, float ty); 33 | 34 | float getNextX() const { return m_nextStepX; } 35 | float getNextY() const { return m_nextStepY; } 36 | float getPathSize() const { return m_pathSize; } 37 | 38 | private: 39 | float m_nextStepX, m_nextStepY, m_pathSize; 40 | std::vector m_path; 41 | }; 42 | 43 | class LuaEntityHandle 44 | { 45 | public: 46 | LuaEntityHandle(Entity::Entity* e) 47 | : e(e) 48 | {} 49 | 50 | // Lua bindings 51 | bool isMoving() const; 52 | void setMoving(bool moving) const; 53 | void setAnimation(const std::string& name) const; 54 | void setVelocity(float x, float y) const; 55 | void setSpriteFlip(bool flipped) const; 56 | float getX() const; 57 | float getY() const; 58 | LuaEntityHandle getPlayer() const; 59 | LuaPathFind getPathFind() const; 60 | 61 | private: 62 | Entity::Entity* e; 63 | }; 64 | 65 | class ScriptComponent : public Component 66 | { 67 | public: 68 | luabridge::lua_State* LuaState; 69 | 70 | ScriptComponent(nlohmann::json json); 71 | 72 | std::unique_ptr clone() override { return std::make_unique(*this); } 73 | 74 | static const uint ID = ComponentID::Script; 75 | }; 76 | } 77 | -------------------------------------------------------------------------------- /src/components/SpriteComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "SpriteComponent.h" 2 | 3 | #include "../app/Application.h" 4 | #include "../util/Log.h" 5 | 6 | namespace Components 7 | { 8 | SpriteComponent::SpriteComponent(const sf::Sprite& sprite) 9 | : sprite(sprite), flipX(false), animated(false) 10 | { 11 | } 12 | 13 | template 14 | void handleJSON(nlohmann::json& animation, uint index, const std::string& varName, T& var) 15 | { 16 | if (animation.find(varName) == animation.end()) 17 | LOG_WARN("[JSON] Entity with Animated sprite but animation with index(", index, ") without \"", varName, "\"! Set to default.."); 18 | else 19 | var = animation[varName].get(); 20 | } 21 | 22 | SpriteComponent::SpriteComponent(nlohmann::json json) 23 | : flipX(false), animated(true) 24 | { 25 | if (json.find("src") == json.end()) 26 | LOG_ERROR("[JSON] Entity with SpriteComponent but without sprite path!"); 27 | else 28 | sprite = sf::Sprite(Application::get().getTexture(json["src"]), sf::IntRect(json["rect"]["left"], json["rect"]["top"], json["rect"]["width"], json["rect"]["height"])); 29 | 30 | sprite.setOrigin({ json["origin"]["x"], json["origin"]["y"] }); 31 | 32 | if (json.find("animations") == json.end()) 33 | return; 34 | 35 | std::vector animations = json["animations"]; 36 | if (animations.empty()) 37 | animated = false; 38 | 39 | for (uint i = 0; i < animations.size(); i++) 40 | { 41 | nlohmann::json animation = animations[i]; 42 | 43 | std::string name = "$$animation_name"; 44 | uint posX = 0, posY = 0, stride = 32, length = 2, fps = 14; 45 | 46 | handleJSON(animation, i, "name", name); 47 | handleJSON(animation, i, "positionX", posX); 48 | handleJSON(animation, i, "positionY", posY); 49 | handleJSON(animation, i, "stride", stride); 50 | handleJSON(animation, i, "length", length); 51 | handleJSON(animation, i, "fps", fps); 52 | 53 | animator.addAnimation(name, posX, posY, stride, length, fps); 54 | } 55 | }; 56 | } 57 | -------------------------------------------------------------------------------- /src/components/SpriteComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Component.h" 4 | #include "../graphics/Animator.h" 5 | 6 | #include 7 | 8 | namespace Components 9 | { 10 | class SpriteComponent : public Component 11 | { 12 | public: 13 | sf::Sprite sprite; 14 | bool flipX; 15 | bool animated; 16 | Graphics::Animator animator; 17 | 18 | public: 19 | SpriteComponent(const sf::Sprite& sprite); 20 | SpriteComponent(nlohmann::json json); 21 | 22 | std::unique_ptr clone() override { return std::make_unique(*this); } 23 | 24 | static const uint ID = ComponentID::Sprite; 25 | }; 26 | } 27 | -------------------------------------------------------------------------------- /src/components/StatsComponent.cpp: -------------------------------------------------------------------------------- 1 | #include "StatsComponent.h" 2 | 3 | namespace Components 4 | { 5 | StatsComponent::StatsComponent() 6 | { 7 | } 8 | 9 | StatsComponent::StatsComponent(nlohmann::json json) 10 | { 11 | if (json.find("base") != json.end()) 12 | { 13 | active_buffs.push_back(std::make_shared(-1, json["base"]["max_health"], json["base"]["health_regen"])); 14 | active_buffs.push_back(std::make_shared(-1, json["base"]["armor"], json["base"]["magic_resist"])); 15 | 16 | stats.health = json["base"]["max_health"]; 17 | } 18 | }; 19 | 20 | #pragma region STATUS_EFFECT 21 | 22 | Buff::Buff(int32 duration) 23 | : m_duration(duration) 24 | {} 25 | 26 | // Effects with m_duration = -1 are infinite. 27 | void Buff::manageDuration() 28 | { 29 | if (m_duration == 0) 30 | active = 0; 31 | 32 | if (m_duration > 0) 33 | m_duration--; 34 | } 35 | 36 | HealthBoost::HealthBoost(int32 duration, int32 maxHealth, int32 healthregen) 37 | : Buff(duration) 38 | , max_health(maxHealth) 39 | , health_regen(healthregen) 40 | {} 41 | 42 | void HealthBoost::effect(Stats& stats) 43 | { 44 | stats.max_health += max_health; 45 | stats.health_regen += health_regen; 46 | } 47 | 48 | Defense::Defense(int32 duration, int32 armor, int32 mr) 49 | : Buff(duration) 50 | , armor(armor) 51 | , magic_resist(mr) 52 | {} 53 | 54 | void Defense::effect(Stats& stats) 55 | { 56 | stats.armor += armor; 57 | stats.magic_resist += magic_resist; 58 | } 59 | 60 | #pragma endregion 61 | } -------------------------------------------------------------------------------- /src/components/StatsComponent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Component.h" 4 | 5 | namespace Components 6 | { 7 | #pragma region STATS 8 | struct Stats 9 | { 10 | int32 health, max_health, health_regen, armor, magic_resist; 11 | }; 12 | #pragma endregion 13 | 14 | #pragma region STATUS_EFFECT 15 | 16 | // A status effect changes values in the stats component over time 17 | class Buff 18 | { 19 | public: 20 | Buff(int32 duration); 21 | virtual ~Buff() = default; 22 | 23 | bool active = 1; 24 | 25 | void manageDuration(); 26 | virtual void effect(Stats& stats) = 0; 27 | 28 | private: 29 | int32 m_duration; 30 | }; 31 | 32 | class HealthBoost : public Buff 33 | { 34 | public: 35 | int32 max_health; 36 | int32 health_regen; 37 | 38 | HealthBoost(int32 duration, int32 maxHealth, int32 healthregen = 0); 39 | 40 | void effect(Stats& stats) override; 41 | }; 42 | 43 | class Defense : public Buff 44 | { 45 | public: 46 | int32 armor; 47 | int32 magic_resist; 48 | 49 | Defense(int32 duration, int32 armor, int32 mr); 50 | 51 | void effect(Stats& stats) override; 52 | }; 53 | 54 | #pragma endregion 55 | 56 | class StatsComponent : public Component 57 | { 58 | public: 59 | Stats stats; 60 | std::vector> active_buffs; 61 | 62 | public: 63 | StatsComponent(); 64 | StatsComponent(nlohmann::json json); 65 | 66 | std::unique_ptr clone() override { return std::make_unique(*this); } 67 | 68 | static const uint ID = ComponentID::Stats; 69 | }; 70 | } 71 | -------------------------------------------------------------------------------- /src/debug/DebugConsole.cpp: -------------------------------------------------------------------------------- 1 | #include "DebugConsole.h" 2 | 3 | namespace Debug 4 | { 5 | using namespace GUI; 6 | 7 | Console::Console() 8 | : Window("Console", FloatRectangle(600.f, 300.f, 200.f, 150.f), { 65.f, 40.f }) 9 | , m_panel(new Panel()) 10 | { 11 | m_panel->add(this); 12 | } 13 | 14 | Console::~Console() 15 | { 16 | delete m_panel; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/debug/DebugConsole.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "../graphics/gui/Window.h" 6 | 7 | namespace Debug 8 | { 9 | class Console : public GUI::Window 10 | { 11 | public: 12 | Console(); 13 | ~Console(); 14 | 15 | private: 16 | GUI::Panel* m_panel; 17 | }; 18 | } -------------------------------------------------------------------------------- /src/debug/DebugMenu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../app/Application.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | namespace Debug 13 | { 14 | const int GUI_WIDTH = 1280; 15 | const int GUI_HEIGHT = 720; 16 | 17 | const int GUI_DISPLAY_VARS = 3; 18 | 19 | struct DebugMenuIntEntry 20 | { 21 | const std::string name; 22 | int* value; 23 | int rangeBeg; 24 | int rangeEnd; 25 | }; 26 | 27 | struct DebugMenuFloatEntry 28 | { 29 | const std::string name; 30 | float* value; 31 | float rangeBeg; 32 | float rangeEnd; 33 | }; 34 | 35 | struct DebugMenuBoolEntry 36 | { 37 | const std::string name; 38 | bool* value; 39 | }; 40 | 41 | class DebugMenu 42 | { 43 | public: 44 | DebugMenu(const sf::Font& font); 45 | 46 | void addEntry(const std::string& name, bool* value); 47 | void addEntry(const std::string& name, int* value, int rangeBeg, int rangeEnd); 48 | void addEntry(const std::string& name, float* value, float rangeBeg, float rangeEnd); 49 | 50 | void removeEntry(const std::string& name); 51 | 52 | void input(); 53 | void update(); 54 | void render(); 55 | private: 56 | std::unordered_map m_boolMap; 57 | std::unordered_map m_intMap; 58 | std::unordered_map m_floatMap; 59 | 60 | bool m_active; 61 | int m_selectedEntry; 62 | int m_numEntries; 63 | 64 | std::string createText(DebugMenuBoolEntry& entry); 65 | std::string createText(DebugMenuIntEntry& entry); 66 | std::string createText(DebugMenuFloatEntry& entry); 67 | 68 | sf::RenderWindow m_window; 69 | 70 | int actualSelectedItem; 71 | sf::Text menu[3]; 72 | }; 73 | } 74 | -------------------------------------------------------------------------------- /src/entity/AStar.cpp: -------------------------------------------------------------------------------- 1 | #include "AStar.h" 2 | 3 | #include "../app/states/StatePlaying.h" 4 | 5 | namespace AStar 6 | { 7 | std::vector neighbors(Location id) 8 | { 9 | int32 x, y, dx, dy; 10 | std::tie(x, y) = id; 11 | std::vector results; 12 | 13 | for (auto dir : directions) 14 | { 15 | std::tie(dx, dy) = dir; 16 | Location next(x + dx, y + dy); 17 | if (State::Playing::get().isTilePassable(0, x + dx, y + dy)) 18 | results.push_back(next); 19 | } 20 | 21 | if ((x + y) % 2 == 0) 22 | std::reverse(results.begin(), results.end()); 23 | 24 | return results; 25 | } 26 | 27 | void search(Location start, Location goal, std::unordered_map& came_from, std::unordered_map& cost_so_far) 28 | { 29 | PriorityQueue frontier; 30 | frontier.put(start, 0); 31 | 32 | came_from[start] = start; 33 | cost_so_far[start] = 0; 34 | 35 | while (!frontier.empty()) 36 | { 37 | auto current = frontier.get(); 38 | 39 | if (current == goal) 40 | break; 41 | 42 | for (auto next : neighbors(current)) 43 | { 44 | double new_cost = cost_so_far[current] + distance(current, next); 45 | if (!cost_so_far.count(next) || new_cost < cost_so_far[next]) 46 | { 47 | cost_so_far[next] = new_cost; 48 | double priority = new_cost + heuristic(next, goal); 49 | frontier.put(next, priority); 50 | came_from[next] = current; 51 | } 52 | } 53 | } 54 | } 55 | 56 | std::vector constructPath(Location start, Location goal) 57 | { 58 | std::unordered_map came_from; 59 | std::unordered_map cost_so_far; 60 | search(start, goal, came_from, cost_so_far); 61 | 62 | std::vector path; 63 | Location current = goal; 64 | path.push_back(current); 65 | while (current != start) 66 | { 67 | current = came_from[current]; 68 | path.push_back(current); 69 | } 70 | 71 | State::Playing::get().getLevel().m_visualPath.clear(); 72 | for (uint i = 0; i < path.size(); i++) 73 | { 74 | Location& loc = path[i]; 75 | State::Playing::get().getLevel().m_visualPath.push_back(Vec2i(std::get<0>(loc), std::get<1>(loc))); 76 | } 77 | 78 | return path; 79 | } 80 | } -------------------------------------------------------------------------------- /src/entity/AStar.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "../maths/maths.h" 6 | #include "../util/PriorityQueue.h" 7 | 8 | #include 9 | 10 | namespace AStar 11 | { 12 | typedef std::tuple Location; 13 | 14 | static std::array directions 15 | { 16 | Location{ 1, 0 }, 17 | Location{ 1, -1 }, 18 | Location{ -1, 1 }, 19 | Location{ 1, 1 }, 20 | Location{ -1, -1 }, 21 | Location{ 0, -1 }, 22 | Location{ -1, 0 }, 23 | Location{ 0, 1 } 24 | }; 25 | 26 | std::vector neighbors(Location id); 27 | 28 | inline double distance(Location a, Location b) 29 | { 30 | int x1, y1, x2, y2; 31 | std::tie(x1, y1) = a; 32 | std::tie(x2, y2) = b; 33 | return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 34 | } 35 | 36 | // Manhattan distance 37 | // If you are wondering: In Manhattan you tell somebody where to go by 38 | // telling him how many blocks he has to go up/side (everything is square grid based 39 | // there). This is what the function is doing. A* doesnt need precise cost to the end 40 | // just an estimate so it's good enough. Avoids using expensive sqrt(..) 41 | inline double heuristic(Location a, Location b) 42 | { 43 | int x1, y1, x2, y2; 44 | std::tie(x1, y1) = a; 45 | std::tie(x2, y2) = b; 46 | return abs(x1 - x2) + abs(y1 - y2); 47 | } 48 | 49 | void search(Location start, Location goal, std::unordered_map& came_from, 50 | std::unordered_map& cost_so_far); 51 | 52 | std::vector constructPath(Location start, Location goal); 53 | } 54 | 55 | namespace std 56 | { 57 | template<> 58 | struct hash 59 | { 60 | size_t operator() (const AStar::Location& pos) const 61 | { 62 | std::hash hasher; 63 | auto h1 = hasher(std::get<0>(pos)); 64 | auto h2 = hasher(std::get<1>(pos)); 65 | 66 | return std::hash{}((h1 ^ h2) >> 2); 67 | } 68 | }; 69 | } -------------------------------------------------------------------------------- /src/entity/Damage.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Types.h" 4 | 5 | namespace Entity 6 | { 7 | // Knowing the damage source is useful! 8 | enum class DamageSource : byte 9 | { 10 | Physical = 0, 11 | Magic = 1, 12 | True = 2 // ignores defenses 13 | }; 14 | 15 | // Damage struct holding some data about damage instances. 16 | struct Damage 17 | { 18 | DamageSource source; 19 | int32 amount; 20 | }; 21 | 22 | class IDamageable 23 | { 24 | public: 25 | virtual void applyDamage(const Damage& dmg) = 0; 26 | }; 27 | } 28 | -------------------------------------------------------------------------------- /src/entity/Entity.cpp: -------------------------------------------------------------------------------- 1 | #include "Entity.h" 2 | 3 | namespace Entity 4 | { 5 | Entity::Entity() 6 | : m_ID(0) 7 | { 8 | } 9 | 10 | Entity::Entity(uint64 ID) 11 | : m_ID(ID) 12 | { 13 | } 14 | 15 | std::unique_ptr Entity::clone(uint64 id) 16 | { 17 | std::unique_ptr cloned = std::make_unique(id); 18 | 19 | for (auto& pair : m_components) 20 | cloned->m_components[pair.first] = pair.second->clone(); 21 | 22 | return cloned; 23 | } 24 | } -------------------------------------------------------------------------------- /src/entity/Entity.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../components/Component.h" 4 | 5 | #include "../maths/maths.h" 6 | 7 | struct Timestep; 8 | 9 | namespace Entity 10 | { 11 | class Entity 12 | { 13 | public: 14 | Entity(); 15 | Entity(uint64 ID); 16 | 17 | uint64 getID() { return m_ID; } 18 | 19 | template 20 | void addComponent(std::unique_ptr component) 21 | { 22 | m_components[int(T::ID)] = std::move(component); 23 | } 24 | 25 | template 26 | T* getComponent() 27 | { 28 | uint id = T::ID; 29 | auto it = m_components.find(id); 30 | if (it == m_components.end()) 31 | return nullptr; 32 | return dynamic_cast(it->second.get()); 33 | } 34 | 35 | std::unique_ptr clone(uint64 id); 36 | private: 37 | uint64 m_ID; 38 | std::unordered_map> m_components; 39 | }; 40 | } 41 | -------------------------------------------------------------------------------- /src/entity/EntityFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "EntityFactory.h" 2 | 3 | #include "../components/Components.h" 4 | 5 | #include "../app/Application.h" 6 | #include "../util/FileUtil.h" 7 | 8 | namespace Entity 9 | { 10 | EntityFactory::EntityFactory() 11 | : m_lastID(0) 12 | { 13 | } 14 | 15 | std::unique_ptr EntityFactory::createEntity(std::string name) 16 | { 17 | if (m_templates.find(name) == m_templates.end()) 18 | createTemplate(name); 19 | 20 | return m_templates.find(name)->second->clone(++m_lastID); 21 | } 22 | 23 | void EntityFactory::createTemplate(std::string filePath) 24 | { 25 | std::string source = getFileContents("res/entities/" + filePath); 26 | nlohmann::json json = nlohmann::json::parse(source.c_str()); 27 | 28 | std::unique_ptr entity = std::make_unique(); 29 | 30 | std::vector componentsJSON = json["components"]; 31 | for (uint i = 0; i < componentsJSON.size(); i++) 32 | { 33 | nlohmann::json componentJSON = componentsJSON[i]; 34 | 35 | using namespace Components; 36 | 37 | if (componentJSON["componentType"].get() == "Physics") 38 | entity->addComponent(std::make_unique(componentJSON)); 39 | if (componentJSON["componentType"].get() == "Sprite") 40 | entity->addComponent(std::make_unique(componentJSON)); 41 | if (componentJSON["componentType"].get() == "Stats") 42 | entity->addComponent(std::make_unique(componentJSON)); 43 | if (componentJSON["componentType"].get() == "Light") 44 | entity->addComponent(std::make_unique(componentJSON)); 45 | if (componentJSON["componentType"].get() == "Script") 46 | entity->addComponent(std::make_unique(componentJSON)); 47 | } 48 | 49 | m_templates[filePath] = std::move(entity); 50 | } 51 | } -------------------------------------------------------------------------------- /src/entity/EntityFactory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Entity.h" 4 | 5 | namespace Entity 6 | { 7 | class EntityFactory 8 | { 9 | public: 10 | EntityFactory(); 11 | 12 | std::unique_ptr createEntity(std::string name); 13 | private: 14 | void createTemplate(std::string filePath); 15 | 16 | std::unordered_map> m_templates; 17 | uint64 m_lastID; 18 | }; 19 | } 20 | -------------------------------------------------------------------------------- /src/entity/system/System.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Entity.h" 4 | 5 | #include 6 | 7 | struct Timestep; 8 | 9 | namespace Entity 10 | { 11 | class System 12 | { 13 | public: 14 | virtual ~System() = default; 15 | 16 | virtual void update(const Timestep& ts, Entity* entity) {}; 17 | }; 18 | 19 | class MoveSystem : public System 20 | { 21 | public: 22 | void update(const Timestep& ts, Entity* entity) override; 23 | }; 24 | 25 | class ScriptSystem : public System 26 | { 27 | public: 28 | void update(const Timestep& ts, Entity* entity) override; 29 | }; 30 | 31 | class LifeSystem : public System 32 | { 33 | public: 34 | void update(const Timestep& ts, Entity* entity) override; 35 | }; 36 | 37 | class StatsSystem : public System 38 | { 39 | public: 40 | void update(const Timestep& ts, Entity* entity) override; 41 | }; 42 | 43 | class AnimatorSystem : public System 44 | { 45 | public: 46 | void update(const Timestep& ts, Entity* entity) override; 47 | }; 48 | 49 | class LightingSystem : public System 50 | { 51 | public: 52 | void update(const Timestep& ts, Entity* entity) override; 53 | }; 54 | 55 | class RenderSystem : public System 56 | { 57 | public: 58 | void update(const Timestep& ts, Entity* entity) override; 59 | }; 60 | } 61 | -------------------------------------------------------------------------------- /src/events/Event.cpp: -------------------------------------------------------------------------------- 1 | #include "Event.h" 2 | 3 | namespace Events 4 | { 5 | Event::Event(Type type) 6 | : m_type(type), m_handled(false) 7 | { 8 | } 9 | 10 | std::string Event::typeToString(Type type) 11 | { 12 | switch (type) 13 | { 14 | case Type::KEY_PRESSED: 15 | return "KEY_PRESSED"; 16 | case Type::KEY_RELEASED: 17 | return "KEY_RELEASED"; 18 | case Type::MOUSE_PRESSED: 19 | return "MOUSE_PRESSED"; 20 | case Type::MOUSE_RELEASED: 21 | return "MOUSE_RELEASED"; 22 | case Type::MOUSE_MOVED: 23 | return "MOUSE_MOVED"; 24 | } 25 | return "INVALID"; 26 | } 27 | } -------------------------------------------------------------------------------- /src/events/Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #undef MOUSE_MOVED 6 | 7 | namespace Events 8 | { 9 | class Event 10 | { 11 | private: 12 | friend class EventDispatcher; 13 | 14 | public: 15 | enum class Type 16 | { 17 | KEY_PRESSED = BIT(0), 18 | KEY_RELEASED = BIT(1), 19 | 20 | MOUSE_PRESSED = BIT(2), 21 | MOUSE_RELEASED = BIT(3), 22 | MOUSE_MOVED = BIT(4), 23 | 24 | WINDOW_RESIZE = BIT(5), 25 | }; 26 | 27 | protected: 28 | bool m_handled; 29 | Type m_type; 30 | 31 | protected: 32 | Event(Type type); 33 | 34 | public: 35 | Type getType() const { return m_type; } 36 | bool isHandled() const { return m_handled; } 37 | 38 | static std::string typeToString(Type type); 39 | }; 40 | } -------------------------------------------------------------------------------- /src/events/EventDispatcher.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Event.h" 4 | 5 | namespace Events 6 | { 7 | class EventDispatcher 8 | { 9 | private: 10 | Event& m_event; 11 | public: 12 | EventDispatcher(Event& event) 13 | : m_event(event) {} 14 | 15 | template 16 | void dispatch(std::function func) 17 | { 18 | if ((int32)m_event.getType() & (int32)T::getStaticType()) 19 | m_event.m_handled = func(*(T*)&m_event); 20 | } 21 | }; 22 | } -------------------------------------------------------------------------------- /src/events/Events.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "KeyEvent.h" 4 | #include "MouseEvent.h" 5 | #include "IEventListener.h" 6 | 7 | #include "EventDispatcher.h" -------------------------------------------------------------------------------- /src/events/IEventListener.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Event.h" 4 | 5 | namespace Events 6 | { 7 | class IEventListener 8 | { 9 | public: 10 | virtual void onEvent(Event& event) = 0; 11 | }; 12 | } -------------------------------------------------------------------------------- /src/events/KeyEvent.cpp: -------------------------------------------------------------------------------- 1 | #include "KeyEvent.h" 2 | 3 | namespace Events 4 | { 5 | KeyEvent::KeyEvent(int32 keyCode, Event::Type type) 6 | : Event(type), m_keyCode(keyCode) 7 | { 8 | } 9 | 10 | KeyPressedEvent::KeyPressedEvent(int32 button, int32 repeat, int32 modifiers) 11 | : KeyEvent(button, KeyPressedEvent::getStaticType()), m_repeat(repeat), m_modifiers(modifiers) 12 | { 13 | } 14 | 15 | KeyReleasedEvent::KeyReleasedEvent(int32 button) 16 | : KeyEvent(button, KeyReleasedEvent::getStaticType()) 17 | { 18 | } 19 | } -------------------------------------------------------------------------------- /src/events/KeyEvent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Event.h" 4 | 5 | namespace Events 6 | { 7 | enum class KeyModifier 8 | { 9 | SHIFT = BIT(0), 10 | CONTROL = BIT(1), 11 | ALT = BIT(2) 12 | }; 13 | 14 | class KeyEvent : public Event 15 | { 16 | public: 17 | KeyEvent(int32 keyCode, Event::Type type); 18 | 19 | int32 getKeyCode() const { return m_keyCode; } 20 | 21 | static int32 getStaticType() { return (int32)Event::Type::KEY_PRESSED | (int32)Event::Type::KEY_RELEASED; } 22 | 23 | protected: 24 | int32 m_keyCode; 25 | int32 m_count; 26 | }; 27 | 28 | class KeyPressedEvent : public KeyEvent 29 | { 30 | public: 31 | KeyPressedEvent(int32 button, int32 repeat, int32 modifiers); 32 | 33 | int32 getRepeat() const { return m_repeat; } 34 | int32 getModifiers() const { return m_modifiers; } 35 | bool isModifier(int32 modifier) const { return (bool)(m_modifiers & modifier); } 36 | 37 | static Type getStaticType() { return Event::Type::KEY_PRESSED; } 38 | 39 | private: 40 | int32 m_repeat; 41 | int32 m_modifiers; 42 | }; 43 | 44 | class KeyReleasedEvent : public KeyEvent 45 | { 46 | public: 47 | KeyReleasedEvent(int32 button); 48 | 49 | static Type getStaticType() { return Event::Type::KEY_RELEASED; } 50 | }; 51 | } -------------------------------------------------------------------------------- /src/events/MouseEvent.cpp: -------------------------------------------------------------------------------- 1 | #include "MouseEvent.h" 2 | 3 | namespace Events 4 | { 5 | MouseButtonEvent::MouseButtonEvent(int32 button, float x, float y, Type type) 6 | : Event(type), m_button(button), m_position({ x, y }) 7 | { 8 | } 9 | 10 | MousePressedEvent::MousePressedEvent(int32 button, float x, float y) 11 | : MouseButtonEvent(button, x, y, MousePressedEvent::getStaticType()) 12 | { 13 | } 14 | 15 | MouseReleasedEvent::MouseReleasedEvent(int32 button, float x, float y) 16 | : MouseButtonEvent(button, x, y, MouseReleasedEvent::GetStaticType()) 17 | { 18 | } 19 | 20 | MouseMovedEvent::MouseMovedEvent(float x, float y, bool dragged) 21 | : Event(MouseMovedEvent::getStaticType()), m_position({ x, y }), m_dragged(dragged) 22 | { 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/events/MouseEvent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Event.h" 4 | #include "../maths/Vec2.h" 5 | 6 | namespace Events 7 | { 8 | class MouseButtonEvent : public Event 9 | { 10 | protected: 11 | MouseButtonEvent(int32 button, float x, float y, Event::Type type); 12 | 13 | public: 14 | const int32 getButton() const { return m_button; } 15 | const float getX() const { return m_position.x; } 16 | const float getY() const { return m_position.y; } 17 | const Vec2& getPosition() const { return m_position; } 18 | 19 | static int32 getStaticType() { return (int32)Event::Type::MOUSE_PRESSED | (int32)Event::Type::MOUSE_RELEASED; } 20 | 21 | protected: 22 | int32 m_button; 23 | Vec2 m_position; 24 | }; 25 | 26 | class MousePressedEvent : public MouseButtonEvent 27 | { 28 | public: 29 | MousePressedEvent(int32 button, float x, float y); 30 | 31 | static Type getStaticType() { return Event::Type::MOUSE_PRESSED; } 32 | }; 33 | 34 | class MouseReleasedEvent : public MouseButtonEvent 35 | { 36 | public: 37 | MouseReleasedEvent(int32 button, float x, float y); 38 | 39 | static Type GetStaticType() { return Event::Type::MOUSE_RELEASED; } 40 | }; 41 | 42 | class MouseMovedEvent : public Event 43 | { 44 | public: 45 | MouseMovedEvent(float x, float y, bool dragged); 46 | 47 | const float getX() const { return m_position.x; } 48 | const float getY() const { return m_position.y; } 49 | const Vec2& getPosition() const { return m_position; } 50 | const bool isDragged() const { return m_dragged; } 51 | 52 | static Type getStaticType() { return Event::Type::MOUSE_MOVED; } 53 | 54 | private: 55 | Vec2 m_position; 56 | bool m_dragged; 57 | }; 58 | } -------------------------------------------------------------------------------- /src/graphics/Animator.cpp: -------------------------------------------------------------------------------- 1 | #include "Animator.h" 2 | 3 | namespace Graphics 4 | { 5 | void Animator::setAnimation(std::string name) 6 | { 7 | m_current = name; 8 | } 9 | 10 | void Animator::addAnimation(const std::string& name, uint xpos, uint ypos, uint stride, uint length, uint fps) 11 | { 12 | Animation anim; 13 | anim.name = name; 14 | anim.xpos = xpos; 15 | anim.ypos = ypos; 16 | anim.stride = stride; 17 | anim.length = length; 18 | anim.fps = fps; 19 | m_animations.push_back(anim); 20 | } 21 | 22 | void Animator::update(const Timestep& ts, sf::Sprite& destsprite) 23 | { 24 | if (m_animations.empty()) 25 | return; 26 | 27 | m_timer += ts.asSeconds(); 28 | Animation* anim = nullptr; 29 | bool animFound = false; 30 | for (uint i = 0; i < m_animations.size(); i++) 31 | { 32 | if (m_animations[i].name == m_current) 33 | { 34 | anim = &m_animations[i]; 35 | animFound = true; 36 | } 37 | } 38 | if (!animFound) 39 | return; 40 | 41 | float animLength = anim->length / (float)anim->fps; 42 | if (m_timer >= animLength) 43 | m_timer -= animLength; 44 | uint frameIndex = static_cast((m_timer / animLength) * anim->length); 45 | uint textureX = anim->xpos + frameIndex * anim->stride; 46 | uint textureY = anim->ypos; 47 | destsprite.setTextureRect(sf::IntRect(textureX, textureY, destsprite.getTextureRect().width, destsprite.getTextureRect().height)); 48 | } 49 | } -------------------------------------------------------------------------------- /src/graphics/Animator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | #include "../util/Timestep.h" 5 | 6 | #include 7 | 8 | namespace Graphics 9 | { 10 | struct Animation 11 | { 12 | std::string name; 13 | uint xpos, ypos, stride, length, fps; 14 | }; 15 | 16 | class Animator 17 | { 18 | private: 19 | std::vector m_animations; 20 | std::string m_current; 21 | 22 | float m_timer = 0; 23 | public: 24 | void setAnimation(const std::string name); 25 | void addAnimation(const std::string& name, uint xpos, uint ypos, uint stride, uint length, uint fps); 26 | 27 | void update(const Timestep& ts, sf::Sprite& destsprite); 28 | }; 29 | } 30 | -------------------------------------------------------------------------------- /src/graphics/Label.cpp: -------------------------------------------------------------------------------- 1 | #include "Label.h" 2 | 3 | namespace Graphics 4 | { 5 | Label::Label(const std::string& text, const sf::Font& font, uint size, Alignment alignment) 6 | : m_text(sf::Text(text, font, size)), m_alignment(alignment) 7 | { 8 | } 9 | 10 | void Label::render(sf::RenderWindow& m_window, sf::RenderStates states) 11 | { 12 | m_window.draw(m_text, states); 13 | } 14 | 15 | void Label::setAlignment(const Alignment& alignment) 16 | { 17 | m_alignment = alignment; 18 | updateBounds(); 19 | } 20 | 21 | void Label::setText(const std::string& text) 22 | { 23 | m_text.setString(text); 24 | updateBounds(); 25 | } 26 | 27 | void Label::setPosition(Vec2 position) 28 | { 29 | m_position = position; 30 | updateBounds(); 31 | } 32 | 33 | void Label::updateBounds() 34 | { 35 | sf::FloatRect rect = m_text.getLocalBounds(); 36 | Vec2 size(rect.width, rect.height); 37 | switch (m_alignment) 38 | { 39 | case Alignment::LEFT: 40 | m_alignmentOffset.x = 0; 41 | break; 42 | case Alignment::CENTER: 43 | m_alignmentOffset.x = -size.x * 0.5f; 44 | break; 45 | case Alignment::RIGHT: 46 | m_alignmentOffset.x = -size.x; 47 | break; 48 | default: 49 | break; 50 | } 51 | m_text.setPosition(m_position + m_alignmentOffset); 52 | } 53 | } -------------------------------------------------------------------------------- /src/graphics/Label.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../maths/maths.h" 4 | 5 | #include 6 | 7 | namespace Graphics 8 | { 9 | // Wrapper for sf::Text with alignments 10 | class Label 11 | { 12 | public: 13 | enum class Alignment 14 | { 15 | NONE = 0, LEFT, CENTER, RIGHT 16 | }; 17 | 18 | public: 19 | explicit Label(const std::string& text, const sf::Font& font, uint size, Alignment alignment = Alignment::LEFT); 20 | 21 | void render(sf::RenderWindow& m_window, sf::RenderStates states = sf::RenderStates()); 22 | 23 | void setAlignment(const Alignment& alignment); 24 | void setText(const std::string& text); 25 | 26 | void setPosition(Vec2 position); 27 | 28 | sf::Text& getText() { return m_text; } 29 | Alignment getAlignment() const { return m_alignment; } 30 | 31 | private: 32 | Vec2 m_position; 33 | 34 | sf::Text m_text; 35 | Alignment m_alignment; 36 | Vec2 m_alignmentOffset; 37 | 38 | void updateBounds(); 39 | }; 40 | } -------------------------------------------------------------------------------- /src/graphics/Renderable2D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "../maths/maths.h" 6 | #include "../maths/Color.h" 7 | 8 | #include 9 | 10 | namespace Graphics 11 | { 12 | class Renderable2D 13 | { 14 | public: 15 | Renderable2D(const Vec2& position, const Vec2& size, const Color& color) 16 | : m_position(position) 17 | , m_size(size) 18 | , m_color(color) 19 | { 20 | m_UVs.push_back(Vec2()); 21 | m_UVs.push_back(Vec2()); 22 | m_UVs.push_back(Vec2()); 23 | m_UVs.push_back(Vec2()); 24 | } 25 | 26 | const Vec2& getPosition() const { return m_position; } 27 | const Vec2& getSize() const { return m_size; } 28 | const Color& getColor() const { return m_color; } 29 | const std::vector& getUVs() const { return m_UVs; } 30 | 31 | public: 32 | Vec2 m_position; 33 | Vec2 m_size; 34 | Color m_color; 35 | std::vector m_UVs; 36 | }; 37 | } -------------------------------------------------------------------------------- /src/graphics/Renderer2D.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer2D.h" 2 | 3 | #include "../maths/maths.h" 4 | #include "../maths/Color.h" 5 | 6 | namespace Graphics 7 | { 8 | Renderer2D::Renderer2D() 9 | { 10 | m_before.buffer = new sf::Vertex[MAX_VERTICES]; 11 | m_after.buffer = new sf::Vertex[MAX_VERTICES]; 12 | } 13 | 14 | Renderer2D::~Renderer2D() 15 | { 16 | delete m_before.buffer; 17 | delete m_after.buffer; 18 | } 19 | 20 | void Renderer2D::begin() 21 | { 22 | m_before.bufferPtr = m_before.buffer; 23 | m_after.bufferPtr = m_after.buffer; 24 | } 25 | 26 | void Renderer2D::submit(const Renderable2D* renderable, const RenderOrder& renderOrder) 27 | { 28 | if (renderOrder == RenderOrder::BEFORE) 29 | submit(renderable, m_before); 30 | if (renderOrder == RenderOrder::AFTER) 31 | submit(renderable, m_after); 32 | } 33 | 34 | void Renderer2D::present(sf::RenderWindow& window, const RenderOrder& renderOrder) 35 | { 36 | if (renderOrder == RenderOrder::BEFORE) 37 | { 38 | window.draw(m_before.buffer, m_before.vertexCount, sf::Quads, m_states); 39 | m_before.vertexCount = 0; 40 | } 41 | if (renderOrder == RenderOrder::AFTER) 42 | { 43 | window.draw(m_after.buffer, m_after.vertexCount, sf::Quads, m_states); 44 | m_after.vertexCount = 0; 45 | } 46 | } 47 | 48 | void Renderer2D::submit(const Renderable2D* renderable, Render& render) 49 | { 50 | const Vec2& position = renderable->getPosition(); 51 | const Vec2& size = renderable->getSize(); 52 | const Color& c = renderable->getColor(); 53 | const std::vector& m_UVs = renderable->getUVs(); 54 | 55 | Vec2* uvPtr = const_cast(&m_UVs[0]); 56 | 57 | render.bufferPtr->position = position; 58 | render.bufferPtr->color = c; 59 | render.bufferPtr->texCoords = *uvPtr++; 60 | render.bufferPtr++; 61 | 62 | render.bufferPtr->position = Vec2(position.x, position.y + size.y); 63 | render.bufferPtr->color = c; 64 | render.bufferPtr->texCoords = *uvPtr++; 65 | render.bufferPtr++; 66 | 67 | render.bufferPtr->position = Vec2(position.x + size.x, position.y + size.y); 68 | render.bufferPtr->color = c; 69 | render.bufferPtr->texCoords = *uvPtr++; 70 | render.bufferPtr++; 71 | 72 | render.bufferPtr->position = Vec2(position.x + size.x, position.y); 73 | render.bufferPtr->color = c; 74 | render.bufferPtr->texCoords = *uvPtr++; 75 | render.bufferPtr++; 76 | 77 | render.vertexCount += 4; 78 | } 79 | } -------------------------------------------------------------------------------- /src/graphics/Renderer2D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "Renderable2D.h" 6 | 7 | #include 8 | 9 | namespace Graphics 10 | { 11 | #define MAX_SUBMITS 50000 12 | #define MAX_VERTICES MAX_SUBMITS * 4 13 | 14 | class Renderer2D 15 | { 16 | public: 17 | // This class has support for two seperate draw calls 18 | // In level BEFORE and AFTER is used to render stuff 19 | // behind or infront of entities. 20 | enum class RenderOrder 21 | { 22 | BEFORE, AFTER 23 | }; 24 | 25 | struct Render 26 | { 27 | sf::Vertex* buffer; 28 | sf::Vertex* bufferPtr; 29 | uint vertexCount; 30 | }; 31 | public: 32 | Renderer2D(); 33 | ~Renderer2D(); 34 | 35 | void begin(); 36 | void submit(const Renderable2D* renderable, const RenderOrder& renderOrder = RenderOrder::BEFORE); 37 | void present(sf::RenderWindow& window, const RenderOrder& renderOrder = RenderOrder::BEFORE); 38 | 39 | sf::RenderStates m_states; 40 | 41 | private: 42 | Render m_before, m_after; 43 | 44 | void submit(const Renderable2D* renderable, Render& render); 45 | }; 46 | } -------------------------------------------------------------------------------- /src/graphics/gui/Button.cpp: -------------------------------------------------------------------------------- 1 | #include "Button.h" 2 | 3 | #include "../../app/Application.h" 4 | 5 | namespace GUI 6 | { 7 | Button::Button(Graphics::Label* label, const FloatRectangle& bounds, const ActionHandler& handler) 8 | : Widget(bounds) 9 | , m_label(label) 10 | , m_actionHandler(handler) 11 | , m_state(ButtonState::UNPRESSED) 12 | , m_atlas((sf::Texture*)&Application::get().getTexture("/gui/button")) 13 | , m_sprite(new sf::RectangleShape(m_bounds.size)) 14 | { 15 | m_label->setAlignment(Graphics::Label::Alignment::CENTER); 16 | m_label->setPosition({ m_bounds.size.x * .5f, m_bounds.size.y * .25f}); 17 | 18 | m_sprite->setTexture(m_atlas); 19 | updateTexture(); 20 | } 21 | 22 | Button::~Button() 23 | { 24 | delete m_label; 25 | delete m_sprite; 26 | } 27 | 28 | void Button::updateTexture() 29 | { 30 | int32 yOffset = m_hovered ? 14 : 0; 31 | if (m_state == ButtonState::PRESSED) 32 | yOffset = 27; 33 | 34 | m_sprite->setTextureRect({ 0, yOffset, 56, 14 }); 35 | } 36 | 37 | bool Button::mousePressed(Events::MousePressedEvent& e) 38 | { 39 | m_state = ButtonState::PRESSED; 40 | 41 | return true; 42 | } 43 | 44 | bool Button::mouseReleased(Events::MouseReleasedEvent& e) 45 | { 46 | if (m_state == ButtonState::PRESSED) 47 | m_actionHandler(); 48 | 49 | m_state = ButtonState::UNPRESSED; 50 | 51 | return true; 52 | } 53 | 54 | bool Button::mouseMoved(Events::MouseMovedEvent& e) 55 | { 56 | Vec2 mouse = Application::get().uiMousePos(); 57 | if (!m_bounds.contains(mouse)) 58 | m_state = ButtonState::UNPRESSED; 59 | 60 | return false; 61 | } 62 | 63 | void Button::update() 64 | { 65 | Vec2 mouse = Application::get().uiMousePos(); 66 | m_hovered = m_bounds.contains(mouse); 67 | updateTexture(); 68 | } 69 | 70 | void Button::render(sf::RenderWindow& window) 71 | { 72 | m_states.transform = sf::Transform().translate(m_bounds.position); 73 | window.draw(*m_sprite, m_states); 74 | m_label->render(window, m_states); 75 | } 76 | } -------------------------------------------------------------------------------- /src/graphics/gui/Button.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Widget.h" 4 | #include "../Label.h" 5 | 6 | namespace GUI 7 | { 8 | class Button : public Widget 9 | { 10 | public: 11 | using ActionHandler = std::function; 12 | 13 | private: 14 | enum class ButtonState 15 | { 16 | UNPRESSED, PRESSED 17 | }; 18 | 19 | protected: 20 | Graphics::Label* m_label; 21 | ButtonState m_state; 22 | ActionHandler m_actionHandler; 23 | 24 | sf::Texture* m_atlas; 25 | sf::RenderStates m_states; 26 | sf::RectangleShape* m_sprite; 27 | 28 | bool m_hovered; 29 | 30 | public: 31 | Button(Graphics::Label* label, const FloatRectangle& bounds, const ActionHandler& handler = &Button::NoAction); 32 | ~Button(); 33 | 34 | bool mousePressed(Events::MousePressedEvent& e) override; 35 | bool mouseReleased(Events::MouseReleasedEvent& e) override; 36 | bool mouseMoved(Events::MouseMovedEvent& e) override; 37 | 38 | virtual void update() override; 39 | virtual void render(sf::RenderWindow& window) override; 40 | 41 | void updateTexture(); 42 | 43 | void setAction(const ActionHandler& action) { m_actionHandler = action; } 44 | 45 | private: 46 | static void NoAction() {} 47 | }; 48 | } -------------------------------------------------------------------------------- /src/graphics/gui/Panel.cpp: -------------------------------------------------------------------------------- 1 | #include "Panel.h" 2 | 3 | #include "Widget.h" 4 | 5 | #include "../../app/Application.h" 6 | 7 | namespace GUI 8 | { 9 | using namespace Events; 10 | 11 | Panel::Panel() 12 | { 13 | Application::get().pushPanel(this); 14 | } 15 | 16 | Panel::~Panel() 17 | { 18 | clear(); 19 | 20 | Application::get().popPanel(this); 21 | } 22 | 23 | Widget* Panel::add(Widget* widget) 24 | { 25 | m_widgets.push_back(widget); 26 | return widget; 27 | } 28 | 29 | void Panel::remove(Widget* widget) 30 | { 31 | for (uint i = 0; i < m_widgets.size(); i++) 32 | { 33 | if (m_widgets[i] == widget) 34 | { 35 | m_widgets.erase(m_widgets.begin() + i); 36 | delete m_widgets[i]; 37 | break; 38 | } 39 | } 40 | } 41 | 42 | void Panel::clear() 43 | { 44 | for (uint i = 0; i < m_widgets.size(); i++) 45 | delete m_widgets[i]; 46 | 47 | m_widgets.clear(); 48 | } 49 | 50 | void Panel::onEvent(Events::Event& event) 51 | { 52 | EventDispatcher dispatcher(event); 53 | dispatcher.dispatch(_METHOD(&Panel::mouseReleased)); 54 | dispatcher.dispatch(_METHOD(&Panel::mousePressed)); 55 | dispatcher.dispatch(_METHOD(&Panel::mouseMoved)); 56 | } 57 | 58 | bool Panel::mousePressed(Events::MousePressedEvent& e) 59 | { 60 | Vec2 mouse = Application::get().uiMousePos(); 61 | for (uint i = 0; i < m_widgets.size(); i++) 62 | { 63 | Widget* widget = m_widgets[i]; 64 | if (widget->getBounds().contains(mouse)) 65 | { 66 | if (widget->isActive()) 67 | if (widget->mousePressed(e)) 68 | return true; 69 | } 70 | } 71 | return false; 72 | } 73 | 74 | bool Panel::mouseReleased(Events::MouseReleasedEvent& e) 75 | { 76 | Vec2 mouse = Application::get().uiMousePos(); 77 | for (uint i = 0; i < m_widgets.size(); i++) 78 | { 79 | Widget* widget = m_widgets[i]; 80 | if (widget->getBounds().contains(mouse)) 81 | { 82 | if (widget->isActive()) 83 | if (widget->mouseReleased(e)) 84 | return true; 85 | } 86 | } 87 | return false; 88 | } 89 | 90 | bool Panel::mouseMoved(Events::MouseMovedEvent& e) 91 | { 92 | for (uint i = 0; i < m_widgets.size(); i++) 93 | { 94 | Widget* widget = m_widgets[i]; 95 | if (widget->isActive()) 96 | if (widget->mouseMoved(e)) 97 | return true; 98 | } 99 | return false; 100 | } 101 | 102 | void Panel::update() 103 | { 104 | for (Widget* widget : m_widgets) 105 | { 106 | if (widget->isActive()) 107 | widget->update(); 108 | } 109 | } 110 | 111 | void Panel::render(sf::RenderWindow& window) 112 | { 113 | for (Widget* widget : m_widgets) 114 | { 115 | if (widget->isActive()) 116 | widget->render(window); 117 | } 118 | } 119 | } -------------------------------------------------------------------------------- /src/graphics/gui/Panel.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../Common.h" 4 | 5 | #include "../../events/Events.h" 6 | 7 | #include 8 | 9 | namespace GUI 10 | { 11 | class Widget; 12 | 13 | class Panel : public Events::IEventListener 14 | { 15 | public: 16 | Panel(); 17 | ~Panel(); 18 | 19 | Widget* add(Widget* widget); 20 | void remove(Widget* widget); 21 | void clear(); 22 | 23 | void onEvent(Events::Event& event) override; 24 | bool mousePressed(Events::MousePressedEvent& e); 25 | bool mouseReleased(Events::MouseReleasedEvent& e); 26 | bool mouseMoved(Events::MouseMovedEvent& e); 27 | 28 | void update(); 29 | void render(sf::RenderWindow& window); 30 | 31 | const std::vector& getWidgets() const { return m_widgets; } 32 | 33 | private: 34 | std::vector m_widgets; 35 | }; 36 | } -------------------------------------------------------------------------------- /src/graphics/gui/Slider.cpp: -------------------------------------------------------------------------------- 1 | #include "Slider.h" 2 | 3 | #include "../../app/Application.h" 4 | 5 | namespace GUI 6 | { 7 | Slider::Slider(const FloatRectangle& bounds, bool vertical) 8 | : Widget(bounds) 9 | , m_value(0.0f) 10 | , m_state(SliderState::UNPRESSED) 11 | , m_headOffset(0.0f) 12 | , m_callback(&Slider::NoCallback) 13 | , m_vertical(vertical) 14 | , m_atlas((sf::Texture*)&Application::get().getTexture("/gui/slider")) 15 | , m_bgSprite(new sf::RectangleShape(m_bounds.size)) 16 | { 17 | float size = vertical ? bounds.width : bounds.height; 18 | m_headBounds = FloatRectangle(bounds.x, bounds.y, size, size); 19 | 20 | m_headSprite = new sf::RectangleShape(m_headBounds.size); 21 | m_headSprite->setTexture(m_atlas); 22 | m_bgSprite->setTexture(m_atlas); 23 | m_bgSprite->setTextureRect({ 0, 0, 56, 14 }); 24 | } 25 | 26 | Slider::Slider(const FloatRectangle& bounds, float value, const ValueChangedCallback& callback, bool vertical) 27 | : Widget(bounds) 28 | , m_value(value) 29 | , m_state(SliderState::UNPRESSED) 30 | , m_headOffset(0.0f) 31 | , m_callback(callback) 32 | , m_vertical(vertical) 33 | , m_atlas((sf::Texture*)&Application::get().getTexture("/gui/slider")) 34 | , m_bgSprite(new sf::RectangleShape(m_bounds.size)) 35 | { 36 | float size = vertical ? bounds.width : bounds.height; 37 | m_headBounds = FloatRectangle(bounds.x, bounds.y, size, size); 38 | 39 | m_headSprite = new sf::RectangleShape(m_headBounds.size); 40 | m_headSprite->setTexture(m_atlas); 41 | m_bgSprite->setTexture(m_atlas); 42 | m_bgSprite->setTextureRect({ 0, 0, 56, 14 }); 43 | } 44 | 45 | bool Slider::mousePressed(Events::MousePressedEvent& e) 46 | { 47 | Vec2 mouse = Application::get().uiMousePos(); 48 | if (m_hovered) 49 | { 50 | m_state = SliderState::PRESSEDHEAD; 51 | m_headOffset = m_vertical ? (mouse.y - m_headBounds.y - m_headBounds.height / 2) : (mouse.x - m_headBounds.x - m_headBounds.width / 2); 52 | } 53 | 54 | return true; 55 | } 56 | 57 | bool Slider::mouseReleased(Events::MouseReleasedEvent& e) 58 | { 59 | m_state = SliderState::UNPRESSED; 60 | return true; 61 | } 62 | 63 | bool Slider::mouseMoved(Events::MouseMovedEvent& e) 64 | { 65 | Vec2 mouse = Application::get().uiMousePos(); 66 | if (m_state == SliderState::PRESSEDHEAD) 67 | { 68 | if (m_vertical) 69 | setValue((mouse.y - m_bounds.getMinimumBound().y - m_headOffset) / (m_bounds.height)); 70 | else 71 | setValue((mouse.x - m_bounds.getMinimumBound().x - m_headOffset) / (m_bounds.width)); 72 | } 73 | 74 | return false; 75 | } 76 | 77 | void Slider::update() 78 | { 79 | if (!sf::Mouse::isButtonPressed(sf::Mouse::Left)) 80 | m_state = SliderState::UNPRESSED; 81 | 82 | Vec2 mouse = Application::get().uiMousePos(); 83 | m_hovered = m_headBounds.contains(mouse); 84 | updateTexture(); 85 | 86 | if (m_vertical) 87 | m_headBounds.y = m_bounds.getMinimumBound().y + m_headBounds.height / 2 + m_value * (m_bounds.height - m_headBounds.height) - m_headBounds.height /2; 88 | else 89 | m_headBounds.x = m_bounds.getMinimumBound().x + m_headBounds.width / 2 + m_value * (m_bounds.width - m_headBounds.width) - m_headBounds.width / 2; 90 | } 91 | 92 | void Slider::render(sf::RenderWindow& window) 93 | { 94 | m_states.transform = sf::Transform().translate(m_bounds.position); 95 | window.draw(*m_bgSprite, m_states); 96 | m_states.transform = sf::Transform().translate(m_headBounds.position); 97 | window.draw(*m_headSprite, m_states); 98 | } 99 | 100 | void Slider::updateTexture() 101 | { 102 | int32 xOffset = m_hovered ? 12 : 0; 103 | if (m_state == SliderState::PRESSEDHEAD) 104 | xOffset = 24; 105 | 106 | m_headSprite->setTextureRect({ xOffset, 14, 12, 14 }); 107 | } 108 | 109 | void Slider::setValue(float value) 110 | { 111 | value = clamp(value, 0.0f, 1.0f); 112 | m_value = value; 113 | m_callback(value); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/graphics/gui/Slider.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Widget.h" 4 | #include "../Label.h" 5 | 6 | #include "../../maths/Rectangle.h" 7 | 8 | namespace GUI 9 | { 10 | class Slider : public Widget 11 | { 12 | public: 13 | using ValueChangedCallback = std::function; 14 | 15 | private: 16 | enum class SliderState 17 | { 18 | UNPRESSED, PRESSEDHEAD 19 | }; 20 | 21 | private: 22 | FloatRectangle m_headBounds; 23 | float m_value; 24 | float m_headOffset; 25 | SliderState m_state; 26 | ValueChangedCallback m_callback; 27 | bool m_vertical; 28 | 29 | bool m_hovered; 30 | 31 | sf::Texture* m_atlas; 32 | sf::RenderStates m_states; 33 | sf::RectangleShape* m_bgSprite; 34 | sf::RectangleShape* m_headSprite; 35 | 36 | public: 37 | Slider(const FloatRectangle& bounds, bool vertical = false); 38 | Slider(const FloatRectangle& bounds, float value = 0.0f, const ValueChangedCallback& callback = &Slider::NoCallback, bool vertical = false); 39 | 40 | bool mousePressed(Events::MousePressedEvent& e) override; 41 | bool mouseReleased(Events::MouseReleasedEvent& e) override; 42 | bool mouseMoved(Events::MouseMovedEvent& e) override; 43 | 44 | virtual void update() override; 45 | virtual void render(sf::RenderWindow& window) override; 46 | 47 | void updateTexture(); 48 | 49 | void setCallback(const ValueChangedCallback& callback) { m_callback = callback; } 50 | inline const ValueChangedCallback& getCallback() const { return m_callback; } 51 | 52 | inline float getValue() const { return m_value; } 53 | void setValue(float value); 54 | 55 | private: 56 | static void NoCallback(float) {} 57 | }; 58 | } -------------------------------------------------------------------------------- /src/graphics/gui/Widget.cpp: -------------------------------------------------------------------------------- 1 | #include "Widget.h" 2 | 3 | #include "../../app/Application.h" 4 | 5 | namespace GUI 6 | { 7 | Widget::Widget(const FloatRectangle& bounds) 8 | : m_bounds(bounds) 9 | , m_active(true) 10 | , m_focused(false) 11 | { 12 | } 13 | 14 | bool Widget::mousePressed(Events::MousePressedEvent& e) 15 | { 16 | return false; 17 | } 18 | 19 | bool Widget::mouseReleased(Events::MouseReleasedEvent& e) 20 | { 21 | return false; 22 | } 23 | 24 | bool Widget::mouseMoved(Events::MouseMovedEvent& e) 25 | { 26 | return false; 27 | } 28 | 29 | void Widget::update() 30 | { 31 | } 32 | 33 | void Widget::render(sf::RenderWindow & renderer) 34 | { 35 | } 36 | } -------------------------------------------------------------------------------- /src/graphics/gui/Widget.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Panel.h" 4 | 5 | #include "../../events/Events.h" 6 | #include "../../maths/maths.h" 7 | 8 | namespace GUI 9 | { 10 | class Widget 11 | { 12 | protected: 13 | bool m_active; 14 | bool m_focused; 15 | 16 | Panel* m_panel; 17 | FloatRectangle m_bounds; 18 | 19 | private: 20 | Widget() {} 21 | 22 | protected: 23 | Widget(const FloatRectangle& bounds); 24 | 25 | public: 26 | virtual bool mousePressed(Events::MousePressedEvent& e); 27 | virtual bool mouseReleased(Events::MouseReleasedEvent& e); 28 | virtual bool mouseMoved(Events::MouseMovedEvent& e); 29 | 30 | virtual void update(); 31 | virtual void render(sf::RenderWindow& renderer); 32 | 33 | const FloatRectangle& getBounds() const { return m_bounds; } 34 | FloatRectangle& getBounds() { return m_bounds; } 35 | void setBounds(const FloatRectangle& bounds) { m_bounds = bounds; } 36 | 37 | bool isActive() const { return m_active; } 38 | void setActive(bool active) { m_active = active; } 39 | }; 40 | } -------------------------------------------------------------------------------- /src/graphics/gui/Window.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../Common.h" 4 | 5 | #include "Widget.h" 6 | 7 | #include "../Label.h" 8 | #include "../../maths/maths.h" 9 | 10 | namespace GUI 11 | { 12 | // Do not confuse with 'real' window. 13 | class Window : public Widget 14 | { 15 | struct Manifold 16 | { 17 | bool active; 18 | Vec2 offset; 19 | }; 20 | public: 21 | Window(const std::string& title, const FloatRectangle& bounds, const Vec2& minSize = Vec2(65, 65)); 22 | 23 | bool mousePressed(Events::MousePressedEvent& e) override; 24 | bool mouseReleased(Events::MouseReleasedEvent& e) override; 25 | bool mouseMoved(Events::MouseMovedEvent& e) override; 26 | 27 | void update() override; 28 | void render(sf::RenderWindow& window) override; 29 | 30 | void rebuildVertexArray(); 31 | 32 | void validateSize(); 33 | void validatePosition(); 34 | 35 | void updateButtons(); 36 | 37 | private: 38 | Graphics::Label m_title; 39 | Vec2 m_minSize; 40 | 41 | Manifold m_moving; 42 | Manifold m_resizing; 43 | 44 | sf::RenderStates m_states; 45 | std::vector m_vao; 46 | 47 | FloatRectangle close_button; 48 | FloatRectangle resize_button; 49 | FloatRectangle move_section; 50 | }; 51 | } -------------------------------------------------------------------------------- /src/item/Item.cpp: -------------------------------------------------------------------------------- 1 | #include "Item.h" 2 | 3 | namespace Item 4 | { 5 | Item::Item() 6 | : m_ID(0) 7 | , m_owningEntity(nullptr) 8 | {} 9 | 10 | Item::Item(Entity::Entity* owningEntity, uint64 ID) 11 | : m_ID(ID) 12 | , m_owningEntity(owningEntity) 13 | {} 14 | 15 | std::unique_ptr Item::clone(uint64 id) 16 | { 17 | std::unique_ptr cloned = std::make_unique(nullptr, id); 18 | 19 | for (auto& pair : m_components) 20 | cloned->m_components[pair.first] = pair.second->clone(); 21 | 22 | return cloned; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/item/Item.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../entity/Entity.h" 4 | #include "../components/Component.h" 5 | 6 | namespace Item 7 | { 8 | class Item 9 | { 10 | public: 11 | Item(); 12 | Item(Entity::Entity* owningEntity, uint64 ID); 13 | 14 | uint64 getID() { return m_ID; } 15 | 16 | template 17 | void addComponent(std::unique_ptr component) 18 | { 19 | m_components[int(T::ID)] = std::move(component); 20 | } 21 | 22 | template 23 | T* getComponent() 24 | { 25 | int id = int(T::ID); 26 | auto it = m_components.find(id); 27 | if (it == m_components.end()) 28 | return nullptr; 29 | else 30 | return dynamic_cast(it->second.get()); 31 | } 32 | 33 | void changeOwningEntity(Entity::Entity* entity) 34 | { 35 | m_owningEntity = entity; 36 | } 37 | 38 | Entity::Entity* getOwningEntity() 39 | { 40 | return m_owningEntity; 41 | } 42 | 43 | std::unique_ptr clone(uint64 id); 44 | 45 | private: 46 | uint64 m_ID; 47 | std::unordered_map> m_components; 48 | Entity::Entity* m_owningEntity; 49 | }; 50 | } 51 | -------------------------------------------------------------------------------- /src/item/ItemFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "ItemFactory.h" 2 | 3 | #include "../util/FileUtil.h" 4 | 5 | #include "../components/SpriteComponent.h" 6 | 7 | namespace Item 8 | { 9 | ItemFactory::ItemFactory() 10 | : m_lastID(0) 11 | { 12 | } 13 | 14 | std::unique_ptr ItemFactory::createItem(std::string name) 15 | { 16 | if (m_templates.find(name) == m_templates.end()) 17 | createTemplate(name); 18 | 19 | return m_templates.find(name)->second->clone(++m_lastID); 20 | } 21 | 22 | void ItemFactory::createTemplate(std::string filePath) 23 | { 24 | std::string source = getFileContents("res/items" + filePath); 25 | nlohmann::json json = nlohmann::json::parse(source.c_str()); 26 | 27 | std::unique_ptr item = std::make_unique(); 28 | 29 | std::vector componentsJSON = json["components"]; 30 | 31 | for (unsigned int i=0; i < componentsJSON.size(); i++) 32 | { 33 | nlohmann::json componentJSON = componentsJSON[i]; 34 | 35 | using namespace Components; 36 | 37 | if (componentJSON["componentType"].get() == "Sprite") 38 | item->addComponent(std::make_unique(componentJSON)); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/item/ItemFactory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Item.h" 4 | 5 | #include "../util/json.hpp" 6 | 7 | namespace Item 8 | { 9 | class ItemFactory 10 | { 11 | public: 12 | ItemFactory(); 13 | 14 | std::unique_ptr createItem(std::string name); 15 | 16 | private: 17 | void createTemplate(std::string filePath); 18 | 19 | std::unordered_map> m_templates; 20 | uint64 m_lastID; 21 | }; 22 | } 23 | -------------------------------------------------------------------------------- /src/item/system/DrawingSystems.cpp: -------------------------------------------------------------------------------- 1 | #include "DrawingSystems.h" 2 | 3 | namespace Item 4 | { 5 | void SpriteDrawingSystem::update(const Timestep& ts, Item* item) 6 | { 7 | } 8 | } -------------------------------------------------------------------------------- /src/item/system/DrawingSystems.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "System.h" 4 | 5 | namespace Item 6 | { 7 | class SpriteDrawingSystem : public System 8 | { 9 | public: 10 | void update(const Timestep& ts, Item* item) override; 11 | }; 12 | } -------------------------------------------------------------------------------- /src/item/system/System.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Item.h" 4 | 5 | struct Timestep; 6 | 7 | namespace Item 8 | { 9 | class System 10 | { 11 | public: 12 | virtual void update(const Timestep& ts, Item* item) = 0; 13 | }; 14 | } -------------------------------------------------------------------------------- /src/level/Level.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "tile/TileMap.h" 6 | #include "../entity/Entity.h" 7 | #include "../entity/system/System.h" 8 | #include "../item/system/DrawingSystems.h" 9 | 10 | #include "../graphics/Renderer2D.h" 11 | 12 | #define WORLD_SIZE 100 13 | 14 | namespace Level 15 | { 16 | class Level 17 | { 18 | public: 19 | Level(uint width, uint height); 20 | 21 | void addEntity(std::unique_ptr entity); 22 | void removeEntity(Entity::Entity* entity); 23 | 24 | void render(sf::RenderWindow& window); 25 | void update(const Timestep& ts); 26 | void windowResize(Vec2 size); 27 | 28 | TileMap& getTiles() { return m_tiles; } 29 | 30 | private: 31 | sf::View m_view; 32 | TileMap m_tiles; 33 | 34 | std::unique_ptr m_renderSystem; 35 | 36 | std::vector> m_entities; 37 | std::vector> m_updateSystems; 38 | 39 | public: 40 | std::vector m_visualPath; 41 | 42 | Entity::Entity* player; 43 | Vec2i player_spawn; 44 | }; 45 | } 46 | -------------------------------------------------------------------------------- /src/level/LightMap.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include "../util/Random.h" 6 | 7 | #include "tile/Tile.h" 8 | #include "../util/Log.h" 9 | 10 | namespace Level 11 | { 12 | struct TileNode; 13 | 14 | // Each tile has one of these 15 | struct LightData 16 | { 17 | Color color; 18 | byte intensity, absorb; 19 | }; 20 | 21 | // Light emitter 22 | struct StaticLight 23 | { 24 | int32 x, y; 25 | Color color; 26 | byte intensity; 27 | }; 28 | 29 | class LightMap 30 | { 31 | public: 32 | int32 width, height; 33 | 34 | LightMap(std::vector>* tiles, int32 width, int32 height); 35 | ~LightMap(); 36 | 37 | // Try to rebuild the lighting if a rebuild is requested 38 | void rebuildLight(); 39 | // Render the light 40 | void renderLight(sf::RenderWindow& window); 41 | 42 | // Add and remove light sources 43 | void addStaticLight(StaticLight* light); 44 | void removeStaticLight(StaticLight* light); 45 | 46 | // Request rebuild of light if a tile or light source changes 47 | void requestRebuild() { m_requestedRebuild = true; } 48 | 49 | private: 50 | void resetLight() const; 51 | void buildLight(); 52 | 53 | // Flood tile neighbours with light 54 | void checkNeighbours(LightData* tile, int32 x, int32 y) const; 55 | 56 | // Add intensities to tiles 57 | void addIntensity(int32 x, int32 y, Color color, byte intensity) const; 58 | static void setIntensity(LightData* tile, Color color, byte intensity); 59 | 60 | // Returns the middle of the tile at x, y 61 | static Vec2 getTileCenter(int32 x, int32 y) { return Vec2(x * TILE_SIZE, y * TILE_SIZE); } 62 | // Returns the color of the tile at x, y 63 | Color getTileLight(int32 x, int32 y) const; 64 | 65 | // Used for rendering 66 | std::vector m_lightMask; 67 | 68 | bool m_requestedRebuild; 69 | std::vector m_staticLights; 70 | 71 | // Pointer to a TileLayer's tiles list 72 | std::vector>* m_tiles; 73 | }; 74 | } 75 | -------------------------------------------------------------------------------- /src/level/gen/Leaf.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../Common.h" 4 | 5 | #include "../../maths/maths.h" 6 | #include "../../util/Random.h" 7 | 8 | namespace WGenerator 9 | { 10 | class Leaf 11 | { 12 | public: 13 | Leaf(IntRectangle t_block, std::shared_ptr > t_generator, uint t_minSize, uint t_minHall, uint t_maxHall); 14 | 15 | bool split(); 16 | void createRooms(); 17 | 18 | std::shared_ptr room; 19 | IntRectangle block; 20 | 21 | std::vector halls; 22 | 23 | std::shared_ptr leftChild; 24 | std::shared_ptr rightChild; 25 | 26 | private: 27 | void createHall(std::shared_ptr left, std::shared_ptr right); 28 | std::shared_ptr getRoom(); 29 | 30 | uint m_minHall; 31 | uint m_maxHall; 32 | std::shared_ptr > generator; 33 | uint m_minSize; 34 | }; 35 | } 36 | -------------------------------------------------------------------------------- /src/level/gen/WorldGenerator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Leaf.h" 4 | 5 | #include "../../Types.h" 6 | #include "../tile/TileMap.h" 7 | #include "../../util/json.hpp" 8 | 9 | #include "../tile/TileMap.h" 10 | 11 | namespace WGenerator 12 | { 13 | struct Map 14 | { 15 | Level::TileMap::AddList addList; 16 | sf::Vector2 playerPosition; 17 | }; 18 | 19 | class WorldGenerator 20 | { 21 | public: 22 | WorldGenerator(std::string folderName); 23 | 24 | void generateMap(); 25 | Map getMap(); 26 | 27 | private: 28 | nlohmann::json mainJSON; 29 | nlohmann::json obligatoryJSON; 30 | nlohmann::json randomJSON; 31 | 32 | std::vector> getRooms(); 33 | std::vector> getRandomSquares(); 34 | std::vector> getHalls(); 35 | void render(std::vector>, byte>> data); 36 | 37 | void renderRooms(); 38 | void renderRoom(IntRectangle rect, nlohmann::json json); 39 | 40 | sf::Vector2 placePlayer(uint roomId); 41 | 42 | Random::Generator<> m_generator; 43 | std::vector> m_leafs; 44 | 45 | Level::TileMap::AddList addList; 46 | }; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /src/level/tile/Tile.cpp: -------------------------------------------------------------------------------- 1 | #include "Tile.h" 2 | 3 | #include "../../graphics/Renderable2D.h" 4 | #include "../../level/Level.h" 5 | #include "TileDatabase.h" 6 | 7 | namespace Level 8 | { 9 | Tile::Tile(byte id, int32 flags, const FloatRectangle& bounds) 10 | : m_data({id, flags, bounds}) 11 | { 12 | if (!renderable) 13 | renderable = new Graphics::Renderable2D({ 0, 0 }, { TILE_SIZE, TILE_SIZE }, sf::Color::White); 14 | } 15 | 16 | TileDefault::TileDefault(byte id, int32 flags, const FloatRectangle& bounds, const std::vector textures) 17 | : Tile(id, flags, bounds) 18 | , m_texture(textures[0]) 19 | { 20 | } 21 | 22 | void TileDefault::render(uint x, uint y, TileDatabase& database, Graphics::Renderer2D& renderer, std::vector>& layer) 23 | { 24 | Vec2& renderTilePos = renderable->m_position; 25 | renderTilePos.x = x * TILE_SIZE; 26 | renderTilePos.y = y * TILE_SIZE; 27 | 28 | float tx = m_texture.x * TILE_SIZE; 29 | float ty = m_texture.y * TILE_SIZE; 30 | 31 | // Pointer arithmetic is faster than [] operator 32 | Vec2* uvPtr = const_cast(&renderable->getUVs()[0]); 33 | *uvPtr++ = { tx, ty }; 34 | *uvPtr++ = { tx, ty + TILE_SIZE}; 35 | *uvPtr++ = { tx + TILE_SIZE, ty + TILE_SIZE }; 36 | *uvPtr = { tx + TILE_SIZE, ty }; 37 | 38 | renderer.submit(renderable); 39 | } 40 | 41 | TileWall::TileWall(byte id, int32 flags, const FloatRectangle& bounds, const std::vector textures) 42 | : Tile(id, flags, bounds) 43 | , m_sideTexture(textures[0]) 44 | , m_topTexture(textures[1]) 45 | { 46 | } 47 | 48 | void TileWall::render(uint x, uint y, TileDatabase& database, Graphics::Renderer2D& renderer, std::vector>& layer) 49 | { 50 | /// Render side 51 | Vec2& renderTilePos = renderable->m_position; 52 | renderTilePos.x = x * TILE_SIZE; 53 | 54 | if (layer[x][y + 1]->id != TileID::Dungeon_BrickWall) 55 | { 56 | renderTilePos.y = y * TILE_SIZE; 57 | 58 | float tx = m_sideTexture.x * TILE_SIZE; 59 | float ty = m_sideTexture.y * TILE_SIZE; 60 | 61 | // Pointer arithmetic is faster than [] operator 62 | Vec2* uvPtr = const_cast(&renderable->getUVs()[0]); 63 | *uvPtr++ = { tx, ty }; 64 | *uvPtr++ = { tx, ty + TILE_SIZE }; 65 | *uvPtr++ = { tx + TILE_SIZE, ty + TILE_SIZE }; 66 | *uvPtr = { tx + TILE_SIZE, ty }; 67 | 68 | renderer.submit(renderable); 69 | } 70 | 71 | /// Render top 72 | renderTilePos.y = y * TILE_SIZE - 32; 73 | 74 | float tx = m_topTexture.x * TILE_SIZE; 75 | float ty = m_topTexture.y * TILE_SIZE; 76 | 77 | // Pointer arithmetic is faster than [] operator 78 | Vec2* uvPtr = const_cast(&renderable->getUVs()[0]); 79 | *uvPtr++ = { tx, ty }; 80 | *uvPtr++ = { tx, ty + TILE_SIZE }; 81 | *uvPtr++ = { tx + TILE_SIZE, ty + TILE_SIZE }; 82 | *uvPtr = { tx + TILE_SIZE, ty }; 83 | 84 | renderer.submit(renderable, Graphics::Renderer2D::RenderOrder::AFTER); 85 | } 86 | } -------------------------------------------------------------------------------- /src/level/tile/Tile.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../Common.h" 4 | 5 | #include "../../maths/maths.h" 6 | 7 | #include 8 | 9 | namespace Graphics 10 | { 11 | class Renderable2D; 12 | class Renderer2D; 13 | } 14 | 15 | namespace Level 16 | { 17 | class Level; 18 | class TileDatabase; 19 | struct TileNode; 20 | 21 | enum class TileFlags : int32 22 | { 23 | PASSABLE = BIT(0), 24 | BREAKABLE = BIT(1) 25 | }; 26 | 27 | enum class TileID : byte 28 | { 29 | Void = 0, 30 | Dungeon_BrickWall = 1, 31 | Dungeon_BrickFloor = 2, 32 | 33 | End 34 | }; 35 | 36 | struct TileData 37 | { 38 | byte id; 39 | int32 flags; 40 | FloatRectangle bounds; 41 | }; 42 | 43 | static Graphics::Renderable2D* renderable; 44 | 45 | class Tile 46 | { 47 | public: 48 | Tile(byte id, int32 flags, const FloatRectangle& bounds); 49 | 50 | // Each tile can have its own render logic 51 | virtual void render(uint x, uint y, TileDatabase& database, Graphics::Renderer2D& renderer, std::vector>& layer) = 0; 52 | 53 | inline byte getID() const { return m_data.id; } 54 | 55 | inline const TileData& getData() const { return m_data; } 56 | 57 | inline bool hasFlag(int32 flag) const { return (bool)(m_data.flags & flag); } 58 | 59 | protected: 60 | TileData m_data; 61 | }; 62 | 63 | class TileDefault : public Tile 64 | { 65 | public: 66 | TileDefault(byte id, int32 flags, const FloatRectangle& bounds, const std::vector textures); 67 | 68 | void render(uint x, uint y, TileDatabase& database, Graphics::Renderer2D& renderer, std::vector>& layer) override; 69 | 70 | private: 71 | Vec2 m_texture; 72 | }; 73 | 74 | class TileWall : public Tile 75 | { 76 | public: 77 | TileWall(byte id, int32 flags, const FloatRectangle& bounds, const std::vector textures); 78 | 79 | void render(uint x, uint y, TileDatabase& database, Graphics::Renderer2D& renderer, std::vector>& layer) override; 80 | 81 | private: 82 | Vec2 m_sideTexture; 83 | Vec2 m_topTexture; 84 | }; 85 | } 86 | -------------------------------------------------------------------------------- /src/level/tile/TileCollision.cpp: -------------------------------------------------------------------------------- 1 | #include "TileCollision.h" 2 | 3 | #include "TileDatabase.h" 4 | 5 | namespace Level 6 | { 7 | std::pair tileCollision(Vec2 position, Vec2 velocity, FloatRectangle& bounds, const Timestep& ts) 8 | { 9 | Vec2 dest = position + velocity * ts.asSeconds(); 10 | 11 | Vec2i from = Vec2i((dest + bounds.getMinimumBound()) / TILE_SIZE); 12 | Vec2i to = Vec2i((dest + bounds.getMaximumBound()) / TILE_SIZE) + Vec2i{ 2, 2 }; 13 | 14 | bool collidingX = false, collidingY = false; 15 | for (int32 x = from.x; x < to.x; x++) 16 | for (int32 y = from.y; y < to.y; y++) 17 | { 18 | if (!State::Playing::get().isTilePassable(0, x, y)) 19 | { 20 | const FloatRectangle& tileCollisionBox = TileDatabase::get().getTileData(byte(State::Playing::get().getLevel().getTiles().getTile(0, x, y)->id)).bounds; 21 | 22 | FloatRectangle tileBounds; 23 | tileBounds.position = Vec2((float)x, (float)y) * TILE_SIZE + tileCollisionBox.position; 24 | tileBounds.size = tileCollisionBox.size; 25 | 26 | if (dest.x + bounds.getMaximumBound().x > tileBounds.x 27 | && dest.x + bounds.x < tileBounds.getMaximumBound().x 28 | && position.y + bounds.y + bounds.height > tileBounds.y 29 | && position.y + bounds.y < tileBounds.getMaximumBound().y) 30 | collidingX = true; 31 | 32 | if (position.x + bounds.x + bounds.width > tileBounds.x 33 | && position.x + bounds.x < tileBounds.getMaximumBound().x 34 | && dest.y + bounds.y + bounds.height > tileBounds.y 35 | && dest.y + bounds.y < tileBounds.getMaximumBound().y) 36 | collidingY = true; 37 | } 38 | } 39 | 40 | return std::make_pair(collidingX, collidingY); 41 | } 42 | } -------------------------------------------------------------------------------- /src/level/tile/TileCollision.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../maths/Rectangle.h" 4 | 5 | #include "../../app/states/StatePlaying.h" 6 | #include "../../level/tile/Tile.h" 7 | 8 | namespace Level 9 | { 10 | std::pair tileCollision(Vec2 position, Vec2 velocity, FloatRectangle& bounds, const Timestep& ts); 11 | } 12 | -------------------------------------------------------------------------------- /src/level/tile/TileDatabase.cpp: -------------------------------------------------------------------------------- 1 | #include "TileDatabase.h" 2 | 3 | #include "../../util/Log.h" 4 | 5 | namespace Level 6 | { 7 | TileDatabase::TileDatabase() 8 | : m_tiles((uint)TileID::End) 9 | { 10 | loadTile(TileID::Void, "Void"); 11 | loadTile(TileID::Dungeon_BrickFloor, "dungeon/BrickFloor"); 12 | loadTile(TileID::Dungeon_BrickWall, "dungeon/BrickWall"); 13 | ASSERT(getTile((byte)TileID::Void), "Void"); 14 | ASSERT(getTile((byte)TileID::Dungeon_BrickWall), "BrickWall"); 15 | ASSERT(getTile((byte)TileID::Dungeon_BrickFloor), "BrickFloor"); 16 | } 17 | 18 | inline Tile* TileDatabase::getTile(byte id) 19 | { 20 | return m_tiles[id].get(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/level/tile/TileDatabase.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Tile.h" 4 | 5 | #include "../../Common.h" 6 | #include "../../maths/maths.h" 7 | #include "../../util/Log.h" 8 | #include "../../util/json.hpp" 9 | #include "../../util/FileUtil.h" 10 | 11 | #include 12 | 13 | namespace Level 14 | { 15 | class TileDatabase 16 | { 17 | public: 18 | TileDatabase(); 19 | 20 | template 21 | void loadTile(const TileID& id, const std::string& filePath); 22 | 23 | Tile* getTile(byte id); 24 | 25 | static TileDatabase& get() { static TileDatabase db; return db; }; 26 | 27 | const TileData& getTileData(byte id) const { return m_tiles[id]->getData(); }; 28 | 29 | private: 30 | std::vector> m_tiles; 31 | }; 32 | 33 | template 34 | void TileDatabase::loadTile(const TileID& id, const std::string& filePath) 35 | { 36 | using json = nlohmann::json; 37 | auto source = getFileContents("res/tiles/" + filePath + ".json"); 38 | 39 | json json_ = json::parse(source.c_str()); 40 | 41 | ///TODO: Metadata 42 | 43 | int32 passable = json_["Passable"] ? (int32)TileFlags::PASSABLE : 0x0; 44 | int32 breakable = json_["Breakable"] ? (int32)TileFlags::BREAKABLE : 0x0; 45 | 46 | FloatRectangle bounds = { { 0, 0 },{ 0, 0 } }; 47 | if (passable == 0x0) 48 | { 49 | if (json_.find("CollisionBox") == json_.end()) 50 | LOG_WARN("[JSON] Entity with PhysicsComponent but without FloatRectangle! Setting to ", bounds, " as default."); 51 | else 52 | bounds = { { json_["CollisionBox"]["x"], json_["CollisionBox"]["y"] },{ json_["CollisionBox"]["width"], json_["CollisionBox"]["height"] } }; 53 | } 54 | 55 | std::vector textures_ = json_["Textures"]; 56 | std::vector textures; 57 | for (uint i = 0; i < textures_.size(); i++) 58 | { 59 | nlohmann::json at = textures_[i]; 60 | auto vec = at.get>(); 61 | textures.push_back(Vec2(vec[0], vec[1])); 62 | } 63 | 64 | m_tiles[(byte)id] = std::make_unique((byte)id, passable | breakable, bounds, textures); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/level/tile/TileFlooding.cpp: -------------------------------------------------------------------------------- 1 | #include "TileFlooding.h" 2 | 3 | namespace Util 4 | { 5 | std::vector> TileFlooding::tileFlooding(Vec2i entityPos, int radius) 6 | { 7 | std::vector> tiles; 8 | std::vector> temp; 9 | 10 | tiles.push_back(std::make_pair(sf::Vector2i(entityPos.x * 32, entityPos.y * 32), radius)); 11 | 12 | int currentCost = radius; 13 | while (currentCost != 0) 14 | { 15 | for (const auto& val : tiles) 16 | { 17 | Vec2i pos = val.first; 18 | Vec2i up = pos + Vec2i(0, 32); 19 | Vec2i down = pos + Vec2i(0, -32); 20 | Vec2i left = pos + Vec2i(-32, 0); 21 | Vec2i right = pos + Vec2i(32, 0); 22 | 23 | if (!hasCoord(tiles, up)) 24 | temp.push_back(std::make_pair(up, currentCost - 1)); 25 | 26 | if (!hasCoord(tiles, down)) 27 | temp.push_back(std::make_pair(down, currentCost - 1)); 28 | 29 | if (!hasCoord(tiles, left)) 30 | temp.push_back(std::make_pair(left, currentCost - 1)); 31 | 32 | if (!hasCoord(tiles, right)) 33 | temp.push_back(std::make_pair(right, currentCost - 1)); 34 | } 35 | 36 | for (const auto& val : temp) 37 | tiles.push_back(val); 38 | 39 | temp.clear(); 40 | 41 | currentCost--; 42 | } 43 | 44 | return tiles; 45 | } 46 | 47 | std::vector TileFlooding::getAllEntitesNearOtherEntity(Vec2i entityPos, int radius) 48 | { 49 | auto tiles = tileFlooding(entityPos, radius); 50 | 51 | std::vector entities; 52 | 53 | for (const auto& tile : tiles) 54 | { 55 | ///@TODO Fix when we have level ready 56 | //Entity::Entity* entity = level->getEntityOnTile(tile.first.x, tile.first.y); 57 | //if (entity != nullptr) 58 | // entities.push_back(entity); 59 | } 60 | 61 | return entities; 62 | } 63 | 64 | bool TileFlooding::hasCoord(const std::vector>& tiles, Vec2i toTest) 65 | { 66 | for (const auto& tile : tiles) 67 | if (tile.first == toTest) 68 | return true; 69 | 70 | return false; 71 | } 72 | } -------------------------------------------------------------------------------- /src/level/tile/TileFlooding.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../entity/Entity.h" 4 | #include "../../maths/maths.h" 5 | 6 | #include 7 | 8 | #include 9 | 10 | namespace Util 11 | { 12 | class TileFlooding 13 | { 14 | public: 15 | static std::vector getAllEntitesNearOtherEntity(Vec2i entityPos, int radius); 16 | static std::vector> tileFlooding(Vec2i entityPos, int radius); 17 | inline static bool hasCoord(const std::vector>& tiles, Vec2i toTest); 18 | }; 19 | } 20 | -------------------------------------------------------------------------------- /src/level/tile/TileMap.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../Common.h" 4 | 5 | #include "../../util/Random.h" 6 | 7 | #include "Tile.h" 8 | #include "../LightMap.h" 9 | 10 | #include "../../graphics/Renderer2D.h" 11 | 12 | namespace Level 13 | { 14 | struct TileNode 15 | { 16 | TileID id; 17 | byte metadata; 18 | LightData light; 19 | }; 20 | 21 | class TileMap 22 | { 23 | public: 24 | typedef std::vector> AddList; 25 | private: 26 | struct TileLayer 27 | { 28 | LightMap* lightMap; 29 | std::vector> tiles; 30 | 31 | ~TileLayer() 32 | { 33 | for (uint i = 0; i < tiles.size(); i++) 34 | tiles[i].clear(); 35 | 36 | delete lightMap; 37 | } 38 | }; 39 | 40 | public: 41 | TileMap(uint width, uint height); 42 | ~TileMap(); 43 | 44 | void addLayer(); 45 | void addTile(uint layer, uint x, uint y, byte id, byte metadata); 46 | void addTiles(uint layer, const AddList& tiles); 47 | 48 | void addStaticLight(uint layer, StaticLight* light); 49 | void removeStaticLight(uint layer, StaticLight* light); 50 | void requestRebuild(uint layer); 51 | 52 | void light(); 53 | 54 | void render(sf::RenderWindow& window, const IntRectangle& renderRegion); 55 | void renderLight(sf::RenderWindow& window); 56 | 57 | void presentBefore(sf::RenderWindow& window); 58 | void presentAfter(sf::RenderWindow& window); 59 | 60 | TileNode* getTile(uint layer, uint x, uint y); 61 | 62 | uint width; 63 | uint height; 64 | 65 | private: 66 | void qAddTile(uint layer, uint x, uint y, byte id, byte metadata); 67 | 68 | Graphics::Renderable2D m_renderTile; 69 | Graphics::Renderer2D m_renderer; 70 | sf::RenderStates m_renderState; 71 | std::vector m_layers; 72 | }; 73 | } 74 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "app/Application.h" 2 | 3 | #include 4 | 5 | #include "util/Log.h" 6 | 7 | #ifdef __WIN32 8 | #include "windows.h" 9 | #endif // __WIN32 10 | 11 | #include "util/Random.h" 12 | 13 | namespace 14 | { 15 | void errorMessage(const std::string& message) 16 | { 17 | #ifdef __WIN32 18 | MessageBox(nullptr, message.c_str(), "Error", MB_OK); 19 | #elif __linux 20 | const std::string command = "zenity --error --text \"" + message + "\""; 21 | system(command.c_str()); 22 | #elif __APPLE__ 23 | const std::string command = "osascript -e 'tell app \"System Events\" to display dialog \"" + message + "\" buttons {\"OK\"} default button 1 with icon caution with title \"Error\"'"; 24 | system(command.c_str()); 25 | #else 26 | LOG_ERROR(message); 27 | std::cin.ignore(); 28 | #endif 29 | } 30 | } 31 | 32 | int main() try 33 | { 34 | Random::init(); 35 | 36 | Application app("Project Comonidy", { 1280, 720, false, VSYNC_DISABLED }); 37 | app.start(); 38 | return EXIT_SUCCESS; 39 | } 40 | catch (std::out_of_range& e) 41 | { 42 | std::string msg = e.what(); 43 | errorMessage(msg); 44 | std::cin.ignore(); 45 | } 46 | catch(std::runtime_error& e) 47 | { 48 | std::string msg = e.what(); 49 | errorMessage(msg); 50 | std::cin.ignore(); 51 | } -------------------------------------------------------------------------------- /src/maths/Color.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Common.h" 4 | 5 | #include 6 | 7 | typedef sf::Color Color; 8 | 9 | inline bool canMixColors(const Color& c1, const Color& c2) 10 | { 11 | if (c2.r > c1.r) return true; 12 | if (c2.g > c1.g) return true; 13 | if (c2.b > c1.b) return true; 14 | 15 | return false; 16 | } 17 | 18 | inline Color mixColors(const Color& c1, const Color& c2) 19 | { 20 | Color result; 21 | 22 | result.a = 255; 23 | result.r = c1.r > c2.r ? c1.r : c2.r; 24 | result.g = c1.g > c2.g ? c1.g : c2.g; 25 | result.b = c1.b > c2.b ? c1.b : c2.b; 26 | 27 | return result; 28 | } 29 | 30 | inline Color applyIntensity(const Color& c, byte intensity) 31 | { 32 | float k = intensity >= LIGHT_ABSOLUTE ? 1.f : static_cast(intensity) / LIGHT_ABSOLUTE; 33 | 34 | return Color(static_cast(c.r * k), static_cast(c.g * k), static_cast(c.b * k), 255); 35 | } 36 | 37 | inline Color reapplyIntensity(const Color& c, byte intensityOld, byte intensityNew) 38 | { 39 | float k1 = intensityNew >= LIGHT_ABSOLUTE ? 1.f : static_cast(intensityNew) / LIGHT_ABSOLUTE, 40 | k2 = intensityOld >= LIGHT_ABSOLUTE ? 1.f : static_cast(intensityOld) / LIGHT_ABSOLUTE; 41 | 42 | return Color(static_cast(c.r * k1 / k2), static_cast(c.g * k1 / k2), static_cast(c.b * k1 / k2), 255); 43 | } 44 | -------------------------------------------------------------------------------- /src/maths/Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include "Rectangle.h" 2 | 3 | FloatRectangle::FloatRectangle() 4 | : position(Vec2()), size(Vec2()) 5 | { 6 | } 7 | 8 | FloatRectangle::FloatRectangle(const Vec2& position, const Vec2& size) 9 | : position(position), size(size) 10 | { 11 | } 12 | 13 | FloatRectangle::FloatRectangle(float x, float y, float width, float height) 14 | : position(Vec2(x, y)), size(Vec2(width, height)) 15 | { 16 | } 17 | 18 | bool FloatRectangle::intersects(const FloatRectangle& other) const 19 | { 20 | return (size > other.position && position < other.size) || (position > other.size && size < other.position); 21 | } 22 | 23 | bool FloatRectangle::contains(const Vec2& point) const 24 | { 25 | return point.x > getMinimumBound().x && point.y > getMinimumBound().y && point.x < getMaximumBound().x && point.y < getMaximumBound().y; 26 | } 27 | 28 | IntRectangle::IntRectangle() 29 | : x(0) 30 | , y(0) 31 | , width(0) 32 | , height(0) 33 | { 34 | } 35 | 36 | IntRectangle::IntRectangle(int32 x, int32 y, int32 width, int32 height) 37 | : x(x) 38 | , y(y) 39 | , width(width) 40 | , height(height) 41 | { 42 | } 43 | 44 | IntRectangle::IntRectangle(const Vec2i& min, const Vec2i& max) 45 | : x(min.x) 46 | , y(min.y) 47 | , width(max.x) 48 | , height(max.y) 49 | { 50 | } 51 | 52 | bool IntRectangle::intersects(const IntRectangle & other) const 53 | { 54 | return (size > other.position && position < other.size) || (position > other.size && size < other.position); 55 | } 56 | 57 | bool IntRectangle::contains(const Vec2i& point) 58 | { 59 | return point.x > getMinimumBound().x && point.y > getMinimumBound().y && point.x < getMaximumBound().x && point.y < getMaximumBound().y; 60 | } -------------------------------------------------------------------------------- /src/maths/Rectangle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Vec2.h" 4 | 5 | #include "../Types.h" 6 | 7 | struct FloatRectangle 8 | { 9 | union 10 | { 11 | Vec2 position; 12 | struct 13 | { 14 | float x; 15 | float y; 16 | }; 17 | }; 18 | union 19 | { 20 | Vec2 size; 21 | struct 22 | { 23 | float width; 24 | float height; 25 | }; 26 | }; 27 | 28 | FloatRectangle(); 29 | FloatRectangle(const Vec2& position, const Vec2& size); 30 | FloatRectangle(float x, float y, float width, float height); 31 | 32 | bool intersects(const FloatRectangle& other) const; 33 | bool contains(const Vec2& point) const; 34 | 35 | Vec2 getMinimumBound() const { return position; } 36 | Vec2 getMaximumBound() const { return position + size; } 37 | }; 38 | 39 | struct IntRectangle 40 | { 41 | union 42 | { 43 | Vec2i position; 44 | struct 45 | { 46 | int32 x; 47 | int32 y; 48 | }; 49 | }; 50 | union 51 | { 52 | Vec2i size; 53 | struct 54 | { 55 | int32 width; 56 | int32 height; 57 | }; 58 | }; 59 | 60 | IntRectangle(); 61 | IntRectangle(const Vec2i& min, const Vec2i& max); 62 | IntRectangle(int32 x, int32 y, int32 width, int32 height); 63 | 64 | bool intersects(const IntRectangle& other) const; 65 | bool contains(const Vec2i& point); 66 | 67 | Vec2i getMinimumBound() const { return position; } 68 | Vec2i getMaximumBound() const { return position + size; } 69 | }; 70 | -------------------------------------------------------------------------------- /src/maths/Vec2.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Types.h" 4 | 5 | #include 6 | 7 | #include 8 | 9 | typedef sf::Vector2f Vec2; 10 | typedef sf::Vector2i Vec2i; 11 | typedef sf::Vector2 Vec2ui; 12 | 13 | inline bool operator<(const Vec2& v1, const Vec2& v2) 14 | { 15 | return v1.x < v2.x && v1.y < v2.y; 16 | } 17 | 18 | inline bool operator>(const Vec2& v1, const Vec2& v2) 19 | { 20 | return v1.x > v2.x && v1.y > v2.y; 21 | } 22 | 23 | inline bool operator<(const Vec2i& v1, const Vec2i& v2) 24 | { 25 | return v1.x < v2.x && v1.y < v2.y; 26 | } 27 | 28 | inline bool operator>(const Vec2i& v1, const Vec2i& v2) 29 | { 30 | return v1.x > v2.x && v1.y > v2.y; 31 | } 32 | 33 | inline float distance(const Vec2& v1, const Vec2& v2) 34 | { 35 | double dx = v1.x - v2.x; 36 | double dy = v1.y - v2.y; 37 | return static_cast(sqrt(dx * dx + dy * dy)); 38 | } 39 | 40 | inline float distance(const Vec2i& v1, const Vec2i& v2) 41 | { 42 | double dx = v1.x - v2.x; 43 | double dy = v1.y - v2.y; 44 | return static_cast(sqrt(dx * dx + dy * dy)); 45 | } 46 | 47 | inline float magnitude(const Vec2& vect) 48 | { 49 | return float(sqrt(vect.x * vect.x + vect.y * vect.y)); 50 | } 51 | 52 | inline float magnitude(const Vec2i& vect) 53 | { 54 | return float(sqrt(vect.x * vect.x + vect.y * vect.y)); 55 | } 56 | 57 | inline void normalize(Vec2& vect) 58 | { 59 | auto length = magnitude(vect); 60 | vect.x /= length; 61 | vect.y /= length; 62 | } 63 | 64 | inline void normalize(Vec2i& vect) 65 | { 66 | auto length = magnitude(vect); 67 | vect.x /= static_cast(length); 68 | vect.y /= static_cast(length); 69 | } 70 | 71 | inline float dot(const Vec2& a, const Vec2& b) 72 | { 73 | auto x = a.x * b.x; 74 | auto y = a.y * b.y; 75 | 76 | return x + y; 77 | } 78 | 79 | inline float dot(const Vec2i& a, const Vec2i& b) 80 | { 81 | auto x = a.x * b.x; 82 | auto y = a.y * b.y; 83 | 84 | return static_cast(x + y); 85 | } -------------------------------------------------------------------------------- /src/maths/maths.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Types.h" 4 | 5 | #include "Vec2.h" 6 | #include "Rectangle.h" 7 | #include "maths_func.h" 8 | -------------------------------------------------------------------------------- /src/maths/maths_func.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define PI 3.14159265358f 6 | 7 | inline float toRadians(float degrees) 8 | { 9 | return static_cast(degrees * (PI / 180.0f)); 10 | } 11 | 12 | inline float toDegrees(float radians) 13 | { 14 | return static_cast(radians * (180.0f / PI)); 15 | } 16 | 17 | inline float clamp(float value, float minimum, float maximum) 18 | { 19 | return (value > minimum) ? (value < maximum) ? value : maximum : minimum; 20 | } -------------------------------------------------------------------------------- /src/resources/ResourceHolder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ResourceManager.h" 4 | 5 | #include 6 | #include 7 | 8 | struct ResourceHolder 9 | { 10 | ResourceHolder() 11 | : textures("res/textures/", ".png") 12 | , fonts("res/fonts/", ".ttf") 13 | , soundBuffers("res/sound/sfx/", ".ogg") 14 | {} 15 | 16 | ResourceManager textures; 17 | ResourceManager fonts; 18 | ResourceManager soundBuffers; 19 | }; 20 | -------------------------------------------------------------------------------- /src/resources/ResourceManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | template 6 | class ResourceManager 7 | { 8 | public: 9 | ResourceManager(std::string&& path, std::string&& extension) 10 | : m_path(std::move(path)) 11 | , m_extension(std::move(extension)) 12 | {} 13 | 14 | const Resource& get(const std::string& name) 15 | { 16 | std::string full = m_path + name + m_extension; 17 | 18 | if (m_resourceMap.find(full) == m_resourceMap.end()) 19 | add(full); 20 | 21 | return m_resourceMap[full]; 22 | } 23 | 24 | void add(const std::string& name) 25 | { 26 | Resource res; 27 | res.loadFromFile(name); 28 | m_resourceMap.insert(std::make_pair(name, res)); 29 | } 30 | 31 | private: 32 | std::string m_path; 33 | std::string m_extension; 34 | 35 | std::unordered_map m_resourceMap; 36 | }; 37 | -------------------------------------------------------------------------------- /src/sound/Music.cpp: -------------------------------------------------------------------------------- 1 | #include "Music.h" 2 | 3 | #include "../app/Application.h" 4 | 5 | namespace Sound 6 | { 7 | void Music::loadMusic() 8 | { 9 | challenge.openFromFile("res/sound/music/Challenge.ogg"); 10 | lunar_streams.openFromFile("res/sound/music/LunarStreams.ogg"); 11 | menu.openFromFile("res/sound/music/Menu.ogg"); 12 | 13 | sf::Music music; 14 | } 15 | 16 | void Music::play(sf::Music& music) 17 | { 18 | music.play(); 19 | } 20 | 21 | void Music::pause(sf::Music& music) 22 | { 23 | music.pause(); 24 | } 25 | } -------------------------------------------------------------------------------- /src/sound/Music.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace Sound 6 | { 7 | class Music // Background music 8 | { 9 | public: 10 | sf::Music challenge; 11 | sf::Music lunar_streams; 12 | sf::Music menu; 13 | 14 | void loadMusic(); 15 | 16 | static void play(sf::Music& music); 17 | static void pause(sf::Music& music); 18 | }; 19 | } -------------------------------------------------------------------------------- /src/sound/SoundFX.cpp: -------------------------------------------------------------------------------- 1 | #include "SoundFX.h" 2 | 3 | #include "../app/Application.h" 4 | 5 | namespace Sound 6 | { 7 | const sf::SoundBuffer* SoundFX::explosion = nullptr; 8 | 9 | void SoundFX::loadSounds() 10 | { 11 | explosion = &Application::get().getResources().soundBuffers.get("explosion"); 12 | 13 | sf::SoundBuffer buffer; 14 | sf::Sound sound; 15 | } 16 | 17 | SoundFX::SoundFX(sf::Sound sound) 18 | : m_sound(sound) 19 | { 20 | } 21 | 22 | SoundFX::~SoundFX() 23 | { 24 | } 25 | 26 | void SoundFX::play(const sf::SoundBuffer& buffer) 27 | { 28 | m_sound.setBuffer(buffer); 29 | m_sound.play(); 30 | } 31 | 32 | void SoundFX::setVolume(float volume) 33 | { 34 | m_sound.setVolume(volume); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/sound/SoundFX.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace Sound 6 | { 7 | class SoundFX // Sound effects 8 | { 9 | public: 10 | static const sf::SoundBuffer* explosion; 11 | 12 | static void loadSounds(); 13 | 14 | protected: 15 | sf::Sound m_sound; 16 | 17 | public: 18 | SoundFX(sf::Sound sound); 19 | ~SoundFX(); 20 | 21 | void play(const sf::SoundBuffer& buffer); 22 | void setVolume(float volume); 23 | }; 24 | } -------------------------------------------------------------------------------- /src/util/FileUtil.cpp: -------------------------------------------------------------------------------- 1 | #include "FileUtil.h" 2 | 3 | // Code is referenced from: 4 | // https://vicidi.wordpress.com/2015/03/09/reading-utf-file-with-bom-to-utf-8-encoded-stdstring-in-c11-on-windows/ 5 | 6 | #include 7 | #include 8 | 9 | #define ENCODING_ASCII 0 10 | #define ENCODING_UTF8 1 11 | 12 | std::string getFileContents(const std::string& filePath) 13 | { 14 | std::string result; 15 | std::ifstream ifs(filePath.c_str(), std::ios::binary); 16 | std::stringstream ss; 17 | 18 | if (!ifs.is_open()) 19 | { 20 | result.clear(); // Unable to read file 21 | return result; 22 | } 23 | 24 | if (ifs.eof()) 25 | result.clear(); 26 | else 27 | { 28 | int ch1 = ifs.get(); 29 | int ch2 = ifs.get(); 30 | int ch3 = ifs.get(); 31 | if (!(ch1 == 0xef && ch2 == 0xbb && ch3 == 0xbf)) 32 | ifs.seekg(0); 33 | } 34 | ss << ifs.rdbuf() << '\0'; 35 | result = ss.str(); 36 | 37 | return result; 38 | } -------------------------------------------------------------------------------- /src/util/FileUtil.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | std::string getFileContents(const std::string& filePath); -------------------------------------------------------------------------------- /src/util/PriorityQueue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | template 7 | struct PriorityQueue 8 | { 9 | typedef std::pair PQElement; 10 | std::priority_queue, std::greater> elements; 11 | 12 | bool empty() const { return elements.empty(); } 13 | 14 | void put(T item, priority_t priority) 15 | { 16 | elements.emplace(priority, item); 17 | } 18 | 19 | T get() 20 | { 21 | T best_item = elements.top().second; 22 | elements.pop(); 23 | return best_item; 24 | } 25 | }; -------------------------------------------------------------------------------- /src/util/Random.cpp: -------------------------------------------------------------------------------- 1 | #include "Random.h" 2 | 3 | namespace Random 4 | { 5 | namespace 6 | { 7 | std::minstd_rand rd; 8 | } 9 | 10 | void init() 11 | { 12 | rd.seed(static_cast(std::time(nullptr))); 13 | } 14 | 15 | int32 intInRange(int32 lowBound, int32 highBound) 16 | { 17 | std::uniform_int_distribution dist(lowBound, highBound); 18 | return dist(rd); 19 | } 20 | 21 | uint64 uint64InRange(uint64 lowBound, uint64 highBound) 22 | { 23 | std::uniform_int_distribution dist(lowBound, highBound); 24 | return dist(rd); 25 | } 26 | 27 | char getRandomChar() 28 | { 29 | return char('A' + intInRange(0, 23)); 30 | } 31 | } -------------------------------------------------------------------------------- /src/util/Random.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Types.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace Random 9 | { 10 | void init(); 11 | int32 intInRange(int32 lowBound, int32 highBound); 12 | uint64 uint64InRange(uint64 lowBound, uint64 highBound); 13 | char getRandomChar(); 14 | 15 | template 16 | class Generator 17 | { 18 | public: 19 | Generator(uint64 seed = std::time(nullptr)) 20 | { 21 | m_device.seed(static_cast(seed)); 22 | } 23 | 24 | void setSeed(uint64 seed) 25 | { 26 | m_device.seed(static_cast(seed)); 27 | } 28 | 29 | int32 intInRange(int32 lowBound, int32 highBound) 30 | { 31 | std::uniform_int_distribution dist(lowBound, highBound); 32 | return dist(m_device); 33 | } 34 | 35 | uint64 uint64InRange(uint64 lowBound, uint64 highBound) 36 | { 37 | std::uniform_int_distribution dist(lowBound, highBound); 38 | return dist(m_device); 39 | } 40 | 41 | char getRandomChar() 42 | { 43 | return char('A' + intInRange(0, 23)); 44 | } 45 | 46 | private: 47 | RandomEngine m_device; 48 | }; 49 | } 50 | -------------------------------------------------------------------------------- /src/util/Timestep.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | struct Timestep 4 | { 5 | private: 6 | float m_timestep; 7 | float m_lastTime; 8 | public: 9 | explicit Timestep(float initialTime) 10 | : m_timestep(0.0f), m_lastTime(initialTime) 11 | { 12 | } 13 | 14 | void update(float currentTime) 15 | { 16 | m_timestep = currentTime - m_lastTime; 17 | m_lastTime = currentTime; 18 | } 19 | 20 | float asSeconds() const { return asMillis() * 0.001f; } 21 | float asMillis() const { return m_timestep; } 22 | }; -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(Community-Game-test) 4 | 5 | add_library(Catch INTERFACE) 6 | target_include_directories(Catch INTERFACE catch) 7 | 8 | add_executable(${PROJECT_NAME} ${TESTS_DIR}) 9 | target_link_libraries(${PROJECT_NAME} Catch) 10 | --------------------------------------------------------------------------------