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

Author Topic: Passing an SFML handle to a function  (Read 8885 times)

0 Members and 1 Guest are viewing this topic.

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Passing an SFML handle to a function
« on: May 07, 2013, 10:25:00 pm »
SFML forum,
  SFML 1.6  Visual C++ 2010

 I have some working SFML programs and need to find out how
 to pass a handle to a function.
int main()
{
        sf::RenderWindow Screen(sf::VideoMode(640, 480, 16), "SFML Window");
-
???? function(Screen);
-
        return EXIT_SUCCESS;
} // end of main


???? function(sf::???)
{
        -
        want to do some "Screen" commands in this function
        -
        -
return ???
} // end function(sf:???)
 

 How do I do this?

jerryd
« Last Edit: May 07, 2013, 10:28:50 pm by Laurent »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Passing an SFML handle to a function
« Reply #1 on: May 07, 2013, 10:33:32 pm »
First you really need to update to SFML 2.0. 1.6 is severely outdated.

Second your problem is a C++ coding basic. C++ does not have "handles". C++ has references and pointers. You really need to learn C++ before trying to use SFML.

int main()
{
    sf::RenderWindow Screen(sf::VideoMode(640, 480, 16), "SFML Window");
-
???? function(Screen);
-
    return EXIT_SUCCESS;
} // end of main

void function(sf::RenderWindow &window)
{
    -
    want to do some "Screen" commands in this function
    -
    -
return ???
} // end function(sf:???)

You really need to get a good book on C++ and learn from that, learning C++ on the internet will not teach you the proper ways to code C++. On top of that, learn C++ first, then try to use SFML.
« Last Edit: May 07, 2013, 10:48:41 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Passing an SFML handle to a function
« Reply #2 on: May 08, 2013, 01:28:35 am »
SFML forum,
 I know how to make and pass regular pointers, I just don't know how
 to make a pointer to "Screen"

 Any hint would get me started.

jerryd

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Passing an SFML handle to a function
« Reply #3 on: May 08, 2013, 02:14:09 am »
SFML forum,
 I know how to make and pass regular pointers, I just don't know how
 to make a pointer to "Screen"

 Any hint would get me started.

jerryd
When you pass a foobar by *foobar or a &foobar you already got a pointer/reference to the foobar. So please explain in more detail exactly what your problem is or what exactly are you trying to do.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Passing an SFML handle to a function
« Reply #4 on: May 08, 2013, 02:22:24 am »
zsbzsb,

 Since "Screen" isn't global I want to pass "Screen" to a function
 and then access members of "Screen" using ->.

Jerryd

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Passing an SFML handle to a function
« Reply #5 on: May 08, 2013, 02:51:06 am »
zsbzsb,

 Since "Screen" isn't global I want to pass "Screen" to a function
 and then access members of "Screen" using ->.

Jerryd

Quote
I know how to make and pass regular pointers

Obviously you don't understand pointers. A pointer is a pointer regardless what it is pointing to. All a pointer is is a memory address that points anything. Just pass it like I posted in the code above.....
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Passing an SFML handle to a function
« Reply #6 on: May 08, 2013, 03:28:15 am »
zsbzsb,

 If I have a multidimentional array defined such as:
 int frame[20][20];

 I make pointer to it using:
 int *pointer = &frame[0][0];

 This works but the compiler won't accept:
 int *pointer = &Screen;

jerryd

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Passing an SFML handle to a function
« Reply #7 on: May 08, 2013, 04:12:25 am »
You really need to learn C++ before trying to use SFML.

Not trying to sound mean, and I doubt zsbzsb is either, but this is some advice you should really follow. Especially if you're trying to do stuff like this:

the compiler won't accept:
 int *pointer = &Screen;

Unless you didn't catch you mistake, that is.

You're trying to give to address of a sf::RenderWindow to something that is asking for the address of an int. See anything wrong with that? :P
DSFML - SFML for the D Programming Language.

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Passing an SFML handle to a function
« Reply #8 on: May 08, 2013, 04:29:39 am »
Jebbs,

 Thanks for the reply.  You've helped me before.

 Good question.
 Screen must be an instance of an sf class so I should be able
 to say, Screen *pointer;

 but the compiler won't accept that?

jerryd

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Passing an SFML handle to a function
« Reply #9 on: May 08, 2013, 04:39:59 am »
No offense, but like zsbzsb said this is some pretty basic pointer stuff.

Screen is the name of the variable, not the type that the pointer is pointing to. Unless you have a type called "Screen," then "Screen *pointer" isn't valid code. Is should be "sf::RenderWindow *pointer" because you are declaring a pointer that points to a sf:RenderWindow which you would then give the address of your Screen variable.

Again, please go a learn C++ before trying to tackle SFML. You'll thank yourself down the road, trust me.
DSFML - SFML for the D Programming Language.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Passing an SFML handle to a function
« Reply #10 on: May 08, 2013, 04:43:08 am »
Stop arguing and just help him.. Yes, you can cast any kind of pointer to any kind of pointer, it works, just like that. It is not illegal or anything to do it, it just will crash or cause undefined behavior when you call invalid methods and access invalid data members.

You can int* e = &screen; without a problem. However, under some compiler settings you will get an error for doing that, which you can overcome with a explicit cast int* e = (int*)&screen; In other compiler environments it won't even complain. At most, you can always use reinterpret_cast to convert the most incompatible pointer types.

Now to your actual problem, you want a function like this:

void myOtherFunction(sf::RenderWindow* screen)
{
    screen->doStuff();
}

main()
{
      sf::RenderWindow screen;
      myOtherFunction(&screen);
}
 

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Passing an SFML handle to a function
« Reply #11 on: May 08, 2013, 04:53:15 am »
Stop arguing and just help him..

Yep, you are right. Long day at work made me crankier than I usually am. :/

Sorry, jerryd. Didn't mean to be grouchy!
DSFML - SFML for the D Programming Language.

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Passing an SFML handle to a function
« Reply #12 on: May 08, 2013, 05:36:59 am »
Grimshaw,
 
 Thank you very much.  That all works, makes sense and I understand it.

 Jebbs,  I've had those kind of days.

I have one other type problem that can wait for another day.

Jerryd

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Passing an SFML handle to a function
« Reply #13 on: May 08, 2013, 12:58:38 pm »
Stop arguing and just help him.
In my defense, I posted the exact code you just posted in my very first post. Here is the code from my very first post.
Quote
int main()
{
    sf::RenderWindow Screen(sf::VideoMode(640, 480, 16), "SFML Window");
-
???? function(Screen);
-
    return EXIT_SUCCESS;
} // end of main

void function(sf::RenderWindow &window)
{
    -
    want to do some "Screen" commands in this function
    -
    -
return ???
} // end function(sf:???)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Passing an SFML handle to a function
« Reply #14 on: May 08, 2013, 03:02:33 pm »
yes sorry, I didn't mean to be grumpy or anything :) He clearly could use a good c++ book as you guys stated, but if he doesn't want to do it, well..  :)

Keep it going :D

 

anything