Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

210 linhas
7.9 KiB

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