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.

101 rivejä
3.7 KiB

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