Some Rendering Stuff.

Se ha añadido un método para cambiar el color de clear.
Se ha abstraido la cámara del renderer para más versatilidad a futuro.
Nota: La implementación de la cámara no es nada segura. Tocará revisarlo
This commit is contained in:
2022-08-29 17:54:34 +02:00
parent 692812a710
commit 132f24720e
8 changed files with 109 additions and 34 deletions
+16
View File
@@ -0,0 +1,16 @@
#include "Camera.h"
#include "Logging/Logger.h"
namespace UE
{
void Camera::SetCamPosition(int posX, int posY)
{
camPosition.x = posX;
camPosition.y = posY;
UE_LOG_TRACE("Cam position chaged to: {0}x{1}", posX, posY);
}
}
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include <glm/vec2.hpp>
//
// ToDO:
// - La cámara debería tener un tamaño definido.
// para hacer zoom y esas cosas.
//
namespace UE
{
class Camera
{
public:
Camera() {}
Camera(int posX, int posY)
: camPosition{posX, posY}
{}
inline glm::ivec2 GetCamPosition() { return camPosition; }
void SetCamPosition(int posX, int posY);
private:
glm::ivec2 camPosition { 0, 0 };
//glm::ivec2 camSize { 0, 0 };
};
}
+16 -21
View File
@@ -56,6 +56,7 @@ namespace UE
{
renderLayers.push_back(std::vector<DrawCall>());
}
}
void Renderer::CleanRenderer()
@@ -91,6 +92,10 @@ namespace UE
//Quiz�s deber�amos limpiar la cola y las Layers aqu� para eliminar residuos...
//callCounter = 0;
//rendererCounter = 0;
//Actualizamos el rect que representa la pantalla.
glm::ivec2 camPos = activeCam->GetCamPosition();
_screenRect = {camPos.x, camPos.y, screenSize.x, screenSize.y};
}
void Renderer::RenderData()
@@ -131,7 +136,6 @@ namespace UE
}
void Renderer::EndFrame()
{
//SDL_UpdateWindowSurface(window);
@@ -160,11 +164,9 @@ namespace UE
UE_LOG_TRACE("Screen size chaged to: {0}x{1}", newWidth, newHeight);
}
void Renderer::SetCamPosition(int posX, int posY)
void Renderer::SetDrawingColor(SDL_Color& newColor)
{
camPosition.x = posX;
camPosition.y = posY;
//UE_LOG_TRACE("Cam position chaged to: {0}x{1}", posX, posY);
SDL_SetRenderDrawColor(main_Renderer, newColor.r, newColor.g, newColor.b, newColor.a);
}
@@ -172,7 +174,7 @@ namespace UE
glm::ivec2 Renderer::WorldToSreenCoords(glm::ivec2 worldCoord)
{
//Convertimos las coordenas de WorldCoords a ScreenCoords relativas a la camPosition.
glm::ivec2 fPos = worldCoord - camPosition;
glm::ivec2 fPos = worldCoord - activeCam->GetCamPosition();
return fPos;
}
@@ -180,7 +182,7 @@ namespace UE
glm::ivec2 Renderer::ScreenToWorldCoords(glm::ivec2 screenCoord)
{
//Convertimos las coordenas de ScreenCoords a WorldCoords relativas a la camPosition.
glm::ivec2 fPos = screenCoord + camPosition;
glm::ivec2 fPos = screenCoord + activeCam->GetCamPosition();
return fPos;
}
@@ -189,11 +191,8 @@ namespace UE
//LAS COORDS QUE USAMOS EN LA COMPROBACI�N NO SON LAS ADECUADAS.
bool Renderer::IsObjectVisible(SDL_Rect* objectRect)
{
//Generamos un rect que representa la pantalla.
SDL_Rect screen{ camPosition.x, camPosition.y, screenSize.x, screenSize.y };
//Comprobamos si hay intersecci�n entre ambos.
if (!SDL_HasIntersection(&screen, objectRect))
if (!SDL_HasIntersection(&_screenRect, objectRect))
{
//UE_LOG_TRACE("Se omite el renderizado de un objeto al estar fuera de la c�mara.");
return false;
@@ -220,16 +219,12 @@ namespace UE
UE_LOG_ERROR("Can't add DrawCall to Layer {0} -> out of range\n", drawCall.layer);
return;
}
//Comprobamos si el objeto es visible.
//
//Generamos un rect que representa la pantalla.
SDL_Rect screen{ 0, 0, screenSize.x, screenSize.y };
//Comprobamos si hay intersecci�n entre ambos.
if (!SDL_HasIntersection(&screen, &drawCall.pos_rect))
{
//UE_LOG_TRACE("Se omite el renderizado de un objeto al estar fuera de la c�mara.");
return;
}
// //Comprobamos si el objeto es visible.
// if(!IsObjectVisible(&drawCall.pos_rect))
// {
// //Si no es visible, no lo renderizaríamos.
// return;
// }
renderLayers.at(drawCall.layer).push_back(drawCall);
//rendererCounter++;
+15 -4
View File
@@ -8,6 +8,7 @@
#include <glm/vec2.hpp>
#include "Drawable.h"
#include "Camera.h"
//
// TODO:
@@ -80,12 +81,18 @@ namespace UE
inline glm::ivec2 GetScreenSize() { return screenSize; }
void SetScreenSize(int newWidth, int newHeight);
inline glm::ivec2 GetCamPosition() { return camPosition; }
void SetCamPosition(int posX, int posY);
//Permite cambiar el color de fondo.
void SetDrawingColor(SDL_Color& newColor);
//Cambia la cámara que se utiliza en el renderizado.
inline void SetCamera(Camera* newCam) { activeCam = newCam; }
inline Camera* GetCamera() { return activeCam; }
/* 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).
//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);
@@ -128,7 +135,11 @@ namespace UE
//int screenHeight = 480;
glm::ivec2 screenSize{ 640,480 };
glm::ivec2 camPosition{ 0,0 };
//glm::ivec2 camPosition{ 0,0 };
Camera* activeCam;
SDL_Rect _screenRect{ 0, 0, screenSize.x, screenSize.y};
//DEBUG
+2
View File
@@ -3,6 +3,8 @@
#include "Logging/Logger.h"
#include "Render/Renderer.h"
#include "Render/Camera.h"
#include "Drawables/Sprite.h"
#include "Assets/AssetManager.h"
+7
View File
@@ -0,0 +1,7 @@
/*
Aquí habrá que definir los tipos custom para UberEngine.
Por ejemplo:
- glm::ivec2 -> UE_Vec2
*/
+10 -9
View File
@@ -116,7 +116,7 @@ void Loop()
}
if (eventHandler.type == SDL_WINDOWEVENT && eventHandler.window.event == SDL_WINDOWEVENT_RESIZED)
{
//GAME_LOG_CRITICAL("Window Size chaged {0}x{1}", eventHandler.window.data1, eventHandler.window.data2);
GAME_LOG_INFO("Window Size chaged {0}x{1}", eventHandler.window.data1, eventHandler.window.data2);
UE_Renderer->SetScreenSize(eventHandler.window.data1, eventHandler.window.data2);
}
if (eventHandler.type == SDL_KEYDOWN)
@@ -146,29 +146,30 @@ void Loop()
}
UE::Camera* cam = UE_Renderer->GetCamera();
if (keyStates[SDL_SCANCODE_W])
{
glm::ivec2 camPos = UE_Renderer->GetCamPosition();
glm::ivec2 camPos = cam->GetCamPosition();
camPos.y -= 5;
UE_Renderer->SetCamPosition(camPos.x, camPos.y);
cam->SetCamPosition(camPos.x, camPos.y);
}
if (keyStates[SDL_SCANCODE_S])
{
glm::ivec2 camPos = UE_Renderer->GetCamPosition();
glm::ivec2 camPos = cam->GetCamPosition();
camPos.y += 5;
UE_Renderer->SetCamPosition(camPos.x, camPos.y);
cam->SetCamPosition(camPos.x, camPos.y);
}
if (keyStates[SDL_SCANCODE_D])
{
glm::ivec2 camPos = UE_Renderer->GetCamPosition();
glm::ivec2 camPos = cam->GetCamPosition();
camPos.x += 5;
UE_Renderer->SetCamPosition(camPos.x, camPos.y);
cam->SetCamPosition(camPos.x, camPos.y);
}
if (keyStates[SDL_SCANCODE_A])
{
glm::ivec2 camPos = UE_Renderer->GetCamPosition();
glm::ivec2 camPos = cam->GetCamPosition();
camPos.x -= 5;
UE_Renderer->SetCamPosition(camPos.x, camPos.y);
cam->SetCamPosition(camPos.x, camPos.y);
}
currentScene->SceneUpdate();
+8
View File
@@ -1,6 +1,7 @@
//#include "UberEngine.h"
#pragma once
#include "Engine.h"
#include "Scene/Scene.h"
#include "Render/Renderer.h"
#include "Assets/AssetManager.h"
@@ -33,6 +34,13 @@ public:
{
UE_LOG_INFO("Escena {0} ha sido cargada", _sceneName);
//Configuramos las cosas del Renderer.
//UE::Engine::GetRenderer()->SetCamPosition(150, 25);
SDL_Color newColor{40, 160, 195, 255};
UE::Engine::GetRenderer()->SetDrawingColor(newColor);
UE::AssetManager* as = UE::Engine::GetAssetManager();
UE::Texture* test1Texture = as->getTexture("test1");