video.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Video extension
  2. package al
  3. /*
  4. #cgo pkg-config: allegro_video-5
  5. #cgo CFLAGS: -I/usr/local/include
  6. #cgo linux LDFLAGS: -lc_nonshared
  7. #include <stdlib.h>
  8. #include <allegro5/allegro.h>
  9. #include <allegro5/allegro_video.h>
  10. #include "helpers.h"
  11. */
  12. import "C"
  13. import "runtime"
  14. // import "unsafe"
  15. type VideoEventType C.enum_ALLEGRO_VIDEO_EVENT_TYPE
  16. const (
  17. EVENT_VIDEO_FRAME_SHOW = VideoEventType(C.ALLEGRO_EVENT_VIDEO_FRAME_SHOW)
  18. EVENT_VIDEO_FRAME_FINISHED = VideoEventType(C.ALLEGRO_EVENT_VIDEO_FINISHED)
  19. eVENT_VIDEO_FRAME_FINISHED = VideoEventType(C._ALLEGRO_EVENT_VIDEO_SEEK)
  20. )
  21. type VideoPositionType C.ALLEGRO_VIDEO_POSITION_TYPE
  22. const (
  23. VIDEO_POSITION_ACTUAL = VideoPositionType(C.ALLEGRO_VIDEO_POSITION_ACTUAL)
  24. VIDEO_POSITION_VIDEO_DECODE = VideoPositionType(C.ALLEGRO_VIDEO_POSITION_VIDEO_DECODE)
  25. VIDEO_POSITION_AUDIO_DECODE = VideoPositionType(C.ALLEGRO_VIDEO_POSITION_AUDIO_DECODE)
  26. )
  27. type Video struct {
  28. handle * C.ALLEGRO_VIDEO
  29. }
  30. // Converts a video to it's underlying C pointer
  31. func (self * Video) toC() *C.ALLEGRO_VIDEO {
  32. return (*C.ALLEGRO_VIDEO)(self.handle)
  33. }
  34. // Destroys the video.
  35. func (self *Video) Destroy() {
  36. if self.handle != nil {
  37. C.al_close_video(self.toC())
  38. }
  39. self.handle = nil
  40. }
  41. // Wraps a C video into a go video
  42. func wrapVideoRaw(data *C.ALLEGRO_VIDEO) *Video {
  43. if data == nil {
  44. return nil
  45. }
  46. return &Video{data}
  47. }
  48. // Sets up a finalizer for this Video that calls Destroy()
  49. func (self *Video) SetDestroyFinalizer() *Video {
  50. if self != nil {
  51. runtime.SetFinalizer(self, func(me *Video) { me.Destroy() })
  52. }
  53. return self
  54. }
  55. // Wraps a C video into a go video and sets up a finalizer that calls Destroy()
  56. func wrapVideo(data *C.ALLEGRO_VIDEO) *Video {
  57. self := wrapVideoRaw(data)
  58. return self.SetDestroyFinalizer()
  59. }
  60. func OpenVideo(filename string) * Video {
  61. cfilename := cstr(filename) ; defer cstrFree(cfilename)
  62. return wrapVideo(C.al_open_video(cfilename))
  63. }
  64. func (video * Video) Close() {
  65. video.Destroy()
  66. }
  67. func (video * Video) Start(mixer * Mixer) {
  68. C.al_start_video(video.toC(), mixer.toC())
  69. }
  70. func (video * Video) StartWithVoice(voice * Voice) {
  71. C.al_start_video_with_voice(video.toC(), voice.toC())
  72. }
  73. func (video * Video) EventSource() * EventSource {
  74. return wrapEventSourceRaw(C.al_get_video_event_source(video.toC()))
  75. }
  76. func (video * Video) SetPlaying(playing bool) {
  77. C.al_set_video_playing(video.toC(), C.bool(playing))
  78. }
  79. func (video * Video) Playing() bool {
  80. return bool(C.al_is_video_playing(video.toC()))
  81. }
  82. func (video * Video) AudioRate() float64 {
  83. return float64(C.al_get_video_audio_rate(video.toC()))
  84. }
  85. func (video * Video) FPS() float64 {
  86. return float64(C.al_get_video_fps(video.toC()))
  87. }
  88. func (video * Video) ScaledWidth() float32 {
  89. return float32(C.al_get_video_scaled_width(video.toC()))
  90. }
  91. func (video * Video) ScaledHeight() float32 {
  92. return float32(C.al_get_video_scaled_height(video.toC()))
  93. }
  94. func (video * Video) Frame() * Bitmap {
  95. return wrapBitmapRaw(C.al_get_video_frame(video.toC()))
  96. }
  97. func (vpt VideoPositionType) toC() C.ALLEGRO_VIDEO_POSITION_TYPE {
  98. return C.ALLEGRO_VIDEO_POSITION_TYPE(vpt)
  99. }
  100. func (video * Video) Position(vpt VideoPositionType) float64 {
  101. return float64(C.al_get_video_position(video.toC(), vpt.toC()))
  102. }
  103. func (video * Video) ActualPosition(vpt VideoPositionType) float64 {
  104. return video.Position(VIDEO_POSITION_ACTUAL)
  105. }
  106. func (video * Video) AudioDecodePosition(vpt VideoPositionType) float64 {
  107. return video.Position(VIDEO_POSITION_AUDIO_DECODE)
  108. }
  109. func (video * Video) VideoDecodePosition(vpt VideoPositionType) float64 {
  110. return video.Position(VIDEO_POSITION_VIDEO_DECODE)
  111. }
  112. func (video * Video) Seek(position_in_seconds float64) bool {
  113. return bool(C.al_seek_video(video.toC(), C.double(position_in_seconds)))
  114. }
  115. func InitVideoAddon() bool {
  116. return bool(C.al_init_video_addon())
  117. }
  118. func ShutdownVideoAddon() {
  119. C.al_shutdown_video_addon()
  120. }
  121. func VideoVersion() uint32 {
  122. return uint32(C.al_get_allegro_video_version())
  123. }