Exemple #1
0
func DetachNotifyHandler(hwnd uint32) {
	key := uintptr(hwnd)
	if _, exists := notifyHandlers[key]; exists {
		C.HTMLayoutSetCallback(C.HWND(C.HANDLE(key)), nil, nil)
		delete(notifyHandlers, key)
	}
}
Exemple #2
0
func RootElement(hwnd uint32) *Element {
	var handle HELEMENT = BAD_HELEMENT
	if ret := C.HTMLayoutGetRootElement(C.HWND(C.HANDLE(uintptr(hwnd))), (*C.HELEMENT)(&handle)); ret != HLDOM_OK {
		domPanic(ret, "Failed to get root element")
	}
	return NewElementFromHandle(handle)
}
Exemple #3
0
func toggleFullscreen(window C.HWND) {
	style := C.GetWindowLong(window, C.GWL_STYLE)
	if style&C.WS_OVERLAPPEDWINDOW != 0 {
		// go into full-screen
		monitorInfo := C.MONITORINFO{cbSize: C.sizeof_MONITORINFO}
		previousPlacement.length = C.sizeof_WINDOWPLACEMENT
		monitor := C.MonitorFromWindow(window, C.MONITOR_DEFAULTTOPRIMARY)
		if C.GetWindowPlacement(window, &previousPlacement) != 0 &&
			C.GetMonitorInfo(monitor, &monitorInfo) != 0 {
			C.SetWindowLong(window, C.GWL_STYLE, style & ^C.WS_OVERLAPPEDWINDOW)
			C.SetWindowPos(window, C.HWND(unsafe.Pointer(uintptr(0))),
				C.int(monitorInfo.rcMonitor.left),
				C.int(monitorInfo.rcMonitor.top),
				C.int(monitorInfo.rcMonitor.right-monitorInfo.rcMonitor.left),
				C.int(monitorInfo.rcMonitor.bottom-monitorInfo.rcMonitor.top),
				C.SWP_NOOWNERZORDER|C.SWP_FRAMECHANGED,
			)
		}
	} else {
		// go into windowed mode
		C.SetWindowLong(window, C.GWL_STYLE, style|C.WS_OVERLAPPEDWINDOW)
		C.SetWindowPlacement(window, &previousPlacement)
		C.SetWindowPos(window, nil, 0, 0, 0, 0,
			C.SWP_NOMOVE|C.SWP_NOSIZE|C.SWP_NOZORDER|
				C.SWP_NOOWNERZORDER|C.SWP_FRAMECHANGED,
		)
	}
}
Exemple #4
0
// Load html contents into window
func LoadHtml(hwnd uint32, data []byte, baseUrl string) error {
	if len(data) > 0 {
		if ok := C.HTMLayoutLoadHtmlEx(C.HWND(C.HANDLE(uintptr(hwnd))), (*C.BYTE)(&data[0]),
			C.UINT(len(data)), (*C.WCHAR)(stringToUtf16Ptr(baseUrl))); ok == 0 {
			return errors.New("HTMLayoutLoadHtmlEx failed")
		}
	}
	return nil
}
Exemple #5
0
func FocusedElement(hwnd uint32) *Element {
	var handle HELEMENT = BAD_HELEMENT
	if ret := C.HTMLayoutGetFocusElement(C.HWND(C.HANDLE(uintptr(hwnd))), (*C.HELEMENT)(&handle)); ret != HLDOM_OK {
		domPanic(ret, "Failed to get focus element")
	}
	if handle != BAD_HELEMENT {
		return NewElementFromHandle(handle)
	}
	return nil
}
Exemple #6
0
func DetachWindowEventHandler(hwnd uint32) {
	key := uintptr(hwnd)
	if handler, exists := windowEventHandlers[hwnd]; exists {
		tag := uintptr(unsafe.Pointer(handler))
		if ret := C.HTMLayoutWindowDetachEventHandler(C.HWND(C.HANDLE(key)), (*[0]byte)(unsafe.Pointer(goElementProc)), C.LPVOID(tag)); ret != HLDOM_OK {
			domPanic(ret, "Failed to detach event handler from window")
		}
		delete(windowEventHandlers, hwnd)
	}
}
Exemple #7
0
func AttachWindowEventHandler(hwnd uint32, handler *EventHandler) {
	key := uintptr(hwnd)
	tag := uintptr(unsafe.Pointer(handler))

	if _, exists := windowEventHandlers[hwnd]; exists {
		if ret := C.HTMLayoutWindowDetachEventHandler(C.HWND(C.HANDLE(key)), (*[0]byte)(unsafe.Pointer(goElementProc)), C.LPVOID(tag)); ret != HLDOM_OK {
			domPanic(ret, "Failed to detach event handler from window before adding the new one")
		}
	}

	// Overwrite if it exists
	windowEventHandlers[hwnd] = handler

	// Don't let the caller disable ATTACH/DETACH events, otherwise we
	// won't know when to throw out our event handler object
	subscription := handler.Subscription()
	subscription &= ^uint32(DISABLE_INITIALIZATION & 0xffffffff)

	if ret := C.HTMLayoutWindowAttachEventHandler(C.HWND(C.HANDLE(key)), (*[0]byte)(unsafe.Pointer(goElementProc)), C.LPVOID(tag), C.UINT(subscription)); ret != HLDOM_OK {
		domPanic(ret, "Failed to attach event handler to window")
	}
}
Exemple #8
0
func (obj DirectInput) RunControlPanel(
	ownerWindow unsafe.Pointer,
	flags uint32,
) (
	err error,
) {
	err = toError(C.IDirectInput8RunControlPanel(
		obj.handle,
		C.HWND(ownerWindow),
		C.DWORD(flags),
	))
	return
}
Exemple #9
0
func (obj Device) SetCooperativeLevel(
	windowHandle unsafe.Pointer,
	flags uint32,
) (
	err error,
) {
	err = toError(C.IDirectInputDevice8SetCooperativeLevel(
		obj.handle,
		C.HWND(windowHandle),
		C.DWORD(flags),
	))
	return
}
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
}
Exemple #11
0
func AttachNotifyHandler(hwnd uint32, handler *NotifyHandler) {
	key := uintptr(hwnd)
	// Overwrite if it exists
	notifyHandlers[key] = handler
	C.HTMLayoutSetCallback(C.HWND(C.HANDLE(key)), (*[0]byte)(unsafe.Pointer(goNotifyProc)), C.LPVOID(key))
}
Exemple #12
0
// Load resource (file or url) into window
func LoadResource(hwnd uint32, uri string) error {
	if ok := C.HTMLayoutLoadFile(C.HWND(C.HANDLE(uintptr(hwnd))), (*C.WCHAR)(stringToUtf16Ptr(uri))); ok == 0 {
		return errors.New("HTMLayoutLoadFile failed")
	}
	return nil
}
Exemple #13
0
// Main htmlayout wndproc
func ProcNoDefault(hwnd, msg uint32, wparam, lparam uintptr) (uintptr, bool) {
	var handled C.BOOL = 0
	var result C.LRESULT = C.HTMLayoutProcND(C.HWND(C.HANDLE(uintptr(hwnd))), C.UINT(msg),
		C.WPARAM(wparam), C.LPARAM(lparam), &handled)
	return uintptr(result), handled != 0
}
Exemple #14
0
func (s *Window) SetTitle(title string) {
	// message handling
	hwnd := C.HWND(unsafe.Pointer(s.GetHwnd()))
	C.SetWindowTextW(hwnd, (*C.WCHAR)(unsafe.Pointer(sciter.StringToWcharPtr(title))))
}
Exemple #15
0
// #include "helper_windows.h"
//
// void * __HWND_BOTTOM    = HWND_BOTTOM;
// void * __HWND_NOTOPMOST = HWND_NOTOPMOST;
// void * __HWND_TOP       = HWND_TOP;
// void * __HWND_TOPMOST   = HWND_TOPMOST;
import "C"
import (
	"errors"
	"fmt"
	"unicode/utf16"
)

var (
	HWND_BOTTOM    = C.HWND(C.__HWND_BOTTOM)
	HWND_NOTOPMOST = C.HWND(C.__HWND_NOTOPMOST)
	HWND_TOP       = C.HWND(C.__HWND_TOP)
	HWND_TOPMOST   = C.HWND(C.__HWND_TOPMOST)
)

func utf16Convert(s string) (ptr C.LPCWSTR, length int) {
	encoded := utf16.Encode([]rune(s + "\x00"))
	return C.LPCWSTR((*C.WCHAR)(&encoded[0])), len(encoded)
}

func utf16ConvertFrom(s []C.WCHAR) string {
	ret := make([]uint16, len(s))
	for i, v := range s {
		ret[i] = uint16(v)
	}
Exemple #16
0
// SetWin32Window sets the Win32 window to use for context creation.
// Call before Init.
func SetWin32Window(wnd uintptr) {
	C.bgfx_win_set_hwnd(C.HWND(unsafe.Pointer(wnd)))
}
Exemple #17
0
// Call this from your NotifyHandler.HandleLoadData method if you want htmlayout to
// process the data right away so you don't have to provide a buffer in the NmhlLoadData structure.
func DataReady(hwnd uint32, uri *uint16, data []byte) bool {
	return C.HTMLayoutDataReady(C.HWND(C.HANDLE(uintptr(hwnd))), (*C.WCHAR)(uri), (*C.BYTE)(&data[0]), C.DWORD(len(data))) != 0
}
Exemple #18
0
func main() {
	// TODO enable VSync in D3D

	windowHandle, err := windows.OpenWindow(windowProc, windowW, windowH)
	check(err)
	window := C.HWND(windowHandle)

	C.SetWindowText(
		window,
		(*C.WCHAR)(syscall.StringToUTF16Ptr("Gophette's Adventure")),
	)

	check(mixer.Init())
	defer mixer.Close()

	C.ShowCursor(0)
	defer C.ShowCursor(1)

	check(d3d9.Init())
	defer d3d9.Close()

	d3d, err := d3d9.Create(d3d9.SDK_VERSION)
	check(err)
	defer d3d.Release()

	var maxScreenW, maxScreenH uint
	for i := uint(0); i < d3d.GetAdapterCount(); i++ {
		mode, err := d3d.GetAdapterDisplayMode(i)
		if err == nil {
			if mode.Width > maxScreenW {
				maxScreenW = mode.Width
			}
			if mode.Height > maxScreenH {
				maxScreenH = mode.Height
			}
		}
	}
	if maxScreenW == 0 || maxScreenH == 0 {
		panic("no monitor detected")
	}

	device, _, err := d3d.CreateDevice(
		d3d9.ADAPTER_DEFAULT,
		d3d9.DEVTYPE_HAL,
		windowHandle,
		d3d9.CREATE_HARDWARE_VERTEXPROCESSING,
		d3d9.PRESENT_PARAMETERS{
			BackBufferWidth:  maxScreenW,
			BackBufferHeight: maxScreenH,
			BackBufferFormat: d3d9.FMT_A8R8G8B8,
			BackBufferCount:  1,
			Windowed:         true,
			SwapEffect:       d3d9.SWAPEFFECT_DISCARD,
			HDeviceWindow:    windowHandle,
		},
	)
	check(err)
	defer device.Release()

	check(device.SetRenderState(d3d9.RS_CULLMODE, uint32(d3d9.CULL_CW)))
	check(device.SetRenderState(d3d9.RS_SRCBLEND, d3d9.BLEND_SRCALPHA))
	check(device.SetRenderState(d3d9.RS_DESTBLEND, d3d9.BLEND_INVSRCALPHA))
	check(device.SetRenderState(d3d9.RS_ALPHABLENDENABLE, 1))

	camera = newWindowCamera(windowW, windowH)
	graphics := newWindowsGraphics(device, camera)
	defer graphics.close()

	assetLoader := newWindowsAssetLoader(device, graphics, camera)
	defer assetLoader.close()

	// charIndex selects which character is being controlled by the user, for
	// the final game this must be 0 but for creating the "AI" for Barney, set
	// this to 1 and delete the recorded inputs so they are not applied
	// additionally to the user controls

	const recordingAI = false // NOTE switch for development mode
	if !recordingAI {
		charIndex = 0
	} else {
		charIndex = 1
		recordedInputs = recordedInputs[:0]
		recordingInput = true
	}

	game = NewGame(
		assetLoader,
		graphics,
		camera,
		charIndex,
	)

	music := assetLoader.LoadSound("music_wav")
	go func() {
		for {
			music.PlayOnce()
			time.Sleep(music.Length() + 5*time.Second)
		}
	}()

	toggleFullscreen(window)

	frameTime := time.Second / 65
	lastUpdate := time.Now().Add(-frameTime)

	var msg C.MSG
	C.PeekMessage(&msg, nil, 0, 0, C.PM_NOREMOVE)
	for msg.message != C.WM_QUIT {
		if C.PeekMessage(&msg, nil, 0, 0, C.PM_REMOVE) != 0 {
			C.TranslateMessage(&msg)
			C.DispatchMessage(&msg)
		} else {
			now := time.Now()
			dt := now.Sub(lastUpdate)
			if dt > frameTime {
				game.Update()
				lastUpdate = now
			}

			check(device.SetViewport(
				d3d9.VIEWPORT{0, 0, uint32(windowW), uint32(windowH), 0, 1},
			))
			check(device.Clear(
				nil,
				d3d9.CLEAR_TARGET,
				d3d9.ColorRGB(0, 95, 83),
				1,
				0,
			))
			game.Render()
			graphics.flush()
			check(device.Present(
				&d3d9.RECT{0, 0, int32(windowW), int32(windowH)},
				nil,
				nil,
				nil,
			))
		}
	}
}