GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2011-11-11 13:19:20

dark_master4
Member
Registered: 2011-11-11
Posts: 2

Taking some of my GML and making a DLL out of it

I posted this in the extending GM forum before reading the rules. It is likely it will be closed but I'm asking here. I've read the rules before posting this time.

Ok so now, I'm an UBER C++ noob. I probably made some UBER mistakes, but this code seems fine to me.

I need your help to check that before I go in too deep with using this code.

Basically, I adapted a noise generator from Ken Perlin (He called it "Improved noise") into GML, and now that it's working in GML I want to make it into a DLL.

Here's my C++ code so far:

Expand#include <cmath>
#include <cstdlib>
#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT double NoiseGet(double seed, double x, double y, double z) {
	double	cx=std::floor(x),
			cy=std::floor(y),
			cz=std::floor(z),
			a=x-cx,
			b=y-cy,
			c=z-cz,
			u=SCurve(a),
			v=SCurve(b),
			w=SCurve(c);
	int	s=(int)seed,
		c000=RandomPoint(s,cx  ,cy  ,cz  ),
		c100=RandomPoint(s,cx+1,cy  ,cz  ),
		c010=RandomPoint(s,cx  ,cy+1,cz  ),
		c001=RandomPoint(s,cx  ,cy  ,cz+1),
		c110=RandomPoint(s,cx+1,cy+1,cz  ),
		c101=RandomPoint(s,cx+1,cy  ,cz+1),
		c011=RandomPoint(s,cx  ,cy+1,cz+1),
		c111=RandomPoint(s,cx+1,cy+1,cz+1);
	return	Lerp(w,Lerp(v,Lerp(u,Grad(c000,a  ,b  ,c  ),
			                     Grad(c100,a-1,b  ,c  )),
			              Lerp(u,Grad(c010,a  ,b-1,c  ),
			                     Grad(c110,a-1,b-1,c  ))),
			       Lerp(v,Lerp(u,Grad(c001,a  ,b  ,c-1),
			                     Grad(c101,a-1,b  ,c-1)),
			              Lerp(u,Grad(c011,a  ,b-1,c-1),
			                     Grad(c111,a-1,b-1,c-1))));
}
double SCurve(double t) {
	return t*t*t*(t*(6*t-15)+10);
}
double Lerp(double t, double a, double b) {
	return a+t*(b-a);
}
int RandomPoint(int s, double x, double y, double z) {
	std::srand(s+1234);
	srand((rand()+1234)*((int)x+1));
	srand((rand()+1234)*((int)y+1));
	srand((rand()+1234)*((int)z+1));
	return rand()&255;
}
double Grad(int h, double x, double y, double z) {
	double u,v,r;
	h=h&15;
	if(h<8) {u=x;} else {u=y;}
	if(h<4) {v=y;} else {if(h==12 or h==14) {v=x;} else {v=z;}}
	if(h&1==0) {r+=u;} else {r-=u;}
	if(h&2==0) {r+=v;} else {r-=v;}
	return r;
}

I need help as to find if my use of random and floor are correct. Again, I barely know C++ (But I do know C# pretty good)

If everything's allright, what are my next steps? (Don't explain them, I can read the how-to from tutorials. Just tell me what-to)

Edit:
Also, is it worth moving to straight C instead of C++ since I do not seem to need any of the features C++ provides. I didn't install any compiler yet.

Edit2:
I also like my registration date... WTH am I doing not playing Skyrim yet?

Last edited by dark_master4 (2011-11-11 13:26:47)

Offline

#2 2011-11-11 17:19:22

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Taking some of my GML and making a DLL out of it

First edit.

It is worth moving to c++ if you do large processing; using gmapi helps in that matter storing data and returning it via ds_ structures or generating models in c++. If you are in a loop in GML, it would be better if you could move the entire loop in the dll. If you simply call a dll function that does the same math as in GML, depending on the size of the code, it may not be the best way. Benchmarking will tell you if it's the right move.

Offline

#3 2011-11-11 20:02:00

dark_master4
Member
Registered: 2011-11-11
Posts: 2

Re: Taking some of my GML and making a DLL out of it

Well, from what you can see there are MANY function calls in this basic function.

paul23 from the GMC has closed my topic, obviously. He still pointed at a few things, like I didn't use any C++ features or even OO. He's right, but I don't think I need this here. Though first thing is, I mixed std::srand and srand in the same function, just next to each others... Fixing that.

I'm pretty sure it will be a speed boost to switch over to DLL. I'm taking the "risk".

If it's too much trouble helping me do a DLL then don't mind me tongue I'll probably get through this with google some day, but I figured I could use some expertise.

Offline

Board footer

Powered by FluxBB