mirror of
https://github.com/raysan5/raylib.git
synced 2026-02-08 07:09:18 -05:00
Compare commits
2 Commits
1664204291
...
3c5b3f1c17
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c5b3f1c17 | |||
| 79b7cd6b9b |
146
examples/shapes/shapes_lines_drawing.c
Normal file
146
examples/shapes/shapes_lines_drawing.c
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
/*******************************************************************************************
|
||||||
|
*
|
||||||
|
* raylib [shapes] example - lines drawing
|
||||||
|
*
|
||||||
|
* Example complexity rating: [★☆☆☆] 1/4
|
||||||
|
*
|
||||||
|
* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6
|
||||||
|
*
|
||||||
|
* Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
* 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) 2025-2025 Robin (@RobinsAviary)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
#include "raymath.h"
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
const int screenWidth = 800;
|
||||||
|
const int screenHeight = 450;
|
||||||
|
|
||||||
|
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - lines drawing");
|
||||||
|
|
||||||
|
// Hint text that shows before you click the screen
|
||||||
|
bool startText = true;
|
||||||
|
|
||||||
|
// The mouse's position on the previous frame
|
||||||
|
Vector2 mousePositionPrevious = GetMousePosition();
|
||||||
|
|
||||||
|
// The canvas to draw lines on
|
||||||
|
RenderTexture canvas = LoadRenderTexture(screenWidth, screenHeight);
|
||||||
|
|
||||||
|
// The background color of the canvas
|
||||||
|
const Color backgroundColor = RAYWHITE;
|
||||||
|
|
||||||
|
// The line's thickness
|
||||||
|
float lineThickness = 8.0f;
|
||||||
|
// The lines hue (in HSV, from 0-360)
|
||||||
|
float lineHue = 0.0f;
|
||||||
|
|
||||||
|
// Clear the canvas to the background color
|
||||||
|
BeginTextureMode(canvas);
|
||||||
|
ClearBackground(backgroundColor);
|
||||||
|
EndTextureMode();
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Disable the hint text once the user clicks
|
||||||
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && startText)
|
||||||
|
{
|
||||||
|
startText = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the canvas when the user middle-clicks
|
||||||
|
if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE))
|
||||||
|
{
|
||||||
|
BeginTextureMode(canvas);
|
||||||
|
ClearBackground(backgroundColor);
|
||||||
|
EndTextureMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store whether the left and right buttons are down
|
||||||
|
bool leftButtonDown = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
|
||||||
|
bool rightButtonDown = IsMouseButtonDown(MOUSE_BUTTON_RIGHT);
|
||||||
|
|
||||||
|
if (leftButtonDown || rightButtonDown)
|
||||||
|
{
|
||||||
|
// The color for the line
|
||||||
|
Color drawColor;
|
||||||
|
|
||||||
|
if (leftButtonDown)
|
||||||
|
{
|
||||||
|
// Increase the hue value by the distance our cursor has moved since the last frame (divided by 3)
|
||||||
|
lineHue += Vector2Distance(mousePositionPrevious, GetMousePosition())/3.0f;
|
||||||
|
|
||||||
|
// While the hue is >=360, subtract it to bring it down into the range 0-360
|
||||||
|
// This is more visually accurate than resetting to zero
|
||||||
|
while (lineHue >= 360.0f)
|
||||||
|
{
|
||||||
|
lineHue -= 360.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the final color
|
||||||
|
drawColor = ColorFromHSV(lineHue, 1.0f, 1.0f);
|
||||||
|
}
|
||||||
|
else if (rightButtonDown)
|
||||||
|
{
|
||||||
|
// Use the background color as an "eraser"
|
||||||
|
drawColor = backgroundColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the line onto the canvas
|
||||||
|
BeginTextureMode(canvas);
|
||||||
|
// Circles act as "caps", smoothing corners
|
||||||
|
DrawCircleV(mousePositionPrevious, lineThickness/2.0f, drawColor);
|
||||||
|
DrawCircleV(GetMousePosition(), lineThickness/2.0f, drawColor);
|
||||||
|
DrawLineEx(mousePositionPrevious, GetMousePosition(), lineThickness, drawColor);
|
||||||
|
EndTextureMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update line thickness based on mousewheel
|
||||||
|
lineThickness += GetMouseWheelMove();
|
||||||
|
lineThickness = Clamp(lineThickness, 1.0, 500.0f);
|
||||||
|
|
||||||
|
// Update mouse's previous position
|
||||||
|
mousePositionPrevious = GetMousePosition();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
BeginDrawing();
|
||||||
|
// Draw the render texture to the screen, flipped vertically to make it appear top-side up
|
||||||
|
DrawTextureRec(canvas.texture, (Rectangle){ 0.0f, 0.0f, (float)canvas.texture.width,(float)-canvas.texture.height }, Vector2Zero(), WHITE);
|
||||||
|
|
||||||
|
// Draw the preview circle
|
||||||
|
if (!leftButtonDown) DrawCircleLinesV(GetMousePosition(), lineThickness/2.0f, (Color){ 127, 127, 127, 127 });
|
||||||
|
|
||||||
|
// Draw the hint text
|
||||||
|
if (startText) DrawText("try clicking and dragging!", 275, 215, 20, LIGHTGRAY);
|
||||||
|
EndDrawing();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
// Unload the canvas render texture
|
||||||
|
UnloadRenderTexture(canvas);
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
examples/shapes/shapes_lines_drawing.png
Normal file
BIN
examples/shapes/shapes_lines_drawing.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
34
src/external/rlsw.h
vendored
34
src/external/rlsw.h
vendored
@ -6,7 +6,7 @@
|
|||||||
* rlsw is a custom OpenGL 1.1-style implementation on software, intended to provide all
|
* rlsw is a custom OpenGL 1.1-style implementation on software, intended to provide all
|
||||||
* functionality available on rlgl.h library used by raylib, becoming a direct software
|
* functionality available on rlgl.h library used by raylib, becoming a direct software
|
||||||
* rendering replacement for OpenGL 1.1 backend and allowing to run raylib on GPU-less
|
* rendering replacement for OpenGL 1.1 backend and allowing to run raylib on GPU-less
|
||||||
* devices when required.
|
* devices when required
|
||||||
*
|
*
|
||||||
* FEATURES:
|
* FEATURES:
|
||||||
* - Rendering to custom internal framebuffer with multiple color modes supported:
|
* - Rendering to custom internal framebuffer with multiple color modes supported:
|
||||||
@ -2892,8 +2892,8 @@ static inline bool sw_quad_is_axis_aligned(void)
|
|||||||
float dx = v1[0] - v0[0];
|
float dx = v1[0] - v0[0];
|
||||||
float dy = v1[1] - v0[1];
|
float dy = v1[1] - v0[1];
|
||||||
|
|
||||||
if (fabsf(dx) > 1e-6f && fabsf(dy) < 1e-6f) horizontal++;
|
if ((fabsf(dx) > 1e-6f) && (fabsf(dy) < 1e-6f)) horizontal++;
|
||||||
else if (fabsf(dy) > 1e-6f && fabsf(dx) < 1e-6f) vertical++;
|
else if ((fabsf(dy) > 1e-6f) && (fabsf(dx) < 1e-6f)) vertical++;
|
||||||
else return false; // Diagonal edge -> not axis-aligned
|
else return false; // Diagonal edge -> not axis-aligned
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2933,8 +2933,8 @@ static inline void sw_quad_sort_cw(const sw_vertex_t* *output)
|
|||||||
// Separate vertices based on Y-coordinate
|
// Separate vertices based on Y-coordinate
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
if (input[i].screen[1] == minY && topCount < 2) top[topCount++] = &input[i];
|
if ((input[i].screen[1] == minY) && (topCount < 2)) top[topCount++] = &input[i];
|
||||||
else if (input[i].screen[1] == maxY && bottomCount < 2) bottom[bottomCount++] = &input[i];
|
else if ((input[i].screen[1] == maxY) && (bottomCount < 2)) bottom[bottomCount++] = &input[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we don't have enough top/bottom vertices (e.g., Y values are all different),
|
// If we don't have enough top/bottom vertices (e.g., Y values are all different),
|
||||||
@ -2960,7 +2960,7 @@ static inline void sw_quad_sort_cw(const sw_vertex_t* *output)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sort bottom vertices by X (left to right)
|
// Sort bottom vertices by X (left to right)
|
||||||
if (bottomCount == 2 && bottom[0]->screen[0] > bottom[1]->screen[0])
|
if ((bottomCount == 2) && (bottom[0]->screen[0] > bottom[1]->screen[0]))
|
||||||
{
|
{
|
||||||
const sw_vertex_t *temp = bottom[0];
|
const sw_vertex_t *temp = bottom[0];
|
||||||
bottom[0] = bottom[1];
|
bottom[0] = bottom[1];
|
||||||
@ -2997,7 +2997,7 @@ static inline void FUNC_NAME(void)
|
|||||||
int width = xMax - xMin; \
|
int width = xMax - xMin; \
|
||||||
int height = yMax - yMin; \
|
int height = yMax - yMin; \
|
||||||
\
|
\
|
||||||
if (width == 0 || height == 0) return; \
|
if ((width == 0) || (height == 0)) return; \
|
||||||
\
|
\
|
||||||
float wRcp = (width > 0.0f)? 1.0f/width : 0.0f; \
|
float wRcp = (width > 0.0f)? 1.0f/width : 0.0f; \
|
||||||
float hRcp = (height > 0.0f)? 1.0f/height : 0.0f; \
|
float hRcp = (height > 0.0f)? 1.0f/height : 0.0f; \
|
||||||
@ -3157,7 +3157,7 @@ static inline void sw_quad_render(void)
|
|||||||
|
|
||||||
if (RLSW.vertexCounter < 3) return;
|
if (RLSW.vertexCounter < 3) return;
|
||||||
|
|
||||||
if (RLSW.vertexCounter == 4 && sw_quad_is_axis_aligned())
|
if ((RLSW.vertexCounter == 4) && sw_quad_is_axis_aligned())
|
||||||
{
|
{
|
||||||
if (SW_STATE_CHECK(SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST | SW_STATE_BLEND)) sw_quad_raster_axis_aligned_TEX_DEPTH_BLEND();
|
if (SW_STATE_CHECK(SW_STATE_TEXTURE_2D | SW_STATE_DEPTH_TEST | SW_STATE_BLEND)) sw_quad_raster_axis_aligned_TEX_DEPTH_BLEND();
|
||||||
else if (SW_STATE_CHECK(SW_STATE_DEPTH_TEST | SW_STATE_BLEND)) sw_quad_raster_axis_aligned_DEPTH_BLEND();
|
else if (SW_STATE_CHECK(SW_STATE_DEPTH_TEST | SW_STATE_BLEND)) sw_quad_raster_axis_aligned_DEPTH_BLEND();
|
||||||
@ -3304,7 +3304,7 @@ static inline void FUNC_NAME(const sw_vertex_t *v0, const sw_vertex_t *v1) \
|
|||||||
int dy = y2 - y1; \
|
int dy = y2 - y1; \
|
||||||
\
|
\
|
||||||
/* Handling of lines that are more horizontal or vertical */ \
|
/* Handling of lines that are more horizontal or vertical */ \
|
||||||
if (dx == 0 && dy == 0) \
|
if ((dx == 0) && (dy == 0)) \
|
||||||
{ \
|
{ \
|
||||||
/* TODO: A point should be rendered here */ \
|
/* TODO: A point should be rendered here */ \
|
||||||
return; \
|
return; \
|
||||||
@ -3444,7 +3444,7 @@ void FUNC_NAME(const sw_vertex_t *v1, const sw_vertex_t *v2) \
|
|||||||
\
|
\
|
||||||
RASTER_FUNC(v1, v2); \
|
RASTER_FUNC(v1, v2); \
|
||||||
\
|
\
|
||||||
if (dx != 0 && abs(dy/dx) < 1) \
|
if ((dx != 0) && (abs(dy/dx) < 1)) \
|
||||||
{ \
|
{ \
|
||||||
int wy = (int)((RLSW.lineWidth - 1.0f)*abs(dx)/sqrtf(dx*dx + dy*dy)); \
|
int wy = (int)((RLSW.lineWidth - 1.0f)*abs(dx)/sqrtf(dx*dx + dy*dy)); \
|
||||||
wy >>= 1; \
|
wy >>= 1; \
|
||||||
@ -3551,13 +3551,13 @@ static inline void FUNC_NAME(int x, int y, float z, const float color[4]) \
|
|||||||
{ \
|
{ \
|
||||||
if (CHECK_BOUNDS == 1) \
|
if (CHECK_BOUNDS == 1) \
|
||||||
{ \
|
{ \
|
||||||
if (x < RLSW.vpMin[0] || x >= RLSW.vpMax[0]) return; \
|
if ((x < RLSW.vpMin[0]) || (x >= RLSW.vpMax[0])) return; \
|
||||||
if (y < RLSW.vpMin[1] || y >= RLSW.vpMax[1]) return; \
|
if ((y < RLSW.vpMin[1]) || (y >= RLSW.vpMax[1])) return; \
|
||||||
} \
|
} \
|
||||||
else if (CHECK_BOUNDS == SW_SCISSOR_TEST) \
|
else if (CHECK_BOUNDS == SW_SCISSOR_TEST) \
|
||||||
{ \
|
{ \
|
||||||
if (x < RLSW.scMin[0] || x >= RLSW.scMax[0]) return; \
|
if ((x < RLSW.scMin[0]) || (x >= RLSW.scMax[0])) return; \
|
||||||
if (y < RLSW.scMin[1] || y >= RLSW.scMax[1]) return; \
|
if ((y < RLSW.scMin[1]) || (y >= RLSW.scMax[1])) return; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
int offset = y*RLSW.framebuffer.width + x; \
|
int offset = y*RLSW.framebuffer.width + x; \
|
||||||
@ -3718,12 +3718,12 @@ static inline bool sw_is_texture_valid(uint32_t id)
|
|||||||
|
|
||||||
static inline bool sw_is_texture_filter_valid(int filter)
|
static inline bool sw_is_texture_filter_valid(int filter)
|
||||||
{
|
{
|
||||||
return (filter == SW_NEAREST || filter == SW_LINEAR);
|
return ((filter == SW_NEAREST) || (filter == SW_LINEAR));
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool sw_is_texture_wrap_valid(int wrap)
|
static inline bool sw_is_texture_wrap_valid(int wrap)
|
||||||
{
|
{
|
||||||
return (wrap == SW_REPEAT || wrap == SW_CLAMP);
|
return ((wrap == SW_REPEAT) || (wrap == SW_CLAMP));
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool sw_is_draw_mode_valid(int mode)
|
static inline bool sw_is_draw_mode_valid(int mode)
|
||||||
@ -4392,7 +4392,7 @@ void swRotatef(float angle, float x, float y, float z)
|
|||||||
|
|
||||||
float lengthSq = x*x + y*y + z*z;
|
float lengthSq = x*x + y*y + z*z;
|
||||||
|
|
||||||
if (lengthSq != 1.0f && lengthSq != 0.0f)
|
if ((lengthSq != 1.0f) && (lengthSq != 0.0f))
|
||||||
{
|
{
|
||||||
float invLength = 1.0f/sqrtf(lengthSq);
|
float invLength = 1.0f/sqrtf(lengthSq);
|
||||||
x *= invLength;
|
x *= invLength;
|
||||||
|
|||||||
Reference in New Issue
Block a user