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

Maximum yolo #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions H264Decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init_avcodec(s):
s.got_frame = s.ffi.new('int *')
s.out_frame = s.ns.avcodec_alloc_frame()

def __init__(s, (in_x, in_y), (out_x, out_y), output_surface):
def __init__(s, (in_x, in_y), (out_x, out_y), output_surface=None):
s.sws_context = None
s.__init_ffi()
s.__init_avcodec()
Expand Down Expand Up @@ -142,7 +142,9 @@ def display_frame(s, encoded_nalu):
(s.out_x, s.out_y),
'RGB')
# I guess this is an extra copy...?
s.output_surface.blit(surface, (0, 0))
# prev implementation just wrote out to the main display and did the flip:
# pygame.display.get_surface().blit(surface, (0, 0))
# pygame.display.flip()
if s.output_surface:
s.output_surface.blit(surface, (0, 0))
else:
# prev implementation just wrote out to the main display and did the flip:
pygame.display.get_surface().blit(surface, (0, 0))
pygame.display.flip()
57 changes: 42 additions & 15 deletions InputData.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@
TODO tests
"""
from construct import (
ULint8,
ULint16,
Array,
Struct,
BitStruct,
BitField,
Byte,
SLInt16,
Padding,
Rename
)
ULInt8,
ULInt16,
SLInt16,
UBInt8,
UBInt16,
SBInt16,
Array,
Struct,
BitStruct,
BitField,
Byte,
Padding,
Rename,
Flag
)

u16 = ULint16
s16 = SLInt16
u8 = ULint8
## turn on to make little-endian values
# u16 = ULInt16
# s16 = SLInt16
# u8 = ULInt8

u16 = UBInt16
s16 = SBInt16
u8 = UBInt8

AccelerometerData = Struct("AccelerometerData",
s16("x_accel"),
Expand Down Expand Up @@ -52,6 +61,24 @@

TouchscreenData = Struct("TouchscreenData", Array(10, Point))

Buttons = BitStruct("Buttons",
Flag("a"),
Flag("b"),
Flag("x"),
Flag("y"),
Flag("left"),
Flag("right"),
Flag("up"),
Flag("down"),
Flag("zl"),
Flag("zr"),
Flag("l"),
Flag("r"),
Flag("plus"),
Flag("minus"),
Flag("home"),
Flag("sync"),)

# the real shebang
InputData = Struct("InputData",
u16("seq_id"),
Expand All @@ -69,4 +96,4 @@
Array(4, Byte("unkown0")),
u8("extra_buttons"),
Array(46, Byte("unknown1")),
u8("fw_version_neg")) # ~fw_version
u8("fw_version_neg")) # u~fw_version
8 changes: 5 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class ExitMain(StandardError):
"""
pass

SURFACE_OPTS = 0 # pygame.FULLSCREEN | pygame.HWSURFACE
SIZE = (854, 480)

class App(object):
"""
Expand All @@ -18,7 +20,7 @@ class App(object):
def __init__(self, title):
pygame.init()
pygame.display.set_caption(title)
pygame.display.set_mode((20, 20))
pygame.display.set_mode((20, 20), SURFACE_OPTS)
load_all_assets()
asset_names = ASSET_DICT.keys()

Expand All @@ -36,8 +38,8 @@ def __init__(self, title):
if w > max_w:
max_w = w
screen = pygame.display.set_mode(
(BORDER_W + max_w + BORDER_W,
BORDER_H + max_h + BORDER_H)
SIZE,
SURFACE_OPTS
)

log('calculated window size ({width}, {height}) from {count} assets'.format(
Expand Down
62 changes: 56 additions & 6 deletions controls/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,36 @@
'A': 0x8000
}

EXTRA_BOTTOMS = {
'L3': 0x08,
'R3': 0x04,
EXTRA_BUTTONS = {
'R3': 0x40,
'L3': 0x80,
}


def inspect_mask(integer, bits=8, byte=8):
"""
print a value as a bits. printing in byte-sized blocks

>>> bits(255)
'11111111'

>>> bits(256)
'00000001 00000000'
"""
as_bin = bin(integer)[2:]
if len(as_bin) < bits:
missing_bits = bits - len(as_bin)
as_bin = '0' * missing_bits + as_bin

return ' '.join(chunks(as_bin, byte))


def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in xrange(0, len(l), n):
yield l[i:i+n]


def button_mask(controls):
"""
Given controls, a `Controls` instance, compute the bitmask of all pressed
Expand All @@ -44,9 +68,11 @@ def button_mask(controls):

def extra_button_mask(controls):
mask = 0
for button_name, button_mask in EXTRA_BOTTOMS.iteritems():
for button_name, button_mask in EXTRA_BUTTONS.iteritems():
if controls.invoke(button_name.lower()):
mask |= button_mask
#log('wow special button {b} clicked and invoked! new mask {m}'.format(
#b=button_name, m=mask), 'FUGGIN BUTTANS')
return mask


Expand Down Expand Up @@ -154,11 +180,21 @@ def accelerometer(self):
def gyroscope(self):
return (0, 0, 0)

"""
should return a tuple (x, y, force) of a touch location.
force should be a float between 0 and 1.

return None if there is no touch.
"""
def touch(self):
return None

def handle_event(self, event):
"""
override if you wish to handle events with your controller
"""
pass
""" pass




class MethodMissing(object):
Expand Down Expand Up @@ -211,3 +247,17 @@ def scale(OldValue, OldMin, OldMax, NewMin, NewMax):

def add(point_a, point_b):
return (point_a[0] + point_b[0], point_a[1] + point_b[1])


def wiiu_axis(orig):
"""
given a joystick axis motion, scale into Wii U space
"""
if abs(orig) < 0.0001:
# unsure why this starts as this value 0x800
return 0x800
return int(scale(orig, -1, 1, 900, 3200))

###
# TODO: move control value constructors to new file
###
8 changes: 4 additions & 4 deletions controls/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def a(self):
return self.key(K_h) or self.key(K_RETURN)

def b(self):
return self.key(K_j)
return self.key(K_j) or self.key(K_BACKSPACE)

def x(self):
return self.key(K_k) or self.key(K_SPACE)
Expand Down Expand Up @@ -83,14 +83,14 @@ def home(self):
return False

def minus(self):
return False
return self.key(K_MINUS)

def plus(self):
return False
return self.key(K_EQUALS)

# joystick presses
def r3(self):
return False
return self.key(K_e)

def l3(self):
return False
Expand Down
30 changes: 21 additions & 9 deletions controls/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
from util import log # TODO switch to absolute paths


JOYSTICK_MIN = 0.17
TOGGLE_LOCK_KEY = K_BACKQUOTE
# lag high-magnitude movements another frame
ENABLE_LAG = True
ENABLE_DOUBLE_LAG = True
ENABLE_DOUBLE_LAG = False
MAX_MOVEMENT = 50.0
LAG_WHEN_LESS_THAN = 0.1
LAG_WHEN_GREATER_THAN = 0.8


def lag(final, prev):
Expand All @@ -21,7 +24,10 @@ def lag(final, prev):

# TO DO: switch joystick magnitude measuring to polar-coordiate vectors
# and mreasure magnitude of both axes at once
if ENABLE_LAG and abs(prev) > 0.8 > abs(final):
if ENABLE_LAG and (
abs(prev) > LAG_WHEN_GREATER_THAN
and abs(final) < LAG_WHEN_LESS_THAN):

# swap em. we'll play this frame again to smooth the falloff
# and we get a bonus frame of high-magnitude movement
log('lagged {prev} again instead of {final}'.format(prev=prev, final=final), 'MOUSE')
Expand Down Expand Up @@ -55,9 +61,9 @@ def get(self):
gets the joystick pairs
"""
dx, dy = pygame.mouse.get_rel()
if abs(dx) > 0 or abs(dy) > 0:
log("pygame mouse movement: ({dx}, {dy})".format(dx=dx, dy=dy),
'MOUSE')
#if abs(dx) > 0 or abs(dy) > 0:
#log("pygame mouse movement: ({dx}, {dy})".format(dx=dx, dy=dy),
#'MOUSE')

return (self.convert_x_axis(dx), self.convert_y_axis(dy))

Expand Down Expand Up @@ -92,7 +98,7 @@ def convert_axis(self, d):
float(abs(d)), # value
0.0, # old min
float(self.get_max()), # old max
0.06, # new min -- escape deadzone
max(JOYSTICK_MIN, 0.0), # new min -- escape deadzone
1.0 # new max
)

Expand Down Expand Up @@ -128,6 +134,9 @@ def handle_event(self, event):
self.sensitivity += self.sensitivity_incr
log('[+] set sensitivity to {s}, delta_max is {max} (lower = more sensitive)'.format(s=self.sensitivity, max=self.get_max()), 'MOUSE')

def is_locked(self):
return pygame.event.get_grab()

def lock(self):
"""
locks the mouse to the window, enabling accurate readings
Expand Down Expand Up @@ -158,10 +167,13 @@ def handle_event(self, event):
self.mouse.handle_event(event)

def r(self):
return pygame.mouse.get_pressed()[2]
if self.mouse.is_locked():
return pygame.mouse.get_pressed()[2]

def zr(self):
return pygame.mouse.get_pressed()[0]
if self.mouse.is_locked():
return pygame.mouse.get_pressed()[0]

def r3(self):
return pygame.mouse.get_pressed()[1]
if self.mouse.is_locked():
return pygame.mouse.get_pressed()[1]
5 changes: 3 additions & 2 deletions controls/wireless.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def a(self):

# extra buttons - joystick presses
def r3(self):
return False
return self.joy.get_button(10)

def l3(self):
return False
return self.joy.get_button(9)


# the 'TV' button. Unsure of utility of implementing this.
def tv(self):
Expand Down
Loading