-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
114 lines (89 loc) · 3.16 KB
/
Menu.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// Menu.cpp
// TicTacToe
//
// Created by Савелий Никулин on 03.07.2024.
//
#include "Menu.hpp"
Menu::Menu(SDL_Renderer *render, float x, float y, float w, float h) {
this->render = render;
this->x = x;
this->y = y;
this->w = w;
this->h = h;
font = TTF_OpenFont("assets/Cannonade.ttf", 48);
Node playerVSplayerButton = Node();
playerVSplayerButton.setName("playerVSplayer");
playerVSplayerButton.setPosition(x + 0, y + 0);
playerVSplayerButton.setSize(w, 44);
Node playerVScomputerButton = Node();
playerVScomputerButton.setName("playerVScomputer");
playerVScomputerButton.setPosition(x + 0, y + 52);
playerVScomputerButton.setSize(w, 44);
playerVSplayerTextSurface = TTF_RenderText_Solid(font, "Player vs Player", SDL_Color{0, 0, 0, 255});
playerVSplayerTextTexture = SDL_CreateTextureFromSurface(render, playerVSplayerTextSurface);
playerVScomputerTextSurface = TTF_RenderText_Solid(font, "Player vs Computer", SDL_Color{0, 0, 0, 255});
playerVScomputerTextTexture = SDL_CreateTextureFromSurface(render, playerVScomputerTextSurface);
buttons.push_back(playerVSplayerButton);
buttons.push_back(playerVScomputerButton);
}
void Menu::eventController(SDL_Event &e) {
int mouseX, mouseY = 0;
SDL_GetMouseState(&mouseX, &mouseY);
if (e.type == SDL_MOUSEBUTTONUP) {
for (Node &button : buttons) {
if (button.getName() == "playerVSplayer" && button.isInteract(mouseX, mouseY)) {
*gameState = game;
*gameMode = playerVSplayer;
} else if (button.getName() == "playerVScomputer" && button.isInteract(mouseX, mouseY)) {
*gameState = game;
*gameMode = playerVScomputer;
}
}
}
}
void Menu::logic() {
}
void Menu::draw() {
for (Node &button : buttons) {
SDL_FRect r{
button.getX(),
button.getY(),
button.getW(),
button.getH()
};
SDL_SetRenderDrawColor(render, 0, 255, 0, 255);
SDL_RenderDrawRectF(render, &r);
if (button.getName() == "playerVSplayer") {
SDL_Rect tRect {
static_cast<int>(r.x + r.w / 2 - 200 / 2),
static_cast<int>(r.y + r.h / 2 - 40 / 2),
200,
40
};
SDL_RenderCopy(render, playerVSplayerTextTexture, nullptr, &tRect);
} else if (button.getName() == "playerVScomputer") {
SDL_Rect tRect {
static_cast<int>(r.x + r.w / 2 - 200 / 2),
static_cast<int>(r.y + r.h / 2 - 40 / 2),
230,
40
};
SDL_RenderCopy(render, playerVScomputerTextTexture, nullptr, &tRect);
}
}
}
void Menu::setGameState(GameState *gameState) {
this->gameState = gameState;
}
void Menu::setGameMode(GameMode *gameMode) {
this->gameMode = gameMode;
}
Menu::~Menu() {
SDL_FreeSurface(playerVScomputerTextSurface);
SDL_FreeSurface(playerVSplayerTextSurface);
TTF_CloseFont(font);
playerVSplayerTextTexture = nullptr;
playerVScomputerTextTexture = nullptr;
font = nullptr;
}