-4

I am building a two-player(CPU-User) Poker Application with C++ and OpenGL. Currently the application just renders the graphics, keeps track of which button is pressed, value entered in textbox something like below.

while(true){
    button.draw();
    textbox.draw();
    cards.draw();//draws cards based on the Mesh index, hard-coded atm
}

I have separately written the game logic which handles hand ranking, turn-by-turn play , settlement of the pot etc.,

How should this interaction happen between the rendering and game logic happen?

The rendering constantly draws button hover, textbox content etc as this is OpenGL and polls for events, whereas the game logic is awaiting an input to proceed forward.

I am asking asking how the program should be structured that facilitates this and this is the focus of my question.

  • Normally you redraw the screen at a constant rate. Use `if` to skip unnecessary actions on each iteration. – HolyBlackCat Aug 26 '23 at 03:44
  • "I feel like I should use separate threads for logic and rendering" - No need for threads. As a clever guy once said; "Threads are for people who don't know how to implement state machines" ;) – Jesper Juhl Aug 26 '23 at 04:11
  • I recommend getting the game logic working on a console based application. Add graphics after the main logic is working. Or you could get the fundamental graphics working first, then add the gaming logic. These are easier steps than trying to get both working at the same time. – Thomas Matthews Aug 26 '23 at 18:43
  • @ThomasMatthews, I have already done that. I render button,textbox, cards etc and poll for events. But when an event occurs, the game logic shouldn't execute from the beginning through a function call, but rather a certain point in the game logic that is waiting for the user input right?. I was wondering if there was a clean way to do this. The game logic awaits an input whereas the graphics part constantly renders things like button hover, textbox content .etc. I am confused as how this interaction takes place. – Incompleteness Aug 27 '23 at 00:23

1 Answers1

2

A typical structure would look like this

while (active) {
    processEvents();
    updateScene();
    renderScene();
    swapBuffers();
}

processEvents:

  • polls events from the graphics environment, like key, button or pointer events
  • sets the game logic states based on the input

updateScene:

  • updates the states of the render engine, based on the game logic states
  • this step can also be done in the step above

renderScene:

  • renders the scene

swapBuffers:

  • swaps the window buffers and presents the (newly) rendered scene

Multi threading is counterproductive here. Every step is based on the previous step, if a previous step didn't changed the state, then the output will be the same (use a framebuffer and render into a texture/renderbuffer, if there was no change, then simply use the previously rendered scene - without rerendering it).

Well, you could also skip the buffer swapping if there was no state change (be prepared for exposure, aka repaint events), or wait until an input event arrived (which would indicate a probable state change).

Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11