Hot answers tagged

19

If you rely on your code in order to pay for food and shelter, and you need to support cross-platform on unknown future platforms (or reasonably might need to support cross-platform in the future), then designing your code to rely on a bunch of unknown compiler writers' correct adherence to bleeding edge language standards is dangerous (and I would argue, ...


8

Smart pointers or handle classes, just like C++11. You could write your own long before C++11. I would even argue that the shared_ptr in C++11 is probably the wrong model entirely. All you do is make a type that holds a reference to a resource. This reference could be a pointer with some kind of lifetime management or it could just be some kind of numeric ...


7

At the moment I'm using std::shared_ptr to support multiple ownership of GameObjects so that they are held by both the scene and any other GameObjects within the game Don't do that. shared_ptr is often the wrong tool for the job, and that certainly applies here. Remember that smart pointers are for managing ownership; shared_ptr is about sharing ...


6

Instead of holding raw pointers, hold some other lightweight reference to the resource. A shared_ptr and corresponding weak_ptr instances are one such mechanism, but you can easily implement your own if you don't have access to those types (or their earlier Boost equivalents) or there are other reasons why those types would not be appropriate for your ...


6

If your willing to do a bit more work, you could look into refractoring solutions. clang has some interesting stuff happening in that area currently. It should be possible to use the auto keyword, run it through the refactorer which will find all uses of it, resolve them for you and output the code then compile it with whatever you want. But that means ...


5

First: Game Industry is moving really, really slow. It's a huge (financial sense) industry, so there are always many people against everything which is new/unknown/maybe smarter than them and/or no competitor uses it and/or no projects are known which were successful using this new stuff. some kind of catch-22. Second: Performance matters. Way (!) more ...


5

Your game loop code is correct, but your units are all mixed up -- you are effectively setting to your timestep to .00000003 nanoseconds! Your clock is counting in nanoseconds, so your dt should be in nanoseconds. If a clock ticks 30 times a second, it will tick every .033 seconds. There are many nanoseconds in a second, so it will take more nanoseconds to ...


5

Depending on your needs there are different data structure that you can use for geometry representation, before I answer your question I need to point out that geometric representation are usually chosen based on two basic factors, Topological Requirements: this includes the types of meshes you are going to store, triangles only? n-polys ? regular, ...


5

Half-edges are usually used for many serious geometrical analysis and modification algorithms. You may find it more convenient to add a higher-level abstraction layer on top of this if you don't want to work with half-edges directly.


5

Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. class Weapon { public: virtual ~Weapon() = default; virtual void action() = 0; }; class AttackWeapon : public Weapon { public: void action() override { // ... } }; class ...


5

Take a look at Awesomium. I have integrated it to SDL before and the process was fast. The biggest thing you have to deal with is translating SDL events to Awesomium. Otherwise the rendering is really easy, you can just query pixel data from Awesomium into an SDL surface IIRC, and then render that to your game.


5

There is a code smell here: Sprite* Graphics::GetSprite(std::string spriteID) { std::map<std::string,Sprite*>::iterator it; it = sprites.find("Square"); return it->second; } By not making sure that the object is valid, the it could be sprites.end(). According to some documentation, it's very dangerous to dereference the end() ...


4

How to I implement a variable number of threads that all execute at the same time? Build/use a container class (I would call it a ThreadPool) for holding threads or thread references (note that std::thread is only movable and not copyable), each time one of the threads is started it is not joined by default. Wehn ThreadPool is going to be destructed ...


4

Using tiles is a great idea first off (for a 2d game), as it allows you to effectively manage memory, loading, etc. and an effective part of using tiles for this reason IS to use a matrix, or map. In combination with a tileset for a given level or area, and with your base reflections (x and y plane) the matrix/map and tileset combo works a treat. Three ...


4

Non-array struct members for constant buffers in HLSL are packed on four byte offsets, as many as it can into 16-byte vectors. If a member would straddle a vector boundary, it starts a new vector. You cannot achieve this with just an alignment and pack directive, you need to have explicit padding in your CPU-side structure to emulate the CB layout rules. ...


3

I did a test for that and the short answer is openGL didn't return any error. OpenGL documentation also states: glBindAttribLocation can be called before any vertex shader objects are bound to the specified program object. It is also permissible to bind a generic attribute index to an attribute variable name that is never used in a vertex shader. ...


3

Easiest is to use stringstream and check if conversion to int succeeded (reached eof) #include "sstream" //your code cout << "Enter a guess: "; std::string str; cin >> str; std::istringstream iss(str); iss >> guess ; if (iss.eof() == false) std::cout << "its not int"; //cin >> guess;


3

The physical centroid is used by the physics engine as a center of mass. The center of mass is not the same as the position of a composition of shapes. For instance, a body can have a transform position of (0, 0), and have it's shape's vertices for a polygon located around (10, 10). The shape vertices will be specified relative to the transform's location. ...


3

I'm not 100% certain that I fully understand the thinking behind your question, but this should cover everything: ID3D11VertexShader *vs1; ID3D11VertexShader *vs2; device->CreateVertexShader (..., &vs1); device->CreateVertexShader (..., &vs2); We now have two vertex shaders, and this only needs to be done once, during startup. ...


3

No need to hook to HTML5 or native iOS/Android graphics functions when you have native OpenGL ES support on both platforms. On android binding native graphics Java functions to your C++ code would give you horrendous results. I think your problem can be solved with a little help from a friend... I would safely bet cocos2d-x does what you need. It is mainly ...


3

Back in the days of C and no templates... we had void pointers. Good thing they weren't made obsolete, because you have just seen that templates aren't omnipotent gods. This situation is perfect for some void pointer hacks. Here, I tried to make an example that was as simple as possible: Edit: The example below makes a dangerous use of shared_ptr, please ...


3

The standard library header chrono (available since C++11) provides various clock and duration types you can use to compute time intervals (in theory -- see my footnote and the comments). You probably want steady_clock. Your periodic update method would look something like: auto currentTime = std::chrono::steady_clock::now(); auto elapsed = previousTime - ...


3

Going with a pure standard library approach, shared_ptr is the best tool you have for resources. Yes, you want to share resources between all the objects that use them. I personally dislike shared_ptr and generally use a custom ref-counted handle for cases where I really need them, such as with resource handles. There's various features that are really ...


3

You close your font with TTF_CloseFont at the end of your constructor. Remove that, and then the rendering should work. You want to move the closing code to the destructor. Also, you release your m_surface first at the end of the Text-classes constructor, and then you do that again in the beginning of Update. You're essentially trying to delete already ...


2

Since the customary language for ios is objective-c and android is java, you should add to your question that you need a kit that also supports C++ across those platforms. http://www.madewithmarmalade.com/ is one, it's probably stronger than what you're looking for and it's not free... but there is a trial version. I have not tried it personally, your ...


2

You could go about this in different ways. Usually, you have a model's geometry centered around the origin (0,0,0), and to place it somewhere in the world you would use a world matrix, which offsets the entire model to a desired new position. This world matrix can reside in a constant buffer accessible to the vertex shader, where it would be combined with ...


2

I do not understand the problem. If you do not call the join(), they won't be waited for. You should call the join() only right before you absolutely need the data they produce. The other threads wont be stopped if you call join() on one of them. See the join() reference: http://www.cplusplus.com/reference/thread/thread/join/ VAOs are not shared in between ...


2

First off all To solve your problem, -if that was the only one-, unlike java, C++(MSVC) assigns 0xcccc for uninitialized variables in debug mode, you are not initializing it in the constructor, make sure to initialize all the variables in your class using the initialize list instead of assignment in the constructor, this has two advantage: It has to do ...


2

This is not exactly an answer - rather a suggestion. A lot of game developers are shifting from LUA (it is slow and it forces you to push data back and forth). For example Star Citizen is using Runtime compiled C++ (actually they use Kythera which runs RCC++). RCC++ lets you script in C++ and reload your modified code on the fly, which - I guess - would ...



Only top voted, non community-wiki answers of a minimum length are eligible