-
Notifications
You must be signed in to change notification settings - Fork 347
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
1 parent
38c896f
commit 59e96c9
Showing
4 changed files
with
88 additions
and
80 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 |
---|---|---|
@@ -1,5 +1,45 @@ | ||
package nanoarch | ||
|
||
import "sync/atomic" | ||
|
||
const RetroDeviceTypeShift = 8 | ||
|
||
// InputState stores full controller state. | ||
// It consists of: | ||
// - uint16 button values | ||
// - int16 analog stick values | ||
type ( | ||
InputState [maxPort]RetroPadState | ||
RetroPadState struct { | ||
keys uint32 | ||
axes [dpadAxes]int32 | ||
} | ||
) | ||
|
||
const ( | ||
maxPort = 8 | ||
dpadAxes = 4 | ||
) | ||
|
||
func NewRetroPadState() InputState { return [maxPort]RetroPadState{} } | ||
|
||
// SetInput sets input state for some player in a game session. | ||
func (s *InputState) SetInput(player int, data []byte) { | ||
atomic.StoreUint32(&s[player].keys, uint32(uint16(data[1])<<8+uint16(data[0]))) | ||
for i, axes := 0, len(data); i < dpadAxes && i<<1+3 < axes; i++ { | ||
axis := i<<1 + 2 | ||
atomic.StoreInt32(&s[player].axes[i], int32(data[axis+1])<<8+int32(data[axis])) | ||
} | ||
} | ||
|
||
// IsKeyPressed checks if some button is pressed by any player. | ||
func (s *InputState) IsKeyPressed(port uint, key int) int { | ||
return int((atomic.LoadUint32(&s[port].keys) >> uint(key)) & 1) | ||
} | ||
|
||
// IsDpadTouched checks if D-pad is used by any player. | ||
func (s *InputState) IsDpadTouched(port uint, axis uint) (shift int16) { | ||
return int16(atomic.LoadInt32(&s[port].axes[axis])) | ||
} | ||
|
||
func RetroDeviceSubclass(base, id int) int { return ((id + 1) << RetroDeviceTypeShift) | base } |
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,21 @@ | ||
package nanoarch | ||
|
||
import ( | ||
"math/rand" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestConcurrentInput(t *testing.T) { | ||
var wg sync.WaitGroup | ||
state := NewRetroPadState() | ||
events := 1000 | ||
wg.Add(2 * events) | ||
|
||
for i := 0; i < events; i++ { | ||
player := rand.Intn(maxPort) | ||
go func() { state.SetInput(player, []byte{0, 1}); wg.Done() }() | ||
go func() { state.IsKeyPressed(uint(player), 100); wg.Done() }() | ||
} | ||
wg.Wait() | ||
} |