Module with useful variables and types. Variables: time, delta, platform Constants: black, white, red, green, blue, yellow, magenta, cyan.

Tophat type aliases

type fu* = real32
// standard type for integer values
type iu* = int32
// standard type for unsigned values
type uu* = uint32

standard type for real values

enum ErrCode

type ErrCode* = enum (int32) {
	ok = 0
	failure = 1
	io
	bad_enum
	bad_action
	bad_input
	alloc
	already
	out_of_bounds
}

struct Vf2

type Vf2* = struct {
	x, y: fu
}

vector 2

fn mkVf2

fn mkVf2*(x: fu = 0, y: fu = 0): Vf2 {
	return Vf2{x, y}
}

Vf2 constructor

fn Vf2.rotated

fn (p: ^Vf2) rotated*(origin: Vf2, rot: fu): Vf2 {

rotates p around origin with rot in degrees

fn Vf2.distanceTo

fn (p1: ^Vf2) distanceTo*(p2: Vf2): fu {

distance between p1 and p2

fn Vf2.angleTo

fn (p1: ^Vf2) angleTo*(p2: Vf2): real {

Angle between p1 and p2

fn Vf2.abs

fn (p: ^Vf2) abs*(): Vf2 {

Absolute value of a vector.

fn Vf2.round

fn (p: ^Vf2) round*(): Vf2 {

Rounds a vector.

fn Vf2.trunc

fn (p: ^Vf2) trunc*(): Vf2 {

Truncates a vector.

fn Vf2.floor

fn (p: ^Vf2) floor*(): Vf2 {

Floors a vector.

fn Vf2.ceil

fn (p: ^Vf2) ceil*(): Vf2 {

Ceils a vector.

fn vf2f

fn vf2f*(f: fu): Vf2 {

Creates a vector with both x and y set to f

fn Vf2.sub

fn (p: ^Vf2) sub*(p2: Vf2): Vf2 {

Subtracts a vector from another one.

fn Vf2.subf

fn (p: ^Vf2) subf*(f: fu): Vf2 {

Subtracts a fu from a vector.

fn Vf2.add

fn (p: ^Vf2) add*(p2: Vf2): Vf2 {

Adds a vector to another one.

fn Vf2.addf

fn (p: ^Vf2) addf*(f: fu): Vf2 {

Adds a fu to a vector.

fn Vf2.div

fn (p: ^Vf2) div*(p2: Vf2): Vf2 {

Divides a vector by another one.

fn Vf2.divf

fn (p: ^Vf2) divf*(f: fu): Vf2 {

Divides a vector by a fu.

fn Vf2.mul

fn (p: ^Vf2) mul*(p2: Vf2): Vf2 {

Multiplies a vector by another one.

fn Vf2.mulf

fn (p: ^Vf2) mulf*(f: fu): Vf2 {

Multiplies a vector by a fu.

fn Vf2.mag

fn (p: ^Vf2) mag*(): fu {

Returns the magnitude of a vector p.

fn Vf2.norm

fn (p: ^Vf2) norm*(): Vf2 {

Normalizes a vector.

fn Vf2.dot

fn (p: ^Vf2) dot*(q: Vf2): fu {

Calculates dot product between 2 vectors.

struct Transform

type Transform* = struct {
	p: Vf2 // position
	s: Vf2 // scale
	o: Vf2 // origin
	r: fu  // rotation
}

Struct defining transformation. Used for example by entities.

fn mkTransform

fn mkTransform*(p: Vf2, s: Vf2 = Vf2{1, 1}, o: Vf2 = Vf2{0, 0}, r: fu = 0.0): Transform {

Transform constructor

fn Transform.transformed

fn (o: ^Transform) transformed*(t: Transform): Transform {

Transforms a transform with another transform.

fn Vf2.transformed

fn (v: ^Vf2) transformed*(t: Transform): Vf2 {

Transforms a vf2 to another vf2. Order:

  1. scale
  2. rotation
  3. position

This allows conversion from a relative to an absolute vf2.

type Quad

type Quad* = [4]Vf2

fn Quad.transformed

fn (q: ^Quad) transformed*(t: Transform): Quad {

Transforms a quad with a transform.

fn Quad.getMax

fn (q: ^Quad) getMax*(): Vf2 {

Gets the maximum coordinate.

fn Quad.getMin

fn (q: ^Quad) getMin*(): Vf2 {

Gets the minimum coordinate.

fn Quad.getDims

fn (q: ^Quad) getDims*(): Vf2 {

Returns the dimensions of the quad's bounding box

fn getGlobal

fn getGlobal*(): ^struct{} {

returns a pointer to the th_global. Set this as your extensions thg.

fn getFuncs

fn getFuncs*(): ^struct{} {

returns pointer to tophat functions. Pass this to th_ext_set.

var enableErrrors

var enableErrors*: bool = true

If true, errors will result in a call to exit(255), otherwise printf is used.

Color constants

const (
	black* = 0xff
	white* = 0xffffffff
	red* = 0xff0000ff
	green* = 0x00ff00ff
	blue* = 0x0000ffff
	yellow* = 0xffff00ff
	magenta* = 0xff00ffff
	cyan* = 0x00ffffff
)

enum Platform

type Platform* = enum {
	unknown
	linux
	windows
	macOs
	web
}

Misc variables

var (
	// time in ms from start of the game
	time*: uint
	// length of the last frame in ms
	delta*: int
	// platform tophat is running on
	platform*: Platform
)