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

DrawRectF, FillRectF are not working #459

Open dev-abir opened 4 years ago

dev-abir commented 4 years ago

This is my code:

package main

import (
    "fmt"
    "os"

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

func main() {
    //==============CREATE WINDOW==============
    window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 500, 500, sdl.WINDOW_SHOWN)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
    }
    defer window.Destroy()

    //==============CREATE RENDERER==============
    renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
    }
    defer renderer.Destroy()

    //==============MAIN LOOP==============
    running := true
    for running {
        //==============EVENT HANDLING==============
        for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            if event.GetType() == sdl.QUIT {
                running = false
            }
        }

        //==============DRAWING==============
        renderer.SetDrawColor(255, 255, 100, 255)
        renderer.Clear() // clear the screen
        renderer.SetDrawColor(255, 100, 100, 255)

        /*
            These are working...

            err := renderer.DrawRect(&sdl.Rect{X: 100, Y: 100, W: 70, H: 50})
            if err != nil {
                fmt.Fprintf(os.Stderr, "Failed to render: %s\n", err)
            }
            err = renderer.FillRect(&sdl.Rect{X: 10, Y: 10, W: 50, H: 50})
            if err != nil {
                fmt.Fprintf(os.Stderr, "Failed to render: %s\n", err)
            }
        */

        err = renderer.DrawRectF(&sdl.FRect{X: 100.0, Y: 100.0, W: 70.0, H: 50.0})
        if err != nil {
            fmt.Fprintf(os.Stderr, "Failed to render: %s\n", err)
        }
        err = renderer.FillRectF(&sdl.FRect{X: 10.0, Y: 10.0, W: 50.0, H: 50.0})
        if err != nil {
            fmt.Fprintf(os.Stderr, "Failed to render: %s\n", err)
        }

        renderer.Present()
    }
}

The methods DrawRectF and FillRectF are not rendering anything on the screen. Whereas, if I use just DrawRect and FillRect in place of them then I can see the expected results.

The CopyExF method is also not working. Am I doing something wrong here or it's a bug?

veeableful commented 4 years ago

Hi @dev-abir, I can't seem to reproduce the issue on Linux, macOS, or Windows. Are you perhaps using an old version of SDL2? The functions are introduced on SDL 2.0.10.

dev-abir commented 4 years ago

Hi @dev-abir, I can't seem to reproduce the issue on Linux, macOS, or Windows. Are you perhaps using an old version of SDL2? The functions are introduced on SDL 2.0.10.

No.

According to my go.mod file, it is using go-sdl v0.4.1(which should use SDL 2.0.10)

veeableful commented 4 years ago

Ah are you using the static compilation method? Unfortunately, the SDL2 included is at version 2.0.9. However, if you're using a standard go build command, it will use the SDL2 library of the system.

dev-abir commented 4 years ago

I used GetVersion(...) method to find the SDL version it is using. I tried to build this way, and I found that it is using 2.0.9, it was previously using 2.0.8.

Still I am facing the same issue.

veeableful commented 4 years ago

Hi @dev-abir, I'm so sorry. We will try to update the static library to match the latest version. In the meantime, perhaps you could use the go build method and install a more up-to-date version of SDL2 in your system? What operating system are you running? I could guide you on how to install it if you'd like.

dev-abir commented 4 years ago

I use an ubuntu-based distro, while developing my application.

I may cross-compile the application for Windows, Mac and other linux distros.

I don't want to mess with the SDL2 installed in my system. I would like to link the libraries statically (maybe by replacing the old libraries inside .go-sdl2-libs), and thanks for helping me :-)

veeableful commented 4 years ago

I see.. in that case, it can't be helped. I will let you know here once the static libraries have been updated!

dev-abir commented 4 years ago

What is the other way?

veeableful commented 4 years ago

The other way I can think of is to download the source from https://libsdl.org/download-2.0.php, extract the library archive and run make inside it. After that, set PKG_CONFIG_PATH so it can find the sdl2.pc file which I believe should reside in the lib directory.

Another way is perhaps you can set PREFIX=$HOME/.local and run make; make install and also set PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH.

I haven't tried these methods myself but it might work!

gen2brain commented 4 years ago

Static libraries are now updated.

veeableful commented 4 years ago

Thank you so much @gen2brain! I have tagged it as the next patch release :smiley:

bardiel commented 3 years ago

Hi! I've had similar behavior when trying to draw using FPoints. Turns out I was using SDL2 2.0.9 provided by Debian 10. Using the static libraries provided by go-sdl2 (2.0.12 at this time), everything works just fine.