subrepo:
  subdir:   "OTRGui"
  merged:   "a6066a251"
upstream:
  origin:   "https://github.com/HarbourMasters/otrgui.git"
  branch:   "master"
  commit:   "a6066a251"
git-subrepo:
  version:  "0.4.1"
  origin:   "???"
  commit:   "???"
This commit is contained in:
M4xw 2022-03-22 02:53:51 +01:00
commit f52a2a6406
1018 changed files with 511803 additions and 0 deletions

View file

@ -0,0 +1,78 @@
/*******************************************************************************************
*
* raylib [shapes] example - Draw basic shapes 2d (rectangle, circle, line...)
*
* 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) 2014 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - basic shapes drawing");
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
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("some basic shapes available on raylib", 20, 20, 20, DARKGRAY);
// Circle shapes and lines
DrawCircle(screenWidth/5, 120, 35, DARKBLUE);
DrawCircleGradient(screenWidth/5, 220, 60, GREEN, SKYBLUE);
DrawCircleLines(screenWidth/5, 340, 80, DARKBLUE);
// Rectangle shapes and ines
DrawRectangle(screenWidth/4*2 - 60, 100, 120, 60, RED);
DrawRectangleGradientH(screenWidth/4*2 - 90, 170, 180, 130, MAROON, GOLD);
DrawRectangleLines(screenWidth/4*2 - 40, 320, 80, 60, ORANGE); // NOTE: Uses QUADS internally, not lines
// Triangle shapes and lines
DrawTriangle((Vector2){screenWidth/4.0f *3.0f, 80.0f},
(Vector2){screenWidth/4.0f *3.0f - 60.0f, 150.0f},
(Vector2){screenWidth/4.0f *3.0f + 60.0f, 150.0f}, VIOLET);
DrawTriangleLines((Vector2){screenWidth/4.0f*3.0f, 160.0f},
(Vector2){screenWidth/4.0f*3.0f - 20.0f, 230.0f},
(Vector2){screenWidth/4.0f*3.0f + 20.0f, 230.0f}, DARKBLUE);
// Polygon shapes and lines
DrawPoly((Vector2){screenWidth/4.0f*3, 320}, 6, 80, 0, BROWN);
DrawPolyLinesEx((Vector2){screenWidth/4.0f*3, 320}, 6, 80, 0, 6, BEIGE);
// NOTE: We draw all LINES based shapes together to optimize internal drawing,
// this way, all LINES are rendered in a single draw pass
DrawLine(18, 42, screenWidth - 18, 42, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View file

@ -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(void)
{
// Initialization
//---------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball");
Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
Vector2 ballSpeed = { 5.0f, 4.0f };
int ballRadius = 20;
bool pause = 0;
int framesCounter = 0;
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
//-----------------------------------------------------
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, (float)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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -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(void)
{
// Initialization
//---------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - collision area");
// Box A: Moving box
Rectangle boxA = { 10, GetScreenHeight()/2.0f - 50, 200, 100 };
int boxASpeedX = 4;
// Box B: Mouse moved box
Rectangle boxB = { GetScreenWidth()/2.0f - 30, GetScreenHeight()/2.0f - 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); // Set our game to run at 60 frames-per-second
//----------------------------------------------------------
// 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 = (float)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(TextFormat("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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,99 @@
/*******************************************************************************************
*
* raylib [shapes] example - Colors palette
*
* 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"
#define MAX_COLORS_COUNT 21 // Number of colors available
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - colors palette");
Color colors[MAX_COLORS_COUNT] = {
DARKGRAY, MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, DARKBROWN,
GRAY, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK, YELLOW,
GREEN, SKYBLUE, PURPLE, BEIGE };
const char *colorNames[MAX_COLORS_COUNT] = {
"DARKGRAY", "MAROON", "ORANGE", "DARKGREEN", "DARKBLUE", "DARKPURPLE",
"DARKBROWN", "GRAY", "RED", "GOLD", "LIME", "BLUE", "VIOLET", "BROWN",
"LIGHTGRAY", "PINK", "YELLOW", "GREEN", "SKYBLUE", "PURPLE", "BEIGE" };
Rectangle colorsRecs[MAX_COLORS_COUNT] = { 0 }; // Rectangles array
// Fills colorsRecs data (for every rectangle)
for (int i = 0; i < MAX_COLORS_COUNT; i++)
{
colorsRecs[i].x = 20.0f + 100.0f *(i%7) + 10.0f *(i%7);
colorsRecs[i].y = 80.0f + 100.0f *(i/7) + 10.0f *(i/7);
colorsRecs[i].width = 100.0f;
colorsRecs[i].height = 100.0f;
}
int colorState[MAX_COLORS_COUNT] = { 0 }; // Color state: 0-DEFAULT, 1-MOUSE_HOVER
Vector2 mousePoint = { 0.0f, 0.0f };
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
//----------------------------------------------------------------------------------
mousePoint = GetMousePosition();
for (int i = 0; i < MAX_COLORS_COUNT; i++)
{
if (CheckCollisionPointRec(mousePoint, colorsRecs[i])) colorState[i] = 1;
else colorState[i] = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("raylib colors palette", 28, 42, 20, BLACK);
DrawText("press SPACE to see all colors", GetScreenWidth() - 180, GetScreenHeight() - 40, 10, GRAY);
for (int i = 0; i < MAX_COLORS_COUNT; i++) // Draw all rectangles
{
DrawRectangleRec(colorsRecs[i], Fade(colors[i], colorState[i]? 0.6f : 1.0f));
if (IsKeyDown(KEY_SPACE) || colorState[i])
{
DrawRectangle((int)colorsRecs[i].x, (int)(colorsRecs[i].y + colorsRecs[i].height - 26), (int)colorsRecs[i].width, 20, BLACK);
DrawRectangleLinesEx(colorsRecs[i], 6, Fade(BLACK, 0.3f));
DrawText(colorNames[i], (int)(colorsRecs[i].x + colorsRecs[i].width - MeasureText(colorNames[i], 10) - 12),
(int)(colorsRecs[i].y + colorsRecs[i].height - 20), 10, colors[i]);
}
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View file

@ -0,0 +1,83 @@
/*******************************************************************************************
*
* 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)
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include <raylib.h>
#define RAYGUI_IMPLEMENTATION
#include "extras/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 circle sector");
Vector2 center = {(GetScreenWidth() - 300)/2.0f, GetScreenHeight()/2.0f };
float outerRadius = 180.0f;
float startAngle = 0.0f;
float endAngle = 180.0f;
int segments = 0;
int minSegments = 4;
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
//----------------------------------------------------------------------------------
// 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.3f));
DrawCircleSectorLines(center, outerRadius, startAngle, endAngle, segments, Fade(MAROON, 0.6f));
// Draw GUI controls
//------------------------------------------------------------------------------
startAngle = GuiSliderBar((Rectangle){ 600, 40, 120, 20}, "StartAngle", NULL, startAngle, 0, 720);
endAngle = GuiSliderBar((Rectangle){ 600, 70, 120, 20}, "EndAngle", NULL, endAngle, 0, 720);
outerRadius = GuiSliderBar((Rectangle){ 600, 140, 120, 20}, "Radius", NULL, outerRadius, 0, 200);
segments = (int)GuiSliderBar((Rectangle){ 600, 170, 120, 20}, "Segments", NULL, (float)segments, 0, 100);
//------------------------------------------------------------------------------
minSegments = (int)ceilf((endAngle - startAngle) / 90);
DrawText(TextFormat("MODE: %s", (segments >= minSegments)? "MANUAL" : "AUTO"), 600, 200, 10, (segments >= minSegments)? MAROON : DARKGRAY);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View file

@ -0,0 +1,89 @@
/*******************************************************************************************
*
* 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)
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include <raylib.h>
#define RAYGUI_IMPLEMENTATION
#include "extras/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); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
Rectangle rec = { ((float)GetScreenWidth() - width - 250)/2, (GetScreenHeight() - height)/2.0f, (float)width, (float)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.6f));
if (drawRoundedRect) DrawRectangleRounded(rec, roundness, segments, Fade(MAROON, 0.2f));
if (drawRoundedLines) DrawRectangleRoundedLines(rec,roundness, segments, (float)lineThick, Fade(MAROON, 0.4f));
// Draw GUI controls
//------------------------------------------------------------------------------
width = (int)GuiSliderBar((Rectangle){ 640, 40, 105, 20 }, "Width", NULL, (float)width, 0, (float)GetScreenWidth() - 300);
height = (int)GuiSliderBar((Rectangle){ 640, 70, 105, 20 }, "Height", NULL, (float)height, 0, (float)GetScreenHeight() - 50);
roundness = GuiSliderBar((Rectangle){ 640, 140, 105, 20 }, "Roundness", NULL, roundness, 0.0f, 1.0f);
lineThick = (int)GuiSliderBar((Rectangle){ 640, 170, 105, 20 }, "Thickness", NULL, (float)lineThick, 0, 20);
segments = (int)GuiSliderBar((Rectangle){ 640, 240, 105, 20}, "Segments", NULL, (float)segments, 0, 60);
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(TextFormat("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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View file

@ -0,0 +1,96 @@
/*******************************************************************************************
*
* 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)
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include <raylib.h>
#define RAYGUI_IMPLEMENTATION
#include "extras/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 ring");
Vector2 center = {(GetScreenWidth() - 300)/2.0f, GetScreenHeight()/2.0f };
float innerRadius = 80.0f;
float outerRadius = 190.0f;
float startAngle = 0.0f;
float endAngle = 360.0f;
int segments = 0;
int minSegments = 4;
bool drawRing = true;
bool drawRingLines = false;
bool drawCircleLines = false;
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
//----------------------------------------------------------------------------------
// 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.3f));
if (drawRingLines) DrawRingLines(center, innerRadius, outerRadius, startAngle, endAngle, segments, Fade(BLACK, 0.4f));
if (drawCircleLines) DrawCircleSectorLines(center, outerRadius, startAngle, endAngle, segments, Fade(BLACK, 0.4f));
// Draw GUI controls
//------------------------------------------------------------------------------
startAngle = GuiSliderBar((Rectangle){ 600, 40, 120, 20 }, "StartAngle", NULL, startAngle, -450, 450);
endAngle = GuiSliderBar((Rectangle){ 600, 70, 120, 20 }, "EndAngle", NULL, endAngle, -450, 450);
innerRadius = GuiSliderBar((Rectangle){ 600, 140, 120, 20 }, "InnerRadius", NULL, innerRadius, 0, 100);
outerRadius = GuiSliderBar((Rectangle){ 600, 170, 120, 20 }, "OuterRadius", NULL, outerRadius, 0, 200);
segments = (int)GuiSliderBar((Rectangle){ 600, 240, 120, 20 }, "Segments", NULL, (float)segments, 0, 100);
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);
//------------------------------------------------------------------------------
int minSegments = (int)ceilf((endAngle - startAngle) / 90);
DrawText(TextFormat("MODE: %s", (segments >= minSegments)? "MANUAL" : "AUTO"), 600, 270, 10, (segments >= minSegments)? MAROON : DARKGRAY);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View file

@ -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 "extras/easings.h" // Required for easing functions
int main(void)
{
// 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); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (state == 0) // Move ball position X with easing
{
framesCounter++;
ballPositionX = (int)EaseElasticOut((float)framesCounter, -100, screenWidth/2.0f + 100, 120);
if (framesCounter >= 120)
{
framesCounter = 0;
state = 1;
}
}
else if (state == 1) // Increase ball radius with easing
{
framesCounter++;
ballRadius = (int)EaseElasticIn((float)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((float)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, (float)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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -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 "extras/easings.h" // Required for easing functions
int main(void)
{
// 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.0f, -100, 100, 100 };
float rotation = 0.0f;
float alpha = 1.0f;
int state = 0;
int framesCounter = 0;
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
//----------------------------------------------------------------------------------
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((float)framesCounter, -100, GetScreenHeight()/2.0f + 100, 120);
if (framesCounter >= 120)
{
framesCounter = 0;
state = 1;
}
} break;
case 1: // Scale box to an horizontal bar
{
framesCounter++;
rec.height = EaseBounceOut((float)framesCounter, 100, -90, 120);
rec.width = EaseBounceOut((float)framesCounter, 100, (float)GetScreenWidth(), 120);
if (framesCounter >= 120)
{
framesCounter = 0;
state = 2;
}
} break;
case 2: // Rotate horizontal bar rectangle
{
framesCounter++;
rotation = EaseQuadOut((float)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((float)framesCounter, 10, (float)GetScreenWidth(), 120);
if (framesCounter >= 120)
{
framesCounter = 0;
state = 4;
}
} break;
case 4: // Fade out animation
{
framesCounter++;
alpha = EaseSineOut((float)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.0f, -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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -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 "extras/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(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings rectangle array");
Rectangle recs[MAX_RECS_X*MAX_RECS_Y] = { 0 };
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.0f + RECS_WIDTH*x;
recs[y*MAX_RECS_X + x].y = RECS_HEIGHT/2.0f + 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); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// 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((float)framesCounter, RECS_HEIGHT, -RECS_HEIGHT, PLAY_TIME_IN_FRAMES);
recs[i].width = EaseCircOut((float)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((float)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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View file

@ -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(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - following eyes");
Vector2 scleraLeftPosition = { GetScreenWidth()/2.0f - 100.0f, GetScreenHeight()/2.0f };
Vector2 scleraRightPosition = { GetScreenWidth()/2.0f + 100.0f, GetScreenHeight()/2.0f };
float scleraRadius = 80;
Vector2 irisLeftPosition = { GetScreenWidth()/2.0f - 100.0f, GetScreenHeight()/2.0f };
Vector2 irisRightPosition = { GetScreenWidth()/2.0f + 100.0f, GetScreenHeight()/2.0f };
float irisRadius = 24;
float angle = 0.0f;
float dx = 0.0f, dy = 0.0f, dxx = 0.0f, dyy = 0.0f;
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
//----------------------------------------------------------------------------------
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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -0,0 +1,59 @@
/*******************************************************************************************
*
* raylib [shapes] example - Cubic-bezier lines
*
* This example has been created using raylib 1.7 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - cubic-bezier lines");
Vector2 start = { 0, 0 };
Vector2 end = { (float)screenWidth, (float)screenHeight };
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
//----------------------------------------------------------------------------------
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) start = GetMousePosition();
else if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) end = GetMousePosition();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("USE MOUSE LEFT-RIGHT CLICK to DEFINE LINE START and END POINTS", 15, 20, 20, GRAY);
DrawLineBezier(start, end, 2.0f, RED);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View file

@ -0,0 +1,56 @@
/*******************************************************************************************
*
* raylib [shapes] example - Draw raylib logo using basic shapes
*
* 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) 2014 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo using shapes");
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
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangle(screenWidth/2 - 128, screenHeight/2 - 128, 256, 256, BLACK);
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, RAYWHITE);
DrawText("raylib", screenWidth/2 - 44, screenHeight/2 + 48, 50, BLACK);
DrawText("this is NOT a texture!", 350, 370, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -0,0 +1,160 @@
/*******************************************************************************************
*
* raylib [shapes] example - raylib logo animation
*
* This example has been created using raylib 2.3 (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(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - raylib logo animation");
int logoPositionX = screenWidth/2 - 128;
int logoPositionY = screenHeight/2 - 128;
int framesCounter = 0;
int lettersCount = 0;
int topSideRecWidth = 16;
int leftSideRecHeight = 16;
int bottomSideRecWidth = 16;
int rightSideRecHeight = 16;
int state = 0; // Tracking animation states (State Machine)
float alpha = 1.0f; // Useful for fading
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
//----------------------------------------------------------------------------------
if (state == 0) // State 0: Small box blinking
{
framesCounter++;
if (framesCounter == 120)
{
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;
if (topSideRecWidth == 256) state = 2;
}
else if (state == 2) // State 2: Bottom and right bars growing
{
bottomSideRecWidth += 4;
rightSideRecHeight += 4;
if (bottomSideRecWidth == 256) state = 3;
}
else if (state == 3) // State 3: Letters appearing (one by one)
{
framesCounter++;
if (framesCounter/12) // Every 12 frames, one more letter!
{
lettersCount++;
framesCounter = 0;
}
if (lettersCount >= 10) // When all letters have appeared, just fade out everything
{
alpha -= 0.02f;
if (alpha <= 0.0f)
{
alpha = 0.0f;
state = 4;
}
}
}
else if (state == 4) // State 4: Reset and Replay
{
if (IsKeyPressed(KEY_R))
{
framesCounter = 0;
lettersCount = 0;
topSideRecWidth = 16;
leftSideRecHeight = 16;
bottomSideRecWidth = 16;
rightSideRecHeight = 16;
alpha = 1.0f;
state = 0; // Return to State 0
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
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);
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(GetScreenWidth()/2 - 112, GetScreenHeight()/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
DrawText(TextSubtext("raylib", 0, lettersCount), GetScreenWidth()/2 - 44, GetScreenHeight()/2 + 48, 50, Fade(BLACK, alpha));
}
else if (state == 4)
{
DrawText("[R] REPLAY", 340, 200, 20, GRAY);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View file

@ -0,0 +1,94 @@
/*******************************************************************************************
*
* raylib [shapes] example - rectangle scaling by mouse
*
* 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)
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2018 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#define MOUSE_SCALE_MARK_SIZE 12
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle scaling mouse");
Rectangle rec = { 100, 100, 200, 80 };
Vector2 mousePosition = { 0 };
bool mouseScaleReady = false;
bool mouseScaleMode = false;
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
//----------------------------------------------------------------------------------
mousePosition = GetMousePosition();
if (CheckCollisionPointRec(mousePosition, rec) &&
CheckCollisionPointRec(mousePosition, (Rectangle){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE, MOUSE_SCALE_MARK_SIZE }))
{
mouseScaleReady = true;
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) mouseScaleMode = true;
}
else mouseScaleReady = false;
if (mouseScaleMode)
{
mouseScaleReady = true;
rec.width = (mousePosition.x - rec.x);
rec.height = (mousePosition.y - rec.y);
if (rec.width < MOUSE_SCALE_MARK_SIZE) rec.width = MOUSE_SCALE_MARK_SIZE;
if (rec.height < MOUSE_SCALE_MARK_SIZE) rec.height = MOUSE_SCALE_MARK_SIZE;
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) mouseScaleMode = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Scale rectangle dragging from bottom-right corner!", 10, 10, 20, GRAY);
DrawRectangleRec(rec, Fade(GREEN, 0.5f));
if (mouseScaleReady)
{
DrawRectangleLinesEx(rec, 1, RED);
DrawTriangle((Vector2){ rec.x + rec.width - MOUSE_SCALE_MARK_SIZE, rec.y + rec.height },
(Vector2){ rec.x + rec.width, rec.y + rec.height },
(Vector2){ rec.x + rec.width, rec.y + rec.height - MOUSE_SCALE_MARK_SIZE }, RED);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,350 @@
/*******************************************************************************************
*
* raylib [shapes] example - top down lights
*
* This example has been created using raylib 4.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2022 Jeffery Myers (@JeffM2501)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#include "rlgl.h"
// Custom Blend Modes
#define RLGL_SRC_ALPHA 0x0302
#define RLGL_MIN 0x8007
#define RLGL_MAX 0x8008
#define MAX_BOXES 20
#define MAX_SHADOWS MAX_BOXES*3 // MAX_BOXES *3. Each box can cast up to two shadow volumes for the edges it is away from, and one for the box itself
#define MAX_LIGHTS 16
// Shadow geometry type
typedef struct ShadowGeometry {
Vector2 vertices[4];
} ShadowGeometry;
// Light info type
typedef struct LightInfo {
bool active; // Is this light slot active?
bool dirty; // Does this light need to be updated?
bool valid; // Is this light in a valid position?
Vector2 position; // Light position
RenderTexture mask; // Alpha mask for the light
float outerRadius; // The distance the light touches
Rectangle bounds; // A cached rectangle of the light bounds to help with culling
ShadowGeometry shadows[MAX_SHADOWS];
int shadowCount;
} LightInfo;
LightInfo lights[MAX_LIGHTS] = { 0 };
// Move a light and mark it as dirty so that we update it's mask next frame
void MoveLight(int slot, float x, float y)
{
lights[slot].dirty = true;
lights[slot].position.x = x;
lights[slot].position.y = y;
// update the cached bounds
lights[slot].bounds.x = x - lights[slot].outerRadius;
lights[slot].bounds.y = y - lights[slot].outerRadius;
}
// Compute a shadow volume for the edge
// It takes the edge and projects it back by the light radius and turns it into a quad
void ComputeShadowVolumeForEdge(int slot, Vector2 sp, Vector2 ep)
{
if (lights[slot].shadowCount >= MAX_SHADOWS) return;
float extension = lights[slot].outerRadius*2;
Vector2 spVector = Vector2Normalize(Vector2Subtract(sp, lights[slot].position));
Vector2 spProjection = Vector2Add(sp, Vector2Scale(spVector, extension));
Vector2 epVector = Vector2Normalize(Vector2Subtract(ep, lights[slot].position));
Vector2 epProjection = Vector2Add(ep, Vector2Scale(epVector, extension));
lights[slot].shadows[lights[slot].shadowCount].vertices[0] = sp;
lights[slot].shadows[lights[slot].shadowCount].vertices[1] = ep;
lights[slot].shadows[lights[slot].shadowCount].vertices[2] = epProjection;
lights[slot].shadows[lights[slot].shadowCount].vertices[3] = spProjection;
lights[slot].shadowCount++;
}
// Draw the light and shadows to the mask for a light
void DrawLightMask(int slot)
{
// Use the light mask
BeginTextureMode(lights[slot].mask);
ClearBackground(WHITE);
// Force the blend mode to only set the alpha of the destination
rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN);
rlSetBlendMode(BLEND_CUSTOM);
// If we are valid, then draw the light radius to the alpha mask
if (lights[slot].valid) DrawCircleGradient((int)lights[slot].position.x, (int)lights[slot].position.y, lights[slot].outerRadius, ColorAlpha(WHITE, 0), WHITE);
rlDrawRenderBatchActive();
// Cut out the shadows from the light radius by forcing the alpha to maximum
rlSetBlendMode(BLEND_ALPHA);
rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MAX);
rlSetBlendMode(BLEND_CUSTOM);
// Draw the shadows to the alpha mask
for (int i = 0; i < lights[slot].shadowCount; i++)
{
DrawTriangleFan(lights[slot].shadows[i].vertices, 4, WHITE);
}
rlDrawRenderBatchActive();
// Go back to normal blend mode
rlSetBlendMode(BLEND_ALPHA);
EndTextureMode();
}
// Setup a light
void SetupLight(int slot, float x, float y, float radius)
{
lights[slot].active = true;
lights[slot].valid = false; // The light must prove it is valid
lights[slot].mask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
lights[slot].outerRadius = radius;
lights[slot].bounds.width = radius * 2;
lights[slot].bounds.height = radius * 2;
MoveLight(slot, x, y);
// Force the render texture to have something in it
DrawLightMask(slot);
}
// See if a light needs to update it's mask
bool UpdateLight(int slot, Rectangle* boxes, int count)
{
if (!lights[slot].active || !lights[slot].dirty) return false;
lights[slot].dirty = false;
lights[slot].shadowCount = 0;
lights[slot].valid = false;
for (int i = 0; i < count; i++)
{
// Are we in a box? if so we are not valid
if (CheckCollisionPointRec(lights[slot].position, boxes[i])) return false;
// If this box is outside our bounds, we can skip it
if (!CheckCollisionRecs(lights[slot].bounds, boxes[i])) continue;
// Check the edges that are on the same side we are, and cast shadow volumes out from them
// Top
Vector2 sp = (Vector2){ boxes[i].x, boxes[i].y };
Vector2 ep = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
if (lights[slot].position.y > ep.y) ComputeShadowVolumeForEdge(slot, sp, ep);
// Right
sp = ep;
ep.y += boxes[i].height;
if (lights[slot].position.x < ep.x) ComputeShadowVolumeForEdge(slot, sp, ep);
// Bottom
sp = ep;
ep.x -= boxes[i].width;
if (lights[slot].position.y < ep.y) ComputeShadowVolumeForEdge(slot, sp, ep);
// Left
sp = ep;
ep.y -= boxes[i].height;
if (lights[slot].position.x > ep.x) ComputeShadowVolumeForEdge(slot, sp, ep);
// The box itself
lights[slot].shadows[lights[slot].shadowCount].vertices[0] = (Vector2){ boxes[i].x, boxes[i].y };
lights[slot].shadows[lights[slot].shadowCount].vertices[1] = (Vector2){ boxes[i].x, boxes[i].y + boxes[i].height };
lights[slot].shadows[lights[slot].shadowCount].vertices[2] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y + boxes[i].height };
lights[slot].shadows[lights[slot].shadowCount].vertices[3] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
lights[slot].shadowCount++;
}
lights[slot].valid = true;
DrawLightMask(slot);
return true;
}
// Set up some boxes
void SetupBoxes(Rectangle *boxes, int *count)
{
boxes[0] = (Rectangle){ 150,80, 40, 40 };
boxes[1] = (Rectangle){ 1200, 700, 40, 40 };
boxes[2] = (Rectangle){ 200, 600, 40, 40 };
boxes[3] = (Rectangle){ 1000, 50, 40, 40 };
boxes[4] = (Rectangle){ 500, 350, 40, 40 };
for (int i = 5; i < MAX_BOXES; i++)
{
boxes[i] = (Rectangle){(float)GetRandomValue(0,GetScreenWidth()), (float)GetRandomValue(0,GetScreenHeight()), (float)GetRandomValue(10,100), (float)GetRandomValue(10,100) };
}
*count = MAX_BOXES;
}
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - top down lights");
// Initialize our 'world' of boxes
int boxCount = 0;
Rectangle boxes[MAX_BOXES] = { 0 };
SetupBoxes(boxes, &boxCount);
// Create a checkerboard ground texture
Image img = GenImageChecked(64, 64, 32, 32, DARKBROWN, DARKGRAY);
Texture2D backgroundTexture = LoadTextureFromImage(img);
UnloadImage(img);
// Create a global light mask to hold all the blended lights
RenderTexture lightMask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
// Setup initial light
SetupLight(0, 600, 400, 300);
int nextLight = 1;
bool showLines = false;
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
//----------------------------------------------------------------------------------
// Drag light 0
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) MoveLight(0, GetMousePosition().x, GetMousePosition().y);
// Make a new light
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) && (nextLight < MAX_LIGHTS))
{
SetupLight(nextLight, GetMousePosition().x, GetMousePosition().y, 200);
nextLight++;
}
// Toggle debug info
if (IsKeyPressed(KEY_F1)) showLines = !showLines;
// Update the lights and keep track if any were dirty so we know if we need to update the master light mask
bool dirtyLights = false;
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (UpdateLight(i, boxes, boxCount)) dirtyLights = true;
}
// Update the light mask
if (dirtyLights)
{
// Build up the light mask
BeginTextureMode(lightMask);
ClearBackground(BLACK);
// Force the blend mode to only set the alpha of the destination
rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN);
rlSetBlendMode(BLEND_CUSTOM);
// Merge in all the light masks
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].active) DrawTextureRec(lights[i].mask.texture, (Rectangle){ 0, 0, (float)GetScreenWidth(), -(float)GetScreenHeight() }, Vector2Zero(), WHITE);
}
rlDrawRenderBatchActive();
// Go back to normal blend
rlSetBlendMode(BLEND_ALPHA);
EndTextureMode();
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BLACK);
// Draw the tile background
DrawTextureRec(backgroundTexture, (Rectangle){ 0, 0, (float)GetScreenWidth(), (float)GetScreenHeight() }, Vector2Zero(), WHITE);
// Overlay the shadows from all the lights
DrawTextureRec(lightMask.texture, (Rectangle){ 0, 0, (float)GetScreenWidth(), -(float)GetScreenHeight() }, Vector2Zero(), ColorAlpha(WHITE, showLines? 0.75f : 1.0f));
// Draw the lights
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].active) DrawCircle((int)lights[i].position.x, (int)lights[i].position.y, 10, (i == 0)? YELLOW : WHITE);
}
if (showLines)
{
for (int s = 0; s < lights[0].shadowCount; s++)
{
DrawTriangleFan(lights[0].shadows[s].vertices, 4, DARKPURPLE);
}
for (int b = 0; b < boxCount; b++)
{
if (CheckCollisionRecs(boxes[b],lights[0].bounds)) DrawRectangleRec(boxes[b], PURPLE);
DrawRectangleLines((int)boxes[b].x, (int)boxes[b].y, (int)boxes[b].width, (int)boxes[b].height, DARKBLUE);
}
DrawText("(F1) Hide Shadow Volumes", 10, 50, 10, GREEN);
}
else
{
DrawText("(F1) Show Shadow Volumes", 10, 50, 10, GREEN);
}
DrawFPS(screenWidth - 80, 10);
DrawText("Drag to move light #1", 10, 10, 10, DARKGREEN);
DrawText("Right click to add new light", 10, 30, 10, DARKGREEN);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(backgroundTexture);
UnloadRenderTexture(lightMask);
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].active) UnloadRenderTexture(lights[i].mask);
}
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB