8000 GitHub - dvoyni/cog: Modular game/app engine written in go. Councurrent. Performant. Cross-platrom. · GitHub
[go: up one dir, main page]

Skip to content
dvoyniPublic

About

Modular game/app engine written in go. Councurrent. Performant. Cross-platrom.

Resources

Code of conduct

Contributing

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

Cog

Cog is a Go engine for games, apps and backends, built for performance from the ground up. One core runs a game client, its dedicated server, or a plain service: you pick the plugins, and Cog runs them.

Why Cog

  • Highly concurrent. Work runs as jobs on a pool of workers that keeps every core busy, and jobs wait on each other only through the dependencies you declare. Nothing is serialized by default.
  • Automatic resource locking. A task declares the state it reads and writes once, at registration, and the scheduler does the rest: readers run side by side, a writer runs alone, and no job ever touches state it didn't lock. No mutexes in your code and no data races.
  • Alloc-free on the hot path. Once running, publishing events, scheduling jobs and running them allocate nothing, and the tests check it. Data in flight is pointer-free, so the garbage collector has nothing to scan and frames stay smooth.
  • Modular. Everything is a plugin: the ECS, pacing and your own game code alike, and a renderer or a network layer plugs in the same way. A server spawns only the plugins it needs.
  • Extensible. Plugins meet through resources and events, never through each other. Swap a backend by installing a different implementation of the same interface.
  • Deterministic when you need it. Fixed-step simulation, replaceable time and a manual clock make tests and replays reproducible.
  • Performance oriented throughout. A cache-friendly entity component system, pinned main-thread work for windows and GPUs, and benchmarks that guard the numbers.
  • AI ready. Your agent knows how to work with Cog, because what it needs is in the repository beside the code: rules in AGENT-RULES.md, each part's terms in CONTEXT.md, guides with examples, and designs that say why. It can check its work too: tests run in a real kernel with no window or GPU, and a drawn frame can be captured as a picture or inspected as data. And a game built on Cog has an MCP server out of the box: list the mcp plugin, and an agent attaches to the running game over the Model Context Protocol, to look at it and drive it natively.

Cog is written in Go 1.27, and its core depends only on the standard library. Some plugins bring dependencies of their own, such as a graphics backend, and only an application that uses them pulls them in.

Hello, world

Install Go 1.27 or later, and start a project:

mkdir hello
cd hello
go mod init hello

Save this as main.go:

package main

import (
	"log"
	"runtime"

	"github.com/dvoyni/cog/kernel"
	"github.com/dvoyni/cog/plugins/canvas"
	"github.com/dvoyni/cog/plugins/gfx"
	"github.com/dvoyni/cog/plugins/gogpu"
	"github.com/dvoyni/cog/plugins/input"
	"github.com/dvoyni/cog/plugins/metronome"
	"github.com/dvoyni/cog/plugins/platform"
	"github.com/dvoyni/cog/plugins/render"
)

// hello is the application's own plugin.
type hello struct{ supervisor *kernel.Supervisor }

func (*hello) Name() kernel.PluginName { return "hello" }

func (*hello) Dependencies() []kernel.PluginName {
	return []kernel.PluginName{platform.PluginName, canvas.PluginName}
}

func (h *hello) Init(configurator *kernel.Configurator, _ any) error {
	// Each frame, draw once canvas is ready for it.
	draw := configurator.RegisterTask1(draw)
	configurator.Subscribe(func(k *kernel.Kernel, ex *kernel.Executor, in kernel.JobHandle, event canvas.FrameEvent) (canvas.FrameEvent, kernel.JobHandle) {
		return event, ex.Join(in, ex.Schedule(draw, kernel.NoArg{}, event.Ready).Handle())
	})
	// Read the window's events each frame, and quit when it is closed.
	tasks, _ := configurator.Resource[platform.Tasks]()
	processEvents := tasks.ProcessEvents
	configurator.Subscribe(func(k *kernel.Kernel, ex *kernel.Executor, in kernel.JobHandle, beat gfx.RenderEvent) (gfx.RenderEvent, kernel.JobHandle) {
		return beat, ex.Join(in, ex.Schedule(processEvents, kernel.NoArg{}).Handle())
	})
	configurator.Subscribe(func(k *kernel.Kernel, ex *kernel.Executor, in kernel.JobHandle, event platform.CloseRequestedEvent) (platform.CloseRequestedEvent, kernel.JobHandle) {
		h.supervisor.Quit()
		return event, in
	})
	return nil
}

func (*hello) Start(*kernel.Kernel) error { return nil }
func (*hello) Stop(*kernel.Kernel) error  { return nil }
func (*hello) Destroy() error             { return nil }

// draw is a task: taking the command list says what it locks.
func draw(k *kernel.Kernel, _ kernel.NoArg, list canvas.CommandList) kernel.NoResult {
	list.DrawText(canvas.Text{String: "Hello, world!", Size: 64, Pivot: canvas.PivotCenter})
	return kernel.NoResult{}
}

func init() { runtime.LockOSThread() } // windows live on the main thread

func main() {
	supervisor := kernel.NewSupervisor(nil)
	config := map[kernel.PluginName]any{
		metronome.PluginName: &metronome.Config{Tracks: []metronome.Track{
			metronome.NewTrack[gfx.RenderEvent]("render", metronome.Variable()),
		}},
		gogpu.PluginName: &gogpu.Config{
			MainWindow: platform.WindowEntry{Title: "Hello", Size: platform.Size{Width: 800, Height: 450}},
		},
	}
	if _, err := supervisor.Spawn(config, runtime.GOMAXPROCS(0),
		metronome.NewPlugin(), platform.NewPlugin(), input.NewPlugin(), gfx.NewPlugin(),
		gogpu.NewPlugin(), render.NewPlugin(), canvas.NewPlugin(),
		&hello{supervisor},
	); err !=
77E4
 nil {
		log.Fatal(err)
	}
	for supervisor.WaitMainThread() {
		supervisor.TickMainThread()
	}
}

Add Cog and run it:

go get github.com/dvoyni/cog@latest
go mod tidy
go run .

A window opens and says hello. Each frame is an event: hello subscribes to it and schedules draw, a task that takes canvas's command list and so is run with the locks it needs.

What's next

Start with your agent. Open your project in it and ask for what you want to build. Cog's docs come with the module, so put this in the project's AGENTS.md first:

This project is built on Cog. Its docs are in its module's directory, which
`go list -m -f "{{.Dir}}" github.com/dvoyni/cog` prints. Before any work, read
there CONTEXT-MAP.md, kernel/docs/README.md, and the README.md of each plugin
you use, plugins/<plugin>/README.md, with the guide and design it links to.

Or read on yourself. Start with the kernel guide: plugins, resources, tasks, events and jobs. Then take the plugins you need:

Plugin What it gives you Read
metronome Fixed-step and variable pacing overview, guide
ecs Entities, components and systems overview, guide
mux A game's true state, and what is shown catching up with it overview, guide
platform Windows and filesystems overview, guide
input Keyboard, mouse and gamepads overview, guide
gamepad The machine's gamepads, for input overview
gfx The GPU, as a thin mirror of wgpu overview, guide
gogpu The real GPU, windows, keyboard and mouse overview, guide
render Meshes, materials and passes overview, guide
canvas 2D: sprites, shapes, text and meshes overview, guide
ui A game's interface overview, guide
sfx Sound: clips, voices, buses and a listener overview, guide
otosound Sound on desktop, for sfx overview
storefront Achievements, stats and the player overview
storefrontsteam Steam, for storefront overview
mcp An agent attached to the running game overview

CONTEXT-MAP.md shows how they relate, and CONTRIBUTING.md how to help. Cog is under the MIT licence.

About

Modular game/app engine written in go. Councurrent. Performant. Cross-platrom.

Resources

Code of conduct

Contributing

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0