Translate some Lua code to C++

Can anyone here help with some C++? I have a bit of Lua code I need translated into C++ to create a hotfix for a program.

The code: https://pastebin.com/1q1zdPcZ
The program: https://github.com/tcobbs/ldview/blob/master/LDLib/LDrawModelViewer.cpp

Thanks!

Umm, LDraw, as in the thing Tim Courtney made a website about? I had many in person conversations with him about it in the early 00s.

What are you using this for? Just curious.

I need a copy of every LDraw model as an .obj file for . . . reasons. There used to be a converter, its defunct now.

I am just trying to add first-person shooter -style controls to the program. Other discussions are ongoing as well. But people there either do not know Lua or do not know C++. And the request would be off topic for SE.

I think LDView supports export to 3DS, which you might then convert to OBJ. (Dunno, have not tried it.)

Doesn’t look as though there’s anything difficult in the conversion, thankfully: some of Lua’s lack of type-safety can make it tricky sometimes.

Essentially, all of the lua stuff (like “function” and “local”) needs to be removed in favour of specifying variable types.
So something like this:

float[3] MatrixToEuler(float[9] in_matrix)
{
    float r00 = in_matrix[8]; //note, c++ arrays are zero-based
    float r01 = in_matrix[7];
    //etc.
    float angleX = 0.0f;
    //etc.
    if(r20 < 1.0f)
    {
        //not much changes in the next bit, except the ifs, as above
    }
    else
    {
        //...
    }
    float out[3] = {angleX, angleY, angleZ};
    return out;
}

You’ll need to make adjustments for the types the variables outside this function are using.

Awesome! I will try and make that work.

Slightly off topic question…
About 1.5 years ago I was looking for a new job. I had lots of C++ and Java experience on my resume. Not a single company had any interest in my C++ experience. It was either Java or C#.

Is C++ dead (except for maintenance) mostly now?

It’s still useful in the games industry. Well, the bits that are making games that are intensive on the processor. The performance advantage of C++ is non-negligible, and controlling your own garbage collection is valuable to avoid frame rate spikes.
Typical games in the company I work for use C++ for most code, C for a few bits of extreme performance-critical code (bits of physics, say) and Lua for scripting.

Plus the Lua JIT compiler is extremely fast, which makes it an ideal C++ partner. It’s also why Love2D is fast.

C++ is still used a lot in industrial applications.

No. Maybe 90% of my professional coding over the last 10 years (and across 4 companies) has been in C++. This would have been various forms of infrastructure at large internet companies, and the actual shipped end-user products at network device manufacturers. None of these were projects in maintenance mode, they were either greenfield or at least rapidly growing.

But these were areas with either realtime requirements or with enough machines that it’s cheaper to have more/better programmers than buy more servers. Obviously nobody in their right mind would write e.g. business logic heavy database apps in C++ today.

Yeah, C++ is still the right choice in quite a few domains. Many fields transitioned to easier/more abstract languages (Java, C#, Python, Elixir, etc.) but the ones that have performance (in a broad, flexible sense) as a big requirement are still mostly reliant on C and C++.

That said, there are languages now that might take that domain from C and C++ one day. Rust is one of the most promising ones, but it’s going to take a while before we know for sure how much things will change (or not).