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