-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCue.cpp
More file actions
68 lines (57 loc) · 2.17 KB
/
Copy pathCue.cpp
File metadata and controls
68 lines (57 loc) · 2.17 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
67
68
#include "Cue.h"
#include <iostream>
#include <cmath>
#include <SDL_image.h>
Cue::Cue(SDL_Renderer* renderer, const char* texturePath)
: renderer(renderer), texture(nullptr), mouseAngle(0.0f), dragging(false), power(0.0f) {
SDL_Surface* surface = IMG_Load(texturePath);
if (!surface) {
std::cerr << "Unable to load texture: " << texturePath << " " << IMG_GetError() << std::endl;
}
else {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (!texture) {
std::cerr << "Unable to create texture from surface: " << SDL_GetError() << std::endl;
}
}
}
Cue::~Cue() {
if (texture) {
SDL_DestroyTexture(texture);
}
}
void Cue::draw(SDL_Renderer* renderer, const Vector2D& cueBallPosition, bool ballsAreMoving, float mouseAngle) {
if (ballsAreMoving) {
return;
}
SDL_Rect destRect;
destRect.w = 400; // Äëèíà êèÿ
destRect.h = 100; // Øèðèíà êèÿ
destRect.x = static_cast<int>(cueBallPosition.getX() - std::cos(mouseAngle) * ballSeparation - destRect.w / 2);
destRect.y = static_cast<int>(cueBallPosition.getY() - std::sin(mouseAngle) * ballSeparation - destRect.h / 2);
SDL_Point center = { destRect.w / 2, destRect.h / 2 };
SDL_RenderCopyEx(renderer, texture, nullptr, &destRect, mouseAngle * 180 / M_PI + 180, ¢er, SDL_FLIP_NONE);
}
void Cue::handleInput(const SDL_Event& event, Vector2D& cueBallPosition, Vector2D& cueBallVelocity) {
if (event.type == SDL_MOUSEMOTION) {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
mouseAngle = std::atan2(mouseY - cueBallPosition.getY(), mouseX - cueBallPosition.getX());
}
if (event.type == SDL_MOUSEBUTTONDOWN && cueBallVelocity.getX() == 0 && cueBallVelocity.getY() == 0) {
cueBallVelocity.setX(std::cos(mouseAngle) * 10);
cueBallVelocity.setY(std::sin(mouseAngle) * 10);
}
}
float Cue::getAngle() const {
return mouseAngle;
}
float Cue::getPower() const {
return power;
}
void Cue::reset() {
power = 0;
ballSeparation = 5.0f;
dragging = false;
}