/*
SDLX extension to SDL, by Ali Arya, 2023
The main purpose of SDL is to hide the texture/surface operations in SDL from non-experienced programmers
*/
#pragma once
#include <SDL.h>
//#include <SDL_image.h>


//data types
struct SDLX_Bitmap
{
	SDL_Surface* surface; //surface is used when we have a software buffer 
	SDL_Texture* texture; //texture is used when directly writing to renderer
	int width;
	int height;
};
struct SDLX_Color
{
	int r;
	int g;
	int b;
	int a;
};

struct SDLX_Event
{
	SDL_Event sdl_event;
	SDL_Keycode keycode;	
	Uint8 button;
	Uint32 type;
};


//global variables
extern SDL_Window* window;
extern SDL_Renderer *renderer;
extern SDL_Surface* buffer;
//extern SDLX_Bitmap* screen;
extern SDL_Keycode last_code;
extern SDLX_Color color_mask;

//functions

//SDL system functions
//SDLX_Init: the only note when using a buffer:
//if we are not using a buffer, then we have created a texture for all bitmaps to be used in rendering
//   in that case, after any putpixel, we have to refresh the bitmap texture by calling SDLX_CreateBitmapTexture
bool SDLX_Init(const char* title, int w, int h, bool useBuffer=false);
void SDLX_End();
void SDLX_Delay(int n);
bool SDLX_PollEvent(SDLX_Event* e);

//use these two when using a buffer
void SDLX_ClearBuffer();
void SDLX_RenderBuffer();
//use these two when not using a buffer 
void SDLX_Render();
void SDLX_Clear();

//bitmaps
void SDLX_DestroyBitmap(SDLX_Bitmap* bmp);
SDLX_Bitmap* SDLX_LoadBitmap(const char * fname, bool useMask = true, SDLX_Color* mask = NULL);
void SDLX_SetTransparentColor(SDLX_Bitmap* bmp, char r, char g, char b);
//if we are using texture for rendering we have to call this at the start and before render for a frame if pixels have changed
void SDLX_CreateBitmapTexture(SDLX_Bitmap* bmp);
//can use surface/buffer or textre/renderer
void SDLX_DrawBitmap(SDLX_Bitmap* bmp, int x, int y, int v=100);
//getpixel using surface. Will work only for a bitmap
void SDLX_GetPixel(SDLX_Bitmap* bmp, int x, int y, SDLX_Color* c);
//putpixel using surface (will work for a bitmap or screen/buffer, if bmp is NULL)
void SDLX_PutPixel(SDLX_Bitmap* bmp, int x, int y, SDLX_Color* c);
void SDLX_PutPixel(SDLX_Bitmap* bmp, int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a=255);
SDLX_Bitmap* SDLX_CloneBitmap(SDLX_Bitmap* src);


//events
//wait for key. saved in last_key
void SDLX_GetKey();