You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 line
5.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - first person maze
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 3.5
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2019-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include <stdlib.h> // Required for: free()
  15. //------------------------------------------------------------------------------------
  16. // Program main entry point
  17. //------------------------------------------------------------------------------------
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze");
  25. // Define the camera to look into our 3d world
  26. Camera camera = { 0 };
  27. camera.position = (Vector3){ 0.2f, 0.4f, 0.2f }; // Camera position
  28. camera.target = (Vector3){ 0.185f, 0.4f, 0.0f }; // Camera looking at point
  29. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  30. camera.fovy = 45.0f; // Camera field-of-view Y
  31. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  32. Image imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
  33. Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
  34. Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
  35. Model model = LoadModelFromMesh(mesh);
  36. // NOTE: By default each cube is mapped to one part of texture atlas
  37. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
  38. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  39. // Get map image data to be used for collision detection
  40. Color *mapPixels = LoadImageColors(imMap);
  41. UnloadImage(imMap); // Unload image from RAM
  42. Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
  43. DisableCursor(); // Limit cursor to relative movement inside the window
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. Vector3 oldCamPos = camera.position; // Store old camera position
  52. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  53. // Check player collision (we simplify to 2D collision detection)
  54. Vector2 playerPos = { camera.position.x, camera.position.z };
  55. float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
  56. int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f);
  57. int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f);
  58. // Out-of-limits security check
  59. if (playerCellX < 0) playerCellX = 0;
  60. else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1;
  61. if (playerCellY < 0) playerCellY = 0;
  62. else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1;
  63. // Check map collisions using image data and player position
  64. // TODO: Improvement: Just check player surrounding cells for collision
  65. for (int y = 0; y < cubicmap.height; y++)
  66. {
  67. for (int x = 0; x < cubicmap.width; x++)
  68. {
  69. if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel
  70. (CheckCollisionCircleRec(playerPos, playerRadius,
  71. (Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
  72. {
  73. // Collision detected, reset camera position
  74. camera.position = oldCamPos;
  75. }
  76. }
  77. }
  78. //----------------------------------------------------------------------------------
  79. // Draw
  80. //----------------------------------------------------------------------------------
  81. BeginDrawing();
  82. ClearBackground(RAYWHITE);
  83. BeginMode3D(camera);
  84. DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map
  85. EndMode3D();
  86. DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4.0f - 20, 20.0f }, 0.0f, 4.0f, WHITE);
  87. DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
  88. // Draw player position radar
  89. DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED);
  90. DrawFPS(10, 10);
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. UnloadImageColors(mapPixels); // Unload color array
  97. UnloadTexture(cubicmap); // Unload cubicmap texture
  98. UnloadTexture(texture); // Unload map texture
  99. UnloadModel(model); // Unload map model
  100. CloseWindow(); // Close window and OpenGL context
  101. //--------------------------------------------------------------------------------------
  102. return 0;
  103. }