This example demonstrates a complete game loop integration combining event-driven architecture with input polling.
- Game Loop Structure: Complete game loop with event processing, update, and render phases
- Event Processing: Processing events at the start of each frame
- Input Polling: Polling input state during the update phase
- Combined Approach: Using both events and polling together
- Frame-Based State: Per-frame input state management with
nextFrame()
cd build
cmake .. -DBUILD_EXAMPLES=ON
cmake --build ../bin/examples/example_05_game_loop_integration[INFO] [vneevents.examples] VneEvents Example: Game Loop Integration
[INFO] [vneevents.examples] ========================================
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] === Game Loop Integration Demonstration ===
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] --- Game Loop Simulation ---
[INFO] [vneevents.examples] Registered event listeners
[INFO] [vneevents.examples] Starting game loop (5 frames)...
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] --- Frame 1 ---
[INFO] [vneevents.examples] [Event] Window resized to: 1920x1080
[INFO] [vneevents.examples] [Update] No movement input
[INFO] [vneevents.examples] [Render] Rendering frame (player at: 0, 0)
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] --- Frame 2 ---
[INFO] [vneevents.examples] [Update] No movement input
[INFO] [vneevents.examples] [Render] Rendering frame (player at: 0, 0)
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] --- Frame 3 ---
[INFO] [vneevents.examples] [Event] Key pressed: 32
[INFO] [vneevents.examples] [Update] Space key just pressed - jump action!
[INFO] [vneevents.examples] [Update] No movement input
[INFO] [vneevents.examples] [Render] Rendering frame (player at: 0, 0)
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] --- Frame 4 ---
[INFO] [vneevents.examples] [Update] No movement input
[INFO] [vneevents.examples] [Render] Rendering frame (player at: 0, 0)
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] --- Frame 5 ---
[INFO] [vneevents.examples] [Event] Window close requested
[INFO] [vneevents.examples] [Update] No movement input
[INFO] [vneevents.examples] [Render] Rendering frame (player at: 0, 0)
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] === Demonstration Complete ===
[INFO] [vneevents.examples]
[INFO] [vneevents.examples] === Done ===
A typical game loop follows this pattern:
while (running) {
// 1. Process events (from platform callbacks)
processEvents();
// 2. Update game state (poll input, update entities)
update(delta_time);
// 3. Render the frame
render();
// 4. Reset per-frame input state
Input::nextFrame();
}- Events are pushed by platform callbacks (GLFW, SDL, etc.)
- Process all events at the start of each frame
- Events handle discrete actions (window close, key press, etc.)
- Poll input state during the update phase
- Use
isKeyPressed()for continuous input (movement) - Use
isKeyJustPressed()for single-press actions (jump, shoot) - Always call
nextFrame()at the end of the frame
- Events: Best for discrete actions (window close, key press events)
- Polling: Best for continuous input (movement, camera control)
- Both: Use events to update input state, poll for game logic
main.cpp: Minimal setup, usesLoggingGuard_Cand callsGameLoopDemo::run()game_loop_demo.h/game_loop_demo.cpp: Helper class encapsulating the game loop logiccommon/logging_guard.h: Shared logging configuration for all examples
In a real game engine:
- Platform Layer: Window callbacks push events and update input state
- Event System: Processes events for UI, window management, etc.
- Game Logic: Polls input state for player movement, camera control
- Render System: Renders the scene based on current game state
This example simulates this architecture without requiring a windowing system.