farmlands/src/graphics/RenderContext.cpp

57 lines
1.2 KiB
C++

/*
* RenderContext.cpp
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#include <components/basic/Camera.h>
#include <graphics/RenderContext.h>
#include <model/GameObject.h>
using namespace farmlands::components::basic;
using namespace farmlands::model;
namespace farmlands {
namespace graphics {
float RenderContext::xToWorld(float x)
{
float cellW = viewport.pixelsPerUnitX * m_camera->scale;
return (x - viewport.width / 2) / cellW + m_cameraObj->transform.x;
}
float RenderContext::yToWorld(float y)
{
float cellH = viewport.pixelsPerUnitY * m_camera->scale;
return (y - viewport.height / 2) / cellH + m_cameraObj->transform.y;
}
float RenderContext::xToScreen(float x)
{
float cellW = viewport.pixelsPerUnitX * m_camera->scale;
return (x - m_cameraObj->transform.x) * cellW + viewport.width / 2;
}
float RenderContext::yToScreen(float y)
{
float cellH = viewport.pixelsPerUnitY * m_camera->scale;
return (y - m_cameraObj->transform.y) * cellH + viewport.height / 2;
}
bool RenderContext::visible(SDL_Rect& rect)
{
SDL_Rect screen = { 0, 0, viewport.width, viewport.height };
return SDL_HasIntersection(&screen, &rect);
}
void RenderContext::setCamera(GameObject* camera)
{
m_cameraObj = camera;
m_camera = camera->component<Camera>();
}
}
}