Skip to content

Commit 470a326

Browse files
committed
Update shapes_rlgl_triangle.c
1 parent 74f7112 commit 470a326

File tree

1 file changed

+73
-91
lines changed

1 file changed

+73
-91
lines changed

examples/shapes/shapes_rlgl_triangle.c

Lines changed: 73 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
********************************************************************************************/
1717

1818
#include "raylib.h"
19+
1920
#include "rlgl.h"
2021
#include "raymath.h"
2122

@@ -33,11 +34,13 @@ int main(void)
3334
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rlgl triangle");
3435

3536
// Starting postions and rendered triangle positions
36-
Vector2 startingPositions[] = {{ 400.0f, 150.0f }, { 300.0f, 300.0f }, { 500.0f, 300.0f }};
37-
Vector2 trianglePositions[] = { startingPositions[0], startingPositions[1], startingPositions[2] };
37+
Vector2 startingPositions[3] = {{ 400.0f, 150.0f }, { 300.0f, 300.0f }, { 500.0f, 300.0f }};
38+
Vector2 trianglePositions[3] = { startingPositions[0], startingPositions[1], startingPositions[2] };
3839

3940
// Currently selected vertex, -1 means none
4041
int triangleIndex = -1;
42+
bool linesMode = false;
43+
float handleRadius = 8.0f;
4144

4245
SetTargetFPS(60);
4346
//--------------------------------------------------------------------------------------
@@ -47,11 +50,7 @@ int main(void)
4750
{
4851
// Update
4952
//----------------------------------------------------------------------------------
50-
// Reset index on release
51-
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
52-
{
53-
triangleIndex = -1;
54-
}
53+
if (IsKeyPressed(KEY_SPACE)) linesMode = !linesMode;
5554

5655
// If the user has selected a vertex, offset it by the mouse's delta this frame
5756
if (triangleIndex != -1)
@@ -62,17 +61,13 @@ int main(void)
6261
position->x += mouseDelta.x;
6362
position->y += mouseDelta.y;
6463
}
64+
65+
// Reset index on release
66+
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) triangleIndex = -1;
6567

6668
// Enable/disable backface culling (2-sided triangles, slower to render)
67-
if (IsKeyPressed(KEY_LEFT))
68-
{
69-
rlEnableBackfaceCulling();
70-
}
71-
72-
if (IsKeyPressed(KEY_RIGHT))
73-
{
74-
rlDisableBackfaceCulling();
75-
}
69+
if (IsKeyPressed(KEY_LEFT)) rlEnableBackfaceCulling();
70+
if (IsKeyPressed(KEY_RIGHT)) rlDisableBackfaceCulling();
7671

7772
// Reset triangle vertices to starting positions and reset backface culling
7873
if (IsKeyPressed(KEY_R))
@@ -89,92 +84,79 @@ int main(void)
8984
//----------------------------------------------------------------------------------
9085
BeginDrawing();
9186

92-
ClearBackground(RAYWHITE);
93-
94-
if (IsKeyDown(KEY_SPACE))
95-
{
96-
// Draw triangle with lines
97-
rlBegin(RL_LINES);
98-
// Three lines, six points
99-
// Define color for next vertex
100-
rlColor4ub(255, 0, 0, 255);
101-
// Define vertex
102-
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
103-
rlColor4ub(0, 255, 0, 255);
104-
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
105-
106-
rlColor4ub(0, 255, 0, 255);
107-
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
108-
rlColor4ub(0, 0, 255, 255);
109-
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
110-
111-
rlColor4ub(0, 0, 255, 255);
112-
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
113-
rlColor4ub(255, 0, 0, 255);
114-
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
115-
rlEnd();
116-
}
117-
else
118-
{
119-
// Draw triangle as a triangle
120-
rlBegin(RL_TRIANGLES);
121-
// One triangle, three points
122-
// Define color for next vertex
123-
rlColor4ub(255, 0, 0, 255);
124-
// Define vertex
125-
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
126-
rlColor4ub(0, 255, 0, 255);
127-
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
128-
rlColor4ub(0, 0, 255, 255);
129-
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
130-
rlEnd();
131-
}
132-
133-
// Render the vertex handles, reacting to mouse movement/input
134-
for (unsigned int i = 0; i < 3; i++)
135-
{
136-
Vector2 position = trianglePositions[i];
137-
138-
float size = 4.0f;
87+
ClearBackground(RAYWHITE);
13988

140-
Vector2 mousePosition = GetMousePosition();
89+
if (linesMode)
90+
{
91+
// Draw triangle with lines
92+
rlBegin(RL_LINES);
93+
// Three lines, six points
94+
// Define color for next vertex
95+
rlColor4ub(255, 0, 0, 255);
96+
// Define vertex
97+
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
98+
rlColor4ub(0, 255, 0, 255);
99+
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
100+
101+
rlColor4ub(0, 255, 0, 255);
102+
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
103+
rlColor4ub(0, 0, 255, 255);
104+
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
105+
106+
rlColor4ub(0, 0, 255, 255);
107+
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
108+
rlColor4ub(255, 0, 0, 255);
109+
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
110+
rlEnd();
111+
}
112+
else
113+
{
114+
// Draw triangle as a triangle
115+
rlBegin(RL_TRIANGLES);
116+
// One triangle, three points
117+
// Define color for next vertex
118+
rlColor4ub(255, 0, 0, 255);
119+
// Define vertex
120+
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
121+
rlColor4ub(0, 255, 0, 255);
122+
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
123+
rlColor4ub(0, 0, 255, 255);
124+
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
125+
rlEnd();
126+
}
141127

142-
// If the cursor is within the handle circle
143-
if (Vector2Distance(mousePosition, position) < size)
128+
// Render the vertex handles, reacting to mouse movement/input
129+
// TODO: Vertex selection can be moved to update logic
130+
for (unsigned int i = 0; i < 3; i++)
144131
{
145-
float fillAlpha = 0.0f;
146-
if (triangleIndex == -1)
147-
{
148-
fillAlpha = 0.5f;
149-
}
132+
Vector2 position = trianglePositions[i];
133+
Vector2 mousePosition = GetMousePosition();
150134

151-
// If handle selected/clicked
152-
if (i == triangleIndex)
135+
// If the cursor is within the handle circle
136+
if (Vector2Distance(mousePosition, position) < handleRadius)
153137
{
154-
fillAlpha = 1.0f;
155-
}
138+
float fillAlpha = 0.0f;
139+
if (triangleIndex == -1) fillAlpha = 0.5f;
156140

157-
// If clicked, set selected index to handle index
158-
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
159-
{
160-
triangleIndex = i;
161-
}
141+
// If handle selected/clicked
142+
if (i == triangleIndex) fillAlpha = 1.0f;
162143

163-
// If visible, draw DARKGRAY circle with varying alpha.
164-
if (fillAlpha > 0.0f)
165-
{
166-
Color fillColor = ColorAlpha(DARKGRAY, fillAlpha);
144+
// If clicked, set selected index to handle index
145+
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) triangleIndex = i;
167146

168-
DrawCircleV(position, size, fillColor);
147+
// If visible, draw DARKGRAY circle with varying alpha.
148+
if (fillAlpha > 0.0f) DrawCircleV(position, handleRadius, ColorAlpha(DARKGRAY, fillAlpha));
169149
}
170-
}
171150

172-
// Draw handle outline
173-
DrawCircleLinesV(position, size, BLACK);
174-
}
151+
// Draw handle outline
152+
DrawCircleLinesV(position, handleRadius, BLACK);
153+
}
175154

176-
// Draw controls
177-
DrawText("space for lines\nleft for backface culling\nright for no backface culling\nclick and drag points\nr to reset", 10, 10, 20, DARKGRAY);
155+
// Draw controls
156+
DrawText("SPACE: Toggle lines mode", 10, 10, 20, DARKGRAY);
157+
DrawText("LEFT-RIGHT: Toggle backface culling", 10, 40, 20, DARKGRAY);
158+
DrawText("MOUSE: Click and drag vertex points", 10, 70, 20, DARKGRAY);
159+
DrawText("R: Reset triangle to start positions", 10, 100, 20, DARKGRAY);
178160

179161
EndDrawing();
180162
//----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)