veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.17k stars 219 forks source link

Error when running code #572

Open vtf6259 opened 11 months ago

vtf6259 commented 11 months ago

Go version: go1.20.6 Go-SDL2 version: 0.4.35 SDL2 version: N/A OS: Windows 11 Architecture: x64 When i run my go-sdl2 code i get the error:" github.com/veandco/go-sdl2/ttf: build constraints exclude all Go files in C:\Users\vtf62\go\pkg\mod\github.com\veandco\go-sdl2@v0.4.35\ttf " and my code is ```go package main

import ( "fmt" "os"

"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"

)

func run() error { err := sdl.Init(sdl.INIT_EVERYTHING) if err != nil { return fmt.Errorf("failed to initialize SDL: %v", err) } defer sdl.Quit()

if err := ttf.Init(); err != nil {
    return fmt.Errorf("failed to initialize TTF: %v", err)
}
defer ttf.Quit()

window, err := sdl.CreateWindow("Hello", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN)
if err != nil {
    return fmt.Errorf("failed to create window: %v", err)
}
defer window.Destroy()

renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
    return fmt.Errorf("failed to create renderer: %v", err)
}
defer renderer.Destroy()

font, err := ttf.OpenFont("arial.ttf", 32)
if err != nil {
    return fmt.Errorf("failed to load font: %v", err)
}
defer font.Close()

surface, err := font.RenderUTF8Solid("hi:)", sdl.Color{R: 255, G: 255, B: 255, A: 255})
if err != nil {
    return fmt.Errorf("failed to render text: %v", err)
}
defer surface.Free()

texture, err := renderer.CreateTextureFromSurface(surface)
if err != nil {
    return fmt.Errorf("failed to create texture: %v", err)
}
defer texture.Destroy()

renderer.Clear()
renderer.Copy(texture, nil, &sdl.Rect{X: 100, Y: 100, W: int32(surface.W), H: int32(surface.H)})
renderer.Present()

sdl.Delay(2000)

return nil

}

func main() { err := run() if err != nil { fmt.Fprintf(os.Stderr, "%v", err) os.Exit(1) } }

veeableful commented 11 months ago

Hi @vtf6259, could you show me how you build the code that generated the error?

vtf6259 commented 11 months ago

@veeableful it is go build main.go and i do go mod tidy to install go-sdl2

veeableful commented 11 months ago

Thanks @vtf6259! Would it compile if you remove usage of ttf from the code? Perhaps put only sdl.Delay(16) in the main() function.

Oh and how did you install SDL2 and SDL2_ttf? Are you using MinGW?

vtf6259 commented 11 months ago

@veeableful to install SDL2 and SDL2_ttf i use go mod tidy

veeableful commented 11 months ago

Oh that wouldn't install SDL2 and SDL2_ttf. They are separate, required packages that go-sdl2 depends on. I'm not sure if the following will work on Windows 11 but perhaps you can try it.

On Windows,

  1. Install mingw-w64 from Mingw-builds. A 7z archive extractor software might be needed which can be downloaded here. In this example, we extract the content, which is mingw64, into C:\.
  2. Download and install SDL2-devel-[version]-mingw.zip files from https://github.com/libsdl-org/SDL/releases.
    • Extract the SDL2 folder from the archive using a tool like 7zip
    • Inside the extracted SDL2 folder, copy the i686-w64-mingw32 and/or x86_64-w64-mingw32 into mingw64 folder e.g. C:\mingw64
  3. Setup Path environment variable
    • Put mingw-w64 binaries location into system Path environment variable (e.g. C:\mingw64\bin)
  4. Close and open terminal again so the new Path environment variable takes effect. Now we should be able to run go build inside the project directory.
  5. Download and install SDL2 runtime libraries from https://github.com/libsdl-org/SDL/releases. Extract and copy the .dll file into the project directory. After that, the program should become runnable.
  6. (Optional) You can repeat Step 2 for SDL_image, SDL_mixer, SDL_ttf