PantallaPrincipal.cpp implementado y sus consecuencias

parent d0b5a3b9
No preview for this file type
...@@ -6,22 +6,28 @@ ...@@ -6,22 +6,28 @@
/// Encapsula un puntero TTF_Font* para tener RAII /// Encapsula un puntero TTF_Font* para tener RAII
class Fuente { class Fuente {
const char *camino;
int tamano_pt;
TTF_Font* fuente; TTF_Font* fuente;
public: public:
Fuente(const char *camino, int tamano_pt) { Fuente(const char *camino, int tamano_pt) : camino(camino), tamano_pt(tamano_pt) {};
fuente = TTF_OpenFont(camino, tamano_pt);
if (fuente == nullptr) {
throw std::runtime_error(SDL_GetError());
}
};
TTF_Font *operator*() { TTF_Font *operator*() {
if (!fuente) {
fuente = TTF_OpenFont(camino, tamano_pt);
if (fuente == nullptr) {
throw std::runtime_error(SDL_GetError());
}
}
return fuente; return fuente;
} }
~Fuente() { ~Fuente() {
TTF_CloseFont(fuente); if (fuente) {
TTF_CloseFont(fuente);
}
} }
}; };
......
#include "GestorPantallas.h" #include "GestorPantallas.h"
#include <SDL2/SDL_render.h>
void Handle::reemplazamePor(shared_ptr<Pantalla> pant){ void Handle::reemplazamePor(shared_ptr<Pantalla> pant){
gestor.pantallas[idPantalla] =pant; gestor.pantallas[idPantalla] =pant;
...@@ -16,22 +17,34 @@ void Handle::removeAll(){ ...@@ -16,22 +17,34 @@ void Handle::removeAll(){
gestor.pantallas.clear(); gestor.pantallas.clear();
} }
void cambiar_buffers(SDL_Renderer *renderer) {
SDL_RenderPresent(renderer);
}
void GestorPantallas::mainLoop(){ void GestorPantallas::mainLoop(){
if (!inicializarSDL()) {
return;
}
std::cout << "Se ha iniciicididialci SDL" << std::endl;
while(!pantallas.empty()){ while(!pantallas.empty()){
int tamx=0; int tamx=0;
int tamy=0; int tamy=0;
SDL_GetWindowSize(window,&tamx,&tamy); SDL_GetWindowSize(window,&tamx,&tamy);
Handle hand(*this,pantallas.size()-1); Handle hand(*this,pantallas.size()-1);
pantallas.back()->manejarEntrada(hand); pantallas.back()->manejarEntrada(hand);
int indice=pantallas.size()-1; int indice=pantallas.size()-1;
while(pantallas[indice]->transparente()){ while(indice >= 0 && pantallas[indice]->transparente()){
indice--; indice--;
} }
for(int i=indice;i<pantallas.size();i++){ for(int i=indice;i<pantallas.size();i++){
pantallas[i]->renderizar(renderer,tamx,tamy); pantallas[i]->renderizar(renderer,tamx,tamy);
} }
cambiar_buffers(renderer);
SDL_Delay(5);
} }
} }
bool GestorPantallas::inicializarSDL(){ bool GestorPantallas::inicializarSDL(){
...@@ -75,9 +88,9 @@ bool GestorPantallas::inicializarSDL(){ ...@@ -75,9 +88,9 @@ bool GestorPantallas::inicializarSDL(){
SDL_SetWindowResizable(window, SDL_TRUE); SDL_SetWindowResizable(window, SDL_TRUE);
SDL_Renderer *Renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(!Renderer){ if(!renderer){
std::cout << "Error creating renderer" << std::endl; std::cout << "Error creating renderer" << std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl; std::cout << "Error: " << SDL_GetError() << std::endl;
......
#pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "SDL2/SDL_image.h" #include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h" #include "SDL2/SDL_ttf.h"
...@@ -40,9 +42,9 @@ class GestorPantallas{ ...@@ -40,9 +42,9 @@ class GestorPantallas{
class Pantalla{ class Pantalla{
public: public:
virtual void manejarEntrada(Handle &)=0; virtual void manejarEntrada(Handle &) = 0;
virtual void renderizar(SDL_Renderer*, int tamx, int tamy)=0; virtual void renderizar(SDL_Renderer*, int tamx, int tamy) = 0;
virtual bool transparente()=0; virtual bool transparente() = 0;
virtual ~Pantalla(); virtual ~Pantalla() {};
}; };
...@@ -5,7 +5,7 @@ Hitbox & Objeto::getHitbox(){ ...@@ -5,7 +5,7 @@ Hitbox & Objeto::getHitbox(){
} }
void Objeto::renderizar(SDL_Renderer *renderer, int tamx, int tamy){ void Objeto::renderizar(SDL_Renderer *renderer, int tamx, int tamy){
std::cout<<estado<<std::endl; std::cout<<"Buenos dias, soy el objeto y mi estado es " << estado<<std::endl;
switch (estado){ switch (estado){
case normal: case normal:
sprite_normal.renderizar(renderer,x,y,w,h, tamx, tamy); sprite_normal.renderizar(renderer,x,y,w,h, tamx, tamy);
......
#include "PantallaPrincipal.h"
PantallaPrincipal::PantallaPrincipal() :
fuente1("assets/OpenSans-Italic.ttf", 100),
Fondo1("assets/fondoHabitacion.jpg")
{
Sprite sprPlanta("assets/planta.png");
Hitbox hitPlanta(350,80,450,120);
Hitbox hitCama(10,50,300,300);
Hitbox hitFondo(1000000,-10000000,50,0);
Objeto Planta(sprPlanta,sprPlanta,sprPlanta,hitPlanta,1200,400,300,400, normal);
objetos.push_back(Planta);
colisiones.push_back(hitPlanta);
colisiones.push_back(hitCama);
colisiones.push_back(hitFondo);
}
void PantallaPrincipal::manejarEntrada(Handle &handle) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_LEFT:
Agapito.move(Izquierda);
break;
case SDLK_RIGHT:
Agapito.move(Derecha);
break;
case SDLK_UP:
Agapito.move(Fondo);
break;
case SDLK_DOWN:
Agapito.move(Frente);
break;
}
for(int i=0;i<colisiones.size();i++){
Agapito.comprobarColision(colisiones[i]);
}
break;
case SDL_QUIT:
handle.removeAll();
}
}
}
void PantallaPrincipal::renderizar(SDL_Renderer *renderer, int tamx, int tamy) {
Texto texto(fuente1, "AGAPITO 3D", {255, 255, 255});
Fondo1.renderizar(renderer, 0, 0,1920,1080, tamx, tamy);
for(int i=0;i<objetos.size();i++){
objetos[i].renderizar(renderer, tamx, tamy);
}
Agapito.renderizar(renderer, tamx, tamy);
texto.renderizar(renderer, 50, 50, tamx, tamy);
}
#pragma once
#include "GestorPantallas.h"
#include <vector>
#include "Objeto.h"
#include "Sprite.h"
#include "Texto.h"
#include "Fuente.h"
#include "Personaje.h"
class PantallaPrincipal : public Pantalla {
vector<Objeto> objetos;
vector<Hitbox> colisiones;
Fuente fuente1;
Sprite Fondo1;
Personaje Agapito;
public:
PantallaPrincipal();
void manejarEntrada(Handle &) override;
void renderizar(SDL_Renderer *, int tamx, int tamy) override;
bool transparente() override {
return false;
};
};
...@@ -11,6 +11,7 @@ SDL_Texture *Sprite::get_textura(SDL_Renderer *Renderer) { ...@@ -11,6 +11,7 @@ SDL_Texture *Sprite::get_textura(SDL_Renderer *Renderer) {
this->texture = IMG_LoadTexture(Renderer, camino.c_str()); this->texture = IMG_LoadTexture(Renderer, camino.c_str());
if (texture == nullptr) { if (texture == nullptr) {
printf("IMG_TEXTURE devuelve nulo lol\n"); printf("IMG_TEXTURE devuelve nulo lol\n");
printf("GET ERROR = ERROR %s\n", SDL_GetError());
} }
} }
return texture; return texture;
......
#include <SDL2/SDL.h> #include "GestorPantallas.h"
#include "SDL2/SDL_image.h" #include "PantallaPrincipal.h"
#include "SDL2/SDL_ttf.h" #include <memory>
#include <SDL2/SDL_video.h>
#include <iostream>
#include "Sprite.h"
#include "Texto.h"
#include "Fuente.h"
#include "Personaje.h"
#include "Objeto.h"
#include <vector>
void blit(SDL_Renderer *Renderer, SDL_Texture *Texture, SDL_Rect *Cut, int x, int y){
SDL_Rect Dest;
Dest.x = x;
Dest.y = y;
SDL_QueryTexture(Texture, NULL, NULL, &Dest.w, &Dest.h);
SDL_RenderCopy(Renderer, Texture, Cut, &Dest);
}
void cambiar_buffers(SDL_Renderer *renderer) {
SDL_RenderPresent(renderer);
}
int main(){ int main(){
GestorPantallas gestor(
std::shared_ptr<Pantalla>((Pantalla *)new PantallaPrincipal())
);
gestor.mainLoop();
vector<Objeto*> objetos;
vector<Hitbox*> colisiones;
Fuente opensans("assets/OpenSans-Italic.ttf", 100);
Texto texto(opensans, "AGAPITO 3D", {255, 255, 255});
Sprite Fondo1("assets/fondoHabitacion.jpg");
Sprite sprPlanta("assets/planta.png");
Hitbox hitPlanta(350,80,450,120);
Hitbox hitCama(10,50,300,300);
Hitbox hitFondo(1000000,-10000000,50,0);
Objeto Planta(sprPlanta,sprPlanta,sprPlanta,hitPlanta,1200,400,300,400, normal);
objetos.push_back(&Planta);
colisiones.push_back(&hitPlanta);
colisiones.push_back(&hitCama);
colisiones.push_back(&hitFondo);
Personaje Agapito;
SDL_Event event;
while (true) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_LEFT:
Agapito.move(Izquierda);
break;
case SDLK_RIGHT:
Agapito.move(Derecha);
break;
case SDLK_UP:
Agapito.move(Fondo);
break;
case SDLK_DOWN:
Agapito.move(Frente);
break;
}
for(int i=0;i<colisiones.size();i++){
Agapito.comprobarColision(*colisiones[i]);
}
break;
case SDL_QUIT:
exit(0);
}
}
std::cout<<"Dimensiones "<<tamx<<tamy<<endl;
Fondo1.renderizar(Renderer, 0, 0,1920,1080, tamx, tamy);
for(int i=0;i<objetos.size();i++){
objetos[i]->renderizar(Renderer, tamx, tamy);
}
Agapito.renderizar(Renderer, tamx, tamy);
texto.renderizar(Renderer, 50, 50, tamx, tamy);
cambiar_buffers(Renderer);
SDL_Delay(5);
}
return 0; return 0;
} }
CC = g++ CC = g++
HeaderFiles = Personaje.h Sprite.h Objeto.h Hitbox.h Texto.h GestorPantallas.h PantallaPrincipal.h HeaderFiles = Personaje.h Sprite.h Objeto.h Hitbox.h Texto.h GestorPantallas.h PantallaPrincipal.h Fuente.h
CodeFiles = main.cpp Personaje.cpp Sprite.cpp Objeto.cpp Hitbox.cpp Texto.cpp GestorPantallas.cpp PantallaPrincipal.cpp CodeFiles = main.cpp Personaje.cpp Sprite.cpp Objeto.cpp Hitbox.cpp Texto.cpp GestorPantallas.cpp PantallaPrincipal.cpp
Executable: $(CodeFiles) $(HeaderFiles) Executable: $(CodeFiles) $(HeaderFiles)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment