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>
|
|
|
|
#include "SDL2/SDL.h"
|
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
static SDL_DisplayMode modes[5];
|
|
|
|
|
|
|
|
static int mode_count = 0, current_mode = 0;
|
|
|
|
|
|
|
|
void print_info(SDL_Window *window, SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
int w, h;
|
|
|
|
SDL_DisplayMode mode;
|
|
|
|
|
|
|
|
SDL_GetWindowSize(window, &w, &h);
|
|
|
|
SDL_Log("window size: %i x %i\n", w, h);
|
|
|
|
SDL_GetRendererOutputSize(renderer, &w, &h);
|
|
|
|
SDL_Log("renderer size: %i x %i\n", w, h);
|
|
|
|
|
|
|
|
SDL_GetCurrentDisplayMode(0, &mode);
|
|
|
|
SDL_Log("display mode: %i x %i @ %i bpp (%s)",
|
|
|
|
mode.w, mode.h,
|
|
|
|
SDL_BITSPERPIXEL(mode.format),
|
|
|
|
SDL_GetPixelFormatName(mode.format));
|
|
|
|
}
|
|
|
|
|
|
|
|
void change_mode(SDL_Window *window)
|
|
|
|
{
|
|
|
|
current_mode++;
|
|
|
|
if (current_mode == mode_count) {
|
|
|
|
current_mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SetWindowDisplayMode(window, &modes[current_mode]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw_rects(SDL_Renderer *renderer, int x, int y)
|
|
|
|
{
|
|
|
|
// R
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
|
|
|
SDL_Rect r = {x, y, 64, 64};
|
|
|
|
SDL_RenderFillRect(renderer, &r);
|
|
|
|
|
|
|
|
// G
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
|
|
|
|
SDL_Rect g = {x + 64, y, 64, 64};
|
|
|
|
SDL_RenderFillRect(renderer, &g);
|
|
|
|
|
|
|
|
// B
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
|
|
|
|
SDL_Rect b = {x + 128, y, 64, 64};
|
|
|
|
SDL_RenderFillRect(renderer, &b);
|
|
|
|
}
|
|
|
|
|
2018-03-10 21:02:38 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-03-16 18:19:48 +00:00
|
|
|
SDL_Event event;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
2019-01-07 15:46:03 +00:00
|
|
|
int done = 0, x = 0, w = 0, h = 0;
|
2018-03-10 21:02:38 +00:00
|
|
|
|
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) {
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_Log("SDL_Init: %s\n", SDL_GetError());
|
2018-03-16 18:19:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2019-01-07 15:46:03 +00:00
|
|
|
/// create a window (OpenGL always enabled)
|
|
|
|
/// available switch SDL2 video modes :
|
|
|
|
/// 1920 x 1080 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
|
|
|
|
/// 1280 x 720 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
|
|
|
|
///
|
|
|
|
/// SDL_SetWindowSize to change window size when SDL_WINDOW_FULLSCREEN is NOT used (preferably)
|
|
|
|
/// SDL_SetDisplayMode to change display size after SDL_CreateWindow called with SDL_WINDOW_FULLSCREEN
|
|
|
|
/// (this means window size won't change, you'll need to handle that, as any SDL2 app)
|
|
|
|
window = SDL_CreateWindow("sdl2_gles2", 0, 0, 1280, 720, 0);
|
2018-03-18 22:55:14 +00:00
|
|
|
if (!window) {
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError());
|
2018-03-18 22:55:14 +00:00
|
|
|
SDL_Quit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
// create a renderer (OpenGL ES2)
|
|
|
|
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
2018-03-18 22:55:14 +00:00
|
|
|
if (!renderer) {
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_Log("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-10-02 06:26:49 +00:00
|
|
|
// pint some info about display/window/renderer
|
|
|
|
print_info(window, renderer);
|
|
|
|
|
|
|
|
// list available display modes
|
|
|
|
mode_count = SDL_GetNumDisplayModes(0);
|
|
|
|
for (int i = 0; i < mode_count; i++) {
|
|
|
|
SDL_DisplayMode mode;
|
|
|
|
SDL_GetDisplayMode(0, i, &mode);
|
|
|
|
modes[i] = mode;
|
2019-01-07 15:46:03 +00:00
|
|
|
SDL_Log("found display mode: %i x %i @ %i bpp (%s)",
|
|
|
|
mode.w, mode.h,
|
|
|
|
SDL_BITSPERPIXEL(mode.format),
|
|
|
|
SDL_GetPixelFormatName(mode.format));
|
2018-10-02 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 08:56:39 +00:00
|
|
|
// open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2
|
2018-10-02 06:26:49 +00:00
|
|
|
// when railed, both joycons are mapped to joystick #0,
|
2018-03-22 08:56:39 +00:00
|
|
|
// 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) {
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_Log("SDL_JoystickOpen: %s\n", SDL_GetError());
|
2018-03-22 08:56:39 +00:00
|
|
|
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) {
|
|
|
|
|
2018-03-27 22:17:08 +00:00
|
|
|
case SDL_JOYAXISMOTION:
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_Log("Joystick %d axis %d value: %d\n",
|
|
|
|
event.jaxis.which,
|
|
|
|
event.jaxis.axis, event.jaxis.value);
|
2018-03-27 22:17:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYBUTTONDOWN:
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_Log("Joystick %d button %d down\n",
|
|
|
|
event.jbutton.which, event.jbutton.button);
|
|
|
|
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L52
|
2019-01-07 15:46:03 +00:00
|
|
|
if (event.jbutton.which == 0) {
|
|
|
|
if (event.jbutton.button == 0) {
|
|
|
|
// joystick #0 down (A)
|
|
|
|
change_mode(window);
|
|
|
|
print_info(window, renderer);
|
|
|
|
}
|
|
|
|
else if (event.jbutton.button == 2) {
|
|
|
|
// joystick #0 down (X)
|
|
|
|
if (w == 1920) {
|
|
|
|
SDL_SetWindowSize(window, 1280, 720);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SDL_SetWindowSize(window, 1920, 1080);
|
|
|
|
}
|
|
|
|
print_info(window, renderer);
|
|
|
|
}
|
2018-10-02 06:26:49 +00:00
|
|
|
}
|
2019-01-07 15:46:03 +00:00
|
|
|
// joystick #0 down (B)
|
2018-03-27 22:17:08 +00:00
|
|
|
if (event.jbutton.which == 0 && event.jbutton.button == 1) {
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2018-03-10 21:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
// Fill renderer bounds
|
|
|
|
SDL_SetRenderDrawColor(renderer, 111, 111, 111, 255);
|
2019-01-07 15:46:03 +00:00
|
|
|
SDL_GetWindowSize(window, &w, &h);
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_Rect f = {0, 0, w, h};
|
|
|
|
SDL_RenderFillRect(renderer, &f);
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
draw_rects(renderer, x, 0);
|
|
|
|
draw_rects(renderer, x, h - 64);
|
2018-03-10 21:02:38 +00:00
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_RenderPresent(renderer);
|
2018-03-18 22:55:14 +00:00
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
x++;
|
|
|
|
if (x > w - 192) {
|
|
|
|
x = 0;
|
2018-03-18 22:55:14 +00:00
|
|
|
}
|
2018-03-10 21:02:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 06:26:49 +00:00
|
|
|
SDL_DestroyRenderer(renderer);
|
|
|
|
SDL_DestroyWindow(window);
|
2018-03-16 18:19:48 +00:00
|
|
|
SDL_Quit();
|
|
|
|
|
2018-03-10 21:02:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-10-02 06:26:49 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nxlink support
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static int s_nxlinkSock = -1;
|
|
|
|
|
|
|
|
static void initNxLink()
|
|
|
|
{
|
|
|
|
if (R_FAILED(socketInitializeDefault()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_nxlinkSock = nxlinkStdio();
|
|
|
|
if (s_nxlinkSock >= 0)
|
|
|
|
printf("printf output now goes to nxlink server\n");
|
|
|
|
else
|
|
|
|
socketExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deinitNxLink()
|
|
|
|
{
|
|
|
|
if (s_nxlinkSock >= 0) {
|
|
|
|
close(s_nxlinkSock);
|
|
|
|
socketExit();
|
|
|
|
s_nxlinkSock = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void userAppInit()
|
|
|
|
{
|
|
|
|
initNxLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
void userAppExit()
|
|
|
|
{
|
|
|
|
deinitNxLink();
|
|
|
|
}
|