Some examples included in this batch require the included libraries: `easings.h` and `raygui.h`. Examples included: - shapes_bouncing_ball - shapes_collision_area - shapes_following_eyes - shapes_draw_circle_sector (requires raygui.h) - shapes_draw_rectangle_rounded (requires raygui.h) - shapes_draw_ring (requires raygui.h) - shapes_easings_ball_anim (requires easings.h) - shapes_easings_box_anim (requires easings.h) - shapes_easings_rectangle_array (requires easings.h)pull/832/head
@ -0,0 +1,257 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib easings (header only file) | |||
* | |||
* Useful easing functions for values animation | |||
* | |||
* This header uses: | |||
* #define EASINGS_STATIC_INLINE // Inlines all functions code, so it runs faster. | |||
* // This requires lots of memory on system. | |||
* How to use: | |||
* The four inputs t,b,c,d are defined as follows: | |||
* t = current time (in any unit measure, but same unit as duration) | |||
* b = starting value to interpolate | |||
* c = the total change in value of b that needs to occur | |||
* d = total time it should take to complete (duration) | |||
* | |||
* Example: | |||
* | |||
* int currentTime = 0; | |||
* int duration = 100; | |||
* float startPositionX = 0.0f; | |||
* float finalPositionX = 30.0f; | |||
* float currentPositionX = startPositionX; | |||
* | |||
* while (currentPositionX < finalPositionX) | |||
* { | |||
* currentPositionX = EaseSineIn(currentTime, startPositionX, finalPositionX - startPositionX, duration); | |||
* currentTime++; | |||
* } | |||
* | |||
* A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/) | |||
* | |||
* Robert Penner License | |||
* --------------------------------------------------------------------------------- | |||
* Open source under the BSD License. | |||
* | |||
* Copyright (c) 2001 Robert Penner. All rights reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without modification, | |||
* are permitted provided that the following conditions are met: | |||
* | |||
* - Redistributions of source code must retain the above copyright notice, | |||
* this list of conditions and the following disclaimer. | |||
* - Redistributions in binary form must reproduce the above copyright notice, | |||
* this list of conditions and the following disclaimer in the documentation | |||
* and/or other materials provided with the distribution. | |||
* - Neither the name of the author nor the names of contributors may be used | |||
* to endorse or promote products derived from this software without specific | |||
* prior written permission. | |||
* | |||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |||
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | |||
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
* OF THE POSSIBILITY OF SUCH DAMAGE. | |||
* --------------------------------------------------------------------------------- | |||
* | |||
* Copyright (c) 2015 Ramon Santamaria | |||
* | |||
* This software is provided "as-is", without any express or implied warranty. In no event | |||
* will the authors be held liable for any damages arising from the use of this software. | |||
* | |||
* Permission is granted to anyone to use this software for any purpose, including commercial | |||
* applications, and to alter it and redistribute it freely, subject to the following restrictions: | |||
* | |||
* 1. The origin of this software must not be misrepresented; you must not claim that you | |||
* wrote the original software. If you use this software in a product, an acknowledgment | |||
* in the product documentation would be appreciated but is not required. | |||
* | |||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented | |||
* as being the original software. | |||
* | |||
* 3. This notice may not be removed or altered from any source distribution. | |||
* | |||
**********************************************************************************************/ | |||
#ifndef EASINGS_H | |||
#define EASINGS_H | |||
#define EASINGS_STATIC_INLINE // NOTE: By default, compile functions as static inline | |||
#if defined(EASINGS_STATIC_INLINE) | |||
#define EASEDEF static inline | |||
#else | |||
#define EASEDEF extern | |||
#endif | |||
#include <math.h> // Required for: sin(), cos(), sqrt(), pow() | |||
#ifndef PI | |||
#define PI 3.14159265358979323846f //Required as PI is not always defined in math.h | |||
#endif | |||
#ifdef __cplusplus | |||
extern "C" { // Prevents name mangling of functions | |||
#endif | |||
// Linear Easing functions | |||
EASEDEF float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); } | |||
EASEDEF float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); } | |||
EASEDEF float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); } | |||
EASEDEF float EaseLinearInOut(float t,float b, float c, float d) { return (c*t/d + b); } | |||
// Sine Easing functions | |||
EASEDEF float EaseSineIn(float t, float b, float c, float d) { return (-c*cos(t/d*(PI/2)) + c + b); } | |||
EASEDEF float EaseSineOut(float t, float b, float c, float d) { return (c*sin(t/d*(PI/2)) + b); } | |||
EASEDEF float EaseSineInOut(float t, float b, float c, float d) { return (-c/2*(cos(PI*t/d) - 1) + b); } | |||
// Circular Easing functions | |||
EASEDEF float EaseCircIn(float t, float b, float c, float d) { return (-c*(sqrt(1 - (t/=d)*t) - 1) + b); } | |||
EASEDEF float EaseCircOut(float t, float b, float c, float d) { return (c*sqrt(1 - (t=t/d-1)*t) + b); } | |||
EASEDEF float EaseCircInOut(float t, float b, float c, float d) | |||
{ | |||
if ((t/=d/2) < 1) return (-c/2*(sqrt(1 - t*t) - 1) + b); | |||
return (c/2*(sqrt(1 - t*(t-=2)) + 1) + b); | |||
} | |||
// Cubic Easing functions | |||
EASEDEF float EaseCubicIn(float t, float b, float c, float d) { return (c*(t/=d)*t*t + b); } | |||
EASEDEF float EaseCubicOut(float t, float b, float c, float d) { return (c*((t=t/d-1)*t*t + 1) + b); } | |||
EASEDEF float EaseCubicInOut(float t, float b, float c, float d) | |||
{ | |||
if ((t/=d/2) < 1) return (c/2*t*t*t + b); | |||
return (c/2*((t-=2)*t*t + 2) + b); | |||
} | |||
// Quadratic Easing functions | |||
EASEDEF float EaseQuadIn(float t, float b, float c, float d) { return (c*(t/=d)*t + b); } | |||
EASEDEF float EaseQuadOut(float t, float b, float c, float d) { return (-c*(t/=d)*(t-2) + b); } | |||
EASEDEF float EaseQuadInOut(float t, float b, float c, float d) | |||
{ | |||
if ((t/=d/2) < 1) return (((c/2)*(t*t)) + b); | |||
return (-c/2*(((t-2)*(--t)) - 1) + b); | |||
} | |||
// Exponential Easing functions | |||
EASEDEF float EaseExpoIn(float t, float b, float c, float d) { return (t == 0) ? b : (c*pow(2, 10*(t/d - 1)) + b); } | |||
EASEDEF float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-pow(2, -10*t/d) + 1) + b); } | |||
EASEDEF float EaseExpoInOut(float t, float b, float c, float d) | |||
{ | |||
if (t == 0) return b; | |||
if (t == d) return (b + c); | |||
if ((t/=d/2) < 1) return (c/2*pow(2, 10*(t - 1)) + b); | |||
return (c/2*(-pow(2, -10*--t) + 2) + b); | |||
} | |||
// Back Easing functions | |||
EASEDEF float EaseBackIn(float t, float b, float c, float d) | |||
{ | |||
float s = 1.70158f; | |||
float postFix = t/=d; | |||
return (c*(postFix)*t*((s + 1)*t - s) + b); | |||
} | |||
EASEDEF float EaseBackOut(float t, float b, float c, float d) | |||
{ | |||
float s = 1.70158f; | |||
return (c*((t=t/d-1)*t*((s + 1)*t + s) + 1) + b); | |||
} | |||
EASEDEF float EaseBackInOut(float t, float b, float c, float d) | |||
{ | |||
float s = 1.70158f; | |||
if ((t/=d/2) < 1) return (c/2*(t*t*(((s*=(1.525f)) + 1)*t - s)) + b); | |||
float postFix = t-=2; | |||
return (c/2*((postFix)*t*(((s*=(1.525f)) + 1)*t + s) + 2) + b); | |||
} | |||
// Bounce Easing functions | |||
EASEDEF float EaseBounceOut(float t, float b, float c, float d) | |||
{ | |||
if ((t/=d) < (1/2.75f)) | |||
{ | |||
return (c*(7.5625f*t*t) + b); | |||
} | |||
else if (t < (2/2.75f)) | |||
{ | |||
float postFix = t-=(1.5f/2.75f); | |||
return (c*(7.5625f*(postFix)*t + 0.75f) + b); | |||
} | |||
else if (t < (2.5/2.75)) | |||
{ | |||
float postFix = t-=(2.25f/2.75f); | |||
return (c*(7.5625f*(postFix)*t + 0.9375f) + b); | |||
} | |||
else | |||
{ | |||
float postFix = t-=(2.625f/2.75f); | |||
return (c*(7.5625f*(postFix)*t + 0.984375f) + b); | |||
} | |||
} | |||
EASEDEF float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d-t, 0, c, d) + b); } | |||
EASEDEF float EaseBounceInOut(float t, float b, float c, float d) | |||
{ | |||
if (t < d/2) return (EaseBounceIn(t*2, 0, c, d)*0.5f + b); | |||
else return (EaseBounceOut(t*2-d, 0, c, d)*0.5f + c*0.5f + b); | |||
} | |||
// Elastic Easing functions | |||
EASEDEF float EaseElasticIn(float t, float b, float c, float d) | |||
{ | |||
if (t == 0) return b; | |||
if ((t/=d) == 1) return (b + c); | |||
float p = d*0.3f; | |||
float a = c; | |||
float s = p/4; | |||
float postFix = a*pow(2, 10*(t-=1)); | |||
return (-(postFix*sin((t*d-s)*(2*PI)/p )) + b); | |||
} | |||
EASEDEF float EaseElasticOut(float t, float b, float c, float d) | |||
{ | |||
if (t == 0) return b; | |||
if ((t/=d) == 1) return (b + c); | |||
float p = d*0.3f; | |||
float a = c; | |||
float s = p/4; | |||
return (a*pow(2,-10*t)*sin((t*d-s)*(2*PI)/p) + c + b); | |||
} | |||
EASEDEF float EaseElasticInOut(float t, float b, float c, float d) | |||
{ | |||
if (t == 0) return b; | |||
if ((t/=d/2) == 2) return (b + c); | |||
float p = d*(0.3f*1.5f); | |||
float a = c; | |||
float s = p/4; | |||
if (t < 1) | |||
{ | |||
float postFix = a*pow(2, 10*(t-=1)); | |||
return -0.5f*(postFix*sin((t*d-s)*(2*PI)/p)) + b; | |||
} | |||
float postFix = a*pow(2, -10*(t-=1)); | |||
return (postFix*sin((t*d-s)*(2*PI)/p)*0.5f + c + b); | |||
} | |||
#ifdef __cplusplus | |||
} | |||
#endif | |||
#endif // EASINGS_H |
@ -0,0 +1,76 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - bouncing ball | |||
* | |||
* This example has been created using raylib 1.0 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2013 Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include "raylib.h" | |||
int main() | |||
{ | |||
// Initialization | |||
//--------------------------------------------------------- | |||
const int screenWidth = 800; | |||
const int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball"); | |||
Vector2 ballPosition = { GetScreenWidth()/2, GetScreenHeight()/2 }; | |||
Vector2 ballSpeed = { 5.0f, 4.0f }; | |||
int ballRadius = 20; | |||
bool pause = 0; | |||
int framesCounter = 0; | |||
SetTargetFPS(60); | |||
//---------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//----------------------------------------------------- | |||
if (IsKeyPressed(KEY_SPACE)) pause = !pause; | |||
if (!pause) | |||
{ | |||
ballPosition.x += ballSpeed.x; | |||
ballPosition.y += ballSpeed.y; | |||
// Check walls collision for bouncing | |||
if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f; | |||
if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f; | |||
} | |||
else framesCounter++; | |||
//----------------------------------------------------- | |||
// Draw | |||
//----------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
DrawCircleV(ballPosition, ballRadius, MAROON); | |||
DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY); | |||
// On pause, we draw a blinking message | |||
if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY); | |||
DrawFPS(10, 10); | |||
EndDrawing(); | |||
//----------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//--------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//---------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,108 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - collision area | |||
* | |||
* This example has been created using raylib 2.5 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2013-2019 Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include "raylib.h" | |||
#include <stdlib.h> // Required for abs() | |||
int main() | |||
{ | |||
// Initialization | |||
//--------------------------------------------------------- | |||
int screenWidth = 800; | |||
int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area"); | |||
// Box A: Moving box | |||
Rectangle boxA = { 10, GetScreenHeight()/2 - 50, 200, 100 }; | |||
int boxASpeedX = 4; | |||
// Box B: Mouse moved box | |||
Rectangle boxB = { GetScreenWidth()/2 - 30, GetScreenHeight()/2 - 30, 60, 60 }; | |||
Rectangle boxCollision = { 0 }; // Collision rectangle | |||
int screenUpperLimit = 40; // Top menu limits | |||
bool pause = false; // Movement pause | |||
bool collision = false; // Collision detection | |||
SetTargetFPS(60); | |||
//---------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//----------------------------------------------------- | |||
// Move box if not paused | |||
if (!pause) boxA.x += boxASpeedX; | |||
// Bounce box on x screen limits | |||
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0)) boxASpeedX *= -1; | |||
// Update player-controlled-box (box02) | |||
boxB.x = GetMouseX() - boxB.width/2; | |||
boxB.y = GetMouseY() - boxB.height/2; | |||
// Make sure Box B does not go out of move area limits | |||
if ((boxB.x + boxB.width) >= GetScreenWidth()) boxB.x = GetScreenWidth() - boxB.width; | |||
else if (boxB.x <= 0) boxB.x = 0; | |||
if ((boxB.y + boxB.height) >= GetScreenHeight()) boxB.y = GetScreenHeight() - boxB.height; | |||
else if (boxB.y <= screenUpperLimit) boxB.y = screenUpperLimit; | |||
// Check boxes collision | |||
collision = CheckCollisionRecs(boxA, boxB); | |||
// Get collision rectangle (only on collision) | |||
if (collision) boxCollision = GetCollisionRec(boxA, boxB); | |||
// Pause Box A movement | |||
if (IsKeyPressed(KEY_SPACE)) pause = !pause; | |||
//----------------------------------------------------- | |||
// Draw | |||
//----------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
DrawRectangle(0, 0, screenWidth, screenUpperLimit, collision? RED : BLACK); | |||
DrawRectangleRec(boxA, GOLD); | |||
DrawRectangleRec(boxB, BLUE); | |||
if (collision) | |||
{ | |||
// Draw collision area | |||
DrawRectangleRec(boxCollision, LIME); | |||
// Draw collision message | |||
DrawText("COLLISION!", GetScreenWidth()/2 - MeasureText("COLLISION!", 20)/2, screenUpperLimit/2 - 10, 20, BLACK); | |||
// Draw collision area | |||
DrawText(FormatText("Collision Area: %i", (int)boxCollision.width*(int)boxCollision.height), GetScreenWidth()/2 - 100, screenUpperLimit + 10, 20, BLACK); | |||
} | |||
DrawFPS(10, 10); | |||
EndDrawing(); | |||
//----------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//--------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//---------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,79 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - draw circle sector (with gui options) | |||
* | |||
* This example has been created using raylib 2.5 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2019 Vlad Adrian (@Demizdor) and Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include <raylib.h> | |||
#define RAYGUI_IMPLEMENTATION | |||
#include "raygui.h" // Required for GUI controls | |||
int main() | |||
{ | |||
// Initialization | |||
//-------------------------------------------------------------------------------------- | |||
const int screenWidth = 800; | |||
const int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - draw circle sector"); | |||
Vector2 center = {(GetScreenWidth() - 300)/2, GetScreenHeight()/2 }; | |||
float outerRadius = 180.f; | |||
int startAngle = 0; | |||
int endAngle = 180; | |||
int segments = 0; | |||
SetTargetFPS(60); | |||
//-------------------------------------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//---------------------------------------------------------------------------------- | |||
// NOTE: All variables update happens inside GUI control functions | |||
//---------------------------------------------------------------------------------- | |||
// Draw | |||
//---------------------------------------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
DrawLine(500, 0, 500, GetScreenHeight(), Fade(LIGHTGRAY, 0.6f)); | |||
DrawRectangle(500, 0, GetScreenWidth() - 500, GetScreenHeight(), Fade(LIGHTGRAY, 0.3f)); | |||
DrawCircleSector(center, outerRadius, startAngle, endAngle, segments, Fade(MAROON, 0.3)); | |||
DrawCircleSectorLines(center, outerRadius, startAngle, endAngle, segments, Fade(MAROON, 0.6)); | |||
// Draw GUI controls | |||
//------------------------------------------------------------------------------ | |||
startAngle = GuiSliderBar((Rectangle){ 600, 40, 120, 20}, "StartAngle", startAngle, 0, 720, true ); | |||
endAngle = GuiSliderBar((Rectangle){ 600, 70, 120, 20}, "EndAngle", endAngle, 0, 720, true); | |||
outerRadius = GuiSliderBar((Rectangle){ 600, 140, 120, 20}, "Radius", outerRadius, 0, 200, true); | |||
segments = GuiSliderBar((Rectangle){ 600, 170, 120, 20}, "Segments", segments, 0, 100, true); | |||
//------------------------------------------------------------------------------ | |||
DrawText(FormatText("MODE: %s", (segments >= 4)? "MANUAL" : "AUTO"), 600, 200, 10, (segments >= 4)? MAROON : DARKGRAY); | |||
DrawFPS(10, 10); | |||
EndDrawing(); | |||
//---------------------------------------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//-------------------------------------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//-------------------------------------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,87 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - draw rectangle rounded (with gui options) | |||
* | |||
* This example has been created using raylib 2.5 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2019 Vlad Adrian (@Demizdor) and Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include <raylib.h> | |||
#define RAYGUI_IMPLEMENTATION | |||
#include "raygui.h" // Required for GUI controls | |||
int main(void) | |||
{ | |||
// Initialization | |||
//-------------------------------------------------------------------------------------- | |||
const int screenWidth = 800; | |||
const int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - draw rectangle rounded"); | |||
float roundness = 0.2f; | |||
int width = 200; | |||
int height = 100; | |||
int segments = 0; | |||
int lineThick = 1; | |||
bool drawRect = false; | |||
bool drawRoundedRect = true; | |||
bool drawRoundedLines = false; | |||
SetTargetFPS(60); | |||
//-------------------------------------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//---------------------------------------------------------------------------------- | |||
Rectangle rec = { (GetScreenWidth() - width - 250)/2, (GetScreenHeight() - height)/2, width, height }; | |||
//---------------------------------------------------------------------------------- | |||
// Draw | |||
//---------------------------------------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
DrawLine(560, 0, 560, GetScreenHeight(), Fade(LIGHTGRAY, 0.6f)); | |||
DrawRectangle(560, 0, GetScreenWidth() - 500, GetScreenHeight(), Fade(LIGHTGRAY, 0.3f)); | |||
if (drawRect) DrawRectangleRec(rec, Fade(GOLD, 0.6)); | |||
if (drawRoundedRect) DrawRectangleRounded(rec, roundness, segments, Fade(MAROON, 0.2)); | |||
if (drawRoundedLines) DrawRectangleRoundedLines(rec,roundness, segments, lineThick, Fade(MAROON, 0.4)); | |||
// Draw GUI controls | |||
//------------------------------------------------------------------------------ | |||
width = GuiSliderBar((Rectangle){ 640, 40, 105, 20 }, "Width", width, 0, GetScreenWidth() - 300, true ); | |||
height = GuiSliderBar((Rectangle){ 640, 70, 105, 20 }, "Height", height, 0, GetScreenHeight() - 50, true); | |||
roundness = GuiSliderBar((Rectangle){ 640, 140, 105, 20 }, "Roundness", roundness, 0.0f, 1.0f, true); | |||
lineThick = GuiSliderBar((Rectangle){ 640, 170, 105, 20 }, "Thickness", lineThick, 0, 20, true); | |||
segments = GuiSliderBar((Rectangle){ 640, 240, 105, 20}, "Segments", segments, 0, 60, true); | |||
drawRoundedRect = GuiCheckBox((Rectangle){ 640, 320, 20, 20 }, "DrawRoundedRect", drawRoundedRect); | |||
drawRoundedLines = GuiCheckBox((Rectangle){ 640, 350, 20, 20 }, "DrawRoundedLines", drawRoundedLines); | |||
drawRect = GuiCheckBox((Rectangle){ 640, 380, 20, 20}, "DrawRect", drawRect); | |||
//------------------------------------------------------------------------------ | |||
DrawText(FormatText("MODE: %s", (segments >= 4)? "MANUAL" : "AUTO"), 640, 280, 10, (segments >= 4)? MAROON : DARKGRAY); | |||
DrawFPS(10, 10); | |||
EndDrawing(); | |||
//---------------------------------------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//-------------------------------------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//-------------------------------------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,92 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - draw ring (with gui options) | |||
* | |||
* This example has been created using raylib 2.5 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2019 Vlad Adrian (@Demizdor) and Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include <raylib.h> | |||
#define RAYGUI_IMPLEMENTATION | |||
#include "raygui.h" // Required for GUI controls | |||
int main() | |||
{ | |||
// Initialization | |||
//-------------------------------------------------------------------------------------- | |||
const int screenWidth = 800; | |||
const int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - draw ring"); | |||
Vector2 center = {(GetScreenWidth() - 300)/2, GetScreenHeight()/2 }; | |||
float innerRadius = 80.0f; | |||
float outerRadius = 190.0f; | |||
int startAngle = 0; | |||
int endAngle = 360; | |||
int segments = 0; | |||
bool drawRing = true; | |||
bool drawRingLines = false; | |||
bool drawCircleLines = false; | |||
SetTargetFPS(60); | |||
//-------------------------------------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//---------------------------------------------------------------------------------- | |||
// NOTE: All variables update happens inside GUI control functions | |||
//---------------------------------------------------------------------------------- | |||
// Draw | |||
//---------------------------------------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
DrawLine(500, 0, 500, GetScreenHeight(), Fade(LIGHTGRAY, 0.6f)); | |||
DrawRectangle(500, 0, GetScreenWidth() - 500, GetScreenHeight(), Fade(LIGHTGRAY, 0.3f)); | |||
if (drawRing) DrawRing(center, innerRadius, outerRadius, startAngle, endAngle, segments, Fade(MAROON, 0.3)); | |||
if (drawRingLines) DrawRingLines(center, innerRadius, outerRadius, startAngle, endAngle, segments, Fade(BLACK, 0.4)); | |||
if (drawCircleLines) DrawCircleSectorLines(center, outerRadius, startAngle, endAngle, segments, Fade(BLACK, 0.4)); | |||
// Draw GUI controls | |||
//------------------------------------------------------------------------------ | |||
startAngle = GuiSliderBar((Rectangle){ 600, 40, 120, 20 }, "StartAngle", startAngle, -450, 450, true); | |||
endAngle = GuiSliderBar((Rectangle){ 600, 70, 120, 20 }, "EndAngle", endAngle, -450, 450, true); | |||
innerRadius = GuiSliderBar((Rectangle){ 600, 140, 120, 20 }, "InnerRadius", innerRadius, 0, 100, true); | |||
outerRadius = GuiSliderBar((Rectangle){ 600, 170, 120, 20 }, "OuterRadius", outerRadius, 0, 200, true); | |||
segments = GuiSliderBar((Rectangle){ 600, 240, 120, 20 }, "Segments", segments, 0, 100, true); | |||
drawRing = GuiCheckBox((Rectangle){ 600, 320, 20, 20 }, "Draw Ring", drawRing); | |||
drawRingLines = GuiCheckBox((Rectangle){ 600, 350, 20, 20 }, "Draw RingLines", drawRingLines); | |||
drawCircleLines = GuiCheckBox((Rectangle){ 600, 380, 20, 20 }, "Draw CircleLines", drawCircleLines); | |||
//------------------------------------------------------------------------------ | |||
DrawText(FormatText("MODE: %s", (segments >= 4)? "MANUAL" : "AUTO"), 600, 270, 10, (segments >= 4)? MAROON : DARKGRAY); | |||
DrawFPS(10, 10); | |||
EndDrawing(); | |||
//---------------------------------------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//-------------------------------------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//-------------------------------------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,136 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - easings box anim | |||
* | |||
* This example has been created using raylib 2.5 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include "raylib.h" | |||
#include "easings.h" // Required for easing functions | |||
int main() | |||
{ | |||
// Initialization | |||
//-------------------------------------------------------------------------------------- | |||
const int screenWidth = 800; | |||
const int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings box anim"); | |||
// Box variables to be animated with easings | |||
Rectangle rec = { GetScreenWidth()/2, -100, 100, 100 }; | |||
float rotation = 0.0f; | |||
float alpha = 1.0f; | |||
int state = 0; | |||
int framesCounter = 0; | |||
SetTargetFPS(60); | |||
//-------------------------------------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//---------------------------------------------------------------------------------- | |||
switch (state) | |||
{ | |||
case 0: // Move box down to center of screen | |||
{ | |||
framesCounter++; | |||
// NOTE: Remember that 3rd parameter of easing function refers to | |||
// desired value variation, do not confuse it with expected final value! | |||
rec.y = EaseElasticOut(framesCounter, -100, GetScreenHeight()/2 + 100, 120); | |||
if (framesCounter >= 120) | |||
{ | |||
framesCounter = 0; | |||
state = 1; | |||
} | |||
} break; | |||
case 1: // Scale box to an horizontal bar | |||
{ | |||
framesCounter++; | |||
rec.height = EaseBounceOut(framesCounter, 100, -90, 120); | |||
rec.width = EaseBounceOut(framesCounter, 100, GetScreenWidth(), 120); | |||
if (framesCounter >= 120) | |||
{ | |||
framesCounter = 0; | |||
state = 2; | |||
} | |||
} break; | |||
case 2: // Rotate horizontal bar rectangle | |||
{ | |||
framesCounter++; | |||
rotation = EaseQuadOut(framesCounter, 0.0f, 270.0f, 240); | |||
if (framesCounter >= 240) | |||
{ | |||
framesCounter = 0; | |||
state = 3; | |||
} | |||
} break; | |||
case 3: // Increase bar size to fill all screen | |||
{ | |||
framesCounter++; | |||
rec.height = EaseCircOut(framesCounter, 10, GetScreenWidth(), 120); | |||
if (framesCounter >= 120) | |||
{ | |||
framesCounter = 0; | |||
state = 4; | |||
} | |||
} break; | |||
case 4: // Fade out animation | |||
{ | |||
framesCounter++; | |||
alpha = EaseSineOut(framesCounter, 1.0f, -1.0f, 160); | |||
if (framesCounter >= 160) | |||
{ | |||
framesCounter = 0; | |||
state = 5; | |||
} | |||
} break; | |||
default: break; | |||
} | |||
// Reset animation at any moment | |||
if (IsKeyPressed(KEY_SPACE)) | |||
{ | |||
rec = (Rectangle){ GetScreenWidth()/2, -100, 100, 100 }; | |||
rotation = 0.0f; | |||
alpha = 1.0f; | |||
state = 0; | |||
framesCounter = 0; | |||
} | |||
//---------------------------------------------------------------------------------- | |||
// Draw | |||
//---------------------------------------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
DrawRectanglePro(rec, (Vector2){ rec.width/2, rec.height/2 }, rotation, Fade(BLACK, alpha)); | |||
DrawText("PRESS [SPACE] TO RESET BOX ANIMATION!", 10, GetScreenHeight() - 25, 20, LIGHTGRAY); | |||
EndDrawing(); | |||
//---------------------------------------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//-------------------------------------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//-------------------------------------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,110 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - easings ball anim | |||
* | |||
* This example has been created using raylib 2.5 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include "raylib.h" | |||
#include "easings.h" // Required for easing functions | |||
int main() | |||
{ | |||
// Initialization | |||
//-------------------------------------------------------------------------------------- | |||
const int screenWidth = 800; | |||
const int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings ball anim"); | |||
// Ball variable value to be animated with easings | |||
int ballPositionX = -100; | |||
int ballRadius = 20; | |||
float ballAlpha = 0.0f; | |||
int state = 0; | |||
int framesCounter = 0; | |||
SetTargetFPS(60); | |||
//-------------------------------------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//---------------------------------------------------------------------------------- | |||
if (state == 0) // Move ball position X with easing | |||
{ | |||
framesCounter++; | |||
ballPositionX = EaseElasticOut(framesCounter, -100, screenWidth/2 + 100, 120); | |||
if (framesCounter >= 120) | |||
{ | |||
framesCounter = 0; | |||
state = 1; | |||
} | |||
} | |||
else if (state == 1) // Increase ball radius with easing | |||
{ | |||
framesCounter++; | |||
ballRadius = EaseElasticIn(framesCounter, 20, 500, 200); | |||
if (framesCounter >= 200) | |||
{ | |||
framesCounter = 0; | |||
state = 2; | |||
} | |||
} | |||
else if (state == 2) // Change ball alpha with easing (background color blending) | |||
{ | |||
framesCounter++; | |||
ballAlpha = EaseCubicOut(framesCounter, 0.0f, 1.0f, 200); | |||
if (framesCounter >= 200) | |||
{ | |||
framesCounter = 0; | |||
state = 3; | |||
} | |||
} | |||
else if (state == 3) // Reset state to play again | |||
{ | |||
if (IsKeyPressed(KEY_ENTER)) | |||
{ | |||
// Reset required variables to play again | |||
ballPositionX = -100; | |||
ballRadius = 20; | |||
ballAlpha = 0.0f; | |||
state = 0; | |||
} | |||
} | |||
if (IsKeyPressed(KEY_R)) framesCounter = 0; | |||
//---------------------------------------------------------------------------------- | |||
// Draw | |||
//---------------------------------------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
if (state >= 2) DrawRectangle(0, 0, screenWidth, screenHeight, GREEN); | |||
DrawCircle(ballPositionX, 200, ballRadius, Fade(RED, 1.0f - ballAlpha)); | |||
if (state == 3) DrawText("PRESS [ENTER] TO PLAY AGAIN!", 240, 200, 20, BLACK); | |||
EndDrawing(); | |||
//---------------------------------------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//-------------------------------------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//-------------------------------------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,118 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - easings rectangle array | |||
* | |||
* NOTE: This example requires 'easings.h' library, provided on raylib/src. Just copy | |||
* the library to same directory as example or make sure it's available on include path. | |||
* | |||
* This example has been created using raylib 2.0 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include "raylib.h" | |||
#include "easings.h" // Required for easing functions | |||
#define RECS_WIDTH 50 | |||
#define RECS_HEIGHT 50 | |||
#define MAX_RECS_X 800/RECS_WIDTH | |||
#define MAX_RECS_Y 450/RECS_HEIGHT | |||
#define PLAY_TIME_IN_FRAMES 240 // At 60 fps = 4 seconds | |||
int main() | |||
{ | |||
// Initialization | |||
//-------------------------------------------------------------------------------------- | |||
int screenWidth = 800; | |||
int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings rectangle array"); | |||
Rectangle recs[MAX_RECS_X*MAX_RECS_Y]; | |||
for (int y = 0; y < MAX_RECS_Y; y++) | |||
{ | |||
for (int x = 0; x < MAX_RECS_X; x++) | |||
{ | |||
recs[y*MAX_RECS_X + x].x = RECS_WIDTH/2 + RECS_WIDTH*x; | |||
recs[y*MAX_RECS_X + x].y = RECS_HEIGHT/2 + RECS_HEIGHT*y; | |||
recs[y*MAX_RECS_X + x].width = RECS_WIDTH; | |||
recs[y*MAX_RECS_X + x].height = RECS_HEIGHT; | |||
} | |||
} | |||
float rotation = 0.0f; | |||
int framesCounter = 0; | |||
int state = 0; // Rectangles animation state: 0-Playing, 1-Finished | |||
SetTargetFPS(60); | |||
//-------------------------------------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//---------------------------------------------------------------------------------- | |||
if (state == 0) | |||
{ | |||
framesCounter++; | |||
for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++) | |||
{ | |||
recs[i].height = EaseCircOut(framesCounter, RECS_HEIGHT, -RECS_HEIGHT, PLAY_TIME_IN_FRAMES); | |||
recs[i].width = EaseCircOut(framesCounter, RECS_WIDTH, -RECS_WIDTH, PLAY_TIME_IN_FRAMES); | |||
if (recs[i].height < 0) recs[i].height = 0; | |||
if (recs[i].width < 0) recs[i].width = 0; | |||
if ((recs[i].height == 0) && (recs[i].width == 0)) state = 1; // Finish playing | |||
rotation = EaseLinearIn(framesCounter, 0.0f, 360.0f, PLAY_TIME_IN_FRAMES); | |||
} | |||
} | |||
else if ((state == 1) && IsKeyPressed(KEY_SPACE)) | |||
{ | |||
// When animation has finished, press space to restart | |||
framesCounter = 0; | |||
for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++) | |||
{ | |||
recs[i].height = RECS_HEIGHT; | |||
recs[i].width = RECS_WIDTH; | |||
} | |||
state = 0; | |||
} | |||
//---------------------------------------------------------------------------------- | |||
// Draw | |||
//---------------------------------------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
if (state == 0) | |||
{ | |||
for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++) | |||
{ | |||
DrawRectanglePro(recs[i], (Vector2){ recs[i].width/2, recs[i].height/2 }, rotation, RED); | |||
} | |||
} | |||
else if (state == 1) DrawText("PRESS [SPACE] TO PLAY AGAIN!", 240, 200, 20, GRAY); | |||
EndDrawing(); | |||
//---------------------------------------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//-------------------------------------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//-------------------------------------------------------------------------------------- | |||
return 0; | |||
} |
@ -0,0 +1,104 @@ | |||
/******************************************************************************************* | |||
* | |||
* raylib [shapes] example - following eyes | |||
* | |||
* This example has been created using raylib 2.5 (www.raylib.com) | |||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |||
* | |||
* Copyright (c) 2013-2019 Ramon Santamaria (@raysan5) | |||
* | |||
********************************************************************************************/ | |||
#include "raylib.h" | |||
#include <math.h> // Required for: atan2f() | |||
int main() | |||
{ | |||
// Initialization | |||
//-------------------------------------------------------------------------------------- | |||
const int screenWidth = 800; | |||
const int screenHeight = 450; | |||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - following eyes"); | |||
Vector2 scleraLeftPosition = { GetScreenWidth()/2 - 100, GetScreenHeight()/2 }; | |||
Vector2 scleraRightPosition = { GetScreenWidth()/2 + 100, GetScreenHeight()/2 }; | |||
float scleraRadius = 80; | |||
Vector2 irisLeftPosition = { GetScreenWidth()/2 - 100, GetScreenHeight()/2 }; | |||
Vector2 irisRightPosition = { GetScreenWidth()/2 + 100, GetScreenHeight()/2}; | |||
float irisRadius = 24; | |||
float angle; | |||
float dx, dy, dxx, dyy; | |||
SetTargetFPS(60); | |||
//-------------------------------------------------------------------------------------- | |||
// Main game loop | |||
while (!WindowShouldClose()) // Detect window close button or ESC key | |||
{ | |||
// Update | |||
//---------------------------------------------------------------------------------- | |||
irisLeftPosition = GetMousePosition(); | |||
irisRightPosition = GetMousePosition(); | |||
// Check not inside the left eye sclera | |||
if (!CheckCollisionPointCircle(irisLeftPosition, scleraLeftPosition, scleraRadius - 20)) | |||
{ | |||
dx = irisLeftPosition.x - scleraLeftPosition.x; | |||
dy = irisLeftPosition.y - scleraLeftPosition.y; | |||
angle = atan2f(dy, dx); | |||
dxx = (scleraRadius - irisRadius)*cosf(angle); | |||
dyy = (scleraRadius - irisRadius)*sinf(angle); | |||
irisLeftPosition.x = scleraLeftPosition.x + dxx; | |||
irisLeftPosition.y = scleraLeftPosition.y + dyy; | |||
} | |||
// Check not inside the right eye sclera | |||
if (!CheckCollisionPointCircle(irisRightPosition, scleraRightPosition, scleraRadius - 20)) | |||
{ | |||
dx = irisRightPosition.x - scleraRightPosition.x; | |||
dy = irisRightPosition.y - scleraRightPosition.y; | |||
angle = atan2f(dy, dx); | |||
dxx = (scleraRadius - irisRadius)*cosf(angle); | |||
dyy = (scleraRadius - irisRadius)*sinf(angle); | |||
irisRightPosition.x = scleraRightPosition.x + dxx; | |||
irisRightPosition.y = scleraRightPosition.y + dyy; | |||
} | |||
//---------------------------------------------------------------------------------- | |||
// Draw | |||
//---------------------------------------------------------------------------------- | |||
BeginDrawing(); | |||
ClearBackground(RAYWHITE); | |||
DrawCircleV(scleraLeftPosition, scleraRadius, LIGHTGRAY); | |||
DrawCircleV(irisLeftPosition, irisRadius, BROWN); | |||
DrawCircleV(irisLeftPosition, 10, BLACK); | |||
DrawCircleV(scleraRightPosition, scleraRadius, LIGHTGRAY); | |||
DrawCircleV(irisRightPosition, irisRadius, DARKGREEN); | |||
DrawCircleV(irisRightPosition, 10, BLACK); | |||
DrawFPS(10, 10); | |||
EndDrawing(); | |||
//---------------------------------------------------------------------------------- | |||
} | |||
// De-Initialization | |||
//-------------------------------------------------------------------------------------- | |||
CloseWindow(); // Close window and OpenGL context | |||
//-------------------------------------------------------------------------------------- | |||
return 0; | |||
} |