Skip to content

Commit

Permalink
Choose GetProcAddr function inside each generated package.
Browse files Browse the repository at this point in the history
This replaces the old procaddr/* packages.
  • Loading branch information
slimsag committed Mar 15, 2015
1 parent a3931c1 commit 8648b94
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
3 changes: 3 additions & 0 deletions package.go
Expand Up @@ -50,6 +50,9 @@ func (pkg *Package) GeneratePackage(dir string) error {
if err := pkg.generateFile("conversions", dir); err != nil {
return err
}
if err := pkg.generateFile("procaddr", dir); err != nil {
return err
}
if pkg.HasDebugCallbackFeature() {
if err := pkg.generateFile("debug", dir); err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions package.tmpl
Expand Up @@ -71,8 +71,6 @@ import (
{{if .HasRequiredFunctions}}
"errors"
{{end}}
"{{.Prefix}}/procaddr"
"{{.Prefix}}/procaddr/auto"
"unsafe"
)

Expand Down Expand Up @@ -110,10 +108,13 @@ func {{.GoName}}({{template "paramsGoDecl" .Parameters}}){{if not .Return.IsVoid
{{end}}

func Init() error {
return InitWithProcAddrFunc(auto.GetProcAddress)
return InitWithProcAddrFunc(getProcAddress)
}

func InitWithProcAddrFunc(getProcAddr procaddr.GetProcAddressFunc) error {
// InitWithProcAddrFunc intializes the package with the given getProcAddr
// function. It is for advanced clients who wish to provide their own OpenGL
// function pointers. For most cases Init will be used instead.
func InitWithProcAddrFunc(getProcAddr func(name string) unsafe.Pointer) error {
{{range .Functions}}
gp{{.GoName}} = (C.GP{{toUpper .GoName}})(getProcAddr("{{.Name}}"))
{{if .Required}}
Expand Down
78 changes: 78 additions & 0 deletions procaddr.tmpl
@@ -0,0 +1,78 @@
// This file implements GlowGetProcAddress for each platform. The correct
// version is chosen automatically based on build tags:
//
// windows: WGL
// darwin: CGL
// linux: GLX
//
// Use of EGL instead of the platform's default (listed above) is made possible
// via the "egl" build tag.
//
// It is also possible to install your own function outside this package for
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.

package {{.Name}}

/*
#cgo windows CFLAGS: -DTAG_WINDOWS
#cgo windows LDFLAGS: -lopengl32

#cgo darwin CFLAGS: -DTAG_DARWIN
#cgo darwin LDFLAGS: -framework OpenGL

#cgo linux CFLAGS: -DTAG_LINUX
#cgo linux LDFLAGS: -lGL

#cgo egl CFLAGS: -DTAG_EGL
#cgo egl LDFLAGS: -lEGL

#ifdef TAG_WINDOWS
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <stdlib.h>
static HMODULE ogl32dll = NULL;
void* GlowGetProcAddress(const char* name) {
void* pf = wglGetProcAddress((LPCSTR)name);
if(pf) {
return pf;
}
if(ogl32dll == NULL) {
ogl32dll = LoadLibraryA("opengl32.dll");
}
return GetProcAddress(ogl32dll, (LPCSTR)name);
}
#endif

#ifdef TAG_DARWIN
#include <stdlib.h>
#include <dlfcn.h>
void* GlowGetProcAddress(const char* name) {
return dlsym(RTLD_DEFAULT, name);
}
#endif

#ifdef TAG_LINUX
#include <stdlib.h>
#include <GL/glx.h>
void* GlowGetProcAddress(const char* name) {
return glXGetProcAddress(name);
}
#endif

#ifdef TAG_EGL
#include <stdlib.h>
#include <EGL/egl.h>
void* GlowGetProcAddress(const char* name) {
return eglGetProcAddress(name);
}
#endif
*/
import "C"

import "unsafe"

func getProcAddress(namea string) unsafe.Pointer {
cname := C.CString(namea)
defer C.free(unsafe.Pointer(cname))
return C.GlowGetProcAddress(cname)
}
2 changes: 1 addition & 1 deletion type.go
Expand Up @@ -75,7 +75,7 @@ func (t Type) GoType() string {
case "GLfixed":
return t.pointers() + "int32"
case "GLchar", "GLcharARB":
// Chosen (vs. byte) for compatibility where GL uses GLubyte for strings (e.g., glGetString)
// Chosen (vs. byte) for compatibility where GL uses GLubyte for strings (e.g., glGetString)
return t.pointers() + "uint8"
case "GLboolean":
return t.pointers() + "bool"
Expand Down

0 comments on commit 8648b94

Please sign in to comment.