FPS_CameraInCubedBox.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "raylib.h"
  2. #include <stdlib.h>
  3. // On my i7-7700HQ with Nvidia 1050ti I get 60Fps with these box settings.
  4. const int mapwidth = 30;
  5. const int mapheight = 10;
  6. const int mapdepth = 30;
  7. int main(void)
  8. {
  9. // Initialization
  10. //--------------------------------------------------------------------------------------
  11. const int screenWidth = 800;
  12. const int screenHeight = 450;
  13. InitWindow(screenWidth, screenHeight, "raylib example.");
  14. // We generate a checked image for texturing
  15. Image checked = GenImageChecked(2, 2, 1, 1, (Color){10,10,10,255}, (Color){50,50,50,255});
  16. Texture2D texture = LoadTextureFromImage(checked);
  17. UnloadImage(checked);
  18. Image checked2 = GenImageChecked(2, 2, 1, 1, (Color){40,10,10,255}, (Color){90,50,50,255});
  19. Texture2D texture2 = LoadTextureFromImage(checked2);
  20. UnloadImage(checked2);
  21. Model model = { 0 };
  22. model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  23. Model model2 = { 0 };
  24. model2 = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  25. // Set checked texture as default diffuse component for all models material
  26. model.materials[0].maps[MAP_DIFFUSE].texture = texture;
  27. model2.materials[0].maps[MAP_DIFFUSE].texture = texture2;
  28. // Define the camera to look into our 3d world
  29. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  30. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
  31. //SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. // Here we store the old camera position and then check if the camera hits the edge
  40. // and if that happens we restore it to the old position. We check x and z different
  41. // so we get sliding in stead of complete stop when hitting an edge.
  42. //
  43. Vector3 oldCamPos = camera.position; // Store old camera position
  44. // Default walking speed
  45. int moveSpeed=1;
  46. // Sprint speed
  47. if (IsKeyDown(KEY_LEFT_SHIFT)){
  48. moveSpeed=2;
  49. }
  50. // We check collision and movement x amount of times.
  51. for(int i=0;i<moveSpeed;i++){
  52. UpdateCamera(&camera); // Update internal camera and our camera
  53. if (camera.position.x<1 || camera.position.x>mapwidth-2){
  54. camera.position.x = oldCamPos.x;
  55. }
  56. if (camera.position.z<1 || camera.position.z>mapdepth-2){
  57. camera.position.z = oldCamPos.z;
  58. }
  59. }
  60. //----------------------------------------------------------------------------------
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. BeginDrawing();
  64. ClearBackground(RAYWHITE);
  65. BeginMode3D(camera);
  66. //Our player box!!
  67. //
  68. // We create our box by creating a tripple for loop and drawing cubes there
  69. // on the edges.
  70. //
  71. for(int x=0;x<mapwidth;x++){
  72. for(int y=0;y<mapheight;y++){
  73. for(int z=0;z<mapdepth;z++){
  74. if(x==0 || y==0 || x==mapwidth-1 || y==mapheight-1 || z==0 || z==mapdepth-1){
  75. Vector3 position;
  76. position.x = x;
  77. position.y = y;
  78. position.z = z;
  79. if (y==0 || y==9 ){
  80. DrawModel(model, position, 1.0f, WHITE);
  81. }else{
  82. DrawModel(model2, position, 1.0f, WHITE);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. EndMode3D();
  89. DrawFPS(0,0);
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }
  93. // De-Initialization
  94. UnloadTexture(texture); // Unload texture
  95. UnloadTexture(texture2); // Unload texture
  96. // Unload models data (GPU VRAM)
  97. UnloadModel(model);
  98. UnloadModel(model2);
  99. //--------------------------------------------------------------------------------------
  100. CloseWindow(); // Close window and OpenGL context
  101. //--------------------------------------------------------------------------------------
  102. return 0;
  103. }