Files
kaizen/external/SDL/examples/input/01-joystick-polling/joystick-polling.c
T
iris 00cc9309cb Squashed 'external/ircolib/' changes from ce3cd726c..de6e324bd
de6e324bd separate emu thread
10d3daf86 Roms List improvements
95d202f37 Let's make the rom list process on a separate thread so the emulator doesnt take ages to load.
fc306967f Wow the ROM Header was just completely busted. Game list view works now
bad1691ee fuck this shit
2b59e5f46 game list in progress
d26417b83 remappable inputs in progress
ac4af8106 input
e72abc240 update readme
430139dc9 Qt6 frontend
3080d4d45 Fix this small bug too
08cd13b85 Cop0 unused functions do not actually pose a threat (as per manual). They don't do anything, so shall we.
61bb4fb44 make idle loop detection a little more specific with where the load goes
b037de4c3 SAZDFsdff
12e81e73e need to figure out why n64-systemtest loops indefinitely at some address that appears to be valid (i think it's me not invalidating the cache properly)
204f0e13b idle skipping seems to work!
cb8bb634a sdkfjlasdf
58e5c89c1 Fix compilation issue on my machine (no idea)
24fb2898e attempting more serious idle skipping
214719577 Place rsp.Step inside cached interpreter. Gains about 3 more fps
bb97dcc23 mmmmm
920b77d38 wjkhasdfjhkasdf
430ccdab4 it's a start...
4f42a673a Cached interpreter plays Mario 64. Start looking into RSP as well
c9a030787 idle skipping works!
5fbda03ce new idea
366637aba Idle skipping... maybe?
609fa2fb0 Cache instructions implemented but broken lmao. Commented out for now
e140a6d12 - Stop using inheritance for CPU, instead use composition. - Introduce KAIZEN_JIT_ENABLED optional define instead of relying on __aarch64__ and the like. - More cache work
68e613057 prep cache impl
811b4d809 fix clang format
fda755f7d idk
d5024ebbf small MI refactor in preparation of (eventually) implementing the RDRAM interface properly
694b45341 Merge commit '206dcdedf195fb320913584180edb12c7731e396' as 'external/SDL'
206dcdedf Squashed 'external/SDL/' content from commit 4d17b99d0a
4d16e1cb4 need to update sdl
848b19920 Fix compilation error
db61b5299 Merge commit 'e94a94559f28e49678fbcf72199a5258137b0fe9' as 'external/imgui'
e94a94559 Squashed 'external/imgui/' content from commit 02e9b8cac
52edb3757 need to update imgui
c1a705e86 Emulate weird JALR behaviour
4b4c32f4b Fix exception for "unusable COP1" in 4 instructions i missed accidentally (again)
df5828142 Bug putting 0s in the log everywhere
f8b580048 Make isviewer a sink to file
8241e9735 Fix exception for "unusable COP1" in 4 instructions i missed accidentally
b29715f20 small changes
d9a620bc1 make use of my new small utility library
0d1aa938e Add 'external/ircolib/' from commit 'ce3cd726c8df8388d554abf8bb55d55020eb4450'
e64eb40b3 Fuck git

git-subtree-dir: external/ircolib
git-subtree-split: de6e324bde
2026-06-15 11:56:38 +02:00

194 lines
7.8 KiB
C

/*
* This example code looks for the current joystick state once per frame,
* and draws a visual representation of it.
*
* This code is public domain. Feel free to use it for any purpose!
*/
/* Joysticks are low-level interfaces: there's something with a bunch of
buttons, axes and hats, in no understood order or position. This is
a flexible interface, but you'll need to build some sort of configuration
UI to let people tell you what button, etc, does what. On top of this
interface, SDL offers the "gamepad" API, which works with lots of devices,
and knows how to map arbitrary buttons and such to look like an
Xbox/PlayStation/etc gamepad. This is easier, and better, for many games,
but isn't necessarily a good fit for complex apps and hardware. A flight
simulator, a realistic racing game, etc, might want the joystick interface
instead of gamepads. */
/* SDL can handle multiple joysticks, but for simplicity, this program only
deals with the first stick it sees. */
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
/* We will use this renderer to draw into this window every frame. */
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static SDL_Joystick *joystick = NULL;
static SDL_Color colors[64];
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
int i;
SDL_SetAppMetadata("Example Input Joystick Polling", "1.0", "com.example.input-joystick-polling");
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
if (!SDL_CreateWindowAndRenderer("examples/input/joystick-polling", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
for (i = 0; i < SDL_arraysize(colors); i++) {
colors[i].r = SDL_rand(255);
colors[i].g = SDL_rand(255);
colors[i].b = SDL_rand(255);
colors[i].a = 255;
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
} else if (event->type == SDL_EVENT_JOYSTICK_ADDED) {
/* this event is sent for each hotplugged stick, but also each already-connected joystick during SDL_Init(). */
if (joystick == NULL) { /* we don't have a stick yet and one was added, open it! */
joystick = SDL_OpenJoystick(event->jdevice.which);
if (!joystick) {
SDL_Log("Failed to open joystick ID %u: %s", (unsigned int) event->jdevice.which, SDL_GetError());
}
}
} else if (event->type == SDL_EVENT_JOYSTICK_REMOVED) {
if (joystick && (SDL_GetJoystickID(joystick) == event->jdevice.which)) {
SDL_CloseJoystick(joystick); /* our joystick was unplugged. */
joystick = NULL;
}
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
int winw = 640, winh = 480;
const char *text = "Plug in a joystick, please.";
float x, y;
int i;
if (joystick) { /* we have a stick opened? */
text = SDL_GetJoystickName(joystick);
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_GetWindowSize(window, &winw, &winh);
/* note that you can get input as events, instead of polling, which is
better since it won't miss button presses if the system is lagging,
but often times checking the current state per-frame is good enough,
and maybe better if you'd rather _drop_ inputs due to lag. */
if (joystick) { /* we have a stick opened? */
const float size = 30.0f;
int total;
/* draw axes as bars going across middle of screen. We don't know if it's an X or Y or whatever axis, so we can't do more than this. */
total = SDL_GetNumJoystickAxes(joystick);
y = (winh - (total * size)) / 2;
x = ((float) winw) / 2.0f;
for (i = 0; i < total; i++) {
const SDL_Color *color = &colors[i % SDL_arraysize(colors)];
const float val = (((float) SDL_GetJoystickAxis(joystick, i)) / 32767.0f); /* make it -1.0f to 1.0f */
const float dx = x + (val * x);
const SDL_FRect dst = { dx, y, x - SDL_fabsf(dx), size };
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);
SDL_RenderFillRect(renderer, &dst);
y += size;
}
/* draw buttons as blocks across top of window. We only know the button numbers, but not where they are on the device. */
total = SDL_GetNumJoystickButtons(joystick);
x = (winw - (total * size)) / 2;
for (i = 0; i < total; i++) {
const SDL_Color *color = &colors[i % SDL_arraysize(colors)];
const SDL_FRect dst = { x, 0.0f, size, size };
if (SDL_GetJoystickButton(joystick, i)) {
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);
} else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
}
SDL_RenderFillRect(renderer, &dst);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, color->a);
SDL_RenderRect(renderer, &dst); /* outline it */
x += size;
}
/* draw hats across the bottom of the screen. */
total = SDL_GetNumJoystickHats(joystick);
x = ((winw - (total * (size * 2.0f))) / 2.0f) + (size / 2.0f);
y = ((float) winh) - size;
for (i = 0; i < total; i++) {
const SDL_Color *color = &colors[i % SDL_arraysize(colors)];
const float thirdsize = size / 3.0f;
const SDL_FRect cross[] = { { x, y + thirdsize, size, thirdsize }, { x + thirdsize, y, thirdsize, size } };
const Uint8 hat = SDL_GetJoystickHat(joystick, i);
SDL_SetRenderDrawColor(renderer, 90, 90, 90, 255);
SDL_RenderFillRects(renderer, cross, SDL_arraysize(cross));
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);
if (hat & SDL_HAT_UP) {
const SDL_FRect dst = { x + thirdsize, y, thirdsize, thirdsize };
SDL_RenderFillRect(renderer, &dst);
}
if (hat & SDL_HAT_RIGHT) {
const SDL_FRect dst = { x + (thirdsize * 2), y + thirdsize, thirdsize, thirdsize };
SDL_RenderFillRect(renderer, &dst);
}
if (hat & SDL_HAT_DOWN) {
const SDL_FRect dst = { x + thirdsize, y + (thirdsize * 2), thirdsize, thirdsize };
SDL_RenderFillRect(renderer, &dst);
}
if (hat & SDL_HAT_LEFT) {
const SDL_FRect dst = { x, y + thirdsize, thirdsize, thirdsize };
SDL_RenderFillRect(renderer, &dst);
}
x += size * 2;
}
}
x = (((float) winw) - (SDL_strlen(text) * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE)) / 2.0f;
y = (((float) winh) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2.0f;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, text);
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
if (joystick) {
SDL_CloseJoystick(joystick);
}
/* SDL will clean up the window/renderer for us. */
}