Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Need explanation on "enum"

Author
Message
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 6th Jul 2010 20:10
I\'m trying to learn how to use this \"enum\" thing, from the Dark GDK tutorial: \"Dark Invaders\"

The game made in the tutorial is fairly complicated, and i can\'t realyl learn this well from it, since some things aren\'t even explained. Can someone show me this in a smaller scale? A very simple program demonstrating the use of the \"enum\" function?

I just love the smell of code in the morning...
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 6th Jul 2010 20:54 Edited at: 6th Jul 2010 20:59
Enum is not a function, it is a data type. Think of it as a collection of constants. It groups and automatically numbers named constants for you, instead of having to declare lots of variables one by one. Here is a short explanation (scroll down to the bottom of the page):

http://www.cplusplus.com/doc/tutorial/other_data_types/

and another page:

http://www.learncpp.com/cpp-tutorial/45-enumerated-types/
Fatal Berserker
14
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 7th Jul 2010 18:37
I like to see enums as types, as in different types the field can be.
For example i can do stuff like

People[Rodger].Nationality = British;
People[Fred].Nationality = American;

This means you dont need to declare alot of constants (variables which begin with const, such as const int British = 1, to declare the different options.

Personally i would set an enum up like:


The first one in the list is default.

Then you make the variable like this:
Nationalities VariableName = British;
Nationalities is the datatype.

That is just how i look at it, might not be right to do.

Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 8th Jul 2010 18:34
I kind of got the general idea from your post Mireben. But i can't seem to get it to appear as a whole in my head yet.. if you know what i mean. I'm really bad at learning on my own. Can you give me a way to learn this by practice? Can you give me a simple asignment for me to make, that uses enum? That would be very apprechiated!

I just love the smell of code in the morning...
jamesL
14
Years of Service
User Offline
Joined: 31st May 2010
Location:
Posted: 8th Jul 2010 23:04
make an enum called eColor
make that eColor have 3 possible values: red, blue, green

make a main function and declare an eColor variable in it, assign the eColor variable a value

make a function that takes an eColor parameter and draws a square or circle the color specifiecd in the eColor parameter

call that function from your main

once you get that working don't change anything except the assignment of the eColor in main;
don't change the definition of eColor, only change the assignment in your main, try to assign it the color yellow
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 9th Jul 2010 10:50 Edited at: 9th Jul 2010 10:51
it's very simple, so instead of "int" which can have the values ...-2, -1, 0, 1, 2..., the enum can have other values that YOU create


that's it, nothing too complicated, by the way the enum can also look like this:


Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 9th Jul 2010 13:26 Edited at: 9th Jul 2010 13:26
Ok.. the best i could come up with is this...



Did i get the point?
So now.. what i'm interested in, is using this in making games. In other examples.. i see people make their work really easy with enum. But they use it with 'switch' and 'break'..for switching game-modes.. or something.
What's that all about? (to see what i'm talking about, see the Dark Invaders tutorial that comes with Dark GDK).
Thanks in advance!

I just love the smell of code in the morning...
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 9th Jul 2010 20:15 Edited at: 9th Jul 2010 20:18
Well the answer is what you kind of already stated. It just makes the work for the programmer a little easier. I mean imagine if you didn't use enumerations in that switch case it would probably look something like this:

Now to switch to each game mode like gameReset() and gameTitle() you would have to do something like this in the code:



Now when it comes to game projects the amount of game modes that you are going to have is more than likely going to get pretty big I mean even in the dark invaders game it has quite a few. So it would get very confusing as to what game mode is associated with what number. So rather than making your life more confusing and anyone else who is going to see your code just use an enumeration of game modes. So using the dark invaders game as an example. You create an enumeration of all the game modes you are going to have.



For the switch case rather than putting the hard codes numbers you just put the name of the game mode that it is associated with like this:



So when you want to switch game modes you would just have to do this:


rather than having to remember what numbers go with what game mode. Just makes things much easier.

Hope this helps.
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 11th Jul 2010 21:29
Before I begin to study this.. i need to know what 'switch', 'case', and 'break' do. And also what the markings infront of some variable names mean. example: eGameMode g_GameMode or _GameMode. I see theese three alot... what are they for?

I just love the smell of code in the morning...
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 12th Jul 2010 00:15 Edited at: 12th Jul 2010 00:18
eName for enum, g_Name for global, _Name for private class members i believe, sz for strings, i for int, f for float, what else? i dont remember

about the switch, its very simple structure:



Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 12th Jul 2010 17:18
So they are just markings? they don't change anythig about the variable?

I just love the smell of code in the morning...
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 12th Jul 2010 17:41
nope

jamesL
14
Years of Service
User Offline
Joined: 31st May 2010
Location:
Posted: 13th Jul 2010 06:20
"Before I begin to study this.. i need to know what 'switch', 'case', and 'break' do."

you need to go here

http://msdn.microsoft.com/en-us/beginner/cc305129.aspx

and download this

http://download.microsoft.com/download/2/F/4/2F46E904-5C10-4728-B1B1-F0CB1948F3EE/CPPBegGuidePDF.zip

its a FREE pdf download of

http://www.amazon.com/Beginners-Guide-Second-Herbert-Schildt/dp/0072232153/ref=sr_1_1?ie=UTF8&s=books&qid=1278991100&sr=8-1

C++: A Beginner's Guide
by Herbert Schildt

Login to post a reply

Server time is: 2024-10-02 07:24:36
Your offset time is: 2024-10-02 07:24:36