Reviewed new examples

This commit is contained in:
raysan5 2019-07-28 15:09:01 +02:00
parent 879c874330
commit 602d2a65dd
4 changed files with 128 additions and 81 deletions

View file

@ -5,6 +5,8 @@
* This example has been created using raylib 2.5 (www.raylib.com) * 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) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Example contributed by Chris Dill (@MysteriousSpace) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Chris Dill (@MysteriousSpace) * Copyright (c) 2019 Chris Dill (@MysteriousSpace)
* *
********************************************************************************************/ ********************************************************************************************/
@ -20,10 +22,10 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [core] example - scissor test"); InitWindow(screenWidth, screenHeight, "raylib [core] example - scissor test");
Rectangle scissorArea = { 0, 0, 300, 300}; Rectangle scissorArea = { 0, 0, 300, 300 };
bool scissorMode = true; bool scissorMode = true;
SetTargetFPS(60); SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
@ -31,14 +33,11 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_S)) if (IsKeyPressed(KEY_S)) scissorMode = !scissorMode;
{
scissorMode = !scissorMode;
}
// Centre the scissor area around the mouse position // Centre the scissor area around the mouse position
scissorArea.x = GetMouseX() - scissorArea.width / 2; scissorArea.x = GetMouseX() - scissorArea.width/2;
scissorArea.y = GetMouseY() - scissorArea.height / 2; scissorArea.y = GetMouseY() - scissorArea.height/2;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -47,22 +46,17 @@ int main(void)
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (scissorMode) if (scissorMode) BeginScissorMode(scissorArea.x, scissorArea.y, scissorArea.width, scissorArea.height);
{
BeginScissorMode(scissorArea.x, scissorArea.y, scissorArea.width, scissorArea.height);
}
DrawRectangle(80, 45, 640, 360, RED); // Draw full screen rectangle and some text
DrawRectangleLines(80, 45, 640, 360, BLACK); // NOTE: Only part defined by scissor area will be rendered
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), RED);
DrawText("Move the mouse around to reveal this text!", 190, 200, 20, LIGHTGRAY); DrawText("Move the mouse around to reveal this text!", 190, 200, 20, LIGHTGRAY);
if (scissorMode) if (scissorMode) EndScissorMode();
{
EndScissorMode();
}
DrawRectangleLinesEx(scissorArea, 2, BLACK); DrawRectangleLinesEx(scissorArea, 1, BLACK);
DrawText("Press s to toggle scissor test", 10, 10, 20, DARKGRAY); DrawText("Press S to toggle scissor test", 10, 10, 20, BLACK);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -5,13 +5,15 @@
* This example has been created using raylib 2.5 (www.raylib.com) * 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) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2019 Chris Dill (@MysteriousSpace) * 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" #include "raylib.h"
#define MAX_COLORS_COUNT 21 // Number of colors available #define MAX_COLORS_COUNT 23 // Number of colors available
int main(void) int main(void)
{ {
@ -20,107 +22,157 @@ int main(void)
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, InitWindow(screenWidth, screenHeight, "raylib [textures] example - mouse painting");
"raylib [textures] example - texture painting");
// Different colours to choose from // Colours to choose from
Color colors[MAX_COLORS_COUNT] = { Color colors[MAX_COLORS_COUNT] = {
DARKGRAY, MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, DARKBROWN, RAYWHITE, YELLOW, GOLD, ORANGE, PINK, RED, MAROON, GREEN, LIME, DARKGREEN,
GRAY, RED, GOLD, LIME, BLUE, VIOLET, BROWN, SKYBLUE, BLUE, DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE, BROWN, DARKBROWN,
LIGHTGRAY, PINK, YELLOW, GREEN, SKYBLUE, PURPLE, BEIGE}; LIGHTGRAY, GRAY, DARKGRAY, BLACK };
const char *colorNames[MAX_COLORS_COUNT] = { // Define colorsRecs data (for every rectangle)
"DARKGRAY", "MAROON", "ORANGE", "DARKGREEN", "DARKBLUE", "DARKPURPLE", Rectangle colorsRecs[MAX_COLORS_COUNT] = { 0 };
"DARKBROWN", "GRAY", "RED", "GOLD", "LIME", "BLUE",
"VIOLET", "BROWN", "LIGHTGRAY", "PINK", "YELLOW", "GREEN",
"SKYBLUE", "PURPLE", "BEIGE"};
int colorState = 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;
}
int colorSelected = 0;
int colorSelectedPrev = colorSelected;
int colorMouseHover = 0;
int brushSize = 20; int brushSize = 20;
Rectangle btnSaveRec = { 750, 10, 40, 30 };
bool btnSaveMouseHover = false;
// Create a RenderTexture2D to use as a canvas // Create a RenderTexture2D to use as a canvas
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
Color clearColor = RAYWHITE;
// Clear render texture before entering the game loop
BeginTextureMode(target); BeginTextureMode(target);
ClearBackground(clearColor); ClearBackground(colors[0]);
EndTextureMode(); EndTextureMode();
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(120); // Set our game to run at 120 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
Vector2 mousePos = GetMousePosition();
// Switch between colors // Switch between colors
if (IsKeyPressed(KEY_RIGHT)) if (IsKeyPressed(KEY_RIGHT)) colorSelected++;
colorState++; else if (IsKeyPressed(KEY_LEFT)) colorSelected--;
else if (IsKeyPressed(KEY_LEFT)) else if (IsKeyPressed(KEY_UP)) colorSelected -= 3;
colorState--; else if (IsKeyPressed(KEY_DOWN)) colorSelected += 3;
if (colorState >= MAX_COLORS_COUNT) for (int i = 0; i < MAX_COLORS_COUNT; i++)
colorState = 0; {
else if (colorState < 0) if (CheckCollisionPointRec(mousePos, colorsRecs[i]))
colorState = MAX_COLORS_COUNT - 1; {
colorMouseHover = i;
break;
}
else colorMouseHover = -1;
}
brushSize += GetMouseWheelMove() * 5; if ((colorMouseHover >= 0) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
if (brushSize < 0) {
brushSize = 0; colorSelected = colorMouseHover;
if (brushSize > 50) colorSelectedPrev = colorSelected;
brushSize = 50; }
Vector2 position = GetMousePosition(); if (colorSelected >= MAX_COLORS_COUNT) colorSelected = MAX_COLORS_COUNT - 1;
else if (colorSelected < 0) colorSelected = 0;
if (IsKeyPressed(KEY_C)) { // Change brush size
brushSize += GetMouseWheelMove()*5;
if (brushSize < 2) brushSize = 2;
if (brushSize > 50) brushSize = 50;
if (IsKeyPressed(KEY_C))
{
// Clear render texture to clear color
BeginTextureMode(target); BeginTextureMode(target);
ClearBackground(RAYWHITE); ClearBackground(colors[0]);
EndTextureMode(); EndTextureMode();
} }
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) { if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
TraceLog(LOG_INFO, "Painting x: %f y: %f", position.x, position.y); {
// 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
BeginTextureMode(target); BeginTextureMode(target);
DrawCircle(position.x, position.y, brushSize, colors[colorState]); if (mousePos.y > 50) DrawCircle(mousePos.x, mousePos.y, brushSize, colors[colorSelected]);
EndTextureMode(); EndTextureMode();
} }
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON))
TraceLog(LOG_INFO, "Erasing x: %f y: %f", position.x, position.y); {
colorSelected = 0;
// Erase circle from render texture
BeginTextureMode(target); BeginTextureMode(target);
DrawCircle(position.x, position.y, brushSize, clearColor); if (mousePos.y > 50) DrawCircle(mousePos.x, mousePos.y, brushSize, colors[0]);
EndTextureMode(); EndTextureMode();
} }
else colorSelected = colorSelectedPrev;
if (IsKeyPressed(KEY_S)) { // Check mouse hover save button
TakeScreenshot("textures_mouse_painting.png"); 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);
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
// (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
DrawTextureRec(target.texture, (Rectangle){0, 0, target.texture.width, -target.texture.height}, (Vector2){0, 0}, WHITE);
// Draw 2d shapes and text over drawn texture // Draw drawing circle for reference
DrawRectangle(0, 9, 380, 60, Fade(LIGHTGRAY, 0.7f)); if (mousePos.y > 50)
{
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) DrawCircleLines(mousePos.x, mousePos.y, brushSize, colors[colorSelected]);
else DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorSelected]);
}
DrawText("COLOR:", 10, 15, 20, BLACK); // Draw top panel
DrawText(colorNames[colorState], 130, 15, 20, RED); DrawRectangle(0, 0, GetScreenWidth(), 50, RAYWHITE);
DrawText("< >", 340, 10, 30, DARKBLUE); DrawLine(0, 50, GetScreenWidth(), 50, LIGHTGRAY);
DrawText("Size:", 10, 40, 20, BLACK); // Draw color selection rectangles
DrawText(FormatText("%i", brushSize), 130, 40, 20, RED); for (int i = 0; i < MAX_COLORS_COUNT; i++) DrawRectangleRec(colorsRecs[i], colors[i]);
DrawRectangleLines(10, 10, 30, 30, LIGHTGRAY);
DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorState]); if (colorMouseHover >= 0) DrawRectangleRec(colorsRecs[colorMouseHover], Fade(WHITE, 0.6f));
DrawFPS(700, 15); DrawRectangleLinesEx((Rectangle){ colorsRecs[colorSelected].x - 2, colorsRecs[colorSelected].y - 2,
colorsRecs[colorSelected].width + 4, colorsRecs[colorSelected].height + 4 }, 2, BLACK);
// Draw save image button
DrawRectangleLinesEx(btnSaveRec, 2, btnSaveMouseHover? RED : BLACK);
DrawText("SAVE!", 755, 20, 10, btnSaveMouseHover? RED : BLACK);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -128,8 +180,9 @@ int main(void)
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadRenderTexture(target); UnloadRenderTexture(target); // Unload render texture
CloseWindow(); // Close window and OpenGL context
CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB