Sign in to follow this  
thefollower

Returning multiple values from function C++

Recommended Posts

Hey i have a function which i use to preload images, but its a bit messy.


I'm trying to load images and return them so then my class can load the images. But I'm complete confused how it works.


This is my function:



[source lang="cpp"]void menuPreload(std::string theme,std::string theme_on,sf::Texture &button, sf::Texture &button_on,int &width,int &height){
std::string img;
std::string img_on;
img = theme;
img_on = theme_on;

if (!button.loadFromFile(img)){
exit(4);
}

if (!button_on.loadFromFile(img_on)){
exit(5);
}

sf::Sprite sprite(button);
width = sprite.getLocalBounds().width;
height = sprite.getLocalBounds().height;
}[/source]

I think call it like this in my main function:

[source lang="cpp"] sf::Texture button;
sf::Texture button_on;
int width;
int height;
menuPreload(config["Button"],config["Button_On"],button,button_on,width,height); [/source]


After i have preloaded the images i call my class to load the button like this:

[source long="cpp"]
//before loop
Button btn_quit(window.getSize().y/2, 200,width,height);

//inside my loop
btn_quit.RenderBttn(window,mouseX,mouseY,button,button_on);
[/source]


So far i think this is correct... but then in my class i have:


[source lang="cpp"]class Button
{
private:
int m_x, m_y;
int m_w, m_h;

public:
Button(int x, int y, int m_w, int m_h)
{
m_y = y;
m_x = x - m_w/2;
}

bool IsIn( int mouseX, int mouseY )
{
exit(m_h);

if (((mouseX > m_x) && (mouseX < m_x + m_w))
&& ((mouseY > m_y) && (mouseY < m_y + m_h ) ) ) {
return true;
} else {
return false;
}
}

void RenderBttn(sf::RenderWindow &destination,int mouseX, int mouseY,sf::Texture button, sf::Texture button_on)
{

sf::Sprite result(IsIn(mouseX,mouseY) ? button_on : button);
result.setPosition( m_x , m_y);
destination.draw(result);

}
};[/source]

The issue is when the mouse is over the button it doesn't change, m_w seems to be not set or incorrect. Am i on the right lines here... my brain is about to explode ... [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Share this post


Link to post
Share on other sites
Just a hunch...

[b]Perhaps you mean to do this:[/b]

void RenderBttn(sf::RenderWindow &destination,int mouseX, int mouseY,[color=#0000ff][b]sf::Texture &button, sf::Texture &button_on[/b][/color])
{

sf::Sprite result(IsIn(mouseX,mouseY) ? button_on : button);
result.setPosition( m_x , m_y);
destination.draw(result);
}

[b]Instead of this:[/b]

void RenderBttn(sf::RenderWindow &destination,int mouseX, int mouseY,[color=#ff0000][b]sf::Texture button, sf::Texture button_on[/b][/color])
{

sf::Sprite result(IsIn(mouseX,mouseY) ? button_on : button);
result.setPosition( m_x , m_y);
destination.draw(result);
}

Share this post


Link to post
Share on other sites
You have a call to exit in your code for some reason: "exit(m_h)". I would advise googling for 0xcccccccc, such patterns are usually indicative of the compiler or runtime setting memory to particular values, trying to flush out bugs for you.

Share this post


Link to post
Share on other sites
I had that to check the value of m_h to make sure it was correct. I tend to exit with the variable if they are ints to check they are correct :)

I removed that exit now.

I've just re-done my header files. And moved the functions to a different cpp file so i can follow it better. It got rid of the 0xcccccccc error but i got this :



[quote]
1> menu.cpp
1>desktop\c++\projects\sfml\sfml\menu.cpp(19): warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
1>desktop\c++\projects\sfml\sfml\menu.cpp(20): warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
1>desktop\c++\projects\sfml\sfml\menu.cpp(53): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>desktop\c++\projects\sfml\sfml\menu.cpp(53): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1> main.cpp
1>desktop\c++\sfml\sfml-2.0\include\sfml\window\window.hpp(476): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function 'sf::Window::Window(const sf::Window &amp;)'
1>desktop\c++\sfml\sfml-2.0\include\sfml\graphics\rendertarget.hpp(369): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget &amp;)'
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/quote] Edited by thefollower

Share this post


Link to post
Share on other sites
[quote]
I tend to exit with the variable if they are ints to check they are correct
[/quote]
A better way to do this is to use assertions. In C++, you can include <cassert> and write code like this:
[code]
assert(someVariable == 42);
[/code]
The nice thing about assertions is they are only compiled in debug builds. When you build for release, these checks will be removed. Also depending on your toolchain they can have good integration with your IDE. For instance, the assertions in Visual Studio allow you to break into the debugger, which allows you to examine the call stack and discover what led to the erroneous value.

It also means that if the value is correct your program will continue to run!

Share this post


Link to post
Share on other sites
If you're using a bit more modern C++, you could use std::tuple for return value and std::tie to unpack it, for example:

[source lang="cpp"]#include <tuple>

std::tuple<int, float> foo()
{
return std::make_tuple(1, 2.0f);
}

int main()
{
int a;
float b;

std::tie(a, b) = foo();
}
[/source]

Share this post


Link to post
Share on other sites
it should be noted that std::tie has some MAJOR performance penalties if you aren't careful. We had some serious issues with boost / std::tie and using that to return multiple values, since you hav ea LOT of possible value-copying going on (I took a co-worker to town for this; it KILLED our application's throughput because of the overhead for both tie creation and access).

In my limited experience, this is the result of people who aren't used to the idea of the entre value being copied (ie, they are just used to references or pointers being copied).
In this case, i believe the OP's problem was solved by using & to make sure references were passed in.

In general, although this depends greatly on your development environment, you may want to consider passing such values as pointers, and non-modifiable references as const XXX&. this will give the programmer (most probably yourself) a clearer understanding of what they can expect to happen in the function.

Share this post


Link to post
Share on other sites
[quote name='noizex' timestamp='1352371238' post='4998823']
If you're using a bit more modern C++, you could use std::tuple for return value and std::tie to unpack it, for example:
[/quote]

[b]Interesting....[/b]

Share this post


Link to post
Share on other sites
[source lang="cpp"]struct Bounds { //or whatever name describes what the thing actually is
int width;
int height;
};

using namespace std;
using namespace sf;

//Are you modifying these string objects? If not then there's no need to copy them - and they should be const.
int menuPreload(const string &theme, const string &theme_on, Texture &button, Texture &button_on, Bounds* dims) {
//Please don't just abort the program if there's an error.
//Return error codes describing the problem so you can try to solve it!
//(Or release held resources and show a message if you can't.)
if(!button.loadFromFile(theme)){return 4;}
if(!button_on.loadFromFile(theme_on)){return 5;}

Sprite sprite(button);
something thing = sprite.getLocalBounds();

dims->width = thing.width;
dims->height = thing.height;
return 0;
}

Texture button;
Texture button_on;
Bounds myBounds;

menuPreload(config["Button"], config["Button_On"], button, button_on, &myBounds);
[/source] Edited by Khatharr

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this