/*
 * FredyGames: camada minima de SDL 1.2 sobre SDL2, so o que o Meritous usa (-include no build).
 *
 * O Meritous desenha em uma tela de 8 bits com paleta e anima a paleta FISICA (SDL_PHYSPAL) sem mexer nos pixels; as imagens
 * sao PNGs em tons de cinza lidas como indices. A emulacao de SDL 1.2 do Emscripten nao faz isso, entao as superficies de 8
 * bits passam a ser as do SDL2 (INDEX8, paleta logica) e a tela mostrada e montada a cada SDL_UpdateRect, com a paleta fisica
 * guardada a parte (sdl1shim.c).
 *
 * Copyright (C) 2026 FredyGames. Este arquivo e o sdl1shim.c sao distribuidos sob a GPL-3.0-or-later, como o jogo.
 */
#ifndef FREDYGAMES_SDL1SHIM_H
#define FREDYGAMES_SDL1SHIM_H

#include <SDL.h>
#include <emscripten.h>

#define SDL_SWSURFACE 0
#define SDL_FULLSCREEN 1
#define SDL_SRCCOLORKEY 1
#define SDL_LOGPAL 0x01
#define SDL_PHYSPAL 0x02

SDL_Surface *SDL1_SetVideoMode(int width, int height, int bpp, Uint32 flags);
void SDL1_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);
int SDL1_SetPalette(SDL_Surface *surface, int flags, const SDL_Color *colors, int firstcolor, int ncolors);
int SDL1_PollEvent(SDL_Event *event);

static inline int SDL1_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
{
	return SDL_SetColorKey(surface, (flag & SDL_SRCCOLORKEY) ? SDL_TRUE : SDL_FALSE, key);
}
static inline void SDL1_WM_SetCaption(const char *title, const char *icon) { (void)title; (void)icon; }
static inline void SDL1_WM_SetIcon(SDL_Surface *icon, Uint8 *mask) { (void)icon; (void)mask; }
static inline int SDL1_SaveBMP(SDL_Surface *surface, const char *file) { (void)surface; (void)file; return 0; }

#define SDL_SetVideoMode SDL1_SetVideoMode
#define SDL_UpdateRect SDL1_UpdateRect
#define SDL_SetPalette SDL1_SetPalette
#define SDL_PollEvent SDL1_PollEvent
#define SDL_SetColorKey SDL1_SetColorKey
#define SDL_WM_SetCaption SDL1_WM_SetCaption
#define SDL_WM_SetIcon SDL1_WM_SetIcon
#undef SDL_SaveBMP
#define SDL_SaveBMP SDL1_SaveBMP
/* O laco do jogo espera o fim do quadro com SDL_Delay: aqui isso devolve o controle ao navegador (ASYNCIFY). */
#undef SDL_Delay
#define SDL_Delay(ms) emscripten_sleep(ms)

#endif
