From 5f263fa71d2af3a8f00759111d138c9d71c19e7f Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Mon, 5 Dec 2022 10:07:27 +0100 Subject: [PATCH] Cambiando printf por std::cout --- source/common/texture.cpp | 34 +++++++++++++++++++++++++--------- source/common/texture.h | 7 +++++-- source/director.cpp | 10 +++++----- source/fade.cpp | 5 ++++- source/hiscore_table.cpp | 3 ++- source/instructions.cpp | 3 ++- source/main.cpp | 4 ++-- 7 files changed, 45 insertions(+), 21 deletions(-) diff --git a/source/common/texture.cpp b/source/common/texture.cpp index fa190d5..dce9ed2 100644 --- a/source/common/texture.cpp +++ b/source/common/texture.cpp @@ -2,9 +2,10 @@ #include "texture.h" #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" +#include // Constructor -Texture::Texture(SDL_Renderer *renderer, std::string path) +Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose) { // Copia punteros this->renderer = renderer; @@ -18,7 +19,7 @@ Texture::Texture(SDL_Renderer *renderer, std::string path) // Carga el fichero en la textura if (path != "") { - loadFromFile(path, renderer); + loadFromFile(path, renderer, verbose); } } @@ -30,9 +31,9 @@ Texture::~Texture() } // 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 width, height, orig_format; unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format); @@ -43,7 +44,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) } else { - printf("Image loaded: %s\n", filename.c_str()); + if (verbose) + { + std::cout << "Image loaded: " << filename.c_str() << std::endl; + } } 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); 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 { @@ -79,7 +86,10 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface); 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 { @@ -92,7 +102,7 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer) SDL_FreeSurface(loadedSurface); } - // Devuelve el resultado + // Return success stbi_image_free(data); texture = newTexture; 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); 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 { @@ -189,4 +199,10 @@ int Texture::getHeight() bool Texture::reLoad() { return loadFromFile(path, renderer); +} + +// Obtiene la textura +SDL_Texture *Texture::getSDLTexture() +{ + return texture; } \ No newline at end of file diff --git a/source/common/texture.h b/source/common/texture.h index 52e8ce9..d83d7e2 100644 --- a/source/common/texture.h +++ b/source/common/texture.h @@ -21,13 +21,13 @@ private: public: // Constructor - Texture(SDL_Renderer *renderer, std::string path = ""); + Texture(SDL_Renderer *renderer, std::string path = "", bool verbose = false); // Destructor ~Texture(); // 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 bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING); @@ -58,6 +58,9 @@ public: // Recarga la textura bool reLoad(); + + // Obtiene la textura + SDL_Texture *getSDLTexture(); }; #endif diff --git a/source/director.cpp b/source/director.cpp index 908d965..e7b9d32 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -108,7 +108,7 @@ bool Director::initSDL() if (SDL_Init(SDL_INIT_EVERYTHING) < 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; } else @@ -119,7 +119,7 @@ bool Director::initSDL() // Establece el filtro de la textura 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 @@ -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); 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; } else @@ -150,7 +150,7 @@ bool Director::initSDL() 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; } else @@ -167,7 +167,7 @@ bool Director::initSDL() } } - printf("\n"); + std::cout << std::endl; return success; } diff --git a/source/fade.cpp b/source/fade.cpp index 48823bc..78e986e 100644 --- a/source/fade.cpp +++ b/source/fade.cpp @@ -1,5 +1,6 @@ #include "fade.h" #include "const.h" +#include // Constructor 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); 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 diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index af8ae86..46794ef 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -1,5 +1,6 @@ #include "hiscore_table.h" #include "common/jscore.h" +#include 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); 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 diff --git a/source/instructions.cpp b/source/instructions.cpp index 124c7c3..754e86a 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -1,4 +1,5 @@ #include "instructions.h" +#include 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); 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 diff --git a/source/main.cpp b/source/main.cpp index b902b37..babfef2 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -44,7 +44,7 @@ Reescribiendo el código el 27/09/2022 int main(int argc, char *args[]) { - printf("Starting the game...\n\n"); + std::cout << "Starting the game...\n\n"; // Crea el objeto Director Director *director = new Director(args[0]); @@ -55,7 +55,7 @@ int main(int argc, char *args[]) // Destruye el objeto Director delete director; - printf("\nShutting down the game...\n"); + std::cout << "\nShutting down the game..." << std::endl; return 0; }