Jump to content
  • Advertisement

Daivuk

Member
  • Content count

    759
  • Joined

  • Last visited

Community Reputation

413 Neutral

About Daivuk

  • Rank
    GDNet+

Personal Information

  • Interests
    Art
    Programming
  1. Daivuk

    Beginner - DirectX and C++

    For inputs you have 2 options: Listen for WM_KEYDOWN and WM_KEYUP events. Use Direct Input. (my recommendation). it is keyboard agnostic so WASD will be mapped to the same keys even on AZERTY keyboards. Google around for Direct Input tutorials.   If you need text input, don't use neither of those. use the WM_CHAR event.
  2. Loading resources or map data asynchronously in games is not an easy task. C++11 makes all of this a lot easier. I am going to demonstrate how to create a thread using async. And then how to sync back to the main loop using lambda functions. Consider the following code, that is blocking: void onNewGame(){ // Load the map loadMap("level1-1.map"); // Start the game startGame();}void loadMap(const std::string& in_filename){ // ... Do extensive work, freezes the screen for couple seconds.}Lets start by adding the loading code into a thread. Using std::async. #include // This is the include for asyncvoid onNewGame(){ // Load the map, asynchronously std::async(loadMap, "level1-1.map"); // return and continue normal execution... render your animated loading screen on your mainloop}void loadMap(const std::string& in_filename){ // ... Do extensive work}That was easy? Now we need to notify the game when the loading is complete. By calling "startGame()" in our example above. This is not as easy as it sound, because the loadMap() function is now running on a different thread. You could set an atomic boolean, and check it every frame. But then we will end up with specific cases all over the place. What we want is to synchronize with our main loop. Consider the following typical mainloop: void mainLoop(){ while (true) { game.update(); game.draw(); }}If we were to set a global atomic bool, we would probably put it like this: void mainLoop(){ while (true) { if (mapLoadingDone) { startGame(); mapLoadingDone = false; } game.update(); game.draw(); }}And mapLoadingDone is a boolean that would be set to true at the end of loadMap(). Lets do this a bit more generic. Let's modify our loadMap() first: void loadMap(const std::string& in_filename){ // ... Do extensive work // Notify that loading is done syncToMainLoop([]() { startGame(); });}Simple function call, with inline code block. This is called a lambda. In previous C++ or C, you would have to define a function and pass it as pointer. Now lets see the implementation of syncToMainLoop. #include #include #include std::queue syncQueue;std::mutex syncMutex;void syncToMainLoop(std::function in_callback){ syncMutex.lock(); syncQueue.push(in_callback); syncMutex.unlock();}The std::function is the new way of doing function pointers in C++. The queue is used to pile up multiple sync request in case multiple happen during a single frame. The mutex is there to prevent the queue from being accessed in multiple places at the same time. Now we just need to add this to the main loop: void mainLoop(){ while (true) { // Do the sync requests syncMutex.lock(); while (!syncQueue.empty()) { auto callback = syncQueue.front(); // Get the next in line callback(); // Call it syncQueue.pop(); // Remove it } syncMutex.unlock(); game.update(); game.draw(); }}Now you can sync back to the main thread during any async loading. Here is the full code of the example: #include #include #include #include std::queue syncQueue;std::mutex syncMutex;void mainLoop(){ while (true) { // Do the sync requests syncMutex.lock(); while (!syncQueue.empty()) { auto callback = syncQueue.front(); // Get the next in line callback(); // Call it syncQueue.pop(); // Remove it } syncMutex.unlock(); game.update(); game.draw(); }}void syncToMainLoop(std::function in_callback){ syncMutex.lock(); ?syncQueue.push(in_callback); syncMutex.unlock();}void onNewGame(){ // Load the map, asynchronously std::async(loadMap, "level1-1.map"); // return and continue normal execution... render your animated loading screen on your mainloop}void loadMap(const std::string& in_filename){ // ... Do extensive work // Notify that loading is done syncToMainLoop([]() { startGame(); });}
  3. Daivuk

    (Super) Smart Pointer

    We call this a "Weak Reference". I don't have examples at hand, but that's the term you want to search for   Edit: What BitMaster said. std::weak_ptr
  4. Exactly, this is the purpose for the original question. It seems that this GL Hint does 'magically' fix it.   I highly doubt OpenGL can fix this also.   The hint is called "PERSPECTIVE". Which means it doesn't work for 2D. It needs the z (depth) value to calculate this correctly. In 2D no such things. The GPU can not know the context of that triangle in a 2d environement. And doesn't understand it is a quad at low level.   If all 4 points have the same z value in your 2D render (let say, 0), and GL gives you the right picture, then the driver or opengl is wrong. What about I want to skew a rectangle on the side?
  5. Daivuk

    A good engine for GameDev...

    If you don't want to do a lot of coding, like you say. And you already know C#. Then Unity3D is your choice.   XNA is certainly enough to make interesting games. Tons of games on Steam use it. And I did pretty amazing stuff with it.   I'm personally not a big fan of "Engines" per say. I prefer having a set of libraries that do certain jobs for you: Load images, draw fonts, do inputs, etc. Like SDL or SFML. Then you build the "game engine" around it.
  6. Daivuk

    Questions for all programmers.

    1) What was the first programming language you studied? QBasic in DOS   2) Did you have any Computer Science background before your first language (ie: boolean algebra, memory organisation, algorithms)? No   3) The first language you studied was it self-taught, formal instruction, or both? Self-taught   4) Was the Computer-Science background self-taught, formal instruction, or both? Self-taught   5) When you started to study Computer Science did it help your understanding of the language you first learned? I never studied Computer Science. But my game programming courses didn't help much as I had already learned everything by myself. But my background helped me get good grades.   6) What kind of environment did you first program in (ie: the IDE or text editor, and the OS)? QBasic had it's own text editor in DOS
  7. As a concrete example of why they are bad:   I am using Cocos2dX for a project. And we had to send network call. Great, cocosx provides a client HTTP built on top of Curl. It's a singleton class (Which means a global var basically) with most of its variable static or globally declared in the CPP. It didn't cause problem at first. Everything is queued when I make multiple call. Ok, perfect.   But then, we started to want to send some calls simultaneously. LIke uploading a file in the background, but continuing navigating in the app.   But we can't, because there is only 1 instance of the HTTP client! And we can't instantiate another one, because everything is static in it. And they even had global vars in the CPP!! A design that sounded good at first ended up a nightmare. I was able to remove the static vars and globals. I can now create multiple instance of the class.   It's a good example I believe. Also if your game end up supporting multiplayer or split-screen. If all you have are static, you will be in hell. (Been there also)
  8. Daivuk

    Managers, which pattern should I use

    Singleton pattern is the most commonly used for managers. Just careful to not put managers as singleton that don't need to be.   One day you will want to add multiplayer, or split screen, to your game. And all those singleton will haunt you. Some things, like Texture manager, should definitely be in a singleton. It's sharing textures across all part of the engine.   But input manager, maybe not. If you add a second controller for player 2, you might want him to handle his own input separately, and you will want a second instance of input manager.   What I do in my games generally, is create a global singleton, where I put all those managers in. Globals::getInstance()->textureManager()->getTexture("baby.png"); Globals::getInstance()->inputManagers[PlayerId]->isButtonDown('A');   Not the best practice, but accessing things easily from everywhere is nice too. Also I cant keep all my other managers singleton-free.
  9. You are going to have to create a font, load it, display it.   Take a look at bmfont: http://www.angelcode.com/products/bmfont/ Some tutorials are included on the site.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!