Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Returning multiple values from function C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 thefollower   Members   -  Reputation: 197

Like
0Likes
Like

Posted 07 November 2012 - 04:31 PM

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 loopButton btn_quit(window.getSize().y/2, 200,width,height);//inside my loopbtn_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 ... Posted Image

Sponsor:

#2 Animate2D   Members   -  Reputation: 182

Like
2Likes
Like

Posted 07 November 2012 - 05:06 PM

Just a hunch...

Perhaps you mean to do this:

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);
}

Instead of this:

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);
}

#3 thefollower   Members   -  Reputation: 197

Like
0Likes
Like

Posted 07 November 2012 - 05:09 PM

Hmm i seem to get this happening :

SFML.exe: Native' has exited with code -858993460 (0xcccccccc).exe

I have no idea what that means to dissect the problem

Edited by thefollower, 07 November 2012 - 05:14 PM.


#4 rip-off   Moderators   -  Reputation: 8668

Like
2Likes
Like

Posted 07 November 2012 - 05:24 PM

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.

#5 thefollower   Members   -  Reputation: 197

Like
0Likes
Like

Posted 07 November 2012 - 05:36 PM

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 :



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 ==========


Edited by thefollower, 07 November 2012 - 05:41 PM.


#6 kidman171   Members   -  Reputation: 498

Like
2Likes
Like

Posted 07 November 2012 - 08:33 PM

You are probably passing the render window to a function as a copy instead of reference. Render Window inherits NonCopyable so you can't do that.

#7 thefollower   Members   -  Reputation: 197

Like
0Likes
Like

Posted 07 November 2012 - 08:43 PM

Yeh you were correct i forgot the &

Thanks :)

#8 rip-off   Moderators   -  Reputation: 8668

Like
0Likes
Like

Posted 08 November 2012 - 03:49 AM

I tend to exit with the variable if they are ints to check they are correct

A better way to do this is to use assertions. In C++, you can include <cassert> and write code like this:
assert(someVariable == 42);
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!

#9 noizex   Members   -  Reputation: 881

Like
0Likes
Like

Posted 08 November 2012 - 04:40 AM

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]

#10 IkarusDowned   Members   -  Reputation: 291

Like
0Likes
Like

Posted 08 November 2012 - 10:13 AM

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.

#11 Animate2D   Members   -  Reputation: 182

Like
0Likes
Like

Posted 08 November 2012 - 08:44 PM

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:


Interesting....

#12 Khatharr   Crossbones+   -  Reputation: 3036

Like
0Likes
Like

Posted 09 November 2012 - 04:12 AM

[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, 10 November 2012 - 01:06 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS