Squashed 'external/gainput/' content from commit 2be0a50
git-subtree-dir: external/gainput git-subtree-split: 2be0a50089eafcc6fccb66142180082e48f27f4c
This commit is contained in:
37
samples/basic/CMakeLists.txt
Normal file
37
samples/basic/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
project(basicsample)
|
||||
|
||||
include_directories(../../lib/include/)
|
||||
|
||||
file(GLOB_RECURSE sources *.cpp)
|
||||
|
||||
if(APPLE)
|
||||
file(GLOB_RECURSE mmsources *.mm)
|
||||
endif()
|
||||
|
||||
if(ANDROID)
|
||||
add_library(basicsample SHARED ${sources})
|
||||
elseif(APPLE AND IOS)
|
||||
set(imagessources
|
||||
"${CMAKE_CURRENT_LIST_DIR}/../../extern/ios/Launch Screen.storyboard"
|
||||
)
|
||||
add_executable(basicsample "MACOSX_BUNDLE" ${sources} ${mmsources} ${imagessources})
|
||||
set_target_properties( basicsample PROPERTIES
|
||||
XCODE_ATTRIBUTE_INFOPLIST_FILE "${CMAKE_CURRENT_LIST_DIR}/../../extern/ios/Info.plist"
|
||||
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer"
|
||||
)
|
||||
target_link_libraries(basicsample ${UIKIT} ${QUARTZCORE})
|
||||
else()
|
||||
add_executable(basicsample WIN32 ${sources} ${mmsources})
|
||||
endif()
|
||||
|
||||
target_link_libraries(basicsample gainputstatic)
|
||||
|
||||
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
|
||||
target_link_libraries(basicsample X11 GL rt)
|
||||
elseif(WIN32)
|
||||
target_link_libraries(basicsample ${XINPUT} ws2_32)
|
||||
elseif(APPLE)
|
||||
target_link_libraries(basicsample ${FOUNDATION} ${IOKIT} ${APPKIT} ${GAME_CONTROLLER})
|
||||
endif()
|
||||
|
||||
106
samples/basic/basicsample_android.cpp
Normal file
106
samples/basic/basicsample_android.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
|
||||
#include <gainput/gainput.h>
|
||||
|
||||
#if defined(GAINPUT_PLATFORM_ANDROID)
|
||||
|
||||
#include <jni.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#include <GLES/gl.h>
|
||||
|
||||
#include <android/sensor.h>
|
||||
#include <android/log.h>
|
||||
#include <android_native_app_glue.h>
|
||||
|
||||
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "gainput", __VA_ARGS__))
|
||||
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "gainput", __VA_ARGS__))
|
||||
|
||||
|
||||
// Define your user buttons
|
||||
enum Button
|
||||
{
|
||||
ButtonMenu,
|
||||
ButtonConfirm
|
||||
};
|
||||
|
||||
|
||||
static bool doExit = false;
|
||||
|
||||
static int32_t MyHandleInput(struct android_app* app, AInputEvent* event)
|
||||
{
|
||||
// Forward input events to Gainput
|
||||
gainput::InputManager* inputManager = (gainput::InputManager*)app->userData;
|
||||
static bool resSet = false;
|
||||
if (!resSet)
|
||||
{
|
||||
inputManager->SetDisplaySize(ANativeWindow_getWidth(app->window), ANativeWindow_getHeight(app->window));
|
||||
resSet = true;
|
||||
}
|
||||
return inputManager->HandleInput(event);
|
||||
}
|
||||
|
||||
static void MyHandleCmd(struct android_app* app, int32_t cmd)
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case APP_CMD_SAVE_STATE:
|
||||
break;
|
||||
case APP_CMD_INIT_WINDOW:
|
||||
break;
|
||||
case APP_CMD_TERM_WINDOW:
|
||||
doExit = true;
|
||||
break;
|
||||
case APP_CMD_LOST_FOCUS:
|
||||
doExit = true;
|
||||
break;
|
||||
case APP_CMD_GAINED_FOCUS:
|
||||
// bring back a certain functionality, like monitoring the accelerometer
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void android_main(struct android_app* state)
|
||||
{
|
||||
app_dummy();
|
||||
|
||||
// Set up Gainput
|
||||
gainput::InputManager manager;
|
||||
manager.CreateDevice<gainput::InputDeviceTouch>();
|
||||
manager.CreateDevice<gainput::InputDeviceKeyboard>();
|
||||
manager.CreateDevice<gainput::InputDevicePad>();
|
||||
|
||||
// Make sure the app frowards input events to Gainput
|
||||
state->userData = &manager;
|
||||
state->onInputEvent = &MyHandleInput;
|
||||
state->onAppCmd = &MyHandleCmd;
|
||||
|
||||
while (!doExit)
|
||||
{
|
||||
// Update Gainput
|
||||
manager.Update();
|
||||
|
||||
int ident;
|
||||
int events;
|
||||
struct android_poll_source* source;
|
||||
|
||||
while (!doExit && (ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0)
|
||||
{
|
||||
if (source != NULL)
|
||||
{
|
||||
source->process(state, source);
|
||||
}
|
||||
|
||||
if (state->destroyRequested != 0)
|
||||
{
|
||||
doExit = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ANativeActivity_finish(state->activity);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
77
samples/basic/basicsample_android_generic.cpp
Normal file
77
samples/basic/basicsample_android_generic.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
#include <gainput/gainput.h>
|
||||
|
||||
#if defined(GAINPUT_PLATFORM_ANDROID)
|
||||
|
||||
#include <jni.h>
|
||||
#include <android/log.h>
|
||||
|
||||
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "gainput", __VA_ARGS__))
|
||||
|
||||
// Define your user buttons
|
||||
enum Button
|
||||
{
|
||||
ButtonMenu,
|
||||
ButtonConfirm,
|
||||
MouseX,
|
||||
MouseY
|
||||
};
|
||||
|
||||
gainput::InputManager* manager;
|
||||
gainput::InputMap* map;
|
||||
gainput::DeviceId mouseId;
|
||||
gainput::DeviceId keyboardId;
|
||||
gainput::DeviceId padId;
|
||||
gainput::DeviceId touchId;
|
||||
|
||||
|
||||
extern "C" {
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_example_gainput_gainput_BasicActivity_nativeOnCreate(JNIEnv * /*env*/, jobject /*thiz*/)
|
||||
{
|
||||
manager = new gainput::InputManager;
|
||||
mouseId = manager->CreateDevice<gainput::InputDeviceMouse>();
|
||||
keyboardId = manager->CreateDevice<gainput::InputDeviceKeyboard>();
|
||||
padId = manager->CreateDevice<gainput::InputDevicePad>();
|
||||
touchId = manager->CreateDevice<gainput::InputDeviceTouch>();
|
||||
|
||||
map = new gainput::InputMap(*manager);
|
||||
map->MapBool(ButtonMenu, keyboardId, gainput::KeyBack);
|
||||
map->MapBool(ButtonConfirm, mouseId, gainput::MouseButtonLeft);
|
||||
map->MapBool(ButtonConfirm, padId, gainput::PadButtonA);
|
||||
map->MapBool(ButtonConfirm, touchId, gainput::Touch0Down);
|
||||
|
||||
map->MapFloat(MouseX, mouseId, gainput::MouseAxisX);
|
||||
map->MapFloat(MouseY, mouseId, gainput::MouseAxisY);
|
||||
map->MapFloat(MouseX, touchId, gainput::Touch0X);
|
||||
map->MapFloat(MouseY, touchId, gainput::Touch0Y);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_example_gainput_gainput_BasicActivity_nativeOnUpdate(JNIEnv * /*env*/, jobject /*thiz*/)
|
||||
{
|
||||
manager->Update();
|
||||
|
||||
if (map->GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
LOGI("Open Menu!!");
|
||||
}
|
||||
if (map->GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
LOGI("Confirmed!!");
|
||||
}
|
||||
if (map->GetBool(ButtonConfirm))
|
||||
{
|
||||
LOGI("LM down");
|
||||
}
|
||||
|
||||
if (map->GetFloatDelta(MouseX) != 0.0f || map->GetFloatDelta(MouseY) != 0.0f)
|
||||
{
|
||||
LOGI("Mouse: %f, %f", map->GetFloat(MouseX), map->GetFloat(MouseY));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
114
samples/basic/basicsample_ios.mm
Normal file
114
samples/basic/basicsample_ios.mm
Normal file
@@ -0,0 +1,114 @@
|
||||
|
||||
#include <gainput/gainput.h>
|
||||
|
||||
#if defined(GAINPUT_PLATFORM_IOS)
|
||||
#include <iostream>
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#include <gainput/GainputIos.h>
|
||||
|
||||
|
||||
// Define your user buttons
|
||||
enum Button
|
||||
{
|
||||
ButtonMenu,
|
||||
ButtonConfirm,
|
||||
MouseX,
|
||||
MouseY
|
||||
};
|
||||
|
||||
gainput::InputManager* manager;
|
||||
gainput::InputMap* map;
|
||||
gainput::DeviceId mouseId;
|
||||
gainput::DeviceId keyboardId;
|
||||
gainput::DeviceId padId;
|
||||
gainput::DeviceId touchId;
|
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
@end
|
||||
|
||||
@implementation AppDelegate
|
||||
CADisplayLink* displayLink;
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||
manager = new gainput::InputManager;
|
||||
mouseId = manager->CreateDevice<gainput::InputDeviceMouse>();
|
||||
keyboardId = manager->CreateDevice<gainput::InputDeviceKeyboard>();
|
||||
padId = manager->CreateDevice<gainput::InputDevicePad>();
|
||||
touchId = manager->CreateDevice<gainput::InputDeviceTouch>();
|
||||
|
||||
map = new gainput::InputMap(*manager);
|
||||
map->MapBool(ButtonMenu, keyboardId, gainput::KeyEscape);
|
||||
map->MapBool(ButtonConfirm, mouseId, gainput::MouseButtonLeft);
|
||||
map->MapBool(ButtonConfirm, padId, gainput::PadButtonA);
|
||||
map->MapBool(ButtonConfirm, touchId, gainput::Touch0Down);
|
||||
|
||||
map->MapFloat(MouseX, mouseId, gainput::MouseAxisX);
|
||||
map->MapFloat(MouseY, mouseId, gainput::MouseAxisY);
|
||||
map->MapFloat(MouseX, touchId, gainput::Touch0X);
|
||||
map->MapFloat(MouseY, touchId, gainput::Touch0Y);
|
||||
|
||||
|
||||
CGRect bounds = [[UIScreen mainScreen] bounds];
|
||||
self.window = [[[UIWindow alloc] init] autorelease];
|
||||
self.window.rootViewController = [[[UIViewController alloc] init] autorelease];
|
||||
self.window.backgroundColor = [UIColor blueColor];
|
||||
[self.window makeKeyAndVisible];
|
||||
[application setIdleTimerDisabled:TRUE];
|
||||
|
||||
UIView* gainputView = [[[GainputView alloc] initWithFrame:bounds inputManager:*manager] autorelease];
|
||||
self.window.rootViewController.view = gainputView;
|
||||
|
||||
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
|
||||
NSAssert(displayLink, @"Failed to create display link.");
|
||||
[displayLink setFrameInterval: 1];
|
||||
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)update
|
||||
{
|
||||
manager->Update();
|
||||
|
||||
// Check button states
|
||||
if (map->GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
gainput::InputDevicePad* pad = static_cast<gainput::InputDevicePad*>(manager->GetDevice(padId));
|
||||
pad->Vibrate(1.0f, 0.0f);
|
||||
}
|
||||
if (map->GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
gainput::InputDevicePad* pad = static_cast<gainput::InputDevicePad*>(manager->GetDevice(padId));
|
||||
pad->Vibrate(0.0f, 0.0f);
|
||||
}
|
||||
|
||||
if (map->GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
std::cout << "Open Menu!!" << std::endl;
|
||||
}
|
||||
if (map->GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
std::cout << "Confirmed!!" << std::endl;
|
||||
}
|
||||
if (map->GetBool(ButtonConfirm))
|
||||
{
|
||||
std::cout << "LM down" << std::endl;
|
||||
}
|
||||
|
||||
if (map->GetFloatDelta(MouseX) != 0.0f || map->GetFloatDelta(MouseY) != 0.0f)
|
||||
{
|
||||
std::cout << "Mouse:" << map->GetFloat(MouseX) << ", " << map->GetFloat(MouseY) << std::endl;
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
@autoreleasepool
|
||||
{
|
||||
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
119
samples/basic/basicsample_linux.cpp
Normal file
119
samples/basic/basicsample_linux.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
|
||||
#include <gainput/gainput.h>
|
||||
|
||||
#if defined(GAINPUT_PLATFORM_LINUX)
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <GL/glx.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
// Define your user buttons
|
||||
enum Button
|
||||
{
|
||||
ButtonMenu,
|
||||
ButtonConfirm,
|
||||
MouseX,
|
||||
MouseY
|
||||
};
|
||||
|
||||
|
||||
const char* windowName = "Gainput basic sample";
|
||||
const int width = 800;
|
||||
const int height = 600;
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
static int attributeListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER, /*In case single buffering is not supported*/ GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
|
||||
None };
|
||||
|
||||
Display* xDisplay = XOpenDisplay(0);
|
||||
if (xDisplay == 0)
|
||||
{
|
||||
std::cerr << "Cannot connect to X server." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Window root = DefaultRootWindow(xDisplay);
|
||||
|
||||
XVisualInfo* vi = glXChooseVisual(xDisplay, DefaultScreen(xDisplay), attributeListDbl);
|
||||
assert(vi);
|
||||
|
||||
GLXContext context = glXCreateContext(xDisplay, vi, 0, GL_TRUE);
|
||||
|
||||
Colormap cmap = XCreateColormap(xDisplay, root, vi->visual, AllocNone);
|
||||
|
||||
XSetWindowAttributes swa;
|
||||
swa.colormap = cmap;
|
||||
swa.event_mask = ExposureMask
|
||||
| KeyPressMask | KeyReleaseMask
|
||||
| PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
|
||||
|
||||
Window xWindow = XCreateWindow(
|
||||
xDisplay, root,
|
||||
0, 0, width, height, 0,
|
||||
CopyFromParent, InputOutput,
|
||||
CopyFromParent, CWEventMask,
|
||||
&swa
|
||||
);
|
||||
|
||||
glXMakeCurrent(xDisplay, xWindow, context);
|
||||
|
||||
XSetWindowAttributes xattr;
|
||||
xattr.override_redirect = False;
|
||||
XChangeWindowAttributes(xDisplay, xWindow, CWOverrideRedirect, &xattr);
|
||||
|
||||
XMapWindow(xDisplay, xWindow);
|
||||
XStoreName(xDisplay, xWindow, windowName);
|
||||
|
||||
// Setup Gainput
|
||||
gainput::InputManager manager;
|
||||
const gainput::DeviceId mouseId = manager.CreateDevice<gainput::InputDeviceMouse>();
|
||||
const gainput::DeviceId keyboardId = manager.CreateDevice<gainput::InputDeviceKeyboard>();
|
||||
const gainput::DeviceId padId = manager.CreateDevice<gainput::InputDevicePad>();
|
||||
|
||||
gainput::InputMap map(manager);
|
||||
map.MapBool(ButtonMenu, keyboardId, gainput::KeyEscape);
|
||||
map.MapBool(ButtonConfirm, mouseId, gainput::MouseButtonLeft);
|
||||
map.MapFloat(MouseX, mouseId, gainput::MouseAxisX);
|
||||
map.MapFloat(MouseY, mouseId, gainput::MouseAxisY);
|
||||
map.MapBool(ButtonConfirm, padId, gainput::PadButtonA);
|
||||
|
||||
manager.SetDisplaySize(width, height);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
// Update Gainput
|
||||
manager.Update();
|
||||
|
||||
XEvent event;
|
||||
while (XPending(xDisplay))
|
||||
{
|
||||
XNextEvent(xDisplay, &event);
|
||||
manager.HandleEvent(event);
|
||||
}
|
||||
|
||||
// Check button states
|
||||
if (map.GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
std::cout << "Open menu!!" << std::endl;
|
||||
}
|
||||
if (map.GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
std::cout << "Confirmed!!" << std::endl;
|
||||
}
|
||||
|
||||
if (map.GetFloatDelta(MouseX) != 0.0f || map.GetFloatDelta(MouseY) != 0.0f)
|
||||
{
|
||||
std::cout << "Mouse: " << map.GetFloat(MouseX) << ", " << map.GetFloat(MouseY) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
XDestroyWindow(xDisplay, xWindow);
|
||||
XCloseDisplay(xDisplay);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
106
samples/basic/basicsample_mac.mm
Normal file
106
samples/basic/basicsample_mac.mm
Normal file
@@ -0,0 +1,106 @@
|
||||
|
||||
#include <gainput/gainput.h>
|
||||
|
||||
#if defined(GAINPUT_PLATFORM_MAC)
|
||||
#include <iostream>
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
// Define your user buttons
|
||||
enum Button
|
||||
{
|
||||
ButtonMenu,
|
||||
ButtonConfirm,
|
||||
MouseX,
|
||||
MouseY
|
||||
};
|
||||
|
||||
|
||||
const char* windowName = "Gainput basic sample";
|
||||
const int width = 800;
|
||||
const int height = 600;
|
||||
|
||||
gainput::InputManager* manager;
|
||||
gainput::InputMap* map;
|
||||
gainput::DeviceId mouseId;
|
||||
gainput::DeviceId keyboardId;
|
||||
gainput::DeviceId padId;
|
||||
|
||||
@interface Updater : NSObject
|
||||
@end
|
||||
|
||||
@implementation Updater
|
||||
- (void)update:(NSTimer *)theTimer
|
||||
{
|
||||
manager->Update();
|
||||
|
||||
// Check button states
|
||||
if (map->GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
gainput::InputDevicePad* pad = static_cast<gainput::InputDevicePad*>(manager->GetDevice(padId));
|
||||
pad->Vibrate(1.0f, 0.0f);
|
||||
}
|
||||
if (map->GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
gainput::InputDevicePad* pad = static_cast<gainput::InputDevicePad*>(manager->GetDevice(padId));
|
||||
pad->Vibrate(0.0f, 0.0f);
|
||||
}
|
||||
|
||||
if (map->GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
std::cout << "Open Menu!!" << std::endl;
|
||||
}
|
||||
if (map->GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
std::cout << "Confirmed!!" << std::endl;
|
||||
}
|
||||
if (map->GetBool(ButtonConfirm))
|
||||
{
|
||||
std::cout << "LM down" << std::endl;
|
||||
}
|
||||
|
||||
if (map->GetFloatDelta(MouseX) != 0.0f || map->GetFloatDelta(MouseY) != 0.0f)
|
||||
{
|
||||
std::cout << "Mouse:" << map->GetFloat(MouseX) << ", " << map->GetFloat(MouseY) << std::endl;
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
[NSApplication sharedApplication];
|
||||
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
||||
id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height)
|
||||
styleMask:NSTitledWindowMask|NSClosableWindowMask backing:NSBackingStoreBuffered defer:NO];
|
||||
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
|
||||
[window setTitle: @"Gainput Basic Sample"];
|
||||
[window makeKeyAndOrderFront:nil];
|
||||
|
||||
manager = new gainput::InputManager;
|
||||
manager->SetDisplaySize(width, height);
|
||||
mouseId = manager->CreateDevice<gainput::InputDeviceMouse>();
|
||||
keyboardId = manager->CreateDevice<gainput::InputDeviceKeyboard>();
|
||||
padId = manager->CreateDevice<gainput::InputDevicePad>();
|
||||
|
||||
map = new gainput::InputMap(*manager);
|
||||
map->MapBool(ButtonMenu, keyboardId, gainput::KeyEscape);
|
||||
map->MapBool(ButtonConfirm, mouseId, gainput::MouseButtonLeft);
|
||||
map->MapFloat(MouseX, mouseId, gainput::MouseAxisX);
|
||||
map->MapFloat(MouseY, mouseId, gainput::MouseAxisY);
|
||||
map->MapBool(ButtonConfirm, padId, gainput::PadButtonA);
|
||||
|
||||
Updater* updater = [[Updater alloc] init];
|
||||
|
||||
[NSTimer scheduledTimerWithTimeInterval:0.016
|
||||
target:updater
|
||||
selector:@selector(update:)
|
||||
userInfo:nil
|
||||
repeats:YES];
|
||||
|
||||
// [NSApp activateIgnoringOtherApps:YES];
|
||||
[NSApp run];
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
159
samples/basic/basicsample_win.cpp
Normal file
159
samples/basic/basicsample_win.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
|
||||
#include <gainput/gainput.h>
|
||||
|
||||
#if defined(GAINPUT_PLATFORM_WIN)
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#define LOG(...) {char buf[256]; sprintf(buf, __VA_ARGS__); OutputDebugStringA(buf); }
|
||||
|
||||
|
||||
// Define your user buttons
|
||||
enum Button
|
||||
{
|
||||
ButtonMenu,
|
||||
ButtonConfirm,
|
||||
MouseX,
|
||||
MouseY
|
||||
};
|
||||
|
||||
|
||||
static char szWindowClass[] = "win32app";
|
||||
const char* windowName = "Gainput basic sample";
|
||||
const int width = 800;
|
||||
const int height = 600;
|
||||
bool doExit = false;
|
||||
|
||||
|
||||
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
PAINTSTRUCT ps;
|
||||
HDC hdc;
|
||||
char greeting[] = "Hello, World!";
|
||||
|
||||
switch (message)
|
||||
{
|
||||
case WM_PAINT:
|
||||
hdc = BeginPaint(hWnd, &ps);
|
||||
TextOut(hdc, 5, 5, greeting, strlen(greeting));
|
||||
EndPaint(hWnd, &ps);
|
||||
break;
|
||||
case WM_DESTROY:
|
||||
PostQuitMessage(0);
|
||||
doExit = true;
|
||||
break;
|
||||
default:
|
||||
return DefWindowProc(hWnd, message, wParam, lParam);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
||||
{
|
||||
WNDCLASSEX wcex;
|
||||
|
||||
wcex.cbSize = sizeof(WNDCLASSEX);
|
||||
wcex.style = CS_HREDRAW | CS_VREDRAW;
|
||||
wcex.lpfnWndProc = WndProc;
|
||||
wcex.cbClsExtra = 0;
|
||||
wcex.cbWndExtra = 0;
|
||||
wcex.hInstance = hInstance;
|
||||
wcex.hIcon = 0;
|
||||
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
|
||||
wcex.lpszMenuName = NULL;
|
||||
wcex.lpszClassName = szWindowClass;
|
||||
wcex.hIconSm = 0;
|
||||
|
||||
if (!RegisterClassEx(&wcex))
|
||||
{
|
||||
MessageBox(NULL, "Call to RegisterClassEx failed!", "Gainput basic sample", NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
HWND hWnd = CreateWindow(
|
||||
szWindowClass,
|
||||
windowName,
|
||||
WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
width, height,
|
||||
NULL,
|
||||
NULL,
|
||||
hInstance,
|
||||
NULL
|
||||
);
|
||||
|
||||
if (!hWnd)
|
||||
{
|
||||
MessageBox(NULL, "Call to CreateWindow failed!", "Gainput basic sample", NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ShowWindow(hWnd, nCmdShow);
|
||||
UpdateWindow(hWnd);
|
||||
|
||||
// Setup Gainput
|
||||
gainput::InputManager manager;
|
||||
manager.SetDisplaySize(width, height);
|
||||
gainput::DeviceId mouseId = manager.CreateDevice<gainput::InputDeviceMouse>();
|
||||
gainput::DeviceId keyboardId = manager.CreateDevice<gainput::InputDeviceKeyboard>();
|
||||
gainput::DeviceId padId = manager.CreateDevice<gainput::InputDevicePad>();
|
||||
|
||||
gainput::InputMap map(manager);
|
||||
map.MapBool(ButtonMenu, keyboardId, gainput::KeyEscape);
|
||||
map.MapBool(ButtonConfirm, mouseId, gainput::MouseButtonLeft);
|
||||
map.MapFloat(MouseX, mouseId, gainput::MouseAxisX);
|
||||
map.MapFloat(MouseY, mouseId, gainput::MouseAxisY);
|
||||
map.MapBool(ButtonConfirm, padId, gainput::PadButtonA);
|
||||
|
||||
while (!doExit)
|
||||
{
|
||||
// Update Gainput
|
||||
manager.Update();
|
||||
|
||||
MSG msg;
|
||||
while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
|
||||
// Forward any input messages to Gainput
|
||||
manager.HandleMessage(msg);
|
||||
}
|
||||
|
||||
// Check button states
|
||||
if (map.GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
gainput::InputDevicePad* pad = static_cast<gainput::InputDevicePad*>(manager.GetDevice(padId));
|
||||
pad->Vibrate(1.0f, 0.0f);
|
||||
}
|
||||
if (map.GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
gainput::InputDevicePad* pad = static_cast<gainput::InputDevicePad*>(manager.GetDevice(padId));
|
||||
pad->Vibrate(0.0f, 0.0f);
|
||||
}
|
||||
|
||||
if (map.GetBoolWasDown(ButtonMenu))
|
||||
{
|
||||
LOG("Open Menu!!\n");
|
||||
}
|
||||
if (map.GetBoolWasDown(ButtonConfirm))
|
||||
{
|
||||
LOG("Confirmed!!\n");
|
||||
}
|
||||
|
||||
if (map.GetFloatDelta(MouseX) != 0.0f || map.GetFloatDelta(MouseY) != 0.0f)
|
||||
{
|
||||
LOG("Mouse: %f, %f\n", map.GetFloat(MouseX), map.GetFloat(MouseY));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user