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
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.
class B
{
public:
int c = 0;
};
class B; // forward declaration of class B
class A
{
public:
B* aPointer;
A();
~A();
};
#include "A.h"
#include "B.h"
A::A()
{
this->aPointer = new B();
}
A::~A()
{
delete this->aPointer;
}
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
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?
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
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.
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.
Okay, I had did that, then I started second-guessing myself and over-complicating stuff. Thanks. ;)
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++.
How can I make a variable named with "?" like you can in Ruby?
ex.
you can't, names can only contain A-Z, a-z, 0-9, and _ and they can't start with a number
Try it and see if it compiles. xD But as Ryex said, you can't. ? is an operator.
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.
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.
Oh okay. So I was right when I was supposed to include the Color header. Oh well.
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.
Okay, I have a couple stupid questions:
- As for pointers, I read a few things to see what I could learn. Correct me if I'm wrong, but I and I can basically add a * at the end of the type declaration in the argument to tell C++ that this is the location of this object, not the object itself. I realize that there will be differences calling the function, etc, etc, but I noticed I messed that up in some of the files I made.
- #include VS. forward declaration. How does one know for sure when to use which?
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-
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.
it true. no negative alpha. I'm not even sure what this would imply.
The same goes with Grayscale values in Color. They don't go below 0.0 either.
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
FTW!!!