Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sdl input #14

Merged
merged 2 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Includes/font.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#include "font.h"

#ifdef NXDK
#include "outputLine.h"
#endif

Font::Font(const char* path) {
outputLine("Opening font %s\n", path);
font = TTF_OpenFont(path, 24);
if (font == nullptr) {
outputLine("Main font could not be opened; %s\n", TTF_GetError());
}
outline_font = TTF_OpenFont(path, 24);
if (outline_font == nullptr) {
outputLine("Outline font could not be opened; %s\n", TTF_GetError());
}
active = {0x7F, 0x7F, 0xFF, 0xFF};
passive = {0xFF, 0x7F, 0xFF, 0xFF};
outline_color = {0x40, 0x40, 0x40, 0x7F};
TTF_SetFontOutline(outline_font, outline_size);
}

Font::~Font() {
outputLine("\nFont destructor called.\n\n");
if (font != nullptr) {
TTF_CloseFont(font);
}
Expand All @@ -20,6 +32,7 @@ Font::~Font() {

bool Font::textureHelper(menuItem* mI, SDL_Color const& c, Renderer* r) {
if (mI == nullptr) {
outputLine("menuItem nullptr check failed.\n");
return false;
}
if (mI->getTexture() != nullptr) {
Expand Down
3 changes: 3 additions & 0 deletions Includes/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int max(int lhs, int rhs) {
}

#ifdef NXDK
#include "outputLine.h"
extern "C" {
const extern int SCREEN_HEIGHT;
const extern int SCREEN_WIDTH;
Expand Down Expand Up @@ -70,11 +71,13 @@ int Renderer::init(const char* bg) {
}
SDL_Surface *bgsurf = SDL_LoadBMP(const_cast<char*>(bg));
if (bgsurf == nullptr) {
outputLine("Creating background surface failed.\n");
return 3;
}
background = SDL_CreateTextureFromSurface(renderer, bgsurf);
SDL_FreeSurface(bgsurf);
if (background == nullptr) {
outputLine("Creating background texture failed.\n");
return 4;
}
return ret;
Expand Down
4 changes: 1 addition & 3 deletions Includes/subsystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
int init_systems() {
#ifdef NXDK
XVideoSetMode(640,480,32,REFRESH_DEFAULT);
XInput_Init();
if (pb_init() != 0) {
return 3;
}
pb_show_debug_screen();
#endif
if (SDL_VideoInit(NULL) != 0) {
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_GAMECONTROLLER) != 0) {
outputLine("Init error: %s", SDL_GetError());
return 2;
}
Expand All @@ -33,7 +32,6 @@ void shutdown_systems(int systems) {
SDL_Quit();
}
#ifdef NXDK
XInput_Quit();
if (systems <= 2) {
pb_kill();
}
Expand Down
113 changes: 71 additions & 42 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "xpadinput.h"

#include <threads.h>
#include <SDL.h>

void goToMainMenu(menuItem *mI, Renderer *r, Font &f,
int &listSize, int &currItem, int &prevItem, int &mMS) {
Expand All @@ -34,6 +35,17 @@ int main(void) {
vector<xbeMenuItem> gamesList;
if (init == 0) {
bool running = true;

// Open our GameController
SDL_GameController *sgc = SDL_GameControllerOpen(0);
if (sgc == nullptr) {
outputLine("Joystick Error: %s", SDL_GetError());
XSleep(2000);
}

// Set a hint that we want to use our gamecontroller always
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");

// Create the worker thread for populating the games list
xbeFinderArg xfa;
xfa.list = &gamesList;
Expand Down Expand Up @@ -71,25 +83,70 @@ int main(void) {
r.flip();
int currItem = 0, prevItem = 0, listSize = mainMenu.size();

SDL_Event event;

while (running) {
// FIXME: Abstract the input- and menu navigation process
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
if (thread_status == 1) {
thrd_join(thr, &thread_status);
}
running = false;
break;
} else if (event.type == SDL_CONTROLLERBUTTONDOWN) {
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) {
prevItem = currItem;
if (currItem == 0) {
currItem = listSize - 1;
} else {
--currItem;
}
} else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) {
prevItem = currItem;
currItem = (currItem + 1) % listSize;
} else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
switch (mainMenuSelection) {
case 0:
mainMenuSelection = currItem + 1;
currItem = 0;
prevItem = 1;
break;
case 1:
if (currItem != (gamesList.size() - 1)) {
#ifdef NXDK
XInput_GetEvents();
for (size_t i = 0; i < XInputGetPadCount(); ++i) {
if (getDigitalKeyDown(&g_Pads[i], XPAD_DPAD_UP)) {
prevItem = currItem;
if (currItem == 0) {
currItem = listSize - 1;
} else {
--currItem;
XLaunchXBE(const_cast<char*>(gamesList[currItem].getXBEPath()));
#endif
}
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
// case 2:
// break;
case 3:
#ifdef NXDK
XLaunchXBE(const_cast<char*>("D:\\default.xbe"));
#endif
mainMenuSelection = 0;
break;
// case 4:
// break;
// case 5:
// break;
default:
break;
}
} else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
switch (mainMenuSelection) {
case 1:
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
default:
break;
}
}
}
if (getDigitalKeyDown(&g_Pads[i], XPAD_DPAD_DOWN)) {
prevItem = currItem;
currItem = (currItem + 1) % listSize;
}
}
#endif
// FIXME: Loads of repetitions ahead - break out into functions
switch (mainMenuSelection) {
case 0:
Expand All @@ -103,13 +160,6 @@ int main(void) {
r.drawMenuTexture(menuListTexture);
r.flip();
}
#ifdef NXDK
if (getAnalogKeyDown(&g_Pads[0], XPAD_A)) {
mainMenuSelection = currItem + 1;
currItem = 0;
prevItem = 1;
}
#endif
break;
case 1:
if (thread_status == 1) {
Expand Down Expand Up @@ -145,33 +195,12 @@ int main(void) {
r.drawMenuTexture(menuListTexture);
r.flip();
}
#ifdef NXDK
if (getAnalogKeyDown(&g_Pads[0], XPAD_A)) {
if (currItem != (gamesList.size() - 1)) {
XLaunchXBE(const_cast<char*>(gamesList[currItem].getXBEPath()));
}
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
}
if (getAnalogKeyDown(&g_Pads[0], XPAD_B)) {
goToMainMenu(&gamesList[currItem], &r, f, listSize, currItem, prevItem,
mainMenuSelection);
break;
}
#endif
break;
case 2:
// FIXME: Essentially the same deal as case 1;
// ought to be trivial to create a function for this mess.
mainMenuSelection = 0;
break;
case 3:
#ifdef NXDK
XLaunchXBE(const_cast<char*>("D:\\default.xbe"));
#endif
mainMenuSelection = 0;
break;
case 4:
// Settings menu. Not sure what we want/need here.
// "it's a problem for the future".
Expand Down