Skip to content

Commit 8596c94

Browse files
Add clipboard image example to showcase the chipboard image functionality (#5604)
1 parent 0b9239e commit 8596c94

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [textures] example - clipboard image
4+
*
5+
* Example complexity rating: [★☆☆☆] 1/4
6+
*
7+
* Example originally created with raylib 5.5, last time updated with raylib 5.6
8+
*
9+
* Example contributed by Maicon Santana (@maiconpintoabreu) and reviewed by Ramon Santamaria (@raysan5)
10+
*
11+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+
* BSD-like license that allows static linking with closed source software
13+
*
14+
* Copyright (c) 2026-2026 Maicon Santana (@maiconpintoabreu)
15+
*
16+
********************************************************************************************/
17+
18+
#define RCORE_PLATFORM_RGFW
19+
#include "raylib.h"
20+
21+
#define MAX_IAMGE_COLLECTION_AMOUNT 1000
22+
23+
typedef struct ImageCollection {
24+
Texture2D texture;
25+
Vector2 position;
26+
} ImageCollection;
27+
28+
//------------------------------------------------------------------------------------
29+
// Program main entry point
30+
//------------------------------------------------------------------------------------
31+
int main(void)
32+
{
33+
// Initialization
34+
//--------------------------------------------------------------------------------------
35+
const int screenWidth = 800;
36+
const int screenHeight = 450;
37+
38+
InitWindow(screenWidth, screenHeight, "raylib [textures] example - clipboard_image");
39+
40+
ImageCollection collection[MAX_IAMGE_COLLECTION_AMOUNT] = {0};
41+
int currentCollectionIndex = 0;
42+
43+
SetTargetFPS(60);
44+
//--------------------------------------------------------------------------------------
45+
46+
// Main game loop
47+
while (!WindowShouldClose()) // Detect window close button or ESC key
48+
{
49+
// Update
50+
//----------------------------------------------------------------------------------
51+
// TODO: Update variables / Implement example logic at this point
52+
//----------------------------------------------------------------------------------
53+
54+
if(IsKeyPressed(KEY_R))
55+
{
56+
currentCollectionIndex = 0;
57+
58+
// Unload Texture to avoid Memory leak
59+
for (int i=0;i<MAX_IAMGE_COLLECTION_AMOUNT;i++)
60+
{
61+
if (IsTextureValid(collection[i].texture))
62+
{
63+
UnloadTexture(collection[i].texture);
64+
}
65+
}
66+
}
67+
68+
if (currentCollectionIndex < MAX_IAMGE_COLLECTION_AMOUNT && IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V))
69+
{
70+
Image image = GetClipboardImage();
71+
if (IsImageValid(image))
72+
{
73+
collection[currentCollectionIndex].texture = LoadTextureFromImage(image);
74+
collection[currentCollectionIndex].position = GetMousePosition();
75+
currentCollectionIndex++;
76+
UnloadImage(image);
77+
}
78+
else
79+
{
80+
TraceLog(LOG_INFO, "Nothing to paste here");
81+
}
82+
}
83+
84+
// Draw
85+
//----------------------------------------------------------------------------------
86+
BeginDrawing();
87+
88+
ClearBackground(RAYWHITE);
89+
for (int i=0;i<currentCollectionIndex;i++)
90+
{
91+
if (IsTextureValid(collection[i].texture))
92+
{
93+
DrawTexturePro(
94+
collection[i].texture,
95+
(Rectangle){0,0,collection[i].texture.width, collection[i].texture.height},
96+
(Rectangle){collection[i].position.x,collection[i].position.y,collection[i].texture.width, collection[i].texture.height},
97+
(Vector2){collection[i].texture.width*0.5f, collection[i].texture.height*0.5f},
98+
0.0f,
99+
WHITE);
100+
}
101+
}
102+
103+
DrawRectangle(0, 0, screenWidth, 40, BLACK);
104+
DrawText("Clipboard Image - Ctrl+V to Paste and R to Reset ", 120, 10, 20, LIGHTGRAY);
105+
106+
EndDrawing();
107+
//----------------------------------------------------------------------------------
108+
}
109+
110+
// De-Initialization
111+
//--------------------------------------------------------------------------------------
112+
113+
for (int i=0;i<MAX_IAMGE_COLLECTION_AMOUNT;i++)
114+
{
115+
if (IsTextureValid(collection[i].texture))
116+
{
117+
UnloadTexture(collection[i].texture);
118+
}
119+
}
120+
121+
CloseWindow(); // Close window and OpenGL context
122+
//--------------------------------------------------------------------------------------
123+
124+
return 0;
125+
}
197 KB
Loading

0 commit comments

Comments
 (0)