Update shaders_game_of_life.c

This commit is contained in:
Ray
2025-12-10 09:46:06 +01:00
parent bc2057345b
commit 5e8118daf2

View File

@@ -60,6 +60,8 @@ int main(void)
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - game of life");
const int menuWidth = 100;
const int windowWidth = screenWidth - menuWidth;
@@ -80,10 +82,9 @@ int main(void)
{ "Puffer train", "puffer_train", { 0.1f, 0.5f } }, { "Glider Gun", "glider_gun", { 0.2f, 0.2f } }, { "Breeder", "breeder", { 0.1f, 0.5f } },
{ "Random", "", { 0.5f, 0.5f } }
};
const int numberOfPresets = sizeof(presetPatterns) / sizeof(presetPatterns[0]);
const int numberOfPresets = sizeof(presetPatterns)/sizeof(presetPatterns[0]);
// Variable declaration
//--------------------------------------------------------------------------------------
int zoom = 1;
float offsetX = (worldWidth - windowWidth)/2.0f; // Centered on window
float offsetY = (worldHeight - windowHeight)/2.0f; // Centered on window
@@ -97,8 +98,6 @@ int main(void)
bool buttonFaster = false;
bool buttonSlower = false;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - game of life");
// Load shader
Shader shdrGameOfLife = LoadShader(0, TextFormat("resources/shaders/glsl%i/game_of_life.fs", GLSL_VERSION));
@@ -115,7 +114,7 @@ int main(void)
EndTextureMode();
Image startPattern = LoadImage("resources/game_of_life/r_pentomino.png");
UpdateTextureRec(world2.texture, (Rectangle) { worldWidth / 2.0f, worldHeight / 2.0f, (float)(startPattern.width), (float)(startPattern.height) }, startPattern.data);
UpdateTextureRec(world2.texture, (Rectangle){ worldWidth/2.0f, worldHeight/2.0f, (float)(startPattern.width), (float)(startPattern.height) }, startPattern.data);
UnloadImage(startPattern);
// Pointers to the two textures, to be swapped
@@ -143,10 +142,8 @@ int main(void)
const float centerX = offsetX + (windowWidth/2.0f)/zoom;
const float centerY = offsetY + (windowHeight/2.0f)/zoom;
if (buttonZoomIn || (mouseWheelMove > 0.0f))
zoom *= 2;
if ((buttonZomOut || (mouseWheelMove < 0.0f)) && (zoom > 1))
zoom /= 2;
if (buttonZoomIn || (mouseWheelMove > 0.0f)) zoom *= 2;
if ((buttonZomOut || (mouseWheelMove < 0.0f)) && (zoom > 1)) zoom /= 2;
offsetX = centerX - (windowWidth/2.0f)/zoom;
offsetY = centerY - (windowHeight/2.0f)/zoom;
}
@@ -156,7 +153,6 @@ int main(void)
if (buttonSlower) framesPerStep++;
// Mouse management
//----------------------------------------------------------------------------------
if ((mode == MODE_RUN) || (mode == MODE_PAUSE))
{
FreeImageToDraw(&imageToDraw); // Free the image to draw: no longer needed in these modes
@@ -177,10 +173,8 @@ int main(void)
const float offsetDecimalY = offsetY - floorf(offsetY);
int sizeInWorldX = (int)(ceilf((float)(windowWidth + offsetDecimalX*zoom)/zoom));
int sizeInWorldY = (int)(ceilf((float)(windowHeight + offsetDecimalY*zoom)/zoom));
if (offsetX + sizeInWorldX >= worldWidth)
sizeInWorldX = worldWidth - (int)floorf(offsetX);
if (offsetY + sizeInWorldY >= worldHeight)
sizeInWorldY = worldHeight - (int)floorf(offsetY);
if (offsetX + sizeInWorldX >= worldWidth) sizeInWorldX = worldWidth - (int)floorf(offsetX);
if (offsetY + sizeInWorldY >= worldHeight) sizeInWorldY = worldHeight - (int)floorf(offsetY);
// Create image to draw if not created yet
if (imageToDraw == NULL)
@@ -192,6 +186,7 @@ int main(void)
EndTextureMode();
imageToDraw = (Image*)RL_MALLOC(sizeof(Image));
*imageToDraw = LoadImageFromTexture(worldOnScreen.texture);
UnloadRenderTexture(worldOnScreen);
}
@@ -201,32 +196,30 @@ int main(void)
{
int mouseX = (int)(mousePosition.x + offsetDecimalX*zoom)/zoom;
int mouseY = (int)(mousePosition.y + offsetDecimalY*zoom)/zoom;
if (mouseX >= sizeInWorldX)
mouseX = sizeInWorldX - 1;
if (mouseY >= sizeInWorldY)
mouseY = sizeInWorldY - 1;
if (firstColor == -1)
firstColor = (GetImageColor(*imageToDraw, mouseX, mouseY).r < 5)? 0 : 1;
if (mouseX >= sizeInWorldX) mouseX = sizeInWorldX - 1;
if (mouseY >= sizeInWorldY) mouseY = sizeInWorldY - 1;
if (firstColor == -1) firstColor = (GetImageColor(*imageToDraw, mouseX, mouseY).r < 5)? 0 : 1;
const int prevColor = (GetImageColor(*imageToDraw, mouseX, mouseY).r < 5)? 0 : 1;
ImageDrawPixel(imageToDraw, mouseX, mouseY, (firstColor) ? BLACK : RAYWHITE);
if (prevColor != firstColor)
UpdateTextureRec(currentWorld->texture, (Rectangle){ floorf(offsetX), floorf(offsetY), (float)(sizeInWorldX), (float)(sizeInWorldY) }, imageToDraw->data);
if (prevColor != firstColor) UpdateTextureRec(currentWorld->texture, (Rectangle){ floorf(offsetX), floorf(offsetY), (float)(sizeInWorldX), (float)(sizeInWorldY) }, imageToDraw->data);
}
else
firstColor = -1;
else firstColor = -1;
}
// Load selected preset
//----------------------------------------------------------------------------------
if (preset >= 0)
{
Image pattern;
if (preset < numberOfPresets - 1) // Preset with pattern image lo load
{
pattern = LoadImage(TextFormat("resources/game_of_life/%s.png", presetPatterns[preset].fileName));
BeginTextureMode(*currentWorld);
ClearBackground(RAYWHITE);
EndTextureMode();
UpdateTextureRec(currentWorld->texture, (Rectangle){ worldWidth*presetPatterns[preset].position.x - pattern.width/2.0f,
worldHeight*presetPatterns[preset].position.y - pattern.height/2.0f,
(float)(pattern.width), (float)(pattern.height) }, pattern.data);
@@ -240,9 +233,12 @@ int main(void)
{
ImageClearBackground(&pattern, RAYWHITE);
for (int x = 0; x < pattern.width; x++)
{
for (int y = 0; y < pattern.height; y++)
if (GetRandomValue(0, 100) < 15)
ImageDrawPixel(&pattern, x, y, BLACK);
{
if (GetRandomValue(0, 100) < 15) ImageDrawPixel(&pattern, x, y, BLACK);
}
}
UpdateTextureRec(currentWorld->texture,
(Rectangle){ (float)(pattern.width*i), (float)(pattern.height*j),
(float)(pattern.width), (float)(pattern.height) }, pattern.data);
@@ -251,26 +247,25 @@ int main(void)
}
UnloadImage(pattern);
mode = MODE_PAUSE;
offsetX = worldWidth * presetPatterns[preset].position.x - windowWidth/zoom/2.0f;
offsetY = worldHeight * presetPatterns[preset].position.y - windowHeight/zoom/2.0f;
offsetX = worldWidth*presetPatterns[preset].position.x - windowWidth/zoom/2.0f;
offsetY = worldHeight*presetPatterns[preset].position.y - windowHeight/zoom/2.0f;
}
// Check window draw inside world limits
if (offsetX < 0) offsetX = 0;
if (offsetY < 0) offsetY = 0;
if (offsetX > worldWidth - (float)(windowWidth)/zoom)
offsetX = worldWidth - (float)(windowWidth)/zoom;
if (offsetY > worldHeight - (float)(windowHeight)/zoom)
offsetY = worldHeight - (float)(windowHeight)/zoom;
if (offsetX > worldWidth - (float)(windowWidth)/zoom) offsetX = worldWidth - (float)(windowWidth)/zoom;
if (offsetY > worldHeight - (float)(windowHeight)/zoom) offsetY = worldHeight - (float)(windowHeight)/zoom;
// Rectangles for drawing texture portion to screen
//----------------------------------------------------------------------------------
const Rectangle textureSourceToScreen = { offsetX, offsetY, (float)windowWidth/zoom, (float)windowHeight/zoom };
//----------------------------------------------------------------------------------
// Draw to texture
//----------------------------------------------------------------------------------
if ((mode == MODE_RUN) && ((frame % framesPerStep) == 0))
if ((mode == MODE_RUN) && ((frame%framesPerStep) == 0))
{
// Swap worlds
RenderTexture2D *tempWorld = currentWorld;
@@ -284,10 +279,12 @@ int main(void)
EndShaderMode();
EndTextureMode();
}
//----------------------------------------------------------------------------------
// Draw to screen
//----------------------------------------------------------------------------------
BeginDrawing();
DrawTexturePro(currentWorld->texture, textureSourceToScreen, textureOnScreen, (Vector2){ 0, 0 }, 0.0f, WHITE);
DrawLine(windowWidth, 0, windowWidth, screenHeight, (Color){ 218, 218, 218, 255 });
@@ -301,8 +298,7 @@ int main(void)
DrawText("Presets", 710, 58, 8, GRAY);
preset = -1;
for (int i = 0; i < numberOfPresets; i++)
if (GuiButton((Rectangle){ 710.0f, 70.0f + 18*i, 80.0f, 16.0f }, presetPatterns[i].name))
preset = i;
if (GuiButton((Rectangle){ 710.0f, 70.0f + 18*i, 80.0f, 16.0f }, presetPatterns[i].name)) preset = i;
GuiToggleGroup((Rectangle){ 710, 258, 80, 16 }, "Run\nPause\nDraw", &mode);
@@ -314,8 +310,6 @@ int main(void)
buttonFaster = GuiButton((Rectangle){ 710, 382, 80, 16 }, "Faster");
buttonSlower = GuiButton((Rectangle){ 710, 400, 80, 16 }, "Slower");
//------------------------------------------------------------------------------
DrawFPS(712, 426);
EndDrawing();