lexsdl 0.3.0

A wrapper for SDL2 to abstract away annoying parts
#include"LEXSDL.h"
#include"LEXSDL_internal.h"

/* Creation/Loading */

LEXSDL_TextureID LEXSDL_TextureAdd(SDL_Texture *texture){
	if (texture == NULL)
		return LEXSDL_TextureInvalid;
	
	for(int i = 0; i < (int)LEXSDLDATA.texturesQuantity; i++){
		if(!LEXSDLDATA.textures[i]){
			LEXSDLDATA.textures[i] = texture;
			return i;
		}
	}
	
	if (LEXSDLDATA.textures == NULL){
		LEXSDLDATA.textures = calloc(1,sizeof(SDL_Texture*));
		if(LEXSDLDATA.textures == NULL)
			return LEXSDL_TextureAddingFailed;
		LEXSDLDATA.textures[LEXSDLDATA.texturesQuantity] = texture;
	}
	
	SDL_Texture **newptr = realloc(LEXSDLDATA.textures,(LEXSDLDATA.texturesQuantity+1) * sizeof(SDL_Texture*));
	
	if(newptr == NULL)
		return LEXSDL_TextureAddingFailed;
	
	LEXSDLDATA.textures = newptr;
	LEXSDLDATA.textures[LEXSDLDATA.texturesQuantity] = texture;
	
	LEXSDLDATA.texturesQuantity++;
	return LEXSDLDATA.texturesQuantity-1;
}

LEXSDL_TextureID LEXSDL_TextureLoad(const char* file){
	SDL_Texture *texture = IMG_LoadTexture(LEXSDLDATA.renderer,file);
	
	if(!texture)
		return LEXSDL_TextureCreationFailed;
	
	return LEXSDL_TextureAdd(texture);
}

LEXSDL_TextureID LEXSDL_TextureLoadBytes(const unsigned char* data, int len){
	SDL_RWops *buf = SDL_RWFromConstMem((const void*)data, len);
	SDL_Texture *texture = IMG_LoadTexture_RW(LEXSDLDATA.renderer, buf, 1);

	if(!texture)
		return LEXSDL_TextureCreationFailed;
	
	return LEXSDL_TextureAdd(texture);
}



/* Getters */
SDL_Texture* LEXSDL_GetTexture(LEXSDL_TextureID id){
	return LEXSDLDATA.textures[id];
}



/* Manipulation */

int LEXSDL_TextureChangeAlpha(LEXSDL_TextureID id, uint8_t a){
	if((id >= LEXSDLDATA.texturesQuantity) || !LEXSDLDATA.textures[id])
		return LEXSDL_TextureIDInvalid;
	return SDL_SetTextureAlphaMod(LEXSDLDATA.textures[id], a);
}

int LEXSDL_TextureChangeColor(LEXSDL_TextureID id, uint8_t r, uint8_t g, uint8_t b){
	if((id >= LEXSDLDATA.texturesQuantity) || !LEXSDLDATA.textures[id])
		return LEXSDL_TextureIDInvalid;
	return SDL_SetTextureColorMod(LEXSDLDATA.textures[id], r,g,b);
}

int LEXSDL_TextureDestroy(LEXSDL_TextureID id){
	if((id >= LEXSDLDATA.texturesQuantity) || !LEXSDLDATA.textures[id])
		return LEXSDL_TextureIDInvalid;
	
	SDL_DestroyTexture(LEXSDLDATA.textures[id]);
	LEXSDLDATA.textures[id] = NULL;
	return LEXSDL_TextureOperationSucceded;
}



/* Drawing */

void LEXSDL_TextureDraw(LEXSDL_TextureID id, SDL_Rect *srcrect, SDL_Rect *dstrect){
	SDL_RenderCopy(LEXSDLDATA.renderer, LEXSDLDATA.textures[id], srcrect, dstrect);
}

void LEXSDL_TextureDrawAt(LEXSDL_TextureID id, int x, int y, int w, int h){
	SDL_Rect dstrect = {x,y,w,h};
	SDL_RenderCopy(LEXSDLDATA.renderer, LEXSDLDATA.textures[id], NULL, &dstrect);
}

void LEXSDL_TextureDrawFill(LEXSDL_TextureID id){
	SDL_RenderCopy(LEXSDLDATA.renderer, LEXSDLDATA.textures[id], NULL, NULL);
}

void LEXSDL_TextureDrawRot(LEXSDL_TextureID id, SDL_Rect *srcrect, SDL_Rect *dstrect, double rot){
	SDL_RenderCopyEx(LEXSDLDATA.renderer, LEXSDLDATA.textures[id], srcrect,dstrect, rot, NULL, SDL_FLIP_NONE);
}

void LEXSDL_TextureDrawRotAt(LEXSDL_TextureID id, int x, int y, int w, int h, double rot){
		SDL_Rect dstrect = {x,y,w,h};
		SDL_RenderCopyEx(LEXSDLDATA.renderer, LEXSDLDATA.textures[id], NULL,&dstrect, rot, NULL, SDL_FLIP_NONE);
}

void LEXSDL_TextureDrawRotCenterAt(LEXSDL_TextureID id, int x, int y, int w, int h, double rot, int cx, int cy){
		SDL_Rect dstrect = {x,y,w,h};
		SDL_Point point = {cx, cy};
		SDL_RenderCopyEx(LEXSDLDATA.renderer, LEXSDLDATA.textures[id], NULL,&dstrect, rot, &point, SDL_FLIP_NONE);
}