Page 1 of 1

[SOLVED] How much c++ should i know before learning a API?

Posted: Wed Feb 24, 2010 8:04 pm
by mv2112
How much c++ should i know? I just started learning about Objects and im getting the hang of it, pointers on the other hand... :roll:
When would you even use pointers? I cant think of any reason, but then again, i am a noob at c++.
Also, anyone ever use DarkGDK?

Re: How much C++ should you know before attempting an API?

Posted: Thu Feb 25, 2010 4:49 am
by Live-Dimension
mv2112 wrote:How much c++ should i know? I just started learning about Objects and im getting the hang of it, pointers on the other hand... :roll:
When would you even use pointers? I cant think of any reason, but then again, i am a noob at c++.
Also, anyone ever use DarkGDK?
ATLEAST OOP and a basic understanding of pointers. Most API's use one or both of them to high extent. Then you can learn the rest while making basic fun games =]

Different SDK's will require different things after that. Just start simple.

Re: How much C++ should you know before attempting an API?

Posted: Thu Feb 25, 2010 5:43 am
by Lord Pingas
mv2112 wrote:How much c++ should i know? I just started learning about Objects and im getting the hang of it, pointers on the other hand... :roll:
When would you even use pointers? I cant think of any reason, but then again, i am a noob at c++.
Also, anyone ever use DarkGDK?
You should know about object oriented methods of programming (encapsulation, inheritance and polymorphism), pointers and what there used for, referencing to data, and of course you should also already know the basics (input, output, statements, strings, etc...).

Re: How much C++ should you know before attempting an API?

Posted: Thu Feb 25, 2010 8:14 am
by ClassA
mv2112 wrote:...When would you even use pointers? I cant think of any reason...
There are all sorts of occasions when you might want to use a pointer, consider:

- You want to pass a reference to an object into a function/method, rather than having the whole object copied (requires more time/memory) each time you call it

- You need several objects to refer to the same object. Maybe you're making an RTS, and all of your hundreds of "Unit" instances contain pointers to the same handful of "Faction" objects

- You want to store some sort of structure like a graph or tree where you are interested in several interconnected objects, perhaps for some sort of tech-tree in your game. Without pointers, how would you handle this? Two objects connected to each other need some way to refer to each other and cannot simply hold a copy of each other (what would these copies contain?) - you would probably give each object an ID number and use these to describe how the objects are connected... This is basically what a pointer provides, an "ID" which refers to a specific object instance (or, more accurately, its memory address).

Re: How much C++ should you know before attempting an API?

Posted: Thu Feb 25, 2010 10:57 am
by nardi11011
Or a slightly more simple explanation...

Let's say you have a game, with "enemies" in it. Every type of enemy looks different, so each one has their own "sprite". Now, what if you have a lot of one type of enemy, let's say about one hundred. You'd also have one hundred sprites, one for each enemy. But they all look the same, so why use so many sprites when only one is actually needed? This is where pointers come in. Instead of giving every enemy a sprite you would give every enemy a pointer to a sprite. This way, you just need one sprite, and can then have all enemies of the same type point to that same sprite. And that's it, no more useless sprites!

Re: How much C++ should you know before attempting an API?

Posted: Thu Feb 25, 2010 12:25 pm
by Falco Girgis
When would you even use pointers?
You probably aren't going to be anywhere near ready until you can answer that question yourself.

No offense, really. It's just that that is an extremely fundamental concept of C/++ development that you're going to need to master before working with any API.

Re: How much C++ should you know before attempting an API?

Posted: Thu Feb 25, 2010 1:51 pm
by mv2112
Ok, i get it, i think i'v got a lot more to learn about c++ before i master it lol 8-) .

Re: How much C++ should you know before attempting an API?

Posted: Thu Feb 25, 2010 3:51 pm
by mv2112
mv2112 wrote:Ok, i get it, i think i'v got a lot more to learn about c++ before i master it lol 8-) .
Now I understand pointers a little better, I just ran into a problem where I wanted a member function to access another object's variable and it worked when i used pointers!

Re: [SOLVED] How much c++ should i know before learning a API?

Posted: Thu Feb 25, 2010 4:14 pm
by K-Bal
I would just try to create a Pong, Pickin' Sticks or an other extremely simple game. The worst thing that can happen is that you fail. In this case, read some articles/books about it and try again.

Code: Select all

while(createSimpleGame() != done)
{
 ++brain;
}
Edit: And show us your results ;)
Edit2: One more thing I wanted to add: there will always be something that you have not understood yet. It does not really matter if your first Pong has memory corruptions and wrong OOP all over the place. Your mistakes will help you to understand these concepts more quickly.

Re: [SOLVED] How much c++ should i know before learning a API?

Posted: Thu Feb 25, 2010 4:41 pm
by mv2112
K-Bal wrote:I would just try to create a Pong, Pickin' Sticks or an other extremely simple game. The worst thing that can happen is that you fail. In this case, read some articles/books about it and try again.

Code: Select all

while(createSimpleGame() != done)
{
 ++brain;
}
Edit: And show us your results ;)
Edit2: One more thing I wanted to add: there will always be something that you have not understood yet. It does not really matter if your first Pong has memory corruptions and wrong OOP all over the place. Your mistakes will help you to understand these concepts more quickly.
Well, i'v been working on learning about objects lately and this is what i have. Its basicly a text-based attack game. Nothing special. However, i did manage to get ONE pointer into the code lol (its used for a cheat code function). Im starting out small with text-based games before i start learning an API. Here is the main.cpp

Code: Select all

#include "main.h"

//Player Object Defenition//
class Player{
public:
	bool a_check();
	int Attack();
	void Defend(int attack);
	void Heal();
	int Special();
	void Recharge();
	Player();
	bool alive;
	void stats();
	bool quit();
	void win(bool* al);
private:
	int health;
	int heal;
	int attack;
	int defend;
	int sattack;
	int recharge;
};

bool Player::a_check(){
	if(health<=0){
		alive=FALSE;
		return FALSE;
	}
	else{
		return TRUE;
	}
}

Player::Player(){
	health=20;
	heal=5;
	alive=TRUE;
	recharge=100;
	cout<<"Get Ready to FIGHT!"<<endl;
}

int Player::Attack(){
	attack=(rand()%5);
	return attack;
}

void Player::Defend(int attack){
	defend=(rand()%5);

	if(defend>=attack){
		cout<<"No damage has been done to you!"<<endl;
		return;
	}
	else{
		health-=attack-defend;

		if(health<=0){
			cout<<"You are dead"<<endl;
			return;
		}

		cout<<"The enemy did "<<attack-defend<<" damage to you!"<<endl<<"You have "<<health<<" HP left!"<<endl;
		return;
	}
}

void Player::Heal(){
	if(heal>0){
		--heal;
		health+=10;
	}
	if(heal==0){
		cout<<"You are out of health packs!"<<endl;
		return;
	}
}

int Player::Special(){
	if(recharge==100){
		sattack=(rand()%10)+1;
		recharge=0;
		return sattack;
	}
	else{
		cout<<"You arn't recharged yet!"<<endl;
		cout<<"Recharged:"<<recharge<<"/100"<<endl;
		attack=(rand()%5);
		cout<<"You regular attack the enemy..."<<endl;
		pause();
		cls();
		return attack;
	}
}

void Player::Recharge(){
	if(recharge<100){
		recharge+=25;
		return;
	}
	else{
		return;
	}
}

void Player::stats(){
	cout<<health<<"/20 HP"<<endl;
	cout<<heal<<"/5 Health Packs"<<endl;
	cout<<recharge<<"% Recharged"<<endl;
}

bool Player::quit(){
	alive=FALSE;
	return alive;
}

void Player::win(bool* al){
	*al=FALSE;
}
//End Player Object Defenition//

//Enemy Object Defenition//
class Enemy{
public:
	Enemy();
	int Attack();
	void Defend(int attack);
	bool a_check();
	bool alive;
	void debug();
private:
	int health;
	int attack;
	int defend;
};

Enemy::Enemy(){
	health=20;
	alive=TRUE;
	cout<<"Im going to kill you..."<<endl;
}

int Enemy::Attack(){
	attack=(rand()%5);
	return attack;
}

void Enemy::Defend(int attack){
	defend=(rand()%5);

	if(defend>=attack){
		cout<<"HA! You have done no damage to me!"<<endl;
		return;
	}

	else{
		health-=attack-defend;
		if(health<=0){
			cout<<"The enemy is dead"<<endl;
			return;
		}
		cout<<"You have done "<<attack-defend<<" damage to the enemy!"<<endl<<"The enemy has "<<health<<" HP left!"<<endl;
		return;
	}
}

bool Enemy::a_check(){
	if(health<=0){
		alive=FALSE;
		return FALSE;
	}

	else{
		return TRUE;
	}
}

void Enemy::debug(){
	cout<<attack<<endl<<defend<<endl<<health<<endl;
}
//End Enemy Object Defenition//

int main(){
	set_title("AttackSimulator V2.0");
	text_color('a');

	char input;

	Enemy bad;
	Player good;
	
	space();
	pause();

	bool* cheat;

	while(bad.alive==TRUE&&good.alive==TRUE){
		cheat=&bad.alive;
		good.Recharge();
a:
		srand(time(NULL));
		cls();
	    cout<<"1. Attack"<<endl;
	    cout<<"2. Special Attack"<<endl;
	    cout<<"3. Heal"<<endl;
	    cout<<"4. View Stats"<<endl;
	    cout<<"5. Run away"<<endl;
	    cin>>input;

		cls();
		
		switch(input){
			case '1':
				bad.Defend(good.Attack());
				if(bad.a_check()==FALSE){
					break;
				}
				space();
				good.Defend(bad.Attack());
				if(good.a_check()==FALSE){
					break;
				}
				space();
				pause();
				break;
			case '2':
				bad.Defend(good.Special());
				if(bad.a_check()){
					break;
				}
				good.Defend(bad.Attack());
				if(good.a_check()){
					break;
				}
				space();
				pause();
				break;
			case '3':
				good.Heal();
				space();
				pause();
				break;
			case '4':
				good.stats();
				space();
				pause();
				goto a;
				break;
			case '5':
				good.quit();
				break;
			case '~':
				good.win(cheat);
				break;
			default:
				continue;
				break;
		}
	}
end:
	if(good.alive==FALSE){
		cout<<"You loose"<<endl;
		space();
		pause();
	}

	if(bad.alive==FALSE){
		cout<<"You Win!!!"<<endl;
		space();
		pause();
	}
	
}
And here is the main.h file, i just included some libraries and defined some usefull functions.

Code: Select all

#include <conio.h>
#include <windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <time.h>

using namespace std;

void pause(){
system("pause");
}

void cls(){
system("cls");
}

void exit(){
system("exit");
}

void text_color(char color){
  
  switch(color){
    case 'a':
    system("color a");
    break;
    case 'b':
    system("color b");
    break;
    case 'c':
    system("color c");
    break;
    case 'd':
    system("color d");
    break;
    case 'e':
    system("color e");
    break;
    case 'f':
    system("color f");
    break;
  }
  cls();
}

void set_title(string title){
     string t;
     t="title "+title;
     system(t.c_str());
     return;
     }
     
void space(){
cout<<endl;
}

I was surprised that the Player class worked. I wrote it down during social studies class in my notebook lol. It actually compiled perfectly on the first try, but i added some different member functions later. So how does my code look? Im open to all criticism(constructive criticism is preferred lol).

Re: [SOLVED] How much c++ should i know before learning a API?

Posted: Thu Feb 25, 2010 4:53 pm
by K-Bal
Not too bad ;) Did you notice that Player and Enemy have many things in common? They are both some kind of entity that can attack, defend, etc. A base class for both would fit perfectly ;)

Re: [SOLVED] How much c++ should i know before learning a API?

Posted: Thu Feb 25, 2010 5:30 pm
by mv2112
K-Bal wrote:Not too bad ;) Did you notice that Player and Enemy have many things in common? They are both some kind of entity that can attack, defend, etc. A base class for both would fit perfectly ;)
I did notice that they had some things in common, ill have to try making a base class.