Sunday, October 27, 2013

DualShock 4 for SDL layout.

Here's a quick dump of getting DualShock 4 support in your SDL title that should work for SDL 1.2 and 2.0.  If you're not using the controller mapping in SDL ( like for Steam OS titles ), but want to do it old-school here's the button layout in SDL terms:

You can detect the pad via the string "Sony Computer Entertainment Wireless Controller".  Here's a dump from Linux dmesg for more USB details if you want to fingerprint:

[ 1154.896287] usb 3-1: new full-speed USB device number 5 using xhci_hcd
[ 1154.913200] usb 3-1: New USB device found, idVendor=054c, idProduct=05c4
[ 1154.913209] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 1154.913215] usb 3-1: Product: Wireless Controller
[ 1154.913220] usb 3-1: Manufacturer: Sony Computer Entertainment
[ 1154.913420] usb 3-1: ep 0x84 - rounding interval to 32 microframes, ep desc says 40 microframes
[ 1154.913435] usb 3-1: ep 0x3 - rounding interval to 32 microframes, ep desc says 40 microframes
[ 1154.919473] input: Sony Computer Entertainment Wireless Controller as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/input/input15
[ 1154.919743] hid-generic 0003:054C:05C4.0007: input,hidraw4: USB HID v1.11 Gamepad [Sony Computer Entertainment Wireless Controller] on usb-0000:00:14.0-1/input0

Analog Sticks and Triggers
Axis 0 - Left Stick Hortz
Axis 1 - Left Stick Vert
Axis 2 - Right Stick Hortz
Axis 5 - Right Stick Vert
Axis 3 - Analog L2
Axis 4 - Analog R2

Directional Pad
Up - 1
Right - 2
Down - 4
Left - 8
Up+Right - 3
Down+Right - 6
Up+Left - 9
Down+Left - 12

Digital buttons
Button 0 - Square
Button 1 - Cross
Button 2 - Circle
Button 3 - Triangle
Button 4 - L1
Button 5 - R1
Button 6 - L2
Button 7 - R2
Button 8 - Share
Button 9 - Options
Button 10 - L3
Button 11 - R3
Button 12 - PS Button
Button 13 - Touchpad depress

Setup Loop
Here's some generic code snippets for setup and detection of the controller.

for ( uint32_t i = 0, count = SDL_NumJoysticks(); i < count; ++i )
{
   /* eg "Sony Computer Entertainment Wireless Controller" */
   const char* joystickName = SDL_JoystickNameForIndex( i );
   SDL_JoystickEventState( SDL_ENABLE );
   sdlDevice->mJoystick = SDL_JoystickOpen( i );
   InputJoystickMap( joystickName, sdlDevice->mJoystick, i );  
}


Event Loop Style Sampling
Here's a generic SDL event loop to access the pad input frame by frame if you like.

 SDL_Event event;
 while ( SDL_PollEvent( &event ) )
 {
  switch (event.type)
  {
  case SDL_JOYDEVICEADDED:
   InputJoystickSignalAdd( event.jdevice.which );
   break;

  case SDL_JOYDEVICEREMOVED:
   InputJoystickSignalRemove( event.jdevice.which );
   break;

  case SDL_JOYHATMOTION:
   InputJoystickSignalDpad( event.jhat.which, event.jhat.hat, event.jhat.value );
   break;

  case SDL_JOYAXISMOTION:
   InputJoystickSignalAxis( event.jaxis.which, event.jaxis.axis, event.jaxis.value );
   break;
  
  case SDL_JOYBUTTONUP:
   InputJoystickSignalButtonRelease( event.jbutton.which, event.jbutton.button, event.jbutton.state );
   break;

  case SDL_JOYBUTTONDOWN:
   InputJoystickSignalButtonPress( event.jbutton.which, event.jbutton.button, event.jbutton.state );
   break;

   ...


Monday, October 7, 2013

Terrain and Biomes and Flora oh my.

So I dusted off the terrain / biome system to hook it up to the voxel terrain system, since it makes a lot more sense to model very large landmasses in a way people can understand.  Density functions by parts isn't a bad thing, but even if you expose this with a UI flow graph -- who the fuck is going to understand it that also wants to use a flow graph chart?  Exactly.

Better to allow for 'big picture' visualization of what those damn things do I say.  Take for example this simple gem on how to generate large landmasses... Clamp your density by masks of large water bodies and scale by smaller ones.  Simple and effective.  Rock composition is the variable that effects water erosion the most.  It happens in nature everywhere that's why you get the grand canyon to the salt flats.  Forget elevation as the main factor.

In fact you can completely ignore the physics and go for a purely artistic version of a bay like below.  Only using three of the density function operators no less for a playground for artists to start hacking away.  Each KaDensity function takes a list of parameters and returns a density.  They're pretty self-explanatory:

KaDensityNoiseSimplex2d : octaves, frequency, lacunarity, gain
KaDensitySaturate : density
KaDensityMul : density_A, density_B

Here each stage is generated via simplex noise and saturated to threshold the values, and then combined to produce composite images.  Don't have to teach artists about physics or generate all the voxel terrain for them to visualize while dialing it in...  make continent iteration quicker.  This mask alone has a large effect on the Terrain and Biomes as you can see.  From here you can pass this to VoxelSector density samplers and get your 3d terrain as a GPU visualization and / or CPU mesh from isosurface extraction as seen previously on the blog.  BTW I have found the ideal width of a river is ~1km so these pixels are about 1km^2 while typically I would generate VoxelSectors at about 1/4km^3 to give a sense of scale.  That makes these preview windows about 512x512km while the camera in game tends to only go to a view distance of 12km.  From here you can feed back masks into each other such as for Fanua mask generator.  :)

Generating Water Mask
Terrain + Biomes Visualization

Flora Visualization