Go call the C language DLL SDL2

Go call the C language DLL SDL2

Go online to find a GUI project basically need to install additional software libraries, try to use a little go call dll library, to gain something, start a discussion, leave a record afraid of the future forget.

1. Directory structure

└─libtest│main.go│sdl.go└─libSDL2.dll

2. Package library function (sdl.go)

packagemainimport("syscall""unsafe")//SDL_INIT_Flag 初始化标志const(SDL_INIT_TIMER          =0x00000001SDL_INIT_AUDIO          =0x00000010SDL_INIT_VIDEO          =0x00000020/**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */SDL_INIT_JOYSTICK      =0x00000200/**< SDL_INIT_JOYSTICK implies SDL_INIT_EVENTS */SDL_INIT_HAPTIC        =0x00001000SDL_INIT_GAMECONTROLLER =0x00002000/**< SDL_INIT_GAMECONTROLLER implies SDL_INIT_JOYSTICK */SDL_INIT_EVENTS        =0x00004000SDL_INIT_NOPARACHUTE    =0x00100000/**< compatibility; this flag is ignored. */SDL_INIT_EVERYTHING    = SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS |SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER)// SDL_WindowFlags 窗体标志const(/* !!!FIXME:change this to name = (1<

  On macOS NSHighResolutionCapable must be set true in the

  application's Info.plist for this to have any effect. */SDL_WINDOW_MOUSE_CAPTURE =0x00004000/**< window has mouse captured (unrelated to INPUT_GRABBED) */SDL_WINDOW_ALWAYS_ON_TOP =0x00008000/**< window should always be above others */SDL_WINDOW_SKIP_TASKBAR  =0x00010000/**< window should not be added to the taskbar */SDL_WINDOW_UTILITY      =0x00020000/**< window should be treated as a utility window */SDL_WINDOW_TOOLTIP      =0x00040000/**< window should be treated as a tooltip */SDL_WINDOW_POPUP_MENU    =0x00080000/**< window should be treated as a popup menu */SDL_WINDOW_VULKAN        =0x10000000/**< window usable for Vulkan surface */)// SDL_Window SDL窗体typeSDL_Windowstruct{}// SDL_Event SDL 事件typeSDL_Eventstruct{}// SDL_Init SDL 初始化funcSDL_Init(flagsuint32)bool{ret := SysCallDll("SDL_Init",1,uintptr (flags)) ifint (ret) == 0 {returntrue} returnfalse} // SDL_CreateWindow create a window form, failed to return nilfuncSDL_CreateWindow (titlestring, posX, posY, width, height, sdlWindowFlagint32) * SDL_Window {vartp, _ = syscall. BytePtrFromString (title) // the string into * bytevartptr = unsafe.Pointer (tp) // get * byte pointer ret: = SysCallDll ( "SDL_CreateWindow", 6, uintptr (tptr), uintptr (posX), uintptr (posY) , uintptr (width), uintptr (height), uintptr (sdlWindowFlag)) ifint (ret) == 0 {returnnil} return (* SDL_Window) (unsafe.Pointer (ret))} // SDL_Delay return delay xxx ms funcSDL_Delay ( msuint32) {SysCallDll ( "SDL_Delay", 1, uintptr (ms))} // SDL_DestroyWindow destruction form funcSDL_DestroyWindow (window * SDL_Window) {SysCallDll ( "SDL_DestroyWindow", 1, uintptr (unsafe.Pointer (window)))} / / SDL_Quit (void) to exit the SDL system funcSDL_Quit () {SysCallDll ( "SDL_Quit",0)} // SDL_PollEvent funcSDL_PollEvent current event pending poll (event * SDL_Event) bool {ret: = SysCallDll ( "SDL_PollEvent", 1, uintptr (unsafe.Pointer (event))) ifint (ret) == 1 { returntrue} returnfalse}

3. Load dynamic library (main.go)

)} Ifdebug {fmt.Println (errs)} returnr} // CharPtr2String read from the char pointer stringfuncCharPtr2String (vcodeuintptr) string {varvbyte [] bytefori: = 0; i <20; i ++ {sbyte: = * ((* byte) (unsafe.Pointer (vcode))) ifsbyte == 0 {break} vbyte = append (vbyte, sbyte) vcode + = 1} returnstring (vbyte)} // testSdlWindow create a form, the form is not so then pause the program in response functestSdlWindow () {LoadDll () // load dynamic library ifSDL_Init (SDL_INIT_VIDEO) {// initialize the video subsystem // create a form win: = SDL_CreateWindow ( "SDL form title", 50,50,600,480, SDL_WINDOW_SHOWN) ifwin == nil { fmt.Println ( "SDL_CreateWindow failure")} SDL_Delay (6000) // pause program 6000msSDL_DestroyWindow (win) // destroy release form} SDL_Quit () // exit cleanup SDLdeferFreeDll () // release the dynamic library SDL2.dll} // testSdlWindow2 create the form, use the event to cycle response form functestSdlWindow2 () {LoadDll () // load dynamic library ifSDL_Init (SDL_INIT_VIDEO) {// initialize the video subsystem // create a form win: = SDL_CreateWindow ( "SDL form title ", 50,50,600,480, SDL_WINDOW_SHOWN) i fwin == nil {fmt.Println ( "

17556386-8e3708634d1cf011.jpg!web

I see Home Introduction free C ++ learning resources, video tutorials, career planning, interview Detailed, learning routes, development tools

Live 8 o'clock every night to explain the C ++ programming techniques.

Guess you like

Origin blog.csdn.net/weixin_34220623/article/details/90792476