Gestion de pantallas

parent 9ce0ea72
#include "GestorPantallas.h"
void Handle::reemplazamePor(shared_ptr<Pantalla> pant){
gestor.pantallas[idPantalla] =pant;
}
void Handle::addPantalla(shared_ptr<Pantalla> pant){
gestor.pantallas.push_back(pant);
}
void Handle::removeme(){
gestor.pantallas.erase(gestor.pantallas.begin()+idPantalla);
}
void Handle::removeAll(){
gestor.pantallas.clear();
}
void GestorPantallas::mainLoop(){
while(!pantallas.empty()){
int tamx=0;
int tamy=0;
SDL_GetWindowSize(window,&tamx,&tamy);
Handle hand(*this,pantallas.size()-1);
pantallas.back()->manejarEntrada(hand);
int indice=pantallas.size()-1;
while(pantallas[indice]->transparente()){
indice--;
}
for(int i=indice;i<pantallas.size();i++){
pantallas[i]->renderizar(renderer,tamx,tamy);
}
}
}
bool GestorPantallas::inicializarSDL(){
if(SDL_Init(SDL_INIT_VIDEO) < 0){
std::cout << "Error at SDL_Init(SDL_INIT_VIDEO)"<<std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return false;
}
if(IMG_Init(IMG_INIT_PNG) < 0){
std::cout << "Error at IMG_Init(IMG_INIT_PNG);"<<std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return false;
}
if (TTF_Init() < 0) {
std::cout << "Error at TTF_Init();"<<std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return false;
}
window = SDL_CreateWindow(
"SDL2 Window",
SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,
1920,1080,
0
);
if(!window){
std::cout << "Error creating window" << std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return false;
}
SDL_SetWindowResizable(window, SDL_TRUE);
SDL_Renderer *Renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(!Renderer){
std::cout << "Error creating renderer" << std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return false;
}
return true;
}
#include <SDL2/SDL.h>
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_ttf.h"
#include <SDL2/SDL_video.h>
#include <memory>
#include <vector>
#include <iostream>
class Handle;
class Pantalla;
class GestorPantallas;
using namespace std;
class Handle{
int idPantalla;
GestorPantallas &gestor;
friend GestorPantallas;
Handle(GestorPantallas &gest, int id): gestor(gest), idPantalla(id){};
public:
void reemplazamePor(shared_ptr<Pantalla> );
void addPantalla(shared_ptr<Pantalla> );
void removeme();
void removeAll();
};
class GestorPantallas{
vector<shared_ptr<Pantalla>> pantallas;
SDL_Renderer *renderer;
SDL_Window *window;
friend Handle;
bool inicializarSDL();
public:
GestorPantallas(shared_ptr<Pantalla> pant){pantallas.push_back(pant);}
void mainLoop();
};
class Pantalla{
public:
virtual void manejarEntrada(Handle &)=0;
virtual void renderizar(SDL_Renderer*, int tamx, int tamy)=0;
virtual bool transparente()=0;
virtual ~Pantalla();
};
File mode changed
File mode changed
...@@ -30,56 +30,9 @@ void cambiar_buffers(SDL_Renderer *renderer) { ...@@ -30,56 +30,9 @@ void cambiar_buffers(SDL_Renderer *renderer) {
int main(){ int main(){
int tamx=0;
int tamy=0;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
std::cout << "Error at SDL_Init(SDL_INIT_VIDEO)"<<std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return -1;
}
if(IMG_Init(IMG_INIT_PNG) < 0){
std::cout << "Error at IMG_Init(IMG_INIT_PNG);"<<std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return -1;
}
if (TTF_Init() < 0) {
std::cout << "Error at TTF_Init();"<<std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Window *Window = SDL_CreateWindow(
"SDL2 Window",
SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,
1920,1080,
0
);
//SDL_SetWindowFullscreen(Window,SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_SetWindowResizable(Window, SDL_TRUE);
if(!Window){
std::cout << "Error creating window" << std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
if(!Renderer){
std::cout << "Error creating renderer" << std::endl;
std::cout << "Error: " << SDL_GetError() << std::endl;
return -1;
}
vector<Objeto*> objetos; vector<Objeto*> objetos;
vector<Hitbox*> colisiones; vector<Hitbox*> colisiones;
...@@ -104,7 +57,7 @@ int main(){ ...@@ -104,7 +57,7 @@ int main(){
SDL_Event event; SDL_Event event;
while (true) { while (true) {
SDL_GetWindowSize(Window,&tamx,&tamy);
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
......
CC = g++ CC = g++
HeaderFiles = Personaje.h Sprite.h Objeto.h Hitbox.h Texto.h HeaderFiles = Personaje.h Sprite.h Objeto.h Hitbox.h Texto.h GestorPantallas.h PantallaPrincipal.h
CodeFiles = main.cpp Personaje.cpp Sprite.cpp Objeto.cpp Hitbox.cpp Texto.cpp CodeFiles = main.cpp Personaje.cpp Sprite.cpp Objeto.cpp Hitbox.cpp Texto.cpp GestorPantallas.cpp PantallaPrincipal.cpp
Executable: $(CodeFiles) $(HeaderFiles) Executable: $(CodeFiles) $(HeaderFiles)
$(CC) $(CodeFiles) -w -lSDL2 -lSDL2_image -lSDL2_ttf -o Executable $(CC) $(CodeFiles) -w -lSDL2 -lSDL2_image -lSDL2_ttf -o Executable
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