Created
July 15, 2020 02:04
-
-
Save igormorgado/25659e5c26f8b5a349267f611d35892b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdbool.h> | |
#include <stdlib.h> | |
#include <SDL2/SDL.h> | |
/* | |
* | |
* STRUCTS, Enums and Typedefs | |
* | |
*/ | |
typedef int8_t i8; | |
typedef int16_t i16; | |
typedef int32_t i32; | |
typedef int64_t i64; | |
typedef uint8_t u8; | |
typedef uint16_t u16; | |
typedef uint32_t u32; | |
typedef uint64_t u64; | |
typedef unsigned int uint; | |
typedef float f32; | |
typedef double f64; | |
struct player { | |
SDL_GameController *gamecontroller; | |
SDL_Joystick *joystick; | |
SDL_Rect viewport; | |
}; | |
enum PLAYER { | |
PLAYER_1, | |
PLAYER_2, | |
PLAYER_3, | |
PLAYER_4, | |
PLAYER_TOTAL | |
}; | |
/* | |
* | |
* GLOBAL STUFF | |
* | |
* */ | |
SDL_Window *window; | |
SDL_Renderer *renderer; | |
struct player player[PLAYER_TOTAL]; | |
u16 screen_width = 1280; // 16 | |
u16 screen_height = 720; // 9 | |
int main(void) { | |
u32 exitval = EXIT_FAILURE; | |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); | |
int sdl_flags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC; | |
int rnd_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; | |
bool is_running = true; | |
SDL_Init(sdl_flags); | |
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt"); | |
window = SDL_CreateWindow("Gamepads and viewports", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_SHOWN); | |
renderer = SDL_CreateRenderer(window, -1, rnd_flags); | |
SDL_RenderSetLogicalSize(renderer, screen_width, screen_height); | |
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); | |
SDL_Event event; | |
const char *name; | |
const char *description; | |
int devid; | |
while(is_running == true) | |
{ | |
while(SDL_PollEvent(&event)) | |
{ | |
switch(event.type) | |
{ | |
case SDL_QUIT: | |
is_running = false; | |
break; | |
case SDL_KEYDOWN: | |
if (event.key.keysym.sym == SDLK_ESCAPE) { | |
is_running = false; | |
} | |
break; | |
case SDL_CONTROLLERDEVICEADDED: | |
SDL_Log("Game controller device %d added.\n", (int) event.cdevice.which); | |
break; | |
case SDL_CONTROLLERDEVICEREMOVED: | |
SDL_Log("Game controller device %d removed.\n", (int) event.cdevice.which); | |
break; | |
case SDL_CONTROLLERBUTTONDOWN: | |
case SDL_CONTROLLERBUTTONUP: | |
SDL_Log("Controller button %s %s\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released"); | |
break; | |
case SDL_JOYDEVICEADDED: | |
devid = event.jdevice.which; | |
SDL_Log("Joystick device %d inserted.\n", devid ); | |
if (SDL_IsGameController(devid)) | |
{ | |
name = SDL_GameControllerNameForIndex(devid); | |
switch (SDL_GameControllerTypeForIndex(devid)) | |
{ | |
case SDL_CONTROLLER_TYPE_XBOX360: | |
description = "Xbox 360 Controller"; | |
break; | |
case SDL_CONTROLLER_TYPE_XBOXONE: | |
description = "Xbox One Controller"; | |
break; | |
case SDL_CONTROLLER_TYPE_PS3: | |
description = "PS3 Controller"; | |
break; | |
case SDL_CONTROLLER_TYPE_PS4: | |
description = "PS4 Controller"; | |
break; | |
case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO: | |
description = "Nintendo Switch Pro Controller"; | |
break; | |
default: | |
description = "Generic Game Controller"; | |
break; | |
} | |
//SDL_GameController *ctrl = SDL_GameControllerOpen(devid); | |
// if(!ctrl) | |
// { | |
// SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, | |
// "%s: SDL_GameControllerOpen(%d): %s\n", | |
// __func__, devid, SDL_GetError()); | |
// } else { | |
// SDL_Log("Controler name: %s desc:%s opened\n", name, description); | |
// } | |
} else { | |
name = SDL_JoystickNameForIndex(devid); | |
description = "Joystick"; | |
} | |
break; | |
case SDL_JOYDEVICEREMOVED: | |
SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which); | |
break; | |
case SDL_JOYBUTTONDOWN: | |
case SDL_JOYBUTTONUP: | |
SDL_Log("Joy %i - Button: %d %s\n", | |
event.jdevice.which, | |
event.jbutton.button, | |
event.jbutton.state ? "pressed" : "released"); | |
break; | |
default: | |
break; | |
} | |
} | |
SDL_RenderClear(renderer); | |
SDL_RenderPresent(renderer); | |
SDL_Delay(1); /* Keep CPU cool */ | |
} | |
exitval = EXIT_SUCCESS; | |
SDL_Log("Closing application\n"); | |
if (renderer) SDL_DestroyRenderer(renderer); | |
if (window) SDL_DestroyWindow(window); | |
SDL_Quit(); | |
SDL_Log("Bye.\n"); | |
return exitval; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment