├── .gitattributes ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── bindgen ├── ast │ ├── ast.tl │ ├── comment.tl │ ├── dependency.tl │ ├── enum.tl │ ├── func.tl │ ├── header.tl │ ├── init.tl │ ├── module.tl │ ├── op.tl │ ├── struct.tl │ ├── symbol.tl │ └── type.tl ├── ast_common.tl ├── ast_reader.tl ├── data │ ├── animation.tl │ ├── audio │ │ ├── music.tl │ │ └── sound.tl │ ├── collision.tl │ ├── color.tl │ ├── core.tl │ ├── font.tl │ ├── intersections.tl │ ├── math │ │ ├── grid.tl │ │ ├── rect.tl │ │ ├── rectf.tl │ │ ├── segment.tl │ │ ├── shape.tl │ │ ├── vec2.tl │ │ └── vec2i.tl │ ├── texture.tl │ ├── texture_atlas.tl │ └── tilemap.tl ├── dependencies.tl ├── generate_carp.tl ├── generate_nelua.tl ├── generator.tl ├── generators │ ├── carp.tl │ └── nelua.tl └── tools │ ├── fun.tl │ └── utils.tl ├── bindings ├── carp │ └── nene │ │ ├── animation.carp │ │ ├── audio │ │ ├── music.carp │ │ └── sound.carp │ │ ├── collision.carp │ │ ├── color.carp │ │ ├── core.carp │ │ ├── font.carp │ │ ├── intersections.carp │ │ ├── math │ │ ├── grid.carp │ │ ├── rect.carp │ │ ├── rectf.carp │ │ ├── segment.carp │ │ ├── shape.carp │ │ ├── vec2.carp │ │ └── vec2i.carp │ │ ├── sdl │ │ ├── sdl2.carp │ │ ├── sdl2_image.carp │ │ ├── sdl2_mixer.carp │ │ └── sdl2_ttf.carp │ │ ├── texture.carp │ │ ├── texture_atlas.carp │ │ └── tilemap.carp └── nelua │ └── nene │ ├── animation.nelua │ ├── audio │ ├── music.nelua │ └── sound.nelua │ ├── collision.nelua │ ├── color.nelua │ ├── core.nelua │ ├── font.nelua │ ├── intersections.nelua │ ├── math │ ├── grid.nelua │ ├── rect.nelua │ ├── rectf.nelua │ ├── segment.nelua │ ├── shape.nelua │ ├── vec2.nelua │ └── vec2i.nelua │ ├── raw │ ├── sdl2.nelua │ ├── sdl2_image.nelua │ ├── sdl2_mixer.nelua │ └── sdl2_ttf.nelua │ ├── texture.nelua │ ├── texture_atlas.nelua │ └── tilemap.nelua ├── compile_flags.txt ├── examples ├── c │ ├── CMakeLists.txt │ └── collisions.c ├── carp │ ├── minimal.carp │ └── reflected_pong.carp └── nelua │ ├── .neluacfg.lua │ ├── camera.nelua │ ├── game_controller.nelua │ ├── pong.nelua │ ├── rects.nelua │ ├── render_clip.nelua │ ├── segments_intersection.nelua │ ├── sound_and_music.nelua │ ├── sprite_animation.nelua │ ├── texture_atlases.nelua │ └── tilemaps.nelua ├── external └── CMakeLists.txt ├── gen-docs.lua ├── genbindings.sh ├── include └── nene │ ├── animation.h │ ├── audio │ ├── music.h │ └── sound.h │ ├── collision.h │ ├── color.h │ ├── config.h │ ├── core.h │ ├── font.h │ ├── impl │ └── utils.h │ ├── intersections.h │ ├── math │ ├── grid.h │ ├── rect.h │ ├── rectf.h │ ├── segment.h │ ├── shape.h │ ├── vec2.h │ └── vec2i.h │ ├── nene.h │ ├── texture.h │ ├── texture_atlas.h │ └── tilemap.h ├── resources ├── 8bit Bossa.ogg ├── img_shapes.png ├── monogram_extended.ttf └── pixelclick.wav ├── src ├── CMakeLists.txt ├── animation.c ├── audio │ ├── music.c │ └── sound.c ├── collision.c ├── color.c ├── core.c ├── font.c ├── impl │ └── utils.c ├── intersections.c ├── math │ ├── grid.c │ ├── rect.c │ ├── rectf.c │ ├── segment.c │ ├── shape.c │ ├── vec2.c │ └── vec2i.c ├── texture.c ├── texture_atlas.c └── tilemap.c └── tlconfig.lua /.gitattributes: -------------------------------------------------------------------------------- 1 | *.nelua text linguist-language=lua 2 | *.carp text linguist-language=clojure 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /nelua_cache/ 2 | /libnene/ 3 | /build/ 4 | /dumps/ 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "external/SDL"] 2 | path = external/SDL 3 | url = https://github.com/libsdl-org/SDL.git 4 | [submodule "external/SDL_ttf"] 5 | path = external/SDL_ttf 6 | url = https://github.com/libsdl-org/SDL_ttf.git 7 | [submodule "external/SDL_image"] 8 | path = external/SDL_image 9 | url = https://github.com/libsdl-org/SDL_image 10 | [submodule "external/SDL_mixer"] 11 | path = external/SDL_mixer 12 | url = https://github.com/libsdl-org/SDL_mixer 13 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021-present André Luiz Alvares 2 | # Nene is licensed under the Zlib license. 3 | # Please refer to the LICENSE file for details 4 | # SPDX-License-Identifier: Zlib 5 | 6 | # Set minimum cmake version, this should go until the latest available release, 7 | # NOTE: if version interval it's changed, be sure to also update the examples/c/CMakeLists.txt file 8 | cmake_minimum_required(VERSION 3.23-3.26) 9 | 10 | # Set the project name, version and language 11 | project( 12 | nene 13 | VERSION 0.4 14 | DESCRIPTION "Game framework on top of SDL aimed for multiple programming languages" 15 | LANGUAGES C 16 | ) 17 | 18 | # add the src and external directories on the building procedure 19 | add_subdirectory(external) 20 | add_subdirectory(src) 21 | 22 | # also add examples directory 23 | add_subdirectory(examples/c) 24 | 25 | # nene installation 26 | install(TARGETS nene external_libs 27 | LIBRARY DESTINATION lib 28 | ARCHIVE DESTINATION lib 29 | RUNTIME DESTINATION bin 30 | INCLUDES DESTINATION include 31 | FILE_SET HEADERS 32 | ) 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | zlib license 2 | 3 | Copyright (c) 2021-present André Luiz Alvares 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgment in the product documentation would be 16 | appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Note! 2 | 3 | This project moved to [CodeBerg](https://codeberg.org/Nene/nene)! 4 | 5 | # nene 6 | [![Matrix](https://img.shields.io/matrix/nene-and-friends:matrix.org?label=Matrix%20chat&logo=matrix)](https://matrix.to/#/#nene-and-friends:matrix.org) 7 | 8 | `Nene` it's a little game framework built on top of `SDL2` and it's extension libraries (`SDL2_image`, `SDL2_ttf`, `SDL2_mixer`). 9 | 10 | > Eventually Nene will upgrade to `SDL3` 11 | 12 | ## Dependencies 13 | 14 | ### Nene library (runtime, used on games) 15 | These are bundled with `Nene` and thus no download it's required, more information below. 16 | 17 | - SDL2 `2.26.5` 18 | - SDL2_image `2.6.3` 19 | - SDL2_ttf `2.20.2` 20 | - SDL2_mixer `2.6.3` 21 | 22 | ### Building Nene library 23 | Installed externally, like on Visual Studio Installer or your system package manager. 24 | 25 | - C compiler (tested: Clang, MSVC, GCC) 26 | - CMake 27 | - Git 28 | - [Ninja](https://ninja-build.org/) (Optional, but recommended) 29 | 30 | ### Generating Nene bindings 31 | For now this is only expected to work on Linux, better support for Windows will be written, however 32 | bindings are already generated on the repository, thus you don't need to generate them unless you change 33 | `Nene` and/or the generators. 34 | 35 | - Lua 36 | - Teal 37 | - clang-check 38 | - Bash 39 | 40 | To generate bindings: 41 | 42 | ```sh 43 | $ sh genbindings.sh 44 | ``` 45 | 46 | ## Building 47 | 48 | First, be sure to install a C compiler and CMake. 49 | 50 | > **Note** 51 | > 52 | > The following steps were reproduced on Fedora Linux 38 53 | 54 | --- 55 | 56 | Clone `nene` with it's dependencies bundled: 57 | 58 | ``` 59 | $ git clone https://github.com/Andre-LA/nene.git --recurse-submodules 60 | ``` 61 | 62 | Generate the building template of CMake: 63 | 64 | ``` 65 | $ cmake -S . -B build -G Ninja 66 | ``` 67 | 68 | > **Note** 69 | > 70 | > To use a different C compiler from the default, append the `CMAKE_C_COMPILER` option, example: `-D CMAKE_C_COMPILER=clang` where `clang` is the C compiler of choice. 71 | 72 | Build Nene and it's dependencies: 73 | 74 | ``` 75 | $ cmake --build build 76 | ``` 77 | 78 | To properly organize the files, install it to the local `libnene` subdirectory: 79 | 80 | ``` 81 | $ cmake --install build --prefix libnene 82 | ``` 83 | 84 | And that's it! Nene development build it's properly done at the `libnene` subdirectory. 85 | 86 | ## Usage 87 | 88 | > **Note** 89 | > This section it's still being written. 90 | 91 | ## Examples 92 | Nene doesn't contains a tutorial yet, but you can find some usage examples on the `examples` directory: 93 | 94 | - [Camera](examples/nelua/camera.nelua) 95 | - [Pong](examples/nelua/pong.nelua) 96 | - [Rects](examples/nelua/rects.nelua) 97 | - [Render clip](examples/nelua/render_clip.nelua) 98 | - [Sound and music](examples/nelua/sound_and_music.nelua) 99 | - [Sprite animation](examples/nelua/sprite_animation.nelua) 100 | - [Tilemap](examples/nelua/tilemap.nelua) 101 | - [Web](examples/nelua/web.nelua) 102 | 103 | Screenshot with some of the examples above (these shapes are sprites): 104 | ![image](https://user-images.githubusercontent.com/8538122/127941148-8597cb04-1bac-49cc-9ba1-909f199be996.png) 105 | 106 | ## License 107 | The license of `nene` it's the same as SDL2: zlib license. 108 | -------------------------------------------------------------------------------- /bindgen/ast/ast.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local Comment = require 'bindgen.ast.comment' 9 | local Enum = require 'bindgen.ast.enum' 10 | local Func = require 'bindgen.ast.func' 11 | local Header = require 'bindgen.ast.header' 12 | local Module = require 'bindgen.ast.module' 13 | local Op = require 'bindgen.ast.op' 14 | local Struct = require 'bindgen.ast.struct' 15 | local Symbol = require 'bindgen.ast.symbol' 16 | local Type = require 'bindgen.ast.type' 17 | local Dependency = require 'bindgen.ast.dependency' 18 | 19 | local AST = { 20 | Comment = Comment, 21 | Enum = Enum, 22 | Func = Func, 23 | Header = Header, 24 | Module = Module, 25 | Dependency = Dependency, 26 | Op = Op, 27 | Struct = Struct, 28 | Symbol = Symbol, 29 | Type = Type, 30 | } 31 | 32 | return AST 33 | -------------------------------------------------------------------------------- /bindgen/ast/comment.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local record Comment 9 | -- an array of lines of text 10 | text: {string} 11 | end 12 | 13 | local Comment_mt: metatable = { __index = Comment } 14 | 15 | function Comment.new(init: Comment): Comment 16 | local com: Comment = { 17 | text = assert(init.text, "you must set 'text' field."), 18 | } 19 | return setmetatable(com, Comment_mt) 20 | end 21 | 22 | function Comment:append_line(line: string) 23 | table.insert(self.text, line) 24 | end 25 | 26 | function Comment:append_paragraph(paragraph: {string}) 27 | for i=1, #paragraph do 28 | self:append_line(paragraph[i]) 29 | end 30 | end 31 | 32 | function Comment:iter_lines(): (function(): integer, string) 33 | return ipairs(self.text) 34 | end 35 | 36 | return Comment 37 | 38 | -------------------------------------------------------------------------------- /bindgen/ast/dependency.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local record Dependency 9 | module: string 10 | end 11 | 12 | local Dependency_mt: metatable = { __index = Dependency } 13 | 14 | function Dependency.new(init: Dependency): Dependency 15 | local module: Dependency = { 16 | module = assert(init.module, "you must set 'module' field."), 17 | } 18 | return setmetatable(module, Dependency_mt) 19 | end 20 | 21 | return Dependency 22 | -------------------------------------------------------------------------------- /bindgen/ast/enum.tl: -------------------------------------------------------------------------------- 1 | 2 | --[[ 3 | Copyright (c) 2021-present André Luiz Alvares 4 | Nene is licensed under the Zlib license. 5 | Please refer to the LICENSE file for details 6 | SPDX-License-Identifier: Zlib 7 | ]] 8 | 9 | local Fun = require 'bindgen.tools.fun' 10 | 11 | local Symbol = require 'bindgen.ast.symbol' 12 | 13 | local record Enum 14 | record Constant 15 | sym: Symbol 16 | constant: integer 17 | end 18 | 19 | sym: Symbol 20 | constants: {Constant} 21 | end 22 | 23 | -- Enum 24 | local Enum_mt: metatable = { __index = Enum } 25 | local EnumConstant_mt: metatable = { __index = Enum.Constant } 26 | 27 | function Enum.Constant.new(init: Enum.Constant): Enum.Constant 28 | local constant: Enum.Constant = { 29 | sym = Symbol.new(assert(init.sym, "you must set 'sym' field.")), 30 | constant = assert(init.constant, "you must set 'constant' field."), 31 | } 32 | return setmetatable(constant, EnumConstant_mt) 33 | end 34 | 35 | function Enum.new(init: Enum): Enum 36 | local enum: Enum = { 37 | sym = Symbol.new(assert(init.sym, "you must set 'sym' field.")), 38 | constants = Fun.map( 39 | assert(init.constants, "you must set 'constants' field."), 40 | function(c: Enum.Constant): Enum.Constant return Enum.Constant.new(c) end 41 | ), 42 | } 43 | return setmetatable(enum, Enum_mt) 44 | end 45 | 46 | return Enum 47 | -------------------------------------------------------------------------------- /bindgen/ast/header.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local record Header 9 | header: string 10 | end 11 | 12 | local Header_mt: metatable
= { __index = Header } 13 | 14 | function Header.new(init: Header): Header 15 | local header: Header = { 16 | header = assert(init.header, "you must set 'header' field."), 17 | } 18 | return setmetatable(header, Header_mt) 19 | end 20 | 21 | return Header 22 | -------------------------------------------------------------------------------- /bindgen/ast/init.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local ast = require 'bindgen.ast.ast' 9 | return ast 10 | -------------------------------------------------------------------------------- /bindgen/ast/module.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local Fun = require 'bindgen.tools.fun' 9 | 10 | local Dependency = require 'bindgen.ast.dependency' 11 | local Comment = require 'bindgen.ast.comment' 12 | local Enum = require 'bindgen.ast.enum' 13 | local Struct = require 'bindgen.ast.struct' 14 | local Func = require 'bindgen.ast.func' 15 | local Op = require 'bindgen.ast.op' 16 | 17 | local record Module 18 | dependencies: {Dependency} 19 | name: string 20 | comment: Comment 21 | enums: {Enum} 22 | structs: {Struct} 23 | funcs: {Func} 24 | end 25 | 26 | local Module_mt: metatable = { __index = Module } 27 | 28 | function Module.new(init: Module): Module 29 | local mod: Module = { 30 | name = assert(init.name, "you must set 'name' field."), 31 | dependencies = Fun.map( 32 | assert(init.dependencies, "you must set 'dependencies' field."), 33 | function(d: Dependency): Dependency return Dependency.new(d) end 34 | ), 35 | enums = Fun.map( 36 | assert(init.enums, "you must set 'enums' field."), 37 | function(e: Enum): Enum return Enum.new(e) end 38 | ), 39 | structs = Fun.map( 40 | assert(init.structs, "you must set 'structs' field."), 41 | function(s: Struct): Struct return Struct.new(s) end 42 | ), 43 | funcs = Fun.map( 44 | assert(init.funcs, "you must set 'funcs' field."), 45 | function(f: Func): Func return Func.new(f) end 46 | ), 47 | } 48 | return setmetatable(mod, Module_mt) 49 | end 50 | 51 | function Module:set_op_overloading(op_table: {string:Op}) 52 | Fun.endo_map(self.funcs, function(func: Func): Func 53 | local op = op_table[func.sym.name] 54 | if op then 55 | func.meta.op_overloading = op 56 | end 57 | return func 58 | end) 59 | end 60 | 61 | return Module 62 | -------------------------------------------------------------------------------- /bindgen/ast/op.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local enum Op 9 | '+' -- add 10 | '-' -- sub 11 | '*' -- mul 12 | '/' -- div 13 | '==' -- eq 14 | '-u' -- neg 15 | end 16 | 17 | return Op 18 | -------------------------------------------------------------------------------- /bindgen/ast/struct.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local Fun = require 'bindgen.tools.fun' 9 | 10 | local Symbol = require 'bindgen.ast.symbol' 11 | local Type = require 'bindgen.ast.type' 12 | 13 | local record Struct 14 | record Field 15 | sym: Symbol 16 | type: Type 17 | end 18 | 19 | sym: Symbol 20 | fields: {Field} 21 | end 22 | 23 | local Struct_mt: metatable = { __index = Struct } 24 | local StructField_mt: metatable = { __index = Struct.Field } 25 | 26 | function Struct.Field.new(init: Struct.Field): Struct.Field 27 | local struct_field --[[]]: Struct.Field = { 28 | sym = Symbol.new(assert(init.sym, "you must set 'sym' field.")), 29 | type = Type.new(assert(init.type, "you must set 'type' field.")), 30 | } 31 | return setmetatable(struct_field, StructField_mt) 32 | end 33 | 34 | function Struct.new(init: Struct): Struct 35 | local struct --[[]]: Struct = { 36 | sym = Symbol.new(assert(init.sym, "you must set 'sym' field.")), 37 | fields = Fun.map( 38 | assert(init.fields, "you must set 'fields' field."), 39 | function(f: Struct.Field): Struct.Field return Struct.Field.new(f) end 40 | ), 41 | } 42 | return setmetatable(struct, Struct_mt) 43 | end 44 | 45 | return Struct 46 | -------------------------------------------------------------------------------- /bindgen/ast/symbol.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local Comment = require 'bindgen.ast.comment' 9 | local Utils = require 'bindgen.tools.utils' 10 | 11 | local record Symbol 12 | name: string 13 | cname: string 14 | comment: Comment 15 | end 16 | 17 | local Symbol_mt: metatable = { __index = Symbol } 18 | 19 | function Symbol.new(init: Symbol): Symbol 20 | local sym: Symbol = { 21 | name = assert(init.name, "you must set 'name' field."), 22 | cname = assert(init.cname, "you must set 'cname' field."), 23 | comment = Comment.new(assert(init.comment, "you must set 'comment' field.")), 24 | } 25 | return setmetatable(sym, Symbol_mt) 26 | end 27 | 28 | function Symbol:cname_without_prefixes(prefix_count: integer): string 29 | assert(prefix_count > 0, 'prefix_count must be greater than 0') 30 | return Utils.pipe { 31 | Utils.string_separate, { self.cname, '(%w+)_?' }, -- separate identifier by underscores 32 | Utils.slice, { prefix_count + 1 }, -- remove "prefix_count" prefixes from the sequence 33 | table.concat, { '_' } -- merge again as an identifier 34 | } as string 35 | end 36 | 37 | function Symbol:cname_with_only_prefixes(prefix_count: integer): string 38 | assert(prefix_count > 0, 'prefix_count must be greater than 0') 39 | return Utils.pipe { 40 | Utils.string_separate, { self.cname, '(%w+)_?' }, -- separate identifier by underscores 41 | Utils.slice, { 1, prefix_count }, -- keep only "prefix_count" prefixes from the sequence 42 | table.concat, { '_' } -- merge again as an identifier 43 | } as string 44 | end 45 | 46 | return Symbol 47 | -------------------------------------------------------------------------------- /bindgen/ast/type.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local Utils = require 'bindgen.tools.utils' 9 | 10 | local primitive_types: {string:boolean} = { 11 | ['int'] = true, 12 | ['float'] = true, 13 | ['double'] = true, 14 | ['int8_t'] = true, 15 | ['int16_t'] = true, 16 | ['int32_t'] = true, 17 | ['int64_t'] = true, 18 | ['uint8_t'] = true, 19 | ['uint16_t'] = true, 20 | ['uint32_t'] = true, 21 | ['uint64_t'] = true, 22 | ['bool'] = true, 23 | ['_Bool'] = true, 24 | ['cstring'] = true, 25 | ['void'] = true, 26 | } 27 | 28 | local function is_primitive_type(ctype: string): boolean 29 | return not not primitive_types[ctype] 30 | end 31 | 32 | local record Type 33 | type: string 34 | ctype: string 35 | end 36 | 37 | local Type_mt: metatable = { __index = Type } 38 | 39 | function Type.new(init: Type): Type 40 | local ty: Type = { 41 | type = assert(init.type, "you must set 'type' field."), 42 | ctype = assert(init.ctype, "you must set 'ctype' field."), 43 | } 44 | return setmetatable(ty, Type_mt) 45 | end 46 | 47 | function Type:is_primitive(): boolean 48 | return is_primitive_type(self.type) or false 49 | end 50 | 51 | function Type.from_ctype(ctype: string): Type 52 | local _ctype = ctype 53 | ctype = ctype:gsub('const char %*', 'cstring') 54 | 55 | local type_id, pointers = ctype:match('([_%w]+) ?(%**)') 56 | 57 | -- if it isn't a primitive type, then remove library prefix 58 | if not is_primitive_type(type_id) then 59 | -- FIXME: This is hacky, but for now this will help, probably be resolved when we stop using SDL's symbols on bindings (like in Color today) 60 | local _type_words = Utils.string_separate(type_id, '(%w+)_?') 61 | local _type_first = Utils.slice(_type_words, 1, 1) 62 | local _type_n = Utils.slice(_type_words, 2) 63 | local _type_id = table.concat(_type_n, '_') 64 | 65 | if _type_first[1] ~= 'SDL' and _type_first[1] ~= 'TTF' and _type_first[1] ~= 'IMG' and _type_first[1] ~= 'Mix' then 66 | type_id = _type_id 67 | end 68 | end 69 | 70 | ctype = (pointers or '') .. type_id 71 | 72 | return Type.new { 73 | type = type_id, 74 | ctype = _ctype, 75 | } 76 | end 77 | 78 | return Type 79 | -------------------------------------------------------------------------------- /bindgen/ast_common.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local Utils = require 'bindgen.tools.utils' 10 | local Fun = require 'bindgen.tools.fun' 11 | 12 | local AstCommon = {} 13 | 14 | function AstCommon.get_methods(struct: AST.Struct, funcs: {AST.Func}): {AST.Func} 15 | return Fun.filter(funcs, function(f: AST.Func): boolean 16 | return f:is_method_of_struct(struct) 17 | end) 18 | end 19 | 20 | function AstCommon.get_static_functions(structs: {AST.Struct}, funcs: {AST.Func}): {AST.Func} 21 | local result: {AST.Func} = {} 22 | 23 | for _, func in ipairs(funcs) do 24 | for _, struct in ipairs(structs) do 25 | if func:is_method_of_struct(struct) then 26 | goto continue 27 | end 28 | end 29 | 30 | table.insert(result, func) 31 | 32 | ::continue:: 33 | end 34 | 35 | return result 36 | end 37 | 38 | function AstCommon.get_maybe_func_result_type(func: AST.Func, maybe_wrap: AST.Func.Meta.MaybeWrap, structs: {AST.Struct}): (AST.Struct.Field, string) 39 | local ret_type = Utils.ifindk(structs, func.ret_type.ctype, function(s: AST.Struct): string 40 | return s.sym.cname 41 | end) as AST.Struct 42 | 43 | if not ret_type then 44 | return nil, "Couldn't find '"..func.ret_type.ctype.."' to wrap for maybe function" 45 | end 46 | 47 | local result_field = Utils.ifindk(ret_type.fields, maybe_wrap.result, function(f: AST.Struct.Field): string 48 | return f.sym.cname 49 | end) as AST.Struct.Field 50 | 51 | if not result_field then 52 | return nil, "Couldn't find '"..maybe_wrap.result.."' result field from the wrapped result structure" 53 | end 54 | 55 | return result_field 56 | end 57 | 58 | function AstCommon.set_maybe_functions(functions: {AST.Func}, maybe_functions: { {{AST.Struct}, string, AST.Func.Meta.MaybeWrap} }) 59 | assert(functions, "'functions' must be given") 60 | assert(maybe_functions, "'maybe_functions' must be given") 61 | 62 | for i, maybe_fn in ipairs(maybe_functions) do 63 | local structs = assert( 64 | maybe_fn[1], 65 | "On 'maybe_functions' #" .. tostring(i) .. " [1]: you must set the first tuple element an AST.Module" 66 | ) 67 | local fn_name = assert( 68 | maybe_fn[2], 69 | "On 'maybe_functions' #" .. tostring(i) .. " [2]: you must set the second tuple element as the function name" 70 | ) 71 | local maybe_wrap = assert( 72 | maybe_fn[3], 73 | "On 'maybe_functions' #" .. tostring(i) .. " [3]: you must set the third tuple element as wraping data" 74 | ) 75 | if maybe_wrap.result_type then 76 | print("warn: On 'maybe_functions' #" .. tostring(i) .. " [3]: Avoid setting 'result_type', because it'll be overwritten") 77 | end 78 | 79 | Utils.find_and_call( 80 | functions, 81 | fn_name, 82 | function(f: AST.Func): string 83 | return f.sym.cname 84 | end, 85 | function(fn: AST.Func): any 86 | local result_field, err = AstCommon.get_maybe_func_result_type(fn, maybe_wrap, structs) 87 | assert(result_field, err) 88 | 89 | maybe_wrap.result_type = result_field.type 90 | 91 | fn:set_as_maybe(maybe_wrap) 92 | end 93 | ) 94 | end 95 | end 96 | 97 | function AstCommon.set_op_overloading_functions(functions: {AST.Func}, op_functions: {string:AST.Op}) 98 | assert(functions, "'functions' must be given") 99 | assert(op_functions, "'op_functions' must be given") 100 | 101 | for fn_name, fn_op in pairs(op_functions) do 102 | Utils.find_and_call( 103 | functions, 104 | fn_name, 105 | function(f: AST.Func): string 106 | return f.sym.cname 107 | end, 108 | function(fn: AST.Func): any 109 | fn:set_as_op_overloading(fn_op) 110 | end 111 | ) 112 | end 113 | end 114 | 115 | function AstCommon.set_on_functions(functions: {AST.Func}, fn_names: {string}, callback: function(AST.Func): any) 116 | assert(functions, "'functions' must be given") 117 | assert(fn_names, "'fn_names' must be given") 118 | 119 | for _, fn_name in ipairs(fn_names) do 120 | Utils.find_and_call( 121 | functions, 122 | fn_name, 123 | function (fn: AST.Func): string return fn.sym.cname end, 124 | callback 125 | ) 126 | end 127 | end 128 | 129 | function AstCommon.autoset_function_kinds(functions: {AST.Func}) 130 | for _, func in ipairs(functions) do 131 | if func.sym.name == 'zero' then 132 | func:set_as_zero_init() 133 | elseif func.sym.name == 'copy' then 134 | func:set_as_copy() 135 | end 136 | end 137 | end 138 | 139 | return AstCommon 140 | -------------------------------------------------------------------------------- /bindgen/data/animation.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('animation', 'nene_Animation') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local enums = ast_reader.get_enums(symbols, 'nene') 16 | assert(enums, "no 'enums' returned") 17 | 18 | local structs = ast_reader.get_structs(symbols, 'nene') 19 | assert(structs, "no 'structs' returned") 20 | 21 | local funcs = ast_reader.get_functions(symbols, 'nene_Animation') 22 | assert(funcs, "no 'funcs' returned") 23 | 24 | AstCommon.autoset_function_kinds(funcs) 25 | 26 | AstCommon.set_op_overloading_functions(funcs, { 27 | ['nene_Animation_equals'] = '==', 28 | }) 29 | 30 | local animation = AST.Module.new { 31 | name = 'Animation', 32 | dependencies = {}, 33 | enums = enums, 34 | structs = structs, 35 | funcs = funcs, 36 | } 37 | assert(animation, "no 'animation' created") 38 | 39 | return animation 40 | -------------------------------------------------------------------------------- /bindgen/data/audio/music.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('audio/music', 'nene_Music') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Music') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_maybe_functions(funcs, { 24 | { 25 | structs, 26 | 'nene_Music_load', 27 | { ok = 'created', result = 'music' } 28 | }, 29 | }) 30 | 31 | local music = AST.Module.new { 32 | name = 'Music', 33 | dependencies = {}, 34 | enums = {}, 35 | structs = structs, 36 | funcs = funcs, 37 | } 38 | assert(music, "no 'music' created") 39 | 40 | return music 41 | -------------------------------------------------------------------------------- /bindgen/data/audio/sound.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('audio/sound', 'nene_Sound') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Sound') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_maybe_functions(funcs, { 24 | { 25 | structs, 26 | 'nene_Sound_load', 27 | { ok = 'created', result = 'sound' } 28 | }, 29 | }) 30 | 31 | local sound = AST.Module.new { 32 | name = 'Sound', 33 | dependencies = {}, 34 | enums = {}, 35 | structs = structs, 36 | funcs = funcs, 37 | } 38 | assert(sound, "no 'sound' created") 39 | 40 | return sound 41 | -------------------------------------------------------------------------------- /bindgen/data/collision.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('collision', 'nene_Collision') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Collision') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | local collision = AST.Module.new { 24 | name = 'Collision', 25 | dependencies = { 26 | { module = 'Vec2' }, 27 | { module = 'Rectf' }, 28 | { module = 'Segment' }, 29 | }, 30 | enums = {}, 31 | structs = structs, 32 | funcs = funcs, 33 | } 34 | assert(collision, "no 'collision' created") 35 | 36 | return collision 37 | -------------------------------------------------------------------------------- /bindgen/data/color.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('color', 'nene_Color') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Color') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.set_op_overloading_functions(funcs, { 22 | ['nene_Color_equals'] = '==', 23 | }) 24 | 25 | AstCommon.autoset_function_kinds(funcs) 26 | 27 | AstCommon.set_on_functions( 28 | funcs, 29 | { 30 | 'nene_Color_black', 31 | 'nene_Color_white', 32 | 'nene_Color_red', 33 | 'nene_Color_green', 34 | 'nene_Color_blue', 35 | 'nene_Color_yellow', 36 | 'nene_Color_cyan', 37 | 'nene_Color_bg', 38 | }, 39 | function(fn: AST.Func): any 40 | fn:set_as_readonly_property() 41 | end 42 | ) 43 | 44 | local color = AST.Module.new { 45 | name = 'Color', 46 | dependencies = {}, 47 | enums = {}, 48 | structs = structs, 49 | funcs = funcs, 50 | } 51 | assert(color, "no 'color' created") 52 | 53 | return color 54 | -------------------------------------------------------------------------------- /bindgen/data/core.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | 11 | local symbols = ast_reader.read_file('core', 'nene_GamepadState') 12 | assert(symbols, "no 'symbols' returned") 13 | 14 | local structs = ast_reader.get_structs(symbols, 'nene') 15 | assert(structs, "no 'structs' returned") 16 | 17 | local funcs = ast_reader.get_functions(symbols, 'nene_Core') 18 | assert(funcs, "no 'funcs' returned") 19 | 20 | local core = AST.Module.new { 21 | name = 'Core', 22 | dependencies = { 23 | { module = 'Vec2' }, 24 | { module = 'Vec2i' }, 25 | { module = 'Rect' }, 26 | { module = 'Color' }, 27 | }, 28 | enums = {}, 29 | structs = structs, 30 | funcs = funcs, 31 | } 32 | assert(core, "no 'core' created") 33 | 34 | return core 35 | -------------------------------------------------------------------------------- /bindgen/data/font.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | local Utils = require 'bindgen.tools.utils' 12 | 13 | -- Danger! Be careful about cyclic dependency 14 | local TextureMod = require 'bindgen.data.texture' 15 | 16 | local symbols = ast_reader.read_file('font', 'nene_TextQuality') 17 | assert(symbols, "no 'symbols' returned") 18 | 19 | local enums = ast_reader.get_enums(symbols, 'nene_TextQuality') 20 | assert(enums, "no 'enums' returned") 21 | 22 | local structs = ast_reader.get_structs(symbols, 'nene') 23 | assert(structs, "no 'structs' returned") 24 | 25 | local font_funcs = ast_reader.get_functions(symbols, 'nene_Font') 26 | assert(font_funcs, "no 'font_funcs' returned") 27 | 28 | local glyph_funcs = ast_reader.get_functions(symbols, 'nene_GlyphMetrics') 29 | assert(glyph_funcs, "no 'glyph_funcs' returned") 30 | 31 | local funcs = Utils.imerge(glyph_funcs, font_funcs) 32 | 33 | AstCommon.autoset_function_kinds(funcs) 34 | 35 | AstCommon.set_maybe_functions(funcs, { 36 | { 37 | structs, 38 | 'nene_Font_load', 39 | { ok = 'created', result = 'font' } 40 | }, 41 | { 42 | structs, 43 | 'nene_Font_get_glyph_metrics', 44 | { ok = 'queried', result = 'metrics' } 45 | }, 46 | { 47 | structs, 48 | 'nene_Font_get_text_dimensions', 49 | { ok = 'calculated', result = 'dimensions' } 50 | }, 51 | { 52 | TextureMod.structs, 53 | 'nene_Font_render', 54 | { ok = 'created', result = 'texture' } 55 | }, 56 | }) 57 | 58 | local font = AST.Module.new { 59 | name = 'Font', 60 | dependencies = { 61 | { module = 'Vec2i' }, 62 | { module = 'Texture' }, 63 | { module = 'Color' }, 64 | }, 65 | enums = enums, 66 | structs = structs, 67 | funcs = funcs, 68 | } 69 | assert(font, "no 'font' created") 70 | 71 | return font 72 | -------------------------------------------------------------------------------- /bindgen/data/intersections.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('intersections', 'nene_IntersectionRectfWithRectf') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Intersection') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | local intersections = AST.Module.new { 24 | name = 'Intersections', 25 | dependencies = { 26 | { module = 'Vec2' }, 27 | { module = 'Rectf' }, 28 | { module = 'Segment' }, 29 | }, 30 | enums = {}, 31 | structs = structs, 32 | funcs = funcs, 33 | } 34 | assert(intersections, "no 'intersections' created") 35 | 36 | return intersections 37 | -------------------------------------------------------------------------------- /bindgen/data/math/grid.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('math/grid', 'nene_Grid') 13 | assert(symbols, " no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, " no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Grid') 19 | assert(funcs, " no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_op_overloading_functions(funcs, { 24 | ['nene_Grid_equals'] = '==', 25 | }) 26 | 27 | AstCommon.set_on_functions( 28 | funcs, 29 | { 'nene_Grid_zero' }, 30 | function(fn: AST.Func): any 31 | fn:set_as_readonly_property() 32 | end 33 | ) 34 | 35 | local grid = AST.Module.new { 36 | name = 'Grid', 37 | dependencies = { 38 | { module = 'Vec2i' }, 39 | { module = 'Rect' }, 40 | }, 41 | enums = {}, 42 | structs = structs, 43 | funcs = funcs, 44 | } 45 | assert(grid, "no 'grid' created") 46 | 47 | return grid 48 | -------------------------------------------------------------------------------- /bindgen/data/math/rect.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('math/rect', 'nene_Rect') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Rect') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_op_overloading_functions(funcs, { 24 | ['nene_Rect_equals'] = '==', 25 | }) 26 | 27 | AstCommon.set_on_functions( 28 | funcs, 29 | { 'nene_Rect_zero' }, 30 | function(fn: AST.Func): any 31 | fn:set_as_readonly_property() 32 | end 33 | ) 34 | 35 | local rect = AST.Module.new { 36 | name = 'Rect', 37 | dependencies = { 38 | { module = 'Vec2i' }, 39 | }, 40 | enums = {}, 41 | structs = structs, 42 | funcs = funcs, 43 | } 44 | assert(rect, "no 'rect' created") 45 | 46 | return rect 47 | -------------------------------------------------------------------------------- /bindgen/data/math/rectf.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('math/rectf', 'nene_Rectf') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Rectf') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_op_overloading_functions(funcs, { 24 | ['nene_Rectf_equals'] = '==', 25 | }) 26 | 27 | AstCommon.set_on_functions( 28 | funcs, 29 | { 'nene_Rectf_zero' }, 30 | function(fn: AST.Func): any 31 | fn:set_as_readonly_property() 32 | end 33 | ) 34 | 35 | local rectf = AST.Module.new { 36 | name = 'Rectf', 37 | dependencies = { 38 | { module = 'Vec2' }, 39 | { module = 'Rect' }, 40 | }, 41 | enums = {}, 42 | structs = structs, 43 | funcs = funcs, 44 | } 45 | assert(rectf, "no 'rectf' created") 46 | 47 | return rectf 48 | -------------------------------------------------------------------------------- /bindgen/data/math/segment.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('math/segment', 'nene_Segment') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Segment') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_op_overloading_functions(funcs, { 24 | ['nene_Segment_equals'] = '==', 25 | }) 26 | 27 | AstCommon.set_on_functions( 28 | funcs, 29 | { 'nene_Segment_zero' }, 30 | function(fn: AST.Func): any 31 | fn:set_as_readonly_property() 32 | end 33 | ) 34 | 35 | local segment = AST.Module.new { 36 | name = 'Segment', 37 | dependencies = { 38 | { module = 'Vec2' }, 39 | }, 40 | enums = {}, 41 | structs = structs, 42 | funcs = funcs, 43 | } 44 | assert(segment, "no 'segment' created") 45 | 46 | return segment 47 | -------------------------------------------------------------------------------- /bindgen/data/math/shape.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('math/shape', 'nene_Shape', 'nene_ShapeQuadrilateral') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Shape') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_on_functions( 24 | funcs, 25 | { 'nene_ShapeQuadrilateral_zero' }, 26 | function(fn: AST.Func): any 27 | fn:set_as_readonly_property() 28 | end 29 | ) 30 | 31 | local shape = AST.Module.new { 32 | name = 'Shape', 33 | dependencies = { 34 | { module = 'Segment' }, 35 | { module = 'Rect' }, 36 | { module = 'Rectf' }, 37 | }, 38 | enums = {}, 39 | structs = structs, 40 | funcs = funcs, 41 | } 42 | assert(shape, "no 'shape' created") 43 | 44 | return shape 45 | -------------------------------------------------------------------------------- /bindgen/data/math/vec2.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('math/vec2', 'nene_Vec2') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Vec2') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_op_overloading_functions(funcs, { 24 | ['nene_Vec2_equals'] = '==', 25 | ['nene_Vec2_add'] = '+', 26 | ['nene_Vec2_sub'] = '-', 27 | ['nene_Vec2_negate'] = '-u', 28 | }) 29 | 30 | AstCommon.set_on_functions( 31 | funcs, 32 | { 'nene_Vec2_mul', 'nene_Vec2_scale' }, 33 | function(fn: AST.Func): any 34 | fn.meta.is_private = true 35 | end 36 | ) 37 | 38 | AstCommon.set_on_functions( 39 | funcs, 40 | { 'nene_Vec2_zero', 'nene_Vec2_one' }, 41 | function(fn: AST.Func): any 42 | fn:set_as_readonly_property() 43 | end 44 | ) 45 | 46 | local vec2 = AST.Module.new { 47 | name = 'Vec2', 48 | dependencies = { 49 | { module = 'Vec2i' }, 50 | }, 51 | enums = {}, 52 | structs = structs, 53 | funcs = funcs, 54 | } 55 | assert(vec2, "no 'vec2' created") 56 | 57 | return vec2 58 | -------------------------------------------------------------------------------- /bindgen/data/math/vec2i.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('math/vec2i', 'nene_Vec2i') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Vec2i') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_op_overloading_functions(funcs, { 24 | ['nene_Vec2i_equals'] = '==', 25 | ['nene_Vec2i_add'] = '+', 26 | ['nene_Vec2i_sub'] = '-', 27 | ['nene_Vec2i_negate'] = '-u', 28 | }) 29 | 30 | AstCommon.set_on_functions( 31 | funcs, 32 | { 'nene_Vec2i_mul', 'nene_Vec2i_scale' }, 33 | function(fn: AST.Func): any 34 | fn.meta.is_private = true 35 | end 36 | ) 37 | 38 | AstCommon.set_on_functions( 39 | funcs, 40 | { 'nene_Vec2i_zero', 'nene_Vec2i_one' }, 41 | function(fn: AST.Func): any 42 | fn:set_as_readonly_property() 43 | end 44 | ) 45 | 46 | local vec2i = AST.Module.new { 47 | name = 'Vec2i', 48 | dependencies = {}, 49 | enums = {}, 50 | structs = structs, 51 | funcs = funcs, 52 | } 53 | assert(vec2i, "no 'vec2i' created") 54 | 55 | return vec2i 56 | -------------------------------------------------------------------------------- /bindgen/data/texture.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('texture', 'nene_Texture') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_Texture') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_maybe_functions(funcs, { 24 | { 25 | structs, 26 | 'nene_Texture_create_with_access', 27 | { ok = 'created', result = 'texture' } 28 | }, 29 | { 30 | structs, 31 | 'nene_Texture_create', 32 | { ok = 'created', result = 'texture' } 33 | }, 34 | { 35 | structs, 36 | 'nene_Texture_load', 37 | { ok = 'created', result = 'texture' } 38 | }, 39 | }) 40 | 41 | local texture = AST.Module.new { 42 | name = 'Texture', 43 | dependencies = { 44 | { module = 'Vec2' }, 45 | { module = 'Rect' }, 46 | { module = 'Color' }, 47 | }, 48 | enums = {}, 49 | structs = structs, 50 | funcs = funcs, 51 | } 52 | assert(texture, "no 'texture' created") 53 | 54 | return texture 55 | -------------------------------------------------------------------------------- /bindgen/data/texture_atlas.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | 12 | local symbols = ast_reader.read_file('texture_atlas', 'nene_TextureAtlas') 13 | assert(symbols, "no 'symbols' returned") 14 | 15 | local structs = ast_reader.get_structs(symbols, 'nene') 16 | assert(structs, "no 'structs' returned") 17 | 18 | local funcs = ast_reader.get_functions(symbols, 'nene_TextureAtlas') 19 | assert(funcs, "no 'funcs' returned") 20 | 21 | AstCommon.autoset_function_kinds(funcs) 22 | 23 | AstCommon.set_maybe_functions(funcs, { 24 | { 25 | structs, 26 | 'nene_TextureAtlas_load', 27 | { ok = 'created', result = 'texture_atlas' } 28 | }, 29 | }) 30 | 31 | local texture_atlas = AST.Module.new { 32 | name = 'TextureAtlas', 33 | dependencies = { 34 | { module = 'Grid' }, 35 | { module = 'Vec2i' }, 36 | { module = 'Vec2' }, 37 | { module = 'Texture' }, 38 | }, 39 | enums = {}, 40 | structs = structs, 41 | funcs = funcs, 42 | } 43 | assert(texture_atlas, "no 'texture_atlas' created") 44 | 45 | return texture_atlas 46 | -------------------------------------------------------------------------------- /bindgen/data/tilemap.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local ast_reader = require 'bindgen.ast_reader' 10 | local AstCommon = require 'bindgen.ast_common' 11 | local Utils = require 'bindgen.tools.utils' 12 | 13 | local symbols = ast_reader.read_file('tilemap', 'nene_Tilemap') 14 | assert(symbols, "no 'symbols' returned") 15 | 16 | local structs = ast_reader.get_structs(symbols, 'nene') 17 | assert(structs, "no 'structs' returned") 18 | 19 | local funcs = ast_reader.get_functions(symbols, 'nene_Tilemap') 20 | assert(funcs, "no 'funcs' returned") 21 | 22 | AstCommon.autoset_function_kinds(funcs) 23 | 24 | AstCommon.set_on_functions(funcs, {'nene_Tilemap_draw'}, function(fn: AST.Func): any 25 | -- ast-dumps don't keep the information that some pointer it's actually an array 26 | -- thus, we need to undo this lost information. 27 | local tiles = Utils.ifindk( 28 | fn.params, 'tiles', 29 | function(param: AST.Func.Param): string 30 | return param.sym.name 31 | end 32 | ) as AST.Func.Param 33 | assert(tiles, "no 'tiles' returned") 34 | 35 | tiles.type.ctype = tiles.type.ctype:gsub('uint16_t %*', 'uint16_t []') 36 | end) 37 | 38 | local tilemap = AST.Module.new { 39 | name = 'Tilemap', 40 | dependencies = { 41 | { module = 'Grid' }, 42 | { module = 'Vec2i' }, 43 | { module = 'Vec2' }, 44 | { module = 'TextureAtlas' }, 45 | }, 46 | enums = {}, 47 | structs = structs, 48 | funcs = funcs, 49 | } 50 | assert(tilemap, "no 'tilemap' created") 51 | 52 | return tilemap 53 | -------------------------------------------------------------------------------- /bindgen/dependencies.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local record Dependencies 9 | record Dependency 10 | {string} 11 | is_header: boolean 12 | end 13 | dependencies: {string:Dependency} 14 | header: string 15 | end 16 | 17 | return Dependencies 18 | -------------------------------------------------------------------------------- /bindgen/generator.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local AST = require 'bindgen.ast' 9 | local Utils = require 'bindgen.tools.utils' 10 | 11 | local record Generator 12 | type TypeMap = {string:string} 13 | type DepMap = {string:string} 14 | 15 | -- TODO: remove ignore_overloading and privatize 16 | record WrapOptions 17 | begin_prepend: string 18 | prepend: string 19 | append: string 20 | ignore_overloading: {AST.Op:boolean} 21 | privatize: {string:boolean} 22 | end 23 | 24 | typemap: TypeMap 25 | depmap: DepMap 26 | end 27 | 28 | local Generator_mt: metatable = { __index = Generator } 29 | 30 | function Generator.new(typemap: Generator.TypeMap, depmap: Generator.DepMap): Generator 31 | local generator: Generator = { 32 | typemap = typemap, 33 | depmap = depmap, 34 | } 35 | 36 | return setmetatable(generator, Generator_mt) 37 | end 38 | 39 | function Generator.WrapOptions.new(init: Generator.WrapOptions): Generator.WrapOptions 40 | return { 41 | begin_prepend = init.begin_prepend or '', 42 | prepend = init.prepend or '', 43 | append = init.append or '', 44 | ignore_overloading = init.ignore_overloading or {}, 45 | privatize = init.privatize or {}, 46 | } 47 | end 48 | 49 | -- returns the module's main strucutre if applicable, nil otherwise. 50 | function Generator.there_is_module_struct(module: AST.Module): AST.Struct 51 | return Utils.ifindk( 52 | module.structs, 53 | module.name, 54 | function(s: AST.Struct): string 55 | return s.sym.name 56 | end 57 | ) as AST.Struct 58 | end 59 | 60 | -- generate comment lines 61 | function Generator:gen_comments(comment: AST.Comment, prefix: string, postfix: string, indent: integer): string 62 | assert(prefix, "you must pass 'prefix'.") 63 | assert(postfix, "you must pass 'postfix'.") 64 | 65 | local result: {string} = {} 66 | 67 | for _, line in comment:iter_lines() do 68 | local indent_str = string.rep(' ', indent) 69 | table.insert(result, string.format("%s%s%s%s", indent_str, prefix, line, postfix)) 70 | end 71 | 72 | return table.concat(result, '\n') 73 | end 74 | 75 | function Generator:apply_typemap(_type: AST.Type): string 76 | local text = _type.ctype 77 | text = text:gsub('const ', '') 78 | 79 | if text == 'char *' then 80 | return self.typemap['cstring'] 81 | end 82 | 83 | local ctype, ptr_or_arr = text:match('([%w_]+) ?([%[%]%*%d]*)') 84 | 85 | if _type:is_primitive() then 86 | for T, langT in pairs(self.typemap) do 87 | if ctype == T then 88 | ctype = langT 89 | break 90 | end 91 | end 92 | else 93 | ctype = _type.type 94 | end 95 | 96 | ptr_or_arr = ptr_or_arr:gsub('%[%]', '*[0]') 97 | 98 | return ptr_or_arr .. ctype 99 | end 100 | 101 | return Generator 102 | -------------------------------------------------------------------------------- /bindgen/tools/fun.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local Fun = {} 9 | 10 | --- Filters the content of the array xs through f function. 11 | --- 12 | --- It returns a new table with only the elements of xs that 13 | --- passes the filter f. 14 | --- 15 | --- Note: The returned table it's a shallow copy. 16 | function Fun.filter(xs: {T}, f: function(T): boolean): {T} 17 | assert(xs, "you must pass the 'xs' argument.") 18 | assert(f, "you must pass the 'f' argument.") 19 | 20 | local result: {T} = {} 21 | local n = 1 22 | 23 | for i=1, #xs do 24 | local x = xs[i] 25 | if f(x) then 26 | result[n] = x 27 | n = n + 1 28 | end 29 | end 30 | 31 | return result 32 | end 33 | 34 | --- Maps xs over the function f. 35 | --- 36 | --- It returns a new array table whose elements are the results 37 | --- of f over the elements of xs. 38 | function Fun.map(xs: {T}, f: function(T): U): {U} 39 | assert(xs, "you must pass the 'xs' argument.") 40 | assert(f, "you must pass the 'f' argument.") 41 | 42 | local result: {U} = {} 43 | 44 | for i=1, #xs do 45 | result[i] = f(xs[i]) 46 | end 47 | 48 | return result 49 | end 50 | 51 | -- endo functions: 52 | -- "endo" functions are mutating functions that changes the content in-place 53 | 54 | --- Similar to Fun.map, but it applies f in-place, without 55 | --- creating a new table. 56 | function Fun.endo_map(xs: {T}, f: function(T): T) 57 | assert(xs, "you must pass the 'xs' argument.") 58 | assert(f, "you must pass the 'f' argument.") 59 | 60 | for i=1, #xs do 61 | xs[i] = f(xs[i]) 62 | end 63 | end 64 | 65 | return Fun 66 | -------------------------------------------------------------------------------- /bindgen/tools/utils.tl: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local Utils = {} 9 | 10 | function Utils.kpairs_to_alphabetic_ipairs(tbl: {K:V}): ({K}, {V}) 11 | local keys: {K} = {} 12 | local values: {V} = {} 13 | for k, _ in pairs(tbl) do 14 | table.insert(keys, k) 15 | end 16 | table.sort(keys) 17 | for i, k in ipairs(keys) do 18 | values[i] = tbl[k] 19 | end 20 | return keys, values 21 | end 22 | 23 | function Utils.imerge(...: {T}): {T} 24 | local result: {T} = {} 25 | for i = 1, select('#', ...) do 26 | local t = select(i, ...) 27 | for j = 1, #t do 28 | table.insert(result, t[j]) 29 | end 30 | end 31 | return result 32 | end 33 | 34 | function Utils.no_repeat(values: {any}, predicate: function(any): any): {any} 35 | local result: {any} = {} 36 | local tbl: {any:boolean} = {} 37 | 38 | for _, value in ipairs(values) do 39 | local r: any = predicate(value) 40 | if not tbl[r] then 41 | table.insert(result, value) 42 | tbl[r] = true 43 | end 44 | end 45 | 46 | return result 47 | end 48 | 49 | function Utils.ifindk(tbl: {any}, value: any, searcher: function(v: any): any): any 50 | for _, v in ipairs(tbl) do 51 | if searcher(v) == value then 52 | return v 53 | end 54 | end 55 | return nil 56 | end 57 | 58 | function Utils.find_and_call(tbl: {T}, value: any, searcher: function(v: T): (any), callback: function(v: T): any): any 59 | local result = Utils.ifindk(tbl, value, searcher) as T 60 | 61 | if result ~= nil then 62 | return callback(result) 63 | end 64 | end 65 | 66 | function Utils.string_separate(str: string, separator: string): {string} 67 | local words: {string} = {} 68 | for word in string.gmatch(str, separator) do 69 | table.insert(words, word) 70 | end 71 | return words 72 | end 73 | 74 | function Utils.slice(arr: {T}, first: integer, last: integer): {T} 75 | assert(arr, "you must pass 'arr' argument") 76 | assert(first ~= 0, "first must be different than 0") 77 | 78 | if not last then 79 | last = #arr 80 | end 81 | 82 | assert(last ~= 0, "last must be different than 0") 83 | 84 | if first < 0 then 85 | first = #arr - first 86 | end 87 | 88 | if last < 0 then 89 | last = #arr - last 90 | end 91 | 92 | return table.move(arr, first, last, 1, {}) 93 | end 94 | 95 | function Utils.from_all_caps_to_pascal_case(all_caps: string): string 96 | local words = Utils.string_separate(all_caps, '(%w+)_?') 97 | local result: {string} = {} 98 | 99 | for _, word in ipairs(words) do 100 | table.insert(result, word:sub(1, 1) .. word:sub(2, -1):lower()) 101 | end 102 | 103 | return table.concat(result) 104 | end 105 | 106 | function Utils.from_snake_case_to_kebab_case(snake_case: string): string 107 | return (snake_case:gsub('_', '-')) 108 | end 109 | 110 | function Utils.pipe(pipeline: {any}): any 111 | assert(pipeline, "'pipeline' argument expected.") 112 | assert(#pipeline % 2 == 0, "on 'pipeline' argument: expected even number of arguments.") 113 | 114 | local result: any = nil 115 | 116 | for i = 1, #pipeline, 2 do 117 | local i_str = tostring(i) 118 | local fn = pipeline[i] as function 119 | local args = pipeline[i+1] as {any} 120 | 121 | assert( 122 | fn and type(fn) == 'function', 123 | "on pipeline #"..i_str..": function expected, got " .. type(fn) 124 | ) 125 | 126 | if i == 1 then 127 | result = table.remove(args, 1) 128 | end 129 | 130 | result = fn(result, table.unpack(args)) 131 | 132 | assert(type(result) ~= nil, "on function #"..i_str..": got nil result") 133 | end 134 | 135 | return result 136 | end 137 | 138 | return Utils 139 | -------------------------------------------------------------------------------- /bindings/carp/nene/animation.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | (doc Animation "Animation range type, used on Spritesheets") 10 | (register-type Animation "nene_Animation" [interval Uint16, from Uint16, to Uint16, loop Bool]) 11 | 12 | (defmodule Animation 13 | (doc zero "Returns a zero-initialized animation.") 14 | (register zero (Fn [] Animation) "nene_Animation_zero") 15 | (implements zero Animation.zero) 16 | (private -private-copy) (hidden -private-copy) 17 | (register -private-copy (Fn [(Ptr Animation)] Animation) "nene_Animation_copy") 18 | (doc copy "Returns a copy of the animation.") 19 | (sig copy (Fn [(Ref Animation)] Animation)) 20 | (defn copy [x] 21 | (Animation.-private-copy (the (Ptr Animation) (Unsafe.coerce x)))) 22 | (implements copy Animation.copy) 23 | (sig -private-blit (Fn [Animation] Animation)) 24 | (defn- -private-blit [x] (the Animation x)) 25 | (implements blit Animation.-private-blit) 26 | (doc equals "Tests if a and b animation ranges are equal.") 27 | (private -private-equals) (hidden -private-equals) 28 | (register -private-equals (Fn [Animation, Animation] Bool) "nene_Animation_equals") 29 | (implements = Animation.-private-equals) 30 | ) 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /bindings/carp/nene/audio/music.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | (doc Music "") 10 | (register-type Music "nene_Music" [raw (Ptr Mix_Music)]) 11 | (doc MusicCreation "") 12 | (register-type MusicCreation "nene_MusicCreation" [created Bool, music Music]) 13 | 14 | (defmodule Music 15 | (doc zero "") 16 | (register zero (Fn [] Music) "nene_Music_zero") 17 | (implements zero Music.zero) 18 | (private -private-copy) (hidden -private-copy) 19 | (register -private-copy (Fn [(Ptr Music)] Music) "nene_Music_copy") 20 | (doc copy "") 21 | (sig copy (Fn [(Ref Music)] Music)) 22 | (defn copy [x] 23 | (Music.-private-copy (the (Ptr Music) (Unsafe.coerce x)))) 24 | (implements copy Music.copy) 25 | (sig -private-blit (Fn [Music] Music)) 26 | (defn- -private-blit [x] (the Music x)) 27 | (implements blit Music.-private-blit) 28 | (register get-raw (Fn [Music] (Ptr Mix_Music)) "nene_Music_get_raw") 29 | (private -private-load) (hidden -private-load) 30 | (register -private-load (Fn [(Ptr CChar)] MusicCreation) "nene_Music_load") 31 | (sig load (Fn [(Ptr CChar)] (Maybe Music))) 32 | (doc load "") 33 | (defn load [filepath] 34 | (let [maybe-result (Music.-private-load filepath)] 35 | (if @(MusicCreation.created &maybe-result) 36 | (Maybe.Just @(Music.music &maybe-result)) 37 | (Nothing)))) 38 | (register play (Fn [Music, Int16] Bool) "nene_Music_play") 39 | (register stop (Fn [] ()) "nene_Music_stop") 40 | (register set-volume (Fn [Float] Float) "nene_Music_set_volume") 41 | (register destroy (Fn [(Ptr Music)] ()) "nene_Music_destroy") 42 | ) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /bindings/carp/nene/audio/sound.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | (doc Sound "") 10 | (register-type Sound "nene_Sound" [raw (Ptr Mix_Chunk), channel Int16]) 11 | (doc SoundCreation "") 12 | (register-type SoundCreation "nene_SoundCreation" [created Bool, sound Sound]) 13 | 14 | (defmodule Sound 15 | (doc zero "") 16 | (register zero (Fn [] Sound) "nene_Sound_zero") 17 | (implements zero Sound.zero) 18 | (private -private-copy) (hidden -private-copy) 19 | (register -private-copy (Fn [(Ptr Sound)] Sound) "nene_Sound_copy") 20 | (doc copy "") 21 | (sig copy (Fn [(Ref Sound)] Sound)) 22 | (defn copy [x] 23 | (Sound.-private-copy (the (Ptr Sound) (Unsafe.coerce x)))) 24 | (implements copy Sound.copy) 25 | (sig -private-blit (Fn [Sound] Sound)) 26 | (defn- -private-blit [x] (the Sound x)) 27 | (implements blit Sound.-private-blit) 28 | (register get-raw (Fn [Sound] (Ptr Mix_Chunk)) "nene_Sound_get_raw") 29 | (private -private-load) (hidden -private-load) 30 | (register -private-load (Fn [(Ptr CChar)] SoundCreation) "nene_Sound_load") 31 | (sig load (Fn [(Ptr CChar)] (Maybe Sound))) 32 | (doc load "") 33 | (defn load [filepath] 34 | (let [maybe-result (Sound.-private-load filepath)] 35 | (if @(SoundCreation.created &maybe-result) 36 | (Maybe.Just @(Sound.sound &maybe-result)) 37 | (Nothing)))) 38 | (register play (Fn [(Ptr Sound), Int16] Bool) "nene_Sound_play") 39 | (register halt (Fn [(Ptr Sound)] Bool) "nene_Sound_halt") 40 | (register halt-all (Fn [] Bool) "nene_Sound_halt_all") 41 | (register set-volume (Fn [Sound, Float] Float) "nene_Sound_set_volume") 42 | (register destroy (Fn [(Ptr Sound)] ()) "nene_Sound_destroy") 43 | ) 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /bindings/carp/nene/collision.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2.carp") 5 | (load "nene/math/rectf.carp") 6 | (load "nene/math/segment.carp") 7 | 8 | 9 | 10 | 11 | 12 | (doc Collision "Collision data structure.") 13 | (register-type Collision "nene_Collision" [collided Bool, delta Vec2]) 14 | 15 | (defmodule Collision 16 | (doc zero "Returns a zero-initialialized collision") 17 | (register zero (Fn [] Collision) "nene_Collision_zero") 18 | (implements zero Collision.zero) 19 | (private -private-copy) (hidden -private-copy) 20 | (register -private-copy (Fn [(Ptr Collision)] Collision) "nene_Collision_copy") 21 | (doc copy "Returns a copy of the collision.") 22 | (sig copy (Fn [(Ref Collision)] Collision)) 23 | (defn copy [x] 24 | (Collision.-private-copy (the (Ptr Collision) (Unsafe.coerce x)))) 25 | (implements copy Collision.copy) 26 | (sig -private-blit (Fn [Collision] Collision)) 27 | (defn- -private-blit [x] (the Collision x)) 28 | (implements blit Collision.-private-blit) 29 | (register no-collision (Fn [] Collision) "nene_Collision_no_collision") 30 | (register rectf-with-rectf (Fn [Rectf, Rectf, Vec2] Collision) "nene_Collision_rectf_with_rectf") 31 | (register rectf-with-segment (Fn [Rectf, Segment, Vec2] Collision) "nene_Collision_rectf_with_segment") 32 | ) 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /bindings/carp/nene/color.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | (doc Color "The color structure, it's a rgba color strucuture using one byte each") 10 | (register-type Color "nene_Color" [r Uint8, g Uint8, b Uint8, a Uint8]) 11 | 12 | (defmodule Color 13 | (doc zero "Returns a zero-initialized color, that's a transparent black.") 14 | (register zero (Fn [] Color) "nene_Color_zero") 15 | (implements zero Color.zero) 16 | (private -private-copy) (hidden -private-copy) 17 | (register -private-copy (Fn [(Ptr Color)] Color) "nene_Color_copy") 18 | (doc copy "Returns a copy of the color.") 19 | (sig copy (Fn [(Ref Color)] Color)) 20 | (defn copy [x] 21 | (Color.-private-copy (the (Ptr Color) (Unsafe.coerce x)))) 22 | (implements copy Color.copy) 23 | (sig -private-blit (Fn [Color] Color)) 24 | (defn- -private-blit [x] (the Color x)) 25 | (implements blit Color.-private-blit) 26 | (private -private-black) (hidden -private-black) 27 | (register -private-black (Fn [] Color) "nene_Color_black") 28 | (doc black "The \"black\" color of nene's palette (`{ .r = 0x00, .g = 0x00, .b = 0x00, .a = 0xff }`)") 29 | (def black (Color.-private-black)) 30 | (private -private-white) (hidden -private-white) 31 | (register -private-white (Fn [] Color) "nene_Color_white") 32 | (doc white "The \"white\" color of nene's palette (`{ .r = 0xff, .g = 0xff, .b = 0xff, .a = 0xff }`)") 33 | (def white (Color.-private-white)) 34 | (private -private-red) (hidden -private-red) 35 | (register -private-red (Fn [] Color) "nene_Color_red") 36 | (doc red "The \"red\" color of nene's palette (`{ .r = 0xff, .g = 0x00, .b = 0x00, .a = 0xff }`)") 37 | (def red (Color.-private-red)) 38 | (private -private-green) (hidden -private-green) 39 | (register -private-green (Fn [] Color) "nene_Color_green") 40 | (doc green "The \"green\" color of nene's palette (`{ .r = 0x00, .g = 0xff, .b = 0x00, .a = 0xff }`)") 41 | (def green (Color.-private-green)) 42 | (private -private-blue) (hidden -private-blue) 43 | (register -private-blue (Fn [] Color) "nene_Color_blue") 44 | (doc blue "The \"blue\" color of nene's palette (`{ .r = 0x00, .g = 0x00, .b = 0xff, .a = 0xff }`)") 45 | (def blue (Color.-private-blue)) 46 | (private -private-yellow) (hidden -private-yellow) 47 | (register -private-yellow (Fn [] Color) "nene_Color_yellow") 48 | (doc yellow "The \"yellow\" color of nene's palette (`{ .r = 0xfc, .g = 0xea, .b = 0x20, .a = 0xff }`)") 49 | (def yellow (Color.-private-yellow)) 50 | (private -private-cyan) (hidden -private-cyan) 51 | (register -private-cyan (Fn [] Color) "nene_Color_cyan") 52 | (doc cyan "The \"cyan\" color of nene's palette (`{ .r = 0x00, .g = 0xff, .b = 0xff, .a = 0xff }`)") 53 | (def cyan (Color.-private-cyan)) 54 | (private -private-bg) (hidden -private-bg) 55 | (register -private-bg (Fn [] Color) "nene_Color_bg") 56 | (doc bg "The \"bg\" color of nene's palette (`{ .r = 0x69, .g = 0x46, .b = 0x6b, .a = 0xff }`)") 57 | (def bg (Color.-private-bg)) 58 | (doc equals "") 59 | (private -private-equals) (hidden -private-equals) 60 | (register -private-equals (Fn [Color, Color] Bool) "nene_Color_equals") 61 | (implements = Color.-private-equals) 62 | ) 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /bindings/carp/nene/intersections.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2.carp") 5 | (load "nene/math/rectf.carp") 6 | (load "nene/math/segment.carp") 7 | 8 | 9 | 10 | 11 | 12 | (doc IntersectionRectfWithRectf "Rectangle with rectangle intersection.") 13 | (register-type IntersectionRectfWithRectf "nene_IntersectionRectfWithRectf" [intersected Bool, intersection Rectf]) 14 | (doc IntersectionSegmentWithRectf "Segment with rectangle intersection.") 15 | (register-type IntersectionSegmentWithRectf "nene_IntersectionSegmentWithRectf") 16 | (doc IntersectionSegmentWithSegment "Segment with segment intersection.") 17 | (register-type IntersectionSegmentWithSegment "nene_IntersectionSegmentWithSegment" [intersected Bool, is_parallel Bool, point Vec2, a_intersecting_scalar Float, b_intersecting_scalar Float]) 18 | 19 | (defmodule IntersectionRectfWithRectf 20 | (doc zero "Returns a zero-initialized RectfWithRectf value.") 21 | (register zero (Fn [] IntersectionRectfWithRectf) "nene_IntersectionRectfWithRectf_zero") 22 | (implements zero IntersectionRectfWithRectf.zero) 23 | (private -private-copy) (hidden -private-copy) 24 | (register -private-copy (Fn [(Ptr IntersectionRectfWithRectf)] IntersectionRectfWithRectf) "nene_IntersectionRectfWithRectf_copy") 25 | (doc copy "Returns a copy of a RectfWithRectf intersection.") 26 | (sig copy (Fn [(Ref IntersectionRectfWithRectf)] IntersectionRectfWithRectf)) 27 | (defn copy [x] 28 | (IntersectionRectfWithRectf.-private-copy (the (Ptr IntersectionRectfWithRectf) (Unsafe.coerce x)))) 29 | (implements copy IntersectionRectfWithRectf.copy) 30 | (sig -private-blit (Fn [IntersectionRectfWithRectf] IntersectionRectfWithRectf)) 31 | (defn- -private-blit [x] (the IntersectionRectfWithRectf x)) 32 | (implements blit IntersectionRectfWithRectf.-private-blit) 33 | (register no-intersection (Fn [] IntersectionRectfWithRectf) "nene_IntersectionRectfWithRectf_no_intersection") 34 | (register get-intersection (Fn [Rectf, Rectf] IntersectionRectfWithRectf) "nene_IntersectionRectfWithRectf_get_intersection") 35 | ) 36 | (defmodule IntersectionSegmentWithRectf 37 | (doc zero "Returns a zero-initialized RectfWithRectf value.") 38 | (register zero (Fn [] IntersectionSegmentWithRectf) "nene_IntersectionSegmentWithRectf_zero") 39 | (implements zero IntersectionSegmentWithRectf.zero) 40 | (private -private-copy) (hidden -private-copy) 41 | (register -private-copy (Fn [(Ptr IntersectionSegmentWithRectf)] IntersectionSegmentWithRectf) "nene_IntersectionSegmentWithRectf_copy") 42 | (doc copy "Returns a copy of a RectfWithRectf intersection.") 43 | (sig copy (Fn [(Ref IntersectionSegmentWithRectf)] IntersectionSegmentWithRectf)) 44 | (defn copy [x] 45 | (IntersectionSegmentWithRectf.-private-copy (the (Ptr IntersectionSegmentWithRectf) (Unsafe.coerce x)))) 46 | (implements copy IntersectionSegmentWithRectf.copy) 47 | (sig -private-blit (Fn [IntersectionSegmentWithRectf] IntersectionSegmentWithRectf)) 48 | (defn- -private-blit [x] (the IntersectionSegmentWithRectf x)) 49 | (implements blit IntersectionSegmentWithRectf.-private-blit) 50 | (register no-intersection (Fn [] IntersectionSegmentWithRectf) "nene_IntersectionSegmentWithRectf_no_intersection") 51 | (register get-intersection (Fn [Segment, Rectf] IntersectionSegmentWithRectf) "nene_IntersectionSegmentWithRectf_get_intersection") 52 | ) 53 | (defmodule IntersectionSegmentWithSegment 54 | (doc zero "Returns a zero-initialized IntersectionSegmentWithSegment value.") 55 | (register zero (Fn [] IntersectionSegmentWithSegment) "nene_IntersectionSegmentWithSegment_zero") 56 | (implements zero IntersectionSegmentWithSegment.zero) 57 | (private -private-copy) (hidden -private-copy) 58 | (register -private-copy (Fn [(Ptr IntersectionSegmentWithSegment)] IntersectionSegmentWithSegment) "nene_IntersectionSegmentWithSegment_copy") 59 | (doc copy "Returns a copy of a IntersectionSegmentWithSegment intersection.") 60 | (sig copy (Fn [(Ref IntersectionSegmentWithSegment)] IntersectionSegmentWithSegment)) 61 | (defn copy [x] 62 | (IntersectionSegmentWithSegment.-private-copy (the (Ptr IntersectionSegmentWithSegment) (Unsafe.coerce x)))) 63 | (implements copy IntersectionSegmentWithSegment.copy) 64 | (sig -private-blit (Fn [IntersectionSegmentWithSegment] IntersectionSegmentWithSegment)) 65 | (defn- -private-blit [x] (the IntersectionSegmentWithSegment x)) 66 | (implements blit IntersectionSegmentWithSegment.-private-blit) 67 | (register no-intersection (Fn [] IntersectionSegmentWithSegment) "nene_IntersectionSegmentWithSegment_no_intersection") 68 | (register get-intersection (Fn [Segment, Segment] IntersectionSegmentWithSegment) "nene_IntersectionSegmentWithSegment_get_intersection") 69 | ) 70 | (defmodule Intersections 71 | (register is-intersecting-rectf-with-rectf (Fn [Rectf, Rectf] Bool) "nene_Intersections_is_intersecting_rectf_with_rectf") 72 | (register is-intersecting-rectf-with-point (Fn [Rectf, Vec2] Bool) "nene_Intersections_is_intersecting_rectf_with_point") 73 | ) 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /bindings/carp/nene/math/grid.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2i.carp") 5 | (load "nene/math/rect.carp") 6 | 7 | 8 | 9 | 10 | 11 | (doc Grid "") 12 | (register-type Grid "nene_Grid" [cell_size Vec2i, gap Vec2i]) 13 | 14 | (defmodule Grid 15 | (doc zero "") 16 | (register zero (Fn [] Grid) "nene_Grid_zero") 17 | (implements zero Grid.zero) 18 | (private -private-copy) (hidden -private-copy) 19 | (register -private-copy (Fn [(Ptr Grid)] Grid) "nene_Grid_copy") 20 | (doc copy "") 21 | (sig copy (Fn [(Ref Grid)] Grid)) 22 | (defn copy [x] 23 | (Grid.-private-copy (the (Ptr Grid) (Unsafe.coerce x)))) 24 | (implements copy Grid.copy) 25 | (sig -private-blit (Fn [Grid] Grid)) 26 | (defn- -private-blit [x] (the Grid x)) 27 | (implements blit Grid.-private-blit) 28 | (doc equals "") 29 | (private -private-equals) (hidden -private-equals) 30 | (register -private-equals (Fn [Grid, Grid] Bool) "nene_Grid_equals") 31 | (implements = Grid.-private-equals) 32 | (register get-nth-cell-coord (Fn [Uint32, Uint16] Vec2i) "nene_Grid_get_nth_cell_coord") 33 | (register get-cell-position (Fn [Grid, Vec2i] Vec2i) "nene_Grid_get_cell_position") 34 | (register get-nth-cell-position (Fn [Grid, Uint32, Uint16] Vec2i) "nene_Grid_get_nth_cell_position") 35 | (register get-rect (Fn [Grid, Vec2i] Rect) "nene_Grid_get_rect") 36 | ) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /bindings/carp/nene/math/rect.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2i.carp") 5 | 6 | 7 | 8 | 9 | 10 | (doc Rect "") 11 | (register-type Rect "nene_Rect" [pos Vec2i, size Vec2i]) 12 | 13 | (defmodule Rect 14 | (doc zero "") 15 | (register zero (Fn [] Rect) "nene_Rect_zero") 16 | (implements zero Rect.zero) 17 | (private -private-copy) (hidden -private-copy) 18 | (register -private-copy (Fn [(Ptr Rect)] Rect) "nene_Rect_copy") 19 | (doc copy "") 20 | (sig copy (Fn [(Ref Rect)] Rect)) 21 | (defn copy [x] 22 | (Rect.-private-copy (the (Ptr Rect) (Unsafe.coerce x)))) 23 | (implements copy Rect.copy) 24 | (sig -private-blit (Fn [Rect] Rect)) 25 | (defn- -private-blit [x] (the Rect x)) 26 | (implements blit Rect.-private-blit) 27 | (register to-raw (Fn [Rect] SDL_Rect) "nene_Rect_to_raw") 28 | (doc equals "") 29 | (private -private-equals) (hidden -private-equals) 30 | (register -private-equals (Fn [Rect, Rect] Bool) "nene_Rect_equals") 31 | (implements = Rect.-private-equals) 32 | (register add-pos (Fn [Rect, Vec2i] Rect) "nene_Rect_add_pos") 33 | (register add-size (Fn [Rect, Vec2i] Rect) "nene_Rect_add_size") 34 | (register get-center (Fn [Rect] Vec2i) "nene_Rect_get_center") 35 | ) 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /bindings/carp/nene/math/rectf.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2.carp") 5 | (load "nene/math/rect.carp") 6 | 7 | 8 | 9 | 10 | 11 | (doc Rectf "") 12 | (register-type Rectf "nene_Rectf" [pos Vec2, size Vec2]) 13 | 14 | (defmodule Rectf 15 | (doc zero "") 16 | (register zero (Fn [] Rectf) "nene_Rectf_zero") 17 | (implements zero Rectf.zero) 18 | (private -private-copy) (hidden -private-copy) 19 | (register -private-copy (Fn [(Ptr Rectf)] Rectf) "nene_Rectf_copy") 20 | (doc copy "") 21 | (sig copy (Fn [(Ref Rectf)] Rectf)) 22 | (defn copy [x] 23 | (Rectf.-private-copy (the (Ptr Rectf) (Unsafe.coerce x)))) 24 | (implements copy Rectf.copy) 25 | (sig -private-blit (Fn [Rectf] Rectf)) 26 | (defn- -private-blit [x] (the Rectf x)) 27 | (implements blit Rectf.-private-blit) 28 | (register to-raw (Fn [Rectf] SDL_FRect) "nene_Rectf_to_raw") 29 | (doc equals "") 30 | (private -private-equals) (hidden -private-equals) 31 | (register -private-equals (Fn [Rectf, Rectf] Bool) "nene_Rectf_equals") 32 | (implements = Rectf.-private-equals) 33 | (register add-pos (Fn [Rectf, Vec2] Rectf) "nene_Rectf_add_pos") 34 | (register add-size (Fn [Rectf, Vec2] Rectf) "nene_Rectf_add_size") 35 | (register get-center (Fn [Rectf] Vec2) "nene_Rectf_get_center") 36 | (register to-rect (Fn [Rectf] Rect) "nene_Rectf_to_rect") 37 | (register from-rect (Fn [Rect] Rectf) "nene_Rectf_from_rect") 38 | ) 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /bindings/carp/nene/math/segment.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2.carp") 5 | 6 | 7 | 8 | 9 | 10 | (doc Segment "") 11 | (register-type Segment "nene_Segment" [origin Vec2, ending Vec2]) 12 | 13 | (defmodule Segment 14 | (doc zero "returns a segment with zeroed values") 15 | (register zero (Fn [] Segment) "nene_Segment_zero") 16 | (implements zero Segment.zero) 17 | (private -private-copy) (hidden -private-copy) 18 | (register -private-copy (Fn [(Ptr Segment)] Segment) "nene_Segment_copy") 19 | (doc copy "returns a segment copy from a reference") 20 | (sig copy (Fn [(Ref Segment)] Segment)) 21 | (defn copy [x] 22 | (Segment.-private-copy (the (Ptr Segment) (Unsafe.coerce x)))) 23 | (implements copy Segment.copy) 24 | (sig -private-blit (Fn [Segment] Segment)) 25 | (defn- -private-blit [x] (the Segment x)) 26 | (implements blit Segment.-private-blit) 27 | (doc equals "tests the equality between two segments") 28 | (private -private-equals) (hidden -private-equals) 29 | (register -private-equals (Fn [Segment, Segment] Bool) "nene_Segment_equals") 30 | (implements = Segment.-private-equals) 31 | (register as-vec2 (Fn [Segment] Vec2) "nene_Segment_as_vec2") 32 | (register get-midpoint (Fn [Segment] Vec2) "nene_Segment_get_midpoint") 33 | (register len-sqr (Fn [Segment] Float) "nene_Segment_len_sqr") 34 | (register len (Fn [Segment] Float) "nene_Segment_len") 35 | ) 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /bindings/carp/nene/math/shape.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/segment.carp") 5 | (load "nene/math/rect.carp") 6 | (load "nene/math/rectf.carp") 7 | 8 | 9 | 10 | 11 | 12 | (doc ShapeQuadrilateral "Quadrilateral shape data strucuture, it holds four segments.") 13 | (register-type ShapeQuadrilateral "nene_ShapeQuadrilateral") 14 | 15 | (defmodule ShapeQuadrilateral 16 | (doc zero "Returns a zeroed quatrilateral shape") 17 | (register zero (Fn [] ShapeQuadrilateral) "nene_ShapeQuadrilateral_zero") 18 | (implements zero ShapeQuadrilateral.zero) 19 | (private -private-copy) (hidden -private-copy) 20 | (register -private-copy (Fn [(Ptr ShapeQuadrilateral)] ShapeQuadrilateral) "nene_ShapeQuadrilateral_copy") 21 | (doc copy "Returns a copy of the quadrilateral shape reference") 22 | (sig copy (Fn [(Ref ShapeQuadrilateral)] ShapeQuadrilateral)) 23 | (defn copy [x] 24 | (ShapeQuadrilateral.-private-copy (the (Ptr ShapeQuadrilateral) (Unsafe.coerce x)))) 25 | (implements copy ShapeQuadrilateral.copy) 26 | (sig -private-blit (Fn [ShapeQuadrilateral] ShapeQuadrilateral)) 27 | (defn- -private-blit [x] (the ShapeQuadrilateral x)) 28 | (implements blit ShapeQuadrilateral.-private-blit) 29 | ) 30 | (defmodule Shape 31 | (register get-rectf-shape (Fn [Rectf] ShapeQuadrilateral) "nene_Shape_get_rectf_shape") 32 | (register get-rect-shape (Fn [Rect] ShapeQuadrilateral) "nene_Shape_get_rect_shape") 33 | (register get-rectf-diagonal (Fn [Rectf, Bool] Segment) "nene_Shape_get_rectf_diagonal") 34 | ) 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /bindings/carp/nene/math/vec2.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2i.carp") 5 | 6 | 7 | 8 | 9 | 10 | (doc Vec2 "2D mathematical vector with `x` and `y` components.") 11 | (register-type Vec2 "nene_Vec2" [x Float, y Float]) 12 | 13 | (defmodule Vec2 14 | (register from-vec2i (Fn [Vec2i] Vec2) "nene_Vec2_from_vec2i") 15 | (register to-vec2i (Fn [Vec2] Vec2i) "nene_Vec2_to_vec2i") 16 | (doc zero "returns a vector with `0` value on `x` and `y` components.") 17 | (register zero (Fn [] Vec2) "nene_Vec2_zero") 18 | (implements zero Vec2.zero) 19 | (private -private-one) (hidden -private-one) 20 | (register -private-one (Fn [] Vec2) "nene_Vec2_one") 21 | (doc one "returns a vector with `1` value on `x` and `y` components.") 22 | (def one (Vec2.-private-one)) 23 | (private -private-copy) (hidden -private-copy) 24 | (register -private-copy (Fn [(Ptr Vec2)] Vec2) "nene_Vec2_copy") 25 | (doc copy "returns a copy of the vector reference.") 26 | (sig copy (Fn [(Ref Vec2)] Vec2)) 27 | (defn copy [x] 28 | (Vec2.-private-copy (the (Ptr Vec2) (Unsafe.coerce x)))) 29 | (implements copy Vec2.copy) 30 | (sig -private-blit (Fn [Vec2] Vec2)) 31 | (defn- -private-blit [x] (the Vec2 x)) 32 | (implements blit Vec2.-private-blit) 33 | (doc equals "tests equality between `a` and `b` vectors.") 34 | (private -private-equals) (hidden -private-equals) 35 | (register -private-equals (Fn [Vec2, Vec2] Bool) "nene_Vec2_equals") 36 | (implements = Vec2.-private-equals) 37 | (doc add "returns the sum of two vectors.") 38 | (private -private-add) (hidden -private-add) 39 | (register -private-add (Fn [Vec2, Vec2] Vec2) "nene_Vec2_add") 40 | (implements + Vec2.-private-add) 41 | (doc sub "returns the difference between two vectors.") 42 | (private -private-sub) (hidden -private-sub) 43 | (register -private-sub (Fn [Vec2, Vec2] Vec2) "nene_Vec2_sub") 44 | (implements - Vec2.-private-sub) 45 | (private -private-mul) (hidden -private-mul) 46 | (register -private-mul (Fn [Vec2, Vec2] Vec2) "nene_Vec2_mul") 47 | (implements * Vec2.-private-mul) 48 | (register scale (Fn [Vec2, Float] Vec2) "nene_Vec2_scale") 49 | (doc negate "returns the negation of the `v` vector.") 50 | (private -private-negate) (hidden -private-negate) 51 | (register -private-negate (Fn [Vec2] Vec2) "nene_Vec2_negate") 52 | (implements neg Vec2.-private-negate) 53 | (register len-sqr (Fn [Vec2] Float) "nene_Vec2_len_sqr") 54 | (register len (Fn [Vec2] Float) "nene_Vec2_len") 55 | (register lerp (Fn [Vec2, Vec2, Float] Vec2) "nene_Vec2_lerp") 56 | (register normalize (Fn [Vec2] Vec2) "nene_Vec2_normalize") 57 | (register dot (Fn [Vec2, Vec2] Float) "nene_Vec2_dot") 58 | (register cross (Fn [Vec2, Vec2] Float) "nene_Vec2_cross") 59 | (register perpendicular (Fn [Vec2] Vec2) "nene_Vec2_perpendicular") 60 | ) 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /bindings/carp/nene/math/vec2i.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | (doc Vec2i "2D mathematical vector with `x` and `y` components (using 32-bit integer type, a.k.a. `int32_t`).") 10 | (register-type Vec2i "nene_Vec2i" [x Int32, y Int32]) 11 | 12 | (defmodule Vec2i 13 | (doc zero "Returns a vector with `0` value on `x` and `y` components.") 14 | (register zero (Fn [] Vec2i) "nene_Vec2i_zero") 15 | (implements zero Vec2i.zero) 16 | (private -private-one) (hidden -private-one) 17 | (register -private-one (Fn [] Vec2i) "nene_Vec2i_one") 18 | (doc one "Returns a vector with `1` value on `x` and `y` components.") 19 | (def one (Vec2i.-private-one)) 20 | (private -private-copy) (hidden -private-copy) 21 | (register -private-copy (Fn [(Ptr Vec2i)] Vec2i) "nene_Vec2i_copy") 22 | (doc copy "Returns a copy of the vector reference.") 23 | (sig copy (Fn [(Ref Vec2i)] Vec2i)) 24 | (defn copy [x] 25 | (Vec2i.-private-copy (the (Ptr Vec2i) (Unsafe.coerce x)))) 26 | (implements copy Vec2i.copy) 27 | (sig -private-blit (Fn [Vec2i] Vec2i)) 28 | (defn- -private-blit [x] (the Vec2i x)) 29 | (implements blit Vec2i.-private-blit) 30 | (doc equals "Tests equality between `a` and `b` vectors.") 31 | (private -private-equals) (hidden -private-equals) 32 | (register -private-equals (Fn [Vec2i, Vec2i] Bool) "nene_Vec2i_equals") 33 | (implements = Vec2i.-private-equals) 34 | (doc add "Returns the sum of two vectors.") 35 | (private -private-add) (hidden -private-add) 36 | (register -private-add (Fn [Vec2i, Vec2i] Vec2i) "nene_Vec2i_add") 37 | (implements + Vec2i.-private-add) 38 | (doc sub "Returns the difference between two vectors.") 39 | (private -private-sub) (hidden -private-sub) 40 | (register -private-sub (Fn [Vec2i, Vec2i] Vec2i) "nene_Vec2i_sub") 41 | (implements - Vec2i.-private-sub) 42 | (private -private-mul) (hidden -private-mul) 43 | (register -private-mul (Fn [Vec2i, Vec2i] Vec2i) "nene_Vec2i_mul") 44 | (implements * Vec2i.-private-mul) 45 | (register scale (Fn [Vec2i, Float] Vec2i) "nene_Vec2i_scale") 46 | (doc negate "Returns the negation of the `v` vector.") 47 | (private -private-negate) (hidden -private-negate) 48 | (register -private-negate (Fn [Vec2i] Vec2i) "nene_Vec2i_negate") 49 | (implements neg Vec2i.-private-negate) 50 | (register len-sqr (Fn [Vec2i] Float) "nene_Vec2i_len_sqr") 51 | (register len (Fn [Vec2i] Float) "nene_Vec2i_len") 52 | (register lerp (Fn [Vec2i, Vec2i, Float] Vec2i) "nene_Vec2i_lerp") 53 | (register dot (Fn [Vec2i, Vec2i] Float) "nene_Vec2i_dot") 54 | (register cross (Fn [Vec2i, Vec2i] Float) "nene_Vec2i_cross") 55 | (register perpendicular (Fn [Vec2i] Vec2i) "nene_Vec2i_perpendicular") 56 | ) 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /bindings/carp/nene/sdl/sdl2_image.carp: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2021-present André Luiz Alvares 2 | ;; Nene is licensed under the Zlib license. 3 | ;; Please refer to the LICENSE file for details 4 | ;; SPDX-License-Identifier: Zlib 5 | 6 | -------------------------------------------------------------------------------- /bindings/carp/nene/sdl/sdl2_mixer.carp: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2021-present André Luiz Alvares 2 | ;; Nene is licensed under the Zlib license. 3 | ;; Please refer to the LICENSE file for details 4 | ;; SPDX-License-Identifier: Zlib 5 | 6 | (register-type Mix_Music) 7 | (register-type Mix_Chunk) 8 | -------------------------------------------------------------------------------- /bindings/carp/nene/sdl/sdl2_ttf.carp: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2021-present André Luiz Alvares 2 | ;; Nene is licensed under the Zlib license. 3 | ;; Please refer to the LICENSE file for details 4 | ;; SPDX-License-Identifier: Zlib 5 | 6 | (register-type TTF_Font) -------------------------------------------------------------------------------- /bindings/carp/nene/texture.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/vec2.carp") 5 | (load "nene/math/rect.carp") 6 | (load "nene/color.carp") 7 | 8 | 9 | 10 | 11 | 12 | (doc Texture "") 13 | (register-type Texture "nene_Texture" [raw (Ptr SDL_Texture), width Uint16, height Uint16, access SDL_TextureAccess]) 14 | (doc TextureCreation "") 15 | (register-type TextureCreation "nene_TextureCreation" [created Bool, texture Texture]) 16 | 17 | (defmodule Texture 18 | (doc zero "") 19 | (register zero (Fn [] Texture) "nene_Texture_zero") 20 | (implements zero Texture.zero) 21 | (private -private-copy) (hidden -private-copy) 22 | (register -private-copy (Fn [(Ptr Texture)] Texture) "nene_Texture_copy") 23 | (doc copy "") 24 | (sig copy (Fn [(Ref Texture)] Texture)) 25 | (defn copy [x] 26 | (Texture.-private-copy (the (Ptr Texture) (Unsafe.coerce x)))) 27 | (implements copy Texture.copy) 28 | (sig -private-blit (Fn [Texture] Texture)) 29 | (defn- -private-blit [x] (the Texture x)) 30 | (implements blit Texture.-private-blit) 31 | (register destroy (Fn [(Ptr Texture)] ()) "nene_Texture_destroy") 32 | (register get-raw (Fn [Texture] (Ptr SDL_Texture)) "nene_Texture_get_raw") 33 | (register apply-raw (Fn [(Ptr Texture), (Ptr SDL_Texture)] Bool) "nene_Texture_apply_raw") 34 | (register set-blend-mode (Fn [Texture, SDL_BlendMode] Bool) "nene_Texture_set_blend_mode") 35 | (register set-color-mod (Fn [Texture, Color] Bool) "nene_Texture_set_color_mod") 36 | (private -private-create-with-access) (hidden -private-create-with-access) 37 | (register -private-create-with-access (Fn [Uint16, Uint16, SDL_TextureAccess] TextureCreation) "nene_Texture_create_with_access") 38 | (sig create-with-access (Fn [Uint16 Uint16 SDL_TextureAccess] (Maybe Texture))) 39 | (doc create_with_access "") 40 | (defn create-with-access [width, height, access] 41 | (let [maybe-result (Texture.-private-create-with-access width height access)] 42 | (if @(TextureCreation.created &maybe-result) 43 | (Maybe.Just @(Texture.texture &maybe-result)) 44 | (Nothing)))) 45 | (private -private-create) (hidden -private-create) 46 | (register -private-create (Fn [Uint16, Uint16] TextureCreation) "nene_Texture_create") 47 | (sig create (Fn [Uint16 Uint16] (Maybe Texture))) 48 | (doc create "") 49 | (defn create [width, height] 50 | (let [maybe-result (Texture.-private-create width height)] 51 | (if @(TextureCreation.created &maybe-result) 52 | (Maybe.Just @(Texture.texture &maybe-result)) 53 | (Nothing)))) 54 | (private -private-load) (hidden -private-load) 55 | (register -private-load (Fn [(Ptr CChar)] TextureCreation) "nene_Texture_load") 56 | (sig load (Fn [(Ptr CChar)] (Maybe Texture))) 57 | (doc load "") 58 | (defn load [filepath] 59 | (let [maybe-result (Texture.-private-load filepath)] 60 | (if @(TextureCreation.created &maybe-result) 61 | (Maybe.Just @(Texture.texture &maybe-result)) 62 | (Nothing)))) 63 | (register get-full-source-rect (Fn [Texture] Rect) "nene_Texture_get_full_source_rect") 64 | (register draw-to-point (Fn [Texture, Rect, Vec2, Bool] Bool) "nene_Texture_draw_to_point") 65 | (register draw-to-rect (Fn [Texture, Rect, Rect] Bool) "nene_Texture_draw_to_rect") 66 | (register draw-to-point-ex (Fn [Texture, Rect, Vec2, Bool, Double, Vec2, Bool, Bool] Bool) "nene_Texture_draw_to_point_ex") 67 | (register draw-to-rect-ex (Fn [Texture, Rect, Rect, Double, Vec2, Bool, Bool] Bool) "nene_Texture_draw_to_rect_ex") 68 | ) 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /bindings/carp/nene/texture_atlas.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/grid.carp") 5 | (load "nene/math/vec2i.carp") 6 | (load "nene/math/vec2.carp") 7 | (load "nene/texture.carp") 8 | 9 | 10 | 11 | 12 | 13 | (doc TextureAtlas "") 14 | (register-type TextureAtlas "nene_TextureAtlas" [width Uint16, grid Grid, texture Texture]) 15 | (doc TextureAtlasCreation "") 16 | (register-type TextureAtlasCreation "nene_TextureAtlasCreation" [created Bool, texture_atlas TextureAtlas]) 17 | 18 | (defmodule TextureAtlas 19 | (doc zero "") 20 | (register zero (Fn [] TextureAtlas) "nene_TextureAtlas_zero") 21 | (implements zero TextureAtlas.zero) 22 | (private -private-copy) (hidden -private-copy) 23 | (register -private-copy (Fn [(Ptr TextureAtlas)] TextureAtlas) "nene_TextureAtlas_copy") 24 | (doc copy "") 25 | (sig copy (Fn [(Ref TextureAtlas)] TextureAtlas)) 26 | (defn copy [x] 27 | (TextureAtlas.-private-copy (the (Ptr TextureAtlas) (Unsafe.coerce x)))) 28 | (implements copy TextureAtlas.copy) 29 | (sig -private-blit (Fn [TextureAtlas] TextureAtlas)) 30 | (defn- -private-blit [x] (the TextureAtlas x)) 31 | (implements blit TextureAtlas.-private-blit) 32 | (register destroy (Fn [(Ptr TextureAtlas)] ()) "nene_TextureAtlas_destroy") 33 | (private -private-load) (hidden -private-load) 34 | (register -private-load (Fn [(Ptr CChar), Uint16, Grid] TextureAtlasCreation) "nene_TextureAtlas_load") 35 | (sig load (Fn [(Ptr CChar) Uint16 Grid] (Maybe TextureAtlas))) 36 | (doc load "") 37 | (defn load [filepath, width, grid] 38 | (let [maybe-result (TextureAtlas.-private-load filepath width grid)] 39 | (if @(TextureAtlasCreation.created &maybe-result) 40 | (Maybe.Just @(TextureAtlas.texture_atlas &maybe-result)) 41 | (Nothing)))) 42 | (register draw-sub-texture (Fn [TextureAtlas, Vec2i, Vec2, Bool] Bool) "nene_TextureAtlas_draw_sub_texture") 43 | (register draw-sub-texture-ex (Fn [TextureAtlas, Vec2i, Vec2, Bool, Double, Vec2, Bool, Bool] Bool) "nene_TextureAtlas_draw_sub_texture_ex") 44 | (register draw-nth-sub-texture (Fn [TextureAtlas, Uint32, Vec2, Bool] Bool) "nene_TextureAtlas_draw_nth_sub_texture") 45 | (register draw-nth-sub-texture-ex (Fn [TextureAtlas, Uint32, Vec2, Bool, Double, Vec2, Bool, Bool] Bool) "nene_TextureAtlas_draw_nth_sub_texture_ex") 46 | (register get-sub-texture-center (Fn [TextureAtlas] Vec2) "nene_TextureAtlas_get_sub_texture_center") 47 | ) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /bindings/carp/nene/tilemap.carp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | (load "nene/math/grid.carp") 5 | (load "nene/math/vec2i.carp") 6 | (load "nene/math/vec2.carp") 7 | (load "nene/texture_atlas.carp") 8 | 9 | 10 | 11 | 12 | 13 | (doc Tilemap "") 14 | (register-type Tilemap "nene_Tilemap" [tileset TextureAtlas, width Uint16, grid Grid, tile_count Uint32]) 15 | 16 | (defmodule Tilemap 17 | (doc zero "") 18 | (register zero (Fn [] Tilemap) "nene_Tilemap_zero") 19 | (implements zero Tilemap.zero) 20 | (private -private-copy) (hidden -private-copy) 21 | (register -private-copy (Fn [(Ptr Tilemap)] Tilemap) "nene_Tilemap_copy") 22 | (doc copy "") 23 | (sig copy (Fn [(Ref Tilemap)] Tilemap)) 24 | (defn copy [x] 25 | (Tilemap.-private-copy (the (Ptr Tilemap) (Unsafe.coerce x)))) 26 | (implements copy Tilemap.copy) 27 | (sig -private-blit (Fn [Tilemap] Tilemap)) 28 | (defn- -private-blit [x] (the Tilemap x)) 29 | (implements blit Tilemap.-private-blit) 30 | (register get-size-in-tiles (Fn [Tilemap] Vec2i) "nene_Tilemap_get_size_in_tiles") 31 | (register get-size (Fn [Tilemap] Vec2i) "nene_Tilemap_get_size") 32 | (register draw (Fn [Tilemap, Vec2, Bool, (Ptr [0]Uint16), Uint32] Bool) "nene_Tilemap_draw") 33 | (register destroy (Fn [(Ptr Tilemap)] ()) "nene_Tilemap_destroy") 34 | ) 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /bindings/nelua/nene/animation.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | --- Animation range type, used on Spritesheets 9 | local Animation = @record{ 10 | --- animation speed 11 | interval: uint16, 12 | --- starting frame of the animation (inclusive) 13 | from: uint16, 14 | --- ending frame of the animation (inclusive) 15 | to: uint16, 16 | --- animation properties 17 | loop: boolean, 18 | } 19 | 20 | --- Returns a zero-initialized animation. 21 | function Animation.zero(): Animation end 22 | 23 | 24 | --- Returns a copy of the animation. 25 | function Animation.copy(animation: *Animation): Animation end 26 | 27 | 28 | --- Tests if a and b animation ranges are equal. 29 | function Animation.__eq(a: Animation, b: Animation): boolean end 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | return Animation 38 | -------------------------------------------------------------------------------- /bindings/nelua/nene/audio/music.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | local Music = @record{ 10 | 11 | raw: *Mix_Music, 12 | } 13 | 14 | 15 | local MusicCreation = @record{ 16 | 17 | created: boolean, 18 | 19 | music: Music, 20 | } 21 | 22 | 23 | function Music.zero(): Music end 24 | 25 | 26 | 27 | function Music.copy(music: *Music): Music end 28 | 29 | 30 | 31 | function Music.get_raw(music: Music): *Mix_Music end 32 | 33 | 34 | local function nene_Music_load(filepath: cstring): MusicCreation end 35 | 36 | 37 | function Music.load(filepath: cstring): (boolean, Music) 38 | local maybe_result = nene_Music_load(filepath) 39 | return maybe_result.created, maybe_result.music 40 | end 41 | 42 | 43 | 44 | function Music.play(music: Music, loops: int16): boolean end 45 | 46 | 47 | 48 | function Music.stop(): void end 49 | 50 | 51 | 52 | function Music.set_volume(volume: float32): float32 end 53 | 54 | 55 | 56 | function Music.destroy(music: *Music): void end 57 | 58 | 59 | 60 | 61 | 62 | 63 | local Music.MusicCreation: type = MusicCreation 64 | 65 | return Music 66 | -------------------------------------------------------------------------------- /bindings/nelua/nene/audio/sound.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | local Sound = @record{ 10 | 11 | raw: *Mix_Chunk, 12 | 13 | channel: int16, 14 | } 15 | 16 | 17 | local SoundCreation = @record{ 18 | 19 | created: boolean, 20 | 21 | sound: Sound, 22 | } 23 | 24 | 25 | function Sound.zero(): Sound end 26 | 27 | 28 | 29 | function Sound.copy(sound: *Sound): Sound end 30 | 31 | 32 | 33 | function Sound.get_raw(sound: Sound): *Mix_Chunk end 34 | 35 | 36 | local function nene_Sound_load(filepath: cstring): SoundCreation end 37 | 38 | 39 | function Sound.load(filepath: cstring): (boolean, Sound) 40 | local maybe_result = nene_Sound_load(filepath) 41 | return maybe_result.created, maybe_result.sound 42 | end 43 | 44 | 45 | 46 | function Sound.play(sound: *Sound, loops: int16): boolean end 47 | 48 | 49 | 50 | function Sound.halt(sound: *Sound): boolean end 51 | 52 | 53 | 54 | function Sound.halt_all(): boolean end 55 | 56 | 57 | 58 | function Sound.set_volume(sound: Sound, volume: float32): float32 end 59 | 60 | 61 | 62 | function Sound.destroy(sound: *Sound): void end 63 | 64 | 65 | 66 | 67 | 68 | 69 | local Sound.SoundCreation: type = SoundCreation 70 | 71 | return Sound 72 | -------------------------------------------------------------------------------- /bindings/nelua/nene/collision.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Vec2 = require 'nene.math.vec2' 5 | local Rectf = require 'nene.math.rectf' 6 | local Segment = require 'nene.math.segment' 7 | 8 | 9 | 10 | 11 | --- Collision data structure. 12 | local Collision = @record{ 13 | 14 | collided: boolean, 15 | 16 | delta: Vec2, 17 | } 18 | 19 | --- Returns a zero-initialialized collision 20 | function Collision.zero(): Collision end 21 | 22 | 23 | --- Returns a copy of the collision. 24 | function Collision.copy(collision: *Collision): Collision end 25 | 26 | 27 | --- Returns a "no collision" value, it's an alias to the "zero" function. 28 | function Collision.no_collision(): Collision end 29 | 30 | 31 | --- Returns the collision response between two rects. 32 | function Collision.rectf_with_rectf(a: Rectf, b: Rectf, delta_pos: Vec2): Collision end 33 | 34 | 35 | --- Returns the collision response between a rectangle and a segment. 36 | function Collision.rectf_with_segment(rect: Rectf, segment: Segment, delta_pos: Vec2): Collision end 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | return Collision 45 | -------------------------------------------------------------------------------- /bindings/nelua/nene/color.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | --- The color structure, it's a rgba color strucuture using one byte each 9 | local Color = @record{ 10 | 11 | r: uint8, 12 | 13 | g: uint8, 14 | 15 | b: uint8, 16 | 17 | a: uint8, 18 | } 19 | 20 | --- Returns a zero-initialized color, that's a transparent black. 21 | function Color.zero(): Color end 22 | 23 | 24 | --- Returns a copy of the color. 25 | function Color.copy(color: *Color): Color end 26 | 27 | 28 | local function nene_Color_black(): Color end 29 | 30 | --- The "black" color of nene's palette (`{ .r = 0x00, .g = 0x00, .b = 0x00, .a = 0xff }`) 31 | local Color.black: Color = nene_Color_black() 32 | 33 | local function nene_Color_white(): Color end 34 | 35 | --- The "white" color of nene's palette (`{ .r = 0xff, .g = 0xff, .b = 0xff, .a = 0xff }`) 36 | local Color.white: Color = nene_Color_white() 37 | 38 | local function nene_Color_red(): Color end 39 | 40 | --- The "red" color of nene's palette (`{ .r = 0xff, .g = 0x00, .b = 0x00, .a = 0xff }`) 41 | local Color.red: Color = nene_Color_red() 42 | 43 | local function nene_Color_green(): Color end 44 | 45 | --- The "green" color of nene's palette (`{ .r = 0x00, .g = 0xff, .b = 0x00, .a = 0xff }`) 46 | local Color.green: Color = nene_Color_green() 47 | 48 | local function nene_Color_blue(): Color end 49 | 50 | --- The "blue" color of nene's palette (`{ .r = 0x00, .g = 0x00, .b = 0xff, .a = 0xff }`) 51 | local Color.blue: Color = nene_Color_blue() 52 | 53 | local function nene_Color_yellow(): Color end 54 | 55 | --- The "yellow" color of nene's palette (`{ .r = 0xfc, .g = 0xea, .b = 0x20, .a = 0xff }`) 56 | local Color.yellow: Color = nene_Color_yellow() 57 | 58 | local function nene_Color_cyan(): Color end 59 | 60 | --- The "cyan" color of nene's palette (`{ .r = 0x00, .g = 0xff, .b = 0xff, .a = 0xff }`) 61 | local Color.cyan: Color = nene_Color_cyan() 62 | 63 | local function nene_Color_bg(): Color end 64 | 65 | --- The "bg" color of nene's palette (`{ .r = 0x69, .g = 0x46, .b = 0x6b, .a = 0xff }`) 66 | local Color.bg: Color = nene_Color_bg() 67 | 68 | 69 | function Color.__eq(a: Color, b: Color): boolean end 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | return Color 78 | -------------------------------------------------------------------------------- /bindings/nelua/nene/math/grid.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Vec2i = require 'nene.math.vec2i' 5 | local Rect = require 'nene.math.rect' 6 | 7 | 8 | 9 | 10 | 11 | local Grid = @record{ 12 | 13 | cell_size: Vec2i, 14 | 15 | gap: Vec2i, 16 | } 17 | 18 | local function nene_Grid_zero(): Grid end 19 | 20 | 21 | local Grid.zero: Grid = nene_Grid_zero() 22 | 23 | 24 | function Grid.copy(grid: *Grid): Grid end 25 | 26 | 27 | 28 | function Grid.__eq(a: Grid, b: Grid): boolean end 29 | 30 | 31 | 32 | function Grid.get_nth_cell_coord(nth: uint32, grid_width: uint16): Vec2i end 33 | 34 | 35 | 36 | function Grid.get_cell_position(grid: Grid, cell_coord: Vec2i): Vec2i end 37 | 38 | 39 | 40 | function Grid.get_nth_cell_position(grid: Grid, nth: uint32, grid_width: uint16): Vec2i end 41 | 42 | 43 | 44 | function Grid.get_rect(grid: Grid, cell_coord: Vec2i): Rect end 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | return Grid 53 | -------------------------------------------------------------------------------- /bindings/nelua/nene/math/rect.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Vec2i = require 'nene.math.vec2i' 5 | 6 | 7 | 8 | 9 | 10 | local Rect = @record{ 11 | 12 | pos: Vec2i, 13 | 14 | size: Vec2i, 15 | } 16 | 17 | local function nene_Rect_zero(): Rect end 18 | 19 | 20 | local Rect.zero: Rect = nene_Rect_zero() 21 | 22 | 23 | function Rect.copy(rect: *Rect): Rect end 24 | 25 | 26 | 27 | function Rect.to_raw(rect: Rect): SDL_Rect end 28 | 29 | 30 | 31 | function Rect.__eq(a: Rect, b: Rect): boolean end 32 | 33 | 34 | 35 | function Rect.add_pos(rect: Rect, pos: Vec2i): Rect end 36 | 37 | 38 | 39 | function Rect.add_size(rect: Rect, size: Vec2i): Rect end 40 | 41 | 42 | 43 | function Rect.get_center(rect: Rect): Vec2i end 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | return Rect 52 | -------------------------------------------------------------------------------- /bindings/nelua/nene/math/rectf.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Vec2 = require 'nene.math.vec2' 5 | local Rect = require 'nene.math.rect' 6 | 7 | 8 | 9 | 10 | 11 | local Rectf = @record{ 12 | 13 | pos: Vec2, 14 | 15 | size: Vec2, 16 | } 17 | 18 | local function nene_Rectf_zero(): Rectf end 19 | 20 | 21 | local Rectf.zero: Rectf = nene_Rectf_zero() 22 | 23 | 24 | function Rectf.copy(rect: *Rectf): Rectf end 25 | 26 | 27 | 28 | function Rectf.to_raw(rect: Rectf): SDL_FRect end 29 | 30 | 31 | 32 | function Rectf.__eq(a: Rectf, b: Rectf): boolean end 33 | 34 | 35 | 36 | function Rectf.add_pos(rect: Rectf, pos: Vec2): Rectf end 37 | 38 | 39 | 40 | function Rectf.add_size(rect: Rectf, size: Vec2): Rectf end 41 | 42 | 43 | 44 | function Rectf.get_center(rect: Rectf): Vec2 end 45 | 46 | 47 | 48 | function Rectf.to_rect(rect: Rectf): Rect end 49 | 50 | 51 | 52 | function Rectf.from_rect(rect: Rect): Rectf end 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | return Rectf 61 | -------------------------------------------------------------------------------- /bindings/nelua/nene/math/segment.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Vec2 = require 'nene.math.vec2' 5 | 6 | 7 | 8 | 9 | 10 | local Segment = @record{ 11 | 12 | origin: Vec2, 13 | 14 | ending: Vec2, 15 | } 16 | 17 | local function nene_Segment_zero(): Segment end 18 | 19 | --- returns a segment with zeroed values 20 | local Segment.zero: Segment = nene_Segment_zero() 21 | 22 | --- returns a segment copy from a reference 23 | function Segment.copy(segment: *Segment): Segment end 24 | 25 | 26 | --- tests the equality between two segments 27 | function Segment.__eq(a: Segment, b: Segment): boolean end 28 | 29 | 30 | --- Returns the segment as 2D vector. 31 | function Segment.as_vec2(segment: Segment): Vec2 end 32 | 33 | 34 | --- Get the midpoint (or middle point) of the segment. 35 | function Segment.get_midpoint(segment: Segment): Vec2 end 36 | 37 | 38 | --- Get the squared length of the segment. 39 | function Segment.len_sqr(segment: Segment): float32 end 40 | 41 | 42 | --- Get the length of the segment. 43 | function Segment.len(segment: Segment): float32 end 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | return Segment 52 | -------------------------------------------------------------------------------- /bindings/nelua/nene/math/shape.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Segment = require 'nene.math.segment' 5 | local Rect = require 'nene.math.rect' 6 | local Rectf = require 'nene.math.rectf' 7 | 8 | 9 | local Shape = @record{} 10 | 11 | 12 | 13 | --- Quadrilateral shape data strucuture, it holds four segments. 14 | local ShapeQuadrilateral = @record{ 15 | 16 | sides: [4]Segment, 17 | } 18 | 19 | local function nene_ShapeQuadrilateral_zero(): ShapeQuadrilateral end 20 | 21 | --- Returns a zeroed quatrilateral shape 22 | local Shape.zero: ShapeQuadrilateral = nene_ShapeQuadrilateral_zero() 23 | 24 | --- Returns a copy of the quadrilateral shape reference 25 | function ShapeQuadrilateral.copy(quadrilateral_shape: *ShapeQuadrilateral): ShapeQuadrilateral end 26 | 27 | 28 | --- Returns a quadrilateral shape made from a rectanle with floating values. 29 | function Shape.get_rectf_shape(rect: Rectf): ShapeQuadrilateral end 30 | 31 | 32 | --- Returns a quadrilateral shape made from a rectanle. 33 | function Shape.get_rect_shape(rect: Rect): ShapeQuadrilateral end 34 | 35 | 36 | --- Returns the diagonal of a rectangle as a segment, 37 | --- with direction that's horizontally left-to-right, while the vertical direction it's given 38 | --- on the `up_to_down` boolean parameter. 39 | function Shape.get_rectf_diagonal(rect: Rectf, up_to_down: boolean): Segment end 40 | 41 | 42 | 43 | 44 | 45 | 46 | local Shape.ShapeQuadrilateral: type = ShapeQuadrilateral 47 | 48 | return Shape 49 | -------------------------------------------------------------------------------- /bindings/nelua/nene/math/vec2.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Vec2i = require 'nene.math.vec2i' 5 | 6 | 7 | 8 | 9 | --- 2D mathematical vector with `x` and `y` components. 10 | local Vec2 = @record{ 11 | --- The `x` component. 12 | x: float32, 13 | --- The `y` component. 14 | y: float32, 15 | } 16 | 17 | --- converts a `nene_Vec2i` value to `nene_Vec2`. 18 | function Vec2.from_vec2i(v: Vec2i): Vec2 end 19 | 20 | 21 | --- converts a `nene_Vec2` value to `nene_Vec2i`. 22 | function Vec2.to_vec2i(v: Vec2): Vec2i end 23 | 24 | 25 | local function nene_Vec2_zero(): Vec2 end 26 | 27 | --- returns a vector with `0` value on `x` and `y` components. 28 | local Vec2.zero: Vec2 = nene_Vec2_zero() 29 | 30 | local function nene_Vec2_one(): Vec2 end 31 | 32 | --- returns a vector with `1` value on `x` and `y` components. 33 | local Vec2.one: Vec2 = nene_Vec2_one() 34 | 35 | --- returns a copy of the vector reference. 36 | function Vec2.copy(v: *Vec2): Vec2 end 37 | 38 | 39 | --- tests equality between `a` and `b` vectors. 40 | function Vec2.__eq(a: Vec2, b: Vec2): boolean end 41 | 42 | 43 | --- returns the sum of two vectors. 44 | function Vec2.__add(a: Vec2, b: Vec2): Vec2 end 45 | 46 | 47 | --- returns the difference between two vectors. 48 | function Vec2.__sub(a: Vec2, b: Vec2): Vec2 end 49 | 50 | 51 | local function nene_Vec2_mul(a: Vec2, b: Vec2): Vec2 end 52 | 53 | 54 | local function nene_Vec2_scale(a: Vec2, s: float32): Vec2 end 55 | 56 | 57 | --- returns the negation of the `v` vector. 58 | function Vec2.__unm(v: Vec2): Vec2 end 59 | 60 | 61 | --- returns a squared length of the `v` vector 62 | function Vec2.len_sqr(v: Vec2): float32 end 63 | 64 | 65 | --- returns the length of the `v` vector. 66 | function Vec2.len(v: Vec2): float32 end 67 | 68 | 69 | --- returns a vector that's the interpolation between `a` and `b` vectors by the scalar `t`. 70 | function Vec2.lerp(a: Vec2, b: Vec2, t: float32): Vec2 end 71 | 72 | 73 | --- returns the `v` vector normalized. 74 | function Vec2.normalize(v: Vec2): Vec2 end 75 | 76 | 77 | --- returns the dot product of `a` and `b` vectors. 78 | function Vec2.dot(a: Vec2, b: Vec2): float32 end 79 | 80 | 81 | --- returns the cross product of the `a` and `b` vector. 82 | --- > note: since this is a 2D vector (no `z` component), the cross product returns 83 | --- > the magnitude of the cross product vector instead of the vector itself. 84 | function Vec2.cross(a: Vec2, b: Vec2): float32 end 85 | 86 | 87 | --- returns the perpendicular vector of the vector v, with the same magnitude (length) 88 | --- > note: The "rotation" it's counterclockwise. 89 | function Vec2.perpendicular(v: Vec2): Vec2 end 90 | 91 | 92 | 93 | 94 | function Vec2.__mul(a: overload(float32, Vec2), b: overload(float32, Vec2)): Vec2 95 | ## static_assert(not (a.type.is_scalar and b.type.is_scalar), 'A Vec2 multiply needs at least one Vec2 value') 96 | ## if a.type == Vec2.value and b.type == Vec2.value then -- both are vectors 97 | return nene_Vec2_mul(a, b) 98 | ## elseif a.type.is_scalar then -- b is a vector 99 | return nene_Vec2_scale(b, a) 100 | ## else 101 | return nene_Vec2_scale(a, b) 102 | ## end 103 | end 104 | 105 | 106 | 107 | return Vec2 108 | -------------------------------------------------------------------------------- /bindings/nelua/nene/math/vec2i.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | --- 2D mathematical vector with `x` and `y` components (using 32-bit integer type, a.k.a. `int32_t`). 9 | local Vec2i = @record{ 10 | --- The `x` component. 11 | x: int32, 12 | --- The `y` component. 13 | y: int32, 14 | } 15 | 16 | local function nene_Vec2i_zero(): Vec2i end 17 | 18 | --- Returns a vector with `0` value on `x` and `y` components. 19 | local Vec2i.zero: Vec2i = nene_Vec2i_zero() 20 | 21 | local function nene_Vec2i_one(): Vec2i end 22 | 23 | --- Returns a vector with `1` value on `x` and `y` components. 24 | local Vec2i.one: Vec2i = nene_Vec2i_one() 25 | 26 | --- Returns a copy of the vector reference. 27 | function Vec2i.copy(v: *Vec2i): Vec2i end 28 | 29 | 30 | --- Tests equality between `a` and `b` vectors. 31 | function Vec2i.__eq(a: Vec2i, b: Vec2i): boolean end 32 | 33 | 34 | --- Returns the sum of two vectors. 35 | function Vec2i.__add(a: Vec2i, b: Vec2i): Vec2i end 36 | 37 | 38 | --- Returns the difference between two vectors. 39 | function Vec2i.__sub(a: Vec2i, b: Vec2i): Vec2i end 40 | 41 | 42 | local function nene_Vec2i_mul(a: Vec2i, b: Vec2i): Vec2i end 43 | 44 | 45 | local function nene_Vec2i_scale(v: Vec2i, s: float32): Vec2i end 46 | 47 | 48 | --- Returns the negation of the `v` vector. 49 | function Vec2i.__unm(v: Vec2i): Vec2i end 50 | 51 | 52 | --- Returns a squared length of the `v` vector 53 | function Vec2i.len_sqr(v: Vec2i): float32 end 54 | 55 | 56 | --- Returns the length of the `v` vector. 57 | function Vec2i.len(v: Vec2i): float32 end 58 | 59 | 60 | --- Returns a vector that's the interpolation between `a` and `b` vectors by the scalar `t`. 61 | function Vec2i.lerp(a: Vec2i, b: Vec2i, t: float32): Vec2i end 62 | 63 | 64 | --- Returns the dot product of `a` and `b` vectors. 65 | function Vec2i.dot(a: Vec2i, b: Vec2i): float32 end 66 | 67 | 68 | --- Note: since this is a 2D vector (or z=0), the cross product returns 69 | --- The magnitude of the cross product vector instead of the vector itself. 70 | function Vec2i.cross(a: Vec2i, b: Vec2i): float32 end 71 | 72 | 73 | --- Returns the perpendicular vector of the vector v, with the same magnitude (length) 74 | --- > note: The "rotation" it's counterclockwise. 75 | function Vec2i.perpendicular(v: Vec2i): Vec2i end 76 | 77 | 78 | 79 | 80 | function Vec2i.__mul(a: overload(float32, Vec2i), b: overload(float32, Vec2i)): Vec2i 81 | ## static_assert(not (a.type.is_scalar and b.type.is_scalar), 'A Vec2i multiply needs at least one Vec2i value') 82 | ## if a.type == Vec2i.value and b.type == Vec2i.value then -- both are vectors 83 | return nene_Vec2i_mul(a, b) 84 | ## elseif a.type.is_scalar then -- b is a vector 85 | return nene_Vec2i_scale(b, a) 86 | ## else 87 | return nene_Vec2i_scale(a, b) 88 | ## end 89 | end 90 | 91 | 92 | 93 | return Vec2i 94 | -------------------------------------------------------------------------------- /bindings/nelua/nene/texture.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Vec2 = require 'nene.math.vec2' 5 | local Rect = require 'nene.math.rect' 6 | local Color = require 'nene.color' 7 | 8 | 9 | 10 | 11 | 12 | local Texture = @record{ 13 | 14 | raw: *SDL_Texture, 15 | 16 | width: uint16, 17 | 18 | height: uint16, 19 | 20 | access: SDL_TextureAccess, 21 | } 22 | 23 | 24 | local TextureCreation = @record{ 25 | 26 | created: boolean, 27 | 28 | texture: Texture, 29 | } 30 | 31 | 32 | function Texture.zero(): Texture end 33 | 34 | 35 | 36 | function Texture.copy(texture: *Texture): Texture end 37 | 38 | 39 | 40 | function Texture.destroy(texture: *Texture): void end 41 | 42 | 43 | 44 | function Texture.get_raw(texture: Texture): *SDL_Texture end 45 | 46 | 47 | 48 | function Texture.apply_raw(texture: *Texture, raw_texture: *SDL_Texture): boolean end 49 | 50 | 51 | 52 | function Texture.set_blend_mode(texture: Texture, blend_mode: SDL_BlendMode): boolean end 53 | 54 | 55 | 56 | function Texture.set_color_mod(texture: Texture, color: Color): boolean end 57 | 58 | 59 | local function nene_Texture_create_with_access(width: uint16, height: uint16, access: SDL_TextureAccess): TextureCreation end 60 | 61 | 62 | function Texture.create_with_access(width: uint16, height: uint16, access: SDL_TextureAccess): (boolean, Texture) 63 | local maybe_result = nene_Texture_create_with_access(width, height, access) 64 | return maybe_result.created, maybe_result.texture 65 | end 66 | 67 | 68 | local function nene_Texture_create(width: uint16, height: uint16): TextureCreation end 69 | 70 | 71 | function Texture.create(width: uint16, height: uint16): (boolean, Texture) 72 | local maybe_result = nene_Texture_create(width, height) 73 | return maybe_result.created, maybe_result.texture 74 | end 75 | 76 | 77 | local function nene_Texture_load(filepath: cstring): TextureCreation end 78 | 79 | 80 | function Texture.load(filepath: cstring): (boolean, Texture) 81 | local maybe_result = nene_Texture_load(filepath) 82 | return maybe_result.created, maybe_result.texture 83 | end 84 | 85 | 86 | 87 | function Texture.get_full_source_rect(texture: Texture): Rect end 88 | 89 | 90 | 91 | function Texture.draw_to_point(texture: Texture, source: Rect, point: Vec2, is_world_position: boolean): boolean end 92 | 93 | 94 | 95 | function Texture.draw_to_rect(texture: Texture, source: Rect, destination: Rect): boolean end 96 | 97 | 98 | 99 | function Texture.draw_to_point_ex(texture: Texture, source: Rect, point: Vec2, is_world_position: boolean, angle: float64, rotation_center: Vec2, flip_x: boolean, flip_y: boolean): boolean end 100 | 101 | 102 | 103 | function Texture.draw_to_rect_ex(texture: Texture, source: Rect, destination: Rect, angle: float64, rotation_center: Vec2, flip_x: boolean, flip_y: boolean): boolean end 104 | 105 | 106 | 107 | 108 | 109 | 110 | local Texture.TextureCreation: type = TextureCreation 111 | 112 | return Texture 113 | -------------------------------------------------------------------------------- /bindings/nelua/nene/texture_atlas.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Grid = require 'nene.math.grid' 5 | local Vec2i = require 'nene.math.vec2i' 6 | local Vec2 = require 'nene.math.vec2' 7 | local Texture = require 'nene.texture' 8 | 9 | 10 | 11 | 12 | 13 | local TextureAtlas = @record{ 14 | 15 | width: uint16, 16 | 17 | grid: Grid, 18 | 19 | texture: Texture, 20 | } 21 | 22 | 23 | local TextureAtlasCreation = @record{ 24 | 25 | created: boolean, 26 | 27 | texture_atlas: TextureAtlas, 28 | } 29 | 30 | 31 | function TextureAtlas.zero(): TextureAtlas end 32 | 33 | 34 | 35 | function TextureAtlas.copy(texture_atlas: *TextureAtlas): TextureAtlas end 36 | 37 | 38 | 39 | function TextureAtlas.destroy(texture_atlas: *TextureAtlas): void end 40 | 41 | 42 | local function nene_TextureAtlas_load(filepath: cstring, width: uint16, grid: Grid): TextureAtlasCreation end 43 | 44 | 45 | function TextureAtlas.load(filepath: cstring, width: uint16, grid: Grid): (boolean, TextureAtlas) 46 | local maybe_result = nene_TextureAtlas_load(filepath, width, grid) 47 | return maybe_result.created, maybe_result.texture_atlas 48 | end 49 | 50 | 51 | 52 | function TextureAtlas.draw_sub_texture(texture_atlas: TextureAtlas, subtexture_coord: Vec2i, position: Vec2, is_world_pos: boolean): boolean end 53 | 54 | 55 | 56 | function TextureAtlas.draw_sub_texture_ex(texture_atlas: TextureAtlas, subtexture_coord: Vec2i, position: Vec2, is_world_pos: boolean, angle: float64, rotation_center: Vec2, flip_x: boolean, flip_y: boolean): boolean end 57 | 58 | 59 | 60 | function TextureAtlas.draw_nth_sub_texture(texture_atlas: TextureAtlas, nth: uint32, position: Vec2, is_world_pos: boolean): boolean end 61 | 62 | 63 | 64 | function TextureAtlas.draw_nth_sub_texture_ex(texture_atlas: TextureAtlas, nth: uint32, position: Vec2, is_world_pos: boolean, angle: float64, rotation_center: Vec2, flip_x: boolean, flip_y: boolean): boolean end 65 | 66 | 67 | 68 | function TextureAtlas.get_sub_texture_center(texture_atlas: TextureAtlas): Vec2 end 69 | 70 | 71 | 72 | 73 | 74 | 75 | local TextureAtlas.TextureAtlasCreation: type = TextureAtlasCreation 76 | 77 | return TextureAtlas 78 | -------------------------------------------------------------------------------- /bindings/nelua/nene/tilemap.nelua: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local Grid = require 'nene.math.grid' 5 | local Vec2i = require 'nene.math.vec2i' 6 | local Vec2 = require 'nene.math.vec2' 7 | local TextureAtlas = require 'nene.texture_atlas' 8 | 9 | 10 | 11 | 12 | 13 | local Tilemap = @record{ 14 | 15 | tileset: TextureAtlas, 16 | 17 | width: uint16, 18 | 19 | grid: Grid, 20 | 21 | tile_count: uint32, 22 | } 23 | 24 | 25 | function Tilemap.zero(): Tilemap end 26 | 27 | 28 | 29 | function Tilemap.copy(tilemap: *Tilemap): Tilemap end 30 | 31 | 32 | 33 | function Tilemap.get_size_in_tiles(tilemap: Tilemap): Vec2i end 34 | 35 | 36 | 37 | function Tilemap.get_size(tilemap: Tilemap): Vec2i end 38 | 39 | 40 | 41 | function Tilemap.draw(tilemap: Tilemap, position: Vec2, is_world_pos: boolean, tiles: *[0]uint16, count: uint32): boolean end 42 | 43 | 44 | 45 | function Tilemap.destroy(tilemap: *Tilemap): void end 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | return Tilemap 54 | -------------------------------------------------------------------------------- /compile_flags.txt: -------------------------------------------------------------------------------- 1 | -Wall 2 | -Wextra 3 | -Wpedantic 4 | -std=c99 5 | -I./include/ 6 | -I./external/SDL/include 7 | -I./external/SDL_mixer/include 8 | -I./external/SDL_image 9 | -I./external/SDL_ttf 10 | -------------------------------------------------------------------------------- /examples/c/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021-present André Luiz Alvares 2 | # Nene is licensed under the Zlib license. 3 | # Please refer to the LICENSE file for details 4 | # SPDX-License-Identifier: Zlib 5 | 6 | # This version should match the interval of nene's root CMakeLists.txt 7 | cmake_minimum_required(VERSION 3.23-3.26) 8 | 9 | # Set the project name, version and language 10 | project( 11 | nene_examples 12 | VERSION 0.1 13 | LANGUAGES C 14 | ) 15 | 16 | # finally, the examples 17 | add_executable(collisions WIN32 collisions.c) 18 | target_link_libraries(collisions PRIVATE nene) 19 | 20 | # set the C standard properties, 21 | # be sure to follow the same standard of nene (see the src/CMakeLists.txt file) 22 | set_target_properties(collisions PROPERTIES 23 | C_STANDARD 99 24 | C_STANDARD_REQUIRED YES 25 | C_EXTENSIONS NO 26 | ) 27 | -------------------------------------------------------------------------------- /examples/c/collisions.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | /* 9 | you can compile this source code using a "simple" compiler command (clang example, on Fedora linux 39): 10 | clang examples/c/collisions.c -o collisions -lnene -lSDL2 -lSDL2_mixer -lSDL2_ttf -lSDL2_image -lm -I libnene/include/ -I libnene/include/SDL2/ -L libnene/lib/ -L libnene/lib64 -Wl,-rpath="\$ORIGIN"/libnene/lib64 11 | 12 | command explanation: 13 | clang: it's clang 14 | examples/c/collisions.c: we're compiling this source code 15 | -o collisions: the output it's a "collisions" executable file 16 | -lnene -lSDL2 -lSDL2_mixer -lSDL2_ttf -lSDL2_image -lm: we're linking to nene, all SDL2 libraries and the math library 17 | -I libnene/include/ -I libnene/include/SDL2/: adding the include paths, otherwise the compiler can't find the headers of "#include" directives 18 | -L libnene/lib/ -L libnene/lib64: adding the search path of binary libraries, on linux "lib" it's for Nene and "lib64" it's for SDL2 19 | -Wl,-rpath="\$ORIGIN"/libnene/lib64: SDL2 it's loaded at runtime (because they're shared libraries), this flags makes the executable to search these libraries on "$ORIGIN/libnene/lib64", "$ORIGIN" is the path relative to the exectuable. 20 | 21 | You can also check the CMakeLists to see how to use CMake in this case, although CMake it's still an WIP work. 22 | */ 23 | 24 | #include 25 | #include "nene/core.h" 26 | #include "nene/collision.h" 27 | 28 | int main(int argc, char *argv[]) { 29 | bool ok = nene_Core_init("collision test", 1280, 720, SDL_WINDOW_SHOWN); 30 | if (!ok) { 31 | return EXIT_FAILURE; 32 | } 33 | 34 | // nene_Rectf rect = { .size = { .x = 40, .y = 260 } }; 35 | nene_Rectf rect = { .size = { .x = 4, .y = 26 } }; 36 | 37 | #define SEGMENTS_COUNT 4 38 | 39 | nene_Segment segments[SEGMENTS_COUNT] = { 40 | { { -500, 200 }, { -400, 150 } }, 41 | { { -300, -130 }, { -120, -100 } }, 42 | { { -40, -030 }, { 220, 200 } }, 43 | { { 200, -10 }, { 400, -300 } }, 44 | }; 45 | 46 | do { 47 | nene_Core_update(); 48 | 49 | nene_Core_render_clear(nene_Color_bg()); 50 | 51 | nene_Vec2 delta = nene_Vec2_zero(); 52 | 53 | if (nene_Core_is_scancode_held(SDL_SCANCODE_W)) { 54 | delta.y += 1; 55 | } 56 | if (nene_Core_is_scancode_held(SDL_SCANCODE_S)) { 57 | delta.y -= 1; 58 | } 59 | if (nene_Core_is_scancode_held(SDL_SCANCODE_A)) { 60 | delta.x -= 1; 61 | } 62 | if (nene_Core_is_scancode_held(SDL_SCANCODE_D)) { 63 | delta.x += 1; 64 | } 65 | 66 | rect.pos = nene_Vec2_add(rect.pos, delta); 67 | 68 | if (!nene_Core_is_scancode_held(SDL_SCANCODE_SPACE)) { 69 | for (int i = 0; i < SEGMENTS_COUNT; ++i) { 70 | nene_Collision collision = nene_Collision_rectf_with_segment(rect, segments[i], delta); 71 | if (collision.collided) { 72 | rect.pos = nene_Vec2_add(rect.pos, collision.delta); 73 | } 74 | } 75 | } 76 | 77 | nene_Color color = nene_Core_is_scancode_held(SDL_SCANCODE_SPACE) ? nene_Color_cyan(): nene_Color_white(); 78 | 79 | nene_Core_render_draw_rect(nene_Rectf_to_rect(rect), true, color, true); 80 | 81 | for (int i = 0; i < SEGMENTS_COUNT; ++i) { 82 | nene_Core_render_draw_line(segments[i].origin, segments[i].ending, nene_Color_black(), true); 83 | } 84 | 85 | nene_Core_render_present(); 86 | } while (!nene_Core_should_quit()); 87 | 88 | #undef SEGMENTS_COUNT 89 | 90 | nene_Core_terminate(); 91 | return EXIT_SUCCESS; 92 | } 93 | -------------------------------------------------------------------------------- /examples/carp/minimal.carp: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2021-present André Luiz Alvares 2 | ;; Nene is licensed under the Zlib license. 3 | ;; Please refer to the LICENSE file for details 4 | ;; SPDX-License-Identifier: Zlib 5 | 6 | (Project.config "search-path" "./bindings/carp") 7 | (Project.config "title" "Minimal_Hello_World") 8 | (Project.config "output-directory" ".") 9 | 10 | (load "nene/core.carp") 11 | (load "nene/color.carp") 12 | 13 | ;; utils 14 | (register exit-failure Int "EXIT_FAILURE") 15 | (register exit-success Int "EXIT_SUCCESS") 16 | (defn fl [v] (from-long v)) 17 | 18 | (defn tick [] 19 | (ignore-do 20 | (Core.update) 21 | (Core.render-clear Color.bg) 22 | (Core.render-present))) 23 | 24 | (defn main [] 25 | (let [ok (Core.init (cstr "Minimal Hello World") (fl 800l) (fl 600l) sdl-window-shown)] 26 | (if (not ok) 27 | (do 28 | (System.exit exit-failure) 29 | exit-failure) 30 | (do 31 | (until (Core.should-quit) 32 | (tick)) 33 | (Core.terminate) 34 | exit-success)))) 35 | -------------------------------------------------------------------------------- /examples/nelua/.neluacfg.lua: -------------------------------------------------------------------------------- 1 | return { 2 | add_path = { 3 | "../../bindings/nelua/" 4 | }, 5 | cflags = "-I../../libnene/include -I../../libnene/include/SDL2", 6 | ldflags = "-L../../libnene/lib -L../../libnene/lib64 -Wl,-rpath=$ORIGIN/../../libnene/lib64", 7 | } 8 | -------------------------------------------------------------------------------- /examples/nelua/camera.nelua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | --[[ 9 | In this demo, is presented a basic camera control 10 | 11 | Actually, there is no "camera" on Nene, instead, there is a 12 | `Vec2` offset done in all rendering calls, this offset is the 13 | `render_offset` field. 14 | 15 | You can modify this field in order to add a basic camera control. 16 | ]] 17 | 18 | local Nene = require 'nene.core' 19 | local Color = require 'nene.color' 20 | local Rect = require 'nene.math.rect' 21 | 22 | --[[ 23 | In this example it's showcased how to split your game logic into multiple functions. 24 | 25 | It's pretty simple, since Nene it's a singleton*, you can get the instance of nene 26 | just by calling `Nene.instance()`, note however that Nene must be initialized before 27 | using this function. 28 | 29 | *Singleton pattern: https://en.wikipedia.org/wiki/Singleton_pattern 30 | ]] 31 | local function draw_parallax_grid() 32 | -- let's draw a grid with parallax effect! 33 | 34 | -- get the running nene instance 35 | local nene = Nene.instance() 36 | 37 | -- save our original render_offset value 38 | local original_offset = nene.render_offset 39 | 40 | -- multiply render_offset by half, this is what makes the parallax effect here. 41 | -- you can read more about parallax on Pedro's tutorial: 42 | -- https://www.patreon.com/posts/parallax-and-7863658 43 | nene.render_offset = nene.render_offset * 0.5 44 | 45 | -- draw the grid 46 | for i = -64, 64 do 47 | Nene.render_draw_line({16 * i, -2048}, {16 * i, 2048}, Color.black, true) 48 | Nene.render_draw_line({-2048, 16 * i}, {2048, 16 * i}, Color.black, true) 49 | end 50 | 51 | -- at the end of the function, we set the render_offset to the original value 52 | nene.render_offset = original_offset 53 | end 54 | 55 | local function draw_parallax_shadow() 56 | -- same logic as draw_parallax_grid 57 | local nene = Nene.instance() 58 | 59 | local original_offset = nene.render_offset 60 | nene.render_offset = nene.render_offset * 0.5 61 | 62 | -- we can also use defer, so this statement is done at the end of the function 63 | -- see more here: https://nelua.io/overview/#defer 64 | defer nene.render_offset = original_offset end 65 | 66 | -- draw shadow 67 | Nene.render_draw_rect({ {-8, 8}, {16, 16} }, false, {0, 0, 0, 100}, true) 68 | end 69 | 70 | -- all the life-cycle of this example is contained in this function 71 | local function camera() 72 | --[[ 73 | Initialize and test nene initialization and defer it's termination (https://nelua.io/overview/#defer). 74 | 75 | When nene is initialized, you get an ok status which should be asserted; 76 | the ok status is `true` on initialization success. 77 | ]] 78 | local ok = Nene.init('nene basic camera', 256, 256) 79 | assert(ok, 'error: nene initialization failed') 80 | 81 | defer Nene.terminate() end 82 | 83 | --[[ 84 | As already descibried, If you need access to the instance, you can get it using `Nene.instance`, 85 | the instance it's never reallocated, thus you don't need to worry about getting an invalidated 86 | pointer. 87 | ]] 88 | local nene = Nene.instance() 89 | 90 | -- let's define a rect 91 | local rect_on_center: Rect = { pos = { -16, 16 }, size = { 32, 32 } } 92 | 93 | repeat 94 | -- update nene state, always do this on the beg 95 | Nene.update() 96 | 97 | -- modify `render_offset` field in order to get a basic camera control 98 | if Nene.is_scancode_held(SDL_SCANCODE_RIGHT) then 99 | nene.render_offset.x = nene.render_offset.x + 1 100 | elseif Nene.is_scancode_held(SDL_SCANCODE_LEFT) then 101 | nene.render_offset.x = nene.render_offset.x - 1 102 | end 103 | 104 | if Nene.is_scancode_held(SDL_SCANCODE_UP) then 105 | nene.render_offset.y = nene.render_offset.y - 1 106 | elseif Nene.is_scancode_held(SDL_SCANCODE_DOWN) then 107 | nene.render_offset.y = nene.render_offset.y + 1 108 | end 109 | 110 | -- rendering logic -- 111 | Nene.render_clear(Color.bg) 112 | do 113 | draw_parallax_grid() 114 | draw_parallax_shadow() 115 | 116 | -- draw the rect, note that rect_on_center is never modified 117 | Nene.render_draw_rect(rect_on_center, false, Color.white, true) 118 | end 119 | 120 | Nene.render_present() 121 | until Nene.should_quit() 122 | end 123 | 124 | -- run example 125 | camera() 126 | -------------------------------------------------------------------------------- /examples/nelua/game_controller.nelua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | --[[ 9 | Gamepad example 10 | ]] 11 | 12 | -- nene modules 13 | local math = require 'math' 14 | 15 | local Nene = require 'nene.core' 16 | local Color = require 'nene.color' 17 | local Vec2 = require 'nene.math.vec2' 18 | local Rect = require 'nene.math.rect' 19 | 20 | --[[ 21 | Let's create an rect that moves with our gamepad left analog thumbstick and 22 | change to an random color with the bottom right-pad button (A button on xbox controller) 23 | ]] 24 | local function game_controller() 25 | local function get_random_color(prev_color: Color) 26 | math.randomseed() 27 | local r: byte, b: byte, g: byte 28 | 29 | local function is_approx_same_color(r: byte, g: byte, b: byte, prev_color: Color): boolean 30 | return math.abs(r - prev_color.r) < 50 and math.abs(g - prev_color.g) < 50 and math.abs(b - prev_color.b) < 50 31 | end 32 | 33 | repeat 34 | r, g, b = math.random(0x00, 0xff), math.random(0x00, 0xff), math.random(0x00, 0xff) 35 | until not (is_approx_same_color(r, g, b, Color.bg) or is_approx_same_color(r, g, b, prev_color)) 36 | 37 | return Color{ r, g, b, 0xff } 38 | end 39 | 40 | local ok = Nene.init('nene game controller', 800, 600) 41 | assert(ok, 'error: nene initialization failed') 42 | 43 | local rect: Rect = { 44 | pos = { x = -40, y = 40 }, 45 | size = { x = 80, y = 80 } 46 | } 47 | local rect_color = get_random_color() 48 | 49 | repeat 50 | -- update game state 51 | Nene.update() 52 | 53 | -- update rect color by pressing FaceButtonDown button 54 | if Nene.is_gamepad_button_pressed(0, SDL_CONTROLLER_BUTTON_A) then 55 | rect_color = get_random_color(rect_color) 56 | end 57 | 58 | -- update rect position by left thumbstick value 59 | local lx = Nene.get_gamepad_axis(0, SDL_CONTROLLER_AXIS_LEFTX) 60 | local ly = Nene.get_gamepad_axis(0, SDL_CONTROLLER_AXIS_LEFTY) 61 | rect = rect:add_pos({lx, -ly}) 62 | 63 | -- draw game state 64 | Nene.render_clear(Color.bg) 65 | 66 | Nene.render_draw_rect(rect, false, rect_color, true) 67 | 68 | Nene.render_present() 69 | until Nene.should_quit() 70 | 71 | Nene.terminate() 72 | end 73 | 74 | game_controller() 75 | -------------------------------------------------------------------------------- /examples/nelua/rects.nelua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | -- nene modules 9 | local Nene = require 'nene.core' 10 | local Color = require 'nene.color' 11 | local Vec2 = require 'nene.math.vec2' 12 | local Rectf = require 'nene.math.rectf' 13 | local Intersections = require 'nene.intersections' 14 | local Collision = require 'nene.collision' 15 | 16 | -- example 17 | local function rects() 18 | local ok = Nene.init('nene rects', 256, 256) 19 | assert(ok, 'error: nene initialization failed') 20 | 21 | -- declare position and two rects 22 | local rect_a: Rectf, rect_b: Rectf = { pos = {-32, -48}, size = { 32, 32} }, 23 | { pos = {-64, 32}, size = {128, 64} } 24 | 25 | repeat 26 | Nene.update() 27 | 28 | local delta_pos: Vec2 29 | if Nene.is_scancode_held(SDL_SCANCODE_W) then 30 | delta_pos.y = 1 31 | end 32 | if Nene.is_scancode_held(SDL_SCANCODE_S) then 33 | delta_pos.y = -1 34 | end 35 | if Nene.is_scancode_held(SDL_SCANCODE_A) then 36 | delta_pos.x = -1 37 | end 38 | if Nene.is_scancode_held(SDL_SCANCODE_D) then 39 | delta_pos.x = 1 40 | end 41 | rect_a.pos = rect_a.pos + delta_pos 42 | 43 | local collision = Collision.rectf_with_rectf(rect_a, rect_b, delta_pos) 44 | 45 | if collision.collided then 46 | rect_a.pos = rect_a.pos + collision.delta 47 | end 48 | 49 | Nene.render_clear(Color.bg) 50 | 51 | Nene.render_draw_rect(rect_a:to_rect(), true, Color.white, true) 52 | Nene.render_draw_rect(rect_b:to_rect(), true, Color.yellow, true) 53 | 54 | Nene.render_present() 55 | until Nene.should_quit() 56 | 57 | Nene.terminate() 58 | end 59 | 60 | rects() 61 | -------------------------------------------------------------------------------- /examples/nelua/render_clip.nelua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | -- This example shows how to using render clipping using a rectangle. 9 | 10 | local Nene = require 'nene.core' 11 | local Color = require 'nene.color' 12 | local Rect = require 'nene.math.rect' 13 | 14 | local function rects() 15 | local ok = Nene.init('nene render rect clip', 256, 256) 16 | assert(ok, 'error: nene initialization failed') 17 | 18 | -- create two rects 19 | local rect_clip: Rect = { size = { 48, 48 } } 20 | 21 | repeat 22 | Nene.update() 23 | 24 | -- copy mouse position to rect_clip position 25 | local nene = Nene.instance() 26 | rect_clip.pos = Nene.screen_point_to_world_pos(nene.mouse_pos):to_vec2i() 27 | 28 | Nene.render_clear(Color.bg) 29 | 30 | -- draw grid 31 | for i = -16, 16 do 32 | Nene.render_draw_line({ 16 * i, -256 }, { 16 * i, 256 }, Color.black, true) 33 | Nene.render_draw_line({ -256, 16 * i }, { 256, 16 * i }, Color.black, true) 34 | end 35 | 36 | -- set render clipping 37 | Nene.set_render_clip(rect_clip, false) 38 | Nene.render_draw_rect(rect_clip, true, Color.red, true) 39 | 40 | Nene.render_present() 41 | until Nene.should_quit() 42 | 43 | Nene.terminate() 44 | end 45 | 46 | rects() 47 | -------------------------------------------------------------------------------- /examples/nelua/segments_intersection.nelua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | -- This example shows how intersection between two segments works 9 | 10 | -- nene modules 11 | local Nene = require 'nene.core' 12 | local Color = require 'nene.color' 13 | local Rect = require 'nene.math.rect' 14 | local Vec2 = require 'nene.math.vec2' 15 | local Segment = require 'nene.math.segment' 16 | local Intersections = require 'nene.intersections' 17 | 18 | -- example 19 | local function example() 20 | local ok = Nene.init('nene segments intersection', 256, 256) 21 | assert(ok, 'error: nene initialization failed') 22 | 23 | -- create the points of intersection ab and cd 24 | local ab: Segment = { 25 | origin = { -120, 120 }, 26 | ending = { 120, -120 }, 27 | } 28 | local cd: Segment = { 29 | origin = { 120, 120 }, 30 | ending = { -120, -120 }, 31 | } 32 | 33 | local controlling_d: boolean 34 | 35 | local nene = Nene.instance() 36 | 37 | repeat 38 | Nene.update() 39 | 40 | if Nene.is_mouse_button_pressed(1) then 41 | controlling_d = not controlling_d 42 | end 43 | 44 | local mouse_world_pos = Nene.screen_point_to_world_pos(nene.mouse_pos) 45 | 46 | if controlling_d then 47 | cd.ending = mouse_world_pos 48 | else 49 | ab.ending = mouse_world_pos 50 | end 51 | 52 | local intersection = Intersections.IntersectionSegmentWithSegment.get_intersection(ab, cd) 53 | 54 | Nene.render_clear(Color.black) 55 | 56 | -- draw ab segment 57 | Nene.render_draw_line(ab.origin, ab.ending, Color.blue, true) 58 | -- draw cd segment 59 | Nene.render_draw_line(cd.origin, cd.ending, Color.green, true) 60 | 61 | -- draw intersection point if an intersection exists 62 | if intersection.intersected then 63 | local rect: Rect = { pos = intersection.point:to_vec2i(), size = {3, 3} } 64 | Nene.render_draw_rect(rect, false, Color.red, true) 65 | end 66 | 67 | Nene.render_present() 68 | until Nene.should_quit() 69 | 70 | Nene.terminate() 71 | end 72 | 73 | example() 74 | -------------------------------------------------------------------------------- /examples/nelua/sound_and_music.nelua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | local math = require 'math' 9 | 10 | local Nene = require 'nene.core' 11 | local Color = require 'nene.color' 12 | local Sound = require 'nene.audio.sound' 13 | local Music = require 'nene.audio.music' 14 | 15 | local function audio_and_music() 16 | -- nene initialization and deferred de-initialization with to-be-closed variable 17 | local ok = Nene.init('nene audio and music', 128, 128) 18 | assert(ok, 'error: nene initialization failed') 19 | 20 | defer Nene.terminate() end 21 | 22 | -- sound and music loading and deferred destroyment 23 | local ok, click = Sound.load('../../resources/pixelclick.wav') 24 | assert(ok , 'could not load pixelclick sound') 25 | 26 | local ok, bossa = Music.load('../../resources/8bit Bossa.ogg') 27 | assert(ok, 'could not load 8bit Bossa music') 28 | 29 | defer 30 | click:destroy() 31 | bossa:destroy() 32 | end 33 | 34 | local mus_volume, click_volume = 1.0, 1.0 35 | 36 | bossa:play() 37 | 38 | repeat 39 | Nene.update() 40 | 41 | if Nene.is_scancode_pressed(SDL_SCANCODE_P) then 42 | mus_volume = math.clamp(mus_volume + 0.1, 0.0, 1.0) 43 | Music.set_volume(mus_volume) 44 | elseif Nene.is_scancode_pressed(SDL_SCANCODE_O) then 45 | mus_volume = math.clamp(mus_volume - 0.1, 0.0, 1.0) 46 | Music.set_volume(mus_volume) 47 | end 48 | 49 | if Nene.is_scancode_pressed(SDL_SCANCODE_M) then 50 | click_volume = math.clamp(click_volume + 0.1, 0.0, 1.0) 51 | click:set_volume(click_volume) 52 | elseif Nene.is_scancode_pressed(SDL_SCANCODE_N) then 53 | click_volume = math.clamp(click_volume - 0.1, 0.0, 1.0) 54 | click:set_volume(click_volume) 55 | end 56 | 57 | if Nene.is_scancode_pressed(SDL_SCANCODE_SPACE) or Nene.is_mouse_button_pressed(1) then 58 | click:play() 59 | end 60 | 61 | Nene.render_clear(Color.bg) 62 | 63 | Nene.render_present() 64 | until Nene.should_quit() 65 | end 66 | 67 | audio_and_music() 68 | -------------------------------------------------------------------------------- /examples/nelua/sprite_animation.nelua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | ]] 7 | 8 | -- This example presents how to use the texture atlas module and using some more advanced texture drawing 9 | 10 | local math = require 'math' 11 | 12 | local Nene = require 'nene.core' 13 | local Color = require 'nene.color' 14 | local Texture = require 'nene.texture' 15 | local TextureAtlas = require 'nene.texture_atlas' 16 | local Animation = require 'nene.animation' 17 | 18 | local function game() 19 | -- initialize nene and test if got succesfully initialized 20 | local ok = Nene.init('nene sprite animation', (64*4), (64*4)) 21 | assert(ok, 'error: nene initialization failed') 22 | 23 | local nene = Nene.instance() 24 | 25 | -- rotation parameter, it'll be increased over time 26 | local rotation = 0.0 27 | 28 | local anim: Animation = { interval = 500 } 29 | local anim2: Animation = { interval = 500 } 30 | 31 | print('eq test:', anim == anim2) 32 | 33 | --[[ 34 | Create a texture atlas with img_shapes.png image on it, 35 | then test initialization and defers it's destruction to the end of scope 36 | using a to-be-closed variable. 37 | 38 | note that img_shapes images contains four shapes, on a grid with 32x32 size per cell. 39 | 40 | More details can be read on the texture_atlas example. 41 | ]] 42 | local ok, spritesheet = TextureAtlas.load('../../resources/img_shapes.png', 2, { cell_size = {32, 32} }) 43 | assert(ok, "error: 'img_shapes.png' could not be loaded") 44 | 45 | defer 46 | spritesheet:destroy() 47 | end 48 | 49 | --[[ 50 | Let's create a timer, this will store the time just after nene initialization. 51 | this variable will only be modified when the sprite changes. 52 | ]] 53 | local prev_time = nene.current_time 54 | 55 | --[[ 56 | The sprite index which we want to render on the current frame, this 57 | refers to the sub-sprite of the spritesheet, the first one is on top-left, 58 | the last is on bottom-right, the flow is left-to-right, up-to-down. 59 | ]] 60 | local sprite_index = 0 61 | 62 | -- with everything setup, now we run our game loop until the game will quit 63 | repeat 64 | -- updates the nene internal state 65 | Nene.update() 66 | 67 | -- increase rotation over time 68 | rotation = nene.current_time * 0.01 69 | 70 | --[[ 71 | If the difference between the last time we changed the sprite and now 72 | is greater than one second, then we update prev_time and change 73 | the sprite index used on spritesheet rendering. 74 | ]] 75 | if nene.current_time - prev_time > 1000 then 76 | prev_time = nene.current_time 77 | sprite_index = (sprite_index + 1) % 4 78 | end 79 | 80 | -- let's draw our game, first we need to clear the backbuffer. 81 | Nene.render_clear(Color.bg) 82 | 83 | --[[ 84 | Then, draw the sprite from the spritesheet at {10, 10} position, at screen space, 85 | using the spritesheet texture atlas, using the sprite index. 86 | ]] 87 | spritesheet:draw_nth_sub_texture_ex(sprite_index, { 10, 10 }, false, rotation, spritesheet:get_sub_texture_center()) 88 | 89 | -- finally, swap buffers and present our final result 90 | Nene.render_present() 91 | until Nene.should_quit() 92 | 93 | Nene.terminate() 94 | end 95 | 96 | -- run the game 97 | game() 98 | -------------------------------------------------------------------------------- /examples/nelua/texture_atlases.nelua: -------------------------------------------------------------------------------- 1 | -- The texture atlases example 2 | -- This example shows how to use the texture atlas module. 3 | 4 | --[[ 5 | Copyright (c) 2021-present André Luiz Alvares 6 | Nene is licensed under the Zlib license. 7 | Please refer to the LICENSE file for details 8 | SPDX-License-Identifier: Zlib 9 | ]] 10 | 11 | local Nene = require 'nene.core' 12 | local TextureAtlas = require 'nene.texture_atlas' 13 | local Color = require 'nene.color' 14 | 15 | local function texture_atlas_example() 16 | -- initialize nene 17 | local ok = Nene.init('nene texture atlas', 240, 160) 18 | assert(ok, "nene couldn't be initialized") 19 | 20 | --[[ 21 | First, let's load the "img_shapes" image from "../../resources", this image contains 22 | four shapes with two shapes per-line, aligned on a 32x32 grid: 23 | 24 | circle, square, 25 | right triangle and a isosceles triangle. 26 | 27 | Since there is two shapes per-line, the `width` should be set to two; 28 | 29 | Although it's a 64x64 texture, each shape it's on a cell of an 32x32 grid, thus the 30 | cell_size of grid should be set to { 32, 32}, there's no gap between cells, thus the 31 | `gap` field isn't set. 32 | ]] 33 | local ok, atlas = TextureAtlas.load('../../resources/img_shapes.png', 2, { cell_size = {32, 32 }}) 34 | assert(ok, "TextureAtlas texture couldn't be loaded") 35 | 36 | defer 37 | atlas:destroy() 38 | end 39 | 40 | -- game loop 41 | repeat 42 | -- update nene state 43 | Nene.update() 44 | 45 | -- clear the rendering target 46 | Nene.render_clear(Color.bg) 47 | 48 | --[[ 49 | let's first draw the circle sub-texture at middle-top of the screen, the circle 50 | it's on the first column and the first row, since it's 0-based, the values are 51 | 0 and 0. 52 | 53 | As texture in general, sub-textures are painted from the top-left corner, thus we need 54 | to subtract 16px (half of the sub-texture width) in order to get really on the center of 55 | the screen. 56 | ]] 57 | atlas.texture:set_color_mod(Color.white) 58 | atlas:draw_sub_texture({0, 0}, { 120-16, 8 }, false) 59 | 60 | --[[ 61 | Then let's draw some isosceles triangles at the bottom of the screen below. 62 | 63 | Here we used another method: drawing the `n`th sub-texture, 64 | since the triangle is the 4th sub-texture and it's 0-based, we use `3` value. 65 | 66 | `draw_sub_texture` and `draw_nth_sub_texture` accepts extended options for drawing just as `Texture.draw`, thus 67 | we can use color tint, angle, rotation center, and finally horizontal and vertical flipping. 68 | 69 | Here the triangle it's drawn with maroon tint with 10 degree rotation from left-bottom. 70 | ]] 71 | atlas.texture:set_color_mod({ 134, 0, 0}) 72 | for i = -8, 240+8, 32 do 73 | -- n position tint angle rotation_center 74 | atlas:draw_nth_sub_texture_ex(3, {i, 160-32}, false, 10, { 0.0, 1.0 }) 75 | end 76 | 77 | -- clear present the painted rendering target 78 | Nene.render_present() 79 | until Nene.should_quit() 80 | 81 | Nene.terminate() 82 | end 83 | 84 | texture_atlas_example() 85 | -------------------------------------------------------------------------------- /external/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021-present André Luiz Alvares 2 | # Nene is licensed under the Zlib license. 3 | # Please refer to the LICENSE file for details 4 | # SPDX-License-Identifier: Zlib 5 | 6 | # This cmake file add the "external_libs" library target, with all external modules (that is, SDL libraries) 7 | 8 | # setup options, in summary, use SDL's vendored dependencies instead of relying on package managers and 9 | # do not "install" SDL. 10 | set(SDL2IMAGE_VENDORED ON CACHE BOOL "") 11 | set(SDL2MIXER_VENDORED ON CACHE BOOL "") 12 | set(SDL2TTF_VENDORED ON CACHE BOOL "") 13 | set(SDL2_DISABLE_INSTALL OFF CACHE BOOL "") 14 | 15 | set(SDL2IMAGE_SAMPLES OFF CACHE BOOL "") 16 | set(SDL2TTF_SAMPLES OFF CACHE BOOL "") 17 | set(SDL2MIXER_SAMPLES OFF CACHE BOOL "") 18 | 19 | set(SDL2IMAGE_TESTS OFF CACHE BOOL "") 20 | set(SDL_TEST OFF CACHE BOOL "") 21 | 22 | # Adds the SDL subdirectories, which are git sub-modules, 23 | # be sure to clone nene with "recurse-submodules" flag, otherwise the SDL 24 | # libraries will not be clonned with nene. 25 | add_subdirectory(SDL) 26 | add_subdirectory(SDL_image) 27 | add_subdirectory(SDL_mixer) 28 | add_subdirectory(SDL_ttf) 29 | 30 | # creating the "external_libs" library target 31 | add_library(external_libs INTERFACE) 32 | 33 | # adding linking to the SDL libraries to the project build. 34 | # context: https://wiki.libsdl.org/SDL2/README/cmake 35 | if(TARGET SDL2::SDL2main) 36 | target_link_libraries(external_libs INTERFACE SDL2::SDL2main) 37 | endif() 38 | 39 | target_link_libraries(external_libs INTERFACE SDL2::SDL2) 40 | target_link_libraries(external_libs INTERFACE SDL2_image::SDL2_image) 41 | target_link_libraries(external_libs INTERFACE SDL2_mixer::SDL2_mixer) 42 | target_link_libraries(external_libs INTERFACE SDL2_ttf::SDL2_ttf) 43 | -------------------------------------------------------------------------------- /gen-docs.lua: -------------------------------------------------------------------------------- 1 | local nldoc = require 'nldoc' 2 | 3 | local symbol_template = [[ 4 | ### $(name) 5 | 6 | ```lua 7 | $(code) 8 | ``` 9 | 10 | $(text) 11 | 12 | ]] 13 | 14 | local function doc(filename, path, docs_path) 15 | local emitter = nldoc.Emitter.create() 16 | 17 | if string.find(filename, '.nelua') then 18 | print('documenting '..path..filename..'..') 19 | 20 | nldoc.generate_doc(emitter, path..filename, { symbol_template = symbol_template }) 21 | 22 | local emitted = emitter:generate() 23 | 24 | local summary = {'### Summary\n'} 25 | for title in emitted:gmatch('### ([%s%g]-)\n') do 26 | local title_link = title:lower():gsub('[%.%:]', '') 27 | table.insert(summary, string.format('* [%s](#%s)\n', title, title_link)) 28 | end 29 | 30 | local outfilename = filename:gsub('.nelua', '.md') 31 | 32 | nldoc.write_file('docs/'..docs_path..outfilename, table.concat(summary)..'\n'..emitted) 33 | end 34 | end 35 | 36 | local function doc_dir(dirname, docs_dir) 37 | local ignored_files = {} 38 | 39 | lfs.mkdir('docs/'..docs_dir) 40 | 41 | for filename in lfs.dir(dirname) do 42 | if not ignored_files[filename] then 43 | doc(filename, dirname, docs_dir) 44 | end 45 | end 46 | end 47 | 48 | doc_dir('nene/', '') 49 | doc_dir('nene/math/', 'math/') 50 | doc_dir('nene/audio/', 'audio/') 51 | doc_dir('nene/raw/', 'raw/') 52 | -------------------------------------------------------------------------------- /genbindings.sh: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021-present André Luiz Alvares 2 | # Nene is licensed under the Zlib license. 3 | # Please refer to the LICENSE file for details 4 | # SPDX-License-Identifier: Zlib 5 | 6 | # note: this script currently requires clang-check and tl commands. 7 | # clang-check: is part of clang tooling. 8 | # tl: the Teal language compiler. 9 | # You also will need to install a Lua runtime on your system (required by Teal). 10 | 11 | # usage: just use the command "$ sh genbindings.sh" 12 | 13 | # utils 14 | bindgen_log() { 15 | echo "nene bindgen log: $1" 16 | } 17 | 18 | # clear previous build 19 | bindgen_log "clear previous build" 20 | 21 | rm -rf dumps/ 22 | mkdir dumps/ 23 | mkdir dumps/math 24 | mkdir dumps/audio 25 | 26 | # dump AST 27 | bindgen_log "dumping ASTs" 28 | dump_ast() { 29 | clang-check include/nene/$1.h -ast-dump -ast-dump-filter=$2 --extra-arg="-fno-color-diagnostics" > dumps/$3.txt 30 | } 31 | 32 | dump_ast "core" "nene" "core" 33 | dump_ast "texture" "nene_Texture" "texture" 34 | dump_ast "font" "nene" "font" 35 | dump_ast "texture_atlas" "nene_TextureAtlas" "texture_atlas" 36 | dump_ast "audio/music" "nene_Music" "audio/music" 37 | dump_ast "audio/sound" "nene_Sound" "audio/sound" 38 | dump_ast "math/vec2i" "nene_Vec2i" "math/vec2i" 39 | dump_ast "math/vec2" "nene_Vec2" "math/vec2" 40 | dump_ast "math/rect" "nene_Rect" "math/rect" 41 | dump_ast "math/rectf" "nene_Rectf" "math/rectf" 42 | dump_ast "math/grid" "nene_Grid" "math/grid" 43 | dump_ast "math/segment" "nene_Segment" "math/segment" 44 | dump_ast "math/shape" "nene_Shape" "math/shape" 45 | dump_ast "intersections" "nene_Intersection" "intersections" 46 | dump_ast "collision" "nene_Collision" "collision" 47 | dump_ast "animation" "nene_Animation" "animation" 48 | dump_ast "tilemap" "nene_Tilemap" "tilemap" 49 | dump_ast "color" "nene_Color" "color" 50 | 51 | # generate bindings 52 | bindgen_log "generating bindings" 53 | bindgen_log "generating nelua bindings" 54 | tl run bindgen/generate_nelua.tl 55 | 56 | bindgen_log "generating carp bindings" 57 | tl run bindgen/generate_carp.tl 58 | 59 | bindgen_log "done" 60 | -------------------------------------------------------------------------------- /include/nene/animation.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_ANIMATION_H 9 | #define NENE_ANIMATION_H 10 | 11 | #include 12 | #include 13 | 14 | /// Animation range type, used on Spritesheets 15 | typedef struct nene_Animation { 16 | /// animation speed 17 | uint16_t interval; 18 | /// starting frame of the animation (inclusive) 19 | uint16_t from; 20 | /// ending frame of the animation (inclusive) 21 | uint16_t to; 22 | /// animation properties 23 | bool loop; 24 | } nene_Animation; 25 | 26 | /// Returns a zero-initialized animation. 27 | nene_Animation nene_Animation_zero(void); 28 | 29 | /// Returns a copy of the animation. 30 | nene_Animation nene_Animation_copy(nene_Animation *animation); 31 | 32 | /// Tests if a and b animation ranges are equal. 33 | bool nene_Animation_equals(nene_Animation a, nene_Animation b); 34 | 35 | #endif // NENE_ANIMATION_H 36 | -------------------------------------------------------------------------------- /include/nene/audio/music.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_MUSIC_H 9 | #define NENE_MUSIC_H 10 | 11 | #include 12 | #include 13 | #include "SDL_mixer.h" 14 | 15 | typedef struct nene_Music { 16 | Mix_Music *raw; 17 | } nene_Music; 18 | 19 | typedef struct nene_MusicCreation { 20 | bool created; 21 | nene_Music music; 22 | } nene_MusicCreation; 23 | 24 | nene_Music nene_Music_zero(void); 25 | 26 | nene_Music nene_Music_copy(nene_Music *music); 27 | 28 | Mix_Music *nene_Music_get_raw(nene_Music music); 29 | 30 | nene_MusicCreation nene_Music_load(const char *filepath); 31 | 32 | bool nene_Music_play(nene_Music music, int16_t loops); 33 | 34 | void nene_Music_stop(void); 35 | 36 | float nene_Music_set_volume(float volume); 37 | 38 | void nene_Music_destroy(nene_Music *music); 39 | 40 | #endif // NENE_MUSIC_H 41 | 42 | -------------------------------------------------------------------------------- /include/nene/audio/sound.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_SOUND_H 9 | #define NENE_SOUND_H 10 | 11 | #include 12 | #include 13 | #include "SDL_mixer.h" 14 | 15 | typedef struct nene_Sound { 16 | Mix_Chunk *raw; 17 | int16_t channel; 18 | } nene_Sound; 19 | 20 | typedef struct nene_SoundCreation { 21 | bool created; 22 | nene_Sound sound; 23 | } nene_SoundCreation; 24 | 25 | nene_Sound nene_Sound_zero(void); 26 | 27 | nene_Sound nene_Sound_copy(nene_Sound *sound); 28 | 29 | Mix_Chunk *nene_Sound_get_raw(nene_Sound sound); 30 | 31 | nene_SoundCreation nene_Sound_load(const char *filepath); 32 | 33 | bool nene_Sound_play(nene_Sound *sound, int16_t loops); 34 | 35 | bool nene_Sound_halt(nene_Sound *sound); 36 | 37 | bool nene_Sound_halt_all(void); 38 | 39 | float nene_Sound_set_volume(nene_Sound sound, float volume); 40 | 41 | void nene_Sound_destroy(nene_Sound *sound); 42 | 43 | #endif // NENE_SOUND_H 44 | 45 | -------------------------------------------------------------------------------- /include/nene/collision.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_COLLISIONS_H 9 | #define NENE_COLLISIONS_H 10 | 11 | #include 12 | #include "nene/math/vec2.h" 13 | #include "nene/math/rectf.h" 14 | #include "nene/math/segment.h" 15 | 16 | /// Collision data structure. 17 | typedef struct nene_Collision { 18 | bool collided; 19 | nene_Vec2 delta; 20 | } nene_Collision; 21 | 22 | /// Returns a zero-initialialized collision 23 | nene_Collision nene_Collision_zero(void); 24 | 25 | /// Returns a copy of the collision. 26 | nene_Collision nene_Collision_copy(nene_Collision *collision); 27 | 28 | /// Returns a "no collision" value, it's an alias to the "zero" function. 29 | nene_Collision nene_Collision_no_collision(void); 30 | 31 | /// Returns the collision response between two rects. 32 | nene_Collision nene_Collision_rectf_with_rectf(nene_Rectf a, nene_Rectf b, nene_Vec2 delta_pos); 33 | 34 | /// Returns the collision response between a rectangle and a segment. 35 | nene_Collision nene_Collision_rectf_with_segment(nene_Rectf rect, nene_Segment segment, nene_Vec2 delta_pos); 36 | 37 | #endif // NENE_COLLISIONS_H 38 | -------------------------------------------------------------------------------- /include/nene/color.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_COLOR_H 9 | #define NENE_COLOR_H 10 | 11 | #include 12 | #include 13 | 14 | /// The color structure, it's a rgba color strucuture using one byte each 15 | typedef struct nene_Color { 16 | uint8_t r; 17 | uint8_t g; 18 | uint8_t b; 19 | uint8_t a; 20 | } nene_Color; 21 | 22 | /// Returns a zero-initialized color, that's a transparent black. 23 | nene_Color nene_Color_zero(void); 24 | 25 | /// Returns a copy of the color. 26 | nene_Color nene_Color_copy(nene_Color *color); 27 | 28 | /// The "black" color of nene's palette (`{ .r = 0x00, .g = 0x00, .b = 0x00, .a = 0xff }`) 29 | nene_Color nene_Color_black(void); 30 | /// The "white" color of nene's palette (`{ .r = 0xff, .g = 0xff, .b = 0xff, .a = 0xff }`) 31 | nene_Color nene_Color_white(void); 32 | /// The "red" color of nene's palette (`{ .r = 0xff, .g = 0x00, .b = 0x00, .a = 0xff }`) 33 | nene_Color nene_Color_red(void); 34 | /// The "green" color of nene's palette (`{ .r = 0x00, .g = 0xff, .b = 0x00, .a = 0xff }`) 35 | nene_Color nene_Color_green(void); 36 | /// The "blue" color of nene's palette (`{ .r = 0x00, .g = 0x00, .b = 0xff, .a = 0xff }`) 37 | nene_Color nene_Color_blue(void); 38 | /// The "yellow" color of nene's palette (`{ .r = 0xfc, .g = 0xea, .b = 0x20, .a = 0xff }`) 39 | nene_Color nene_Color_yellow(void); 40 | /// The "cyan" color of nene's palette (`{ .r = 0x00, .g = 0xff, .b = 0xff, .a = 0xff }`) 41 | nene_Color nene_Color_cyan(void); 42 | /// The "bg" color of nene's palette (`{ .r = 0x69, .g = 0x46, .b = 0x6b, .a = 0xff }`) 43 | nene_Color nene_Color_bg(void); 44 | 45 | bool nene_Color_equals(nene_Color a, nene_Color b); 46 | 47 | #endif // NENE_COLOR_H 48 | -------------------------------------------------------------------------------- /include/nene/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_CONFIG_H 9 | #define NENE_CONFIG_H 10 | 11 | #define NENE_CFG_GAMEPAD_COUNT 4 12 | #define NENE_CFG_GAMEPAD_DEADZONE 0.025f 13 | 14 | #endif // NENE_CONFIG_H 15 | 16 | -------------------------------------------------------------------------------- /include/nene/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_FONT_H 9 | #define NENE_FONT_H 10 | 11 | #include 12 | #include 13 | #include "SDL_ttf.h" 14 | #include "nene/math/vec2i.h" 15 | #include "nene/texture.h" 16 | #include "nene/color.h" 17 | 18 | /// The quality of the text rendering. 19 | typedef enum nene_TextQuality { 20 | /// Solid is faster, but not "smooth", ideal for fast rendering or pixel-art fonts. 21 | NENE_TEXTQUALITY_SOLID = 0, 22 | /// Blended is slower, but have high-quality, with antialiasing. 23 | NENE_TEXTQUALITY_BLENDED = 2, 24 | } nene_TextQuality; 25 | 26 | /// The metrics of the font 27 | typedef struct nene_GlyphMetrics { 28 | int16_t min_x; 29 | int16_t min_y; 30 | int16_t max_x; 31 | int16_t max_y; 32 | } nene_GlyphMetrics; 33 | 34 | /// The query result about a font. 35 | typedef struct nene_GlyphMetricsQuery { 36 | bool queried; 37 | nene_GlyphMetrics metrics; 38 | } nene_GlyphMetricsQuery; 39 | 40 | /// The dimensions result of a text. 41 | typedef struct nene_TextDimensions { 42 | bool calculated; 43 | nene_Vec2i dimensions; 44 | } nene_TextDimensions; 45 | 46 | /// The Font handle. 47 | /// You should not use the internal resource 48 | typedef struct nene_Font { 49 | TTF_Font *raw; 50 | } nene_Font; 51 | 52 | /// A font creation result. 53 | typedef struct nene_FontCreation { 54 | /// true if font creation succeded. 55 | bool created; 56 | /// the font handle created, empty on case of failure. 57 | nene_Font font; 58 | } nene_FontCreation; 59 | 60 | /// Returns a zero-initialized Glyph Metrics. 61 | nene_GlyphMetrics nene_GlyphMetrics_zero(void); 62 | 63 | /// Returns a copy of the glyph metrics 64 | nene_GlyphMetrics nene_GlyphMetrics_copy(nene_GlyphMetrics *metrics); 65 | 66 | /// Returns a zero-initialized Font handle. 67 | nene_Font nene_Font_zero(void); 68 | 69 | /// Returns a copy of the Font handle (it doesn't clones the Font resource). 70 | nene_Font nene_Font_copy(nene_Font *font); 71 | 72 | /// Returns the internal data of the Font handle. 73 | TTF_Font *nene_Font_get_raw(nene_Font font); 74 | 75 | /// Returns the font creation result, this creates a font from a font asset (like .ttf files). 76 | nene_FontCreation nene_Font_load(const char filepath[], int16_t pt_size); 77 | 78 | /// Renders a text using the given font, returns a texture creation result. 79 | nene_TextureCreation nene_Font_render(nene_Font font, const char text[], nene_TextQuality quality, nene_Color color, uint32_t wrap_length); 80 | 81 | /// Updates the text of the reference texture, (this mutates the reference). 82 | bool nene_Font_update_text(nene_Font font, nene_Texture *text_texture, const char text[], nene_TextQuality quality, nene_Color color, uint32_t wrap_length); 83 | 84 | /// Query the metrics of the given glyph of the given font. 85 | nene_GlyphMetricsQuery nene_Font_get_glyph_metrics(nene_Font font, uint32_t glyph); 86 | 87 | /// Get font's height. 88 | uint16_t nene_Font_get_height(nene_Font font); 89 | 90 | /// Get font's ascent. 91 | uint16_t nene_Font_get_ascent(nene_Font font); 92 | 93 | /// Get font's descent. 94 | uint16_t nene_Font_get_descent(nene_Font font); 95 | 96 | /// Get's the line skip height. 97 | uint16_t nene_Font_get_line_skip(nene_Font font); 98 | 99 | /// Calculates the dimensions of the given text using the given font. 100 | nene_TextDimensions nene_Font_get_text_dimensions(nene_Font font, const char text[]); 101 | 102 | /// Checks if the given font is mono spaced. 103 | bool nene_Font_is_monospaced(nene_Font font); 104 | 105 | /// Destroys the font resource. 106 | void nene_Font_destroy(nene_Font *font); 107 | 108 | #endif /// NENE_FONT_H 109 | -------------------------------------------------------------------------------- /include/nene/impl/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | // ImplUtils shouldn't be included for bindings! 9 | 10 | #ifndef NENE_IMPL_UTILS_H 11 | #define NENE_IMPL_UTILS_H 12 | 13 | #include "SDL.h" 14 | 15 | #include "nene/color.h" 16 | 17 | SDL_Color ImplUtils_color_to_sdlcolor(nene_Color); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /include/nene/intersections.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_INTERSECTIONS_H 9 | #define NENE_INTERSECTIONS_H 10 | 11 | #include 12 | #include "nene/math/vec2.h" 13 | #include "nene/math/rectf.h" 14 | #include "nene/math/segment.h" 15 | 16 | /// Rectangle with rectangle intersection. 17 | typedef struct nene_IntersectionRectfWithRectf { 18 | bool intersected; 19 | nene_Rectf intersection; 20 | } nene_IntersectionRectfWithRectf; 21 | 22 | /// Segment with rectangle intersection. 23 | typedef struct nene_IntersectionSegmentWithRectf { 24 | uint8_t count; 25 | nene_Segment intersection; 26 | /// The intersected "sides" of the rectangle, the first element 27 | /// it's the one that intersects `intersection.origin`, while the 28 | /// second element it's the one that intersects `intersection.ending`. 29 | nene_Segment intersected_rect_sides[2]; 30 | } nene_IntersectionSegmentWithRectf; 31 | 32 | /// Segment with segment intersection. 33 | typedef struct nene_IntersectionSegmentWithSegment { 34 | bool intersected; 35 | bool is_parallel; 36 | nene_Vec2 point; 37 | float a_intersecting_scalar; 38 | float b_intersecting_scalar; 39 | } nene_IntersectionSegmentWithSegment; 40 | 41 | /// Returns if a rect is intersecting with another rect. 42 | bool nene_Intersections_is_intersecting_rectf_with_rectf(nene_Rectf a, nene_Rectf b); 43 | 44 | /// Returns if a rectangle is intersecting with a point. 45 | bool nene_Intersections_is_intersecting_rectf_with_point(nene_Rectf rect, nene_Vec2 point); 46 | 47 | /// Returns a zero-initialized RectfWithRectf value. 48 | nene_IntersectionRectfWithRectf nene_IntersectionRectfWithRectf_zero(void); 49 | 50 | /// Returns a copy of a RectfWithRectf intersection. 51 | nene_IntersectionRectfWithRectf nene_IntersectionRectfWithRectf_copy(nene_IntersectionRectfWithRectf *intersection); 52 | 53 | /// Returns "no rect with rect intersection" value, it's an alias to the "zero" function. 54 | nene_IntersectionRectfWithRectf nene_IntersectionRectfWithRectf_no_intersection(void); 55 | 56 | /// Returns the intersection rectangle between two rectangles. 57 | nene_IntersectionRectfWithRectf nene_IntersectionRectfWithRectf_get_intersection(nene_Rectf a, nene_Rectf b); 58 | 59 | /// Returns a zero-initialized RectfWithRectf value. 60 | nene_IntersectionSegmentWithRectf nene_IntersectionSegmentWithRectf_zero(void); 61 | 62 | /// Returns a copy of a RectfWithRectf intersection. 63 | nene_IntersectionSegmentWithRectf nene_IntersectionSegmentWithRectf_copy(nene_IntersectionSegmentWithRectf *intersection); 64 | 65 | /// Returns "no segment with rect intersection" value, it's an alias to the "zero" function. 66 | nene_IntersectionSegmentWithRectf nene_IntersectionSegmentWithRectf_no_intersection(void); 67 | 68 | /// Returns the intersection data between a segment and a rectangle. 69 | nene_IntersectionSegmentWithRectf nene_IntersectionSegmentWithRectf_get_intersection(nene_Segment segment, nene_Rectf rect); 70 | 71 | /// Returns a zero-initialized IntersectionSegmentWithSegment value. 72 | nene_IntersectionSegmentWithSegment nene_IntersectionSegmentWithSegment_zero(void); 73 | 74 | /// Returns a copy of a IntersectionSegmentWithSegment intersection. 75 | nene_IntersectionSegmentWithSegment nene_IntersectionSegmentWithSegment_copy(nene_IntersectionSegmentWithSegment *intersection); 76 | 77 | /// Returns "no segment with segment intersection" value, it's an alias to the "zero" function. 78 | nene_IntersectionSegmentWithSegment nene_IntersectionSegmentWithSegment_no_intersection(void); 79 | 80 | /// Returns the intersection data between two segments. 81 | nene_IntersectionSegmentWithSegment nene_IntersectionSegmentWithSegment_get_intersection(nene_Segment a, nene_Segment b); 82 | 83 | 84 | #endif // NENE_INTERSECTIONS_H 85 | -------------------------------------------------------------------------------- /include/nene/math/grid.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_GRID_H 9 | #define NENE_GRID_H 10 | 11 | #include 12 | #include "nene/math/vec2i.h" 13 | #include "nene/math/rect.h" 14 | 15 | typedef struct nene_Grid { 16 | nene_Vec2i cell_size; 17 | nene_Vec2i gap; 18 | } nene_Grid; 19 | 20 | nene_Grid nene_Grid_zero(void); 21 | 22 | nene_Grid nene_Grid_copy(nene_Grid *grid); 23 | 24 | bool nene_Grid_equals(nene_Grid a, nene_Grid b); 25 | 26 | nene_Vec2i nene_Grid_get_nth_cell_coord(uint32_t nth, uint16_t grid_width); 27 | 28 | nene_Vec2i nene_Grid_get_cell_position(nene_Grid grid, nene_Vec2i cell_coord); 29 | 30 | nene_Vec2i nene_Grid_get_nth_cell_position(nene_Grid grid, uint32_t nth, uint16_t grid_width); 31 | 32 | nene_Rect nene_Grid_get_rect(nene_Grid grid, nene_Vec2i cell_coord); 33 | 34 | #endif // NENE_GRID_H 35 | -------------------------------------------------------------------------------- /include/nene/math/rect.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_RECT_H 9 | #define NENE_RECT_H 10 | 11 | #include 12 | #include "SDL.h" 13 | 14 | #include "nene/math/vec2i.h" 15 | 16 | typedef struct nene_Rect { 17 | nene_Vec2i pos; 18 | nene_Vec2i size; 19 | } nene_Rect; 20 | 21 | nene_Rect nene_Rect_zero(void); 22 | nene_Rect nene_Rect_copy(nene_Rect *rect); 23 | SDL_Rect nene_Rect_to_raw(nene_Rect rect); 24 | bool nene_Rect_equals(nene_Rect a, nene_Rect b); 25 | nene_Rect nene_Rect_add_pos(nene_Rect rect, nene_Vec2i pos); 26 | nene_Rect nene_Rect_add_size(nene_Rect rect, nene_Vec2i size); 27 | nene_Vec2i nene_Rect_get_center(nene_Rect rect); 28 | 29 | #endif // NENE_RECT_H 30 | -------------------------------------------------------------------------------- /include/nene/math/rectf.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_RECTF_H 9 | #define NENE_RECTF_H 10 | 11 | #include 12 | #include "SDL.h" 13 | 14 | #include "nene/math/vec2.h" 15 | #include "nene/math/rect.h" 16 | 17 | typedef struct nene_Rectf { 18 | nene_Vec2 pos; 19 | nene_Vec2 size; 20 | } nene_Rectf; 21 | 22 | nene_Rectf nene_Rectf_zero(void); 23 | nene_Rectf nene_Rectf_copy(nene_Rectf *rect); 24 | SDL_FRect nene_Rectf_to_raw(nene_Rectf rect); 25 | bool nene_Rectf_equals(nene_Rectf a, nene_Rectf b); 26 | nene_Rectf nene_Rectf_add_pos(nene_Rectf rect, nene_Vec2 pos); 27 | nene_Rectf nene_Rectf_add_size(nene_Rectf rect, nene_Vec2 size); 28 | nene_Vec2 nene_Rectf_get_center(nene_Rectf rect); 29 | nene_Rect nene_Rectf_to_rect(nene_Rectf rect); 30 | nene_Rectf nene_Rectf_from_rect(nene_Rect rect); 31 | 32 | #endif // NENE_RECTF_H 33 | -------------------------------------------------------------------------------- /include/nene/math/segment.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_SEGMENT_H 9 | #define NENE_SEGMENT_H 10 | 11 | #include "nene/math/vec2.h" 12 | 13 | typedef struct nene_Segment { 14 | nene_Vec2 origin; 15 | nene_Vec2 ending; 16 | } nene_Segment; 17 | 18 | /// returns a segment with zeroed values 19 | nene_Segment nene_Segment_zero(void); 20 | 21 | /// returns a segment copy from a reference 22 | nene_Segment nene_Segment_copy(nene_Segment *segment); 23 | 24 | /// tests the equality between two segments 25 | bool nene_Segment_equals(nene_Segment a, nene_Segment b); 26 | 27 | /// Returns the segment as 2D vector. 28 | nene_Vec2 nene_Segment_as_vec2(nene_Segment segment); 29 | 30 | /// Get the midpoint (or middle point) of the segment. 31 | nene_Vec2 nene_Segment_get_midpoint(nene_Segment segment); 32 | 33 | /// Get the squared length of the segment. 34 | float nene_Segment_len_sqr(nene_Segment segment); 35 | 36 | /// Get the length of the segment. 37 | float nene_Segment_len(nene_Segment segment); 38 | 39 | #endif // NENE_SEGMENT_H 40 | -------------------------------------------------------------------------------- /include/nene/math/shape.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_SHAPE_H 9 | #define NENE_SHAPE_H 10 | 11 | #include "nene/math/segment.h" 12 | #include "nene/math/rect.h" 13 | #include "nene/math/rectf.h" 14 | 15 | // FIXME: the module is "Shape", but it implements "ShapeQuadrilateral", this confuses binding generators. 16 | // TODO: separate as a separate module or refactor this module. 17 | 18 | /// Quadrilateral shape data strucuture, it holds four segments. 19 | typedef struct nene_ShapeQuadrilateral { 20 | nene_Segment sides[4]; 21 | } nene_ShapeQuadrilateral; 22 | 23 | /// Returns a zeroed quatrilateral shape 24 | nene_ShapeQuadrilateral nene_ShapeQuadrilateral_zero(void); 25 | 26 | /// Returns a copy of the quadrilateral shape reference 27 | nene_ShapeQuadrilateral nene_ShapeQuadrilateral_copy(nene_ShapeQuadrilateral *quadrilateral_shape); 28 | 29 | /// Returns a quadrilateral shape made from a rectanle with floating values. 30 | nene_ShapeQuadrilateral nene_Shape_get_rectf_shape(nene_Rectf rect); 31 | 32 | /// Returns a quadrilateral shape made from a rectanle. 33 | nene_ShapeQuadrilateral nene_Shape_get_rect_shape(nene_Rect rect); 34 | 35 | /// Returns the diagonal of a rectangle as a segment, 36 | /// with direction that's horizontally left-to-right, while the vertical direction it's given 37 | /// on the `up_to_down` boolean parameter. 38 | nene_Segment nene_Shape_get_rectf_diagonal(nene_Rectf rect, bool up_to_down); 39 | 40 | #endif // NENE_SHAPE_H 41 | -------------------------------------------------------------------------------- /include/nene/math/vec2.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_VEC2_H 9 | #define NENE_VEC2_H 10 | 11 | #include 12 | #include 13 | #include "nene/math/vec2i.h" 14 | 15 | /// 2D mathematical vector with `x` and `y` components. 16 | typedef struct nene_Vec2 { 17 | /// The `x` component. 18 | float x; 19 | /// The `y` component. 20 | float y; 21 | } nene_Vec2; 22 | 23 | /// converts a `nene_Vec2i` value to `nene_Vec2`. 24 | nene_Vec2 nene_Vec2_from_vec2i(nene_Vec2i v); 25 | 26 | /// converts a `nene_Vec2` value to `nene_Vec2i`. 27 | nene_Vec2i nene_Vec2_to_vec2i(nene_Vec2 v); 28 | 29 | /// returns a vector with `0` value on `x` and `y` components. 30 | nene_Vec2 nene_Vec2_zero(void); 31 | 32 | /// returns a vector with `1` value on `x` and `y` components. 33 | nene_Vec2 nene_Vec2_one(void); 34 | 35 | /// returns a copy of the vector reference. 36 | nene_Vec2 nene_Vec2_copy(nene_Vec2 *v); 37 | 38 | /// tests equality between `a` and `b` vectors. 39 | bool nene_Vec2_equals(nene_Vec2 a, nene_Vec2 b); 40 | 41 | /// returns the sum of two vectors. 42 | nene_Vec2 nene_Vec2_add(nene_Vec2 a, nene_Vec2 b); 43 | 44 | /// returns the difference between two vectors. 45 | nene_Vec2 nene_Vec2_sub(nene_Vec2 a, nene_Vec2 b); 46 | 47 | /// returns the product between two vectors. 48 | nene_Vec2 nene_Vec2_mul(nene_Vec2 a, nene_Vec2 b); 49 | 50 | /// returns a vector that's the vector `a` scaled by `s` scalar. 51 | nene_Vec2 nene_Vec2_scale(nene_Vec2 a, float s); 52 | 53 | /// returns the negation of the `v` vector. 54 | nene_Vec2 nene_Vec2_negate(nene_Vec2 v); 55 | 56 | /// returns a squared length of the `v` vector 57 | float nene_Vec2_len_sqr(nene_Vec2 v); 58 | 59 | /// returns the length of the `v` vector. 60 | float nene_Vec2_len(nene_Vec2 v); 61 | 62 | /// returns a vector that's the interpolation between `a` and `b` vectors by the scalar `t`. 63 | nene_Vec2 nene_Vec2_lerp(nene_Vec2 a, nene_Vec2 b, float t); 64 | 65 | /// returns the `v` vector normalized. 66 | nene_Vec2 nene_Vec2_normalize(nene_Vec2 v); 67 | 68 | /// returns the dot product of `a` and `b` vectors. 69 | float nene_Vec2_dot(nene_Vec2 a, nene_Vec2 b); 70 | 71 | /// returns the cross product of the `a` and `b` vector. 72 | /// > note: since this is a 2D vector (no `z` component), the cross product returns 73 | /// > the magnitude of the cross product vector instead of the vector itself. 74 | float nene_Vec2_cross(nene_Vec2 a, nene_Vec2 b); 75 | 76 | /// returns the perpendicular vector of the vector v, with the same magnitude (length) 77 | /// > note: The "rotation" it's counterclockwise. 78 | nene_Vec2 nene_Vec2_perpendicular(nene_Vec2 v); 79 | 80 | #endif // NENE_VEC2_H 81 | -------------------------------------------------------------------------------- /include/nene/math/vec2i.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_VEC2I_H 9 | #define NENE_VEC2I_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | /// 2D mathematical vector with `x` and `y` components (using 32-bit integer type, a.k.a. `int32_t`). 16 | typedef struct nene_Vec2i { 17 | /// The `x` component. 18 | int32_t x; 19 | /// The `y` component. 20 | int32_t y; 21 | } nene_Vec2i; 22 | 23 | /// Returns a vector with `0` value on `x` and `y` components. 24 | nene_Vec2i nene_Vec2i_zero(void); 25 | 26 | /// Returns a vector with `1` value on `x` and `y` components. 27 | nene_Vec2i nene_Vec2i_one(void); 28 | 29 | /// Returns a copy of the vector reference. 30 | nene_Vec2i nene_Vec2i_copy(nene_Vec2i *v); 31 | 32 | /// Tests equality between `a` and `b` vectors. 33 | bool nene_Vec2i_equals(nene_Vec2i a, nene_Vec2i b); 34 | 35 | /// Returns the sum of two vectors. 36 | nene_Vec2i nene_Vec2i_add(nene_Vec2i a, nene_Vec2i b); 37 | 38 | /// Returns the difference between two vectors. 39 | nene_Vec2i nene_Vec2i_sub(nene_Vec2i a, nene_Vec2i b); 40 | 41 | /// Returns the product between two vectors. 42 | nene_Vec2i nene_Vec2i_mul(nene_Vec2i a, nene_Vec2i b); 43 | 44 | /// Returns a vector that's the vector `a` scaled by `s` scalar. 45 | nene_Vec2i nene_Vec2i_scale(nene_Vec2i v, float s); 46 | 47 | /// Returns the negation of the `v` vector. 48 | nene_Vec2i nene_Vec2i_negate(nene_Vec2i v); 49 | 50 | /// Returns a squared length of the `v` vector 51 | float nene_Vec2i_len_sqr(nene_Vec2i v); 52 | 53 | /// Returns the length of the `v` vector. 54 | float nene_Vec2i_len(nene_Vec2i v); 55 | 56 | /// Returns a vector that's the interpolation between `a` and `b` vectors by the scalar `t`. 57 | nene_Vec2i nene_Vec2i_lerp(nene_Vec2i a, nene_Vec2i b, float t); 58 | 59 | /// Returns the dot product of `a` and `b` vectors. 60 | float nene_Vec2i_dot(nene_Vec2i a, nene_Vec2i b); 61 | 62 | /// Note: since this is a 2D vector (or z=0), the cross product returns 63 | /// The magnitude of the cross product vector instead of the vector itself. 64 | float nene_Vec2i_cross(nene_Vec2i a, nene_Vec2i b); 65 | 66 | /// Returns the perpendicular vector of the vector v, with the same magnitude (length) 67 | /// > note: The "rotation" it's counterclockwise. 68 | nene_Vec2i nene_Vec2i_perpendicular(nene_Vec2i v); 69 | 70 | #endif // NENE_VEC2I_H 71 | -------------------------------------------------------------------------------- /include/nene/nene.h: -------------------------------------------------------------------------------- 1 | #ifndef NENE_H 2 | #define NENE_H 3 | 4 | #include "nene/animation.h" 5 | #include "nene/math/vec2.h" 6 | 7 | #endif // NENE_H 8 | -------------------------------------------------------------------------------- /include/nene/texture.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_TEXTURE_H 9 | #define NENE_TEXTURE_H 10 | 11 | #include 12 | #include 13 | #include "SDL.h" 14 | 15 | #include "nene/math/vec2.h" 16 | #include "nene/math/rect.h" 17 | #include "nene/color.h" 18 | 19 | typedef struct nene_Texture { 20 | SDL_Texture *raw; 21 | uint16_t width; 22 | uint16_t height; 23 | SDL_TextureAccess access; 24 | } nene_Texture; 25 | 26 | typedef struct nene_TextureCreation { 27 | bool created; 28 | nene_Texture texture; 29 | } nene_TextureCreation; 30 | 31 | nene_Texture nene_Texture_zero(void); 32 | nene_Texture nene_Texture_copy(nene_Texture *texture); 33 | void nene_Texture_destroy(nene_Texture *texture); 34 | SDL_Texture *nene_Texture_get_raw(nene_Texture texture); 35 | bool nene_Texture_apply_raw(nene_Texture *texture, SDL_Texture *raw_texture); 36 | bool nene_Texture_set_blend_mode(nene_Texture texture, SDL_BlendMode blend_mode); 37 | bool nene_Texture_set_color_mod(nene_Texture texture, nene_Color color); 38 | nene_TextureCreation nene_Texture_create_with_access(uint16_t width, uint16_t height, SDL_TextureAccess access); 39 | nene_TextureCreation nene_Texture_create(uint16_t width, uint16_t height); 40 | nene_TextureCreation nene_Texture_load(const char *filepath); 41 | nene_Rect nene_Texture_get_full_source_rect(nene_Texture texture); 42 | bool nene_Texture_draw_to_point(nene_Texture texture, nene_Rect source, nene_Vec2 point, bool is_world_position); 43 | bool nene_Texture_draw_to_rect(nene_Texture texture, nene_Rect source, nene_Rect destination); 44 | bool nene_Texture_draw_to_point_ex(nene_Texture texture, nene_Rect source, nene_Vec2 point, bool is_world_position, double angle, nene_Vec2 rotation_center, bool flip_x, bool flip_y); 45 | bool nene_Texture_draw_to_rect_ex(nene_Texture texture, nene_Rect source, nene_Rect destination, double angle, nene_Vec2 rotation_center, bool flip_x, bool flip_y); 46 | 47 | #endif // NENE_TEXTURE_H 48 | -------------------------------------------------------------------------------- /include/nene/texture_atlas.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_TEXTURE_ATLAS_H 9 | #define NENE_TEXTURE_ATLAS_H 10 | 11 | #include 12 | #include 13 | 14 | #include "nene/math/grid.h" 15 | #include "nene/texture.h" 16 | #include "nene/math/vec2i.h" 17 | #include "nene/math/vec2.h" 18 | 19 | typedef struct nene_TextureAtlas { 20 | uint16_t width; 21 | nene_Grid grid; 22 | nene_Texture texture; 23 | } nene_TextureAtlas; 24 | 25 | typedef struct nene_TextureAtlasCreation { 26 | bool created; 27 | nene_TextureAtlas texture_atlas; 28 | } nene_TextureAtlasCreation; 29 | 30 | nene_TextureAtlas nene_TextureAtlas_zero(void); 31 | 32 | nene_TextureAtlas nene_TextureAtlas_copy(nene_TextureAtlas *texture_atlas); 33 | 34 | void nene_TextureAtlas_destroy(nene_TextureAtlas *texture_atlas); 35 | 36 | nene_TextureAtlasCreation nene_TextureAtlas_load(const char *filepath, uint16_t width, nene_Grid grid); 37 | 38 | bool nene_TextureAtlas_draw_sub_texture(nene_TextureAtlas texture_atlas, nene_Vec2i subtexture_coord, nene_Vec2 position, bool is_world_pos); 39 | 40 | bool nene_TextureAtlas_draw_sub_texture_ex(nene_TextureAtlas texture_atlas, nene_Vec2i subtexture_coord, nene_Vec2 position, bool is_world_pos, double angle, nene_Vec2 rotation_center, bool flip_x, bool flip_y); 41 | 42 | bool nene_TextureAtlas_draw_nth_sub_texture(nene_TextureAtlas texture_atlas, uint32_t nth, nene_Vec2 position, bool is_world_pos); 43 | 44 | bool nene_TextureAtlas_draw_nth_sub_texture_ex(nene_TextureAtlas texture_atlas, uint32_t nth, nene_Vec2 position, bool is_world_pos, double angle, nene_Vec2 rotation_center, bool flip_x, bool flip_y); 45 | 46 | nene_Vec2 nene_TextureAtlas_get_sub_texture_center(nene_TextureAtlas texture_atlas); 47 | 48 | #endif // NENE_TEXTURE_ATLAS_H 49 | -------------------------------------------------------------------------------- /include/nene/tilemap.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #ifndef NENE_TILEMAP_H 9 | #define NENE_TILEMAP_H 10 | 11 | #include 12 | #include 13 | #include "nene/texture.h" 14 | #include "nene/texture_atlas.h" 15 | #include "nene/math/grid.h" 16 | #include "nene/math/vec2i.h" 17 | 18 | typedef struct nene_Tilemap { 19 | nene_TextureAtlas tileset; 20 | uint16_t width; 21 | nene_Grid grid; 22 | uint32_t tile_count; 23 | } nene_Tilemap; 24 | 25 | nene_Tilemap nene_Tilemap_zero(void); 26 | 27 | nene_Tilemap nene_Tilemap_copy(nene_Tilemap *tilemap); 28 | 29 | nene_Vec2i nene_Tilemap_get_size_in_tiles(nene_Tilemap tilemap); 30 | 31 | nene_Vec2i nene_Tilemap_get_size(nene_Tilemap tilemap); 32 | 33 | bool nene_Tilemap_draw(nene_Tilemap tilemap, nene_Vec2 position, bool is_world_pos, uint16_t tiles[], uint32_t count); 34 | 35 | void nene_Tilemap_destroy(nene_Tilemap *tilemap); 36 | 37 | #endif // NENE_TILEMAP_H 38 | -------------------------------------------------------------------------------- /resources/8bit Bossa.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andre-LA/nene/daa9edf2d4c25c3480345175002d7bcb698b587d/resources/8bit Bossa.ogg -------------------------------------------------------------------------------- /resources/img_shapes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andre-LA/nene/daa9edf2d4c25c3480345175002d7bcb698b587d/resources/img_shapes.png -------------------------------------------------------------------------------- /resources/monogram_extended.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andre-LA/nene/daa9edf2d4c25c3480345175002d7bcb698b587d/resources/monogram_extended.ttf -------------------------------------------------------------------------------- /resources/pixelclick.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andre-LA/nene/daa9edf2d4c25c3480345175002d7bcb698b587d/resources/pixelclick.wav -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021-present André Luiz Alvares 2 | # Nene is licensed under the Zlib license. 3 | # Please refer to the LICENSE file for details 4 | # SPDX-License-Identifier: Zlib 5 | 6 | # variables 7 | set(NENE_PROJECT_DIR "${PROJECT_SOURCE_DIR}") 8 | set(NENE_INCLUDE_DIR "${NENE_PROJECT_DIR}/include") 9 | 10 | add_library(nene STATIC) 11 | add_library(nene::nene ALIAS nene) 12 | 13 | # set the C standard, be sure to follow what's written on compile_flags.txt file. 14 | # NOTE: if you change this, be sure to update the same setting examples/c/CMakeLists.txt 15 | set_target_properties(nene PROPERTIES 16 | C_STANDARD 99 17 | C_STANDARD_REQUIRED YES 18 | C_EXTENSIONS NO 19 | ) 20 | 21 | # add the nene include directory 22 | target_include_directories(nene PUBLIC "${PROJECT_SOURCE_DIR}/include") 23 | 24 | # add the sdl include directories (TODO: remove these once the (game) usage of SDL get's unnecessary) 25 | target_include_directories(nene PUBLIC "${PROJECT_SOURCE_DIR}/external/SDL/include") 26 | target_include_directories(nene PUBLIC "${PROJECT_SOURCE_DIR}/external/SDL_mixer/include") 27 | target_include_directories(nene PUBLIC "${PROJECT_SOURCE_DIR}/external/SDL_image") 28 | target_include_directories(nene PUBLIC "${PROJECT_SOURCE_DIR}/external/SDL_ttf") 29 | 30 | # set the sources of the nene target 31 | set(NENE_SOURCE_FILES 32 | impl/utils.c 33 | core.c 34 | texture.c 35 | font.c 36 | texture_atlas.c 37 | audio/music.c 38 | audio/sound.c 39 | math/vec2i.c 40 | math/vec2.c 41 | math/rect.c 42 | math/rectf.c 43 | math/grid.c 44 | math/segment.c 45 | math/shape.c 46 | intersections.c 47 | collision.c 48 | animation.c 49 | tilemap.c 50 | color.c 51 | ) 52 | 53 | # set the headers of the nene target 54 | set(NENE_HEADER_FILES 55 | "${NENE_INCLUDE_DIR}/nene/impl/utils.h" 56 | "${NENE_INCLUDE_DIR}/nene/core.h" 57 | "${NENE_INCLUDE_DIR}/nene/config.h" 58 | "${NENE_INCLUDE_DIR}/nene/texture.h" 59 | "${NENE_INCLUDE_DIR}/nene/font.h" 60 | "${NENE_INCLUDE_DIR}/nene/texture_atlas.h" 61 | "${NENE_INCLUDE_DIR}/nene/audio/music.h" 62 | "${NENE_INCLUDE_DIR}/nene/audio/sound.h" 63 | "${NENE_INCLUDE_DIR}/nene/math/vec2i.h" 64 | "${NENE_INCLUDE_DIR}/nene/math/vec2.h" 65 | "${NENE_INCLUDE_DIR}/nene/math/rect.h" 66 | "${NENE_INCLUDE_DIR}/nene/math/rectf.h" 67 | "${NENE_INCLUDE_DIR}/nene/math/grid.h" 68 | "${NENE_INCLUDE_DIR}/nene/math/segment.h" 69 | "${NENE_INCLUDE_DIR}/nene/math/shape.h" 70 | "${NENE_INCLUDE_DIR}/nene/intersections.h" 71 | "${NENE_INCLUDE_DIR}/nene/collision.h" 72 | "${NENE_INCLUDE_DIR}/nene/animation.h" 73 | "${NENE_INCLUDE_DIR}/nene/tilemap.h" 74 | "${NENE_INCLUDE_DIR}/nene/color.h" 75 | ) 76 | 77 | # set the sources and the headers as the nene target sources 78 | target_sources(nene PUBLIC ${NENE_SOURCE_FILES}) 79 | 80 | # header files added, that's also necessary to install the header files 81 | # for development 82 | target_sources(nene PUBLIC FILE_SET HEADERS 83 | BASE_DIRS "${NENE_INCLUDE_DIR}" 84 | FILES ${NENE_HEADER_FILES} 85 | ) 86 | 87 | # add linking to the external dependencies (SDL libraries) to the target 88 | target_link_libraries(nene PUBLIC external_libs) 89 | 90 | # add linking to libm if necessary 91 | # context: https://cliutils.gitlab.io/modern-cmake/chapters/features/small.html#little-libraries 92 | find_library(LIB_MATH m) 93 | if (LIB_MATH) 94 | target_link_libraries(nene PUBLIC ${LIB_MATH}) 95 | endif() 96 | -------------------------------------------------------------------------------- /src/animation.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/animation.h" 9 | #include 10 | 11 | nene_Animation nene_Animation_zero(void) { 12 | return (nene_Animation){ 13 | .loop = false 14 | }; 15 | } 16 | 17 | nene_Animation nene_Animation_copy(nene_Animation *animation) { 18 | if (animation == NULL) { 19 | return nene_Animation_zero(); 20 | } 21 | else { 22 | return *animation; 23 | } 24 | } 25 | 26 | bool nene_Animation_equals(nene_Animation a, nene_Animation b) { 27 | return a.from == b.from && a.to == b.to && a.loop == b.loop && 28 | a.interval == b.interval; 29 | } 30 | -------------------------------------------------------------------------------- /src/audio/music.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/audio/music.h" 9 | #include "SDL.h" 10 | 11 | nene_Music nene_Music_zero(void) { 12 | return (nene_Music){ 13 | .raw = NULL 14 | }; 15 | } 16 | 17 | nene_Music nene_Music_copy(nene_Music *music) { 18 | if (music == NULL) { 19 | return nene_Music_zero(); 20 | } 21 | else { 22 | return *music; 23 | } 24 | } 25 | 26 | Mix_Music *nene_Music_get_raw(nene_Music music) { 27 | SDL_assert(music.raw != NULL); 28 | return music.raw; 29 | } 30 | 31 | nene_MusicCreation nene_Music_load(const char *filepath) { 32 | SDL_assert(filepath != NULL); 33 | 34 | if (filepath == NULL) { 35 | return (nene_MusicCreation){ 36 | .created = false, 37 | }; 38 | } 39 | 40 | Mix_Music *raw_music = Mix_LoadMUS(filepath); 41 | 42 | if (raw_music == NULL) { 43 | return (nene_MusicCreation){ 44 | .created = false, 45 | }; 46 | } 47 | 48 | return (nene_MusicCreation){ 49 | .created = true, 50 | .music = (nene_Music){ 51 | .raw = raw_music, 52 | } 53 | }; 54 | } 55 | 56 | bool nene_Music_play(nene_Music music, int16_t loops) { 57 | SDL_assert(music.raw != NULL); 58 | 59 | if (music.raw == NULL) { 60 | return false; 61 | } 62 | 63 | if (Mix_PlayMusic(nene_Music_get_raw(music), loops) != 0) { 64 | return false; 65 | } 66 | 67 | return true; 68 | } 69 | 70 | void nene_Music_stop(void) { 71 | Mix_HaltMusic(); 72 | } 73 | 74 | float nene_Music_set_volume(float volume) { 75 | SDL_assert(volume >= 0.0f && volume <= 1.0f); 76 | int scaled_vol = (int)(volume * MIX_MAX_VOLUME); 77 | 78 | // clamp(scaled_vol, 0, MIX_MAX_VOLUME) 79 | scaled_vol = scaled_vol < 0 ? 0 : scaled_vol; 80 | scaled_vol = scaled_vol > MIX_MAX_VOLUME ? MIX_MAX_VOLUME : scaled_vol; 81 | 82 | int16_t prev_volume = Mix_VolumeMusic(scaled_vol); 83 | 84 | return (float)prev_volume / MIX_MAX_VOLUME; 85 | } 86 | 87 | void nene_Music_destroy(nene_Music *music) { 88 | SDL_assert_release(music != NULL); 89 | 90 | if (music == NULL || music->raw == NULL) { 91 | return; 92 | } 93 | 94 | Mix_FreeMusic(nene_Music_get_raw(*music)); 95 | 96 | *music = (nene_Music){ .raw = NULL }; 97 | } 98 | -------------------------------------------------------------------------------- /src/audio/sound.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/audio/sound.h" 9 | #include "SDL.h" 10 | 11 | nene_Sound nene_Sound_zero(void) { 12 | return (nene_Sound){ 13 | .raw = NULL, 14 | }; 15 | } 16 | 17 | nene_Sound nene_Sound_copy(nene_Sound *sound) { 18 | if (sound == NULL) { 19 | return nene_Sound_zero(); 20 | } 21 | else { 22 | return *sound; 23 | } 24 | } 25 | 26 | Mix_Chunk *nene_Sound_get_raw(nene_Sound sound) { 27 | SDL_assert(sound.raw != NULL); 28 | return sound.raw; 29 | } 30 | 31 | nene_SoundCreation nene_Sound_load(const char *filepath) { 32 | SDL_assert(filepath != NULL); 33 | 34 | if (filepath == NULL) { 35 | return (nene_SoundCreation){ 36 | .created = false, 37 | }; 38 | } 39 | 40 | Mix_Chunk *raw_sound = Mix_LoadWAV(filepath); 41 | 42 | if (raw_sound == NULL) { 43 | return (nene_SoundCreation){ 44 | .created = false, 45 | }; 46 | } 47 | 48 | return (nene_SoundCreation){ 49 | .created = true, 50 | .sound = (nene_Sound){ 51 | .raw = raw_sound, 52 | .channel = -1, 53 | }, 54 | }; 55 | } 56 | 57 | bool nene_Sound_play(nene_Sound *sound, int16_t loops) { 58 | SDL_assert(sound != NULL && sound->raw != NULL); 59 | 60 | if (sound == NULL || sound->raw == NULL) { 61 | return false; 62 | } 63 | 64 | sound->channel = Mix_PlayChannel(-1, nene_Sound_get_raw(*sound), loops); 65 | 66 | if (sound->channel == -1) { 67 | return false; 68 | } 69 | 70 | return true; 71 | } 72 | 73 | bool nene_Sound_halt(nene_Sound *sound) { 74 | SDL_assert(sound != NULL); 75 | 76 | if (sound == NULL) { 77 | return false; 78 | } 79 | 80 | if (sound->channel >= 0) { 81 | if (Mix_HaltChannel(sound->channel) != 0) { 82 | return false; 83 | } 84 | } 85 | 86 | return true; 87 | } 88 | 89 | bool nene_Sound_halt_all(void) { 90 | return Mix_HaltChannel(-1) != 0; 91 | } 92 | 93 | float nene_Sound_set_volume(nene_Sound sound, float volume) { 94 | SDL_assert(volume >= 0.0f && volume <= 1.0f); 95 | int scaled_vol = (int)(volume * MIX_MAX_VOLUME); 96 | 97 | // clamp(scaled_vol, 0, MIX_MAX_VOLUME) 98 | scaled_vol = scaled_vol < 0 ? 0 : scaled_vol; 99 | scaled_vol = scaled_vol > MIX_MAX_VOLUME ? MIX_MAX_VOLUME : scaled_vol; 100 | 101 | int16_t prev_volume = Mix_VolumeChunk(nene_Sound_get_raw(sound), scaled_vol); 102 | 103 | return (float)prev_volume / MIX_MAX_VOLUME; 104 | } 105 | 106 | void nene_Sound_destroy(nene_Sound *sound) { 107 | SDL_assert_release(sound != NULL); 108 | 109 | if (sound == NULL || sound->raw == NULL) { 110 | return; 111 | } 112 | 113 | Mix_FreeChunk(nene_Sound_get_raw(*sound)); 114 | 115 | *sound = (nene_Sound){ .raw = NULL }; 116 | } 117 | -------------------------------------------------------------------------------- /src/color.c: -------------------------------------------------------------------------------- 1 | #include "nene/color.h" 2 | #include 3 | 4 | nene_Color nene_Color_zero(void) { 5 | return (nene_Color) { 6 | .r = 0x00 7 | }; 8 | } 9 | 10 | nene_Color nene_Color_copy(nene_Color *color) { 11 | if (color == NULL) { 12 | return nene_Color_zero(); 13 | } 14 | else { 15 | return *color; 16 | } 17 | } 18 | 19 | nene_Color nene_Color_black(void) { 20 | return (nene_Color){ .r = 0x00, .g = 0x00, .b = 0x00, .a = 0xff }; 21 | } 22 | 23 | nene_Color nene_Color_white(void) { 24 | return (nene_Color){ .r = 0xff, .g = 0xff, .b = 0xff, .a = 0xff }; 25 | } 26 | 27 | nene_Color nene_Color_red(void) { 28 | return (nene_Color){ .r = 0xff, .g = 0x00, .b = 0x00, .a = 0xff }; 29 | } 30 | 31 | nene_Color nene_Color_green(void) { 32 | return (nene_Color){ .r = 0x00, .g = 0xff, .b = 0x00, .a = 0xff }; 33 | } 34 | 35 | nene_Color nene_Color_blue(void) { 36 | return (nene_Color){ .r = 0x00, .g = 0x00, .b = 0xff, .a = 0xff }; 37 | } 38 | 39 | nene_Color nene_Color_yellow(void) { 40 | return (nene_Color){ .r = 0xfc, .g = 0xea, .b = 0x20, .a = 0xff }; 41 | } 42 | 43 | nene_Color nene_Color_cyan(void) { 44 | return (nene_Color){ .r = 0x00, .g = 0xff, .b = 0xff, .a = 0xff }; 45 | } 46 | 47 | nene_Color nene_Color_bg(void) { 48 | return (nene_Color){ .r = 0x69, .g = 0x46, .b = 0x6b, .a = 0xff }; 49 | } 50 | 51 | bool nene_Color_equals(nene_Color a, nene_Color b) { 52 | return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a; 53 | } 54 | -------------------------------------------------------------------------------- /src/impl/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | // ImplUtils shouldn't be included for bindings! 9 | 10 | #include "nene/impl/utils.h" 11 | 12 | SDL_Color ImplUtils_color_to_sdlcolor(nene_Color color) { 13 | return (SDL_Color){ 14 | .r = color.r, 15 | .g = color.g, 16 | .b = color.b, 17 | .a = color.a, 18 | }; 19 | } 20 | -------------------------------------------------------------------------------- /src/math/grid.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/math/grid.h" 9 | #include "nene/math/vec2i.h" 10 | 11 | nene_Grid nene_Grid_zero(void) { 12 | return (nene_Grid){ 13 | .cell_size = nene_Vec2i_zero(), 14 | }; 15 | } 16 | 17 | nene_Grid nene_Grid_copy(nene_Grid *grid) { 18 | if (grid == NULL) { 19 | return nene_Grid_zero(); 20 | } 21 | else { 22 | return *grid; 23 | } 24 | } 25 | 26 | bool nene_Grid_equals(nene_Grid a, nene_Grid b) { 27 | return nene_Vec2i_equals(a.cell_size, b.cell_size) && nene_Vec2i_equals(a.gap, b.gap); 28 | } 29 | 30 | nene_Vec2i nene_Grid_get_nth_cell_coord(uint32_t nth, uint16_t grid_width) { 31 | return (nene_Vec2i){ 32 | .x = nth % grid_width, 33 | .y = nth / grid_width, 34 | }; 35 | } 36 | 37 | nene_Vec2i nene_Grid_get_cell_position(nene_Grid grid, nene_Vec2i cell_coord) { 38 | return nene_Vec2i_add( 39 | nene_Vec2i_mul(grid.cell_size, cell_coord), 40 | nene_Vec2i_mul(grid.gap, cell_coord) 41 | ); 42 | } 43 | 44 | nene_Vec2i nene_Grid_get_nth_cell_position(nene_Grid grid, uint32_t nth, uint16_t grid_width) { 45 | return nene_Grid_get_cell_position(grid, nene_Grid_get_nth_cell_coord(nth, grid_width)); 46 | } 47 | 48 | nene_Rect nene_Grid_get_rect(nene_Grid grid, nene_Vec2i cell_coord) { 49 | return (nene_Rect){ 50 | .pos = nene_Grid_get_cell_position(grid, cell_coord), 51 | .size = grid.cell_size, 52 | }; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /src/math/rect.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/math/rect.h" 9 | 10 | /* 11 | TODO: This code contains some code expressions to get a specific side of the 12 | rectangle from an axis, like `a.pos.x + a.pos.w`; 13 | it would be convenient to separate this as functions 14 | */ 15 | 16 | nene_Rect nene_Rect_zero(void) { 17 | return (nene_Rect){ 18 | .pos = nene_Vec2i_zero(), 19 | .size = nene_Vec2i_zero(), 20 | }; 21 | } 22 | 23 | nene_Rect nene_Rect_copy(nene_Rect *rect) { 24 | if (rect == NULL) { 25 | return nene_Rect_zero(); 26 | } 27 | else { 28 | return *rect; 29 | } 30 | } 31 | 32 | SDL_Rect nene_Rect_to_raw(nene_Rect rect) { 33 | return (SDL_Rect){ 34 | .x = rect.pos.x, 35 | .y = rect.pos.y, 36 | .w = rect.size.x, 37 | .h = rect.size.y, 38 | }; 39 | } 40 | 41 | bool nene_Rect_equals(nene_Rect a, nene_Rect b) { 42 | return nene_Vec2i_equals(a.pos, b.pos) && nene_Vec2i_equals(a.size, b.size); 43 | } 44 | 45 | nene_Rect nene_Rect_add_pos(nene_Rect rect, nene_Vec2i pos) { 46 | rect.pos = nene_Vec2i_add(rect.pos, pos); 47 | return rect; 48 | } 49 | 50 | nene_Rect nene_Rect_add_size(nene_Rect rect, nene_Vec2i size) { 51 | rect.size = nene_Vec2i_add(rect.size, size); 52 | return rect; 53 | } 54 | 55 | nene_Vec2i nene_Rect_get_center(nene_Rect rect) { 56 | rect.size.y = -rect.size.y; // math it's done at "world space" 57 | return nene_Vec2i_add(rect.pos, nene_Vec2i_scale(rect.size, 0.5f)); 58 | } 59 | -------------------------------------------------------------------------------- /src/math/rectf.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/math/rectf.h" 9 | 10 | /* 11 | TODO: This code contains some code expressions to get a specific side of the 12 | rectangle from an axis, like `a.pos.x + a.pos.w`; 13 | it would be convenient to separate this as functions 14 | */ 15 | 16 | nene_Rectf nene_Rectf_zero(void) { 17 | return (nene_Rectf){ 18 | .pos = nene_Vec2_zero(), 19 | .size = nene_Vec2_zero(), 20 | }; 21 | } 22 | 23 | nene_Rectf nene_Rectf_copy(nene_Rectf *rect) { 24 | if (rect == NULL) { 25 | return nene_Rectf_zero(); 26 | } 27 | else { 28 | return *rect; 29 | } 30 | } 31 | 32 | SDL_FRect nene_Rectf_to_raw(nene_Rectf rect) { 33 | return (SDL_FRect){ 34 | .x = rect.pos.x, 35 | .y = rect.pos.y, 36 | .w = rect.size.x, 37 | .h = rect.size.y, 38 | }; 39 | } 40 | 41 | bool nene_Rectf_equals(nene_Rectf a, nene_Rectf b) { 42 | return nene_Vec2_equals(a.pos, b.pos) && nene_Vec2_equals(a.size, b.size); 43 | } 44 | 45 | nene_Rectf nene_Rectf_add_pos(nene_Rectf rect, nene_Vec2 pos) { 46 | rect.pos = nene_Vec2_add(rect.pos, pos); 47 | return rect; 48 | } 49 | 50 | nene_Rectf nene_Rectf_add_size(nene_Rectf rect, nene_Vec2 size) { 51 | rect.size = nene_Vec2_add(rect.size, size); 52 | return rect; 53 | } 54 | 55 | nene_Vec2 nene_Rectf_get_center(nene_Rectf rect) { 56 | rect.size.y = -rect.size.y; // math it's done at "world space" 57 | return nene_Vec2_add(rect.pos, nene_Vec2_scale(rect.size, 0.5f)); 58 | } 59 | 60 | nene_Rect nene_Rectf_to_rect(nene_Rectf rect) { 61 | return (nene_Rect){ 62 | .pos = nene_Vec2_to_vec2i(rect.pos), 63 | .size = nene_Vec2_to_vec2i(rect.size), 64 | }; 65 | } 66 | 67 | nene_Rectf nene_Rectf_from_rect(nene_Rect rect) { 68 | return (nene_Rectf){ 69 | .pos = nene_Vec2_from_vec2i(rect.pos), 70 | .size = nene_Vec2_from_vec2i(rect.size), 71 | }; 72 | } 73 | -------------------------------------------------------------------------------- /src/math/segment.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/math/segment.h" 9 | 10 | nene_Segment nene_Segment_zero(void) { 11 | return (nene_Segment){ 12 | .origin = nene_Vec2_zero(), 13 | }; 14 | } 15 | 16 | nene_Segment nene_Segment_copy(nene_Segment *segment) { 17 | if (segment == NULL) { 18 | return nene_Segment_zero(); 19 | } 20 | else { 21 | return *segment; 22 | } 23 | } 24 | 25 | bool nene_Segment_equals(nene_Segment a, nene_Segment b) { 26 | return nene_Vec2_equals(a.origin, b.origin) 27 | && nene_Vec2_equals(a.ending, b.ending); 28 | } 29 | 30 | nene_Vec2 nene_Segment_as_vec2(nene_Segment segment) { 31 | return nene_Vec2_sub(segment.ending, segment.origin); 32 | } 33 | 34 | nene_Vec2 nene_Segment_get_midpoint(nene_Segment segment) { 35 | return nene_Vec2_lerp(segment.origin, segment.ending, 0.5f); 36 | } 37 | 38 | float nene_Segment_len_sqr(nene_Segment segment) { 39 | return nene_Vec2_len_sqr(nene_Segment_as_vec2(segment)); 40 | } 41 | 42 | float nene_Segment_len(nene_Segment segment) { 43 | return nene_Vec2_len(nene_Segment_as_vec2(segment)); 44 | } 45 | -------------------------------------------------------------------------------- /src/math/shape.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/math/shape.h" 9 | #include "nene/math/segment.h" 10 | 11 | nene_ShapeQuadrilateral nene_ShapeQuadrilateral_zero(void) { 12 | return (nene_ShapeQuadrilateral) { 13 | nene_Segment_zero(), 14 | nene_Segment_zero(), 15 | nene_Segment_zero(), 16 | nene_Segment_zero() 17 | }; 18 | } 19 | 20 | nene_ShapeQuadrilateral nene_ShapeQuadrilateral_copy(nene_ShapeQuadrilateral *quadrilateral_shape) { 21 | if (quadrilateral_shape == NULL) { 22 | return nene_ShapeQuadrilateral_zero(); 23 | } 24 | else { 25 | nene_ShapeQuadrilateral result = { 26 | .sides = { 27 | quadrilateral_shape->sides[0], 28 | quadrilateral_shape->sides[1], 29 | quadrilateral_shape->sides[2], 30 | quadrilateral_shape->sides[3] 31 | } 32 | }; 33 | return result; 34 | } 35 | } 36 | 37 | nene_ShapeQuadrilateral nene_Shape_get_rectf_shape(nene_Rectf rect) { 38 | // create the four sides of the rectangle. 39 | nene_ShapeQuadrilateral quad_shape = { 40 | .sides = { 41 | { .origin = { .x = rect.size.x, .y = 0.0f }, .ending = { .x = rect.size.x, .y = -rect.size.y } }, 42 | { .origin = { .x = 0.0f, .y = 0.0f }, .ending = { .x = rect.size.x, .y = 0.0f } }, 43 | { .origin = { .x = 0.0f, .y = -rect.size.y }, .ending = { .x = 0.0f, .y = 0.0f } }, 44 | { .origin = { .x = rect.size.x, .y = -rect.size.y }, .ending = { .x = 0.0f, .y = -rect.size.y } } 45 | } 46 | }; 47 | 48 | // a quadrilateral shape has four sides. 49 | for (int i = 0; i < 4; ++i) { 50 | quad_shape.sides[i].origin = nene_Vec2_add(quad_shape.sides[i].origin, rect.pos); 51 | quad_shape.sides[i].ending = nene_Vec2_add(quad_shape.sides[i].ending, rect.pos); 52 | } 53 | 54 | return quad_shape; 55 | } 56 | 57 | nene_ShapeQuadrilateral nene_Shape_get_rect_shape(nene_Rect rect) { 58 | return nene_Shape_get_rectf_shape(nene_Rectf_from_rect(rect)); 59 | } 60 | 61 | nene_Segment nene_Shape_get_rectf_diagonal(nene_Rectf rect, bool up_to_down) { 62 | nene_Vec2 first_corner = { 63 | .x = rect.pos.x, 64 | .y = up_to_down ? rect.pos.y : rect.pos.y - rect.size.y, 65 | }; 66 | nene_Vec2 second_corner = { 67 | .x = rect.pos.x + rect.size.x, 68 | .y = up_to_down ? rect.pos.y - rect.size.y : rect.pos.y, 69 | }; 70 | 71 | return (nene_Segment){ 72 | .origin = first_corner, 73 | .ending = second_corner, 74 | }; 75 | } 76 | -------------------------------------------------------------------------------- /src/math/vec2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/math/vec2.h" 9 | #include 10 | 11 | #include "nene/math/vec2i.h" 12 | 13 | nene_Vec2 nene_Vec2_from_vec2i(nene_Vec2i v) { 14 | return (nene_Vec2){ .x = v.x, .y = v.y }; 15 | } 16 | 17 | nene_Vec2i nene_Vec2_to_vec2i(nene_Vec2 v) { 18 | float x = roundf(v.x); 19 | float y = roundf(v.y); 20 | return (nene_Vec2i){ .x = (int32_t)x, .y = (int32_t)y }; 21 | } 22 | 23 | nene_Vec2 nene_Vec2_zero(void) { 24 | return (nene_Vec2){ .x = 0.0f, .y = 0.0f }; 25 | } 26 | 27 | nene_Vec2 nene_Vec2_copy(nene_Vec2 *v) { 28 | if (v == NULL) { 29 | return nene_Vec2_zero(); 30 | } 31 | else { 32 | return *v; 33 | } 34 | } 35 | 36 | nene_Vec2 nene_Vec2_one(void) { 37 | return (nene_Vec2){ .x = 1.0f, .y = 1.0f }; 38 | } 39 | 40 | bool nene_Vec2_equals(nene_Vec2 a, nene_Vec2 b) { 41 | return a.x == b.x && a.y == b.y; 42 | } 43 | 44 | nene_Vec2 nene_Vec2_add(nene_Vec2 a, nene_Vec2 b) { 45 | return (nene_Vec2){ .x = a.x + b.x, .y = a.y + b.y }; 46 | } 47 | 48 | nene_Vec2 nene_Vec2_sub(nene_Vec2 a, nene_Vec2 b) { 49 | return (nene_Vec2){ .x = a.x - b.x, .y = a.y - b.y }; 50 | } 51 | 52 | nene_Vec2 nene_Vec2_mul(nene_Vec2 a, nene_Vec2 b) { 53 | return (nene_Vec2){ .x = a.x * b.x, .y = a.y * b.y }; 54 | } 55 | 56 | nene_Vec2 nene_Vec2_scale(nene_Vec2 a, float s) { 57 | return (nene_Vec2){ .x = a.x * s, .y = a.y * s }; 58 | } 59 | 60 | nene_Vec2 nene_Vec2_negate(nene_Vec2 v) { 61 | return (nene_Vec2){ .x = -v.x, .y = -v.y }; 62 | } 63 | 64 | float nene_Vec2_len_sqr(nene_Vec2 v) { 65 | return v.x * v.x + v.y * v.y; 66 | } 67 | 68 | float nene_Vec2_len(nene_Vec2 v) { 69 | return sqrtf(nene_Vec2_len_sqr(v)); 70 | } 71 | 72 | nene_Vec2 nene_Vec2_lerp(nene_Vec2 a, nene_Vec2 b, float t) { 73 | // (1.0 - t) * a + t * b 74 | nene_Vec2 a_scaled = nene_Vec2_scale(a, 1.0f - t); 75 | nene_Vec2 b_scaled = nene_Vec2_scale(b, t); 76 | return nene_Vec2_add(a_scaled, b_scaled); 77 | } 78 | 79 | nene_Vec2 nene_Vec2_normalize(nene_Vec2 v) { 80 | nene_Vec2 result = nene_Vec2_zero(); 81 | float len = nene_Vec2_len(v); 82 | 83 | if (len > 0.0f) { 84 | return (nene_Vec2){ .x = v.x / len, .y = v.y / len }; 85 | } 86 | 87 | return result; 88 | } 89 | 90 | float nene_Vec2_dot(nene_Vec2 a, nene_Vec2 b) { 91 | return a.x * b.x + a.y * b.y; 92 | } 93 | 94 | float nene_Vec2_cross(nene_Vec2 a, nene_Vec2 b) { 95 | return a.x * b.y - a.y * b.x; 96 | } 97 | 98 | nene_Vec2 nene_Vec2_perpendicular(nene_Vec2 v) { 99 | return (nene_Vec2){ .x = -v.y, .y = v.x }; 100 | } 101 | -------------------------------------------------------------------------------- /src/math/vec2i.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/math/vec2i.h" 9 | #include 10 | 11 | nene_Vec2i nene_Vec2i_zero(void) { 12 | return (nene_Vec2i){ .x = 0, .y = 0 }; 13 | } 14 | 15 | nene_Vec2i nene_Vec2i_copy(nene_Vec2i *v) { 16 | if (v == NULL) { 17 | return nene_Vec2i_zero(); 18 | } 19 | else { 20 | return *v; 21 | } 22 | } 23 | 24 | nene_Vec2i nene_Vec2i_one(void) { 25 | return (nene_Vec2i){ .x = 1, .y = 1 }; 26 | } 27 | 28 | bool nene_Vec2i_equals(nene_Vec2i a, nene_Vec2i b) { 29 | return a.x == b.x && a.y == b.y; 30 | } 31 | 32 | nene_Vec2i nene_Vec2i_add(nene_Vec2i a, nene_Vec2i b) { 33 | return (nene_Vec2i){ .x = a.x + b.x, .y = a.y + b.y }; 34 | } 35 | 36 | nene_Vec2i nene_Vec2i_sub(nene_Vec2i a, nene_Vec2i b) { 37 | return (nene_Vec2i){ .x = a.x - b.x, .y = a.y - b.y }; 38 | } 39 | 40 | nene_Vec2i nene_Vec2i_mul(nene_Vec2i a, nene_Vec2i b) { 41 | return (nene_Vec2i){ .x = a.x * b.x, .y = a.y * b.y }; 42 | } 43 | 44 | nene_Vec2i nene_Vec2i_scale(nene_Vec2i v, float s) { 45 | float x = roundf(v.x * s); 46 | float y = roundf(v.y * s); 47 | return (nene_Vec2i){ .x = (int32_t)x, .y = (int32_t)y }; 48 | } 49 | 50 | nene_Vec2i nene_Vec2i_negate(nene_Vec2i v) { 51 | return (nene_Vec2i){ .x = -v.x, .y = -v.y }; 52 | } 53 | 54 | float nene_Vec2i_len_sqr(nene_Vec2i v) { 55 | return v.x * v.x + v.y * v.y; 56 | } 57 | 58 | float nene_Vec2i_len(nene_Vec2i v) { 59 | return sqrtf(nene_Vec2i_len_sqr(v)); 60 | } 61 | 62 | nene_Vec2i nene_Vec2i_lerp(nene_Vec2i a, nene_Vec2i b, float t) { 63 | // (1.0 - t) * a + t * b 64 | nene_Vec2i a_scaled = nene_Vec2i_scale(a, 1.0f - t); 65 | nene_Vec2i b_scaled = nene_Vec2i_scale(b, t); 66 | return nene_Vec2i_add(a_scaled, b_scaled); 67 | } 68 | 69 | float nene_Vec2i_dot(nene_Vec2i a, nene_Vec2i b) { 70 | return a.x * b.x + a.y * b.y; 71 | } 72 | 73 | float nene_Vec2i_cross(nene_Vec2i a, nene_Vec2i b) { 74 | return a.x * b.y - a.y * b.x; 75 | } 76 | 77 | nene_Vec2i nene_Vec2i_perpendicular(nene_Vec2i v) { 78 | return (nene_Vec2i){ .x = -v.y, .y = v.x }; 79 | } 80 | -------------------------------------------------------------------------------- /src/texture_atlas.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/texture_atlas.h" 9 | #include "nene/core.h" 10 | 11 | nene_TextureAtlas nene_TextureAtlas_zero(void) { 12 | return (nene_TextureAtlas){ 13 | .texture = nene_Texture_zero() 14 | }; 15 | } 16 | 17 | nene_TextureAtlas nene_TextureAtlas_copy(nene_TextureAtlas *texture_atlas) { 18 | if (texture_atlas == NULL) { 19 | return nene_TextureAtlas_zero(); 20 | } 21 | else { 22 | return *texture_atlas; 23 | } 24 | } 25 | 26 | void nene_TextureAtlas_destroy(nene_TextureAtlas *texture_atlas) { 27 | SDL_assert_release(texture_atlas != NULL); 28 | 29 | if (texture_atlas == NULL) { 30 | return; 31 | } 32 | 33 | nene_Texture_destroy(&texture_atlas->texture); 34 | *texture_atlas = (nene_TextureAtlas){ .width = 0 }; 35 | } 36 | 37 | nene_TextureAtlasCreation nene_TextureAtlas_load(const char *filepath, uint16_t width, nene_Grid grid) { 38 | SDL_assert(filepath != NULL); 39 | SDL_assert(width > 0); 40 | 41 | nene_TextureCreation texture_creation = nene_Texture_load(filepath); 42 | 43 | if (!texture_creation.created) { 44 | return (nene_TextureAtlasCreation){ 45 | .created = false, 46 | }; 47 | } 48 | 49 | return (nene_TextureAtlasCreation){ 50 | .created = true, 51 | .texture_atlas = (nene_TextureAtlas){ 52 | .texture = texture_creation.texture, 53 | .grid = grid, 54 | .width = width, 55 | }, 56 | }; 57 | } 58 | 59 | bool nene_TextureAtlas_draw_sub_texture(nene_TextureAtlas texture_atlas, nene_Vec2i subtexture_coord, nene_Vec2 position, bool is_world_pos) { 60 | if (is_world_pos) { 61 | position = nene_Core_world_pos_to_screen_point(position); 62 | } 63 | nene_Rect source_rect = nene_Grid_get_rect(texture_atlas.grid, subtexture_coord); 64 | nene_Rect destination_rect = { 65 | .pos = nene_Vec2_to_vec2i(position), 66 | .size = source_rect.size, 67 | }; 68 | 69 | return nene_Texture_draw_to_rect(texture_atlas.texture, source_rect, destination_rect); 70 | } 71 | 72 | bool nene_TextureAtlas_draw_sub_texture_ex(nene_TextureAtlas texture_atlas, nene_Vec2i subtexture_coord, nene_Vec2 position, bool is_world_pos, double angle, nene_Vec2 rotation_center, bool flip_x, bool flip_y) { 73 | if (is_world_pos) { 74 | position = nene_Core_world_pos_to_screen_point(position); 75 | } 76 | nene_Rect source_rect = nene_Grid_get_rect(texture_atlas.grid, subtexture_coord); 77 | nene_Rect destination_rect = { 78 | .pos = nene_Vec2_to_vec2i(position), 79 | .size = source_rect.size, 80 | }; 81 | 82 | return nene_Texture_draw_to_rect_ex(texture_atlas.texture, source_rect, destination_rect, angle, rotation_center, flip_x, flip_y); 83 | } 84 | 85 | bool nene_TextureAtlas_draw_nth_sub_texture(nene_TextureAtlas texture_atlas, uint32_t nth, nene_Vec2 position, bool is_world_pos) { 86 | nene_Vec2i texture_coord = nene_Grid_get_nth_cell_coord(nth, texture_atlas.width); 87 | return nene_TextureAtlas_draw_sub_texture(texture_atlas, texture_coord, position, is_world_pos); 88 | } 89 | 90 | bool nene_TextureAtlas_draw_nth_sub_texture_ex(nene_TextureAtlas texture_atlas, uint32_t nth, nene_Vec2 position, bool is_world_pos, double angle, nene_Vec2 rotation_center, bool flip_x, bool flip_y) { 91 | nene_Vec2i texture_coord = nene_Grid_get_nth_cell_coord(nth, texture_atlas.width); 92 | return nene_TextureAtlas_draw_sub_texture_ex(texture_atlas, texture_coord, position, is_world_pos, angle, rotation_center, flip_x, flip_y); 93 | } 94 | 95 | nene_Vec2 nene_TextureAtlas_get_sub_texture_center(nene_TextureAtlas texture_atlas) { 96 | return nene_Vec2_scale(nene_Vec2_from_vec2i(texture_atlas.grid.cell_size), 0.5f); 97 | } 98 | -------------------------------------------------------------------------------- /src/tilemap.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2021-present André Luiz Alvares 3 | Nene is licensed under the Zlib license. 4 | Please refer to the LICENSE file for details 5 | SPDX-License-Identifier: Zlib 6 | */ 7 | 8 | #include "nene/core.h" 9 | #include "nene/tilemap.h" 10 | #include "nene/math/vec2.h" 11 | 12 | nene_Vec2i nene_Tilemap_get_size_in_tiles(nene_Tilemap tilemap) { 13 | const nene_Vec2i at_tile_coord = nene_Grid_get_nth_cell_coord(tilemap.tile_count, tilemap.width); 14 | return (nene_Vec2i){ 15 | .x = tilemap.width, 16 | .y = at_tile_coord.y + 1, 17 | }; 18 | } 19 | 20 | nene_Vec2i nene_Tilemap_get_size(nene_Tilemap tilemap) { 21 | nene_Vec2i size_in_tiles = nene_Tilemap_get_size_in_tiles(tilemap); 22 | size_in_tiles.y--; 23 | return nene_Vec2i_sub( 24 | nene_Grid_get_cell_position( 25 | tilemap.grid, 26 | size_in_tiles 27 | ), 28 | tilemap.grid.gap 29 | ); 30 | } 31 | 32 | bool nene_Tilemap_draw(nene_Tilemap tilemap, nene_Vec2 position, bool is_world_pos, uint16_t tiles[], uint32_t count) { 33 | SDL_assert(tiles != NULL); 34 | SDL_assert(count > 0); 35 | SDL_assert(count <= tilemap.tile_count); 36 | SDL_assert(tilemap.width > 0); 37 | 38 | if (count <= 0 || count > tilemap.tile_count || tilemap.width <= 0) { 39 | return false; 40 | } 41 | 42 | if (is_world_pos) { 43 | position = nene_Core_world_pos_to_screen_point(position); 44 | } 45 | 46 | bool fail = false; 47 | 48 | // TODO: here we "trust" that `count` it's on a valid range of `tiles` array, it's this the 49 | // best approach? 50 | for (size_t tile_i = 0; tile_i < count; ++tile_i) { 51 | const uint16_t tileset_n = tiles[tile_i]; 52 | 53 | // tiles with 0 value are ignored 54 | if (tileset_n > 0) { 55 | const nene_Vec2 tile_position = nene_Vec2_from_vec2i( 56 | nene_Grid_get_nth_cell_position(tilemap.grid, tile_i, tilemap.width) 57 | ); 58 | // NOTE: tileset_n - 1 it's safe because it's on a tileset_n > 0 condition. 59 | fail = fail || !nene_TextureAtlas_draw_nth_sub_texture(tilemap.tileset, tileset_n - 1, nene_Vec2_add(position, tile_position), false); 60 | } 61 | } 62 | 63 | return !fail; 64 | } 65 | 66 | void nene_Tilemap_destroy(nene_Tilemap *tilemap) { 67 | SDL_assert(tilemap != NULL); 68 | 69 | if (tilemap == NULL) { 70 | return; 71 | } 72 | 73 | nene_TextureAtlas_destroy(&tilemap->tileset); 74 | *tilemap = (nene_Tilemap){ .tile_count = 0 }; 75 | } 76 | -------------------------------------------------------------------------------- /tlconfig.lua: -------------------------------------------------------------------------------- 1 | return { 2 | gen_target="5.4", 3 | gen_compat="off", 4 | } 5 | --------------------------------------------------------------------------------