How to Return Multiple values

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

How to Return Multiple values

Post by Asimov » May 27th, 2012, 8:46 pm

Hi all,

Well I am still playing with my spirograph program, but I thought this question should be put in a different thread as to the specific problem. I have successfully made a class and it compiles fine. Phew first step done. Temporily I used a void. Now I know void can't return values, but I have a bigger problem than that. I need to return multiple values and I know a function can only return one value. I have googled a bit, but nothing has come up that I understand.

I need to return the r,g, b values so that is 3 values I need to return from the function. Or is there a better way to do this?

I will now paste my code so far. I will paste my header file first and then my cpp. I use I shouldn't leave the user variable in the game.h. They are only there at the moment because I still have my own code there. Also I had a small problem creating the class at first. Visual C++ decided to put my head file and cpp file in the root folder instead of the assets folder. Solved that problem by manually moving them afterwards.

Oh and one more question. I think pragma is something to do with memory. Could you tell me the reason it is used?

PS: Just found out I can make it work by making my int r; int g; int b; public in my Colour class and accessing it with int fb=Colour.b;, but I think this is considered bad programming. As it is dangerous to have too many public variables.

Colour.h

Code: Select all

#pragma once

#include <Windows.h>

class Colour
{
public:
	Colour();
	void Colour::RColour();
private:
	

	int colchange;
	int r;
	int g;
	int b;

};
Colour.cpp

Code: Select all

#include "Colour.h"

Colour::Colour()
	:colchange (0),
	r (255),
	g (255),
	b( 255 )
{
}

void Colour::RColour()
{
	if (colchange>100)
	{
		r=(rand() % 255);
		g=(rand() % 255);
		b=(rand() % 255);
		colchange=0;
	}

	colchange++;
}

Game.h

Code: Select all

#pragma once

#include "D3DGraphics.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "Sound.h"
#include "Colour.h"

class Game
{
public:
	Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer );
	void Go();
private:
	void ComposeFrame();
	/********************************/
	/*  User Functions              */

	void Draw( float theta,int tx,int ty,int r,int g,int b );

	/********************************/
private:
	D3DGraphics gfx;
	KeyboardClient kbd;
	MouseClient mouse;
	DSound audio;
	Colour Colour;
	
	/********************************/
	/*  User Variables              */

	float rotationAngle;
	int colchange;
	int r;
	int g;
	int b;
	/********************************/
};
Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

Sleet
Posts: 12
Joined: March 3rd, 2012, 6:51 pm

Re: How to Return Multiple values

Post by Sleet » May 28th, 2012, 4:58 am

A way to return several different values is to return a structure. I forget if chili has gone over structures but if you look up some examples with that it should help your problem pretty easily. (reason I say look it up is it has been a while since i've looked at it and I also have been slacking with coding so afraid joor on your own unless some one else knows about it though I am sure chili knows lol)

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How to Return Multiple values

Post by chili » May 28th, 2012, 12:38 pm

Hey Asimov, looks like you're making some progress there experimenting with classes.
Asimov wrote: I think pragma is something to do with memory. Could you tell me the reason it is used?
Actually, #pragma once just tells the compiler to not include this file more than once in any one translation unit (any one .cpp file).

I have a good idea for how to implement the color randomization in your program. Post your solution folder cleaned and zipped and I will show you what I have in mind.
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: How to Return Multiple values

Post by Asimov » May 28th, 2012, 5:13 pm

Hi Chilli,

I have posted my zip file for you to look at.

Was just looking throught the files and found the fart.wav. That made me laugh. Played it really loud and made me wife jump heh heh.

Your tutorials are the best.

Asimov

PS btw been watching the tic tac toe tutorial. When I made my Thomthello game which was based on othello I used a 2 dimensional array, but then I was dealing with 64 squares in that game. You can access any element of the array like that. Finding your tutorial interesting though.
Attachments
SpirocrapColourChange.zip
(174.46 KiB) Downloaded 177 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How to Return Multiple values

Post by chili » May 29th, 2012, 12:03 am

Asimov wrote:When I made my Thomthello game which was based on othello I used a 2 dimensional array, but then I was dealing with 64 squares in that game. You can access any element of the array like that.
Yup, part of the reason why I do the Tic Tac Toe game like this is to show how useful and convenient arrays are compared to the alternative. I like to show not only how to do things, but also why we do them, and if possible, the evolution of technique that has lead to the current method. You will be seeing arrays soon bro, don't worry. ;)

I'll take a look at your project tonight and show you my take on the Color class.
Chili

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How to Return Multiple values

Post by chili » May 29th, 2012, 3:35 pm

Here's how I think it should be done.

To be honest a new class is probably overkill in this instance, but still it's good practice I guess.
Attachments
SpirocrapColourChange.zip
(174.33 KiB) Downloaded 184 times
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: How to Return Multiple values

Post by Asimov » May 29th, 2012, 4:07 pm

Hi Chilli,

Just downloaded it. will have a look in a bit.

Yes I agree making a class for this is overkill, but I have a good reason for doing it. I want to do lots of things with colours, so I am aiming to have more than one colour function. So rather than clog up the main game class I thought I would make a class just to do all the colour functions I am planning.

Also even though I have used classes before this is the first time I separated the header file from the cpp file. So it is a learning experience as well.

Just attached an image of my files from my Thomthello game. You see there is no header and cpp file which are connected heh heh.

I have an idea to remake an old game, just for the practice, and everything in the tutorials so far can help me do it. I am thinking of remaking Missile command. The only thing about missile command I didn't like was the fact like space invaders there was no proper end. It just got harder and harder. I am thinking of putting a new slant on it. The only new thing I would have to learn is mouse movement, and also pixel by pixel collision detection.

Asimov

PS if you use camtasia 7 to record your tutorials it records computer audio and microphone audio as separate tracks, so you can have both.

PPS Just got your tutorial on structs, and before I got there I thought a better way of doing sprites would be to have a sprite class with all the movement and stuff in that class. Then you can instance the class with something like ball = new ball or something like that. I used something similar in XNA, but that was C#. Then in your structs tutorial you cover just what I was thinking heh heh, except you use structs, and I know structs are similar to classes heh heh. Anyway back to the tutorial.
Attachments
thomthello.jpg
(134.98 KiB) Downloaded 302 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: How to Return Multiple values

Post by Asimov » May 29th, 2012, 8:28 pm

Hi Chilli,

Now you have opened a can of worms. I am not understanding how it works. You have the return; in the header rather than the cpp. ie int GetBlue() const { return b;}

Also you have a function called randomize, but I can't see anywhere in the program where randomize is called.

It would be great if you could take me through the code.

After watching tutorial 15 I am wondering if I could have put r,g,b in a struct, inside my colour class.

ie

struct rgb
{
int r,
int g,
int b
};

rgb.r = rand() % 256;
rgb.g = rand() % 256;
rgb.b = rand() % 256;
return rgb;

I am wondering if this is a valid way of doing this? Of course the struct would be in the header file.

Asimov

PS I have been looking for the Lux program for taking images into PutPixel. I wonder if you could give me the link. I probbably just missed it.

Asimov

PPS Who made the graphic for the chilli logo. Was it built in 3ds Max or done in 2D in photoshop?


PPPS I can turn my shape off using the mousekey now LOL. Missile command here I come.

if (!mouse.RightIsPressed())
{
Draw( rotationAngle,400,250,r,g,b );
}
Last edited by Asimov on May 30th, 2012, 12:01 am, edited 2 times in total.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: How to Return Multiple values

Post by LuX » May 29th, 2012, 8:43 pm

http://www.planetchili.net/forum/viewto ... ?f=3&t=991 Here : -)

I'll bump it up too, it seems to have gone deep into the topics

The chili logo is a brand of noodles, you find the images when you google.
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: How to Return Multiple values

Post by chili » May 30th, 2012, 2:08 pm

Asimov wrote: I am not understanding how it works. You have the return; in the header rather than the cpp. ie int GetBlue() const { return b;}
Remeber you asked me this question before Asimov? ;) You can put the definition of member functions in the header file. It is common to do this for very short functions such as accessor functions like GetRed() etc.
Asimov wrote:Also you have a function called randomize, but I can't see anywhere in the program where randomize is called.
It is called in the Go() function in draw.cpp. For future reference, you can use Ctrl+F to search throughout a project for any text string. ;)
Asimov wrote:After watching tutorial 15 I am wondering if I could have put r,g,b in a struct, inside my colour class.
Bad idea. Put it in a class or put it in a struct. Don't put it in a struct inside of a class. :?
Chili

Post Reply