Example_-_Flood_Path_Map.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "raylib.h"
  2. // Our flood path function
  3. void floodmap(int map[][10],int x, int y);
  4. int main(void)
  5. {
  6. // Initialization
  7. //--------------------------------------------------------------------------------------
  8. const int screenWidth = 800;
  9. const int screenHeight = 450;
  10. InitWindow(screenWidth, screenHeight, "raylib example.");
  11. // Create a Image in memory
  12. RenderTexture2D target = LoadRenderTexture(32, 32);
  13. int sprite[8][8] = {
  14. {0,1,0,0,0,0,1,0},
  15. {0,0,1,0,0,1,0,0},
  16. {0,0,0,2,2,0,0,0},
  17. {0,0,1,2,2,1,0,0},
  18. {0,1,0,2,2,0,1,0},
  19. {0,0,0,1,1,0,0,0},
  20. {0,0,2,3,3,2,0,0},
  21. {0,0,1,1,1,1,0,0}};
  22. // Clear our texture(image) before entering the game loop
  23. BeginTextureMode(target);
  24. ClearBackground(BLANK); // Make the entire Sprite Transparent.
  25. EndTextureMode();
  26. // Draw something on it.
  27. for (int y=0;y<8;y++)
  28. {
  29. for (int x=0;x<8; x++)
  30. {
  31. // Our sprite color 1
  32. if (sprite[y][x]==1)
  33. {
  34. BeginTextureMode(target);
  35. DrawRectangle(x*4,y*4,4,4,BLUE);
  36. EndTextureMode();
  37. }
  38. // Our sprite color 2
  39. if (sprite[y][x]==2)
  40. {
  41. BeginTextureMode(target);
  42. DrawRectangle(x*4,y*4,4,8,LIGHTGRAY);
  43. EndTextureMode();
  44. }
  45. // Our sprite color 12
  46. if (sprite[y][x]==12)
  47. {
  48. BeginTextureMode(target);
  49. DrawRectangle(x*4,y*4,4,8,BLACK);
  50. EndTextureMode();
  51. }
  52. }
  53. }
  54. int map[10][10] = {0};
  55. floodmap(map,3,3);
  56. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  57. //--------------------------------------------------------------------------------------
  58. // Main game loop
  59. while (!WindowShouldClose()) // Detect window close button or ESC key
  60. {
  61. // Update
  62. //----------------------------------------------------------------------------------
  63. Vector2 mousePos = GetMousePosition();
  64. if(IsMouseButtonPressed(0)){
  65. int x = GetMouseX()/20;
  66. int y = GetMouseY()/20;
  67. if(x<10 && y<10)floodmap(map,x,y);
  68. }
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. DrawText("Simple Floodfill pathmap.", 100, 180, 40, LIGHTGRAY);
  75. for(int y=0;y<10;y++){
  76. for(int x=0;x<10;x++){
  77. DrawText(TextFormat("%i",map[x][y]),x*20,y*20,20,BLACK);
  78. }}
  79. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  80. DrawTexture(target.texture,mousePos.x, mousePos.y, WHITE);
  81. EndDrawing();
  82. //----------------------------------------------------------------------------------
  83. }
  84. // De-Initialization
  85. //--------------------------------------------------------------------------------------
  86. UnloadRenderTexture(target); // Unload render texture
  87. //--------------------------------------------------------------------------------------
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }
  92. // Here we flood the map from the destination to the other parts of the map.
  93. void floodmap(int map[][10],int x,int y){
  94. // Clear the old map
  95. for(int y=0;y<10;y++){
  96. for(int x=0;x<10;x++){
  97. map[x][y]=0;
  98. }}
  99. // set our destination
  100. map[x][y]=1;
  101. // flood map
  102. int low = 1;
  103. bool end=false;
  104. while(end==false){
  105. end = true;
  106. for(int y=0;y<10;y++){
  107. for(int x=0;x<10;x++){
  108. if(map[x][y]==low){
  109. if(map[x-1][y]==0){map[x-1][y]=low+1;end = false;}
  110. if(map[x+1][y]==0){map[x+1][y]=low+1;end = false;}
  111. if(map[x][y-1]==0){map[x][y-1]=low+1;end = false;}
  112. if(map[x][y+1]==0){map[x][y+1]=low+1;end = false;}
  113. }
  114. }
  115. }
  116. low++;
  117. }
  118. }