Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 16, 2005, 11:17 PM   #1
Roark
macrumors newbie
 
Join Date: Dec 2004
Location: Fountainhead
C++ Help

Okay

So far I have 2 problems in C++

1) I'm pretty much new to this soo bear with me. I get the whole variables thing and simple algebraic expressions. I even get the if else statements. Where i get stuck is the functions . could someone please give me a VERY simple explanation of what they do and are for. Also that thing at the top -int main()-...I don't get that either.

2) If somebody could give me an extremely simple game example or a website with one to check ou that would be great. It needs to be thouroughly explained though so I learn something.

Thanks,
Roark
__________________
He who hesitates, is not only lost...
But miles from the next exit
Roark is offline   0 Reply With Quote
Old Jan 16, 2005, 11:36 PM   #2
mwpeters8182
macrumors 6502
 
Join Date: Apr 2003
Location: Boston, MA
I'd check out a book on basic C/C++ programming. Anything like a game example would be far too complicated for someone who's just started programming (which it sounds like you are). I'd suggest the SAMS books, or something along those lines.

Have you studied any of the counting structures yet? I would do that before functions. But the best way to learn is by getting a book, and programming any of the examples.

MP
__________________
2.0 GHz Aluminum MacBook
iPhone 3G
mwpeters8182 is offline   0 Reply With Quote
Old Jan 16, 2005, 11:37 PM   #3
Mechcozmo
macrumors 603
 
Mechcozmo's Avatar
 
Join Date: Jul 2004
A function is a piece of code that you write to compute, say, sales tax. Then, wherever you are in your code, you type the name of the function and how much the item costs, and set the function equal to a variable. The function goes, calculates the sales tax, and then returns the value (initial value+sales tax) which you can then use. Better than re writing a million lines of code to get the sales tax every time you need it.
__________________
This is not the signature you're looking for.
This is not the signature we're looking for.
You can scroll down now.
You can scroll down now.
Mechcozmo is offline   0 Reply With Quote
Old Jan 17, 2005, 12:00 AM   #4
slooksterPSV
macrumors 68030
 
Join Date: Apr 2004
Location: Nowheresville
Send a message via AIM to slooksterPSV Send a message via MSN to slooksterPSV Send a message via Yahoo to slooksterPSV
int main() ( or even if you wanted to use void main() ) is the programs starting execution. It looks there to begin the program. The reason people use int is because they can send return codes at the end example:
PHP Code:
#include <iostream>

using namespace std

int main()
{
  
//some code
  
return program_code//0 if everything functioned correctly
  //x if it didn't which it'll let you know
  // but most programmers just put return 0;

Now with int main you can do some pretty cool things, you can have it to where the user sends arguments that do things when you load the program. NOTE: THIS NEXT EXAMPLE ISN'T 100% CORRECT! LOOK IT UP!

PHP Code:

... //started all the functions, we're skipping writing code

bool start(char[] v)
{
 
bool execution_code;
 ... 
//more code
 
return execution_code;
}

int main (int argvchar[] argcchar *args)
{
 
bool worked 0;
 if((
argv == 1) && (argc == '-v')) //lets say -v is for verbose mode
 
{
   
worked start(argc);
 }
 return 
worked;

3 dots (...) signifies, code would go there, but they didn't type it or that. There's a quick example. Pretty soon you can make cool looking functions that just pass functions like this
(*)(apple)(x, y) //in sense that would look like this for calling that function
apple(x, y);

There's my 2 cents
__________________
AMD Phenom 9600, 2GB RAM, 250GB HDD, ATI Radeon 4850, Vista Home Premium 64-bit
Gateway NV53 with Windows 7 Home Premium 64-bit & Ubuntu 10.04 Lucid Lynx 64-bit
slooksterPSV is offline   0 Reply With Quote
Old Jan 17, 2005, 11:12 AM   #5
Roark
Thread Starter
macrumors newbie
 
Join Date: Dec 2004
Location: Fountainhead
Hey

Thanks for the help but I don't think I asked the right way. I wanted to see a very simple game and its code EXPLAINED (even if I don't understand it). You know. Something 2d and simple so I could know where I should work up to.

Also... I need an editor (I have Taco if that works) and a free easy to download and install compiler.

Thanks
__________________
He who hesitates, is not only lost...
But miles from the next exit
Roark is offline   0 Reply With Quote
Old Jan 17, 2005, 11:18 AM   #6
hcuar
macrumors 65816
 
hcuar's Avatar
 
Join Date: Jul 2004
Location: Dallas
Quote:
Originally Posted by Roark
Hey

Thanks for the help but I don't think I asked the right way. I wanted to see a very simple game and its code EXPLAINED (even if I don't understand it). You know. Something 2d and simple so I could know where I should work up to.

Also... I need an editor (I have Taco if that works) and a free easy to download and install compiler.

Thanks
I think you are a long, long way from understanding even a simple 2d game. I'd recommend going out and buying a book as mentioned before. I you aren't understanding simple function declarations, you're a long way from understanding object oriented programming.
__________________
iMac 27" i5 Mid 2011 | 1TB HD | 16GB RAM
iPhone 4 32 Gig
hcuar is offline   0 Reply With Quote
Old Jan 17, 2005, 11:34 AM   #7
Toreador93
macrumors regular
 
Join Date: Sep 2003
Send a message via AIM to Toreador93
http://www.cplusplus.com/

A game, I think, would be very complicated. I've taken C++ I and part of C++ II, and I wouldn't know where to begin programming a game like Pong.
Toreador93 is offline   0 Reply With Quote
Old Feb 10, 2006, 03:50 PM   #8
locrelite
macrumors newbie
 
Join Date: Feb 2006
Absurd

Quote:
Originally Posted by hcuar
I think you are a long, long way from understanding even a simple 2d game. I'd recommend going out and buying a book as mentioned before. I you aren't understanding simple function declarations, you're a long way from understanding object oriented programming.
What is this?

I've been trying to get help from programmers with some conceptual stuff, and I get more of these "You won't understand, buy a book, step off".... Doesn't it piss you off? Why would anybody bother writing something like this? I you aren't understanding them.

A function is mini program. It is a set of commands, given a name that can be called later, thereby executing the commands. I write actionscript and php, so my syntax would be relatively alien; but for something like breakout, you would have a couple of functions called, say, "impact".

Now you would lay out a mathematical description of the ball's motion, and upon any change in the state of the ball, you compare its coordinates to the
coordinates of the bricks and the paddle-thingy, whatever it is.

if (the coordinates overlap) {
call the impact function
}

then send the angle and nature of the collision into the impact function. If it's a breakable brick, it's destroyed, if its the paddle, it may call another function determining the movement of a panel... regardless, the impact function will calculate the new trajectory of the ball, and determine if the thing it hit was destroyed. If it was destroyed, it would call a destroy function, which would remove the coordinates of the brick from the possible obstacle list (database, array, what-you-will).

Now everytime the ball hits something, it calls the impact function, which determines essentially everything necessary for the game to run (sans graphics and GUI).
locrelite is offline   0 Reply With Quote
Old Feb 10, 2006, 05:54 PM   #9
zimv20
macrumors 601
 
zimv20's Avatar
 
Join Date: Jul 2002
Location: chicago
Quote:
Originally Posted by locrelite
for something like breakout, you would have a couple of functions called, say, "impact".

Now you would lay out a mathematical description of the ball's motion, and upon any change in the state of the ball, you compare its coordinates to the
coordinates of the bricks and the paddle-thingy, whatever it is.

if (the coordinates overlap) {
call the impact function
}

then send the angle and nature of the collision into the impact function. If it's a breakable brick, it's destroyed, if its the paddle, it may call another function determining the movement of a panel... regardless, the impact function will calculate the new trajectory of the ball, and determine if the thing it hit was destroyed. If it was destroyed, it would call a destroy function, which would remove the coordinates of the brick from the possible obstacle list (database, array, what-you-will).

Now everytime the ball hits something, it calls the impact function, which determines essentially everything necessary for the game to run (sans graphics and GUI).
there are, of course, a million different ways to code a solution to a given problem.

but if i were to write a version of breakout, i wouldn't do it like this. not if i had an OO language at my disposal.
__________________
Oct 2011: check out my band's first album @ boxsetauthentic.com
zimv20 is offline   0 Reply With Quote
Old Feb 10, 2006, 06:19 PM   #10
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
Quote:
Originally Posted by locrelite
What is this?

I've been trying to get help from programmers with some conceptual stuff, and I get more of these "You won't understand, buy a book, step off".... Doesn't it piss you off? Why would anybody bother writing something like this? I you aren't understanding them.
It still doesn't mean that someone new to programming can start learning by looking at something as complex as a game. Even a simple game.

Maybe a nice straight forward animation would work as a suitable motivator because something actually happens on the screen while it's still simple enough to grasp.
gekko513 is offline   0 Reply With Quote
Old Feb 10, 2006, 07:15 PM   #11
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
The source code for the relatively simple 2D scroller SuperTux.

The source:

Get it here.
gekko513 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 05:25 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC