hlg

package module
v0.0.0-...-8789af9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2024 License: MIT Imports: 9 Imported by: 0

README

hlg (High Level Graphics)

This project is a WIP. The goal is to make a high level graphics api for golang.

Go Reference Documentation: https://dfirebaugh.github.io/hlg/

Examples

check the ./examples dir for some basic examples

Triangle
package main

import (
	"github.com/dfirebaugh/hlg"
	"golang.org/x/image/colornames"
)

var triangle hlg.Shape

// update operation need to happen less frequently than render operations
func update() {
}

func render() {
	hlg.Clear(colornames.Skyblue)
	triangle.Render()
}

func main() {
	hlg.SetWindowSize(720, 480)
	hlg.SetScreenSize(240, 160)
	triangle = hlg.Triangle(0, 160, 120, 0, 240, 160, colornames.Orangered)

	hlg.Run(update, render)
}

triangle_example

Colored Triangle
package main

import (
	"github.com/dfirebaugh/hlg"
	"golang.org/x/image/colornames"
)

var triangle hlg.Shape

const (
	screenWidth  = 240
	screenHeight = 160
)

// update operations happen less frequently than render operations
func update() {
}

func render() {
	hlg.Clear(colornames.Skyblue)
	triangle.Render()
}

func main() {
	hlg.SetWindowSize(screenWidth, screenHeight)
	hlg.SetTitle("color triangle")

	triangle = hlg.PolygonFromVertices(0, 0, 0, []hlg.Vertex{
		{
			Position: [3]float32{0, screenHeight, 0},
			Color:    colornames.Red,
		},
		{
			Position: [3]float32{screenWidth / 2, 0, 0},
			Color:    colornames.Green,
		},
		{
			Position: [3]float32{screenWidth, screenHeight, 0},
			Color:    colornames.Blue,
		},
	})

	hlg.Run(update, render)
}

color_triangle_example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear(c color.RGBA)

Clear clears the screen with the specified color.

func Close

func Close()

func CompileShader

func CompileShader(shaderCode string) int

CompileShader takes wgsl shader code as an argument and returns a hash that can be used internally to reference a shader

func CreateRenderQueue

func CreateRenderQueue() graphics.RenderQueue

func DisableFPS

func DisableFPS()

DisableFPS disables the FPS counter in the window title.

func DisableWindowResize

func DisableWindowResize()

func EnableFPS

func EnableFPS()

EnableFPS enables the FPS counter in the window title.

func GetCursorPosition

func GetCursorPosition() (int, int)

func GetFPS

func GetFPS() float64

func GetScreenSize

func GetScreenSize() (int, int)

func GetWindowHeight

func GetWindowHeight() int

func GetWindowPosition

func GetWindowPosition() (int, int)

func GetWindowSize

func GetWindowSize() (int, int)

GetWindowSize retrieves the current window size.

func GetWindowWidth

func GetWindowWidth() int

func IsButtonJustPressed

func IsButtonJustPressed(buttonCode input.MouseButton) bool

IsButtonJustPressed checks if a mouse button was just pressed

func IsButtonPressed

func IsButtonPressed(buttonCode input.MouseButton) bool

func IsKeyJustPressed

func IsKeyJustPressed(keyCode input.Key) bool

func IsKeyPressed

func IsKeyPressed(keyCode input.Key) bool

IsKeyPressed checks if a key is currently pressed

func PressButton

func PressButton(buttonCode input.MouseButton)

PressButton simulates a mouse button press

func PressKey

func PressKey(keyCode input.Key)

func PrintAt deprecated

func PrintAt(s string, x int, y int, c color.Color)

PrintAt renders text at a specified position with a specified color. s is the string to be rendered. x, y define the position where the text will be rendered. c specifies the color of the text.

Deprecated: the original implementation of this was inefficient... TODO: fix this

func ReleaseButton

func ReleaseButton(buttonCode input.MouseButton)

func ReleaseKey

func ReleaseKey(keyCode input.Key)

func Run

func Run(updateFn func(), renderFn func())

Run is the main update function called to refresh the engine state.

func RunApp

func RunApp(game Game)

func RunGame

func RunGame(game Game)

func SetBorderlessWindowed

func SetBorderlessWindowed(v bool)

func SetScreenSize

func SetScreenSize(width, height int)

SetScreenSize sets the size of the screen.

func SetScrollCallback

func SetScrollCallback(cb func(x float64, y float64))

func SetTitle

func SetTitle(title string)

SetTitle sets the title of the window.

func SetWindowSize

func SetWindowSize(width, height int)

SetWindowSize sets the size of the window.

func SubmitDrawBuffer

func SubmitDrawBuffer(vertices []gui.Vertex)

Types

type Game

type Game interface {
	Update()
	Render()
}

type Renderable

type Renderable graphics.ShaderRenderable

func CreateRenderable

func CreateRenderable(shaderHandle int, vertexData []byte, layout VertexBufferLayout, uniforms map[string]Uniform, dataMap map[string][]byte) Renderable

CreateRenderable creates a `graphics.ShaderRenderable` with the provided shader handle, uniforms, and vertices.

type Shape

type Shape interface {
	graphics.Shape
}

func Circle

func Circle(x, y int, radius float32, c color.Color) Shape

Circle creates a circle shape with specified center, radius, and color. x, y define the center of the circle. radius defines the radius of the circle. c specifies the color of the circle.

func Line

func Line(x1, y1, x2, y2 int, width float32, c color.Color) Shape

Line creates a line with specified start and end points, width, and color. x1, y1 define the start point of the line. x2, y2 define the end point of the line. width defines the thickness of the line. c specifies the color of the line.

func Polygon

func Polygon(x, y int, width float32, sides int, c color.Color) Shape

Polygon creates a polygon shape with a specified number of sides, position, width, and color. x, y define the center of the polygon. width defines the diameter of the circumcircle of the polygon. sides specify the number of sides (vertices) of the polygon. c specifies the color of the polygon.

func PolygonFromVertices

func PolygonFromVertices(x, y int, width float32, vertices []Vertex) Shape

PolygonFromVertices creates a polygon shape using a specified array of vertices. The vertices should be defined with their positions and colors. The function converts the input vertices from the local Vertex type to the graphics.Vertex type required by the graphics backend. The created polygon is then added to the render queue.

Parameters:

  • x, y: The x and y coordinates of the polygon's position.
  • width: The width of the polygon (not used directly in this function, included for interface consistency).
  • vertices: A slice of Vertex that defines the positions and colors of the polygon's vertices.

Returns:

  • A graphics.Shape that represents the created polygon.

func Rectangle

func Rectangle(x, y, width, height int, c color.Color) Shape

Rectangle creates a rectangle shape with specified position, dimensions, and color. x, y define the top-left corner of the rectangle. width, height define the dimensions of the rectangle. c specifies the color of the rectangle.

func Triangle

func Triangle(x1, y1, x2, y2, x3, y3 int, c color.Color) Shape

Triangle creates a triangle shape with specified vertices and color. x1, y1, x2, y2, x3, y3 define the coordinates of the three vertices of the triangle. c specifies the color of the triangle.

type Sprite

type Sprite struct {
	*Texture
	// contains filtered or unexported fields
}

func NewSprite

func NewSprite(img image.Image, frameSize, sheetSize image.Point) *Sprite

func (*Sprite) NextFrame

func (s *Sprite) NextFrame()

func (*Sprite) SetFrame

func (s *Sprite) SetFrame(index int)

SetFrame sets the sprite to a specific frame by index

type Texture

type Texture struct {
	graphics.Texture
}

func CreateTexture

func CreateTexture(x, y, w, h int) (*Texture, error)

func CreateTextureFromImage

func CreateTextureFromImage(img image.Image) (*Texture, error)

func (Texture) Destroy

func (t Texture) Destroy()

Destroy removes the texture from the renderer

type Uniform

type Uniform struct {
	Binding uint32
	Size    uint64
}

type Vertex

type Vertex struct {
	Position [3]float32
	Color    color.Color
}

func ConvertVerticesToNDC2D

func ConvertVerticesToNDC2D(vertices []Vertex, screenWidth, screenHeight float32) []Vertex

type VertexAttributeLayout

type VertexAttributeLayout struct {
	ShaderLocation uint32
	Offset         uint64
	Format         string
}

type VertexBufferLayout

type VertexBufferLayout struct {
	ArrayStride uint64
	Attributes  []VertexAttributeLayout
}

Directories

Path Synopsis
examples
gol
gui
ui
Package graphics provides functionality for 2D graphics rendering, including textures, sprites, text, and shapes.
Package graphics provides functionality for 2D graphics rendering, including textures, sprites, text, and shapes.
gui
pkg
component
The component package has some basic structs that are common in games (e.g.
The component package has some basic structs that are common in games (e.g.
draw
draw is a package from drawing with the cpu
draw is a package from drawing with the cpu
fb
The fb package provides a framebuffer abstraction.
The fb package provides a framebuffer abstraction.
grugui
grugui draws directly to textures.
grugui draws directly to textures.
grugui/renderer
grugRenderer draws to textures.
grugRenderer draws to textures.
math/geom
The geom package provides basic geometry types and functions.
The geom package provides basic geometry types and functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL