Skip to content
Snippets Groups Projects
Commit e28c79c7 authored by Joakim Borge Hunskår's avatar Joakim Borge Hunskår
Browse files

Merge branch 'mousewheel-and-const-correctness' into 'main'

added mousewheel support and marked functions as const

See merge request !2
parents 250fe053 2d640cae
No related branches found
No related tags found
1 merge request!2added mousewheel support and marked functions as const
......@@ -41,7 +41,7 @@ class AnimationWindow {
private:
void show_frame();
void update_gui();
TDT4102::Point getWindowDimensions();
TDT4102::Point getWindowDimensions() const;
void startNuklearDraw(TDT4102::Point location, std::string uniqueWindowName, unsigned int width = 0, unsigned int height = 0);
void endNuklearDraw();
void destroy();
......@@ -54,7 +54,7 @@ class AnimationWindow {
std::vector<std::reference_wrapper<TDT4102::Widget>> widgets;
TDT4102::Color backgroundColour = TDT4102::Color::white;
TDT4102::Color backgroundColor = TDT4102::Color::white;
// SDL related context
SDL_Window* windowHandle = nullptr;
......@@ -69,6 +69,7 @@ class AnimationWindow {
std::unordered_map<KeyboardKey, bool> currentKeyStates;
bool currentLeftMouseButtonState = false;
bool currentRightMouseButtonState = false;
float deltaMouseWheel = 0;
public:
explicit AnimationWindow(int x = 50, int y = 50, int width = 1024, int height = 768, const std::string& title = "Animation Window");
......@@ -105,8 +106,8 @@ class AnimationWindow {
void draw_arc(TDT4102::Point center, int width, int height, int start_degree, int end_degree, TDT4102::Color color = TDT4102::Color::black);
// And these functions are for handling input
bool is_key_down(KeyboardKey key);
TDT4102::Point get_mouse_coordinates();
bool is_key_down(KeyboardKey key) const;
TDT4102::Point get_mouse_coordinates() const;
bool is_left_mouse_button_down() const;
bool is_right_mouse_button_down() const;
......@@ -119,9 +120,11 @@ class AnimationWindow {
void show_error_dialog(const std::string& message) const;
// Getters for the window dimensions
int width();
int height();
int get_width() const;
int get_height() const;
void setBackgroundColor(TDT4102::Color newBackgroundColor);
float get_delta_mouse_wheel() const;
};
} // namespace TDT4102
......@@ -41,7 +41,7 @@ TDT4102::AnimationWindow::AnimationWindow(int x, int y, int width, int height, c
}
// Default window background colour
SDL_SetRenderDrawColor(rendererHandle, backgroundColour.redChannel, backgroundColour.greenChannel, backgroundColour.blueChannel, backgroundColour.alphaChannel);
SDL_SetRenderDrawColor(rendererHandle, backgroundColor.redChannel, backgroundColor.greenChannel, backgroundColor.blueChannel, backgroundColor.alphaChannel);
SDL_RenderClear(rendererHandle);
SDL_RendererInfo rendererInfo;
......@@ -77,6 +77,7 @@ void TDT4102::AnimationWindow::destroy() {
void TDT4102::AnimationWindow::show_frame() {
SDL_RenderPresent(rendererHandle);
deltaMouseWheel = 0;
SDL_Event event;
nk_input_begin(context);
......@@ -102,6 +103,8 @@ void TDT4102::AnimationWindow::show_frame() {
} else if (event.button.button == SDL_BUTTON_RIGHT) {
currentRightMouseButtonState = false;
}
} else if (event.type == SDL_MOUSEWHEEL) {
deltaMouseWheel = event.wheel.preciseY; // The amount scrolled vertically, positive away from the user and negative toward the user
}
nk_sdl_handle_event(&event);
......@@ -128,7 +131,7 @@ void TDT4102::AnimationWindow::next_frame() {
// Colour must be reset as a previously drawn element may have changed the current colour
if (!keepPreviousFrame) {
SDL_SetRenderDrawColor(rendererHandle, backgroundColour.redChannel, backgroundColour.greenChannel, backgroundColour.blueChannel, backgroundColour.alphaChannel);
SDL_SetRenderDrawColor(rendererHandle, backgroundColor.redChannel, backgroundColor.greenChannel, backgroundColor.blueChannel, backgroundColor.alphaChannel);
SDL_RenderClear(rendererHandle);
}
......@@ -302,14 +305,14 @@ void TDT4102::AnimationWindow::draw_arc(TDT4102::Point center, int width, int he
SDL_RenderDrawLines(rendererHandle, internal::circleBorderBuffer.data(), internal::SLICES_PER_CIRCLE);
}
bool TDT4102::AnimationWindow::is_key_down(KeyboardKey key) {
bool TDT4102::AnimationWindow::is_key_down(KeyboardKey key) const {
if (currentKeyStates.count(key) == 0) {
return false;
}
return currentKeyStates.at(key);
}
TDT4102::Point TDT4102::AnimationWindow::get_mouse_coordinates() {
TDT4102::Point TDT4102::AnimationWindow::get_mouse_coordinates() const {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
return {mouseX, mouseY};
......@@ -328,17 +331,17 @@ void TDT4102::AnimationWindow::show_error_dialog(const std::string& message) con
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", message.c_str(), windowHandle);
}
TDT4102::Point TDT4102::AnimationWindow::getWindowDimensions() {
TDT4102::Point TDT4102::AnimationWindow::getWindowDimensions() const {
TDT4102::Point dimensions;
SDL_GetRendererOutputSize(rendererHandle, &dimensions.x, &dimensions.y);
return dimensions;
}
int TDT4102::AnimationWindow::width() {
int TDT4102::AnimationWindow::get_width() const {
return getWindowDimensions().x;
}
int TDT4102::AnimationWindow::height() {
int TDT4102::AnimationWindow::get_height() const {
return getWindowDimensions().y;
}
......@@ -350,6 +353,10 @@ bool TDT4102::AnimationWindow::is_right_mouse_button_down() const {
return currentRightMouseButtonState;
}
float TDT4102::AnimationWindow::get_delta_mouse_wheel() const {
return deltaMouseWheel;
}
void TDT4102::AnimationWindow::startNuklearDraw(TDT4102::Point location, std::string uniqueWindowName, unsigned int width, unsigned int height) {
// Make window transparent; we just want to see individual GUI elements
struct nk_style* s = &context->style;
......@@ -390,5 +397,5 @@ void TDT4102::AnimationWindow::endNuklearDraw() {
}
void TDT4102::AnimationWindow::setBackgroundColor(TDT4102::Color newBackgroundColor) {
backgroundColour = newBackgroundColor;
backgroundColor = newBackgroundColor;
}
......@@ -76,8 +76,8 @@ int main(int argc, char* argv[]) {
std::vector<Ball> circles;
circles.resize(5000);
for (unsigned int i = 0; i < circles.size(); i++) {
circles.at(i).x = randint(0, window.width());
circles.at(i).y = randint(0, window.height());
circles.at(i).x = randint(0, window.get_width());
circles.at(i).y = randint(0, window.get_height());
circles.at(i).radius = randint(5, 10);
circles.at(i).xSpeed = float(randint(-400, 400)) / 100.0f;
circles.at(i).ySpeed = float(randint(-400, 400)) / 100.0f;
......@@ -106,8 +106,8 @@ int main(int argc, char* argv[]) {
window.draw_image(mousePosition, image);
bool allAtCenter = true;
for (Ball& ball : circles) {
float gravityX = (float(window.width()) / 2.0f) - ball.x;
float gravityY = (float(window.height()) / 2.0f) - ball.y;
float gravityX = (float(window.get_width()) / 2.0f) - ball.x;
float gravityY = (float(window.get_height()) / 2.0f) - ball.y;
float absoluteSpeed = std::max(std::sqrt(gravityX * gravityX + gravityY * gravityY), 3.0f);
const float gravity = 0.07f;
ball.xSpeed += (gravityX / absoluteSpeed) * gravity;
......@@ -126,7 +126,7 @@ int main(int argc, char* argv[]) {
} else if(ball.y + ball.radius < 0) {
ball.y = window.h() + ball.radius;
}*/
if ((std::abs(ball.x - float(window.width()) / 2.0f) > 1.0f) && (std::abs(ball.y - float(window.height()) / 2.0f) > 1.0f)) {
if ((std::abs(ball.x - float(window.get_width()) / 2.0f) > 1.0f) && (std::abs(ball.y - float(window.get_height()) / 2.0f) > 1.0f)) {
allAtCenter = false;
}
window.draw_circle({int(ball.x), int(ball.y)}, ball.radius, ball.colour, TDT4102::Color::black);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment