• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Direct3D9 wrapper for Go.

d3d9

This library is a pure Go wrapper for Microsoft's Direct3D9 API.

Games

Here you can see the library in action, it was used for two 48 hour game jams.

This is my entry to the GopherGala 2016:

Gophette Screenshot

And here is my entry for the Ludum Dare Game Jam 2016:

Reinventing the Wheel Screenshot

Installation

Get and build the library with:

go get -u github.com/gonutz/d3d9

To run a Direct3D9 application you need to have d3d9.dll installed on your system. Luckily, all Windows versions starting with Windows XP have this pre-installed. This means that you will not need to ship any additional DLL with your application when using this library.

Obsolete CGo Version 1

This library does not use CGo anymore. With this change the API has incompatibly changed in some places. If you have a project using the old API and do not want to update everything right now, you can use a copy of version 1.0.0 of this library at github.com/gonutz/cgo/d3d9. All you have to do is go get that library and update your imports from github.com/gonutz/d3d9 to github.com/gonutz/cgo/d3d9.

Usage

All Direct3D9 interfaces are translated to Go types and their methods are translated to functions on the types so the usage is very close to the C++ API.

There are some differences in the names in Go, since the package is named d3d9, all names in that package drop the D3D and 9 parts because they would be redundant. The changes are:

  • Interfaces drop the IDirect3D prefix and the 9 suffix, e.g. IDirect3DDevice9 becomes d3d9.Device. The only exception is IDirect3D9 which in Go becomes Direct3D.
  • Constants and enumerations drop the D3D prefix, otherwise they are the same and keep the upper case convention so users of Direct3D can easily find what they are looking for. For example D3DADAPTER_DEFAULT becomes d3d9.ADAPTER_DEFAULT, D3DFMT_R8G8B8 becomes d3d9.FMT_R8G8B8 etc.
  • Structs, like constants, only drop the D3D prefix, they too keep the upper case naming convention, so D3DRANGE becomes d3d9.RANGE.
  • Error constants also drop the D3D prefix so D3DERR_OUTOFVIDEOMEMORY becomes ERR_OUTOFVIDEOMEMORY. However, the interface functions do not return these constants, they return Go errors instead of HRESULTs.
  • Instead of returning HRESULT, functions return d3d9.Error which implements the standard Go error interface and has an additional function Code() int32 which returns the error code. This code can be checked against the defined error constants. If a function succeeds it returns nil (and not D3D_OK) as the d3d9.Error.

Note that Direct3D9 needs a window handle for setting it up. This means that you need some way to create a native window and get the handle to pass it to d3d9. In the samples you can see how to do it using the SDL2 Go wrapper and Allen Dang's w32. You could also use other Windows wrapper libraries, like the walk library, or just write a little CGo code to set up a window yourself. This library does not provide window creation or event handling functionality, only the Direct3D9 wrapper.

All calls to Direct3D9 must happen from the same thread that creates the Direct3D9 object so make sure to add this code in your main package:

func init() {
    runtime.LockOSThread()
}

There are some additional convenience functions. IndexBuffer and VertexBuffer have Lock functions which return void* pointers in the C API and would thus return uintptrs in Go. You can use these pointers to read and write various types from/to that memory. However, using uintptr or unsafe.Pointer is not idiomatic Go so the Lock functions return a wrapper around the uintptr instead, providing functions of the form GetFloat32s and SetFloat32s which take a slice of []float32 and handle copying the data for you. See the documentation of these functions for further information.

Similarly, when locking a two-dimensional resource, like a texture, you will get a d3d9.LOCKED_RECT. It too has a wrapper function for setting its 2D data. There is however no function to read the data, yet, and no functions for reading and writing sub-rectangles which could be useful. These functions can be added when needed by a real application to see the use case first and implement the functions according to that instead of guessing what might work. If you need such a function, create a pull request or an issue and it can be incorporated in this library.

Documentation

See the GoDoc for the Go API. The functions are only documented very generally, to get more detailed information about the Direct3D9 API see the MSDN documentation.

Examples

The samples folder contains some example programs that show the basics of setting up and using the library.

A real world usage of this library is my entry to the GopherGala 2016. It was first built using SDL2 and after the competition I ported it to DirectX, see main_windows.go for the implementation.

Another real world example is my entry for the Ludum Dare Game Jam 2016 which is Windows only.

The prototype library also uses d3d9 on Windows.

Status

The code started out as a rough version generated from the MSDN online documentation and was then manually refined to work well with Go and its conventions. It wraps the whole Direct3D9 API right now and adds some additional convenience functions for easy Go usage. However, LOCKED_RECT only has one function for setting the whole data rectangle at once, there is no simple way to set only sub-rectangles right now. Similarly the CubeTexture's LOCKED_BOX has no wrapper functions for getting and setting its data, yet. For now the raw uintptr memory pointers can be used along with the pitch values and in the future such functions can be added when needed.

Help improve this library

Only real world use and feedback can improve the usability of this library, so please use it, fork it, send pull requests and create issues to help improve this library.

More Repositories

1

wui

Windows GUI library for Go (Golang). Comes with a graphical UI designer.
Go
167
star
2

prototype

Simple 2D game prototyping framework.
Go
82
star
3

framebuffer

Go library for accessing the framebuffer under Raspbian as a Go Image.
Go
37
star
4

ide

Go IDE written in Go; currently Windows-only desktop application
Go
11
star
5

no-brain-jogging

Game written in Go, shoot Zombies with Mathematics
Go
7
star
6

go-sdl2-dll

Go wrapper for SDL2 - this is for Windows only and uses the DLL, no need for CGo
Go
5
star
7

dsp

Digital signal processing functions in Go
Go
3
star
8

ftp-client

Go implementation of the FTP protocol (RFC 959)
Go
3
star
9

auto

Go library for Windows automation
Go
3
star
10

dfm

Go library to parse/modify/generate Delphi DFM files
Go
2
star
11

tic

Simple timing output for Go functions
Go
2
star
12

aoc22

https://adventofcode.com/2022
Go
2
star
13

payload

Append a data file to your executable and read it back in at runtime.
Go
2
star
14

pas

Delphi parser written in Go, work in progress
Go
2
star
15

ds

DirectSound wrapper for Go.
Go
1
star
16

mbw

Monospaced black and white fonts
Go
1
star
17

cgo

Obsolete Go wrappers that use CGo on Windows but now have pure Go counterparts
Go
1
star
18

jolina

A little two-day game created with my friend Jolina
Go
1
star
19

blob

Go library for building binary blob files, mapping string IDs to byte data.
Go
1
star
20

expr

Arithmetic expression parser
Go
1
star
21

zoom_thumbs_up

Windows tool to click the thumbs up reaction in the Zoom client.
Go
1
star
22

ld41

Ludum Dare 41 Entry - Combine Two Incompatible Genres
Go
1
star
23

gif2pngs

Convert a GIF image to PNG frames
Go
1
star
24

checkmark

Windows tool that copies a checkmark character ✓ into the clipboard
Go
1
star
25

work

Work time tracking GUI tool for Windows, written in Go
Go
1
star
26

dfm_clear_explicit_zeros

Go tool to remove unnecessary Explicit properties from Delphi DFM files.
Go
1
star
27

usbreset

Go tool that does what the C version of usbreset does - reset USB devices
Go
1
star
28

getmp3

Linux script to load Youtube videos as mp3s into your Music folder
Shell
1
star
29

volume

Windows tools for volume up/down/mute written in Go
Go
1
star
30

sdl2

Pure Go SDL2 wrapper for Windows, no CGo
Go
1
star
31

ico

Go library and command line tool to generate .ico files from image
Go
1
star
32

sdl2-dll-issue

This is an issue on 32 bit Windows when calling the SDL2.dll
Go
1
star
33

tile_screen

Windows program to move a window on a tiled screen
Go
1
star
34

zoom_emote

Windows program to click Zoom emojis with keyboard shortcuts
Go
1
star