eliminada la dependencia de sdl2_image
This commit is contained in:
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"iostream": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Makefile
2
Makefile
@@ -2,4 +2,4 @@ source := source/*.cpp
|
|||||||
executable_name := pelota
|
executable_name := pelota
|
||||||
|
|
||||||
windows:
|
windows:
|
||||||
g++ $(source) -lmingw32 -lws2_32 -lSDL2main -lSDL2 -lSDL2_image -o $(executable_name).exe
|
g++ $(source) -lmingw32 -lws2_32 -lSDL2main -lSDL2 -o $(executable_name).exe
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_image.h>
|
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include "ball.h"
|
#include "ball.h"
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
@@ -98,14 +97,7 @@ bool init()
|
|||||||
// Initialize renderer color
|
// Initialize renderer color
|
||||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||||
|
|
||||||
// Initialize PNG loading
|
// Establece el tamaño del renderizador
|
||||||
int imgFlags = IMG_INIT_PNG;
|
|
||||||
if (!(IMG_Init(imgFlags) & imgFlags))
|
|
||||||
{
|
|
||||||
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,7 +136,6 @@ void close()
|
|||||||
deleteBalls();
|
deleteBalls();
|
||||||
|
|
||||||
// Quit SDL subsystems
|
// Quit SDL subsystems
|
||||||
IMG_Quit();
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
7897
source/stb_image.h
Normal file
7897
source/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,12 +1,13 @@
|
|||||||
#include <SDL2/SDL_image.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include "stb_image.h"
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
|
|
||||||
Texture::Texture(SDL_Renderer *renderer)
|
Texture::Texture(SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
this->renderer = renderer;
|
this->renderer = renderer;
|
||||||
texture = NULL;
|
texture = nullptr;
|
||||||
width = 0;
|
width = 0;
|
||||||
height = 0;
|
height = 0;
|
||||||
}
|
}
|
||||||
@@ -14,7 +15,7 @@ Texture::Texture(SDL_Renderer *renderer)
|
|||||||
Texture::Texture(SDL_Renderer *renderer, std::string filepath)
|
Texture::Texture(SDL_Renderer *renderer, std::string filepath)
|
||||||
{
|
{
|
||||||
this->renderer = renderer;
|
this->renderer = renderer;
|
||||||
texture = NULL;
|
texture = nullptr;
|
||||||
width = 0;
|
width = 0;
|
||||||
height = 0;
|
height = 0;
|
||||||
loadFromFile(filepath);
|
loadFromFile(filepath);
|
||||||
@@ -27,52 +28,79 @@ Texture::~Texture()
|
|||||||
|
|
||||||
bool Texture::loadFromFile(std::string path)
|
bool Texture::loadFromFile(std::string path)
|
||||||
{
|
{
|
||||||
// Get rid of preexisting texture
|
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||||
free();
|
int req_format = STBI_rgb_alpha;
|
||||||
|
int width, height, orig_format;
|
||||||
// The final texture
|
unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format);
|
||||||
SDL_Texture *newTexture = NULL;
|
if (data == nullptr)
|
||||||
|
|
||||||
// Load image at specified path
|
|
||||||
SDL_Surface *loadedSurface = IMG_Load(path.c_str());
|
|
||||||
if (loadedSurface == NULL)
|
|
||||||
{
|
{
|
||||||
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
|
SDL_Log("Loading image failed: %s", stbi_failure_reason());
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Color key image
|
std::cout << "Image loaded: " << filename.c_str() << std::endl;
|
||||||
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));
|
}
|
||||||
|
|
||||||
// Create texture from surface pixels
|
int depth, pitch;
|
||||||
|
Uint32 pixel_format;
|
||||||
|
if (req_format == STBI_rgb)
|
||||||
|
{
|
||||||
|
depth = 24;
|
||||||
|
pitch = 3 * width; // 3 bytes por pixel * pixels por linea
|
||||||
|
pixel_format = SDL_PIXELFORMAT_RGB24;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // STBI_rgb_alpha (RGBA)
|
||||||
|
depth = 32;
|
||||||
|
pitch = 4 * width;
|
||||||
|
pixel_format = SDL_PIXELFORMAT_RGBA32;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limpia
|
||||||
|
free();
|
||||||
|
|
||||||
|
// La textura final
|
||||||
|
SDL_Texture *newTexture = nullptr;
|
||||||
|
|
||||||
|
// Carga la imagen desde una ruta específica
|
||||||
|
SDL_Surface *loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom((void *)data, width, height, depth, pitch, pixel_format);
|
||||||
|
if (loadedSurface == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "Unable to load image " << path.c_str() << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Crea la textura desde los pixels de la surface
|
||||||
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
|
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
|
||||||
if (newTexture == NULL)
|
if (newTexture == nullptr)
|
||||||
{
|
{
|
||||||
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
|
std::cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Get image dimensions
|
// Obtiene las dimensiones de la imagen
|
||||||
width = loadedSurface->w;
|
this->width = loadedSurface->w;
|
||||||
height = loadedSurface->h;
|
this->height = loadedSurface->h;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get rid of old loaded surface
|
// Elimina la textura cargada
|
||||||
SDL_FreeSurface(loadedSurface);
|
SDL_FreeSurface(loadedSurface);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return success
|
// Return success
|
||||||
|
stbi_image_free(data);
|
||||||
texture = newTexture;
|
texture = newTexture;
|
||||||
return texture != NULL;
|
return texture != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::free()
|
void Texture::free()
|
||||||
{
|
{
|
||||||
// Free texture if it exists
|
// Free texture if it exists
|
||||||
if (texture != NULL)
|
if (texture != nullptr)
|
||||||
{
|
{
|
||||||
SDL_DestroyTexture(texture);
|
SDL_DestroyTexture(texture);
|
||||||
texture = NULL;
|
texture = nullptr;
|
||||||
width = 0;
|
width = 0;
|
||||||
height = 0;
|
height = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user