-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
43 lines (41 loc) · 1.27 KB
/
Button.cpp
File metadata and controls
43 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Code written by Christian Neij
#include "Button.h"
#include <SDL_ttf.h>
#include "System.h"
#include <SDL_image.h>
namespace minMotor {
Button::Button(int x, int y, int w, int h, std::string txt, const char* upIconPath, const char* downIconPath) : NonMoveableSprite(x, y, w, h)
{
SDL_Surface* surf = TTF_RenderText_Solid(sys.getFont(), txt.c_str(), { 0,0,0 });
texture = SDL_CreateTextureFromSurface(sys.getRen(), surf);
SDL_FreeSurface(surf);
upIcon = IMG_LoadTexture(sys.getRen(), upIconPath);
downIcon = IMG_LoadTexture(sys.getRen(), downIconPath);
}
Button::~Button()
{
SDL_DestroyTexture(texture);
SDL_DestroyTexture(upIcon);
SDL_DestroyTexture(downIcon);
}
void Button::mouseDown(const SDL_Event& eve) {
SDL_Point p = { eve.button.x, eve.button.y };
if (SDL_PointInRect(&p, &getRect())) {
isDown = true;
}
}
void Button::mouseUp(const SDL_Event& eve) {
SDL_Point p = { eve.button.x, eve.button.y };
if (SDL_PointInRect(&p, &getRect())) {
perform();
}
isDown = false;
}
void Button::draw() const {
if (isDown)
SDL_RenderCopy(sys.getRen(), downIcon, NULL, &getRect());
else
SDL_RenderCopy(sys.getRen(), upIcon, NULL, &getRect());
SDL_RenderCopy(sys.getRen(), texture, NULL, &getRect());
}
}