Merge branch 'master' into examples/Fixed_shaders_game_of_life_For_Web
This commit is contained in:
commit
34e11d38e1
1 changed files with 100 additions and 68 deletions
|
|
@ -59,6 +59,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;
|
||||
|
|
@ -79,10 +81,9 @@ int main(void)
|
|||
{ "Puffer train", { 0.1f, 0.5f } }, { "Glider Gun", { 0.2f, 0.2f } }, { "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
|
||||
|
|
@ -96,8 +97,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));
|
||||
|
||||
|
|
@ -114,7 +113,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
|
||||
|
|
@ -142,10 +141,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;
|
||||
}
|
||||
|
|
@ -155,7 +152,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
|
||||
|
|
@ -176,10 +172,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)
|
||||
|
|
@ -191,6 +185,7 @@ int main(void)
|
|||
EndTextureMode();
|
||||
imageToDraw = (Image*)RL_MALLOC(sizeof(Image));
|
||||
*imageToDraw = LoadImageFromTexture(worldOnScreen.texture);
|
||||
|
||||
UnloadRenderTexture(worldOnScreen);
|
||||
}
|
||||
|
||||
|
|
@ -200,23 +195,19 @@ 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;
|
||||
|
|
@ -237,6 +228,7 @@ int main(void)
|
|||
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);
|
||||
|
|
@ -250,9 +242,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);
|
||||
|
|
@ -261,26 +256,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;
|
||||
|
|
@ -294,10 +288,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 });
|
||||
|
|
@ -311,8 +307,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);
|
||||
|
||||
|
|
@ -324,8 +319,6 @@ int main(void)
|
|||
buttonFaster = GuiButton((Rectangle){ 710, 382, 80, 16 }, "Faster");
|
||||
buttonSlower = GuiButton((Rectangle){ 710, 400, 80, 16 }, "Slower");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
DrawFPS(712, 426);
|
||||
|
||||
EndDrawing();
|
||||
|
|
|
|||
Loading…
Reference in a new issue