ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - gonutz/d3d9: Direct3D9 wrapper for Go.
Direct3D9 wrapper for Go. Contribute to gonutz/d3d9 development by creating an account on GitHub.
Visit Site

GitHub - gonutz/d3d9: Direct3D9 wrapper for Go.

GitHub - gonutz/d3d9: 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.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory