raylib/examples/shapes_logo_raylib_anim.c

160 lines
5.6 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [shapes] example - raylib logo animation
*
* This example has been created using raylib 1.4 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
int main()
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
2014-09-30 01:41:05 +04:00
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo animation");
2014-09-30 01:41:05 +04:00
int logoPositionX = screenWidth/2 - 128;
int logoPositionY = screenHeight/2 - 128;
2014-09-30 01:41:05 +04:00
int framesCounter = 0;
int lettersCount = 0;
2014-09-30 01:41:05 +04:00
int topSideRecWidth = 16;
int leftSideRecHeight = 16;
2014-09-30 01:41:05 +04:00
int bottomSideRecWidth = 16;
int rightSideRecHeight = 16;
2014-09-30 01:41:05 +04:00
int state = 0; // Tracking animation states (State Machine)
2016-01-16 14:52:55 +03:00
float alpha = 1.0f; // Useful for fading
2014-09-30 01:41:05 +04:00
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
2014-09-30 01:41:05 +04:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (state == 0) // State 0: Small box blinking
{
framesCounter++;
2014-09-30 01:41:05 +04:00
if (framesCounter == 120)
2014-09-30 01:41:05 +04:00
{
state = 1;
framesCounter = 0; // Reset counter... will be used later...
}
}
else if (state == 1) // State 1: Top and left bars growing
{
topSideRecWidth += 4;
leftSideRecHeight += 4;
2014-09-30 01:41:05 +04:00
if (topSideRecWidth == 256) state = 2;
}
else if (state == 2) // State 2: Bottom and right bars growing
{
bottomSideRecWidth += 4;
rightSideRecHeight += 4;
2014-09-30 01:41:05 +04:00
if (bottomSideRecWidth == 256) state = 3;
}
else if (state == 3) // State 3: Letters appearing (one by one)
{
framesCounter++;
2014-09-30 01:41:05 +04:00
if (framesCounter/12) // Every 12 frames, one more letter!
2014-09-30 01:41:05 +04:00
{
lettersCount++;
framesCounter = 0;
}
2014-09-30 01:41:05 +04:00
if (lettersCount >= 10) // When all letters have appeared, just fade out everything
{
alpha -= 0.02f;
2014-09-30 01:41:05 +04:00
if (alpha <= 0.0f)
2013-12-27 15:24:28 +04:00
{
alpha = 0.0f;
2013-12-27 15:24:28 +04:00
state = 4;
}
}
}
else if (state == 4) // State 4: Reset and Replay
{
if (IsKeyPressed('R'))
{
framesCounter = 0;
lettersCount = 0;
2014-09-30 01:41:05 +04:00
2013-12-27 15:24:28 +04:00
topSideRecWidth = 16;
leftSideRecHeight = 16;
2014-09-30 01:41:05 +04:00
2013-12-27 15:24:28 +04:00
bottomSideRecWidth = 16;
rightSideRecHeight = 16;
2014-09-30 01:41:05 +04:00
alpha = 1.0f;
state = 0; // Return to State 0
}
}
//----------------------------------------------------------------------------------
2014-09-30 01:41:05 +04:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
2014-09-30 01:41:05 +04:00
ClearBackground(RAYWHITE);
2014-09-30 01:41:05 +04:00
if (state == 0)
{
if ((framesCounter/15)%2) DrawRectangle(logoPositionX, logoPositionY, 16, 16, BLACK);
}
else if (state == 1)
{
DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
}
else if (state == 2)
{
DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
2014-09-30 01:41:05 +04:00
DrawRectangle(logoPositionX + 240, logoPositionY, 16, rightSideRecHeight, BLACK);
DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, BLACK);
}
else if (state == 3)
{
DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, Fade(BLACK, alpha));
DrawRectangle(logoPositionX, logoPositionY + 16, 16, leftSideRecHeight - 32, Fade(BLACK, alpha));
DrawRectangle(logoPositionX + 240, logoPositionY + 16, 16, rightSideRecHeight - 32, Fade(BLACK, alpha));
DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, Fade(BLACK, alpha));
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
DrawText(SubText("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
2013-12-27 15:24:28 +04:00
}
else if (state == 4)
{
DrawText("[R] REPLAY", 340, 200, 20, GRAY);
}
2014-09-30 01:41:05 +04:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
2014-09-30 01:41:05 +04:00
return 0;
}