mirror of
https://github.com/NoelFB/blah.git
synced 2025-07-15 18:51:53 +08:00
simplifying input backend & input state update
This commit is contained in:
@ -27,7 +27,7 @@ namespace Blah
|
||||
Renderer renderer();
|
||||
|
||||
// Called once per frame
|
||||
void frame();
|
||||
void update();
|
||||
|
||||
// Called before rendering begins
|
||||
void before_render();
|
||||
|
@ -792,7 +792,7 @@ namespace Blah
|
||||
return state.features;
|
||||
}
|
||||
|
||||
void GraphicsBackend::frame()
|
||||
void GraphicsBackend::update()
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -177,7 +177,7 @@ namespace Blah
|
||||
return features;
|
||||
}
|
||||
|
||||
void GraphicsBackend::frame() {}
|
||||
void GraphicsBackend::update() {}
|
||||
void GraphicsBackend::before_render() {}
|
||||
void GraphicsBackend::after_render() {}
|
||||
|
||||
|
@ -1159,7 +1159,7 @@ namespace Blah
|
||||
return gl.features;
|
||||
}
|
||||
|
||||
void GraphicsBackend::frame() {}
|
||||
void GraphicsBackend::update() {}
|
||||
void GraphicsBackend::before_render() {}
|
||||
void GraphicsBackend::after_render() {}
|
||||
|
||||
|
@ -5,50 +5,13 @@ namespace Blah
|
||||
{
|
||||
namespace InputBackend
|
||||
{
|
||||
// This is called internally by the app, and initializes the input state
|
||||
// Initializes the Input State
|
||||
void init();
|
||||
|
||||
// This is called internally by the app, and updates the input state
|
||||
void frame();
|
||||
// Steps the input state
|
||||
void update_state();
|
||||
|
||||
// Call this when the Mouse moves relative to the window
|
||||
void on_mouse_move(float x, float y);
|
||||
|
||||
// Call this when the Mouse moves relative to the screen
|
||||
void on_mouse_screen_move(float x, float y);
|
||||
|
||||
// Call this when a Mouse Button is pressed
|
||||
void on_mouse_down(MouseButton button);
|
||||
|
||||
// Call this when a Mouse Button is released
|
||||
void on_mouse_up(MouseButton button);
|
||||
|
||||
// Call this when the Mouse Wheel moves
|
||||
void on_mouse_wheel(Point wheel);
|
||||
|
||||
// Call this when a keyboard key is pressed
|
||||
void on_key_down(Key key);
|
||||
|
||||
// Call this when a keyboard key is released
|
||||
void on_key_up(Key key);
|
||||
|
||||
// Call this on Text Input
|
||||
void on_text_utf8(const char* text);
|
||||
|
||||
// Call this when a Controller is connected. Note that the Name parameter must be kept valid
|
||||
// until on_controller_disconnect is called with the same index.
|
||||
void on_controller_connect(int index, const char* name, int isGamepad, int buttonCount, int axisCount, u16 vendor, u16 product, u16 version);
|
||||
|
||||
// Call this when a controller is disconnected
|
||||
void on_controller_disconnect(int index);
|
||||
|
||||
// Call this when a controller button is pressed
|
||||
void on_button_down(int index, int button);
|
||||
|
||||
// Call this when a controller button is released
|
||||
void on_button_up(int index, int button);
|
||||
|
||||
/// Call this when a controller axis is moved
|
||||
void on_axis_move(int index, int axis, float value);
|
||||
// Updates bindings
|
||||
void update_bindings();
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <blah/filesystem.h>
|
||||
#include <blah/input.h>
|
||||
#include <blah/containers/vector.h>
|
||||
|
||||
namespace Blah
|
||||
@ -25,7 +26,7 @@ namespace Blah
|
||||
u64 ticks();
|
||||
|
||||
// Called every frame
|
||||
void frame();
|
||||
void update(InputState& state);
|
||||
|
||||
// Sleeps the current thread
|
||||
void sleep(int milliseconds);
|
||||
|
@ -164,11 +164,6 @@ namespace Blah
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
|
||||
#endif
|
||||
}
|
||||
// enable DirectX
|
||||
else if (App::renderer() == Renderer::D3D11)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// create the window
|
||||
@ -246,16 +241,18 @@ namespace Blah
|
||||
// Macro defined by X11 conflicts with MouseButton enum
|
||||
#undef None
|
||||
|
||||
void PlatformBackend::frame()
|
||||
void PlatformBackend::update(InputState& state)
|
||||
{
|
||||
// update the mouse every frame
|
||||
{
|
||||
int winX, winY, x, y;
|
||||
SDL_GetWindowPosition(window, &winX, &winY);
|
||||
int win_x, win_y, x, y;
|
||||
|
||||
SDL_GetWindowPosition(window, &win_x, &win_y);
|
||||
SDL_GetGlobalMouseState(&x, &y);
|
||||
|
||||
InputBackend::on_mouse_move((float)(x - winX), (float)(y - winY));
|
||||
InputBackend::on_mouse_screen_move((float)x, (float)y);
|
||||
state.mouse.on_move(
|
||||
Vec2((float)(x - win_x), (float)(y - win_y)),
|
||||
Vec2((float)x, (float)y));
|
||||
}
|
||||
|
||||
// poll normal events
|
||||
@ -278,7 +275,8 @@ namespace Blah
|
||||
btn = MouseButton::Right;
|
||||
else if (event.button.button == SDL_BUTTON_MIDDLE)
|
||||
btn = MouseButton::Middle;
|
||||
InputBackend::on_mouse_down(btn);
|
||||
|
||||
state.mouse.on_press(btn);
|
||||
}
|
||||
else if (event.type == SDL_MOUSEBUTTONUP)
|
||||
{
|
||||
@ -289,26 +287,27 @@ namespace Blah
|
||||
btn = MouseButton::Right;
|
||||
else if (event.button.button == SDL_BUTTON_MIDDLE)
|
||||
btn = MouseButton::Middle;
|
||||
InputBackend::on_mouse_up(btn);
|
||||
|
||||
state.mouse.on_release(btn);
|
||||
}
|
||||
else if (event.type == SDL_MOUSEWHEEL)
|
||||
{
|
||||
InputBackend::on_mouse_wheel(Point(event.wheel.x, event.wheel.y));
|
||||
state.mouse.wheel = Point(event.wheel.x, event.wheel.y);
|
||||
}
|
||||
// Keyboard
|
||||
else if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
if (event.key.repeat == 0)
|
||||
InputBackend::on_key_down((Key)event.key.keysym.scancode);
|
||||
state.keyboard.on_press((Key)event.key.keysym.scancode);
|
||||
}
|
||||
else if (event.type == SDL_KEYUP)
|
||||
{
|
||||
if (event.key.repeat == 0)
|
||||
InputBackend::on_key_up((Key)event.key.keysym.scancode);
|
||||
state.keyboard.on_release((Key)event.key.keysym.scancode);
|
||||
}
|
||||
else if (event.type == SDL_TEXTINPUT)
|
||||
{
|
||||
InputBackend::on_text_utf8(event.text.text);
|
||||
state.keyboard.text += event.text.text;
|
||||
}
|
||||
// Joystick Controller
|
||||
else if (event.type == SDL_JOYDEVICEADDED)
|
||||
@ -325,7 +324,7 @@ namespace Blah
|
||||
auto product = SDL_JoystickGetProduct(ptr);
|
||||
auto version = SDL_JoystickGetProductVersion(ptr);
|
||||
|
||||
InputBackend::on_controller_connect(index, name, 0, button_count, axis_count, vendor, product, version);
|
||||
state.controllers[index].on_connect(name, 0, button_count, axis_count, vendor, product, version);
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_JOYDEVICEREMOVED)
|
||||
@ -335,7 +334,7 @@ namespace Blah
|
||||
{
|
||||
if (SDL_IsGameController(index) == SDL_FALSE)
|
||||
{
|
||||
InputBackend::on_controller_disconnect(index);
|
||||
state.controllers[index].on_disconnect();
|
||||
SDL_JoystickClose(joysticks[index]);
|
||||
}
|
||||
}
|
||||
@ -346,7 +345,7 @@ namespace Blah
|
||||
if (index >= 0)
|
||||
{
|
||||
if (SDL_IsGameController(index) == SDL_FALSE)
|
||||
InputBackend::on_button_down(index, event.jbutton.button);
|
||||
state.controllers[index].on_press((Button)event.jbutton.button);
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_JOYBUTTONUP)
|
||||
@ -355,7 +354,7 @@ namespace Blah
|
||||
if (index >= 0)
|
||||
{
|
||||
if (SDL_IsGameController(index) == SDL_FALSE)
|
||||
InputBackend::on_button_up(index, event.jbutton.button);
|
||||
state.controllers[index].on_release((Button)event.jbutton.button);
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_JOYAXISMOTION)
|
||||
@ -370,7 +369,7 @@ namespace Blah
|
||||
value = event.jaxis.value / 32767.0f;
|
||||
else
|
||||
value = event.jaxis.value / 32768.0f;
|
||||
InputBackend::on_axis_move(index, event.jaxis.axis, value);
|
||||
state.controllers[index].on_axis((Axis)event.jaxis.axis, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -386,7 +385,7 @@ namespace Blah
|
||||
auto product = SDL_GameControllerGetProduct(ptr);
|
||||
auto version = SDL_GameControllerGetProductVersion(ptr);
|
||||
|
||||
InputBackend::on_controller_connect(index, name, 1, 15, 6, vendor, product, version);
|
||||
state.controllers[index].on_connect(name, 1, 15, 6, vendor, product, version);
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_CONTROLLERDEVICEREMOVED)
|
||||
@ -394,7 +393,7 @@ namespace Blah
|
||||
auto index = find_gamepad_index(event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
InputBackend::on_controller_disconnect(index);
|
||||
state.controllers[index].on_disconnect();
|
||||
SDL_GameControllerClose(gamepads[index]);
|
||||
}
|
||||
}
|
||||
@ -403,11 +402,11 @@ namespace Blah
|
||||
auto index = find_gamepad_index(event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
int button = (int)Button::None;
|
||||
Button button = Button::None;
|
||||
if (event.cbutton.button >= 0 && event.cbutton.button < 15)
|
||||
button = event.cbutton.button; // NOTE: These map directly to Engine Buttons enum!
|
||||
button = (Button)event.cbutton.button; // NOTE: These map directly to Engine Buttons enum!
|
||||
|
||||
InputBackend::on_button_down(index, button);
|
||||
state.controllers[index].on_press(button);
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_CONTROLLERBUTTONUP)
|
||||
@ -415,11 +414,11 @@ namespace Blah
|
||||
auto index = find_gamepad_index(event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
int button = (int)Button::None;
|
||||
Button button = Button::None;
|
||||
if (event.cbutton.button >= 0 && event.cbutton.button < 15)
|
||||
button = event.cbutton.button; // NOTE: These map directly to Engine Buttons enum!
|
||||
button = (Button)event.cbutton.button; // NOTE: These map directly to Engine Buttons enum!
|
||||
|
||||
InputBackend::on_button_up(index, button);
|
||||
state.controllers[index].on_release(button);
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_CONTROLLERAXISMOTION)
|
||||
@ -427,9 +426,9 @@ namespace Blah
|
||||
auto index = find_gamepad_index(event.cdevice.which);
|
||||
if (index >= 0)
|
||||
{
|
||||
int axis = (int)Axis::None;
|
||||
Axis axis = Axis::None;
|
||||
if (event.caxis.axis >= 0 && event.caxis.axis < 6)
|
||||
axis = event.caxis.axis; // NOTE: These map directly to Engine Axis enum!
|
||||
axis = (Axis)event.caxis.axis; // NOTE: These map directly to Engine Axis enum!
|
||||
|
||||
float value;
|
||||
if (event.caxis.value >= 0)
|
||||
@ -437,7 +436,7 @@ namespace Blah
|
||||
else
|
||||
value = event.caxis.value / 32768.0f;
|
||||
|
||||
InputBackend::on_axis_move(index, axis, value);
|
||||
state.controllers[index].on_axis(axis, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,6 +50,9 @@ namespace Blah
|
||||
RECT g_windowed_position;
|
||||
bool g_fullscreen = false;
|
||||
|
||||
// current input state
|
||||
InputState* g_input_state = nullptr;
|
||||
|
||||
// Converts Windows scancode to Blah key
|
||||
Key scancode_to_key(WPARAM wParam, LPARAM lParam);
|
||||
|
||||
@ -329,35 +332,35 @@ namespace Blah
|
||||
|
||||
// Mouse Input
|
||||
case WM_LBUTTONDOWN:
|
||||
InputBackend::on_mouse_down(MouseButton::Left);
|
||||
g_input_state->mouse.on_press(MouseButton::Left);
|
||||
return 0;
|
||||
|
||||
case WM_LBUTTONUP:
|
||||
InputBackend::on_mouse_up(MouseButton::Left);
|
||||
g_input_state->mouse.on_release(MouseButton::Left);
|
||||
return 0;
|
||||
|
||||
case WM_RBUTTONDOWN:
|
||||
InputBackend::on_mouse_down(MouseButton::Right);
|
||||
g_input_state->mouse.on_press(MouseButton::Right);
|
||||
return 0;
|
||||
|
||||
case WM_RBUTTONUP:
|
||||
InputBackend::on_mouse_up(MouseButton::Right);
|
||||
g_input_state->mouse.on_release(MouseButton::Right);
|
||||
return 0;
|
||||
|
||||
case WM_MBUTTONDOWN:
|
||||
InputBackend::on_mouse_down(MouseButton::Middle);
|
||||
g_input_state->mouse.on_press(MouseButton::Middle);
|
||||
return 0;
|
||||
|
||||
case WM_MBUTTONUP:
|
||||
InputBackend::on_mouse_up(MouseButton::Middle);
|
||||
g_input_state->mouse.on_release(MouseButton::Middle);
|
||||
return 0;
|
||||
|
||||
case WM_MOUSEMOVE:
|
||||
InputBackend::on_mouse_move((float)((u16)lParam), (float)(lParam >> 16));
|
||||
g_input_state->mouse.on_move(Vec2((float)((u16)lParam), (float)(lParam >> 16)), Vec2::zero);
|
||||
return 0;
|
||||
|
||||
case WM_MOUSEWHEEL:
|
||||
InputBackend::on_mouse_wheel(Point(0, GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA));
|
||||
g_input_state->mouse.wheel = Point(0, GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA);
|
||||
return 0;
|
||||
|
||||
// Text Input
|
||||
@ -369,7 +372,7 @@ namespace Blah
|
||||
String result;
|
||||
result.append((u32)wParam);
|
||||
if (result.length() > 0)
|
||||
InputBackend::on_text_utf8(result.cstr());
|
||||
g_input_state->keyboard.text += result.cstr();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -382,7 +385,7 @@ namespace Blah
|
||||
{
|
||||
auto key = scancode_to_key(wParam, lParam);
|
||||
if (key != Key::Unknown)
|
||||
InputBackend::on_key_down(key);
|
||||
g_input_state->keyboard.on_press(key);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -392,7 +395,7 @@ namespace Blah
|
||||
{
|
||||
auto key = scancode_to_key(wParam, lParam);
|
||||
if (key != Key::Unknown)
|
||||
InputBackend::on_key_up(key);
|
||||
g_input_state->keyboard.on_release(key);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -400,8 +403,10 @@ namespace Blah
|
||||
return DefWindowProc(hwnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
void PlatformBackend::frame()
|
||||
void PlatformBackend::update(InputState& state)
|
||||
{
|
||||
g_input_state = &state;
|
||||
|
||||
// Catch & Dispatch Window Messages
|
||||
MSG msg;
|
||||
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
||||
@ -850,4 +855,4 @@ namespace Blah
|
||||
}
|
||||
}
|
||||
|
||||
#endif // BLAH_PLATFORM_WINDOWS
|
||||
#endif // BLAH_PLATFORM_WIN32
|
||||
|
Reference in New Issue
Block a user