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.

96 lines
3.4 KiB

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