Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct Rectangle is missing initialization parameters #6

Closed
rishavs opened this issue Jun 3, 2022 · 2 comments
Closed

struct Rectangle is missing initialization parameters #6

rishavs opened this issue Jun 3, 2022 · 2 comments

Comments

@rishavs
Copy link
Contributor

rishavs commented Jun 3, 2022

The Rectangle (and all other) struct doesn't have a initialization constructor.

Due to this, initializing Rectangle with argument throws exception.

using

LibRaylib::Rectangle.new(100, 100, 100, 100)

throws the error

In src\sceneMainMenu.cr:22:55

 22 | if LibRayGUI.button( LibRaylib::Rectangle.new(100, 100, 100, 100), "Press me!" )
                                                ^--
Error: wrong number of arguments for 'LibRaylib::Rectangle.new' (given 4, expected 0)

Overloads are:
 - LibRaylib::Rectangle.new()

The way to make this work is to update the properties of Rectangle after it is initialized.
This works;

            LibRaylib.draw_text("Scene Main Menu", 190, 200, 20, LibRaylib::BLACK)
            btnRec = LibRaylib::Rectangle.new
            btnRec.x = 100
            btnRec.y = 100
            btnRec.width = 100
            btnRec.height = 100
            if LibRayGUI.button( btnRec, "Press me!" ) 
                pp "Pressed";
            end

but this is cumbersome to write and deviates from the raylib api where users can create Rectangle with inline parameters like this;

            if LibRayGUI.button( LibRaylib::Rectangle.new(100, 100, 100, 100), "Press me!" )
                pp "Pressed";
            end

Recommendation would be to update the Rectangle struct.
instead of having the Rectangle struct like this;

  struct Rectangle
    x : LibC::Float
    y : LibC::Float
    width : LibC::Float
    height : LibC::Float
  end

we can go for more idiomatic properties with default values and overloaded constructors;

  struct Rectangle
    property x : LibC::Float = 0
    property y : LibC::Float = 0
    property width : LibC::Float = 120
    property height : LibC::Float = 30
    
    def initialize
    end

    def initialize(x, y, width, height)
      @x = x
      @y = y
      @width = width
      @height = height
    end
  end
@sol-vin
Copy link
Owner

sol-vin commented Jun 3, 2022

lib structs use named arguments, you need to use it like

Rl::Rectangle.new(
 x: 0,
 y: 0,
 width: 0,
 height: 0
)

@sol-vin sol-vin closed this as completed Jun 3, 2022
@rishavs
Copy link
Contributor Author

rishavs commented Jun 3, 2022

can confirm that works. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants