2018-03-10 21:02:38 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <switch.h>
|
2018-03-18 22:55:14 +00:00
|
|
|
#include <video/SDL_sysvideo.h>
|
2018-03-10 21:02:38 +00:00
|
|
|
#include "SDL2/SDL.h"
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-03-16 18:19:48 +00:00
|
|
|
SDL_Event event;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
// redirect stdout to emulators
|
2018-03-10 21:02:38 +00:00
|
|
|
consoleDebugInit(debugDevice_SVC);
|
|
|
|
stdout = stderr;
|
|
|
|
|
2018-03-16 18:19:48 +00:00
|
|
|
// mandatory at least on switch, else gfx is not properly closed
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
|
|
|
printf("SDL_Init: %s\n", SDL_GetError());
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2018-03-18 22:55:14 +00:00
|
|
|
// create a 800x600 centered window for demonstration.
|
|
|
|
// if SDL_WINDOW_FULLSCREEN flag is passed, the window will be hardware scaled to fit switch screen.
|
|
|
|
// maximum window dimension is currently limited to 1280x720
|
|
|
|
window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
|
|
|
|
if (!window) {
|
|
|
|
printf("SDL_CreateWindow: %s\n", SDL_GetError());
|
|
|
|
SDL_Quit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:19:48 +00:00
|
|
|
// switch only support software renderer for now
|
2018-03-18 22:55:14 +00:00
|
|
|
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_SOFTWARE);
|
|
|
|
if (!renderer) {
|
|
|
|
printf("SDL_CreateRenderer: %s\n", SDL_GetError());
|
2018-03-16 18:19:48 +00:00
|
|
|
SDL_Quit();
|
2018-03-10 21:02:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-22 08:56:39 +00:00
|
|
|
// open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2
|
|
|
|
// when connected, both joycons are mapped to joystick #0,
|
|
|
|
// else joycons are individually mapped to joystick #0, joystick #1, ...
|
2018-03-16 18:19:48 +00:00
|
|
|
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L45
|
2018-03-22 08:56:39 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (SDL_JoystickOpen(i) == NULL) {
|
|
|
|
printf("SDL_JoystickOpen: %s\n", SDL_GetError());
|
|
|
|
SDL_Quit();
|
|
|
|
return -1;
|
|
|
|
}
|
2018-03-16 18:19:48 +00:00
|
|
|
}
|
2018-03-10 21:02:38 +00:00
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
|
|
|
|
while (SDL_PollEvent(&event)) {
|
2018-03-22 08:56:39 +00:00
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
|
|
|
|
case SDL_JOYAXISMOTION:
|
|
|
|
printf("Joystick %d axis %d value: %d\n",
|
|
|
|
event.jaxis.which,
|
|
|
|
event.jaxis.axis, event.jaxis.value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYBUTTONDOWN:
|
|
|
|
printf("Joystick %d button %d down\n",
|
|
|
|
event.jbutton.which, event.jbutton.button);
|
|
|
|
// seek for joystick #0 down (B)
|
|
|
|
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L51
|
|
|
|
if (event.jbutton.which == 0 && event.jbutton.button == 1) {
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2018-03-10 21:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 22:55:14 +00:00
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2018-03-18 22:55:14 +00:00
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
|
|
|
|
SDL_Rect bg = {0, 0, window->w, window->h};
|
|
|
|
SDL_RenderFillRect(renderer, &bg);
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2018-03-18 22:55:14 +00:00
|
|
|
// R
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
|
|
|
SDL_Rect r = {0, 0, 64, 64};
|
|
|
|
SDL_RenderFillRect(renderer, &r);
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2018-03-18 22:55:14 +00:00
|
|
|
// G
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
|
|
|
|
SDL_Rect g = {64, 0, 64, 64};
|
|
|
|
SDL_RenderFillRect(renderer, &g);
|
|
|
|
|
|
|
|
// B
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
|
|
|
|
SDL_Rect b = {128, 0, 64, 64};
|
|
|
|
SDL_RenderFillRect(renderer, &b);
|
|
|
|
}
|
2018-03-10 21:02:38 +00:00
|
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:19:48 +00:00
|
|
|
SDL_Quit();
|
|
|
|
|
2018-03-10 21:02:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|