g2d

package module
v0.0.0-...-735b6b1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2025 License: BSL-1.0 Imports: 12 Imported by: 1

README

g2d

Go Reference Go Report Card

About

g2d is a framework to create 2D graphic applications. It is published on https://github.com/vbsw/g2d and https://gitlab.com/vbsw/g2d.

Demo is available on https://github.com/vbsw/g2d-demo.

Copyright 2023, 2025, Vitali Baumtrok ([email protected]).

g2d is distributed under the Boost Software License, version 1.0. (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

g2d is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Boost Software License for more details.

Compile

Install Go (https://golang.org/doc/install). For Cgo install a C compiler (https://jmeubank.github.io/tdm-gcc/).

For Windows: To compile an executable that doesn't open a console, use

-ldflags -H=windowsgui

Example

package main

import (
	"fmt"
	"github.com/vbsw/g2d"
	"runtime"
)

func init() {
	// run Main on main thread
	runtime.LockOSThread()
}

func Main() {
	g2d.Init()
	g2d.MainLoop(new(g2d.WindowImpl))
	if g2d.Err != nil {
		fmt.Println(g2d.Err.Error())
	}
}

References

Documentation

Overview

Package g2d is a framework to create 2D graphic applications.

Index

Constants

This section is empty.

Variables

View Source
var (
	MaxTexSize, MaxTexUnits int
	MaxTextures             int
	VSyncAvailable          bool
	AVSyncAvailable         bool
	Err                     error
)

Functions

func BytesFromImage

func BytesFromImage(img image.Image) []byte

BytesFromImage returns bytes from image. (This is for test purposes.)

func ImageFromFile

func ImageFromFile(path string) (image.Image, error)

ImageFromFile reads image from file. (This is for test purposes.)

Types

type Configuration

type Configuration struct {
	ClientX, ClientY                  int
	ClientWidth, ClientHeight         int
	ClientWidthMin, ClientHeightMin   int
	ClientWidthMax, ClientHeightMax   int
	MouseLocked, Borderless, Dragable bool
	Resizable, Fullscreen, Centered   bool
	Title                             string
}

Configuration is the initial setting of window.

type Framebuffer

type Framebuffer interface {
}

Framebuffer provides a buffer to draw to.

type Graphics

type Graphics struct {
	BgR, BgG, BgB float32
	VSync, AVSync bool

	Layers []Layer
	// contains filtered or unexported fields
}

Graphics draws graphics.

func (*Graphics) CreateFramebuffer

func (gfx *Graphics) CreateFramebuffer(texture Texture)

CreateFramebuffer creates a buffer to draw to. [not implemented]

func (*Graphics) LoadTexture

func (gfx *Graphics) LoadTexture(texture Texture)

LoadTexture loads a texture into video memory. This is asynchronous. After the texture has been loaded OnTextureLoaded is called. (Loading a new texture with same id as a previous one will release the previous texture.)

type Layer

type Layer interface {
	// contains filtered or unexported methods
}

type Properties

type Properties struct {
	MouseX, MouseY                    int
	ClientX, ClientY                  int
	ClientWidth, ClientHeight         int
	ClientWidthMin, ClientHeightMin   int
	ClientWidthMax, ClientHeightMax   int
	MouseLocked, Borderless, Dragable bool
	Resizable, Fullscreen             bool
	Title                             string
}

Properties are the current window properties.

type Rectangle

type Rectangle struct {
	X, Y, Width, Height  float32
	R, G, B, A           float32
	RotX, RotY, RotAlpha float32
	TexRef, TexX, TexY   int
	TexWidth, TexHeight  int
	Enabled              bool
	// contains filtered or unexported fields
}

Rectangle is an entity from a RectanglesLayer.

type RectanglesLayer

type RectanglesLayer struct {
	Enabled bool
	// contains filtered or unexported fields
}

RectanglesLayer is a layer holding rectangles.

func (*RectanglesLayer) NewEntity

func (layer *RectanglesLayer) NewEntity() *Rectangle

NewEntity returns a new instance of Rectangle.

func (*RectanglesLayer) Release

func (layer *RectanglesLayer) Release(r *Rectangle) *Rectangle

Release releases the entity. This entity may be reused when calling NewEntity.

func (*RectanglesLayer) UseTexture

func (layer *RectanglesLayer) UseTexture(ref, textureId int)

UseTexture associates a reference with a texture. Reference must be in range of [0, 15].

type Stats

type Stats struct {
	AppTime, DeltaTime int

	UPS, FPS int
	// contains filtered or unexported fields
}

Stats has useful data. Time is in milliseconds.

type Texture

type Texture interface {
	Id() int
	RGBABytes() ([]byte, error)
	Dimensions() (int, int)
	GenMipMap() bool
	IsMipMap() bool
	FilterLinear() bool
}

Texture provides a texture.

type Window

type Window interface {
	OnConfig(config *Configuration) error
	OnCreate() error
	OnShow() error
	OnResize() error
	OnMove() error
	OnKeyDown(keyCode int, repeated uint) error
	OnKeyUp(keyCode int) error
	OnMouseMove() error
	OnButtonDown(buttonCode int, doubleClicked bool) error
	OnButtonUp(buttonCode int, doubleClicked bool) error
	OnWheel(rotation float32) error
	OnCustom(obj interface{}) error
	OnTextureLoaded(texture Texture) error
	OnFramebufferCreated(buffer Framebuffer) error
	OnUpdate() error
	OnClose() (bool, error)
	OnDestroy(error) error
	OnMinimize() error
	OnRestore() error
	OnFocus(focus bool) error
	Custom(obj interface{})
	Update()
	Close()
	Quit()
	Show(window Window)
	// contains filtered or unexported methods
}

Window is callback for window handling.

type WindowImpl

type WindowImpl struct {
	Props Properties
	Stats Stats
	Gfx   Graphics
	// contains filtered or unexported fields
}

WindowImpl is the obligatory struct to embed, when using interface Window.

func (*WindowImpl) Close

func (wnd *WindowImpl) Close()

Close triggers OnClose event.

func (*WindowImpl) Custom

func (wnd *WindowImpl) Custom(obj interface{})

Custom triggers OnCustom event.

func (*WindowImpl) OnButtonDown

func (wnd *WindowImpl) OnButtonDown(buttonCode int, doubleClicked bool) error

OnButtonDown is called when mouse button has been pressed.

func (*WindowImpl) OnButtonUp

func (wnd *WindowImpl) OnButtonUp(buttonCode int, doubleClicked bool) error

OnButtonUp is called when mouse button has been released.

func (*WindowImpl) OnClose

func (wnd *WindowImpl) OnClose() (bool, error)

OnClose is called after close button of window has been pressed. If function returns true, window will be destroyed.

func (*WindowImpl) OnConfig

func (wnd *WindowImpl) OnConfig(config *Configuration) error

OnConfig is called before creating the window. Configuration is used to create a window.

func (*WindowImpl) OnCreate

func (wnd *WindowImpl) OnCreate() error

OnCreate is called after window has been created. This event could be used to allocate ressources and set graphics.

func (*WindowImpl) OnCustom

func (wnd *WindowImpl) OnCustom(obj interface{}) error

OnCustom is called after calling Custom().

func (*WindowImpl) OnDestroy

func (wnd *WindowImpl) OnDestroy(err error) error

OnDestroy is called before window gets destroyed.

func (*WindowImpl) OnFocus

func (wnd *WindowImpl) OnFocus(focus bool) error

OnFocus is called when window gets or loses focus.

func (*WindowImpl) OnFramebufferCreated

func (wnd *WindowImpl) OnFramebufferCreated(texture Framebuffer) error

OnFramebufferCreated is called after texture has been created.

func (*WindowImpl) OnKeyDown

func (wnd *WindowImpl) OnKeyDown(keyCode int, repeated uint) error

OnKeyDown is called when a key has been pressed. If key stayes pressed, parameter repeated is != 0.

func (*WindowImpl) OnKeyUp

func (wnd *WindowImpl) OnKeyUp(keyCode int) error

OnKeyUp is called when a key has been released.

func (*WindowImpl) OnMinimize

func (wnd *WindowImpl) OnMinimize() error

OnMinimize is called after window has been minimized.

func (*WindowImpl) OnMouseMove

func (wnd *WindowImpl) OnMouseMove() error

OnMouseMove is called when mouse is moved. New properties are in wnd.Props.

func (*WindowImpl) OnMove

func (wnd *WindowImpl) OnMove() error

OnMove is called when Window is moved. New properties are in wnd.Props.

func (*WindowImpl) OnResize

func (wnd *WindowImpl) OnResize() error

OnResize is called when Window is resized. New properties are in wnd.Props.

func (*WindowImpl) OnRestore

func (wnd *WindowImpl) OnRestore() error

OnRestore is called after window has returned from minimized state.

func (*WindowImpl) OnShow

func (wnd *WindowImpl) OnShow() error

OnShow is called after window has been set visible. This event culd be used to start animations.

func (*WindowImpl) OnTextureLoaded

func (wnd *WindowImpl) OnTextureLoaded(texture Texture) error

OnTextureLoaded is called after texture has been loaded to video memory.

func (*WindowImpl) OnUpdate

func (wnd *WindowImpl) OnUpdate() error

OnCustom is called after calling Update(). After OnUpdate graphis is redrawn.

func (*WindowImpl) OnWheel

func (wnd *WindowImpl) OnWheel(rotation float32) error

OnWheel is called when mouse wheel has been moved.

func (*WindowImpl) Quit

func (wnd *WindowImpl) Quit()

Quit destroys window unconditionally.

func (*WindowImpl) Show

func (wnd *WindowImpl) Show(window Window)

Show creates a new window.

func (*WindowImpl) Update

func (wnd *WindowImpl) Update()

Update triggers OnUpdate event.

Jump to

Keyboard shortcuts

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