You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
7.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Draw part of the texture tiled
  4. *
  5. * This example has been created using raylib 3.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2020 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define SIZEOF(A) (sizeof(A)/sizeof(A[0]))
  13. #define OPT_WIDTH 220 // Max width for the options container
  14. #define MARGIN_SIZE 8 // Size for the margins
  15. #define COLOR_SIZE 16 // Size of the color select buttons
  16. int main(int argc, char **argv)
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. SetConfigFlags(FLAG_WINDOW_RESIZABLE); // Make the window resizable
  23. InitWindow(screenWidth, screenHeight, "raylib [textures] example - Draw part of a texture tiled");
  24. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  25. Texture texPattern = LoadTexture("resources/patterns.png");
  26. SetTextureFilter(texPattern, TEXTURE_FILTER_TRILINEAR); // Makes the texture smoother when upscaled
  27. // Coordinates for all patterns inside the texture
  28. const Rectangle recPattern[] = {
  29. (Rectangle){ 3, 3, 66, 66 },
  30. (Rectangle){ 75, 3, 100, 100 },
  31. (Rectangle){ 3, 75, 66, 66 },
  32. (Rectangle){ 7, 156, 50, 50 },
  33. (Rectangle){ 85, 106, 90, 45 },
  34. (Rectangle){ 75, 154, 100, 60}
  35. };
  36. // Setup colors
  37. const Color colors[] = { BLACK, MAROON, ORANGE, BLUE, PURPLE, BEIGE, LIME, RED, DARKGRAY, SKYBLUE };
  38. enum { MAX_COLORS = SIZEOF(colors) };
  39. Rectangle colorRec[MAX_COLORS] = { 0 };
  40. // Calculate rectangle for each color
  41. for (int i = 0, x = 0, y = 0; i < MAX_COLORS; i++)
  42. {
  43. colorRec[i].x = 2.0f + MARGIN_SIZE + x;
  44. colorRec[i].y = 22.0f + 256.0f + MARGIN_SIZE + y;
  45. colorRec[i].width = COLOR_SIZE*2.0f;
  46. colorRec[i].height = (float)COLOR_SIZE;
  47. if (i == (MAX_COLORS/2 - 1))
  48. {
  49. x = 0;
  50. y += COLOR_SIZE + MARGIN_SIZE;
  51. }
  52. else x += (COLOR_SIZE*2 + MARGIN_SIZE);
  53. }
  54. int activePattern = 0, activeCol = 0;
  55. float scale = 1.0f, rotation = 0.0f;
  56. SetTargetFPS(60);
  57. //---------------------------------------------------------------------------------------
  58. // Main game loop
  59. while (!WindowShouldClose()) // Detect window close button or ESC key
  60. {
  61. // Update
  62. //----------------------------------------------------------------------------------
  63. screenWidth = GetScreenWidth();
  64. screenHeight = GetScreenHeight();
  65. // Handle mouse
  66. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  67. {
  68. const Vector2 mouse = GetMousePosition();
  69. // Check which pattern was clicked and set it as the active pattern
  70. for (int i = 0; i < SIZEOF(recPattern); i++)
  71. {
  72. if (CheckCollisionPointRec(mouse, (Rectangle){ 2 + MARGIN_SIZE + recPattern[i].x, 40 + MARGIN_SIZE + recPattern[i].y, recPattern[i].width, recPattern[i].height }))
  73. {
  74. activePattern = i;
  75. break;
  76. }
  77. }
  78. // Check to see which color was clicked and set it as the active color
  79. for (int i = 0; i < MAX_COLORS; ++i)
  80. {
  81. if (CheckCollisionPointRec(mouse, colorRec[i]))
  82. {
  83. activeCol = i;
  84. break;
  85. }
  86. }
  87. }
  88. // Handle keys
  89. // Change scale
  90. if (IsKeyPressed(KEY_UP)) scale += 0.25f;
  91. if (IsKeyPressed(KEY_DOWN)) scale -= 0.25f;
  92. if (scale > 10.0f) scale = 10.0f;
  93. else if ( scale <= 0.0f) scale = 0.25f;
  94. // Change rotation
  95. if (IsKeyPressed(KEY_LEFT)) rotation -= 25.0f;
  96. if (IsKeyPressed(KEY_RIGHT)) rotation += 25.0f;
  97. // Reset
  98. if (IsKeyPressed(KEY_SPACE)) { rotation = 0.0f; scale = 1.0f; }
  99. //----------------------------------------------------------------------------------
  100. // Draw
  101. //----------------------------------------------------------------------------------
  102. BeginDrawing();
  103. ClearBackground(RAYWHITE);
  104. // Draw the tiled area
  105. DrawTextureTiled(texPattern, recPattern[activePattern], (Rectangle){(float)OPT_WIDTH+MARGIN_SIZE, (float)MARGIN_SIZE, screenWidth - OPT_WIDTH - 2.0f*MARGIN_SIZE, screenHeight - 2.0f*MARGIN_SIZE},
  106. (Vector2){0.0f, 0.0f}, rotation, scale, colors[activeCol]);
  107. // Draw options
  108. DrawRectangle(MARGIN_SIZE, MARGIN_SIZE, OPT_WIDTH - MARGIN_SIZE, screenHeight - 2*MARGIN_SIZE, ColorAlpha(LIGHTGRAY, 0.5f));
  109. DrawText("Select Pattern", 2 + MARGIN_SIZE, 30 + MARGIN_SIZE, 10, BLACK);
  110. DrawTexture(texPattern, 2 + MARGIN_SIZE, 40 + MARGIN_SIZE, BLACK);
  111. DrawRectangle(2 + MARGIN_SIZE + (int)recPattern[activePattern].x, 40 + MARGIN_SIZE + (int)recPattern[activePattern].y, (int)recPattern[activePattern].width, (int)recPattern[activePattern].height, ColorAlpha(DARKBLUE, 0.3f));
  112. DrawText("Select Color", 2+MARGIN_SIZE, 10+256+MARGIN_SIZE, 10, BLACK);
  113. for (int i = 0; i < MAX_COLORS; i++)
  114. {
  115. DrawRectangleRec(colorRec[i], colors[i]);
  116. if (activeCol == i) DrawRectangleLinesEx(colorRec[i], 3, ColorAlpha(WHITE, 0.5f));
  117. }
  118. DrawText("Scale (UP/DOWN to change)", 2 + MARGIN_SIZE, 80 + 256 + MARGIN_SIZE, 10, BLACK);
  119. DrawText(TextFormat("%.2fx", scale), 2 + MARGIN_SIZE, 92 + 256 + MARGIN_SIZE, 20, BLACK);
  120. DrawText("Rotation (LEFT/RIGHT to change)", 2 + MARGIN_SIZE, 122 + 256 + MARGIN_SIZE, 10, BLACK);
  121. DrawText(TextFormat("%.0f degrees", rotation), 2 + MARGIN_SIZE, 134 + 256 + MARGIN_SIZE, 20, BLACK);
  122. DrawText("Press [SPACE] to reset", 2 + MARGIN_SIZE, 164 + 256 + MARGIN_SIZE, 10, DARKBLUE);
  123. // Draw FPS
  124. DrawText(TextFormat("%i FPS", GetFPS()), 2 + MARGIN_SIZE, 2 + MARGIN_SIZE, 20, BLACK);
  125. EndDrawing();
  126. //----------------------------------------------------------------------------------
  127. }
  128. // De-Initialization
  129. //--------------------------------------------------------------------------------------
  130. UnloadTexture(texPattern); // Unload texture
  131. CloseWindow(); // Close window and OpenGL context
  132. //--------------------------------------------------------------------------------------
  133. return 0;
  134. }