#pragma once
#include "includes.h"
#include "config.h"

void set_pixel_rgb24(int x, int y, Uint32 pixel, SDL_Surface* surface);
Uint32 get_pixel_rgb24(int x, int y, SDL_Surface* surface);
Uint32 get_pixel_rgb32(int x, int y, SDL_Surface* surface);

inline void sample_nearest(DT_FP u, DT_FP v, SDL_Surface* texture, DT_FP result[3]) {
	glLib::Math::Vec::fillzeros3(result);
	Uint8 r,g,b,a;
	SDL_GetRGBA(
		get_pixel_rgb32(
			glLib::Math::Misc::clamp(glLib::Math::Misc::rndint(u),0,texture->w),
			glLib::Math::Misc::clamp(glLib::Math::Misc::rndint(v),0,texture->h),
			texture
		),
		texture->format,
		&r,&g,&b,&a
	);
	result[0] += r/255.0;
	result[1] += g/255.0;
	result[2] += b/255.0;
}

void sample_cubic(DT_FP u, DT_FP v, SDL_Surface* texture, DT_FP result[3]);

void sample_bilinear(DT_FP u, DT_FP v, SDL_Surface* texture, DT_FP result[3]);

void sample_gauss(DT_FP u, DT_FP v, SDL_Surface* texture, DT_FP result[3], int radius);

inline void sample_default(DT_FP u, DT_FP v, SDL_Surface* texture, DT_FP result[3]) {
	#ifdef SAMPLE_TEXTURE_DEFAULT_NEAREST
		sample_nearest(u,v,texture,result);
	#elif defined SAMPLE_TEXTURE_DEFAULT_BILINEAR
		sample_bilinear(u,v,texture,result);
	#elif defined SAMPLE_TEXTURE_DEFAULT_BICUBIC
		sample_cubic(u,v,texture,result);
	#elif defined SAMPLE_TEXTURE_DEFAULT_GAUSS
		sample_gauss(u,v,texture,result,SAMPLES_GAUSS_DEFAULT);
	#else
		#error "Unknown default texture sampling type!"
	#endif
}

inline void save_surface(SDL_Surface* image, const char* path) {
	SDL_SaveBMP(image,path);
}