/*
 * cfsdl12.h: the part of the SDL 1.2 API that Don Ceferino Hazaña uses, on top of SDL2, for
 * the fredygames.com browser build (Emscripten). Implemented in cfsdl12.c.
 *
 * Keys: SDL 1.2 indexes the keyboard state by key symbol, and the game reads it that way
 * (tecla[SDLK_LEFT]). Here every key symbol is "folded" to a small index: printable keys keep their
 * code (the same in SDL 1.2 and SDL2), and the keys SDL2 numbers from scancodes (arrows, F keys,
 * keypad, page up/down) become 256 + scancode. The SDLK_ names the game uses are redefined to those
 * indexes, SDL_GetKeyState returns a table indexed the same way, and SDL_PollEvent folds keysym.sym
 * before the game sees it.
 *
 * Copyright (C) 2026 FredyGames. GNU GPL version 2 or later, like Don Ceferino Hazaña.
 */
#ifndef CF_SDL12_H
#define CF_SDL12_H

#include <SDL.h>

#ifdef __cplusplus
extern "C" {
#endif

/* surface and video mode flags of SDL 1.2 (only the ones that still mean something are honoured) */
#define SDL_SWSURFACE 0x00000000
#define SDL_HWSURFACE 0x00000001
#define SDL_ASYNCBLIT 0x00000004
#define SDL_ANYFORMAT 0x10000000
#define SDL_HWPALETTE 0x20000000
#define SDL_DOUBLEBUF 0x40000000
#define SDL_FULLSCREEN 0x80000000
#define SDL_OPENGL 0x00000002
#define SDL_OPENGLBLIT 0x0000000A
#define SDL_RESIZABLE 0x00000010
#define SDL_HWACCEL 0x00000100
#define SDL_SRCCOLORKEY 0x00001000
#define SDL_SRCALPHA 0x00010000
#define SDL_INIT_EVENTTHREAD 0x01000000
/* SDL2's "everything" includes subsystems the browser does not have (haptic, sensors), and SDL_Init
 * fails if one of them cannot start */
#undef SDL_INIT_EVERYTHING
#define SDL_INIT_EVERYTHING (SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_EVENTS)

/* SDL 1.2 events that SDL2 does not have; they never arrive */
#define SDL_ACTIVEEVENT 0x7F00
#define SDL_VIDEORESIZE 0x7F01
#define SDL_VIDEOEXPOSE 0x7F02
#define SDL_APPMOUSEFOCUS 0x01
#define SDL_APPINPUTFOCUS 0x02
#define SDL_APPACTIVE 0x04

typedef SDL_Keysym SDL_keysym;
typedef SDL_Keycode SDLKey;
typedef SDL_Keymod SDLMod;

typedef struct
{
  Uint32 hw_available:1;
  Uint32 wm_available:1;
  Uint32 blit_hw:1;
  Uint32 blit_hw_CC:1;
  Uint32 blit_hw_A:1;
  Uint32 blit_sw:1;
  Uint32 blit_sw_CC:1;
  Uint32 blit_sw_A:1;
  Uint32 blit_fill:1;
  Uint32 video_mem;
  SDL_PixelFormat *vfmt;
  int current_w;
  int current_h;
} SDL_VideoInfo;

const SDL_VideoInfo *SDL_GetVideoInfo (void);
SDL_Rect **SDL_ListModes (SDL_PixelFormat * format, Uint32 flags);
int SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags);
SDL_Surface *SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags);
SDL_Surface *SDL_GetVideoSurface (void);
int SDL_Flip (SDL_Surface * screen);
void SDL_UpdateRect (SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);
void SDL_UpdateRects (SDL_Surface * screen, int numrects, SDL_Rect * rects);
void SDL_WM_SetCaption (const char *title, const char *icon);
void SDL_WM_SetIcon (SDL_Surface * icon, Uint8 * mask);
int SDL_WM_ToggleFullScreen (SDL_Surface * surface);
int SDL_EnableKeyRepeat (int delay, int interval);
int SDL_EnableUNICODE (int enable);
Uint8 *SDL_GetKeyState (int *numkeys);
int SDL_SetAlpha (SDL_Surface * surface, Uint32 flags, Uint8 alpha);
SDL_Surface *SDL_DisplayFormat (SDL_Surface * surface);
SDL_Surface *SDL_DisplayFormatAlpha (SDL_Surface * surface);
void SDL_KillThread (SDL_Thread * thread);
int cf_PollEvent (SDL_Event * event);
SDL_Thread *cf_CreateThread (int (*fn) (void *), void *data);
/* SDL 1.2 took a depth that does not fit the masks (the game asks for its 32-bit images with the masks
 * of the 16-bit screen); SDL2 rejects it, so the depth that fits the masks is used */
SDL_Surface *cf_CreateRGBSurface (Uint32 flags, int width, int height, int depth, Uint32 rmask,
                                  Uint32 gmask, Uint32 bmask, Uint32 amask);
/* SDL 1.2 kept the color key and the surface alpha in the pixel format */
Uint32 cf_ColorKey (SDL_Surface * surface);
Uint8 cf_SurfaceAlpha (SDL_Surface * surface);
#define SDL_PollEvent cf_PollEvent
#define SDL_CreateThread cf_CreateThread
#define SDL_CreateRGBSurface cf_CreateRGBSurface
/* SDL2's SDL_Keysym has no unicode field; cf_PollEvent stores the character in its spare field */
#define unicode unused

/* key symbols folded to the indexes of SDL_GetKeyState (see above) */
#define CF_KEY(scancode) (256 + (scancode))
#undef SDLK_UP
#undef SDLK_DOWN
#undef SDLK_LEFT
#undef SDLK_RIGHT
#undef SDLK_F1
#undef SDLK_PAGEUP
#undef SDLK_PAGEDOWN
#undef SDLK_KP_ENTER
#undef SDLK_KP_MINUS
#undef SDLK_KP_PLUS
#define SDLK_UP CF_KEY (SDL_SCANCODE_UP)
#define SDLK_DOWN CF_KEY (SDL_SCANCODE_DOWN)
#define SDLK_LEFT CF_KEY (SDL_SCANCODE_LEFT)
#define SDLK_RIGHT CF_KEY (SDL_SCANCODE_RIGHT)
#define SDLK_F1 CF_KEY (SDL_SCANCODE_F1)
#define SDLK_PAGEUP CF_KEY (SDL_SCANCODE_PAGEUP)
#define SDLK_PAGEDOWN CF_KEY (SDL_SCANCODE_PAGEDOWN)
#define SDLK_KP_ENTER CF_KEY (SDL_SCANCODE_KP_ENTER)
#define SDLK_KP_MINUS CF_KEY (SDL_SCANCODE_KP_MINUS)
#define SDLK_KP_PLUS CF_KEY (SDL_SCANCODE_KP_PLUS)

#ifdef __cplusplus
}
#endif

#endif
