Skip to content

Instantly share code, notes, and snippets.

@klaussilveira
Last active September 19, 2019 20:02
Show Gist options
  • Save klaussilveira/3ab34d2e62f1ed1f2abc3174cbc33c7d to your computer and use it in GitHub Desktop.
Save klaussilveira/3ab34d2e62f1ed1f2abc3174cbc33c7d to your computer and use it in GitHub Desktop.
PHP 7.4 FFI + SDL2
#define FFI_SCOPE "SDL"
#define FFI_LIB "libSDL2.so"
typedef uint8_t Uint8;
typedef uint16_t Uint16;
typedef uint32_t Uint32;
typedef uint64_t Uint64;
extern int SDL_Init(Uint32 flags);
extern int SDL_InitSubSystem(Uint32 flags);
extern void SDL_QuitSubSystem(Uint32 flags);
extern Uint32 SDL_WasInit(Uint32 flags);
extern void SDL_Quit(void);
typedef enum { SDL_FALSE = 0, SDL_TRUE = 1 } SDL_bool;
typedef struct SDL_Point {
int x;
int y;
} SDL_Point;
typedef struct SDL_Rect {
int x, y;
int w, h;
} SDL_Rect;
typedef struct {
Uint32 format;
int w;
int h;
int refresh_rate;
void *driverdata;
} SDL_DisplayMode;
typedef struct SDL_Color {
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
} SDL_Color;
#define SDL_Colour SDL_Color
typedef struct SDL_Palette {
int ncolors;
SDL_Color *colors;
Uint32 version;
int refcount;
} SDL_Palette;
typedef struct SDL_PixelFormat {
Uint32 format;
SDL_Palette *palette;
Uint8 BitsPerPixel;
Uint8 BytesPerPixel;
Uint8 padding[2];
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
Uint8 Rloss;
Uint8 Gloss;
Uint8 Bloss;
Uint8 Aloss;
Uint8 Rshift;
Uint8 Gshift;
Uint8 Bshift;
Uint8 Ashift;
int refcount;
struct SDL_PixelFormat *next;
} SDL_PixelFormat;
typedef struct SDL_Surface {
Uint32 flags;
SDL_PixelFormat *format;
int w, h;
int pitch;
void *pixels;
void *userdata;
int locked;
void *lock_data;
SDL_Rect clip_rect;
struct SDL_BlitMap *map;
int refcount;
} SDL_Surface;
typedef struct SDL_Window SDL_Window;
typedef enum {
SDL_WINDOW_FULLSCREEN = 0x00000001,
SDL_WINDOW_OPENGL = 0x00000002,
SDL_WINDOW_SHOWN = 0x00000004,
SDL_WINDOW_HIDDEN = 0x00000008,
SDL_WINDOW_BORDERLESS = 0x00000010,
SDL_WINDOW_RESIZABLE = 0x00000020,
SDL_WINDOW_MINIMIZED = 0x00000040,
SDL_WINDOW_MAXIMIZED = 0x00000080,
SDL_WINDOW_INPUT_GRABBED = 0x00000100,
SDL_WINDOW_INPUT_FOCUS = 0x00000200,
SDL_WINDOW_MOUSE_FOCUS = 0x00000400,
SDL_WINDOW_FULLSCREEN_DESKTOP = (SDL_WINDOW_FULLSCREEN | 0x00001000),
SDL_WINDOW_FOREIGN = 0x00000800,
SDL_WINDOW_ALLOW_HIGHDPI = 0x00002000,
SDL_WINDOW_MOUSE_CAPTURE = 0x00004000,
SDL_WINDOW_ALWAYS_ON_TOP = 0x00008000,
SDL_WINDOW_SKIP_TASKBAR = 0x00010000,
SDL_WINDOW_UTILITY = 0x00020000,
SDL_WINDOW_TOOLTIP = 0x00040000,
SDL_WINDOW_POPUP_MENU = 0x00080000,
SDL_WINDOW_VULKAN = 0x10000000
} SDL_WindowFlags;
typedef enum {
SDL_WINDOWEVENT_NONE,
SDL_WINDOWEVENT_SHOWN,
SDL_WINDOWEVENT_HIDDEN,
SDL_WINDOWEVENT_EXPOSED,
SDL_WINDOWEVENT_MOVED,
SDL_WINDOWEVENT_RESIZED,
SDL_WINDOWEVENT_SIZE_CHANGED,
SDL_WINDOWEVENT_MINIMIZED,
SDL_WINDOWEVENT_MAXIMIZED,
SDL_WINDOWEVENT_RESTORED,
SDL_WINDOWEVENT_ENTER,
SDL_WINDOWEVENT_LEAVE,
SDL_WINDOWEVENT_FOCUS_GAINED,
SDL_WINDOWEVENT_FOCUS_LOST,
SDL_WINDOWEVENT_CLOSE,
SDL_WINDOWEVENT_TAKE_FOCUS,
SDL_WINDOWEVENT_HIT_TEST
} SDL_WindowEventID;
typedef enum {
SDL_DISPLAYEVENT_NONE,
SDL_DISPLAYEVENT_ORIENTATION
} SDL_DisplayEventID;
typedef enum {
SDL_ORIENTATION_UNKNOWN,
SDL_ORIENTATION_LANDSCAPE,
SDL_ORIENTATION_LANDSCAPE_FLIPPED,
SDL_ORIENTATION_PORTRAIT,
SDL_ORIENTATION_PORTRAIT_FLIPPED
} SDL_DisplayOrientation;
typedef void *SDL_GLContext;
typedef enum {
SDL_GL_RED_SIZE,
SDL_GL_GREEN_SIZE,
SDL_GL_BLUE_SIZE,
SDL_GL_ALPHA_SIZE,
SDL_GL_BUFFER_SIZE,
SDL_GL_DOUBLEBUFFER,
SDL_GL_DEPTH_SIZE,
SDL_GL_STENCIL_SIZE,
SDL_GL_ACCUM_RED_SIZE,
SDL_GL_ACCUM_GREEN_SIZE,
SDL_GL_ACCUM_BLUE_SIZE,
SDL_GL_ACCUM_ALPHA_SIZE,
SDL_GL_STEREO,
SDL_GL_MULTISAMPLEBUFFERS,
SDL_GL_MULTISAMPLESAMPLES,
SDL_GL_ACCELERATED_VISUAL,
SDL_GL_RETAINED_BACKING,
SDL_GL_CONTEXT_MAJOR_VERSION,
SDL_GL_CONTEXT_MINOR_VERSION,
SDL_GL_CONTEXT_EGL,
SDL_GL_CONTEXT_FLAGS,
SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_SHARE_WITH_CURRENT_CONTEXT,
SDL_GL_FRAMEBUFFER_SRGB_CAPABLE,
SDL_GL_CONTEXT_RELEASE_BEHAVIOR,
SDL_GL_CONTEXT_RESET_NOTIFICATION,
SDL_GL_CONTEXT_NO_ERROR
} SDL_GLattr;
typedef enum {
SDL_GL_CONTEXT_PROFILE_CORE = 0x0001,
SDL_GL_CONTEXT_PROFILE_COMPATIBILITY = 0x0002,
SDL_GL_CONTEXT_PROFILE_ES = 0x0004
} SDL_GLprofile;
typedef enum {
SDL_GL_CONTEXT_DEBUG_FLAG = 0x0001,
SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG = 0x0002,
SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG = 0x0004,
SDL_GL_CONTEXT_RESET_ISOLATION_FLAG = 0x0008
} SDL_GLcontextFlag;
typedef enum {
SDL_GL_CONTEXT_RELEASE_BEHAVIOR_NONE = 0x0000,
SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH = 0x0001
} SDL_GLcontextReleaseFlag;
typedef enum {
SDL_GL_CONTEXT_RESET_NO_NOTIFICATION = 0x0000,
SDL_GL_CONTEXT_RESET_LOSE_CONTEXT = 0x0001
} SDL_GLContextResetNotification;
extern int SDL_GetNumVideoDrivers(void);
extern const char *SDL_GetVideoDriver(int index);
extern int SDL_VideoInit(const char *driver_name);
extern void SDL_VideoQuit(void);
extern const char *SDL_GetCurrentVideoDriver(void);
extern int SDL_GetNumVideoDisplays(void);
extern const char *SDL_GetDisplayName(int displayIndex);
extern int SDL_GetDisplayBounds(int displayIndex, SDL_Rect *rect);
extern int SDL_GetDisplayUsableBounds(int displayIndex, SDL_Rect *rect);
extern int SDL_GetDisplayDPI(int displayIndex, float *ddpi, float *hdpi,
float *vdpi);
extern SDL_DisplayOrientation SDL_GetDisplayOrientation(int displayIndex);
extern int SDL_GetNumDisplayModes(int displayIndex);
extern int SDL_GetDisplayMode(int displayIndex, int modeIndex,
SDL_DisplayMode *mode);
extern int SDL_GetDesktopDisplayMode(int displayIndex, SDL_DisplayMode *mode);
extern int SDL_GetCurrentDisplayMode(int displayIndex, SDL_DisplayMode *mode);
extern SDL_DisplayMode *SDL_GetClosestDisplayMode(int displayIndex,
const SDL_DisplayMode *mode,
SDL_DisplayMode *closest);
extern int SDL_GetWindowDisplayIndex(SDL_Window *window);
extern int SDL_SetWindowDisplayMode(SDL_Window *window,
const SDL_DisplayMode *mode);
extern int SDL_GetWindowDisplayMode(SDL_Window *window, SDL_DisplayMode *mode);
extern Uint32 SDL_GetWindowPixelFormat(SDL_Window *window);
extern SDL_Window *SDL_CreateWindow(const char *title, int x, int y, int w,
int h, Uint32 flags);
extern SDL_Window *SDL_CreateWindowFrom(const void *data);
extern Uint32 SDL_GetWindowID(SDL_Window *window);
extern SDL_Window *SDL_GetWindowFromID(Uint32 id);
extern Uint32 SDL_GetWindowFlags(SDL_Window *window);
extern void SDL_SetWindowTitle(SDL_Window *window, const char *title);
extern const char *SDL_GetWindowTitle(SDL_Window *window);
extern void SDL_SetWindowIcon(SDL_Window *window, SDL_Surface *icon);
extern void *SDL_SetWindowData(SDL_Window *window, const char *name,
void *userdata);
extern void *SDL_GetWindowData(SDL_Window *window, const char *name);
extern void SDL_SetWindowPosition(SDL_Window *window, int x, int y);
extern void SDL_GetWindowPosition(SDL_Window *window, int *x, int *y);
extern void SDL_SetWindowSize(SDL_Window *window, int w, int h);
extern void SDL_GetWindowSize(SDL_Window *window, int *w, int *h);
extern int SDL_GetWindowBordersSize(SDL_Window *window, int *top, int *left,
int *bottom, int *right);
extern void SDL_SetWindowMinimumSize(SDL_Window *window, int min_w, int min_h);
extern void SDL_GetWindowMinimumSize(SDL_Window *window, int *w, int *h);
extern void SDL_SetWindowMaximumSize(SDL_Window *window, int max_w, int max_h);
extern void SDL_GetWindowMaximumSize(SDL_Window *window, int *w, int *h);
extern void SDL_SetWindowBordered(SDL_Window *window, SDL_bool bordered);
extern void SDL_SetWindowResizable(SDL_Window *window, SDL_bool resizable);
extern void SDL_ShowWindow(SDL_Window *window);
extern void SDL_HideWindow(SDL_Window *window);
extern void SDL_RaiseWindow(SDL_Window *window);
extern void SDL_MaximizeWindow(SDL_Window *window);
extern void SDL_MinimizeWindow(SDL_Window *window);
extern void SDL_RestoreWindow(SDL_Window *window);
extern int SDL_SetWindowFullscreen(SDL_Window *window, Uint32 flags);
extern SDL_Surface *SDL_GetWindowSurface(SDL_Window *window);
extern int SDL_UpdateWindowSurface(SDL_Window *window);
extern int SDL_UpdateWindowSurfaceRects(SDL_Window *window,
const SDL_Rect *rects, int numrects);
extern void SDL_SetWindowGrab(SDL_Window *window, SDL_bool grabbed);
extern SDL_bool SDL_GetWindowGrab(SDL_Window *window);
extern SDL_Window *SDL_GetGrabbedWindow(void);
extern int SDL_SetWindowBrightness(SDL_Window *window, float brightness);
extern float SDL_GetWindowBrightness(SDL_Window *window);
extern int SDL_SetWindowOpacity(SDL_Window *window, float opacity);
extern int SDL_GetWindowOpacity(SDL_Window *window, float *out_opacity);
extern int SDL_SetWindowModalFor(SDL_Window *modal_window,
SDL_Window *parent_window);
extern int SDL_SetWindowInputFocus(SDL_Window *window);
extern int SDL_SetWindowGammaRamp(SDL_Window *window, const Uint16 *red,
const Uint16 *green, const Uint16 *blue);
extern int SDL_GetWindowGammaRamp(SDL_Window *window, Uint16 *red,
Uint16 *green, Uint16 *blue);
typedef enum {
SDL_HITTEST_NORMAL,
SDL_HITTEST_DRAGGABLE,
SDL_HITTEST_RESIZE_TOPLEFT,
SDL_HITTEST_RESIZE_TOP,
SDL_HITTEST_RESIZE_TOPRIGHT,
SDL_HITTEST_RESIZE_RIGHT,
SDL_HITTEST_RESIZE_BOTTOMRIGHT,
SDL_HITTEST_RESIZE_BOTTOM,
SDL_HITTEST_RESIZE_BOTTOMLEFT,
SDL_HITTEST_RESIZE_LEFT
} SDL_HitTestResult;
typedef SDL_HitTestResult (*SDL_HitTest)(SDL_Window *win, const SDL_Point *area,
void *data);
extern int SDL_SetWindowHitTest(SDL_Window *window, SDL_HitTest callback,
void *callback_data);
extern void SDL_DestroyWindow(SDL_Window *window);
extern SDL_bool SDL_IsScreenSaverEnabled(void);
extern void SDL_EnableScreenSaver(void);
extern void SDL_DisableScreenSaver(void);
extern int SDL_GL_LoadLibrary(const char *path);
extern void *SDL_GL_GetProcAddress(const char *proc);
extern void SDL_GL_UnloadLibrary(void);
extern SDL_bool SDL_GL_ExtensionSupported(const char *extension);
extern void SDL_GL_ResetAttributes(void);
extern int SDL_GL_SetAttribute(SDL_GLattr attr, int value);
extern int SDL_GL_GetAttribute(SDL_GLattr attr, int *value);
extern SDL_GLContext SDL_GL_CreateContext(SDL_Window *window);
extern int SDL_GL_MakeCurrent(SDL_Window *window, SDL_GLContext context);
extern SDL_Window *SDL_GL_GetCurrentWindow(void);
extern SDL_GLContext SDL_GL_GetCurrentContext(void);
extern void SDL_GL_GetDrawableSize(SDL_Window *window, int *w, int *h);
extern int SDL_GL_SetSwapInterval(int interval);
extern int SDL_GL_GetSwapInterval(void);
extern void SDL_GL_SwapWindow(SDL_Window *window);
extern void SDL_GL_DeleteContext(SDL_GLContext context);
extern void SDL_Delay(Uint32 ms);
<?php
$sdl = FFI::load(__DIR__ . '/sdl.h');
$sdl->SDL_Init(2);
$window = $sdl->SDL_CreateWindow('Hello World', 0, 0, 640, 480, 0);
$surface = $sdl->SDL_GetWindowSurface($window);
$sdl->SDL_UpdateWindowSurface($window);
$sdl->SDL_Delay(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment