-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_gamepad.cpp
More file actions
144 lines (125 loc) · 5.59 KB
/
Copy pathtest_gamepad.cpp
File metadata and controls
144 lines (125 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <nothofagus.h>
int main()
{
Nothofagus::ScreenSize screenSize{200, 150};
Nothofagus::Canvas canvas(screenSize, "Gamepad test", {0.15f, 0.15f, 0.2f}, 6);
Nothofagus::ColorPallete pallete{
{0.0, 0.0, 0.0, 0.0},
{0.2, 0.5, 0.9, 1.0},
{0.4, 0.7, 1.0, 1.0},
{0.6, 0.9, 1.0, 1.0},
};
Nothofagus::IndirectTexture texture({8, 8}, {0.5, 0.5, 0.5, 1.0});
texture.setPallete(pallete)
.setPixels(
{
0,0,3,3,3,3,0,0,
0,3,2,2,2,2,3,0,
3,2,1,2,2,1,2,3,
3,2,2,2,2,2,2,3,
3,2,2,2,2,2,2,3,
3,2,1,2,2,1,2,3,
0,3,2,2,2,2,3,0,
0,0,3,3,3,3,0,0,
}
);
Nothofagus::TextureId textureId = canvas.addTexture(texture);
Nothofagus::BellotaId bellotaId = canvas.addBellota({{{100.0f, 75.0f}}, textureId});
float time = 0.0f;
constexpr float horizontalSpeed = 0.12f;
constexpr float angularSpeed = 0.1f;
constexpr float discreteStep = 10.0f;
bool rotate = false;
float pulseTimer = 0.0f;
Nothofagus::Controller controller;
// Keyboard: Escape to quit
controller.registerAction({Nothofagus::Key::ESCAPE, Nothofagus::DiscreteTrigger::Press},
[&]() { canvas.close(); });
// Gamepad: A button pulse
controller.registerGamepadAction({0, Nothofagus::GamepadButton::A, Nothofagus::DiscreteTrigger::Press},
[&]() { pulseTimer = 500.0f; });
// Gamepad: Start toggles rotation
controller.registerGamepadAction({0, Nothofagus::GamepadButton::Start, Nothofagus::DiscreteTrigger::Press},
[&]() { rotate = not rotate; });
// Gamepad: D-pad discrete movement
controller.registerGamepadAction({0, Nothofagus::GamepadButton::DpadUp, Nothofagus::DiscreteTrigger::Press},
[&]() { canvas.bellota(bellotaId).transform().location().y += discreteStep; });
controller.registerGamepadAction({0, Nothofagus::GamepadButton::DpadDown, Nothofagus::DiscreteTrigger::Press},
[&]() { canvas.bellota(bellotaId).transform().location().y -= discreteStep; });
controller.registerGamepadAction({0, Nothofagus::GamepadButton::DpadLeft, Nothofagus::DiscreteTrigger::Press},
[&]() { canvas.bellota(bellotaId).transform().location().x -= discreteStep; });
controller.registerGamepadAction({0, Nothofagus::GamepadButton::DpadRight, Nothofagus::DiscreteTrigger::Press},
[&]() { canvas.bellota(bellotaId).transform().location().x += discreteStep; });
auto update = [&](float deltaTime)
{
time += deltaTime;
Nothofagus::Bellota& bellota = canvas.bellota(bellotaId);
// Smooth movement via left stick (polling)
std::vector<int> connectedIds = controller.getConnectedGamepadIds();
if (not connectedIds.empty())
{
int gamepadId = connectedIds[0];
float leftX = controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::LeftX);
float leftY = controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::LeftY);
bellota.transform().location().x += leftX * horizontalSpeed * deltaTime;
bellota.transform().location().y += leftY * horizontalSpeed * deltaTime;
}
// Clamp to screen
bellota.transform().location().x = std::clamp(bellota.transform().location().x, 10.0f, static_cast<float>(screenSize.width) - 10.0f);
bellota.transform().location().y = std::clamp(bellota.transform().location().y, 10.0f, static_cast<float>(screenSize.height) - 10.0f);
// Rotation
if (rotate)
bellota.transform().angle() += angularSpeed * deltaTime;
// Pulse effect from A button
if (pulseTimer > 0.0f)
{
pulseTimer -= deltaTime;
float pulse = 1.0f + 0.5f * std::sin(pulseTimer * 0.02f);
bellota.transform().scale() = glm::vec2(pulse * 3.0f, pulse * 3.0f);
}
else
{
bellota.transform().scale() = glm::vec2(3.0f, 3.0f);
}
// ImGui status
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f), ImGuiCond_Once);
ImGui::Begin("Gamepad Test");
ImGui::Text("Left stick: move sprite");
ImGui::Text("D-pad: discrete movement");
ImGui::Text("A: pulse | Start: toggle rotation");
ImGui::Text("Escape: quit");
ImGui::Separator();
if (connectedIds.empty())
{
ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "No gamepads connected");
}
else
{
std::string idList;
for (int id : connectedIds)
{
if (not idList.empty()) idList += ", ";
idList += std::to_string(id);
}
ImGui::Text("Connected: [%s]", idList.c_str());
int gamepadId = connectedIds[0];
ImGui::Text("LX: %.2f LY: %.2f",
controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::LeftX),
controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::LeftY));
ImGui::Text("RX: %.2f RY: %.2f",
controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::RightX),
controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::RightY));
ImGui::Text("LT: %.2f RT: %.2f",
controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::LeftTrigger),
controller.getGamepadAxis(gamepadId, Nothofagus::GamepadAxis::RightTrigger));
}
ImGui::End();
};
canvas.run(update, controller);
return 0;
}