raylib/examples/textures/textures_mouse_painting.c

211 lines
8.0 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [textures] example - Mouse painting
*
* 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)
*
2019-07-28 16:09:01 +03:00
* Example contributed by Chris Dill (@MysteriousSpace) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Chris Dill (@MysteriousSpace) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
2019-07-28 16:09:01 +03:00
#define MAX_COLORS_COUNT 23 // Number of colors available
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
2019-07-28 16:09:01 +03:00
InitWindow(screenWidth, screenHeight, "raylib [textures] example - mouse painting");
2019-07-28 16:09:01 +03:00
// Colours to choose from
Color colors[MAX_COLORS_COUNT] = {
2019-07-28 16:09:01 +03:00
RAYWHITE, YELLOW, GOLD, ORANGE, PINK, RED, MAROON, GREEN, LIME, DARKGREEN,
SKYBLUE, BLUE, DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE, BROWN, DARKBROWN,
LIGHTGRAY, GRAY, DARKGRAY, BLACK };
// Define colorsRecs data (for every rectangle)
Rectangle colorsRecs[MAX_COLORS_COUNT] = { 0 };
for (int i = 0; i < MAX_COLORS_COUNT; i++)
{
colorsRecs[i].x = 10 + 30*i + 2*i;
colorsRecs[i].y = 10;
colorsRecs[i].width = 30;
colorsRecs[i].height = 30;
}
2019-07-28 16:09:01 +03:00
int colorSelected = 0;
int colorSelectedPrev = colorSelected;
int colorMouseHover = 0;
int brushSize = 20;
2019-07-28 16:09:01 +03:00
Rectangle btnSaveRec = { 750, 10, 40, 30 };
bool btnSaveMouseHover = false;
2019-07-28 16:26:22 +03:00
bool showSaveMessage = false;
int saveMessageCounter = 0;
// Create a RenderTexture2D to use as a canvas
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
2019-07-28 16:09:01 +03:00
// Clear render texture before entering the game loop
BeginTextureMode(target);
2019-07-28 16:09:01 +03:00
ClearBackground(colors[0]);
EndTextureMode();
2019-07-28 16:09:01 +03:00
SetTargetFPS(120); // Set our game to run at 120 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
2019-07-28 16:09:01 +03:00
while (!WindowShouldClose()) // Detect window close button or ESC key
{
2019-07-28 16:33:55 +03:00
// Update
//----------------------------------------------------------------------------------
Vector2 mousePos = GetMousePosition();
2019-07-28 16:09:01 +03:00
2019-07-28 20:04:30 +03:00
// Move between colors with keys
2019-07-28 16:33:55 +03:00
if (IsKeyPressed(KEY_RIGHT)) colorSelected++;
else if (IsKeyPressed(KEY_LEFT)) colorSelected--;
2019-07-28 16:09:01 +03:00
2019-07-28 20:04:30 +03:00
if (colorSelected >= MAX_COLORS_COUNT) colorSelected = MAX_COLORS_COUNT - 1;
else if (colorSelected < 0) colorSelected = 0;
// Choose color with mouse
2019-07-28 16:09:01 +03:00
for (int i = 0; i < MAX_COLORS_COUNT; i++)
{
if (CheckCollisionPointRec(mousePos, colorsRecs[i]))
{
colorMouseHover = i;
break;
}
else colorMouseHover = -1;
}
if ((colorMouseHover >= 0) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
colorSelected = colorMouseHover;
colorSelectedPrev = colorSelected;
}
// Change brush size
2019-07-28 16:33:55 +03:00
brushSize += GetMouseWheelMove()*5;
if (brushSize < 2) brushSize = 2;
if (brushSize > 50) brushSize = 50;
2019-07-28 16:09:01 +03:00
2019-07-28 16:33:55 +03:00
if (IsKeyPressed(KEY_C))
2019-07-28 16:09:01 +03:00
{
// Clear render texture to clear color
2019-07-28 16:33:55 +03:00
BeginTextureMode(target);
ClearBackground(colors[0]);
EndTextureMode();
}
2020-03-02 14:22:38 +03:00
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) || (GetGestureDetected() == GESTURE_DRAG))
2019-07-28 16:09:01 +03:00
{
// Paint circle into render texture
// NOTE: To avoid discontinuous circles, we could store
// previous-next mouse points and just draw a line using brush size
2019-07-28 16:33:55 +03:00
BeginTextureMode(target);
if (mousePos.y > 50) DrawCircle(mousePos.x, mousePos.y, brushSize, colors[colorSelected]);
EndTextureMode();
}
2019-07-28 16:33:55 +03:00
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON))
2019-07-28 16:09:01 +03:00
{
colorSelected = 0;
// Erase circle from render texture
2019-07-28 16:33:55 +03:00
BeginTextureMode(target);
if (mousePos.y > 50) DrawCircle(mousePos.x, mousePos.y, brushSize, colors[0]);
EndTextureMode();
}
2019-07-28 16:09:01 +03:00
else colorSelected = colorSelectedPrev;
// Check mouse hover save button
if (CheckCollisionPointRec(mousePos, btnSaveRec)) btnSaveMouseHover = true;
else btnSaveMouseHover = false;
// Image saving logic
// NOTE: Saving painted texture to a default named image
if ((btnSaveMouseHover && IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) || IsKeyPressed(KEY_S))
{
Image image = GetTextureData(target.texture);
ImageFlipVertical(&image);
ExportImage(image, "my_amazing_texture_painting.png");
UnloadImage(image);
2019-07-28 16:26:22 +03:00
showSaveMessage = true;
}
if (showSaveMessage)
{
2019-07-28 20:04:30 +03:00
// On saving, show a full screen message for 2 seconds
2019-07-28 16:26:22 +03:00
saveMessageCounter++;
if (saveMessageCounter > 240)
{
showSaveMessage = false;
saveMessageCounter = 0;
}
2019-07-28 16:09:01 +03:00
}
2019-07-28 16:33:55 +03:00
//----------------------------------------------------------------------------------
2019-07-28 16:33:55 +03:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
2019-07-28 16:09:01 +03:00
ClearBackground(RAYWHITE);
2019-07-28 16:33:55 +03:00
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
2019-07-28 16:09:01 +03:00
// Draw drawing circle for reference
if (mousePos.y > 50)
{
2020-03-02 02:09:05 +03:00
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) DrawCircleLines(mousePos.x, mousePos.y, brushSize, GRAY);
2019-07-28 16:09:01 +03:00
else DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorSelected]);
}
// Draw top panel
DrawRectangle(0, 0, GetScreenWidth(), 50, RAYWHITE);
DrawLine(0, 50, GetScreenWidth(), 50, LIGHTGRAY);
2019-07-28 16:09:01 +03:00
// Draw color selection rectangles
for (int i = 0; i < MAX_COLORS_COUNT; i++) DrawRectangleRec(colorsRecs[i], colors[i]);
DrawRectangleLines(10, 10, 30, 30, LIGHTGRAY);
2019-07-28 16:09:01 +03:00
if (colorMouseHover >= 0) DrawRectangleRec(colorsRecs[colorMouseHover], Fade(WHITE, 0.6f));
2019-07-28 16:09:01 +03:00
DrawRectangleLinesEx((Rectangle){ colorsRecs[colorSelected].x - 2, colorsRecs[colorSelected].y - 2,
colorsRecs[colorSelected].width + 4, colorsRecs[colorSelected].height + 4 }, 2, BLACK);
2019-07-28 16:09:01 +03:00
// Draw save image button
DrawRectangleLinesEx(btnSaveRec, 2, btnSaveMouseHover? RED : BLACK);
DrawText("SAVE!", 755, 20, 10, btnSaveMouseHover? RED : BLACK);
2019-07-28 20:04:30 +03:00
// Draw save image message
2019-07-28 16:26:22 +03:00
if (showSaveMessage)
{
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
DrawRectangle(0, 150, GetScreenWidth(), 80, BLACK);
DrawText("IMAGE SAVED: my_amazing_texture_painting.png", 150, 180, 20, RAYWHITE);
}
2020-03-02 14:22:38 +03:00
2019-07-28 16:33:55 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
2019-07-28 16:09:01 +03:00
UnloadRenderTexture(target); // Unload render texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}