I am programming the game Battleship in OpenGL(GLUT) and C++.
I know how do to the graphics stuff (like displaying the field and predefined ships),
but where should I put my game code like:
- if key left pressed select one field left and display it
- if a ship is placed then display the ship
Things like place a ship (switching between the fields) I can’t do in the display() function, but if a ship is placed it must be drawn by the display() function, right?
I don’t know where to put the game logic.
What you are looking for is the basic, classic game loop. Effectively, your game runs in one giant loop. In this loop, you handle user input, react to the user input, run AI, and update the video/audio in response. This is, of course, a very simplified take on it, but it will suffice for Battleship.
Conceptually, more or less:
If you’re actually doing real-time as opposed to event-driven, then you need to make your updates take into account the time-passed since the last update, as well as player input. I’ll keep this short and limited to what you’re asking though.