callbacks.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. */
  7. import "C"
  8. import "unsafe"
  9. // Callback function for CreateCustomBitmap()
  10. type CreateCustomBitmapCallback func(bitmap *Bitmap, data interface{}) bool
  11. // Callback context for CreateCustomBitmap()
  12. type createCustomBitmapContext struct {
  13. fn CreateCustomBitmapCallback
  14. data interface{}
  15. }
  16. //export go_create_custom_bitmap_callback
  17. func go_create_custom_bitmap_callback(ptr unsafe.Pointer, context unsafe.Pointer) C.bool {
  18. ccbd := (*createCustomBitmapContext)(context)
  19. cbmp := (*C.ALLEGRO_BITMAP)(ptr)
  20. bmp := wrapBitmapRaw(cbmp)
  21. fptr := ccbd.fn
  22. return b2cb((fptr)(bmp, ccbd.data))
  23. }
  24. // Callback function for RunMain()
  25. type RunMainCallback func(args []string, data interface{}) int
  26. // Callback context for CreateCustomBitmap()
  27. type runMainContext struct {
  28. fn RunMainCallback
  29. data interface{}
  30. }
  31. var runMain runMainContext
  32. //export go_run_main_callback
  33. func go_run_main_callback(argc C.int, argv ** C.char) C.int {
  34. args := GoStrings(argc, argv)
  35. return C.int(runMain.fn(args, runMain.data))
  36. }
  37. type ThreadCallbackFunction func(*Thread, unsafe.Pointer) unsafe.Pointer
  38. type threadCallbackData struct {
  39. fn ThreadCallbackFunction
  40. data unsafe.Pointer
  41. }
  42. //export go_create_thread_callback
  43. func go_create_thread_callback(thread * C.ALLEGRO_THREAD, arg unsafe.Pointer) unsafe.Pointer {
  44. cbd := (*threadCallbackData)(arg)
  45. fn := cbd.fn
  46. res := fn(wrapThreadRaw(thread), cbd.data)
  47. return unsafe.Pointer(res)
  48. }