callbacks.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. #include "callbacks.h"
  7. */
  8. import "C"
  9. import "unsafe"
  10. // Callback function for CreateCustomBitmap()
  11. type CreateCustomBitmapCallback func(bitmap *Bitmap, data interface{}) bool
  12. // Callback context for CreateCustomBitmap()
  13. type createCustomBitmapContext struct {
  14. fn CreateCustomBitmapCallback
  15. data interface{}
  16. }
  17. //export go_create_custom_bitmap_callback
  18. func go_create_custom_bitmap_callback(ptr unsafe.Pointer, context unsafe.Pointer) C.bool {
  19. ccbd := (*createCustomBitmapContext)(context)
  20. cbmp := (*C.ALLEGRO_BITMAP)(ptr)
  21. bmp := wrapBitmapRaw(cbmp)
  22. fptr := ccbd.fn
  23. return b2cb((fptr)(bmp, ccbd.data))
  24. }
  25. // generic function pointer caller
  26. var CallbackInt func() int = nil
  27. //export go_generic_callback_int
  28. func go_generic_callback_int() int {
  29. if CallbackInt != nil {
  30. return CallbackInt()
  31. }
  32. return 0
  33. }
  34. type CallbackFunction func() int
  35. type callbackData struct {
  36. fn CallbackFunction
  37. data unsafe.Pointer
  38. }
  39. // the function below is the C callback that will be given to the
  40. // C api that needs a callback. It uses a callback context with a
  41. // Go function pointer in it to call that go function.
  42. //export go_take_callback_callback
  43. func go_take_callback_callback(context unsafe.Pointer) int {
  44. cbd := (*callbackData)(context)
  45. fn := cbd.fn
  46. return fn()
  47. }
  48. // Finally wrap the C callback caller function.
  49. func TakeCallback(fn CallbackFunction) int {
  50. ctx := unsafe.Pointer(&callbackData{fn, nil})
  51. return int(C.go_take_callback(ctx))
  52. }