textures_sprite_button.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - sprite button
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  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) 2019-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define NUM_FRAMES 3 // Number of frames (rectangles) for the button sprite texture
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite button");
  27. InitAudioDevice(); // Initialize audio device
  28. Sound fxButton = LoadSound("resources/buttonfx.wav"); // Load button sound
  29. Texture2D button = LoadTexture("resources/button.png"); // Load button texture
  30. // Define frame rectangle for drawing
  31. float frameHeight = (float)button.height/NUM_FRAMES;
  32. Rectangle sourceRec = { 0, 0, (float)button.width, frameHeight };
  33. // Define button bounds on screen
  34. Rectangle btnBounds = { screenWidth/2.0f - button.width/2.0f, screenHeight/2.0f - button.height/NUM_FRAMES/2.0f, (float)button.width, frameHeight };
  35. int btnState = 0; // Button state: 0-NORMAL, 1-MOUSE_HOVER, 2-PRESSED
  36. bool btnAction = false; // Button action should be activated
  37. Vector2 mousePoint = { 0.0f, 0.0f };
  38. SetTargetFPS(60);
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. mousePoint = GetMousePosition();
  46. btnAction = false;
  47. // Check button state
  48. if (CheckCollisionPointRec(mousePoint, btnBounds))
  49. {
  50. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) btnState = 2;
  51. else btnState = 1;
  52. if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) btnAction = true;
  53. }
  54. else btnState = 0;
  55. if (btnAction)
  56. {
  57. PlaySound(fxButton);
  58. // TODO: Any desired action
  59. }
  60. // Calculate button frame rectangle to draw depending on button state
  61. sourceRec.y = btnState*frameHeight;
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. DrawTextureRec(button, sourceRec, (Vector2){ btnBounds.x, btnBounds.y }, WHITE); // Draw button frame
  68. EndDrawing();
  69. //----------------------------------------------------------------------------------
  70. }
  71. // De-Initialization
  72. //--------------------------------------------------------------------------------------
  73. UnloadTexture(button); // Unload button texture
  74. UnloadSound(fxButton); // Unload sound
  75. CloseAudioDevice(); // Close audio device
  76. CloseWindow(); // Close window and OpenGL context
  77. //--------------------------------------------------------------------------------------
  78. return 0;
  79. }