Chaos Project

Featured Projects => Advanced RPG Creator => ARC Reactor Engine => Topic started by: G_G on March 09, 2011, 06:00:39 pm

Title: General Coding Help
Post by: G_G on March 09, 2011, 06:00:39 pm
This topic is for general help with C++ coding specifically for ARC. This is so we don't spam other topics and get them off topic. If you disagree, delete.

Alright just so I know I'm doing it right, heres Color.cpp so far.
namespace zer0
{
namespace RGSS
{
Color::Color()
{
this->red = 255;
this->green = 255;
this->blue = 255;
this->alpha = 255;
}
}
}


I've also committed an updated Color.h and Color.cpp
Title: Re: General Coding Help
Post by: Blizzard on March 09, 2011, 06:09:31 pm
I have updated your code.

Advice in between: It is good practice to include as few headers in other headers as possible. This reduces recompilation time when you change something. Adding headers in source files is fine.

One trick that is also often used are forward declarations to avoid header inclusion. e.g. if you have a class pointer of a certain type , you don't need to include the header to that type. You can use forward declarations and include the header in the source file only.

Code: B.h

class B
{
public:
    int c = 0;
};


Code: A.h

class B; // forward declaration of class B
class A
{
public:
   B* aPointer;
   A();
    ~A();
};


Code: A.cpp

#include "A.h"
#include "B.h"
A::A()
{
   this->aPointer = new B();
}
A::~A()
{
    delete this->aPointer;
}
Title: Re: General Coding Help
Post by: G_G on March 09, 2011, 06:13:49 pm
Alright thanks Blizzard. ^_^

Ugh! I can't update or commit the Color.cpp. Says its in conflict. :(

EDIT: Got it. Updated Color.cpp again. Getting the hang of this C++ stuff. Can't wait to get into the graphics drawing :3
Title: Re: General Coding Help
Post by: ForeverZer0 on March 09, 2011, 11:19:48 pm
I have a really stupid question:

I'm doing the Rect class at the moment, and I was wondering if I was doing this wrong.
For instance, I have the function "setX". Should the function simply change the "x" property I created, or should it simply be a function that will call the "setPosition" method from the parent class?

Title: Re: General Coding Help
Post by: G_G on March 09, 2011, 11:21:56 pm
I'm positive it just changes the X property. In fact, I'm just as stumped as you are now that you bring it up. O_O
Title: Re: General Coding Help
Post by: ForeverZer0 on March 09, 2011, 11:31:27 pm
The Rectangle class is already included, simply creating classes with properties doesn't seem neccessary.
I imagine I should be kind of creating an intermediate class that uses the RGSS arguments and methods of the Rect class, but "translates" them into the actual C++ Rectangle class that is being used.
Title: Re: General Coding Help
Post by: Ryex on March 10, 2011, 12:07:24 am
just create a class with methods like

void set_x(int x)
{
    this->x = x;
}
int get_x(int x)
{
    return this->x
}

and then we wrap them in ruby.
Title: Re: General Coding Help
Post by: ForeverZer0 on March 10, 2011, 12:14:22 am
Okay, I had did that, then I started second-guessing myself and over-complicating stuff. Thanks.  ;)
Title: Re: General Coding Help
Post by: Blizzard on March 10, 2011, 02:43:14 am
Quote from: ForeverZer0 on March 09, 2011, 11:31:27 pm
I imagine I should be kind of creating an intermediate class that uses the RGSS arguments and methods of the Rect class, but "translates" them into the actual C++ Rectangle class that is being used.


Exactly. Over time the RGSS classes will be remove and instead a Ruby script (the RGSS Compatibility Script) will provide this functionality and translate them into the Zer0 classes. But for now these classes will be in C++.
Title: Re: General Coding Help
Post by: ForeverZer0 on March 12, 2011, 01:31:45 am
How can I make a variable named with "?" like you can in Ruby?

ex.
bool disposed?() {}
Title: Re: General Coding Help
Post by: Ryex on March 12, 2011, 04:09:22 am
you can't, names can only contain A-Z, a-z, 0-9, and _ and they can't start with a number
Title: Re: General Coding Help
Post by: Blizzard on March 12, 2011, 04:16:06 am
Try it and see if it compiles. xD But as Ryex said, you can't. ? is an operator.
Title: Re: General Coding Help
Post by: ForeverZer0 on March 12, 2011, 04:25:32 am
It's no biggie, I ended up just dropping the "?".  I was just curious. I kinda like the convention of naming boolean returning methods with a "?" at the end. 
Title: Re: General Coding Help
Post by: Blizzard on March 13, 2011, 11:31:47 am
I have noticed that some of you have troubles with pointers.

In Ruby every object is actually a pointer to an object. That means the object is only one and there are many references to that object. In C++ it's different. You can have reference variables and pointer variables and you can have value variables. Color, Rect and Tone should always be value variables because they should work like integral types so that you don't have to worry about destroying them and clearing memory.

If you have value variables in a class header, you have to include the header to that type or you will get compilation errors. In that case a forward declaration becomes obsolete. Forward declarations should only be used for pointer variables instead of including headers.
Title: Re: General Coding Help
Post by: G_G on March 13, 2011, 11:37:16 am
Oh okay. So I was right when I was supposed to include the Color header. Oh well.
Title: Re: General Coding Help
Post by: Blizzard on March 13, 2011, 11:51:26 am
I just noticed something else I forgot to mention earlier. Default values for arguments are only necessary in headers. In source files you don't add them.
Title: Re: General Coding Help
Post by: ForeverZer0 on March 13, 2011, 02:45:13 pm
Okay, I have a couple stupid questions:
Title: Re: General Coding Help
Post by: Blizzard on March 13, 2011, 03:53:03 pm
1. Yes, you pretty much got it. When you use * after the type name, it means that this variable's actual value will be literally the memory address of the object. You can also use ** and *** and even more, but double pointers are very rarely needed, not to talk about triple pointers or more.

2. When you have a type as pointer, you can use a forward declaration. When you have it as normal value type (without any *), you have to include a header. You also have to include a header of the superclass if you subclass it. In source files (.cpp) you have to include headers either way.

EDIT: Here is a useful list of similarities and differences between Ruby and C/C++. http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-c-and-c-
Title: Re: General Coding Help
Post by: ForeverZer0 on March 15, 2011, 10:49:37 pm
Okay, really stupid question here. I noticed that we have been using the hclamp method to keep values in within range, such as values for color and tone.
Why are we keeping the opacity of things in a -255.0 to 255 range? I have been just following what others have seemed to do, but correct me if I'm wrong, but isn't the proper range from 0.0 to 255.0? I have never heard of negative alpha values before.
Title: Re: General Coding Help
Post by: Ryex on March 15, 2011, 11:27:31 pm
it true. no negative alpha. I'm not even sure what this would imply.
Title: Re: General Coding Help
Post by: ForeverZer0 on March 15, 2011, 11:30:56 pm
The same goes with Grayscale values in Color. They don't go below 0.0 either.
Title: Re: General Coding Help
Post by: Blizzard on March 16, 2011, 02:39:08 am
My mistake. Feel free to fix it.

EDIT: BTW, type conversion are not compiler errors but warnings. You don't have to fix them. I prefer to cast the types, but you can leave it for temporary code.

EDIT: Lol, a release compiled and optimized ARC Game.exe is only 11 kB without the icon. xD
Title: Re: General Coding Help
Post by: G_G on March 18, 2011, 10:45:55 am
FTW!!!