main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2024 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. _ "embed"
  17. "fmt"
  18. "io"
  19. "log"
  20. "net/http"
  21. "os"
  22. "github.com/hajimehoshi/ebiten/v2"
  23. "github.com/hajimehoshi/ebiten/v2/audio"
  24. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  25. )
  26. // mpgURL is a URL of an example MPEG-1 video. The license is the following:
  27. //
  28. // https://commons.wikimedia.org/wiki/File:Shibuya_Crossing,_Tokyo,_Japan_(video).webm
  29. // "Shibuya Crossing, Tokyo, Japan (video).webm" by Basile Morin
  30. // The Creative Commons Attribution-Share Alike 4.0 International license
  31. const mpgURL = "https://example-resources.ebitengine.org/shibuya.mpg"
  32. type Game struct {
  33. player *mpegPlayer
  34. err error
  35. }
  36. func (g *Game) Update() error {
  37. if g.err != nil {
  38. return g.err
  39. }
  40. return nil
  41. }
  42. func (g *Game) Draw(screen *ebiten.Image) {
  43. if g.err != nil {
  44. return
  45. }
  46. if err := g.player.Draw(screen); err != nil {
  47. g.err = err
  48. }
  49. ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.ActualFPS()))
  50. }
  51. func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
  52. return outsideWidth, outsideHeight
  53. }
  54. func main() {
  55. // Initialize audio context.
  56. _ = audio.NewContext(48000)
  57. // If you want to play your own video, the video must be an MPEG-1 video with 48000 audio sample rate.
  58. // You can convert the video to MPEG-1 with the below command:
  59. //
  60. // ffmpeg -i YOUR_VIDEO -c:v mpeg1video -q:v 8 -c:a mp2 -format mpeg -ar 48000 output.mpg
  61. //
  62. // You can adjust quality by changing -q:v value. A lower value indicates better quality.
  63. var in io.ReadCloser
  64. if len(os.Args) > 1 {
  65. f, err := os.Open(os.Args[1])
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. in = f
  70. } else {
  71. res, err := http.Get(mpgURL)
  72. if err != nil {
  73. log.Fatal(err)
  74. }
  75. in = res.Body
  76. fmt.Println("Play the default video. You can specify a video file as an argument.")
  77. }
  78. player, err := newMPEGPlayer(in)
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. g := &Game{
  83. player: player,
  84. }
  85. player.Play()
  86. ebiten.SetWindowTitle("Video (Ebitengine Demo)")
  87. ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
  88. if err := ebiten.RunGame(g); err != nil {
  89. log.Fatal(err)
  90. }
  91. }