Code:
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
typedef struct{
//Textura real do hardware
SDL_Texture* mTexture;
//Dimensão da imagem
int mWidth;
int mHeight;
};LTexture;
LTexture_Create();
//Deallocate memory
LTexture_Destroy();
//Loads image on specified path
bool LTexture_loadFromFile( const char *path );
//Deallocate texture
void LTexture_free();
//Color Modulation
void LTexture_setColor( Uint8 red, Uint8 green, Uint8 blue );
//Set blending
void LTexture_setBlendMode( SDL_BlendMode blending );
//Set alpha modulation
void LTexture_setAlpha( Uint8 alpha );
//Renders texture on given point
void LTexture_render( int x, int y, SDL_Rect* clip = NULL );
//Gets image dimension
int LTexture_getWidth();
int LTexture_getHeight();
LTexture_Create(LTexture *texture)
{
//Initialize
texture->mTexture = NULL;
texture->mWidth = 0;
texture->mHeight = 0;
}
LTexture_Destroy()
{
//Deallocate
free();
}
bool LTexture_loadFromFile(LTexture *texture; const char *path )
{
//Get rid of preexisting texture
free();
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError() );
}
else
{
//Color key image
SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0x69, 0 ) );
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
if( newTexture == NULL )
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError() );
}
else
{
//Get image dimensions
texture->mWidth = loadedSurface->w;
texture->mHeight = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
//Return success
texture->mTexture = newTexture;
return texture.mTexture != NULL;
}
void LTexture_free(LTexture *texture)
{
//Free texture if it exists
if( texture.mTexture != NULL )
{
SDL_DestroyTexture( texture.mTexture );
texture->mTexture = NULL;
texture->mWidth = 0;
texture->mHeight = 0;
}
}
void LTexture_setColor(LTexture *texture, Uint8 red, Uint8 green, Uint8 blue )
{
//Modulate texture rgb
SDL_SetTextureColorMod( texture.mTexture, red, green, blue );
}
void LTexture_setBlendMode(LTexture *texture, SDL_BlendMode blending )
{
//Set blending function
SDL_SetTextureBlendMode( texture.mTexture, blending );
}
void LTexture_setAlpha(LTexture *texture, Uint8 alpha )
{
//Modulate texture alpha
SDL_SetTextureAlphaMod( texture.mTexture, alpha );
}
void LTexture_render(LTexture *texture; int x, int y, SDL_Rect* clip )
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y,texture.mWidth, texture.mHeight };
//Set clip rendering dimensions
if( clip != NULL )
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopy( gRenderer, texture.mTexture, clip, &renderQuad );
}
int LTexture_getWidth(LTexture *texture)
{
return texture.mWidth;
}
int LTexture_getHeight(LTexture *texture)
{
return texture.mHeight;
}
/////////////////////////////////////////////////////////////////////
I believe this is a better attempt, but stuff just seem to get worse. How can I fix this ?