/
Camera.cpp
79 lines (62 loc) · 2.68 KB
/
Camera.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
#include "Camera.h"
Camera::Camera(){
this->reset();
}
void Camera::update(sf::Vector2f pos){
// Pencere boyutu ve katman boyutu global olarak alýnýr.
sf::VideoMode windowBounds(globals.getWindowBounds());
sf::Vector2f layoutSize(globals.getLayoutSize());
// Pencere boyutlandýrmasý yapýldýysa görüþ alanýnýn güncellenmesi gerekiyor.
// Bu sebeple bir önceki frame ile pencere boyutu ayný mý kontrolü saðlanýyor.
// Deðerler farklý ise kamera resetleniyor ve view güncelleniyor.
if(this->lastWindowBounds.width != windowBounds.width ||
this->lastWindowBounds.height != windowBounds.height){
this->reset();
this->lastWindowBounds.width = windowBounds.width;
this->lastWindowBounds.height = windowBounds.height;
}
//std::cout << pos.x << " " << pos.y << " " << layoutSize.x << "-" << layoutSize.y << std::endl;
/*
* Kameranýn istenilen konuma taþýnýp taþýnamayacaðýnýn kontrolü yapýlýyor.
* Eðer verilen pozisyon görünür alanýn dýþýndaysa kamera görünür alaný
* gösterecek þekilde tekrar konumlandýrýlýyor.
*/
if(layoutSize.x > windowBounds.width){
if(pos.x > windowBounds.width / 2.f && pos.x < layoutSize.x - windowBounds.width / 2.f)
position.x = pos.x;
else if(pos.x < windowBounds.width / 2.f)
position.x = windowBounds.width / 2.f;
else
position.x = layoutSize.x - windowBounds.width / 2.f;
}
if(layoutSize.y > windowBounds.height){
if(pos.y > windowBounds.height / 2.f && pos.y < layoutSize.y - windowBounds.height / 2.f)
position.y = pos.y;
else if(pos.y < windowBounds.height / 2.f)
position.y = windowBounds.height / 2.f;
else
position.y = layoutSize.y - windowBounds.height / 2.f;
}
//std::cout << position.x << " " << position.y << std::endl;
view.setCenter(position);
}
sf::View Camera::getView (){
return view;
}
void Camera::reset(){
/* Pencrenin yeni boyutu alýnýr. */
sf::VideoMode windowBounds(globals.getWindowBounds());
sf::Vector2f layoutSize(globals.getLayoutSize());
/* Pencere boyutuna göre görünür alan düzenlenir. */
view.reset(sf::FloatRect(0, 0, windowBounds.width, windowBounds.height));
/* Görünür alanýn ölçeklendirmesi yapýlýr. */
if(layoutSize.x < windowBounds.width){
// Scale outer
view.setViewport(sf::FloatRect(0, 0, windowBounds.width/layoutSize.x, windowBounds.width / layoutSize.x));
} else{
view.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
}
/* Kamera pozisyonu ortalanýr. */
position.x = windowBounds.width / 2.f;
position.y = windowBounds.height / 2.f;
}