Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
add joycon detached controls, increase enemy count
Browse files Browse the repository at this point in the history
  • Loading branch information
vgmoose committed Jan 5, 2018
1 parent ec4bebe commit 05bcf34
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Space Game NX
This is a WIP port of [Space Game](https://github.com/vgmoose/space) to the Switch, using libtransistor.
This is a port of [Space Game](https://github.com/vgmoose/space) to the Switch, using libtransistor.

The game is currently fully playable, but enemies are limited to 10 on screen at once, and some Switch-specific featuers are planned.
The game is currently fully playable, but there's no music, enemy count is limited to compared to the Wii U version, and some Switch-specific features are planned.

### Compiling
#### For Switch
Expand Down
62 changes: 39 additions & 23 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ void PADDestroy()
#endif
}

#if defined(LINUX) || defined(__APPLE__)
#else // switch
static void readInputInternal(struct PADData* data, hid_controller_state_entry_t ent)
{
// process inputs
data->btns_h |= ((ent.button_state & JOYPAD_A)? BUTTON_A : 0);
data->btns_h |= ((ent.button_state & JOYPAD_B)? BUTTON_B : 0);
data->btns_h |= ((ent.button_state & JOYPAD_UP)? BUTTON_UP : 0);
data->btns_h |= ((ent.button_state & JOYPAD_DOWN)? BUTTON_DOWN : 0);
data->btns_h |= ((ent.button_state & JOYPAD_LEFT)? BUTTON_LEFT : 0);
data->btns_h |= ((ent.button_state & JOYPAD_RIGHT)? BUTTON_RIGHT : 0);
data->btns_h |= ((ent.button_state & JOYPAD_START)? BUTTON_PLUS : 0);
data->btns_h |= ((ent.button_state & JOYPAD_SELECT)? BUTTON_MINUS : 0);

// get int values of sticks
int p1 = ent.left_stick_x, p2 = ent.left_stick_y, p3 = ent.right_stick_x, p4 = ent.right_stick_y;

// make doubles
double d1 = (float) p1, d2 = (float) p2, d3 = (float) p3, d4 = (float) p4;

// update stick values (between -1 and 1 for each)
data->lstick_x += d1 / 32768.0;
data->lstick_y += d2 / 32768.0;
data->rstick_x += d3 / 32768.0;
data->rstick_y += d4 / 32768.0;
}
#endif

void PADRead(struct PADData* data)
{
// reset buttons
Expand Down Expand Up @@ -53,33 +81,21 @@ void PADRead(struct PADData* data)
}

#else // switch
data->lstick_x = 0;
data->lstick_y = 0;
data->rstick_x = 0;
data->rstick_y = 0;

// scan for controller
hid_controller_t* num8 = hid_get_shared_memory()->controllers + 8;
// scan for controllers (main unit or 1P joycons detached)
hid_controller_t* num = hid_get_shared_memory()->controllers; // joycons detached
hid_controller_t* num8 = hid_get_shared_memory()->controllers + 8; // main unit

hid_controller_state_entry_t ent = num8->main.entries[num8->main.latest_idx];
hid_controller_state_entry_t ent = num->main.entries[num->main.latest_idx];
hid_controller_state_entry_t ent8 = num8->main.entries[num8->main.latest_idx];

// process inputs
data->btns_h |= ((ent.button_state & JOYPAD_A)? BUTTON_A : 0);
data->btns_h |= ((ent.button_state & JOYPAD_B)? BUTTON_B : 0);
data->btns_h |= ((ent.button_state & JOYPAD_UP)? BUTTON_UP : 0);
data->btns_h |= ((ent.button_state & JOYPAD_DOWN)? BUTTON_DOWN : 0);
data->btns_h |= ((ent.button_state & JOYPAD_LEFT)? BUTTON_LEFT : 0);
data->btns_h |= ((ent.button_state & JOYPAD_RIGHT)? BUTTON_RIGHT : 0);
data->btns_h |= ((ent.button_state & JOYPAD_START)? BUTTON_PLUS : 0);
data->btns_h |= ((ent.button_state & JOYPAD_SELECT)? BUTTON_MINUS : 0);

// get int values of sticks
int p1 = ent.left_stick_x, p2 = ent.left_stick_y, p3 = ent.right_stick_x, p4 = ent.right_stick_y;

// make doubles
double d1 = (float) p1, d2 = (float) p2, d3 = (float) p3, d4 = (float) p4;
readInputInternal(data, ent);
readInputInternal(data, ent8);

// update stick values (between -1 and 1 for each)
data->lstick_x = d1 / 32768.0;
data->lstick_y = d2 / 32768.0;
data->rstick_x = d3 / 32768.0;
data->rstick_y = d4 / 32768.0;

#endif
}
4 changes: 3 additions & 1 deletion program.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <SDL2/SDL.h>

#define MAX_ENEMIES 60

//Using modified version of draw to render at twice the scale to improve framerate

struct Graphics {
Expand Down Expand Up @@ -73,7 +75,7 @@ struct SpaceGlobals{
struct Pixel stars[200];

// the location of enemies
struct Enemy enemies[10];
struct Enemy enemies[MAX_ENEMIES];

int renderResetFlag;
int invalid;
Expand Down
14 changes: 7 additions & 7 deletions space.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void handleCollisions(struct SpaceGlobals * mySpaceGlobals)

// check enemies if they collide with the player or any of the 20 active bullets
int x;
for (x=0; x<10; x++)
for (x=0; x<MAX_ENEMIES; x++)
{
if (mySpaceGlobals->enemies[x].position.active == 1)
{
Expand Down Expand Up @@ -250,7 +250,7 @@ void makeScaleMatrix(int frame, int width, void *orig, void *targ, int transInde
void handleExplosions(struct SpaceGlobals* mySpaceGlobals)
{
int x;
for (x=0; x<10; x++)
for (x=0; x<MAX_ENEMIES; x++)
{
if (mySpaceGlobals->enemies[x].position.active > 1)
{
Expand Down Expand Up @@ -344,7 +344,7 @@ void renderEnemies(struct SpaceGlobals *mySpaceGlobals)
}

// for all active enemies, advance them
for (x=0; x<10; x++) // up to 100 enemies at once
for (x=0; x<MAX_ENEMIES; x++) // up to 100 enemies at once
{
if (mySpaceGlobals->enemies[x].position.active >= 1)
{
Expand Down Expand Up @@ -450,7 +450,7 @@ void moveBullets(struct SpaceGlobals *mySpaceGlobals)

}

for (x=0; x<10; x++)
for (x=0; x<MAX_ENEMIES; x++)
{
if (mySpaceGlobals->enemies[x].position.active == 1)
{
Expand Down Expand Up @@ -544,7 +544,7 @@ void initGameState(struct SpaceGlobals *mySpaceGlobals)
}

// init enemies
for (x=0; x<10; x++)
for (x=0; x<MAX_ENEMIES; x++)
{
mySpaceGlobals->enemies[x].position.active = 0;
// mySpaceGlobals->enemies[x].angle = 3.14f;
Expand Down Expand Up @@ -588,8 +588,8 @@ void displayTitle(struct SpaceGlobals * mySpaceGlobals)
char credits[255];
snprintf(credits, 255, "by vgmoose");

// char musiccredits[255];
// snprintf(musiccredits, 255, "~*cruise*~ by (T-T)b");
char musiccredits[255];
snprintf(musiccredits, 255, "~*cruise*~ by (T-T)b");

char license[255];
snprintf(license, 255, "MIT License");
Expand Down

0 comments on commit 05bcf34

Please sign in to comment.