Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

148 rader
6.5 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 ptex = LoadTexture("resources/pat.png");
  26. SetTextureFilter(ptex, FILTER_TRILINEAR); // Makes the texture smoother when upscaled
  27. // Coordinates for all patterns inside the texture
  28. const Rectangle patRec[] = { (Rectangle){3,3,66,66}, (Rectangle){75,3,100,100},
  29. (Rectangle){3,75,66,66}, (Rectangle){7,156,50,50}, (Rectangle){85,106,90,45}, (Rectangle){75,154,100,60} };
  30. // Setup colors
  31. const Color colors[] = { BLACK, MAROON, ORANGE, BLUE, PURPLE, BEIGE, LIME, RED, DARKGRAY, SKYBLUE};
  32. enum {MAX_COLORS = SIZEOF(colors)};
  33. Rectangle colorRec[MAX_COLORS] = { 0 };
  34. // Calculate rectangle for each color
  35. for(int i=0, x=0, y=0; i<MAX_COLORS; i++) {
  36. colorRec[i].x = 2+MARGIN_SIZE + x;
  37. colorRec[i].y = 22+256+MARGIN_SIZE + y;
  38. colorRec[i].width = COLOR_SIZE*2;
  39. colorRec[i].height = COLOR_SIZE;
  40. if(i == MAX_COLORS/2 - 1) {
  41. x = 0; y += COLOR_SIZE + MARGIN_SIZE;
  42. } else x += COLOR_SIZE * 2 + MARGIN_SIZE;
  43. }
  44. int activePat = 0, activeCol = 0;
  45. float scale = 1.0f, rotation = 0.0f;
  46. SetTargetFPS(60);
  47. //---------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. screenWidth = GetScreenWidth();
  54. screenHeight = GetScreenHeight();
  55. // Handle mouse
  56. if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
  57. const Vector2 mouse = GetMousePosition();
  58. // Check which pattern was clicked and set it as the active pattern
  59. for(int i=0; i<SIZEOF(patRec); ++i) {
  60. if(CheckCollisionPointRec(mouse, (Rectangle){2+MARGIN_SIZE+patRec[i].x, 40+MARGIN_SIZE+patRec[i].y,patRec[i].width, patRec[i].height})) {
  61. activePat = i;
  62. break;
  63. }
  64. }
  65. // Check to see which color was clicked and set it as the active color
  66. for(int i=0; i<MAX_COLORS; ++i) {
  67. if(CheckCollisionPointRec(mouse, colorRec[i])) {
  68. activeCol = i;
  69. break;
  70. }
  71. }
  72. }
  73. // Handle keys
  74. // Change scale
  75. if(IsKeyPressed(KEY_UP)) scale += 0.25f;
  76. if(IsKeyPressed(KEY_DOWN)) scale -= 0.25f;
  77. if(scale > 10.0f) scale = 10.0f;
  78. else if( scale <= 0.0f) scale = 0.25f;
  79. // Change rotation
  80. if(IsKeyPressed(KEY_LEFT)) rotation -= 25.0f;
  81. if(IsKeyPressed(KEY_RIGHT)) rotation += 25.0f;
  82. // Reset
  83. if(IsKeyPressed(KEY_SPACE)) { rotation = 0.0f; scale = 1.0f; }
  84. //----------------------------------------------------------------------------------
  85. // Draw
  86. //----------------------------------------------------------------------------------
  87. BeginDrawing();
  88. ClearBackground(RAYWHITE);
  89. // Draw the tiled area
  90. DrawTextureTiled(ptex, patRec[activePat], (Rectangle){OPT_WIDTH+MARGIN_SIZE, MARGIN_SIZE, screenWidth - OPT_WIDTH - 2*MARGIN_SIZE, screenHeight - 2*MARGIN_SIZE},
  91. (Vector2){0.0f, 0.0f}, rotation, scale, colors[activeCol]);
  92. // Draw options
  93. DrawRectangle(MARGIN_SIZE, MARGIN_SIZE, OPT_WIDTH - MARGIN_SIZE, screenHeight-2*MARGIN_SIZE, ColorAlpha(LIGHTGRAY, 0.5f));
  94. DrawText("Select Pattern", 2+MARGIN_SIZE, 30+MARGIN_SIZE, 10, BLACK);
  95. DrawTexture(ptex, 2+MARGIN_SIZE, 40+MARGIN_SIZE, BLACK);
  96. DrawRectangle(2+MARGIN_SIZE + patRec[activePat].x, 40+MARGIN_SIZE+patRec[activePat].y,patRec[activePat].width, patRec[activePat].height, ColorAlpha(DARKBLUE, 0.3f));
  97. DrawText("Select Color", 2+MARGIN_SIZE, 10+256+MARGIN_SIZE, 10, BLACK);
  98. for(int i=0; i<MAX_COLORS; ++i) {
  99. DrawRectangleRec(colorRec[i], colors[i]);
  100. if(activeCol == i) DrawRectangleLinesEx(colorRec[i], 3.0f, ColorAlpha(WHITE, 0.5f));
  101. }
  102. DrawText("Scale (UP/DOWN to change)", 2+MARGIN_SIZE, 80+256+MARGIN_SIZE, 10, BLACK);
  103. DrawText(TextFormat("%.2fx", scale), 2+MARGIN_SIZE, 92+256+MARGIN_SIZE, 20, BLACK);
  104. DrawText("Rotation (LEFT/RIGHT to change)", 2+MARGIN_SIZE, 122+256+MARGIN_SIZE, 10, BLACK);
  105. DrawText(TextFormat("%.0f degrees", rotation), 2+MARGIN_SIZE, 134+256+MARGIN_SIZE, 20, BLACK);
  106. DrawText("Press [SPACE] to reset", 2+MARGIN_SIZE, 164+256+MARGIN_SIZE, 10, DARKBLUE);
  107. // Draw FPS
  108. DrawText(TextFormat("%i FPS", GetFPS()), 2+MARGIN_SIZE, 2+MARGIN_SIZE, 20, BLACK);
  109. EndDrawing();
  110. //----------------------------------------------------------------------------------
  111. }
  112. // De-Initialization
  113. //--------------------------------------------------------------------------------------
  114. UnloadTexture(ptex);
  115. CloseWindow(); // Close window and OpenGL context
  116. //--------------------------------------------------------------------------------------
  117. return 0;
  118. }