go-sdl2 icon indicating copy to clipboard operation
go-sdl2 copied to clipboard

DrawRectF, FillRectF are not working

Open dev-abir opened this issue 4 years ago • 12 comments

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?

dev-abir avatar May 04 '20 13:05 dev-abir

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.

veeableful avatar May 04 '20 15:05 veeableful

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)

dev-abir avatar May 04 '20 15:05 dev-abir

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.

veeableful avatar May 04 '20 15:05 veeableful

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.

dev-abir avatar May 04 '20 16:05 dev-abir

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.

veeableful avatar May 05 '20 12:05 veeableful

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 :-)

dev-abir avatar May 05 '20 14:05 dev-abir

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

veeableful avatar May 07 '20 12:05 veeableful

What is the other way?

dev-abir avatar May 07 '20 12:05 dev-abir

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!

veeableful avatar May 07 '20 12:05 veeableful

Static libraries are now updated.

gen2brain avatar May 12 '20 12:05 gen2brain

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

veeableful avatar May 12 '20 14:05 veeableful

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.

bardiel avatar Sep 20 '20 15:09 bardiel