Example #1
0
func (ic *contextInternal) initializeFromSettings(settings ContextSettings, width, height int) ThreadError {
	ic.deactivateSignal = make(chan bool)
	ic.settings = settings

	ic.window = createHiddenWindow(width, height)
	if ic.window == nil {
		// C.GetLastError()
		return NewThreadError(fmt.Errorf("could not create window (%d)", C.GetLastError()), true)
	}
	ic.ownsWindow = true

	ic.hdc = C.GetDC(ic.window)
	if ic.hdc == nil {
		return NewThreadError(errors.New("no device context"), true)
	}

	if sharedContext == nil { // if there is no shared context, we are it
		ic.isSharedContext = true

		pfd := C.PIXELFORMATDESCRIPTOR{
			nSize:      C.PIXELFORMATDESCRIPTOR_size, // size of this pfd
			nVersion:   1,                            // version number
			iPixelType: C.PFD_TYPE_RGBA,              // RGBA type
			cColorBits: 24,                           // 24-bit color depth
			cDepthBits: 32,                           // 32-bit z-buffer
			iLayerType: C.PFD_MAIN_PLANE,             // main layer

			// support window | OpenGL | double buffer
			dwFlags: C.PFD_DRAW_TO_WINDOW | C.PFD_SUPPORT_OPENGL | C.PFD_DOUBLEBUFFER,
		}

		// get the best available match of pixel format for the device context
		// make that the pixel format of the device context
		if iPixelFormat := C.ChoosePixelFormat(ic.hdc, &pfd); iPixelFormat == 0 {
			return NewThreadError(fmt.Errorf("sharedContext: ChoosePixelFormat failed (%d)", C.GetLastError()), true)
		} else if C.SetPixelFormat(ic.hdc, iPixelFormat, &pfd) == C.FALSE {
			return NewThreadError(fmt.Errorf("sharedContext: SetPixelFormat failed (%d)", C.GetLastError()), true)
		}

		ic.context = C.wglCreateContext(ic.hdc)
		if ic.context == nil {
			return NewThreadError(fmt.Errorf("sharedContext: wglCreateContext failed (%d)", C.GetLastError()), true)
		}
	} else { // otherwise we push the commands onto the shared context thread

		bitsPerPixel := GetDefaultMonitor().GetDesktopMode().BitsPerPixel
		ic.context = createContext(&sharedContext.internal.procs, sharedContext.internal.context, ic.hdc, bitsPerPixel, &ic.settings)
		if ic.context == nil {
			return NewThreadError(fmt.Errorf("could not create context (%d)", C.GetLastError()), true)
		}
	}
	// signal because we start out deactivated
	ic.signalDeactivation()
	return nil
}
Example #2
0
// Find a pixel format with no antialiasing, if not needed or not supported
func BestChoosePixelFormat(hdc C.HDC, bitsPerPixel uint, settings *ContextSettings) C.int {
	// Setup a pixel format descriptor from the rendering settings
	descriptor := C.PIXELFORMATDESCRIPTOR{
		nSize:        C.PIXELFORMATDESCRIPTOR_size,
		nVersion:     1,
		iLayerType:   C.PFD_MAIN_PLANE,
		dwFlags:      C.PFD_DRAW_TO_WINDOW | C.PFD_SUPPORT_OPENGL | C.PFD_DOUBLEBUFFER,
		iPixelType:   C.PFD_TYPE_RGBA,
		cColorBits:   C.BYTE(bitsPerPixel),
		cDepthBits:   C.BYTE(settings.DepthBits),
		cStencilBits: C.BYTE(settings.StencilBits),
	}
	if bitsPerPixel == 32 {
		descriptor.cAlphaBits = 8
	}

	// Get the pixel format that best matches our requirements
	return C.ChoosePixelFormat(hdc, &descriptor)
}