Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 08 Aug 2000
Offline Last Active Aug 17 2016 04:05 AM

#5292998 Wrapper Function for lua_register?

Posted by on 23 May 2016 - 01:28 AM

This has nothing to do with the Lua registry. Or even Lua.

A member function (that is, a non-static function in a class or struct) is fundamentally different from a free function. A free function (often called a C function) can interact very simply with C code. A member function cannot.

Lua is written in C and thus cannot interact interact with C++ (there is no well-defined C++ ABI and as such 'calling a member function' is an unsolvable problem because different C++ compilers have very different idea what exactly that means and how to do that).

The link ApochPiQ posted will certainly help with part of your problem ('how do I write glue code to deal with that'). It does not seem to go into much detail beyond that and it opens a whole can of worms about 'why do I have to do that at all?' (for a few basic keywords to start your search, see above) as well as where to store the this-pointer as well as questions about the lifetime and ownership of this between C++ and Lua from then on.

It would have been a better idea to post this in For Beginners since that would have obligated people to be a bit more verbose in their answers (I don't have the time now, though). I would also advise you to take a step back from Lua for the moment and deal with the issues raised here in a pure C++ world for now. It's not horribly difficult but you will need to spend some time there to get a feeling for the area and Lua just adds additional complications on top of that.

#5291083 Operating System Questions in Assembly

Posted by on 11 May 2016 - 01:27 AM

Based on your question, I think you should step back and evaluate your ability to complete this project.

Honestly, you are building a space shuttle and you've just asked about spray painting the NASA logo on the side.

The OP is (according to their profile) thirteen. Personally, for that age group I find it advisable to just give them the information desired and let the empirical values of something deceptively simple-sounding become a huge problem be a first-hand experience. We get a lot of older people who really should have learned that lesson earlier.

It's not even wasted time (especially at that age). You will be gaining skill points all over the place while working on it even with the originally intended goal not achievable.

#5290804 Operating System Questions in Assembly

Posted by on 09 May 2016 - 08:42 AM

Executables on Windows follow the PE Format. Note that to run Windows executables you not only have to interpret the data format correctly you will also have to emulate a considerable part of the Windows runtime environment. Wine might be a good starting point for that.

Also note that it is generally an extremely horrible idea to put two completely different questions into the same thread.

#5290768 Manually loading OpenGL functions on Windows

Posted by on 09 May 2016 - 03:05 AM

I have never dealt with working with loading GL functions by hand but that does not surprise me. The documentation of wglGetProcAddress says

The wglGetProcAddress function returns the address of an OpenGL extension function [...]

. glClear is to my knowledge as core as you can get in any version of OpenGL so should never be retrievable via wglGetProcAddress.

#5290520 How to implement Lua scripting into my game?

Posted by on 07 May 2016 - 02:12 AM

There are plenty of solutions. I had a look at both lua-intf and integral in the past and they seemed to offer a lot of convenience and ease of use. Note you might also want to look at less general and more domain-specific solutions for your scripting. That will almost certainly require some work of your own but you can squeeze out a few drops more of performance and support scripting semantics which will be more difficult with a general solution. You will also learn a lot of the nitty gritty details of interaction with Lua.

#5289216 Data alignment on ARM processors

Posted by on 29 April 2016 - 05:23 AM

*((unsigned char*)(&result)) = *offset;

Watch out for compiler optimisations breaking code like this (called type punning). It breaks the language aliasing rules. See this link:


Except this is irrelevant since the char-types are explicitly allowed to do this.

#5289201 Which alignment to use?

Posted by on 29 April 2016 - 01:30 AM

[...] if your Component types are not strictly PODs (as in is_pod_v<Component> == true).

Correct me if I'm wrong, but isn't the check for PODs too strong? std::is_trivially_copyable_v should be enough.

#5287751 std::sort corrupts my memory

Posted by on 20 April 2016 - 06:21 AM

Then you either use an extremely ancient version of the compiler or you can just write
without extra spaces between template closing brackets.

#5286849 Simple code for reading and writing file in UTF-16 mode

Posted by on 14 April 2016 - 05:56 AM

I would just try to read/write the data using standard streams on char16_t instead of char while not trying to change the locale or anything else.

Well, actually I would avoid UTF-16 like the plague.

#5286635 I need a book (or somebody's project source code) which properly explains...

Posted by on 13 April 2016 - 05:32 AM

As you can see I have no idea what I'm doing. I'm taking lines of code of random websites and pasting them into my program, then when it doesn't compile I move parts around so that stuff is (I hope) in the scope it's meant to be in. This is the technique I used to learn Blitz Max, and it works, because I can use Blitz today and instantly make a program which uses OOP, even though I haven't used Blitz since 2006  :lol:

This is not a method I would recommend to learn any language, least of all C++ where a lot of things can "appear to work" very easily while blowing up in your face later (sometimes much later, as in weeks or months).

Your problems are not really related to OOP though but essential bits of the language, like scoping.

This might be actually a disservice to you, but:
class Thing
Thing(int a, int b)
ThingRec.x = x;
ThingRec.y = y;
ThingRec.w = (x + 32);
ThingRec.h = (y + 32);

void drawMe(SDL_Surface* pic, SDL_Surface* vid) const
SDL_BlitSurface(pic, 0, vid, &ThingRec);
int x;
int y;
SDL_Rect ThingRec;

int main(int argc, char* argv[])
std::list<Thing> myList;
myList.push_back(Thing(10, 12));
myList.push_back(Thing(11, 13));

for (const Thing& thing : myList)
thing.drawMe(/* whatever */);

On a sidenote, when I see "using namespace std;" in a file I have the urgent need to punch the author.

Edit: Mhm. Apparently the darn forum editor does not even allow me to preserve spaces anymore. Apparently not a week can go by without it becoming ever slightly more broken...

#5286630 I need a book (or somebody's project source code) which properly explains...

Posted by on 13 April 2016 - 05:04 AM

Yes, I suspect that "abstract class" is a term one uses when there is an abstract class, with inheritance used for sub-classes - hence, the original class (not actually used directly) is called the abstract class. I'll edit my file now and add the proper terms  :)

You suspect wrongly. An abstract class is a class which contains one or more pure virtual member functions. An abstract class cannot be instantiated because the compile prevents you from doing that. A lot of base classes are not abstract even when they are seldom or never intended to be instantiated.

#5285755 Should getters and setters be avoided?

Posted by on 08 April 2016 - 03:36 AM

One of the reasons Yegor mentions is the dependency of clients connected to the object. Say, we suddenly want to know who withdrawn something from the object? I am not understanding why his "idea" would help us in any way. We still have to change the withdraw method and all clients using this withdraw method need to be changed. I guess I still have no clue what he is talking about.



If you can only withdraw money by calling 'withdraw' you can just add a new parameter to withdraw and your compiler will scream at you until all places where money is withdrawn supply that information. If you just manually decreased amount everywhere you have to find all uses of 'amount' manually in the code, identify which are actually withdraw operations and add the withdrawer information manually (help the compiler can be to you in that case: zero).



This eventually results in long constructors since all the final properties that are allowed to mutate (yes it sounds crazy) need to be set in the constructor.


If your constructor becomes so complicated you have to pass so many parameters you probably have a class which just tries to do far too much. Look into the Single Responsibility Principle (https://en.wikipedia.org/wiki/Single_responsibility_principle). Having immutable classes where reasonably possible certainly simplifies a lot of problems, but especially in the context of game programming it is not always doable. Spending a few moment to ponder "Do I actually need that to be mutable?" for a new class is not a bad idea though.


Edit: Stupid editor screwed up the link...

#5285398 glew does not initializes extensions

Posted by on 06 April 2016 - 12:59 AM

Or you are linking GLEW statically which means executable and DLL both have their own distinct copy of GLEW and the copy you are initializing is not the copy used for further rendering.

#5285234 Project is missing SDL_mixer.h; I add it and the compiler gives 77 errors

Posted by on 05 April 2016 - 05:19 AM

I'm not familiar with SDL but my guess would be these errors have nothing to do with SDL but originate in whatever framework your book supplies, probably because utility code it expects is not compiled/linked together.

#5285222 Is it bad to use macros such as __LINE__ or __DATE__?

Posted by on 05 April 2016 - 03:38 AM

There is no library which could provide this information. You could only get that functionality with a custom preprocessing step before passing the source to the compiler (which would be just a hand-rolled kind preprocessor definition again).