/*
 * tbzsdl12.h: the part of the SDL 1.2 API that TecnoballZ uses, on top of SDL2, for the fredygames.com
 * browser build (Emscripten). Implemented in tbzsdl12.c.
 *
 * Keys: SDL 1.2 indexes the keyboard state by key symbol, and the game keeps SDL 1.2 symbols in its
 * tables (keys[SDLK_LEFT], key_codes[], keysym.sym). 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, modifiers, 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 3 or later, like TecnoballZ.
 */
#ifndef TBZ_SDL12_H
#define TBZ_SDL12_H

#include <SDL.h>

#ifdef __cplusplus
extern "C" {
#endif

/* SDL_SetVideoMode, SDL_CreateRGBSurface and SDL_SetPalette flags of SDL 1.2 */
#define SDL_SWSURFACE 0x00000000
#define SDL_HWSURFACE 0x00000001
#define SDL_ANYFORMAT 0x10000000
#define SDL_HWPALETTE 0x20000000
#define SDL_FULLSCREEN 0x80000000
#define SDL_LOGPAL 0x01
#define SDL_PHYSPAL 0x02

typedef enum
{
  SDL_GRAB_QUERY = -1,
  SDL_GRAB_OFF = 0,
  SDL_GRAB_ON = 1
} SDL_GrabMode;

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;
} 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);
int SDL_Flip (SDL_Surface * screen);
void SDL_UpdateRect (SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);
int SDL_SetPalette (SDL_Surface * surface, int flags, const SDL_Color * colors, int firstcolor,
                    int ncolors);
void SDL_WM_SetCaption (const char *title, const char *icon);
SDL_GrabMode SDL_WM_GrabInput (SDL_GrabMode mode);
void SDL_WarpMouse (Uint16 x, Uint16 y);
char *SDL_VideoDriverName (char *namebuf, int maxlen);
int SDL_EnableUNICODE (int enable);
Uint8 *SDL_GetKeyState (int *numkeys);
int tbz_PollEvent (SDL_Event * event);
/* SDL 1.2 ignores the color masks of 8-bit surfaces; SDL2 rejects the ones the game passes */
SDL_Surface *tbz_CreateRGBSurface (Uint32 flags, int width, int height, int depth, Uint32 rmask,
                                   Uint32 gmask, Uint32 bmask, Uint32 amask);
#define SDL_CreateRGBSurface tbz_CreateRGBSurface
#define SDL_PollEvent tbz_PollEvent
#define SDL_JoystickName(index) SDL_JoystickNameForIndex (index)
/* SDL2's SDL_Keysym has no unicode field; tbz_PollEvent stores the character in its spare field */
#define unicode unused

/* key symbols folded to the indexes of SDL_GetKeyState (see above) */
#define TBZ_KEY(scancode) (256 + (scancode))
#undef SDLK_UP
#undef SDLK_DOWN
#undef SDLK_LEFT
#undef SDLK_RIGHT
#define SDLK_UP TBZ_KEY (SDL_SCANCODE_UP)
#define SDLK_DOWN TBZ_KEY (SDL_SCANCODE_DOWN)
#define SDLK_LEFT TBZ_KEY (SDL_SCANCODE_LEFT)
#define SDLK_RIGHT TBZ_KEY (SDL_SCANCODE_RIGHT)
#define SDLK_LSHIFT TBZ_KEY (SDL_SCANCODE_LSHIFT)
#define SDLK_RSHIFT TBZ_KEY (SDL_SCANCODE_RSHIFT)
#define SDLK_LCTRL TBZ_KEY (SDL_SCANCODE_LCTRL)
#define SDLK_RCTRL TBZ_KEY (SDL_SCANCODE_RCTRL)
#define SDLK_LALT TBZ_KEY (SDL_SCANCODE_LALT)
#define SDLK_RALT TBZ_KEY (SDL_SCANCODE_RALT)
#define SDLK_PAGEUP TBZ_KEY (SDL_SCANCODE_PAGEUP)
#define SDLK_PAGEDOWN TBZ_KEY (SDL_SCANCODE_PAGEDOWN)
#define SDLK_F1 TBZ_KEY (SDL_SCANCODE_F1)
#define SDLK_F2 TBZ_KEY (SDL_SCANCODE_F2)
#define SDLK_F3 TBZ_KEY (SDL_SCANCODE_F3)
#define SDLK_F4 TBZ_KEY (SDL_SCANCODE_F4)
#define SDLK_F5 TBZ_KEY (SDL_SCANCODE_F5)
#define SDLK_F6 TBZ_KEY (SDL_SCANCODE_F6)
#define SDLK_F7 TBZ_KEY (SDL_SCANCODE_F7)
#define SDLK_F8 TBZ_KEY (SDL_SCANCODE_F8)
#define SDLK_F9 TBZ_KEY (SDL_SCANCODE_F9)
#define SDLK_F10 TBZ_KEY (SDL_SCANCODE_F10)
#define SDLK_F11 TBZ_KEY (SDL_SCANCODE_F11)
#define SDLK_F12 TBZ_KEY (SDL_SCANCODE_F12)

#ifdef __cplusplus
}
#endif

#endif
