-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
184 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from enum import IntEnum | ||
|
||
GB_WIDTH = 160 | ||
GB_HEIGHT = 144 | ||
GB_FPS = 59.727500569606 | ||
GB_TICKS_IN_FRAME = 35112 | ||
|
||
|
||
class GBInput(IntEnum): | ||
A = 0x01 | ||
B = 0x02 | ||
SELECT = 0x04 | ||
START = 0x08 | ||
RIGHT = 0x10 | ||
LEFT = 0x20 | ||
UP = 0x40 | ||
DOWN = 0x80 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import os | ||
from contextlib import contextmanager | ||
|
||
from .constants import GBInput | ||
|
||
|
||
def get_controller_mapping(): | ||
return { | ||
# Directions | ||
"A1-": GBInput.UP, | ||
"H1-": GBInput.UP, | ||
"A1+": GBInput.DOWN, | ||
"H1+": GBInput.DOWN, | ||
"A0-": GBInput.LEFT, | ||
"H0-": GBInput.LEFT, | ||
"A0+": GBInput.RIGHT, | ||
"H0+": GBInput.RIGHT, | ||
# A button | ||
"B0": GBInput.A, | ||
"B3": GBInput.A, | ||
# B button | ||
"B1": GBInput.B, | ||
"B2": GBInput.B, | ||
# Start button | ||
"B7": GBInput.START, | ||
# Select button | ||
"B6": GBInput.SELECT, | ||
} | ||
|
||
|
||
@contextmanager | ||
def pygame_button_pressed_context(deadzone=0.4): | ||
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "1" | ||
import pygame | ||
|
||
pygame.init() | ||
pygame.joystick.init() | ||
joystick = None | ||
|
||
def get_pressed(): | ||
nonlocal joystick | ||
pygame.event.get() | ||
if pygame.joystick.get_count() == 0: | ||
joystick = None | ||
return {} | ||
if joystick is None: | ||
joystick = pygame.joystick.Joystick(0) | ||
pressed = { | ||
f"B{x}" for x in range(joystick.get_numbuttons()) if joystick.get_button(x) | ||
} | ||
if joystick.get_numhats() >= 1: | ||
if joystick.get_hat(0)[0] > 0: | ||
pressed.add(f"H0+") | ||
if joystick.get_hat(0)[0] < 0: | ||
pressed.add(f"H0-") | ||
if joystick.get_hat(0)[1] < 0: | ||
pressed.add(f"H1+") | ||
if joystick.get_hat(0)[1] > 0: | ||
pressed.add(f"H1-") | ||
if joystick.get_numaxes() >= 2: | ||
if joystick.get_axis(0) > deadzone: | ||
pressed.add(f"A0+") | ||
if joystick.get_axis(0) < -deadzone: | ||
pressed.add(f"A0-") | ||
if joystick.get_axis(1) > deadzone: | ||
pressed.add(f"A1+") | ||
if joystick.get_axis(1) < -deadzone: | ||
pressed.add(f"A1-") | ||
return pressed | ||
|
||
yield get_pressed | ||
|
||
|
||
@contextmanager | ||
def gb_input_from_controller_context(): | ||
controller_mapping = get_controller_mapping() | ||
|
||
def get_gb_input(): | ||
value = 0 | ||
for keysym in joystick_get_pressed(): | ||
value |= controller_mapping.get(keysym, 0) | ||
return value | ||
|
||
with pygame_button_pressed_context() as joystick_get_pressed: | ||
yield get_gb_input | ||
|
||
|
||
@contextmanager | ||
def combine_gb_input_from_controller_context(context): | ||
with context as getter1: | ||
with gb_input_from_controller_context() as getter2: | ||
yield lambda: getter1() | getter2() |
This file contains 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
This file contains 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
This file contains 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
This file contains 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