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.

169 lines
7.1 KiB

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