Page 1 of 1

[SOLVED]Help with function poiners.

Posted: Sat Aug 29, 2009 9:12 am
by Zer0XoL
If you ever played zelda majoras mask you know you can wear masks with different effects.
I tried making masks so when i use them a special function is executed, it compiles however does not print any text, so i assume the functions is not executing properly.
Also if someone knows a better way to do this, please say it! :P

here is the main.cpp:

Code: Select all

//main.cpp
#include <iostream>
#include "mask.h"

int main()
{
    Player Link;
    Link.UseMask();
    std::cin.get();
    std::cin.get();
    return 0;
}
and here is the mask.h:

Code: Select all

//mask.h
#include <iostream>
using namespace std;

void mNormal()
{
     cout << "Normal";
}
void mDecu()
{
     cout << "DECCU!!";
}
void mGoron()
{
     cout << "GROONEN!!?";
}
void mZora()
{
     cout << "zorraen!";
}
void mMajora()
{
     cout << "Majjorra";
}

enum masks{NORMAL=0,DECU=1,GORON=2,ZORA=3,BAJS=4,MAJORA=5};

struct mask
{
       string name;
       void (*Use)();
};

class Player
{
      private:
              int mask_wearing;
              mask Maskz[20];
      public:
             void UseMask()
             {
                  Maskz[mask_wearing].Use;
             }
             Player()
             {
              mask_wearing = ZORA;
              Maskz[NORMAL].name = "NONE";
              Maskz[NORMAL].Use = &mNormal;
              Maskz[DECU].name = "Deccu mask";
              Maskz[DECU].Use = &mDecu;
              Maskz[GORON].name = "Goron mask";
              Maskz[GORON].Use = &mGoron;
              Maskz[ZORA].name = "Zora mask";
              Maskz[ZORA].Use = &mZora;
              Maskz[MAJORA].name = "Majoras mask";
              Maskz[MAJORA].Use = &mMajora;
             }
             
};
Please correct me about how i should handle .cpp and .h files, thanks. :)

Re: Help with function poiners.

Posted: Sat Aug 29, 2009 1:11 pm
by dani93
Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?

Re: Help with function poiners.

Posted: Sat Aug 29, 2009 1:51 pm
by Zer0XoL
dani93 wrote:Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?
Nope it says nothing, i use dev cpp.
Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)

Re: Help with function poiners.

Posted: Sat Aug 29, 2009 2:18 pm
by dani93
Zer0XoL wrote:Nope it says nothing, i use dev cpp.
I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Zer0XoL wrote:Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
I don't know if this is better, but that's the way I handle such things in my map editor:
Every tile has an index, depending on the position in the tilesheet. E.g. the first tile has the index 0. If the index is larger than the width of the tilesheet divided by the width of a single tile, it is not in the first row. And so you can calculate the position in the tilesheet. You could make a row for every mask and add a (constant) value for the status of the character (looking right, left, etc) to the index.

Example:
single tile: 32*32
tilesheet: 128*256 (4 rows, 8 columns)
first mask index: 8 (first tile in the third row)
moving down: first in the third row (nothing to be done)
moving right: second in the third row (add 1 to the index)
moving left: third in the third row (add 2 to the index)
...

But you must always know where which mask begins. This should be marked by constants.

Just my suggestion :)

Re: Help with function poiners.

Posted: Sat Aug 29, 2009 3:08 pm
by Falco Girgis
I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Dev-C++ uses GCC (MinGW).

Re: Help with function poiners.

Posted: Sat Aug 29, 2009 4:05 pm
by Zer0XoL
dani93 wrote:
Zer0XoL wrote:Nope it says nothing, i use dev cpp.
I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Zer0XoL wrote:Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
I don't know if this is better, but that's the way I handle such things in my map editor:
Every tile has an index, depending on the position in the tilesheet. E.g. the first tile has the index 0. If the index is larger than the width of the tilesheet divided by the width of a single tile, it is not in the first row. And so you can calculate the position in the tilesheet. You could make a row for every mask and add a (constant) value for the status of the character (looking right, left, etc) to the index.

Example:
single tile: 32*32
tilesheet: 128*256 (4 rows, 8 columns)
first mask index: 8 (first tile in the third row)
moving down: first in the third row (nothing to be done)
moving right: second in the third row (add 1 to the index)
moving left: third in the third row (add 2 to the index)
...

But you must always know where which mask begins. This should be marked by constants.

Just my suggestion :)
Ooh, im sorry but you may not know what i ment by "masks", i mean wearable masks than gives you powers in a game called zelda majoras mask, not tile masks or something like that but thanks anyway. xD
Actually you helped me know alot more about how i can make maps. :) :worship:

Re: Help with function poiners.

Posted: Sat Aug 29, 2009 5:02 pm
by dani93
I used g++ with the -Wall flag to compile it. Didn't they stop to develop DevCpp? I'd use something else like gcc/g++, CodeBlocks (with gcc/g++) or VC++ (although I don't like it).
Dev-C++ uses GCC (MinGW).
I thought it has its own compiler. If it uses MinGW you should be able to activate the -Wall flag somewhere in the compiler options.
Zer0XoL wrote:Ooh, im sorry but you may not know what i ment by "masks", i mean wearable masks than gives you powers in a game called zelda majoras mask, not tile masks or something like that but thanks anyway. xD
Soory, misunderstood you :shock2:
Can't think of a better way to manage this at the moment.

Re: Help with function poiners.

Posted: Sat Aug 29, 2009 6:23 pm
by Joeyotrevor
Zer0XoL wrote:
dani93 wrote:Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?
Nope it says nothing, i use dev cpp.
Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
A "more c++" way would be having a Mask interface and use inheritance and polymorphism, like this:

Code: Select all

//Mask.h

class Mask
{
protected:
  string name;
public:
  virtual void Use() = 0;
}

Code: Select all

//Decu.h

class DecuMask : public Mask
{
public:
	Decu()
	{
		name = "Decu mask";
	}

	void Use()
	{
		cout << DECCU!!";
	}
};
Etc...

Then you would make your Mask array an array of mask pointers and do this:

Code: Select all

masks[DECU] = new Decu();
masks[GORON] = new Goron();
to initialize it, instead of

Code: Select all

Maskz[DECU].name = "Deccu mask";
Maskz[DECU].Use = &mDecu;
Maskz[GORON].name = "Goron mask";
Maskz[GORON].Use = &mGoron;

Re: [SOLVED]Help with function poiners.

Posted: Sat Aug 29, 2009 9:19 pm
by Falco Girgis
I completely agree with Joeyotrevor. His C++-ified implementation is basically the same thing as a "functor."

Check them out here:
http://www.newty.de/fpt/functor.html

Function pointers are nifty and all, but they are rather limited, ugly, and fairly complex to do things with. Functors are just much cleaner ways to handle them, and that's how the old Elysian Shadows engine handled different AI actions.

Re: [SOLVED]Help with function poiners.

Posted: Sun Aug 30, 2009 2:46 pm
by Zer0XoL
Joeyotrevor wrote:
Zer0XoL wrote:
dani93 wrote:Thats because you don't call the function.

Code: Select all

Maskz[mask_wearing].Use ();
You forgot the brackets after "Use" and you should use "endl" after cout to make sure everything is printed out.
Isn't your compiler showing a warning "This statement has no effect"?
Nope it says nothing, i use dev cpp.
Thanks for pointing it out for me, also suggestions about how i could make this in a better way are wellcome. :)
A "more c++" way would be having a Mask interface and use inheritance and polymorphism, like this:

Code: Select all

//Mask.h

class Mask
{
protected:
  string name;
public:
  virtual void Use() = 0;
}

Code: Select all

//Decu.h

class DecuMask : public Mask
{
public:
	Decu()
	{
		name = "Decu mask";
	}

	void Use()
	{
		cout << DECCU!!";
	}
};
Etc...

Then you would make your Mask array an array of mask pointers and do this:

Code: Select all

masks[DECU] = new Decu();
masks[GORON] = new Goron();
to initialize it, instead of

Code: Select all

Maskz[DECU].name = "Deccu mask";
Maskz[DECU].Use = &mDecu;
Maskz[GORON].name = "Goron mask";
Maskz[GORON].Use = &mGoron;
Oh, thanks a lot :shock2:
Im not done learning c++ and now you got me exited xD
GyroVorbis wrote:I completely agree with Joeyotrevor. His C++-ified implementation is basically the same thing as a "functor."

Check them out here:
http://www.newty.de/fpt/functor.html

Function pointers are nifty and all, but they are rather limited, ugly, and fairly complex to do things with. Functors are just much cleaner ways to handle them, and that's how the old Elysian Shadows engine handled different AI actions.
Thank you, i didnt know about functors :D
Its kinda cool to get help from you xD

Re: [SOLVED]Help with function poiners.

Posted: Tue Sep 01, 2009 12:14 pm
by dandymcgee
Zer0XoL wrote:Im not done learning c++ and now you got me exited xD
Look what you've done, you made the poor guy leave.