-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
86 lines (68 loc) · 1.8 KB
/
window.cpp
File metadata and controls
86 lines (68 loc) · 1.8 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "window.h"
#include <iostream>
Window::Window()
{
this->windowClosed = false;
this->videoMode.width = 1500;
this->videoMode.height = 800;
this->window = new sf::RenderWindow(this->videoMode, "2D Game Engine", sf::Style::Close | sf::Style::Titlebar | sf::Style::Resize);
this->view = sf::View::View(sf::FloatRect(0.0f, 0.0f, this->VIEW_DIMENSIONS[0], this->VIEW_DIMENSIONS[1]));
this->resize(this->videoMode.width, this->videoMode.height);
}
Window::~Window()
{
std::cout << "DELETING WINDOW\n";
this->window->close();
delete this->window;
}
void Window::resize(int width, int height)
{
float viewWidth;
float viewHeight;
float floatWidth = static_cast<float>(width);
float floatHeight = static_cast<float>(height);
// Limited by width
if (height * this->ASPECT_RATIO > width)
{
viewWidth = floatWidth;
viewHeight = floatHeight / this->ASPECT_RATIO;
}
// Limited by height
else
{
viewWidth = floatHeight * this->ASPECT_RATIO;
viewHeight = floatHeight;
}
this->view.setViewport(sf::FloatRect((width - viewWidth) / (2 * width), (height - viewHeight) / (2 * height), viewWidth / width, viewHeight / height));
}
void Window::update(sf::Event ev)
{
while (window->pollEvent(ev))
{
switch (ev.type)
{
case sf::Event::Closed:
this->windowClosed = true;
case sf::Event::Resized:
this->resize(ev.size.width, ev.size.height);
}
}
}
void Window::prepare()
{
this->window->clear(sf::Color::Black);
this->window->setView(this->view);
sf::RectangleShape bg(this->view.getSize());
bg.setFillColor(sf::Color(50, 50, 50, 255));
bg.setPosition(0, 0);
this->window->draw(bg);
}
void Window::render()
{
this->window->display();
}
sf::Vector2f Window::getMousePos()
{
sf::Vector2i mousePos = sf::Mouse::getPosition(*this->window);
return this->window->mapPixelToCoords(mousePos);
}