-
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
-
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.
-
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
-
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.
-
zsbzsb,
Since "Screen" isn't global I want to pass "Screen" to a function
and then access members of "Screen" using ->.
Jerryd
-
zsbzsb,
Since "Screen" isn't global I want to pass "Screen" to a function
and then access members of "Screen" using ->.
Jerryd
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.....
-
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
-
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
-
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
-
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.
-
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);
}
-
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!
-
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
-
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.
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:???)
-
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
-
SFML forum,
Maybe I can wrap this up for now.
I am a long time programmer but don't have much C++ experience.
Based on zsbzsb's comments, I've spent the last couple of days
at the local book stores looking for the right book.
Incidently, I realize I didn't look close enough at his post. He did
have the answer.
My goal here is to learn OOP graphics programming because I'm
helping a 13 year old who whats to get into game programming.
He's already written games with Lego, DarkBasic and Game Maker
but it looks like the pros all use C++.
I intend to switch to SFML 2.0 as soon as I have some more
experience with 1.6.
This forum has helped a lot and I'm sure I'll be back with more
questions.
Any suggestions on the "right book"?
Jerryd
-
I always point people to this list (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), since it's collected by the community, that should know it and checking with my own virtual list, the good and recent books are all up there.
I'd still suggest to switch to SFML 2 sooner than later. You'll have to fight less bugs, get more features an overall cleaner API, a very good doc and top tutorial section. But I'll also totally understand if you want to finish things first.