• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
thefollower

Returning multiple values from function C++

11 posts in this topic

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]
0

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

Share this post


Link to post
Share on other sites
Hmm i seem to get this happening :

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

I have no idea what that means to dissect the problem Edited by thefollower
0

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.
2

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
0

Share this post


Link to post
Share on other sites
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.
2

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!
0

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]
0

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.
0

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]
0

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
0

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  
Followers 0