raylib/examples/models/models_first_person_maze.c

128 lines
5.5 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [models] example - first person maze
*
2022-07-20 02:28:37 +03:00
* Example originally created with raylib 2.5, last time updated with raylib 3.5
*
2022-07-20 02:28:37 +03:00
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
2023-01-01 18:00:56 +03:00
* Copyright (c) 2019-2023 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
2019-05-02 14:24:02 +03:00
#include <stdlib.h> // Required for: free()
2019-05-02 14:15:05 +03:00
2022-06-21 20:53:18 +03:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze");
// Define the camera to look into our 3d world
2019-05-27 01:18:15 +03:00
Camera camera = { { 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
Image imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
Model model = LoadModelFromMesh(mesh);
2019-05-20 17:36:42 +03:00
// NOTE: By default each cube is mapped to one part of texture atlas
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
2019-05-20 17:36:42 +03:00
// Get map image data to be used for collision detection
2021-01-31 18:22:24 +03:00
Color *mapPixels = LoadImageColors(imMap);
UnloadImage(imMap); // Unload image from RAM
2019-05-20 17:36:42 +03:00
Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
2019-05-20 17:36:42 +03:00
SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
Vector3 oldCamPos = camera.position; // Store old camera position
2019-05-20 17:36:42 +03:00
2022-08-02 19:12:57 +03:00
UpdateCamera(&camera);
2019-05-20 17:36:42 +03:00
// Check player collision (we simplify to 2D collision detection)
Vector2 playerPos = { camera.position.x, camera.position.z };
float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
2019-05-20 17:36:42 +03:00
int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f);
int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f);
2019-05-20 17:36:42 +03:00
// Out-of-limits security check
if (playerCellX < 0) playerCellX = 0;
else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1;
2019-05-20 17:36:42 +03:00
if (playerCellY < 0) playerCellY = 0;
else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1;
2019-05-20 17:36:42 +03:00
// 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
2019-05-20 17:36:42 +03:00
(CheckCollisionCircleRec(playerPos, 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);
BeginMode3D(camera);
DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map
EndMode3D();
2019-05-20 17:36:42 +03:00
DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4.0f - 20, 20.0f }, 0.0f, 4.0f, WHITE);
DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
2019-05-20 17:36:42 +03:00
// Draw player position radar
DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
2021-01-31 18:22:24 +03:00
UnloadImageColors(mapPixels); // Unload color array
2019-05-20 17:36:42 +03:00
2021-01-31 18:22:24 +03:00
UnloadTexture(cubicmap); // Unload cubicmap texture
UnloadTexture(texture); // Unload map texture
UnloadModel(model); // Unload map model
2021-01-31 18:22:24 +03:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}