#ifndef SURFACE_H
#define SURFACE_H

SDL_Surface *surface_from_file(const char *filepath);

#if defined(SURFACE_IMP) || defined(IMP)

#ifdef STB_IMAGE_IMPLEMENTATION
  #undef STB_IMAGE_IMPLEMENTATION
  #include "stb_image.h"
  #define STB_IMAGE_IMPLEMENTATION
#else
  #include "stb_image.h"
#endif

#include "sc.h"

SDL_Surface *
surface_from_file(const char *filepath)
{
	int32_t w, h, n;
	uint8_t *data = stbi_load(filepath, &w, &h, &n, STBI_rgb_alpha);

	if ( data == NULL ) {
		fprintf(stderr, "ERROR loading file %s: %s",
			filepath, stbi_failure_reason());
		exit(EXIT_FAILURE);
	}

	uint32_t rmask = 0xFF000000;
	uint32_t gmask = 0x00FF0000;
	uint32_t bmask = 0x0000FF00;
	uint32_t amask = 0x000000FF;

	int32_t depth = 32;
	int32_t pitch = 4 * w;

	SDL_Surface *sur = SDL_CreateRGBSurfaceFrom(data,
						    w, h,
						    depth, pitch,
						    rmask, gmask, bmask, amask);
	SCP(sur);
	return sur;
}

#endif /* defined(SURFACE_IMP) || defined(IMP) */

#endif /* SURFACE_H */
