diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c9561b..45af300 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [vX.X.X] - Unreleased +### Added +- `Model.Draw()` +- `Model.DrawWires()` +- `models_first_person_maze.cpp` example +- Error correction when unloading images, materials, models and meshes + ## [v3.5.0-alpha1] - 2020-12-26 ### Changed - Update to raylib 3.5.0 diff --git a/examples/models/models_first_person_maze.cpp b/examples/models/models_first_person_maze.cpp new file mode 100644 index 0000000..fafbe72 --- /dev/null +++ b/examples/models/models_first_person_maze.cpp @@ -0,0 +1,137 @@ +/******************************************************************************************* +* +* raylib [models] example - first person maze +* +* This example has been created using raylib 2.5 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2019 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib-cpp.hpp" + +#include // Required for: free() + +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + raylib::Window window(screenWidth, screenHeight, "raylib [models] example - first person maze"); + + // Define the camera to look into our 3d world + raylib::Camera camera({ 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f); + + raylib::Image imMap("resources/cubicmap.png"); // Load cubicmap image (RAM) + raylib::Texture cubicmap(imMap); // Convert image to texture to display (VRAM) + raylib::Mesh mesh = raylib::Mesh::Cubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f }); + raylib::Model model(mesh); + + // NOTE: By default each cube is mapped to one part of texture atlas + raylib::Texture texture("resources/cubicmap_atlas.png"); // Load map texture + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture + + // Get map image data to be used for collision detection + Color *mapPixels = imMap.LoadColors(); + + imMap.Unload(); // Unload image from RAM + + raylib::Vector3 mapPosition(-16.0f, 0.0f, -8.0f); // Set model position + raylib::Vector3 playerPosition(camera.position); // Set player position + + camera.SetMode(CAMERA_FIRST_PERSON); // Set camera mode + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!window.ShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + raylib::Vector3 oldCamPos(camera.position); // Store old camera position + + camera.Update(); // Update camera + + // Check player collision (we simplify to 2D collision detection) + raylib::Vector2 playerPos(camera.position.x, camera.position.z); + float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision) + + int playerCellX = static_cast(playerPos.x - mapPosition.x + 0.5f); + int playerCellY = static_cast(playerPos.y - mapPosition.z + 0.5f); + + // Out-of-limits security check + if (playerCellX < 0) playerCellX = 0; + else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1; + + if (playerCellY < 0) playerCellY = 0; + else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1; + + // Check map collisions using image data and player position + // TODO: Improvement: Just check player surrounding cells for collision + for (int y = 0; y < cubicmap.height; y++) + { + for (int x = 0; x < cubicmap.width; x++) + { + if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel + (playerPos.CheckCollisionCircleRec(playerRadius, + (Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f }))) + { + // Collision detected, reset camera position + camera.position = oldCamPos; + } + } + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + camera.BeginMode3D(); + + model.Draw(mapPosition); // Draw maze map + // DrawCubeV(playerPosition, (Vector3){ 0.2f, 0.4f, 0.2f }, RED); // Draw player + + camera.EndMode3D(); + + cubicmap.Draw((Vector2){ GetScreenWidth() - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE); + DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN); + + // Draw player position radar + DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + free(mapPixels); // Unload color array + + /** + * TODO: Fix double freeing of Mesh, possibly from UnloadModel() unloading meshes in Model.hpp? + * + * INFO: TEXTURE: [ID 4] Unloaded texture data from VRAM (GPU) + * INFO: VAO: [ID 2] Unloaded vertex data from VRAM (GPU) + * INFO: MODEL: Unloaded model (and meshes) from RAM and VRAM + * double free or corruption (!prev) + */ + mesh.vboId = NULL; // This shouldn't be needed, but currently is. + + //UnloadTexture(cubicmap); // Unload cubicmap texture + //UnloadTexture(texture); // Unload map texture + //UnloadModel(model); // Unload map model + + //CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/models/resources/cubicmap.png b/examples/models/resources/cubicmap.png new file mode 100644 index 0000000..392dbf2 Binary files /dev/null and b/examples/models/resources/cubicmap.png differ diff --git a/examples/models/resources/cubicmap_atlas.png b/examples/models/resources/cubicmap_atlas.png new file mode 100644 index 0000000..9fc404a Binary files /dev/null and b/examples/models/resources/cubicmap_atlas.png differ diff --git a/include/Image.hpp b/include/Image.hpp index 0cd1b7b..fa6bac0 100644 --- a/include/Image.hpp +++ b/include/Image.hpp @@ -163,7 +163,10 @@ class Image : public ::Image { } inline void Unload() { - ::UnloadImage(*this); + if (data != NULL) { + ::UnloadImage(*this); + data = NULL; + } } /** diff --git a/include/Material.hpp b/include/Material.hpp index c3a7929..f5d7832 100644 --- a/include/Material.hpp +++ b/include/Material.hpp @@ -76,7 +76,10 @@ class Material : public ::Material { * Unload material from memory */ inline void Unload() { - ::UnloadMaterial(*this); + if (maps != NULL) { + ::UnloadMaterial(*this); + maps = NULL; + } } /** diff --git a/include/Mesh.hpp b/include/Mesh.hpp index e26ec32..41e86a0 100644 --- a/include/Mesh.hpp +++ b/include/Mesh.hpp @@ -27,6 +27,7 @@ #include #include +#include #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" @@ -161,7 +162,10 @@ class Mesh : public ::Mesh { } inline void Unload() { - ::UnloadMesh(*this); + if (vboId != NULL) { + ::UnloadMesh(*this); + vboId = NULL; + } } inline raylib::BoundingBox BoundingBox() const { diff --git a/include/Model.hpp b/include/Model.hpp index ce4f3a8..0d9e9e4 100644 --- a/include/Model.hpp +++ b/include/Model.hpp @@ -53,7 +53,12 @@ class Model : public ::Model { GETTERSETTER(::Matrix, Transform, transform) GETTERSETTER(int, MeshCount, meshCount) GETTERSETTER(int, MaterialCount, materialCount) + GETTERSETTER(::Mesh *, Meshes, meshes) + GETTERSETTER(::Material *, Materials, materials) + GETTERSETTER(int *, MeshMaterial, meshMaterial) GETTERSETTER(int, BoneCount, boneCount) + GETTERSETTER(::BoneInfo *, Bones, bones) + GETTERSETTER(::Transform *, BindPoe, bindPose) Model& operator=(const ::Model& model) { set(model); @@ -69,7 +74,11 @@ class Model : public ::Model { * Unload model (including meshes) from memory (RAM and/or VRAM) */ inline void Unload() { - ::UnloadModel(*this); + if (meshes != NULL || materials != NULL) { + ::UnloadModel(*this); + meshes = NULL; + materials = NULL; + } } /** @@ -110,6 +119,38 @@ class Model : public ::Model { return ::IsModelAnimationValid(*this, anim); } + /** + * Draw a model (with texture if set) + */ + inline Model& Draw(::Vector3 position, float scale = 1.0f, ::Color tint = WHITE) { + ::DrawModel(*this, position, scale, tint); + return *this; + } + + /** + * Draw a model with extended parameters + */ + inline Model& Draw(::Vector3 position, ::Vector3 rotationAxis, float rotationAngle = 0.0f, ::Vector3 scale = {1.0f,1.0f,1.0f}, ::Color tint = WHITE) { + ::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint); + return *this; + } + + /** + * Draw a model wires (with texture if set) + */ + inline Model& DrawWires(::Vector3 position, float scale = 1.0f, ::Color tint = WHITE) { + ::DrawModelWires(*this, position, scale, tint); + return *this; + } + + /** + * Draw a model wires (with texture if set) with extended parameters + */ + inline Model& DrawWires(::Vector3 position, ::Vector3 rotationAxis, float rotationAngle = 0.0f, ::Vector3 scale = {1.0f,1.0f,1.0f}, ::Color tint = WHITE) { + ::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint); + return *this; + } + protected: inline void set(::Model model) { transform = model.transform; diff --git a/include/Music.hpp b/include/Music.hpp index b5c29f2..e746b90 100644 --- a/include/Music.hpp +++ b/include/Music.hpp @@ -51,9 +51,11 @@ class Music : public ::Music { Unload(); } - GETTERSETTER(int, CtxType, ctxType) - GETTERSETTER(bool, Looping, looping) + GETTERSETTER(::AudioStream, Stream, stream) GETTERSETTER(unsigned int, SampleCount, sampleCount) + GETTERSETTER(bool, Looping, looping) + GETTERSETTER(int, CtxType, ctxType) + GETTERSETTER(void*, CtxData, ctxData) Music& operator=(const ::Music& music) { set(music);