Skip to content

Commit 130b132

Browse files
committed
Add DPad4
1 parent 5adbf6e commit 130b132

2 files changed

Lines changed: 78 additions & 12 deletions

File tree

src/firefly.c

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,51 @@ AudioTime miliseconds(int32_t s)
4242
return t;
4343
}
4444

45-
/// @brief Convert Pad to DPad.
46-
DPad pad_to_dpad(Pad pad)
47-
{
48-
DPad dpad = {
49-
.left = pad.x <= -100,
50-
.right = pad.x >= 100,
51-
.up = pad.y <= -100,
52-
.down = pad.y >= 100};
45+
/// @brief Convert Pad to DPad8.
46+
DPad8 pad_to_dpad8(Pad pad)
47+
{
48+
DPad8 dpad = {
49+
.left = pad.x <= -300,
50+
.right = pad.x >= 300,
51+
.up = pad.y <= -300,
52+
.down = pad.y >= 300};
5353
return dpad;
5454
}
5555

56+
/// @brief Convert Pad to DPad4.
57+
DPad4 pad_to_dpad4(Pad self)
58+
{
59+
int x = self.x;
60+
int y = self.y;
61+
int absX = x;
62+
if (absX < 0)
63+
{
64+
absX = -absX;
65+
}
66+
int absY = y;
67+
if (absY < 0)
68+
{
69+
absY = -absY;
70+
}
71+
if (y > 300 && y > absX)
72+
{
73+
return UP;
74+
}
75+
if (y < -300 && -y > absX)
76+
{
77+
return DOWN;
78+
}
79+
if (x > 300 && x > absY)
80+
{
81+
return RIGHT;
82+
}
83+
if (x < -300 && -x > absY)
84+
{
85+
return LEFT;
86+
}
87+
return NONE;
88+
}
89+
5690
// -- GRAPHICS -- //
5791

5892
/// @brief Fill the whole frame with the given color.

src/firefly.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,29 @@ struct Pad
179179
};
180180
typedef struct Pad Pad;
181181

182-
/// @brief A classic 4-button representation of the Pad.
183-
struct DPad
182+
/// @brief 8-directional DPad-like representation of the Pad.
183+
///
184+
/// @details Constructed with Pad.toDPad8. Useful for simple games and ports.
185+
/// The middle of the pad is a "dead zone" pressing which will not activate any direction.
186+
///
187+
/// Implements all the same methods as DPad4.
188+
///
189+
/// Invariant: it's not possible for opposite directions (left and right, or down and up)
190+
/// to be active at the same time. However, it's possible for heighboring directions
191+
/// (like up and right) to be active at the same time if the player presses a diagonal.
192+
///
193+
/// For completeness, here is the full list of possible states:
194+
///
195+
/// * right
196+
/// * right and up
197+
/// * up
198+
/// * left and up
199+
/// * left
200+
/// * left and down
201+
/// * down
202+
/// * right and down
203+
/// * none
204+
struct DPad8
184205
{
185206
/// @brief If the left segment of the Pad is pressed.
186207
bool left;
@@ -191,7 +212,17 @@ struct DPad
191212
/// @brief If the lower segment of the Pad is pressed.
192213
bool down;
193214
};
194-
typedef struct DPad DPad;
215+
typedef struct DPad8 DPad8;
216+
217+
enum DPad4
218+
{
219+
NONE = 0,
220+
LEFT = 1,
221+
RIGHT = 2,
222+
UP = 3,
223+
DOWN = 4,
224+
};
225+
typedef enum DPad4 DPad4;
195226

196227
/// @brief State of the buttons. True is pressed, false is released.
197228
struct Buttons
@@ -351,7 +382,8 @@ Angle degrees(float a);
351382
AudioTime samples(int32_t s);
352383
AudioTime seconds(int32_t s);
353384
AudioTime miliseconds(int32_t s);
354-
DPad pad_to_dpad(Pad pad);
385+
DPad8 pad_to_dpad8(Pad pad);
386+
DPad4 pad_to_dpad4(Pad pad);
355387

356388
void clear_screen(Color c);
357389
void set_color(Color c, RGB v);

0 commit comments

Comments
 (0)