Пример #1
0
func (window *Win32Window) GetHDC() unsafe.Pointer {
	if window.hdc != nil {
		return unsafe.Pointer(&window.hdc)
	}

	window.hdc = C.GetDC(window.hwnd)

	px := C.PIXELFORMATDESCRIPTOR{
		dwFlags:    0x00000001 | 0x00000020 | 0x00000004,
		iPixelType: 0,
		cColorBits: 32,
		cDepthBits: 32,
		iLayerType: 0,
	}

	px.nSize = (C.WORD)(unsafe.Sizeof(px))

	pxFormat := C.ChoosePX(window.hdc, &px)
	if pxFormat == 0 {
		return nil
	}

	res := C.SetPixelFormat(window.hdc, pxFormat, &px)
	if res == 0 {
		return nil
	}

	return unsafe.Pointer(&window.hdc)
}
Пример #2
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
}
Пример #3
0
// Creates the window. This function expects not to be called on a ContextThread
func (wi *windowInternal) initialize(monitor *Monitor, mode VideoMode, title string, style WindowStyle) ThreadError {
	// Compute position and size
	screenDC := C.GetDC(nil)
	width := C.int(mode.Width)
	height := C.int(mode.Height)
	left := (C.GetDeviceCaps(screenDC, C.HORZRES) - width) / 2
	top := (C.GetDeviceCaps(screenDC, C.VERTRES) - height) / 2
	C.ReleaseDC(nil, screenDC)

	// Choose the window style according to the Style parameter
	win32Style := C.DWORD(C.WS_VISIBLE)
	if style == WindowStyleNone {
		win32Style |= C.WS_POPUP
	} else {
		if style&WindowStyleTitlebar != 0 {
			win32Style |= C.WS_CAPTION | C.WS_MINIMIZEBOX
		}
		if style&WindowStyleResize != 0 {
			win32Style |= C.WS_THICKFRAME | C.WS_MAXIMIZEBOX
		}
		if style&WindowStyleClose != 0 {
			win32Style |= C.WS_SYSMENU
		}
	}

	// In windowed mode, adjust width and height so that window will have the requested client area
	fullscreen := style&WindowStyleFullscreen != 0
	if !fullscreen {
		rectangle := C.RECT{
			left:   C.LONG(left),
			top:    C.LONG(top),
			right:  C.LONG(left + width),
			bottom: C.LONG(top + height),
		}
		C.__AdjustWindowRect(&rectangle, win32Style, C.FALSE)
		left = C.int(rectangle.left)
		top = C.int(rectangle.top)
		width = C.int(rectangle.right - rectangle.left)
		height = C.int(rectangle.bottom - rectangle.top)
	}
	wTitle, _ := utf16Convert(title)
	wi.window.Handle = C.CreateWindowExW(0, className, wTitle, win32Style, left, top, width, height, nil, nil, windowClass.hInstance, C.LPVOID(wi))

	// Switch to fullscreen if requested
	if fullscreen {
		wi.switchToFullscreen(monitor, mode)
	}

	return nil
}
Пример #4
0
func (ic *contextInternal) initializeFromOwner(settings ContextSettings, owner *windowInternal, bitsPerPixel uint) ThreadError {
	ic.deactivateSignal = make(chan bool)
	ic.settings = settings

	// Get the owner window and its device context
	ic.window = C.HWND(owner.window.Handle)
	ic.ownsWindow = false

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

	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
}