1 | /******************************************************************************************* |
2 | * |
3 | * raylib [models] example - first person maze |
4 | * |
5 | * This example has been created using raylib 2.5 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2019 Ramon Santamaria (@raysan5) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | #include <stdlib.h> // Required for: free() |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze" ); |
24 | |
25 | // Define the camera to look into our 3d world |
26 | Camera camera = { { 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; |
27 | |
28 | Image imMap = LoadImage("resources/cubicmap.png" ); // Load cubicmap image (RAM) |
29 | Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM) |
30 | Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f }); |
31 | Model model = LoadModelFromMesh(mesh); |
32 | |
33 | // NOTE: By default each cube is mapped to one part of texture atlas |
34 | Texture2D texture = LoadTexture("resources/cubicmap_atlas.png" ); // Load map texture |
35 | model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture |
36 | |
37 | // Get map image data to be used for collision detection |
38 | Color *mapPixels = GetImageData(imMap); |
39 | UnloadImage(imMap); // Unload image from RAM |
40 | |
41 | Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position |
42 | Vector3 playerPosition = camera.position; // Set player position |
43 | |
44 | SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode |
45 | |
46 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
47 | //-------------------------------------------------------------------------------------- |
48 | |
49 | // Main game loop |
50 | while (!WindowShouldClose()) // Detect window close button or ESC key |
51 | { |
52 | // Update |
53 | //---------------------------------------------------------------------------------- |
54 | Vector3 oldCamPos = camera.position; // Store old camera position |
55 | |
56 | UpdateCamera(&camera); // Update camera |
57 | |
58 | // Check player collision (we simplify to 2D collision detection) |
59 | Vector2 playerPos = { camera.position.x, camera.position.z }; |
60 | float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision) |
61 | |
62 | int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f); |
63 | int playerCellY = (int)(playerPos.y - mapPosition.z + 0.5f); |
64 | |
65 | // Out-of-limits security check |
66 | if (playerCellX < 0) playerCellX = 0; |
67 | else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1; |
68 | |
69 | if (playerCellY < 0) playerCellY = 0; |
70 | else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1; |
71 | |
72 | // Check map collisions using image data and player position |
73 | // TODO: Improvement: Just check player surrounding cells for collision |
74 | for (int y = 0; y < cubicmap.height; y++) |
75 | { |
76 | for (int x = 0; x < cubicmap.width; x++) |
77 | { |
78 | if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel |
79 | (CheckCollisionCircleRec(playerPos, playerRadius, |
80 | (Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f }))) |
81 | { |
82 | // Collision detected, reset camera position |
83 | camera.position = oldCamPos; |
84 | } |
85 | } |
86 | } |
87 | //---------------------------------------------------------------------------------- |
88 | |
89 | // Draw |
90 | //---------------------------------------------------------------------------------- |
91 | BeginDrawing(); |
92 | |
93 | ClearBackground(RAYWHITE); |
94 | |
95 | BeginMode3D(camera); |
96 | |
97 | DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map |
98 | //DrawCubeV(playerPosition, (Vector3){ 0.2f, 0.4f, 0.2f }, RED); // Draw player |
99 | |
100 | EndMode3D(); |
101 | |
102 | DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE); |
103 | DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN); |
104 | |
105 | // Draw player position radar |
106 | DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED); |
107 | |
108 | DrawFPS(10, 10); |
109 | |
110 | EndDrawing(); |
111 | //---------------------------------------------------------------------------------- |
112 | } |
113 | |
114 | // De-Initialization |
115 | //-------------------------------------------------------------------------------------- |
116 | free(mapPixels); // Unload color array |
117 | |
118 | UnloadTexture(cubicmap); // Unload cubicmap texture |
119 | UnloadTexture(texture); // Unload map texture |
120 | UnloadModel(model); // Unload map model |
121 | |
122 | CloseWindow(); // Close window and OpenGL context |
123 | //-------------------------------------------------------------------------------------- |
124 | |
125 | return 0; |
126 | } |
127 | |