Direct3D

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 7 Imported by: 0

README

This package is a Direct3D9 overlay renderer written in Go.
It allows you to easily draw images on top of the Windows screen, which can be extended to implement fonts, shapes, animations, and various other elements.

[!WARNING]
You must always set WindowProc.
For proper operation, events such as WM_PAINT and WM_DESTROY must be handled manually within the message loop.


package main

import (
	"image"
	"image/color"
	"image/draw"
	"image/gif"
	"os"
	"time"

	"github.com/fluffy-melli/Direct3D"
	"golang.org/x/sys/windows"
)

func main() {
	direct3D := Direct3D.New("GIF")
	direct3D.WindowProc = func(hwnd windows.HWND, msg uint32, wParam, lParam uintptr) uintptr {
		switch msg {
		case Direct3D.WM_PAINT:
			err := direct3D.Render()
			if err != nil {
				return uintptr(err.Code())
			}
			return 0
		case Direct3D.WM_DESTROY:
			Direct3D.ProcPostQuitMessage.Call(0)
			return 0
		}
		ret, _, _ := Direct3D.ProcDefWindowProc.Call(uintptr(hwnd), uintptr(msg), wParam, lParam)
		return ret
	}
	direct3D.INIT()

	file, err := os.Open("./example.gif")
	if err != nil {
		panic(err)
	}
    
	g, err := gif.DecodeAll(file)
	if err != nil {
		panic(err)
	}
	file.Close()

	canvas := image.NewRGBA(g.Image[0].Bounds())
	transparent := &image.Uniform{C: color.Transparent}

	direct3D.Ax = 0
	direct3D.Ay = 0

	go func() {
		frame := 0
		for {
			draw.Draw(canvas, canvas.Bounds(), transparent, image.Point{}, draw.Src)
			draw.Draw(canvas, canvas.Bounds(), g.Image[frame], image.Point{}, draw.Over)

			direct3D.SetImage(canvas)

			delay := time.Duration(g.Delay[frame]) * 10 * time.Millisecond
			if delay <= 0 {
				delay = 100 * time.Millisecond
			}
			time.Sleep(delay)

			frame++
			if frame >= len(g.Image) {
				frame = 0
			}
		}
	}()

	direct3D.Run()
}

Documentation

Index

Constants

View Source
const (
	WM_DESTROY = 0x0002
	WM_PAINT   = 0x000F

	WS_POPUP         = 0x80000000
	WS_VISIBLE       = 0x10000000
	WS_EX_TOPMOST    = 0x00000008
	WS_EX_LAYERED    = 0x00080000
	WS_EX_NOACTIVATE = 0x08000000

	SW_SHOW = 5

	LWA_ALPHA    = 0x00000002
	LWA_COLORKEY = 0x00000001

	TRANSPARENT_COLOR = 0x00000000
)
View Source
const (
	D3DFVF_XYZRHW  = 0x004
	D3DFVF_DIFFUSE = 0x040
	D3DFVF_TEX1    = 0x100
	CUSTOM_FVF     = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1
)

Variables

View Source
var (
	ProcRegisterClass              = user32.NewProc("RegisterClassW")
	ProcCreateWindowEx             = user32.NewProc("CreateWindowExW")
	ProcDefWindowProc              = user32.NewProc("DefWindowProcW")
	ProcGetMessage                 = user32.NewProc("GetMessageW")
	ProcTranslateMessage           = user32.NewProc("TranslateMessage")
	ProcDispatchMessage            = user32.NewProc("DispatchMessageW")
	ProcPostQuitMessage            = user32.NewProc("PostQuitMessage")
	ProcShowWindow                 = user32.NewProc("ShowWindow")
	ProcUpdateWindow               = user32.NewProc("UpdateWindow")
	ProcGetWindowDC                = user32.NewProc("GetWindowDC")
	ProcReleaseDC                  = user32.NewProc("ReleaseDC")
	ProcGetModuleHandle            = kernel32.NewProc("GetModuleHandleW")
	ProcSetLayeredWindowAttributes = user32.NewProc("SetLayeredWindowAttributes")
	ProcSetWindowPos               = user32.NewProc("SetWindowPos")
	ProcGetSystemMetrics           = user32.NewProc("GetSystemMetrics")
	ProcInvalidateRect             = user32.NewProc("InvalidateRect")
	ProcCreateSolidBrush           = gdi32.NewProc("CreateSolidBrush")
	ProcDeleteObject               = gdi32.NewProc("DeleteObject")
	ProcFillRect                   = user32.NewProc("FillRect")
)

Functions

This section is empty.

Types

type BITMAPINFO

type BITMAPINFO struct {
	// contains filtered or unexported fields
}

type BITMAPINFOHEADER

type BITMAPINFOHEADER struct {
	// contains filtered or unexported fields
}

type MSG

type MSG struct {
	// contains filtered or unexported fields
}

type Rect

type Rect struct {
	Left, Top, Right, Bottom int32
}

type Render

type Render struct {
	WindowProc func(hwnd windows.HWND, msg uint32, wParam, lParam uintptr) uintptr
	Window     windows.HWND
	D3d9Obj    *d3d9.Direct3D
	Device     *d3d9.Device

	LastTexture *d3d9.Texture
	Texture     *d3d9.Texture
	ClassName   string
	LastDx      int
	LastDy      int
	LastAx      int
	LastAy      int
	Dx          int
	Dy          int
	Ax          int
	Ay          int
	// contains filtered or unexported fields
}

func New

func New(className string) *Render

func (*Render) ClearGDI

func (s *Render) ClearGDI()

func (*Render) INIT

func (s *Render) INIT() error

func (*Render) ModuleHandle

func (s *Render) ModuleHandle() windows.Handle

func (*Render) Release

func (s *Render) Release()

func (*Render) Render

func (s *Render) Render() d3d9.Error

func (*Render) Run

func (s *Render) Run()

func (*Render) ScreenSize

func (s *Render) ScreenSize() (int, int)

func (*Render) SetImage

func (s *Render) SetImage(img image.Image) error

type Vertex

type Vertex struct {
	X, Y, Z float32
	Rhw     float32
	Color   uint32
	U, V    float32
}

type WNDCLASS

type WNDCLASS struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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