- Home /
Converting c++ to c# and procedural galaxy generation.
I am currently working on a project were I want to randomly generate a galaxy. Using the information found here I have been able to generate a rather good looking galaxy, however, I think that I could do better.
The website linked above contains source code on generating a galaxy, however, it is made in c++. I have been playing around with different aspects of this code for the last week, however, I have not been able to convert one section of the script into c#.
Bare with me, as the section is composed of a series of sections.
To start with the following line of code is presented in the .h script.
class CumulativeDistributionFunction
{
public:
typedef double (CumulativeDistributionFunction::*dist_fun_t)(double x);
private:
dist_fun_t m_pDistFun;
}
I did some research into 'typedef' and found that it is used to define a variable. In this case it is showing that m_pDistFun is a double. However, I do not understand what the '*' and the extra (double x) is doing.
Another point of confusion is when the variable for m_pDistFun is set in the cpp script.
void CumulativeDistributionFunction::SetupRealistic(double I0, double k, double a, double RBulge, double min, double max, int nSteps)
{
m_pDistFun = &CumulativeDistributionFunction::Intensity;
}
What confuses me most about this is that the function "Intensity" takes a variable like shown bellow.
double Intensity(double x){
return somevalue;
}
I believe that this can be done because of how 'm_pDistFun ' is used in a calculation.
y += h/3 * ((this->*m_pDistFun)(m_fMin + i*h) + 4*(this->*m_pDistFun)(m_fMin + (i+1)*h) + (this->*m_pDistFun)(m_fMin + (i+2)*h) );
I have no idea what 'this->' does. I think it is a value being put into the 'Intensity' calculation but I am not sure.
I would really appreciate if someone will c++ knowledge could have a look at the source code and explain to me what is happening so I can replicate the affects in c#.
It's rather a big ask to explain C++ in a Q&A style site! However, to answer most of your questions I recommend you look up pointers - they're pretty fundamental to the language and will be explained in any introductory C++ reference.
Answer by toromano · Jul 01, 2016 at 06:28 AM
dist_fun_t is a function pointer which takes double as a parameter and returns a double. This is how you can assign it to "CumulativeDistributionFunction::Intensity" function since the signature is valid for the case. You can think function pointers as delegates in c#. (Also i should mention that "functors" in c++ are more similar to delegates in c# since they are callable objects too.)
The expression "(this->*m_pDistFun)(m_fMin + i*h)" is how member function pointers are called.
http://www.learncpp.com/cpp-tutorial/78-function-pointers/
"this->" does the same for c++ and c#. "this" is the object itself in the class scope. (Also you might use "this." sometimes instead of of "this->" if the member object is stack allocated.)
http://en.cppreference.com/w/cpp/language/this
Hope this will be useful.
@toromano That sounds about right. So what you are saying is that calling
(this->*m_pDistFun)(m_f$$anonymous$$in + i*h)
is the same as calling
Intensity(m_f$$anonymous$$in + i*h)
however, due to the nature of c++ it was written like the former?
It is not about c++ being an older language. There is probably a good reason to use function pointers rather than calling the functions directly in the sdk that you are using. Function pointers let you use functions as parameters and be able to pass them to functions. It is more flexible.
Your answer
Follow this Question
Related Questions
How to step into a native C++ dll in Visual Studio? 0 Answers
DllNotFoundException in standalone 2 Answers
RunTime Error in Unity when I'm using a function from a C++ Dll 0 Answers
proceduraly generated galaxy 3 Answers
C++/CLI DLL import error 0 Answers