00001 #include "Camera.h"
00002 #include "Display.h"
00003 #include "Input.h"
00004
00005 const int Camera::_edgeDistance = 25;
00006 const float Camera::_scrollRatio = 0.5f;
00007 const float Camera::_marginRatio = 0.5f;
00008
00009 Camera::Camera() :
00010 _position(0,0)
00011 {
00012
00013 }
00014
00015 Camera::Camera(int x, int y) :
00016 _position(-x, -y)
00017 {
00018
00019 }
00020
00021 Camera::~Camera()
00022 {
00023 }
00024
00025 void Camera::setBorders( int left, int right, int top, int bottom )
00026 {
00027 Display& d = Display::instance();
00028 Point screenMargin;
00029 screenMargin.x = static_cast<int>(static_cast<double>(d.getScreenWidth()) * _marginRatio);
00030 screenMargin.y = static_cast<int>(static_cast<double>(d.getScreenHeight()) * _marginRatio);
00031
00032 _leftBorder = left - screenMargin.x;
00033 _rightBorder = max ((right+screenMargin.x), (_leftBorder+d.getScreenWidth()));
00034 _topBorder = top - screenMargin.y;
00035 _bottomBorder = max ((bottom+screenMargin.y), (_topBorder+d.getScreenHeight()));
00036 _position.x = _leftBorder;
00037 _position.y = _topBorder;
00038 }
00039
00040 void Camera::update(unsigned int deltaTime)
00041 {
00042 int distance = static_cast<int>(deltaTime * _scrollRatio);
00043
00044 int width = Display::instance().getScreenWidth();
00045 int height = Display::instance().getScreenHeight();
00046 Point p = Input::instance().getMousePosition();
00047 if (p.x > width - _edgeDistance) {
00048 _position.x += distance;
00049 }
00050 else if (p.x < _edgeDistance) {
00051 _position.x += -distance;
00052 }
00053 if (p.y > height - _edgeDistance) {
00054 _position.y += distance;
00055 }
00056 else if (p.y < _edgeDistance) {
00057 _position.y += -distance;
00058 }
00059
00060
00061 if ( _position.x < _leftBorder )
00062 _position.x = _leftBorder;
00063 else if ( _position.x + Display::instance().getScreenWidth() > _rightBorder )
00064 _position.x = _rightBorder - Display::instance().getScreenWidth();
00065 if ( _position.y < _topBorder )
00066 _position.y = _topBorder;
00067 else if ( _position.y + Display::instance().getScreenHeight() > _bottomBorder )
00068 _position.y = _bottomBorder - Display::instance().getScreenHeight();
00069 }
00070
00071 const Point& Camera::getPosition() const
00072 {
00073 return _position;
00074 }
00075
00076 void Camera::setPosition(int x, int y)
00077 {
00078 _position = Point(x,y);
00079 }