Alice's C and C++ Tutorials

You must understand the Game Editor concepts, before post here.

Alice's C and C++ Tutorials

Postby AliceXIII » Tue Jan 04, 2011 6:19 pm

[ I advise that you have had some experience with GE scripting before learning this as it will make it easier]

Hello i have decided to start a thread to teach basic C and C++! :D

i suggest going here and downloading CodeBlocks as it compiles C and C++ files making it loads easier for you:

http://www.codeblocks.org/downloads/26

•Windows 2000/XP/Vista/7
•Linux 32-bit
•Linux 64-bit
•Mac OS X
^^ These are the supported platforms for CodeBlocks!

if somebody wants something a little more advance there will be more advanced C tutorials later on C++ maybe that gets a bit more diffucult :P

But onto the first tutorial:

Firstly, you'll need CodeBlocks open and click File, New project, Empty Project and go through the PopUp screens till your to your blank typing canvas :D

im going to post a block of code and dissect and explain it to you so you get C easily The Anatomy of a C Program:

Code: Select all
/* Volume.C: Calculate sphere's volume. */
#include <stdio.h>
#define PI 3.14

float sphere( int rad );

int main()
{
    float volume;
    int radius = 3;
    volume = sphere( radius );
    printf( "Volume: %f\n", volume );
}

float sphere( int rad )
{
    float result;
    result = rad * rad * rad;
    result = 4 * PI * result;
    result = result / 3;
    return result;
}


Starting with the first line at the top of the code:

1.) This is a Comment just like the comment's in GE except you use /* to begin a comment & */ to end a comment.

2.) This include's C libray Function files so C can use them this particular one is for Input and Output (typing something in and getting a response).

3.) #define used the same as in GE to define a variable it's for defining constants in this example PI = 3.14 and we dont want it to change so we define it.

4.) is a C prototype sounds big but all it does is tells C the sphere function returns a float value and requires one integer argument.

5.) This ones easy main() in every C program is where the program starts to read at every C program has a main() in it.

6.) Declares that volume is a float.

7.) Declares that radius is an integer (int) and intialize's it to 3 or in other words.. The integer Radius equals 3.

8.) Initialize's volume to equal sphere's return result.

9.) is like GE's sprintf() function printf("anything you want to type: %f\n", integer or float or so on type of value) the %f\n means it prints the called for value the little f is for a float value little i would be for a integer.

10.) This is sphere's own function so it can do the math to determine the sphere's volume.

11.) just like 6 declares that result is a float value.

12.) just like 7 initializes that result = rad multiplied by rad multiplied by rad.

13.) these next 3 lines are a bit more interesting. but this line takes the previous result from line 12 and multiplys it by 4 and PI.

14.) line 14 takes the result from line 13 and divides it by 3.

15.) and finally line 15 returns the summed up result from line 12 to 14 to the sphere function line 10 which is where line 8 assigns this result to volume and line 9 prints this result on the screen.

whew that was long :D

i hope this helps people a C++ tutorial will come later hope your ready :P

{EDIT: i did forget to say that to compile and run your code press the blue arrow under the edit tab}
"Taking a breath of fresh air."
User avatar
AliceXIII
 
Posts: 325
Joined: Fri Sep 17, 2010 2:36 am
Location: victoria, texas
Score: 37 Give a positive score

Re: Alice's C and C++ Tutorials

Postby SuperSonic » Sun Jan 16, 2011 2:58 am

Good job alice :D :wink:
Thanx. Now I can make better games in GE :D
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Alice's C and C++ Tutorials

Postby Game A Gogo » Sun Jan 16, 2011 2:52 pm

just a slight clarification: Ge handles /* */ comment blocks, just won't highlight it.

Score for this tutorial :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Alice's C and C++ Tutorials

Postby AliceXIII » Sun Jan 16, 2011 6:40 pm

yeah i actually didn't think about it but the normal // comments in GE also work in C /* */ is more for paragraphs.. :P
"Taking a breath of fresh air."
User avatar
AliceXIII
 
Posts: 325
Joined: Fri Sep 17, 2010 2:36 am
Location: victoria, texas
Score: 37 Give a positive score

Re: Alice's C and C++ Tutorials

Postby Game A Gogo » Sun Jan 16, 2011 7:08 pm

well here's a little historical lesson :P
/* */ came from C
// came from C++
// was addopted by C++ because less chance of creating errors, but most compilers will support the old fashion way for portability reasons
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Alice's C and C++ Tutorials

Postby AliceXIII » Sun Jan 16, 2011 8:23 pm

hmmm commen's that's the one thing in programming i could never learn :lol:
"Taking a breath of fresh air."
User avatar
AliceXIII
 
Posts: 325
Joined: Fri Sep 17, 2010 2:36 am
Location: victoria, texas
Score: 37 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest