Cambiando printf por std::cout

This commit is contained in:
2022-12-05 10:07:27 +01:00
parent a32582f1ec
commit 5f263fa71d
7 changed files with 45 additions and 21 deletions

View File

@@ -2,9 +2,10 @@
#include "texture.h" #include "texture.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#include <iostream>
// Constructor // Constructor
Texture::Texture(SDL_Renderer *renderer, std::string path) Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
{ {
// Copia punteros // Copia punteros
this->renderer = renderer; this->renderer = renderer;
@@ -18,7 +19,7 @@ Texture::Texture(SDL_Renderer *renderer, std::string path)
// Carga el fichero en la textura // Carga el fichero en la textura
if (path != "") if (path != "")
{ {
loadFromFile(path, renderer); loadFromFile(path, renderer, verbose);
} }
} }
@@ -30,7 +31,7 @@ Texture::~Texture()
} }
// Carga una imagen desde un fichero // Carga una imagen desde un fichero
bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose)
{ {
const std::string filename = path.substr(path.find_last_of("\\/") + 1); const std::string filename = path.substr(path.find_last_of("\\/") + 1);
int req_format = STBI_rgb_alpha; int req_format = STBI_rgb_alpha;
@@ -43,7 +44,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer)
} }
else else
{ {
printf("Image loaded: %s\n", filename.c_str()); if (verbose)
{
std::cout << "Image loaded: " << filename.c_str() << std::endl;
}
} }
int depth, pitch; int depth, pitch;
@@ -71,7 +75,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer)
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format); SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
if (loadedSurface == nullptr) if (loadedSurface == nullptr)
{ {
printf("Unable to load image %s!\n", path.c_str()); if (verbose)
{
std::cout << "Unable to load image " << path.c_str() << std::endl;
}
} }
else else
{ {
@@ -79,7 +86,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer)
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface); newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == nullptr) if (newTexture == nullptr)
{ {
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); if (verbose)
{
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
}
} }
else else
{ {
@@ -92,7 +102,7 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer)
SDL_FreeSurface(loadedSurface); SDL_FreeSurface(loadedSurface);
} }
// Devuelve el resultado // Return success
stbi_image_free(data); stbi_image_free(data);
texture = newTexture; texture = newTexture;
return texture != nullptr; return texture != nullptr;
@@ -105,7 +115,7 @@ bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_Tex
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height); texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, access, width, height);
if (texture == nullptr) if (texture == nullptr)
{ {
printf("Unable to create blank texture! SDL Error: %s\n", SDL_GetError()); std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl;
} }
else else
{ {
@@ -190,3 +200,9 @@ bool Texture::reLoad()
{ {
return loadFromFile(path, renderer); return loadFromFile(path, renderer);
} }
// Obtiene la textura
SDL_Texture *Texture::getSDLTexture()
{
return texture;
}

View File

@@ -21,13 +21,13 @@ private:
public: public:
// Constructor // Constructor
Texture(SDL_Renderer *renderer, std::string path = ""); Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false);
// Destructor // Destructor
~Texture(); ~Texture();
// Carga una imagen desde un fichero // Carga una imagen desde un fichero
bool loadFromFile(std::string path, SDL_Renderer *renderer); bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false);
// Crea una textura en blanco // Crea una textura en blanco
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
@@ -58,6 +58,9 @@ public:
// Recarga la textura // Recarga la textura
bool reLoad(); bool reLoad();
// Obtiene la textura
SDL_Texture *getSDLTexture();
}; };
#endif #endif

View File

@@ -108,7 +108,7 @@ bool Director::initSDL()
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
// if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0) // if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0)
{ {
printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError()); std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
success = false; success = false;
} }
else else
@@ -119,7 +119,7 @@ bool Director::initSDL()
// Establece el filtro de la textura // Establece el filtro de la textura
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->filter).c_str())) if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->filter).c_str()))
{ {
printf("Warning: Nearest texture filtering not enabled!\n"); std::cout << "Warning: Nearest texture filtering not enabled!\n";
} }
// Crea la ventana // Crea la ventana
@@ -133,7 +133,7 @@ bool Director::initSDL()
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (options->gameWidth + incW) * options->windowSize, (options->gameHeight + incH) * options->windowSize, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (options->gameWidth + incW) * options->windowSize, (options->gameHeight + incH) * options->windowSize, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == nullptr) if (window == nullptr)
{ {
printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError()); std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
success = false; success = false;
} }
else else
@@ -150,7 +150,7 @@ bool Director::initSDL()
if (renderer == nullptr) if (renderer == nullptr)
{ {
printf("Renderer could not be created!\nSDL Error: %s\n", SDL_GetError()); std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
success = false; success = false;
} }
else else
@@ -167,7 +167,7 @@ bool Director::initSDL()
} }
} }
printf("\n"); std::cout << std::endl;
return success; return success;
} }

View File

@@ -1,5 +1,6 @@
#include "fade.h" #include "fade.h"
#include "const.h" #include "const.h"
#include <iostream>
// Constructor // Constructor
Fade::Fade(SDL_Renderer *renderer) Fade::Fade(SDL_Renderer *renderer)
@@ -8,7 +9,9 @@ Fade::Fade(SDL_Renderer *renderer)
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (mBackbuffer == nullptr) if (mBackbuffer == nullptr)
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError()); {
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
} }
// Destructor // Destructor

View File

@@ -1,5 +1,6 @@
#include "hiscore_table.h" #include "hiscore_table.h"
#include "common/jscore.h" #include "common/jscore.h"
#include <iostream>
const Uint8 SELF = 0; const Uint8 SELF = 0;
@@ -21,7 +22,7 @@ HiScoreTable::HiScoreTable(SDL_Renderer *renderer, Screen *screen, Asset *asset,
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (backbuffer == nullptr) if (backbuffer == nullptr)
{ {
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError()); std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
} }
// Inicializa variables // Inicializa variables

View File

@@ -1,4 +1,5 @@
#include "instructions.h" #include "instructions.h"
#include <iostream>
const Uint8 SELF = 0; const Uint8 SELF = 0;
@@ -39,7 +40,7 @@ Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset,
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (backbuffer == nullptr) if (backbuffer == nullptr)
{ {
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError()); std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
} }
// Inicializa variables // Inicializa variables

View File

@@ -44,7 +44,7 @@ Reescribiendo el código el 27/09/2022
int main(int argc, char *args[]) int main(int argc, char *args[])
{ {
printf("Starting the game...\n\n"); std::cout << "Starting the game...\n\n";
// Crea el objeto Director // Crea el objeto Director
Director *director = new Director(args[0]); Director *director = new Director(args[0]);
@@ -55,7 +55,7 @@ int main(int argc, char *args[])
// Destruye el objeto Director // Destruye el objeto Director
delete director; delete director;
printf("\nShutting down the game...\n"); std::cout << "\nShutting down the game..." << std::endl;
return 0; return 0;
} }