forked from DKU-EmbeddedSystem-Lab/2025_DKU_OpenSourceBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
66 lines (59 loc) · 1.86 KB
/
Button.cpp
File metadata and controls
66 lines (59 loc) · 1.86 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "button.hpp"
#include "game.hpp"
#include <SDL_ttf.h>
// 이미지 버튼
Button::Button(std::string path, std::function<void()> callback, int posX, int posY)
{
callbackFunction = callback;
position_x = posX;
position_y = posY;
texture = new Texture;
texture->loadFromImage(path);
width = texture->getWidth();
height = texture->getHeight();
isTextButton = false;
}
// 텍스트 버튼
Button::Button(const std::string& text, std::function<void()> callback, int posX, int posY, int w, int h)
{
callbackFunction = callback;
position_x = posX;
position_y = posY;
width = w;
height = h;
mText = text;
isTextButton = true;
texture = nullptr;
}
Button::~Button()
{
if (texture)
texture->free();
}
void Button::draw()
{
if (isTextButton) {
SDL_Renderer* renderer = Game::getInstance()->mRenderer->mSDLRenderer;
SDL_Rect rect = {position_x, position_y, width, height};
SDL_SetRenderDrawColor(renderer, 80, 80, 200, 255);
SDL_RenderFillRect(renderer, &rect);
TTF_Font* font = Game::getInstance()->mRenderer->mediumFont;
SDL_Color color = {255, 255, 255, 255};
SDL_Surface* surf = TTF_RenderUTF8_Blended(font, mText.c_str(), color);
SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
SDL_Rect dst = {position_x + (width - surf->w) / 2, position_y + (height - surf->h) / 2, surf->w, surf->h};
SDL_RenderCopy(renderer, tex, nullptr, &dst);
SDL_FreeSurface(surf);
SDL_DestroyTexture(tex);
} else {
texture->render(position_x, position_y);
}
}
void Button::execute() {
if (callbackFunction)
callbackFunction();
}
int Button::getX() { return position_x; }
int Button::getY() { return position_y; }
int Button::getWidth() { return width; }
int Button::getHeight() { return height; }