Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 27 Nov 2013
Offline Last Active Oct 20 2016 10:14 PM

#5309753 Transitioning game states when using a stack of states

Posted by on 06 September 2016 - 11:51 PM

Hi Kylotan, thanks for your reply. Simple is good for me! I like the idea of each state being able to push and replace states directly...


I am a bit confused when you say pass that in to each GameState when it is created,


So my StateMachine class currently looks as:

class StateMachine

	std::vector<std::shared_ptr<GameState>> m_states;

	void pushState(const std::shared_ptr<GameState> &state);

Do I have this StateMachine in my Game class?


Thanks for your help!

#5309636 Transitioning game states when using a stack of states

Posted by on 06 September 2016 - 04:02 AM

I am trying to implement a game state system in my engine. I have read around a bit and think I am able to tackle a stack of gamestates. In order to prototype I am aiming to have first a splash screen, then a main menu, and then the "playing" state.


I am wondering the best way to transition from one state to another?


Right now, I have a GameState class,

class GameState
    virtual void update(const float deltaTime) = 0;

and my Game class (of which my actual game inherits from), with a vector of states

std::vector<std::shared_ptr<GameState>> m_states;

So right now, I can add the first state (the SplashScreenState, Inherited from GameState) using


and in my game update loop I call

	for (std::vector<std::shared_ptr<GameState>>::reverse_iterator i = m_states.rbegin(); i != m_states.rend(); ++i)

to update each State. So right now my SplashScreenState is getting updated, and a timer transitions it. Once the timer runs out I want the MainMenuState to activate.


The question is, given my current design, how do I best go about this?

Should I have something inside SplashScreenState::update() that lets me push on the next state directly?

Should My GameState class handle all transitions internally?

Where do I go about actually explicitly stating which state I want to go from where and to (Splash Screen to Main Menu, Main Menu to Playing, Playing to Paused...).


Thanks for any insight.

#5308788 Getting debug information using TextOut()

Posted by on 31 August 2016 - 12:31 AM

What value are you returning after handling WM_PAINT? It should be zero; do not pass it on to DefWindowProc, if you draw things yourself.

The flickering could well be WM_ERASEBKGND. If you do all your drawing in WM_PAINT, you can handle WM_ERASEBKGND doing nothing and return a non zero value.

Remember: The return value of a handled message is important. And, if not told otherwise in the messages docs, pass the message on to DefWindowProc. A good WindowProc looks like this:

switch ( MessageType )
  case WM_xxx:
    // do stuff
    return or break, depending on what the message docs say
  case WM_other_xxx:
    // do other stuff
    return or break, depending on what the message docs say
return DefWindowProc( arguments );


Hi Endurion, thanks for your reply. Sorry I should have showed my whole WindowProc:


    switch (uMsg)
    case WM_DESTROY:
        return 0;

    case WM_PAINT:
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        //Textout output
        wchar_t text[] = L"tess test test";

        TextOut(hdc, 1, 1, text, ARRAYSIZE(text));
        //Textout output end

        //FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

        EndPaint(hwnd, &ps);
    return 0;

    return DefWindowProc(hwnd, uMsg, wParam, lParam);


Once I include my graphics class, DirectX will do the drawing, but for now I just want to work with testing values. I cant quite work how how to use WM_ERASEBKGND to avoid flickering? I thought the false flag in InvalidateRect should do this?


#5308774 Getting debug information using TextOut()

Posted by on 30 August 2016 - 09:46 PM

You can use OutputDebugString() which will print to your console window in the debugger if you are using Visual Studio or you can see this by using DebugView without a debugger.

When dealing with GDI as TextOut is you need a WM_PAINT or similar message to update the screen in.


Hi, thanks for your response. I am a bit confused first of all. I don't seem to have a 'console' window, just the win32 window that the demo creates, so I dont know where this output goes? I will have a look into DebugView.



It's been quite awhile since I've written a Win32 game but if I'm not mistaken, calling InvalidateRect() causes a WM_Paint messages to be sent. Setup a timer to periodically call InvalidateRect() and you should see your text output update. You'll probably want to store that hwnd value from when you create the window so that you'll have access to it for the first parameter. You can use a NULL for the rect parameter to paint the entire window's contents and then it's just whether you want to erase the current contents of the window or not.


Thank you, I have kind of got it to work. I am seeing the debug text flicker on the screen, as if it is getting overwritten with a blank white fill or something... I am not sure why. Win32 stuff is so confusing...


My message loop now looks like this

    while (msg.message != WM_QUIT)
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            InvalidateRect(hwnd, NULL, false);


with WM paint


    case WM_PAINT:
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        //Textout output
        wchar_t text[] = L"tess test test";
        TextOut(hdc, 1, 1, text, ARRAYSIZE(text));
        //Textout output end

        //FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

        EndPaint(hwnd, &ps);


When FillRect is not commented out the screen stays white. Thanks for all your help guys.



#5308586 Getting debug information using TextOut()

Posted by on 29 August 2016 - 09:15 PM

Hey all,


I am trying to get some debug information using Microsoft inbuilt text display e.g. TextOut(). I am trying to make a very simple base game engine (for learning purposes) and before adding its graphics class I want to test a game state system and some other things.


My game uses this demo to get the win32 window up




and after ShowWindow(hwnd, nCmdShow);, I create my game Game myGame; and then call its init() function myGame.init().

In the message loop I peek messages and if no messages are to be dispatched I run game.run().


This is where I get confused: I want to write out to the things like e.g. the current game state (I am trying for a 1 second splash screen, then main menu). I can see it is working when I debug and look at my m_state variable, but I want to see it change in real time on the window, so I thought i could use TextOut. The problem is I seem to need all of the information from WM_PAINT to use textout (like the PAINTSTRUCT), but my game does not hit WM_PAINT...


Am I going about this the right way?


Thanks for all your help






#5284002 Gamestate and Intro, Main Menu, Playing state

Posted by on 29 March 2016 - 12:47 AM

Hi All,


I have added a game state FSM based on the 'Miner' Demo of Matt Bucklands 'Programming Game AI by Example'.


I would like some advice on how to link the Intro Splash Screen, Main menu (which just will have Play game, Settings, Quit), and then the actual Game with it. I am working with what will hopefully be a component based engine .


In my game::init() function I have




as I want the program to first display a quick logo.


game::update() function I will have


GameState state(m_gameState.getGameState());

if (state == GS_Playing)
        // update game specifc systems here


First thing, how should I handle things like the splash screen and main menu? When should they get loaded / rendered? Should there be a specific system for them (MenuSystem)? Do they need to be updated in the game::update() loop, or just the game::render() loop?


I would love any advice on how to handle this, or links to a sample project that show this.



#5271487 How to actually measure stuff like cache hit, prefetch, ram traversal?

Posted by on 16 January 2016 - 07:53 PM

Hi Guys,


This question is mostly out of pure interest.


Reading a lot of topics on this forum about game engine design and code, I often come across posts that say something like "this way you are more likely to load the next bit of data into the memory that the cpu will need, increasing cache coherency...", usually in topics about data oriented design (DOD).


From my understanding, DOD is something to do with stuff being in contiguous memory that the cpu cycles over each game loop, and it seems easier to achieve this using DOD rather than inheritance?


Secondly... how can you actually measure this stuff? I know in a very very simply game engine it doesn't really matter, but I have a learning game written using both ECS and just OOP that essentially does the same thing. (sprite moving around, shoots, gets shot at by enemies). How can I compare which implementation is actually better, and what areas of each one are working well?


I hope my question makes sense.



#5251322 Examples of win32 Gui code?

Posted by on 09 September 2015 - 06:58 AM

Thanks guys, some great links here to get me started.



Win32 is old. It's expanded here and there, but alot of the "old" stuff is still how it's done. It's not like DirectX where Microsoft discards the API and gets to redesign it from the ground up - older applications must still work on newer systems, so the "old" Win32 is still Win32.


Were you wanting to learn the newer Microsoft APIs? You probably want WinRT or .NET then.


Are you just wanting to make desktop applications? I'm a huge fan of Qt (which is free even for closed-source commercial use if you link by DLL; don't get mislead by the website).

Qt provides cross-platform native-interface GUIs, and is very powerful, and also has fantastic documentation, a large community, and plenty of open-source example applications. It's best used with the QtCreator IDE, though.


I didn't know that about WinRT or .NET, I will look into them, thanks.


I am looking to make a desktop gui for myself and maybe a few others, for data processing. I have heard Qt is the way to go for making Win32 apps so I will probably play around a bit with the API to get a general feel for things then dive into Qt.


Quick question, I am getting a bit confused about all the terminology they are throwing around. In terms of the Win32 API, which is pretty much raw C and C++ (isint it?), where does WinRT and .NET fit into things?


I find examples of things I have used can help me understand. When making a game using D3D11 I used the DirectX tool kit, and SFML. Are WinRT and .NET anything like them?



#5249018 Programming scientific GUI's, data and gui layout?

Posted by on 26 August 2015 - 10:46 AM

Hi Guys,


Thanks for all the input, some very sound advice here I am learning a lot. I think the biggest problem is as people have said, design patterns are more of a guideline, and there are no concrete 'correct' ways that can cover all the different combinations of data processing that may be undertaken.


Find a Gui toolkit for your programming language, and do the tutorial, make a button, print 'hello' when you push it, draw lines at the canvas, that sort of things. It should give you enough ideas of how to scale up to a more complete gui.
If you use C++ and don't know what Gui library to use, I'd recommend Qt. They have great tutorials and good documentation.


I am using MATLAB and quite comfortable with it and programming a GUI using OOP with it. I find reading about patterns like MVC is all well and good but without a proper example or implementation I really have no idea how to apply them to my own problems. I will give an example of the kind of things I am stuck with though. I attach a screenshot of my GUI as it stands now:


So the first thing the user does is press 'Add raw data', this opens a window where they can chose 1 or multiple data files. For the purposes of this example, each data file could be an .asc file, where each .asc file has a series of x and y data points. The file names are populated in the tab 'Raw Data', and the selected file name is plotted in the graph tab 'View 1'.


The user can select 1 or multiple files in the 'Raw Data' tab and click 'Double data values' to double the y values of the data points. The files that were doubled go into the tab 'Doubled Data', and when they are clicked on, will be displayed in 'View 1'.


The purpose of the View tabs could be different ways to show the data, such as line plot, scatter plot, bar chart...


My questions:


What exactly is the MVC setup for this?


Should 'Views' depend on the List boxes, or should the List boxes depends on the 'Views'. What I mean by this is when a data set is clicked on in 'Raw Data' should it check the View tabs with a switch statement to see which to display? And if I then click on a different 'View' should it query the List Box to see which file is highlighted, and then display that? But it would need to query both the 'Raw Data' and 'Doubled Data' List boxes, to first see which is 'Active' and then select the right file...? Basically there are so many combinations of things that I am struggling to see how best to have them communicate, and this is a tiny basic GUI...


Or am I linking the GUI elements with the data too tightly?


I have more people I need to quote and ask things but do not have time for now. I will get back to it. Thanks everyone for your input.

Attached Thumbnails

  • gui.png

#5243333 First game AI pathfinding

Posted by on 29 July 2015 - 12:07 AM

@ferrous Thanks, I would like to code a variety of solutions for learning purposes, and then implement the one I find is the best into my game. I have heard that steering behaviour can sometimes be used instead of pathfinding. I am probably overshooting with ambition here but eventaully when a few enemy ships are around I would like them to also be able to not run into eachother.. but first things first.


@xoxos I like the idea of temporal behaviour switching, that would be easy to code and could look quite cool. I guess it could mimic the 'randomness' of humans. E.g. fly towards player for 1-3 seconds, then shift to the left then back off.


@braindigitalis Thanks for the input. I am quite confident with maths (use MATLAB a lot for work), so not worried about that aspect of it. I am using C++ with VS2013. I am using the directX tool kit to do my spirte rendering for me, and entityX ECS framework for game objects that have complex behaviours and / or components (such as ships with different guns / effects).


Thanks all. I will start with some basic implementations and go from there.

#5243212 First game AI pathfinding

Posted by on 28 July 2015 - 11:21 AM



I am a relatively new programmer building a spaceship game and I'm at the point where I would like to add in some (basic) interesting A.I.


Right now there are two AI 'options'. One of them is just moving directly towards the player. The other is shooting at the player. I want to make the movement more interesting (right now it is just in a straight line towards the player.)


I would also like to take this opportunity to learn pathfinding. My idea is to 'draw' some invisible barriers that the enemy ships cannot pass (walls if this would be a ground based game). The enemies would have to strafe around these barriers to get a clear 'shot' on the player. Of course the barriers are not actually visible in the game world, so I think it would make for a cool effect. Attached is an illustration of the idea.



In pic 2, the red barrier (could be another map layer that is 'collidable') blocks the line of sight from the ship, so it has to scoot around the side to get a clear shot.


My questions:

1. What terms should I be looking up / reading that are relevant to this cause?  I have read about A star pathfinding… is that applicable in this situation? Remember I don’t want the ship to literally hug the wall, but a ‘smooth’ strafe around the side of it. (Starcraft 2 style… I wish)


2. How does pathfinding and AI interact? Who is the ‘boss’ so to speak. Is it pathfinding that tells AI “go here now, go here now” or is it AI that asks pathfinding “Where do I go, is this clear, can I shoot now?” What is the order in the update loop?


3. Is this kind of situation a constant (per update loop) “polling” type situation (like controller input), or an event type situation (like perhaps two objects colliding), or a combination of both.


4. I have read that things in game programming should be decoupled as much as possible, is that also relevant in this case?


Thanks for all and any assistance.

#5226921 Adding non ECS features in an ECS engine (tilemap)?

Posted by on 03 May 2015 - 02:35 AM

Why do you need to use ECS to accomplish your goal (a game)?

Why does every class member start with “m_” except the “graphics” class member?  What kind of sloppiness is this?



L. Spiro



I love your posts Spiro. They are always incredibly helpful, but also strike the fear of god into me...


Why do you need to use ECS to accomplish your goal (a game)?


To be honest I am not good enough at C++ to know what I do or do not need... but when I was working with my original OOP game and wanted to start adding in enemies with different guns and shields and all sorts of fancy ideas in my head, posts that I found (not just on gamedev) kept saying ECS this ECS this ECS this so I wanted to see what the fuss is about and give it a go.


Why does every class member start with “m_” except the “graphics” class member?  What kind of sloppiness is this?


Well spotted, I did not even realize that lol.. I do not really want to use the m_ prefix at all. But I found when I was initializing objects in their constructor using initialized lists, I kept getting confused about the private member I was trying to initialize, and the passed in variable or object I was initializing it with... so I say in my head (m_ goes Outside the brackets...)


#5226452 What is this stuff called in c++

Posted by on 30 April 2015 - 02:24 AM



Yes it is similar to that. Its going to take me quite some time to understand what is going on there though. I need really basic examples that literally just do operations on numbers to make the link to an actual engines implementation.




Thanks a lot for your comprehensive explanation. Definitely clears a lot up for me better than what readings I could find on the net. I understand only what of templates was covered in the C++ book I read (Starting Out with C++ from Control Structures to Objects). I will revise that chapter to clue up on them again since the key things here seem to be templates.


Unless I'm mistaken, EntityX uses the CRTP in this case to generate a unique TypeID for the derived system. This is possible since by inheriting from Receiver<Game>, the compiler generates a seperate template instantiation. Somewhere inside the code, there is a static variable that is incremented every time a certain GetID()-function is called, but only once for every different instantation. Looks like this implementationwise:


Yes it does seem to be doing something like that when I step into the code. Now for once I actually have a clue as to what use the CRTP is.

Reading through the rest of your post while looking at the source code gives me a lot of help so thanks again.




That's the kind of key words I am looking for. Thanks.




It is primarily the strange looking code that I have not seen in my book before, combined with not knowing what this stuff is CALLED that I was struggling with. Your psot gave me a lot of what I needed too.


Thanks everyone I have learnt a lot, and have a lot of reading to do now on these new keywords.

#5225485 Dx11, directxmath and d3dx

Posted by on 25 April 2015 - 11:57 AM

And if you're going to use directXTK, you can also use SimpleMath which wraps DirectXMath



#5222013 How to pass DeviceContext to other classes?

Posted by on 08 April 2015 - 01:26 AM

I asked a very similar question here: https://directxtk.codeplex.com/discussions/611607


Basically when you create your device and deviceContext, these initialized objects "live" in the private member section of your game class as you wrote. Now you want to load a texture, and you see you need the device and/or deviceContext for that, so you need to have a place for them in your MyTexture class. You can either just create temporary objects of the device and device context in the constructor as Josh suggested, or store them in your MyTexture as private members.