Cambios en Camera.
Ahora la cámara hereda de Transformable. Preparando la "proyección" de Rects respecto a Camera. Probablemente requiera crear la clase Window para ello.
This commit is contained in:
@@ -1,16 +1,34 @@
|
|||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
#include "Logging/Logger.h"
|
#include "Logging/Logger.h"
|
||||||
|
|
||||||
|
#include <SDL_rect.h>
|
||||||
|
|
||||||
namespace UE
|
namespace UE
|
||||||
{
|
{
|
||||||
|
glm::vec2 Camera::WorldToSreenCoords(glm::vec<2, float> worldCoord)
|
||||||
void Camera::SetCamPosition(int posX, int posY)
|
|
||||||
{
|
{
|
||||||
camPosition.x = posX;
|
//Convertimos las coordenas de WorldCoords a ScreenCoords relativas a la camPosition.
|
||||||
camPosition.y = posY;
|
glm::ivec2 fPos = worldCoord - _pos;
|
||||||
|
|
||||||
UE_LOG_TRACE("Cam position chaged to: {0}x{1}", posX, posY);
|
return fPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::vec2 Camera::ScreenToWorldCoords(glm::vec<2, float> screenCoord)
|
||||||
|
{
|
||||||
|
//Convertimos las coordenas de ScreenCoords a WorldCoords relativas a la camPosition.
|
||||||
|
glm::ivec2 fPos = screenCoord + _pos;
|
||||||
|
|
||||||
|
return fPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Rect Camera::WorldToScreenRect(SDL_Rect worldRect)
|
||||||
|
{
|
||||||
|
return SDL_Rect();
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Rect Camera::ScreenToWorldRect(SDL_Rect screenRect)
|
||||||
|
{
|
||||||
|
return SDL_Rect();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <glm/vec2.hpp>
|
#include <glm/vec2.hpp>
|
||||||
|
|
||||||
|
#include "Interfaces/Transformable.h"
|
||||||
|
|
||||||
//
|
//
|
||||||
// ToDO:
|
// ToDO:
|
||||||
// - La cámara debería tener un tamaño definido.
|
// - La cámara debería tener un tamaño definido.
|
||||||
@@ -12,23 +14,43 @@
|
|||||||
|
|
||||||
namespace UE
|
namespace UE
|
||||||
{
|
{
|
||||||
class Camera
|
class Camera : public Transformable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Camera() {}
|
Camera() {}
|
||||||
|
|
||||||
Camera(int posX, int posY)
|
Camera(int posX, int posY)
|
||||||
: camPosition{posX, posY}
|
{
|
||||||
{}
|
_pos = { posX, posY };
|
||||||
|
}
|
||||||
|
|
||||||
inline glm::ivec2 GetCamPosition() { return camPosition; }
|
Camera(int posX, int posY, int sizeX, int sizeY)
|
||||||
void SetCamPosition(int posX, int posY);
|
{
|
||||||
|
_pos = { posX, posY };
|
||||||
|
_size = { sizeX, sizeY };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
/* Métodos para transformar y proyectar coordenadas en función de la Camera. */
|
||||||
|
//WorldCoords = ScreenPos + CamPos -> ScreenPos = WorldCoords - CamPos;
|
||||||
|
|
||||||
|
//Proyecta las coordenadas de mundo a coordenadas de pantalla.
|
||||||
|
glm::vec2 WorldToSreenCoords(glm::vec<2, float> worldCoord);
|
||||||
|
|
||||||
|
//Proyecta coordenadas de pantalla al mundo.
|
||||||
|
glm::vec2 ScreenToWorldCoords(glm::vec<2, float> screenCoord);
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Rect WorldToScreenRect(SDL_Rect worldRect);
|
||||||
|
SDL_Rect ScreenToWorldRect(SDL_Rect screenRect);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
glm::ivec2 camPosition { 0, 0 };
|
|
||||||
|
|
||||||
//glm::ivec2 camSize { 0, 0 };
|
//glm::ivec2 camSize { 0, 0 };
|
||||||
|
|
||||||
|
|||||||
@@ -98,8 +98,10 @@ namespace UE
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Actualizamos el rect que representa la pantalla.
|
//Actualizamos el rect que representa la pantalla.
|
||||||
glm::ivec2 camPos = activeCam->GetCamPosition();
|
glm::vec2 camPos = activeCam->GetPosition();
|
||||||
_screenRect = {camPos.x, camPos.y, screenSize.x, screenSize.y};
|
int x = camPos.x;
|
||||||
|
int y = camPos.y;
|
||||||
|
_screenRect = {x, y, screenSize.x, screenSize.y};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::RenderData()
|
void Renderer::RenderData()
|
||||||
@@ -118,11 +120,13 @@ namespace UE
|
|||||||
//Extract Drawable k
|
//Extract Drawable k
|
||||||
Drawable* item = layer.at(k);
|
Drawable* item = layer.at(k);
|
||||||
|
|
||||||
|
// FIX: Cuando la tx sea nullptr o salte except. -> Log y usar default tx.
|
||||||
|
// En definitiva, añadir seguridad aquí, se puede usar la tx default.
|
||||||
SDL_Texture* tx = item->_tx->GetTexture();
|
SDL_Texture* tx = item->_tx->GetTexture();
|
||||||
|
|
||||||
//Los están en WorldCoords. Convertimos la posición a ScreenCoords.
|
//Los están en WorldCoords. Convertimos la posición a ScreenCoords.
|
||||||
// NOTA: Se deberá transformar todo el Rect en función de la cam, no sólo la pos.
|
// NOTA: Se deberá transformar todo el Rect en función de la cam, no sólo la pos.
|
||||||
glm::ivec2 screenCoords = WorldToSreenCoords(item->_pos);
|
glm::ivec2 screenCoords = activeCam->WorldToSreenCoords(item->_pos);
|
||||||
SDL_Rect screenRect = { screenCoords.x, screenCoords.y, item->_size.x, item->_size.y };
|
SDL_Rect screenRect = { screenCoords.x, screenCoords.y, item->_size.x, item->_size.y };
|
||||||
|
|
||||||
//Le pasamos todo al SDL_Renderer.
|
//Le pasamos todo al SDL_Renderer.
|
||||||
@@ -194,23 +198,6 @@ namespace UE
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
glm::ivec2 Renderer::WorldToSreenCoords(glm::ivec2 worldCoord)
|
|
||||||
{
|
|
||||||
//Convertimos las coordenas de WorldCoords a ScreenCoords relativas a la camPosition.
|
|
||||||
glm::ivec2 fPos = worldCoord - activeCam->GetCamPosition();
|
|
||||||
|
|
||||||
return fPos;
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::ivec2 Renderer::ScreenToWorldCoords(glm::ivec2 screenCoord)
|
|
||||||
{
|
|
||||||
//Convertimos las coordenas de ScreenCoords a WorldCoords relativas a la camPosition.
|
|
||||||
glm::ivec2 fPos = screenCoord + activeCam->GetCamPosition();
|
|
||||||
|
|
||||||
return fPos;
|
|
||||||
}
|
|
||||||
|
|
||||||
//CREO QUE ESTE MÉTODO NO FUNCIONA.
|
//CREO QUE ESTE MÉTODO NO FUNCIONA.
|
||||||
//LAS COORDS QUE USAMOS EN LA COMPROBACI�N NO SON LAS ADECUADAS.
|
//LAS COORDS QUE USAMOS EN LA COMPROBACI�N NO SON LAS ADECUADAS.
|
||||||
bool Renderer::IsObjectVisible(SDL_Rect* objectRect)
|
bool Renderer::IsObjectVisible(SDL_Rect* objectRect)
|
||||||
|
|||||||
@@ -145,18 +145,6 @@ namespace UE
|
|||||||
////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
/* M�todos para transformar las coordenadas entre Screen y Wolrd. */
|
|
||||||
//WorldCoords = ScreenPos + CamPos -> ScreenPos = WorldCoords - CamPos;
|
|
||||||
|
|
||||||
//Convierte coordenadas de mundo a la pantalla (en función de la cámara).
|
|
||||||
glm::ivec2 WorldToSreenCoords(glm::ivec2 worldCoord);
|
|
||||||
//Convierte coordenadas de la pantalla al mundo.
|
|
||||||
glm::ivec2 ScreenToWorldCoords(glm::ivec2 screenCoord);
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
|
|
||||||
//Comprueba si un objeto est� dentro del rect�ngulo de la c�mara.
|
//Comprueba si un objeto est� dentro del rect�ngulo de la c�mara.
|
||||||
//Usada para saber si un objeto se deber�a de renderizar o no.
|
//Usada para saber si un objeto se deber�a de renderizar o no.
|
||||||
bool IsObjectVisible(SDL_Rect* objectRect);
|
bool IsObjectVisible(SDL_Rect* objectRect);
|
||||||
|
|||||||
+8
-8
@@ -150,27 +150,27 @@ void Loop()
|
|||||||
UE::Camera* cam = UE_Renderer->GetCamera();
|
UE::Camera* cam = UE_Renderer->GetCamera();
|
||||||
if (keyStates[SDL_SCANCODE_W])
|
if (keyStates[SDL_SCANCODE_W])
|
||||||
{
|
{
|
||||||
glm::ivec2 camPos = cam->GetCamPosition();
|
glm::ivec2 camPos = cam->GetPosition();
|
||||||
camPos.y -= 5;
|
camPos.y -= 5;
|
||||||
cam->SetCamPosition(camPos.x, camPos.y);
|
cam->SetPosition(camPos.x, camPos.y);
|
||||||
}
|
}
|
||||||
if (keyStates[SDL_SCANCODE_S])
|
if (keyStates[SDL_SCANCODE_S])
|
||||||
{
|
{
|
||||||
glm::ivec2 camPos = cam->GetCamPosition();
|
glm::ivec2 camPos = cam->GetPosition();
|
||||||
camPos.y += 5;
|
camPos.y += 5;
|
||||||
cam->SetCamPosition(camPos.x, camPos.y);
|
cam->SetPosition(camPos.x, camPos.y);
|
||||||
}
|
}
|
||||||
if (keyStates[SDL_SCANCODE_D])
|
if (keyStates[SDL_SCANCODE_D])
|
||||||
{
|
{
|
||||||
glm::ivec2 camPos = cam->GetCamPosition();
|
glm::ivec2 camPos = cam->GetPosition();
|
||||||
camPos.x += 5;
|
camPos.x += 5;
|
||||||
cam->SetCamPosition(camPos.x, camPos.y);
|
cam->SetPosition(camPos.x, camPos.y);
|
||||||
}
|
}
|
||||||
if (keyStates[SDL_SCANCODE_A])
|
if (keyStates[SDL_SCANCODE_A])
|
||||||
{
|
{
|
||||||
glm::ivec2 camPos = cam->GetCamPosition();
|
glm::ivec2 camPos = cam->GetPosition();
|
||||||
camPos.x -= 5;
|
camPos.x -= 5;
|
||||||
cam->SetCamPosition(camPos.x, camPos.y);
|
cam->SetPosition(camPos.x, camPos.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentScene->SceneUpdate();
|
currentScene->SceneUpdate();
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ public:
|
|||||||
{
|
{
|
||||||
UE::Renderer* rend = UE::Engine::GetRenderer();
|
UE::Renderer* rend = UE::Engine::GetRenderer();
|
||||||
glm::ivec2 mPos(event->button.x, event->button.y);
|
glm::ivec2 mPos(event->button.x, event->button.y);
|
||||||
glm::ivec2 wPos = rend->ScreenToWorldCoords(mPos);
|
glm::ivec2 wPos = rend->GetCamera()->ScreenToWorldCoords(mPos);
|
||||||
GAME_LOG_TRACE("Posicion del raton: \n\tPantalla: {0}x{1}, Mundo: {2}x{3}: ", mPos.x, mPos.y, wPos.x, wPos.y);
|
GAME_LOG_TRACE("Posicion del raton: \n\tPantalla: {0}x{1}, Mundo: {2}x{3}: ", mPos.x, mPos.y, wPos.x, wPos.y);
|
||||||
ModificarSprite(wPos);
|
ModificarSprite(wPos);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user