Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 Jan 2000
Offline Last Active Yesterday, 07:47 PM

#5303017 Any Book Recomendations?

Posted by on 28 July 2016 - 05:45 PM

I'd recommend these books: 


-The Art of Game Design: A book of Lenses by Jesse Schell- This is a great book about how to look at game play and understand why its fun/engaging (not really programming)

-Game Coding Complete by Mike McShaffrey-This is another great book about what it takes to build a game and the fundamentals that go into it (more programming related, but at a high level)


Why are you waiting to learn a language?   A language is just a tool to express your ideas into something a machine understand.  Whoever advised you not learn other languages is wrong, for any reason.  If you can learn one language, it can make leaning others easier.  If your classes have some made up language for a test, knowing a real language can't hurt, it should be helpful especially if they're trying to teach fundamentals of computers.  


Go out and learn C# or Javascript and start experimenting in Unity right now.  It's free, there are great prebuilt assets to keep you moving forward, and it will let you focus on making your game fun vs getting bogged down in trying to  making a game.  It's a great way to get started and prevent stalling out.


BTW, you will never feel ready to start making a game.  There is always one more thing to learn.  Start now and build your experience base, even if it's not the perfect.





#5298526 Sharing object between 2 threads

Posted by on 29 June 2016 - 08:06 AM

It sounds like your main thread is running the simulation and your auxiliary thread is rendering the entities.  You can probably get away with reading the entity without locking if you make sure that you only read from it.  You need to look at it from a memory contention standpoint.  If you don't lock, you  run the risk of having part of the data change on you while you are reading from it.  It's up to you if that is a bad thing or not.


Double buffering the data may help prevent partial updates, but you need to prevent the buffers from being swapped while somebody is reading from it (again locking). 


You might want to consider your threading approach.  Having a separate render thread may not be the best thing in this case.  You might want to have the main thread do an update pass then a render pass but use a worker thread pool to achieve parallelism.  Instead of having a single "coarse grain" thread for rendering, use a bunch of work threads to get "fine grain" threading within a single phase.  


For example: In your main game loop, you update your game objects by passing them all off to worker threads to be done in parallel.  Then when they're all done, you go to the render phase.  In your render phase, you do entity visibility culling and build render commands per entity in a bunch of worker threads in parallel.  if you are able to encapsulate the render commands, you can build a list of them in the main thread and then pass the entire list off to a coarse grained rendering thread for execution while your main thread goes back to updating.  





#5287818 Object reference not set to an instance of an object

Posted by on 20 April 2016 - 01:24 PM

So the problem is that you are expecting the elements of the array to be there, but they're not.  You're only allocating the space for the elements in your array, you then need to put something there.  

PlanetDetails[] Planet = new PlanetDetails[10];
for(int i=0; i<10;i++)
  Planet[i]=new PlanetDetails();




#5283221 Engine RPM and wheel angular velocity

Posted by on 24 March 2016 - 01:29 PM

I would make engine RPM be some function of fuel flow or normalized throttle input.  Maybe something linear where 0 throttle = 500rpm (idle) and a throttle of 1.0 = 5000rpm.  You could then convert RPM to torque using some kind of mapping function, maybe use a datafile that defines the graph so you can have different types of engines.  Here's an example image that I found showing RPM -> torque




Once you have a torque value from your rpm value, then you need to apply your "transmission." Based on the current gear, maybe it's in reverse, you get a wheel revolution speed out of it and use that value to move your model.  To implement a clutch, you would have a function before your transmission that could simply either pass through the torque value or pass a 0 when the clutch pedal is pressed.


I'm sure I'm missing a bunch of things, but does that help answer your question?





#5278178 Embedding Lua in C# with P/Invoke

Posted by on 25 February 2016 - 03:51 PM

So the problem is that in Lua 5.3 lua_pcall is a macro, not a function.  You need to write a function that calls lua_pcallk (notice the K at the end).  I wrote mine like this: 


      [DllImport(LUA_DLL, EntryPoint = "lua_pcallk", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern int lua_pcallk(IntPtr state, int nargs, int nresults, int errfunc, IntPtr ctx, LuaContinuationFunction k);

      public static int lua_pcall(IntPtr state, int nargs, int nresults, int errfunc)
         return lua_pcallk(state, nargs, nresults, errfunc, IntPtr.Zero, null);

There were quite a few changes in the API from 5.1 to 5.3.





#5278159 Embedding Lua in C# with P/Invoke

Posted by on 25 February 2016 - 02:30 PM

I'll fill in what I can.   This may not be the best way to do this (and I'm sure there's bugs), but its been working for me so far.  


In order to use p/invoke means you need to have a DLL that exports some functions.  Don't worry about the calling convention, the P/Invoke API lets you set that for your DLL you're calling into.  You also don't really need a def file, building your project as a DLL and having the right preprocessor flags set will export the symbols you want to bind to (at least in the Lua source). To build the Lua library as a DLL you simply need to build the lua library with the flag LUA_BUILD_AS_DLL, which if you look in luaconfig.h (line 235) to cause the symbol LUA_API to be defined as __declspec(dllexport).   I didn't change any code and that is all that you need to do to build lua5.3.dll with its symbols exported.  


Once you have a dll with symbols (or feel free to take mine, it's a vanilla 64bit Lua dll), you need to write a C# file that can call into it.  I usually create my dll interface as a static class since each of these functions will be static functions.  I simply went through the public Lua header and then copied the functions I want to be able to call to my C# file as comments so I have them there to reference and then started writing out the p/invoke function definition for each of the C functions. 


      #region state manipulation
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
LUA_API void       (lua_close) (lua_State *L);
LUA_API lua_State *(lua_newthread) (lua_State *L);
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
      [DllImport(LUA_DLL, EntryPoint = "lua_newstate", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern IntPtr lua_newstate(lua_Alloc f, IntPtr ud);

      [DllImport(LUA_DLL, EntryPoint = "lua_close", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern void lua_close(IntPtr state);

      [DllImport(LUA_DLL, EntryPoint = "lua_newthread", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern IntPtr lua_newthread(IntPtr state);

      [DllImport(LUA_DLL, EntryPoint = "lua_atpanic", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern LuaCSFunction lua_atpanic(IntPtr state, LuaCSFunction panicf);


Here you can see each of the static functions bind to a function in the C dll.  The DllImport attribute takes the dll filename, the function name as the entry point, the calling convention (see, no need to change how Lua is built, just change how pinvoke calls it), and a flag to suppress unmanaged security features for performance reasons.  Most basic types are handled automatically.  IntPtr is used for the pointer to the lua_State* since that's an opaque object (we don't access anything inside it ever).  If you need to shuffle complex structures between C and C#, you should look into how marshalling and layout is done on the C# side.  For callbacks, you simply need to define a C# delegate, but give it attributes that allow it to be called from C, and respecting the calling convention of your library.  Here's the delegate definition for a typical function pushed into Lua from C#:

   //Delegate for functions passed to Lua as function pointers using cdecl vs the standard stdcall
   public delegate int LuaCSFunction(IntPtr luaState);

You also need to make sure any delegates you pass into Lua from C# don't get garbage collected.  You do this by keeping a reference to the delegate around, maybe as a member of a class (see how I have myPrintFunction as a member of the LuaState below).


The dll filename is where I do something a little different.  Its an idea blatenly stolen from another project (OVR C# bindings I believe).  I preload the dll into the application before pinvoke looks for it.  I can specific the architecture I want (x86 vs x64) by prepending a path to the dll name and then using LoadLibrary to bring that dll into the application space before any p/invoke functions are called. When P/Invoke tries to find the function it looks in the application's loaded dlls first (only by name, not path+name which is why this works), then going to the working directory and looking for dlls with matching names, then if one isn't found there looking in the system path.  Pulling the DLL into the application space first can help prevent the wrong DLL being used (can happen when you have 2 versions of lua in your path, or getting a 32bit DLL  when you need a 64bit DLL).  You don't have to do this, just be sure that the DLL you want is in your application working directory or your path.


As I worked through the lua C api, I occasionally came across a macro that acted like a function, so I just implemented it as a function. Such as this:

/*#define luaL_dostring(L, s) \
   (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))

      public static int luaL_dostring(IntPtr L, String s)
         int ret = luaL_loadstring(L, s);
         if (ret != 0)
            return ret;

         return lua_pcall(L, 0, LUA_MULTRET, 0);

Once I got through the API (there were a couple of things I either stubbed out or didn't implement, but not many), I started adding some functionality on top of the raw dll calls.  I created a LuaState object that internally has an IntPtr to a lua_State* object that is created from LuaDLL.  This class provides access functions to get/set values by name/index as well as create tables and references to them. The class also provides some nice debug functions such as printing out the lua stack and printing out a lua table to a C# console.

public class LuaState
      LuaCSFunction myPrintFuction;
      IntPtr myStatePtr;
      LuaObject myGlobalTable;

      public LuaState()
         myStatePtr = LuaDLL.luaL_newstate();

         LuaDLL.lua_getglobal(myStatePtr, "_G");
         myGlobalTable = new LuaObject(this, -1);

         myPrintFuction = new LuaCSFunction(print);

         LuaDLL.lua_pushcclosure(myStatePtr, myPrintFuction, 0);
         LuaDLL.lua_setglobal(myStatePtr, "print");

         printCallback = new PrintCallback(defaultPrint);



The LuaObject provides an interface to any lua value whether or not it's a number, string, table, or function.  It allows for iterating over a table, retrieving values or calling lua functions.  It's just a nice abstraction. 


Let me know if you have any questions.






#5277991 Embedding Lua in C# with P/Invoke

Posted by on 24 February 2016 - 08:38 PM

I wanted the same thing a while ago and rolled my own for my game engine.  If you want, please take a look at my Lua bindings for C# at: https://github.com/bholcomb/Lua-  There is a thin layer on top of the pinvoke bindings, which covers about 99% of the Lua API.  I wrote c# functions to implement the macro's in the lua header as well.  If you only want the "pure" C api, then you should only need what is in the LuaDll.cs file.  The LuaState and LuaObject are part of my thin wrapper. 


It has the full lua 5.3.2 source built as part of the project, so you should only need to download, then run the build config (premake) then build the project to produce all the dlls you need.  It is expecting lua5.3.dll in a x86 or x64 folder where you run your executable from.  You can change how this works in the dllLoader class in the luaDll.cs file.


There is also a simple test console written in C# that uses the Lua#.dll to verify that it's working.  


Please let me know if you find any bugs or have any questions.





#5270562 Modern OpenGL Transforms, HOW?

Posted by on 11 January 2016 - 12:33 PM

You could load up your model and indices into a VBO/IBO and then setup your VAO + shader with them.  You would then create a uniform buffer object that holds the transform matrices  as an array for all your entities and update that every frame.  Then render all the models at once using either instancing or multidraw rendering.  In your shader use the draw_ID to index into the transform matrix array to get that particular model's transform.


If you have multiple models, as long as they use the same shader and have the same vertex format, you could store them all into a single VBO/IBO/VAO and use mulitidraw with different offsets.  You would need to look at putting all the textures in a texture array or using direct state access for the texture data.


Instead of a uniform buffer, you could use a shader storage buffer if you have a lot of data to store per instance (transform, material ids, etc), then index into it using the draw_ID.  If you go the SSBO route, you may want to look at triple buffering it and persistently mapping it so that you don't stall out rendering trying to update it.





#5174203 Looking for 'Visual' HTML/PHP editor.

Posted by on 16 August 2014 - 07:53 PM

I did several heavy web apps (like games) for my day job last year.  The best IDE I found for javascript/html/php/SQL was phpStorm by JetBrains (http://www.jetbrains.com/phpstorm/).  It is able to edit everything, deal with remote servers, databases (including mysql like you're using), integrate with version control systems.  There' s a long list of features.  They also have a webStorm without the php stuff or database stuff.  I believe you can try it out for 30 days and a buy it for $99 as an individual. 





#5146806 Tips for Programming a GUI

Posted by on 13 April 2014 - 09:43 PM

You should also look at the "Immediate Mode" GUI talk on Molly rocket (https://mollyrocket.com/861). It's a slightly different way of thinking about GUI development. In my opinion it's easier to use as an end user, but not necessarily easier to build.

It's great for simple in game menus, but apparently can be used In fairly complex systems. Unity uses an IMGUI in their editor.



#5144111 How to communicate between c++ and c#?

Posted by on 03 April 2014 - 08:59 AM

Binding C# to C++ is hard and painful.  I recommend not doing it (I will give SWIG another look).  


I recently needed to bring a rather large c++ API into Unity (C#).  To do this I built a very minimalist C API (just 4 functions: init, shutdown, send message, tick), which I use P/Invoke to call.  I then pass messages back and forth between managed and unmanaged code using this API.  Messages are code generated classes (from a simple message definition) that serialize to buffers and then passed through the sendMessage function to the other side.  The code generator actually generates C++ and C# versions of the class so they know how to serialize/deserialize on either side.  There's definitely a performance hit for this approach, but it is the cleanest way I came up with that still allows me to expand what is passed between managed/unmanaged code with a minimal amount of work.


You might also want to consider doing something similar but with TCP sockets.  I've sat in a couple of sessions at GDC recently where people are building remote debug/edit capabilities into their game engines by passing messages over a socket, even if it's all within the same application.  





#5142725 Exporting "to WebGL"!?

Posted by on 27 March 2014 - 06:32 PM

You may want to look at glTF, it's a runtime format from Khronos.  It's designed originally for webGL.  Currently the way to get models in the format is to convert from Collada using their tool, but I'm working on a Blender import/export for it.





#5141591 Multithreaded drawing and logic

Posted by on 23 March 2014 - 08:44 PM

Another option is to change your threading model. You could run your update phase in a multithreaded fashion, using worker threads to update multiple groups of entities at the same time. Then if it's time to render the scene, create all your draw calls (also could be done in a bunch of worker treads) and send them off to the gpu.



#5138790 C++ or C# For Game engine with OpenGL and SlimDX

Posted by on 13 March 2014 - 06:43 PM

I think C# is a fine choice, especially for learning how a game engine works. OpenTK is a great library to use to get started.  It has openGL/AL/CL bindings + all the utility, input, math classes you can shake a stick at.  My game engine uses an OpenGL 4.3 context just fine with OpenTK and I haven't run into any limitations, aside from my own capability.


I've integrated Lua into my C# game engine and if you're looking at other languages, There's the iron languages (IronPython, IronScheme, etc) which integrate really nicely into C# since they're all managed languages using the same runtime.  





#5135032 Fastest map-like collection for <GUID, BaseClass*> lookup

Posted by on 27 February 2014 - 07:49 AM

I've found 64bit unsigned ints are more than enough for any GUID I've had.  I tend to use them as bit fields as well to help identify type and which machine they come from, so I could imagine I could get away with 32 bits in a simpler game.  The nice thing is that the comparison is simply an integer comparison, which is really fast.


As for quick look-up, you need to think about how many objects you have total.  If you have less than 100 objects, a simple array of objects may actually be faster than using std::map (which if I recall, is a red-black tree data structure).  However, if you have many objects (10000-1000000) you may need to test which is better, a std::map or some kind of hash table.