REVIEWED: Code/Web-Makefile formatting
This commit is contained in:
parent
43db59d1aa
commit
a1896c7a90
9 changed files with 302 additions and 269 deletions
|
@ -1,10 +1,87 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [shapes] example - Rectangle advanced
|
||||
*
|
||||
* Example originally created with raylib 5.5, last time updated with raylib 5.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2024-2025 raylib contributors and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "rlgl.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
// Draw rectangle with rounded edges and horizontal gradient, with options to choose side of roundness
|
||||
// Adapted from both `DrawRectangleRounded` and `DrawRectangleGradientH`
|
||||
void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, float roundnessRight, int segments, Color left, Color right)
|
||||
static void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, float roundnessRight, int segments, Color left, Color right);
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle avanced");
|
||||
|
||||
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 bounds
|
||||
//----------------------------------------------------------------------------------
|
||||
float width = GetScreenWidth()/2.0f, height = GetScreenHeight()/6.0f;
|
||||
Rectangle rec = {
|
||||
GetScreenWidth() / 2.0f - width/2,
|
||||
GetScreenHeight() / 2.0f - 5*(height/2),
|
||||
width, height
|
||||
};
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// Draw All Rectangles with different roundess for each side and different gradients
|
||||
DrawRectangleRoundedGradientH(rec, 0.8f, 0.8f, 36, BLUE, RED);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 0.5f, 1.0f, 36, RED, PINK);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 1.0f, 0.5f, 36, RED, BLUE);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 0.0f, 1.0f, 36, BLUE, BLACK);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 1.0f, 0.0f, 36, BLUE, PINK);
|
||||
EndDrawing();
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Draw rectangle with rounded edges and horizontal gradient, with options to choose side of roundness
|
||||
// NOTE: Adapted from both 'DrawRectangleRounded()' and 'DrawRectangleGradientH()' raylib [rshapes] implementations
|
||||
static void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, float roundnessRight, int segments, Color left, Color right)
|
||||
{
|
||||
// Neither side is rounded
|
||||
if ((roundnessLeft <= 0.0f && roundnessRight <= 0.0f) || (rec.width < 1) || (rec.height < 1 ))
|
||||
|
@ -29,7 +106,7 @@ void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, float rou
|
|||
float stepLength = 90.0f/(float)segments;
|
||||
|
||||
/*
|
||||
Diagram Copied here for reference, original at `DrawRectangleRounded` source code
|
||||
Diagram Copied here for reference, original at 'DrawRectangleRounded()' source code
|
||||
|
||||
P0____________________P1
|
||||
/| |\
|
||||
|
@ -113,12 +190,9 @@ void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, float rou
|
|||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Here we use the `Diagram` to guide ourselves to which point receives what color.
|
||||
//
|
||||
// Here we use the 'Diagram' to guide ourselves to which point receives what color
|
||||
// By choosing the color correctly associated with a pointe the gradient effect
|
||||
// will naturally come from OpenGL interpolation.
|
||||
//
|
||||
// will naturally come from OpenGL interpolation
|
||||
|
||||
// [2] Upper Rectangle
|
||||
rlColor4ub(left.r, left.g, left.b, left.a);
|
||||
|
@ -187,27 +261,25 @@ void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, float rou
|
|||
rlSetTexture(0);
|
||||
#else
|
||||
|
||||
//
|
||||
// Here we use the `Diagram` to guide ourselves to which point receives what color.
|
||||
//
|
||||
// Here we use the 'Diagram' to guide ourselves to which point receives what color.
|
||||
// By choosing the color correctly associated with a pointe the gradient effect
|
||||
// will naturally come from OpenGL interpolation.
|
||||
// But this time instead of Quad, we think in triangles.
|
||||
//
|
||||
|
||||
rlBegin(RL_TRIANGLES);
|
||||
|
||||
// Draw all of the 4 corners: [1] Upper Left Corner, [3] Upper Right Corner, [5] Lower Right Corner, [7] Lower Left Corner
|
||||
for (int k = 0; k < 4; ++k)
|
||||
{
|
||||
Color color;
|
||||
float radius;
|
||||
Color color = { 0 };
|
||||
float radius = 0.0f;
|
||||
if (k == 0) color = left, radius = radiusLeft; // [1] Upper Left Corner
|
||||
if (k == 1) color = right, radius = radiusRight; // [3] Upper Right Corner
|
||||
if (k == 2) color = right, radius = radiusRight; // [5] Lower Right Corner
|
||||
if (k == 3) color = left, radius = radiusLeft; // [7] Lower Left Corner
|
||||
|
||||
float angle = angles[k];
|
||||
const Vector2 center = centers[k];
|
||||
|
||||
for (int i = 0; i < segments; i++)
|
||||
{
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
@ -274,57 +346,3 @@ void DrawRectangleRoundedGradientH(Rectangle rec, float roundnessLeft, float rou
|
|||
rlEnd();
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rectangle avanced");
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update rectangle bounds
|
||||
//----------------------------------------------------------------------------------
|
||||
float width = GetScreenWidth()/2.0f, height = GetScreenHeight()/6.0f;
|
||||
Rectangle rec = {
|
||||
GetScreenWidth() / 2.0f - width/2,
|
||||
GetScreenHeight() / 2.0f - (5)*(height/2),
|
||||
width, height
|
||||
};
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// Draw All Rectangles with different roundess for each side and different gradients
|
||||
DrawRectangleRoundedGradientH(rec, 0.8f, 0.8f, 36, BLUE, RED);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 0.5f, 1.0f, 36, RED, PINK);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 1.0f, 0.5f, 36, RED, BLUE);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 0.0f, 1.0f, 36, BLUE, BLACK);
|
||||
|
||||
rec.y += rec.height + 1;
|
||||
DrawRectangleRoundedGradientH(rec, 1.0f, 0.0f, 36, BLUE, PINK);
|
||||
EndDrawing();
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue