github.com/gotranspile/cxgo@v0.3.8-0.20240118201721-29871598a6a2/libs/sdl2.go (about)

     1  package libs
     2  
     3  import (
     4  	"github.com/gotranspile/cxgo/types"
     5  )
     6  
     7  const (
     8  	sdl2H = "SDL2/SDL.h"
     9  )
    10  
    11  func init() {
    12  	RegisterLibrary(sdl2H, func(c *Env) *Library {
    13  		boolT := types.BoolT()
    14  		sintT := types.IntT(4)
    15  		uintT := types.UintT(4)
    16  		sint16T := types.IntT(2)
    17  		uint16T := types.UintT(2)
    18  		uint8T := types.UintT(1)
    19  		strT := c.C().String()
    20  		bufT := c.PtrT(nil)
    21  		scanT := types.NamedT("sdl.Scancode", sintT)
    22  		blendT := types.NamedTGo("SDL_BlendMode", "sdl.BlendMode", sintT)
    23  		timerT := types.NamedTGo("SDL_TimerID", "sdl.TimerID", sintT)
    24  		keymodT := types.NamedTGo("SDL_Keymod", "sdl.Keymod", sintT)
    25  		fingerT := types.NamedTGo("SDL_FingerID", "sdl.FingerID", sintT)
    26  		rectT := types.NamedT("sdl.Rect", types.StructT([]*types.Field{
    27  			{Name: types.NewIdentGo("x", "X", sint16T)},
    28  			{Name: types.NewIdentGo("y", "Y", sint16T)},
    29  			{Name: types.NewIdentGo("w", "W", uint16T)},
    30  			{Name: types.NewIdentGo("h", "H", uint16T)},
    31  		}))
    32  		muT := types.NamedT("sdl.Mutex", c.MethStructT(map[string]*types.FuncType{
    33  			"Lock":    c.FuncTT(nil),
    34  			"Unlock":  c.FuncTT(nil),
    35  			"Destroy": c.FuncTT(nil),
    36  		}))
    37  		ksymT := types.NamedT("sdl.Keysym", types.StructT([]*types.Field{
    38  			{Name: types.NewIdentGo("scancode", "Scancode", scanT)},
    39  		}))
    40  		evkeybT := types.NamedT("sdl.KeyboardEvent", types.StructT([]*types.Field{
    41  			{Name: types.NewIdentGo("keysym", "Keysym", ksymT)},
    42  			{Name: types.NewIdentGo("state", "Env", uint8T)},
    43  		}))
    44  		evwinT := types.NamedT("sdl.WindowEvent", types.StructT([]*types.Field{
    45  			{Name: types.NewIdentGo("event", "Event", uint8T)},
    46  		}))
    47  		evmbT := types.NamedT("sdl.MouseButtonEvent", types.StructT([]*types.Field{
    48  			{Name: types.NewIdentGo("button", "Button", uint8T)},
    49  			{Name: types.NewIdentGo("state", "Env", uint8T)},
    50  			{Name: types.NewIdentGo("x", "X", sintT)},
    51  			{Name: types.NewIdentGo("y", "Y", sintT)},
    52  		}))
    53  		evmmT := types.NamedT("sdl.MouseMotionEvent", types.StructT([]*types.Field{
    54  			{Name: types.NewIdentGo("x", "X", sintT)},
    55  			{Name: types.NewIdentGo("y", "Y", sintT)},
    56  			{Name: types.NewIdentGo("xrel", "Xrel", sintT)},
    57  			{Name: types.NewIdentGo("yrel", "Yrel", sintT)},
    58  		}))
    59  		evmwT := types.NamedT("sdl.MouseWheelEvent", types.StructT([]*types.Field{
    60  			{Name: types.NewIdentGo("x", "X", sintT)},
    61  			{Name: types.NewIdentGo("y", "Y", sintT)},
    62  		}))
    63  		textT := c.C().BytesN(32)
    64  		evtxtT := types.NamedT("sdl.TextInputEvent", types.StructT([]*types.Field{
    65  			{Name: types.NewIdentGo("text", "Text", textT)},
    66  		}))
    67  		evtxteT := types.NamedT("sdl.TextEditingEvent", types.StructT([]*types.Field{
    68  			{Name: types.NewIdentGo("text", "Text", textT)},
    69  		}))
    70  		surfaceT := types.NamedTGo("SDL_Surface", "sdl.Surface", types.StructT([]*types.Field{
    71  			{Name: types.NewIdentGo("w", "W", sintT)},
    72  			{Name: types.NewIdentGo("h", "H", sintT)},
    73  			{Name: types.NewIdentGo("pitch", "Pitch", sintT)},
    74  			{Name: types.NewIdentGo("pixels", "Pixels", bufT)},
    75  			{Name: types.NewIdentGo("clip_rect", "ClipRect", rectT)},
    76  			{Name: types.NewIdent("Lock", c.FuncTT(sintT))},
    77  			{Name: types.NewIdent("Unlock", c.FuncTT(nil))},
    78  			{Name: types.NewIdent("Free", c.FuncTT(nil))},
    79  			{Name: types.NewIdent("SetColorKey", c.FuncTT(sintT, sintT, uintT))},
    80  			{Name: types.NewIdent("GetClipRect", c.FuncTT(nil, c.PtrT(rectT)))},
    81  			{Name: types.NewIdent("SetBlendMode", c.FuncTT(sintT, blendT))},
    82  		}))
    83  		textureT := types.NamedTGo("SDL_Texture", "sdl.Texture", c.MethStructT(map[string]*types.FuncType{
    84  			"Destroy": c.FuncTT(nil),
    85  		}))
    86  		evT := types.NamedT("sdl.Event", types.StructT([]*types.Field{
    87  			{Name: types.NewIdentGo("type", "Type", sintT)},
    88  			{Name: types.NewIdentGo("edit", "Edit", evtxteT)},
    89  			{Name: types.NewIdentGo("text", "Text", evtxtT)},
    90  			{Name: types.NewIdentGo("key", "Key", evkeybT)},
    91  			{Name: types.NewIdentGo("button", "Button", evmbT)},
    92  			{Name: types.NewIdentGo("motion", "Motion", evmmT)},
    93  			{Name: types.NewIdentGo("wheel", "Wheel", evmwT)},
    94  			{Name: types.NewIdentGo("window", "Window", evwinT)},
    95  		}))
    96  		glCtxT := types.NamedTGo("SDL_GLContext", "sdl.GLContext", types.StructT(nil))
    97  		winT := types.NamedT("sdl.Window", c.MethStructT(map[string]*types.FuncType{
    98  			"SetFullscreen":     c.FuncTT(sintT, uintT),
    99  			"GetSize":           c.FuncTT(nil, c.PtrT(sintT), c.PtrT(sintT)),
   100  			"GetPosition":       c.FuncTT(nil, c.PtrT(sintT), c.PtrT(sintT)),
   101  			"SetSize":           c.FuncTT(nil, sintT, sintT),
   102  			"SetPosition":       c.FuncTT(nil, sintT, sintT),
   103  			"Minimize":          c.FuncTT(nil),
   104  			"Restore":           c.FuncTT(nil),
   105  			"SetGrab":           c.FuncTT(nil, boolT),
   106  			"SetResizable":      c.FuncTT(nil, boolT),
   107  			"SetBordered":       c.FuncTT(nil, boolT),
   108  			"SetTitle":          c.FuncTT(nil, strT),
   109  			"GetDisplayIndex":   c.FuncTT(sintT, strT),
   110  			"GLGetDrawableSize": c.FuncTT(nil, c.PtrT(sintT), c.PtrT(sintT)),
   111  			"GLSwap":            c.FuncTT(nil),
   112  			"GLCreateContext":   c.FuncTT(glCtxT),
   113  		}))
   114  		renderT := types.NamedTGo("SDL_Renderer", "sdl.Renderer", c.MethStructT(map[string]*types.FuncType{
   115  			"Present": c.FuncTT(nil),
   116  			"Destroy": c.FuncTT(nil),
   117  		}))
   118  		timerFuncT := types.NamedTGo("SDL_TimerCallback", "sdl.TimerFunc", c.FuncTT(uintT, uintT, c.PtrT(nil)))
   119  		return &Library{
   120  			Imports: map[string]string{
   121  				"sdl": RuntimeOrg + "/sdl",
   122  			},
   123  			Types: map[string]types.Type{
   124  				"SDL_Window":           winT,
   125  				"SDL_Renderer":         renderT,
   126  				"SDL_Scancode":         scanT,
   127  				"SDL_BlendMode":        blendT,
   128  				"SDL_Keymod":           keymodT,
   129  				"SDL_FingerID":         fingerT,
   130  				"SDL_TimerID":          timerT,
   131  				"SDL_Rect":             rectT,
   132  				"SDL_mutex":            muT,
   133  				"SDL_Keysym":           ksymT,
   134  				"SDL_KeyboardEvent":    evkeybT,
   135  				"SDL_WindowEvent":      evwinT,
   136  				"SDL_MouseButtonEvent": evmbT,
   137  				"SDL_MouseMotionEvent": evmmT,
   138  				"SDL_MouseWheelEvent":  evmwT,
   139  				"SDL_TextInputEvent":   evtxtT,
   140  				"SDL_TextEditingEvent": evtxteT,
   141  				"SDL_Event":            evT,
   142  				"SDL_Surface":          surfaceT,
   143  				"SDL_Texture":          textureT,
   144  				"SDL_GLContext":        glCtxT,
   145  				"SDL_TimerCallback":    timerFuncT,
   146  			},
   147  			Idents: map[string]*types.Ident{
   148  				"SDL_itoa":                       types.NewIdent("sdl.Itoa", c.FuncTT(strT, sintT, strT, sintT)),
   149  				"SDL_uitoa":                      types.NewIdent("sdl.Uitoa", c.FuncTT(strT, uintT, strT, sintT)),
   150  				"SDL_GetError":                   types.NewIdent("sdl.GetError", c.FuncTT(strT)),
   151  				"SDL_GetTicks":                   types.NewIdent("sdl.GetTicks", c.FuncTT(uintT)),
   152  				"SDL_PollEvent":                  types.NewIdent("sdl.PollEvent", c.FuncTT(sintT, c.PtrT(evT))),
   153  				"SDL_Delay":                      types.NewIdent("sdl.Delay", c.FuncTT(nil, uintT)),
   154  				"SDL_Init":                       types.NewIdent("sdl.Init", c.FuncTT(sintT, uintT)),
   155  				"SDL_CreateMutex":                types.NewIdent("sdl.CreateMutex", c.FuncTT(c.PtrT(muT))),
   156  				"SDL_CreateWindow":               types.NewIdent("sdl.CreateWindow", c.FuncTT(c.PtrT(winT), strT, sintT, sintT, sintT, sintT, uintT)),
   157  				"SDL_CreateRGBSurfaceWithFormat": types.NewIdentGo("SDL_CreateRGBSurfaceWithFormat", "sdl.CreateRGBSurfaceWithFormat", c.FuncTT(c.PtrT(surfaceT), uintT, sintT, sintT, sintT, uintT)),
   158  				"SDL_BlitSurface":                types.NewIdentGo("SDL_BlitSurface", "sdl.BlitSurface", c.FuncTT(sintT, c.PtrT(surfaceT), c.PtrT(rectT), c.PtrT(surfaceT), c.PtrT(rectT))),
   159  				"SDL_BlitScaled":                 types.NewIdentGo("SDL_BlitScaled", "sdl.BlitScaled", c.FuncTT(sintT, c.PtrT(surfaceT), c.PtrT(rectT), c.PtrT(surfaceT), c.PtrT(rectT))),
   160  				"SDL_AddTimer":                   types.NewIdentGo("SDL_AddTimer", "sdl.AddTimer", c.FuncTT(timerT, uintT, timerFuncT, c.PtrT(nil))),
   161  				"SDL_RemoveTimer":                types.NewIdentGo("SDL_RemoveTimer", "sdl.RemoveTimer", c.FuncTT(boolT, timerT)),
   162  				"SDL_GL_DeleteContext":           types.NewIdentGo("SDL_GL_DeleteContext", "sdl.GLDeleteContext", c.FuncTT(nil, glCtxT)),
   163  				"SDL_GL_SetSwapInterval":         types.NewIdentGo("SDL_GL_SetSwapInterval", "sdl.GLSetSwapInterval", c.FuncTT(sintT, sintT)),
   164  				"SDL_GetDisplayBounds":           types.NewIdentGo("SDL_GetDisplayBounds", "sdl.GetDisplayBounds", c.FuncTT(sintT, sintT, c.PtrT(rectT))),
   165  				"SDL_GetGlobalMouseState":        types.NewIdentGo("SDL_GetGlobalMouseState", "sdl.GetGlobalMouseState", c.FuncTT(uintT, c.PtrT(sintT), c.PtrT(sintT))),
   166  				"SDL_GetEventState":              types.NewIdentGo("SDL_GetEventState", "sdl.GetEventState", c.FuncTT(uint8T, uintT)),
   167  				"SDL_SetRelativeMouseMode":       types.NewIdentGo("SDL_SetRelativeMouseMode", "sdl.SetRelativeMouseMode", c.FuncTT(sintT, boolT)),
   168  				"SDL_GetModState":                types.NewIdentGo("SDL_GetModState", "sdl.GetModState", c.FuncTT(keymodT)),
   169  				"SDL_StartTextInput":             types.NewIdentGo("SDL_StartTextInput", "sdl.StartTextInput", c.FuncTT(nil)),
   170  				"SDL_StopTextInput":              types.NewIdentGo("SDL_StopTextInput", "sdl.StopTextInput", c.FuncTT(nil)),
   171  				"SDL_CreateRenderer":             types.NewIdentGo("SDL_CreateRenderer", "sdl.CreateRenderer", c.FuncTT(c.PtrT(renderT), c.PtrT(winT), sintT, uintT)),
   172  				"SDL_CreateTextureFromSurface":   types.NewIdentGo("SDL_CreateTextureFromSurface", "sdl.CreateTextureFromSurface", c.FuncTT(c.PtrT(textureT), c.PtrT(renderT), c.PtrT(surfaceT))),
   173  				"SDL_RenderCopy":                 types.NewIdentGo("SDL_RenderCopy", "sdl.RenderCopy", c.FuncTT(sintT, c.PtrT(renderT), c.PtrT(textureT), c.PtrT(rectT), c.PtrT(rectT))),
   174  
   175  				"SDL_BLENDMODE_NONE":            types.NewIdentGo("SDL_BLENDMODE_NONE", "sdl.BLENDMODE_NONE", blendT),
   176  				"SDL_PIXELFORMAT_RGBA5551":      types.NewIdentGo("SDL_PIXELFORMAT_RGBA5551", "sdl.PIXELFORMAT_RGBA5551", sintT),
   177  				"SDL_PIXELFORMAT_RGB565":        types.NewIdentGo("SDL_PIXELFORMAT_RGB565", "sdl.PIXELFORMAT_RGB565", sintT),
   178  				"SDL_PIXELFORMAT_RGB555":        types.NewIdentGo("SDL_PIXELFORMAT_RGB555", "sdl.PIXELFORMAT_RGB555", sintT),
   179  				"SDL_RENDERER_ACCELERATED":      types.NewIdentGo("SDL_RENDERER_ACCELERATED", "sdl.RENDERER_ACCELERATED", sintT),
   180  				"SDL_RENDERER_PRESENTVSYNC":     types.NewIdentGo("SDL_RENDERER_PRESENTVSYNC", "sdl.RENDERER_PRESENTVSYNC", sintT),
   181  				"SDL_NUM_SCANCODES":             types.NewIdentGo("SDL_NUM_SCANCODES", "sdl.NUM_SCANCODES", sintT),
   182  				"SDL_PRESSED":                   types.NewIdentGo("SDL_PRESSED", "sdl.PRESSED", sintT),
   183  				"SDL_SCANCODE_SPACE":            types.NewIdentGo("SDL_SCANCODE_SPACE", "sdl.SCANCODE_SPACE", sintT),
   184  				"KMOD_LSHIFT":                   types.NewIdentGo("KMOD_LSHIFT", "sdl.KMOD_LSHIFT", sintT),
   185  				"KMOD_RSHIFT":                   types.NewIdentGo("KMOD_RSHIFT", "sdl.KMOD_RSHIFT", sintT),
   186  				"KMOD_RALT":                     types.NewIdentGo("KMOD_RALT", "sdl.KMOD_RALT", sintT),
   187  				"SDL_INIT_VIDEO":                types.NewIdent("sdl.INIT_VIDEO", uintT),
   188  				"SDL_INIT_TIMER":                types.NewIdent("sdl.INIT_TIMER", uintT),
   189  				"SDL_WINDOWPOS_UNDEFINED":       types.NewIdent("sdl.WINDOWPOS_UNDEFINED", sintT),
   190  				"SDL_WINDOWPOS_CENTERED":        types.NewIdent("sdl.WINDOWPOS_CENTERED", sintT),
   191  				"SDL_WINDOW_RESIZABLE":          types.NewIdent("sdl.WINDOW_RESIZABLE", uintT),
   192  				"SDL_WINDOW_OPENGL":             types.NewIdent("sdl.WINDOW_OPENGL", uintT),
   193  				"SDL_WINDOW_FULLSCREEN_DESKTOP": types.NewIdent("sdl.WINDOW_FULLSCREEN_DESKTOP", uintT),
   194  			},
   195  		}
   196  	})
   197  }