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