Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Draw shape function  (Read 1029 times)

0 Members and 1 Guest are viewing this topic.

ceeplusplus

  • Newbie
  • *
  • Posts: 1
    • View Profile
Draw shape function
« on: April 01, 2020, 11:03:25 pm »
I have this chunk of code that draws a square:

RectangleShape rect (Vector2f(100.f, 100.f));
rect.setFillColor(Color(100, 50, 200));
rect.setPosition(50.f, 50.f);
window.draw(rect);

I want to have some kind of function/class that does this.  The problem I'm having is that when I put this into a function, it doesn't work anymore, because the "window" object is declared inside of main{}.  Is there a way I can pass in the renderwindow from main{} to a function?

full code:
(click to show/hide)

p.s. i'm very new to cpp development, i might be asking the wrong question

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Draw shape function
« Reply #1 on: April 02, 2020, 07:24:04 pm »
Pass the window by reference (or pointer) to the function.

This is a C++ question more than an SFML one but I shall endeavour to give you a simple example, based on your code, passing by reference:

Function declaration:
void drawRectangle(const sf::RenderWindow& window)
{
    sf::RectangleShape rect (Vector2f(100.f, 100.f));
    rect.setFillColor(Color(100, 50, 200));
    rect.setPosition(50.f, 50.f);
    window.draw(rect);
}

Function call:
drawRectangle(window);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything