/* * The SDL 1.2 calls Fish Fillets NG uses, on top of SDL2 (see compat/SDL.h). * Copyright (C) 2026 FredyGames. GNU GPL version 2 or later, like Fish Fillets NG. */ #include #undef SDL_SetColorKey #undef SDL_PollEvent #undef SDL_GetKeyName #include extern "C" { static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; static SDL_Texture *texture = NULL; static SDL_Surface *screen = NULL; static Uint8 keystate[1024]; static int fold(SDL_Keycode sym) { if (sym & SDLK_SCANCODE_MASK) { return 256 + (sym & ~SDLK_SCANCODE_MASK); } return sym < 256 ? sym : 0; } static SDL_Keycode unfold(int sym) { if (sym >= 256) { return (SDL_Keycode)((sym - 256) | SDLK_SCANCODE_MASK); } return sym; } SDL_Surface *fy_SetVideoMode(int width, int height, int bpp, Uint32 flags) { (void)bpp; (void)flags; if (window == NULL) { window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0); if (window == NULL) { return NULL; } renderer = SDL_CreateRenderer(window, -1, 0); if (renderer == NULL) { return NULL; } } else { SDL_SetWindowSize(window, width, height); } if (texture) { SDL_DestroyTexture(texture); } if (screen) { SDL_FreeSurface(screen); } screen = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_RGB888); texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, width, height); if (screen == NULL || texture == NULL) { return NULL; } return screen; } SDL_Surface *fy_GetVideoSurface(void) { return screen; } int fy_Flip(SDL_Surface *s) { if (s == NULL || texture == NULL) { return -1; } SDL_UpdateTexture(texture, NULL, s->pixels, s->pitch); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); return 0; } void fy_WM_SetCaption(const char *title, const char *icon) { (void)icon; if (window) { SDL_SetWindowTitle(window, title); } } Uint16 fy_Unicode(int sym, Uint16 mod) { if (sym < 32 || sym > 126) { return sym == '\b' || sym == '\r' || sym == '\t' ? sym : 0; } bool shift = (mod & KMOD_SHIFT) != 0; if (sym >= 'a' && sym <= 'z') { bool caps = (mod & KMOD_CAPS) != 0; return (shift != caps) ? sym - 32 : sym; } if (shift) { static const char from[] = "1234567890-=[];'`\\,./"; static const char to[] = "!@#$%^&*()_+{}:\"~|<>?"; const char *p = strchr(from, sym); if (p && *p) { return to[p - from]; } } return sym; } int fy_PollEvent(SDL_Event *event) { while (SDL_PollEvent(event)) { if (event->type == SDL_KEYDOWN || event->type == SDL_KEYUP) { int sym = fold(event->key.keysym.sym); if (sym == 0) { continue; } keystate[sym] = event->type == SDL_KEYDOWN ? 1 : 0; event->key.keysym.sym = sym; return 1; } if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP || event->type == SDL_QUIT) { return 1; } } return 0; } Uint8 *fy_GetKeyState(int *numkeys) { if (numkeys) { *numkeys = 1024; } return keystate; } const char *fy_GetKeyName(int sym) { return SDL_GetKeyName(unfold(sym)); } SDL_Surface *fy_DisplayFormat(SDL_Surface *surface) { return SDL_ConvertSurface(surface, screen->format, 0); } SDL_Surface *fy_DisplayFormatAlpha(SDL_Surface *surface) { return SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0); } int fy_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key) { return SDL_SetColorKey(surface, (flag & 0x1000) ? SDL_TRUE : SDL_FALSE, key); } Uint32 fy_GetColorKey(SDL_Surface *surface) { Uint32 key = 0; SDL_GetColorKey(surface, &key); return key; } int fy_SetAlpha(SDL_Surface *surface, Uint32 flag, Uint8 alpha) { if (flag & 0x10000) { SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND); return SDL_SetSurfaceAlphaMod(surface, alpha); } SDL_SetSurfaceAlphaMod(surface, 255); return SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE); } void fy_WarpMouse(Uint16 x, Uint16 y) { if (window) { SDL_WarpMouseInWindow(window, x, y); } } }