FPS_-_Generate_CubeStructure.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // This example uses the included fps free movement camera. It needs the entire map(all models) to move up or down since the camera
  3. // with this mode is locked to its y position.
  4. //
  5. //
  6. #include "raylib.h"
  7. #include <stdlib.h>
  8. int main(void)
  9. {
  10. // Initialization
  11. //--------------------------------------------------------------------------------------
  12. const int screenWidth = 800;
  13. const int screenHeight = 450;
  14. InitWindow(screenWidth, screenHeight, "raylib example.");
  15. // We generate a checked image for texturing WALLS
  16. Image checked = GenImageChecked(2, 2, 1, 1, WHITE, DARKGRAY);
  17. Texture2D texture = LoadTextureFromImage(checked);
  18. UnloadImage(checked);
  19. Model model = { 0 };
  20. model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  21. // Set checked texture as default diffuse component for all models material
  22. model.materials[0].maps[MAP_DIFFUSE].texture = texture;
  23. //////// WALKABLE
  24. Image checked2 = GenImageChecked(2, 2, 1, 1, (Color){100,0,0,255},RED);
  25. Texture2D texture2 = LoadTextureFromImage(checked2);
  26. UnloadImage(checked2);
  27. Model model2 = { 0 };
  28. model2 = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  29. // Set checked texture as default diffuse component for all models material
  30. model2.materials[0].maps[MAP_DIFFUSE].texture = texture2;
  31. ///////// GRASS
  32. Image checked3 = GenImageChecked(8, 8, 1, 1, (Color){10,150,20,255},GREEN);
  33. Texture2D texture3 = LoadTextureFromImage(checked3);
  34. UnloadImage(checked3);
  35. Model model3 = { 0 };
  36. model3 = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  37. // Set checked texture as default diffuse component for all models material
  38. model3.materials[0].maps[MAP_DIFFUSE].texture = texture3;
  39. // Define the camera to look into our 3d world
  40. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  41. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
  42. //SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. float myy={0.0f};
  46. // drunkenwalk from top to down.
  47. int cavew=39;
  48. int caveh=29;
  49. int caved=39;
  50. int cave[cavew][caveh][caved];
  51. int caveb[cavew][caveh][caved];
  52. for(int x=0;x<cavew;x++){
  53. for(int y=0;y<caveh;y++){
  54. for(int z=0;z<caved;z++){
  55. cave[x][y][z] = 0;
  56. caveb[x][y][z] = 0;
  57. }
  58. }
  59. }
  60. int x=20;
  61. int y;
  62. int z=20;
  63. for(int y=caveh-1;y>0;y--){
  64. int oldx=x;
  65. int oldz=z;
  66. x+=GetRandomValue(-1,1);
  67. z+=GetRandomValue(-1,1);
  68. if(x<0 || x>=cavew)x=oldx;
  69. if(z<0 || z>=caved)z=oldz;
  70. // cave[x][y][z]=1;
  71. for(int x1=-3;x1<3;x1++){
  72. for(int z1=-3;z1<3;z1++){
  73. for(int y1=-3;y1<3;y1++){
  74. if(x+x1<0 || x+x1>=cavew)continue;
  75. if(z+z1<0 || z+z1>=caved)continue;
  76. if(y+y1<0 || y+y1>=caveh)continue;
  77. cave[x+x1][y+y1][z+z1] = 1;
  78. }
  79. }
  80. }
  81. if(GetRandomValue(0,10)<9 && y<caveh)y+=1;
  82. }
  83. //
  84. // Remove doubles
  85. for(int x1=0;x1<cavew;x1++){
  86. for(int z1=0;z1<caved;z1++){
  87. for(int y1=0;y1<caveh;y1++){
  88. if(cave[x1][y1][z1]==0){
  89. int cnt=0;
  90. for(int x2=-1;x2<2;x2++){
  91. for(int z2=-1;z2<2;z2++){
  92. for(int y2=-1;y2<2;y2++){
  93. if(x1+x2<0 || x1+x2>=cavew || y1+y2<0 || y1+y2>=caveh || z1+z2<0 || z1+z2>=caved)continue;
  94. if(cave[x1+x2][y1+y2][z1+z2]==1)cnt+=1;
  95. }
  96. }
  97. }
  98. if(cnt>0)caveb[x1][y1][z1]=1;
  99. }
  100. }
  101. }
  102. }
  103. // Walkable/roofs (model2)
  104. for(int x1=0;x1<cavew;x1++){
  105. for(int z1=0;z1<caved;z1++){
  106. for(int y1=0;y1<caveh;y1++){
  107. if(caveb[x1][y1][z1]==1){
  108. if(y1+1<=caveh && caveb[x1][y1+1][z1]==0){
  109. caveb[x1][y1][z1] = 2;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. // Main game loop
  116. while (!WindowShouldClose()) // Detect window close button or ESC key
  117. {
  118. // Update
  119. //----------------------------------------------------------------------------------
  120. UpdateCamera(&camera); // Update internal camera and our camera
  121. // Move all models up or down.
  122. if(IsKeyDown(KEY_R))myy-=0.2f;
  123. if(IsKeyDown(KEY_F))myy+=0.2f;
  124. //----------------------------------------------------------------------------------
  125. // Draw
  126. //----------------------------------------------------------------------------------
  127. BeginDrawing();
  128. ClearBackground(RAYWHITE);
  129. BeginMode3D(camera);
  130. for(int x=0;x<50;x++){
  131. for(int z=0;z<50;z++){
  132. Vector3 position;
  133. position.x = x;
  134. position.y = 0+myy;
  135. position.z = z;
  136. DrawModel(model3, position, 1.0f, WHITE);
  137. }
  138. }
  139. for(int x=0;x<cavew;x++){
  140. for(int y=0;y<caveh;y++){
  141. for(int z=0;z<caved;z++){
  142. if(caveb[x][y][z]>0){
  143. Vector3 position;
  144. position.x = x;
  145. position.y = y+myy;
  146. position.z = z;
  147. if(caveb[x][y][z]==1){
  148. DrawModel(model, position, 1.0f, WHITE);
  149. }else{
  150. DrawModel(model2, position, 1.0f, WHITE);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. EndMode3D();
  157. DrawFPS(0,0);
  158. DrawText("W/A/S/D - R/F and Mouse = Controls.",100,0,20,BLACK);
  159. EndDrawing();
  160. //----------------------------------------------------------------------------------
  161. }
  162. // De-Initialization
  163. //--------------------------------------------------------------------------------------
  164. UnloadTexture(texture); // Unload texture
  165. UnloadTexture(texture2); // Unload texture
  166. UnloadTexture(texture3); // Unload texture
  167. // Unload models data (GPU VRAM)
  168. UnloadModel(model);
  169. UnloadModel(model2);
  170. UnloadModel(model3);
  171. CloseWindow(); // Close window and OpenGL context
  172. //--------------------------------------------------------------------------------------
  173. return 0;
  174. }