gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > object oriented

#99951 - xtoc - Thu Aug 24, 2006 10:10 pm

Could someone give me a example of how to program object oriented?

java example :


The main class
---------------------

public class Main {

public static void main(String[] args) {
printitout pit1;
printitout pit2;
pit1 = new printitout("hello world");
pit2 = new printitout("universal acces");
}
}



The second class :
----------------------

public class printitout {

public printitout(String text){
System.out.println(text);
}
}


Thx, greetz

#99961 - sajiimori - Thu Aug 24, 2006 10:43 pm

You mean in C++? Neither of those constructs contains any state, therefore objects are not appropriate. Objects only make sense for things that contain state.
Code:

void printItOut(const std::string& s)
{
  std::cout << s << std::endl;
}

int main()
{
  printItOut("hello world");
  return 0;
}

#99970 - xtoc - Fri Aug 25, 2006 12:07 am

k but if you want to create a monster unit class that you want to use 20 times in a game? like every monster unit has there own speed, own x en y,....

How can you do such thing like that with ndslib code?

#99971 - DynamicStability - Fri Aug 25, 2006 12:42 am

You need to learn C++ if you want to instantiate class objects on the ds. Download the subpixel font class demo-ey thing to get an example of a class implementation and usage on the ds in C++. It's based off the 'combined' libnds template, and it's at my site!

But if you only know java....go get a c++ book. Try to find one that talks about the 'standard template library' also. You'll make fewer programming errors this way.

Don't listen to anyone that say's c++ is bloat. Hell, you might as well use PA_lib if you are new to both c++ and the ds. Whatever it takes to jump in.

Welcome!
_________________
Framebuffer is dead.
DynaStab.DrunkenCoders.com

#99972 - StoneCypher - Fri Aug 25, 2006 12:49 am

NDSLib is just a set of functions and definitions to handle the DS hardware. All you need to do is write standard C++ code.

Code:
class Monster {

    public:

        int       HpRemaining;
        const int BaseHp;

        void      TakeDamage(int Damage) { HpRemaining -= Damage; if (HpRemaining <= 0) { die(); } }
        void      Heal(int Health) { HpRemaining += Health; if (HpRemaining > BaseHp) { HpRemaining = BaseHp; } }

        Monster(useHp) : BaseHp(useHp) {}

};


You might consider reading a C++ book. One reasonably decent one which is free online is Thinking in C++, by Bruce Eckel. A much better book, which is not free, is Accelerated C++, by Barbara Moo.
_________________
Quidquid Latine dictum sit, altum Sonatur
[Images not permitted - Click here to view it]

#99999 - xtoc - Fri Aug 25, 2006 8:28 am

I was planning of creating a program that switch java code to ds code.

Still i did already do a experiment with c++,
c++ is more advanced

thank you all.

greetz

#100004 - Spaceface - Fri Aug 25, 2006 9:47 am

Go to http://www.cplusplus.com/doc/language/tutorial/
And read the Object Oriented Programming part half-way. But maybe the whole tutorial would be a good advice to you.

#100030 - sajiimori - Fri Aug 25, 2006 4:58 pm

Did you say you're going to write a program that automatically converts Java code to C++? That's an interesting project -- seems like it would work. The main concern is that Java runs in a garbage collected environment.