Nohiya Dev

Game Loops

Each example opens an 800 × 600 window or canvas. Create your game once, then handle input, update and draw each frame. Add your code at the comments.

2D:

SDL 3:

C:

Install SDL 3 and link against it.

#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

// Define your entity types and functions here.

int main(int argc, char **argv) {
	(void)argc;
	(void)argv;

	SDL_Window *w;
	SDL_Renderer *r;
	SDL_Event e;
	SDL_Init(SDL_INIT_VIDEO);
	SDL_CreateWindowAndRenderer("Game", 800, 600, 0, &w, &r);

	// Create your starting player, enemies and map here — runs once.

	for (;;) {
		while (SDL_PollEvent(&e)) {
			if (e.type == SDL_EVENT_QUIT) {
				SDL_Quit();
				return 0;
			}
			// Handle input events here.
		}

		// Update your game here: movement, collisions, spawning.

		SDL_RenderClear(r);
		// Draw your game here: map, entities, interface.
		SDL_RenderPresent(r);
		SDL_Delay(16);
	}
}

Nim:

Install nim-lang/sdl3 and the SDL 3 library.

import sdl3

# Define your entity types and functions here.

var w: Window
var r: Renderer
var e: Event
discard init(INIT_VIDEO)
discard createWindowAndRenderer("Game", 800, 600, 0, w, r)

# Create your starting player, enemies and map here — runs once.

while true:
  while pollEvent(e):
    if e.type == EVENT_QUIT:
      sdl3.quit()
      system.quit()
    # Handle input events here.

  # Update your game here: movement, collisions, spawning.

  discard renderClear(r)
  # Draw your game here: map, entities, interface.
  discard renderPresent(r)
  delay(16)

PixiJS:

JavaScript:

Use PixiJS 8 in a browser project using a bundler, with pixi.js installed.

import { Application } from "pixi.js";

// Define your entity types and functions here.

const app = new Application();
await app.init({ width: 800, height: 600 });
document.body.appendChild(app.canvas);

// Create your starting player, enemies and map here — runs once.
// Register keyboard and pointer event handlers here.

app.ticker.add(() => {
  // Update your game here: movement, collisions, spawning.
  // Update objects in app.stage; PixiJS draws them automatically.
});

3D:

Sokol:

Sokol runs the loop, calls your input and frame functions, and handles closing the window.

C:

Use the Sokol headers and link one implementation built with SOKOL_IMPL and your chosen graphics backend.

#define SOKOL_IMPL 
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_glue.h"

// Define your entity types and functions here.

static void init(void) {
    sg_setup(&(sg_desc){ .environment = sglue_environment() });
    // Create your starting player, enemies and map here — runs once.
}

static void on_event(const sapp_event *e) {
    (void)e; // Handle input events here.
}

static void frame(void) {
    // Update your game here: movement, collisions, spawning.
    sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain() });
    // Draw your game here: map, entities, interface.
    sg_end_pass();
    sg_commit();
}

static void cleanup(void) {
    sg_shutdown();
}

sapp_desc sokol_main(int argc, char **argv) {
    (void)argc;
    (void)argv;
    return (sapp_desc){
        .init_cb = init, .event_cb = on_event,
        .frame_cb = frame, .cleanup_cb = cleanup,
        .width = 800, .height = 600, .window_title = "Game"
    };
}

Nim:

Install the sokol-nim bindings with nimble install sokol.

import sokol/app as sapp
import sokol/gfx as sg
import sokol/glue as sglue

# Define your entity types and functions here.

proc init() {.cdecl.} =
  sg.setup(sg.Desc(environment: sglue.environment()))
  # Create your starting player, enemies and map here — runs once.

proc onEvent(e: ptr sapp.Event) {.cdecl.} =
  discard e # Handle input events here.

proc frame() {.cdecl.} =
  # Update your game here: movement, collisions, spawning.
  sg.beginPass(sg.Pass(swapchain: sglue.swapchain()))
  # Draw your game here: map, entities, interface.
  sg.endPass()
  sg.commit()

proc cleanup() {.cdecl.} =
  sg.shutdown()

sapp.run(sapp.Desc(
  initCb: init, eventCb: onEvent,
  frameCb: frame, cleanupCb: cleanup,
  width: 800, height: 600, windowTitle: "Game"
))

Three JS:

JavaScript:

Use Three.js in a browser project using a bundler, with three installed.

import * as THREE from "three";

// Define your entity types and functions here.

const renderer = new THREE.WebGLRenderer();
renderer.setSize(800, 600);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, 800 / 600, 0.1, 100);
camera.position.z = 5;

// Create your starting player, enemies and map here — runs once.
// Register keyboard and pointer event handlers here.

renderer.setAnimationLoop(() => {
  // Update your game here: movement, collisions, spawning.
  renderer.render(scene, camera); // Draw your game.
});