25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
8.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Mouse painting
  4. *
  5. * Example complexity rating: [] 3/4
  6. *
  7. * Example originally created with raylib 3.0, last time updated with raylib 3.0
  8. *
  9. * Example contributed by Chris Dill (@MysteriousSpace) 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) 2019-2025 Chris Dill (@MysteriousSpace) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define MAX_COLORS_COUNT 23 // Number of colors available
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [textures] example - mouse painting");
  29. // Colors to choose from
  30. Color colors[MAX_COLORS_COUNT] = {
  31. RAYWHITE, YELLOW, GOLD, ORANGE, PINK, RED, MAROON, GREEN, LIME, DARKGREEN,
  32. SKYBLUE, BLUE, DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE, BROWN, DARKBROWN,
  33. LIGHTGRAY, GRAY, DARKGRAY, BLACK };
  34. // Define colorsRecs data (for every rectangle)
  35. Rectangle colorsRecs[MAX_COLORS_COUNT] = { 0 };
  36. for (int i = 0; i < MAX_COLORS_COUNT; i++)
  37. {
  38. colorsRecs[i].x = 10 + 30.0f*i + 2*i;
  39. colorsRecs[i].y = 10;
  40. colorsRecs[i].width = 30;
  41. colorsRecs[i].height = 30;
  42. }
  43. int colorSelected = 0;
  44. int colorSelectedPrev = colorSelected;
  45. int colorMouseHover = 0;
  46. float brushSize = 20.0f;
  47. bool mouseWasPressed = false;
  48. Rectangle btnSaveRec = { 750, 10, 40, 30 };
  49. bool btnSaveMouseHover = false;
  50. bool showSaveMessage = false;
  51. int saveMessageCounter = 0;
  52. // Create a RenderTexture2D to use as a canvas
  53. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  54. // Clear render texture before entering the game loop
  55. BeginTextureMode(target);
  56. ClearBackground(colors[0]);
  57. EndTextureMode();
  58. SetTargetFPS(120); // Set our game to run at 120 frames-per-second
  59. //--------------------------------------------------------------------------------------
  60. // Main game loop
  61. while (!WindowShouldClose()) // Detect window close button or ESC key
  62. {
  63. // Update
  64. //----------------------------------------------------------------------------------
  65. Vector2 mousePos = GetMousePosition();
  66. // Move between colors with keys
  67. if (IsKeyPressed(KEY_RIGHT)) colorSelected++;
  68. else if (IsKeyPressed(KEY_LEFT)) colorSelected--;
  69. if (colorSelected >= MAX_COLORS_COUNT) colorSelected = MAX_COLORS_COUNT - 1;
  70. else if (colorSelected < 0) colorSelected = 0;
  71. // Choose color with mouse
  72. for (int i = 0; i < MAX_COLORS_COUNT; i++)
  73. {
  74. if (CheckCollisionPointRec(mousePos, colorsRecs[i]))
  75. {
  76. colorMouseHover = i;
  77. break;
  78. }
  79. else colorMouseHover = -1;
  80. }
  81. if ((colorMouseHover >= 0) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  82. {
  83. colorSelected = colorMouseHover;
  84. colorSelectedPrev = colorSelected;
  85. }
  86. // Change brush size
  87. brushSize += GetMouseWheelMove()*5;
  88. if (brushSize < 2) brushSize = 2;
  89. if (brushSize > 50) brushSize = 50;
  90. if (IsKeyPressed(KEY_C))
  91. {
  92. // Clear render texture to clear color
  93. BeginTextureMode(target);
  94. ClearBackground(colors[0]);
  95. EndTextureMode();
  96. }
  97. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || (GetGestureDetected() == GESTURE_DRAG))
  98. {
  99. // Paint circle into render texture
  100. // NOTE: To avoid discontinuous circles, we could store
  101. // previous-next mouse points and just draw a line using brush size
  102. BeginTextureMode(target);
  103. if (mousePos.y > 50) DrawCircle((int)mousePos.x, (int)mousePos.y, brushSize, colors[colorSelected]);
  104. EndTextureMode();
  105. }
  106. if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
  107. {
  108. if (!mouseWasPressed)
  109. {
  110. colorSelectedPrev = colorSelected;
  111. colorSelected = 0;
  112. }
  113. mouseWasPressed = true;
  114. // Erase circle from render texture
  115. BeginTextureMode(target);
  116. if (mousePos.y > 50) DrawCircle((int)mousePos.x, (int)mousePos.y, brushSize, colors[0]);
  117. EndTextureMode();
  118. }
  119. else if (IsMouseButtonReleased(MOUSE_BUTTON_RIGHT) && mouseWasPressed)
  120. {
  121. colorSelected = colorSelectedPrev;
  122. mouseWasPressed = false;
  123. }
  124. // Check mouse hover save button
  125. if (CheckCollisionPointRec(mousePos, btnSaveRec)) btnSaveMouseHover = true;
  126. else btnSaveMouseHover = false;
  127. // Image saving logic
  128. // NOTE: Saving painted texture to a default named image
  129. if ((btnSaveMouseHover && IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) || IsKeyPressed(KEY_S))
  130. {
  131. Image image = LoadImageFromTexture(target.texture);
  132. ImageFlipVertical(&image);
  133. ExportImage(image, "my_amazing_texture_painting.png");
  134. UnloadImage(image);
  135. showSaveMessage = true;
  136. }
  137. if (showSaveMessage)
  138. {
  139. // On saving, show a full screen message for 2 seconds
  140. saveMessageCounter++;
  141. if (saveMessageCounter > 240)
  142. {
  143. showSaveMessage = false;
  144. saveMessageCounter = 0;
  145. }
  146. }
  147. //----------------------------------------------------------------------------------
  148. // Draw
  149. //----------------------------------------------------------------------------------
  150. BeginDrawing();
  151. ClearBackground(RAYWHITE);
  152. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  153. DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2) { 0, 0 }, WHITE);
  154. // Draw drawing circle for reference
  155. if (mousePos.y > 50)
  156. {
  157. if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) DrawCircleLines((int)mousePos.x, (int)mousePos.y, brushSize, GRAY);
  158. else DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorSelected]);
  159. }
  160. // Draw top panel
  161. DrawRectangle(0, 0, GetScreenWidth(), 50, RAYWHITE);
  162. DrawLine(0, 50, GetScreenWidth(), 50, LIGHTGRAY);
  163. // Draw color selection rectangles
  164. for (int i = 0; i < MAX_COLORS_COUNT; i++) DrawRectangleRec(colorsRecs[i], colors[i]);
  165. DrawRectangleLines(10, 10, 30, 30, LIGHTGRAY);
  166. if (colorMouseHover >= 0) DrawRectangleRec(colorsRecs[colorMouseHover], Fade(WHITE, 0.6f));
  167. DrawRectangleLinesEx((Rectangle){ colorsRecs[colorSelected].x - 2, colorsRecs[colorSelected].y - 2,
  168. colorsRecs[colorSelected].width + 4, colorsRecs[colorSelected].height + 4 }, 2, BLACK);
  169. // Draw save image button
  170. DrawRectangleLinesEx(btnSaveRec, 2, btnSaveMouseHover ? RED : BLACK);
  171. DrawText("SAVE!", 755, 20, 10, btnSaveMouseHover ? RED : BLACK);
  172. // Draw save image message
  173. if (showSaveMessage)
  174. {
  175. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, 0.8f));
  176. DrawRectangle(0, 150, GetScreenWidth(), 80, BLACK);
  177. DrawText("IMAGE SAVED: my_amazing_texture_painting.png", 150, 180, 20, RAYWHITE);
  178. }
  179. EndDrawing();
  180. //----------------------------------------------------------------------------------
  181. }
  182. // De-Initialization
  183. //--------------------------------------------------------------------------------------
  184. UnloadRenderTexture(target); // Unload render texture
  185. CloseWindow(); // Close window and OpenGL context
  186. //--------------------------------------------------------------------------------------
  187. return 0;
  188. }