SDL2 joystick interrogator animation

I recently rebooted development of a project I tried to develop for the Ludum Dare 20 contest held in April 2011, the theme of which was “It’s dangerous to go alone! Take this!”

As the project will require a controller with an analogue stick to play I’ve begun to develop a utility to interrogate the capabilities of various controllers – and as I feel that the SDL2 documentation is lacking with regards to the joystick support offered by that library I’m posting here the source code to the first version of that utility, for the benefit both of myself and of others.

SDL2 joystick interrogator.mm:

#include <SDL2/SDL.h>
#include "seal_intToString.h"
#include "seal_stringConcatenation.h"
#include "seal_tileBackground.h"

void p_StringToTileBackground( const char * const p_string, seal_TileBackground *p_tileBackground )
{
  const size_t transparent_sub_texture = 10 /* '0'..'9' */ + 26 /* 'A'..'Z' */ + 1 /* '.' */;
  p_tileBackground->p_SetAllSubTextures( transparent_sub_texture );
  size_t i = 0;
  for( ; i < strlen( p_string ); i ++ )
  {
    const char character = p_string[ i ];
    size_t sub_texture = transparent_sub_texture;
    if(( character >= '0' ) && ( character <= '9' ))
      sub_texture = character - '0';
    else
      if(( character >= 'a' ) && ( character <= 'z' ))
        sub_texture = character - 'a' + 10;
      else
        if(( character >= 'A' ) && ( character <= 'Z' ))
          sub_texture = character - 'A' + 10;
        else
          if( character == '.' )
            sub_texture = 36;
    p_tileBackground->p_SetSubTexture( i, 0, sub_texture );
  }
}

static seal_TileBackground *g_number_of_joysticks_background;

void p_UpdateNumberOfJoysticksBackground( void )
{
  const int number_of_joysticks = SDL_NumJoysticks();
  char *number_of_joysticks_string = f_seal_IntToString( number_of_joysticks, false );
  char *message = f_seal_ConcatenateStrings(( number_of_joysticks == 0 )?( char* )"No":number_of_joysticks_string, " joystick", ( number_of_joysticks == 1 )?"":"s", " detected.", NULL );
  free( number_of_joysticks_string );
  p_StringToTileBackground( message, g_number_of_joysticks_background );
  free( message );
}

int main( const int argc, const char * const argv[] )
{
  const int W = 437, H = 19;

  SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK );
  SDL_Window *window = SDL_CreateWindow( "SDL2 joystick interrogator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, W, H, SDL_WINDOW_OPENGL );
  SDL_GLContext context = SDL_GL_CreateContext( window );

  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  glOrtho( 0, W, 0, H, -1, 1 );
  glMatrixMode( GL_MODELVIEW );
  glEnable( GL_TEXTURE_2D );
  glEnableClientState( GL_VERTEX_ARRAY );
  glEnableClientState( GL_TEXTURE_COORD_ARRAY );

  seal_Texture *font = new seal_Texture( "font.png", 19, 19 );
  g_number_of_joysticks_background = new seal_TileBackground( font, 23, 1 );

  p_UpdateNumberOfJoysticksBackground();

  bool quit = false;
  do
  {
    SDL_Event event;
    while( SDL_PollEvent( &event ))
      switch( event.type )
      {
        case SDL_QUIT:
          quit = true;
          break;
        case SDL_JOYDEVICEADDED:
        case SDL_JOYDEVICEREMOVED:
          p_UpdateNumberOfJoysticksBackground();
      }
    glClear( GL_COLOR_BUFFER_BIT );
    g_number_of_joysticks_background->p_Render();
    SDL_GL_SwapWindow( window );
  }
  while( !quit );

  delete g_number_of_joysticks_background;
  delete font;

  SDL_GL_DeleteContext( context );
  SDL_DestroyWindow( window );
  SDL_Quit();
  return 0;
}

font.png:

font.png

Advertisements

Easy Prey

10Apr15
Easy Prey: Solve twenty-five of the fifty easiest problems

Easy Prey: Solve twenty-five of the fifty easiest problems


[…]

The Advent Programming Contest 2014, organized by the IEEE Student Branch Klagenfurt will provide a new problem every day from December 1st to December 24th. On Saturdays and Sundays, new problems will appear at 12:00 Central European Time, on workdays at 18:00 CET. You can submit solutions any day until the contest ends on December 26[th]. You can choose to use C, C++, Java, Python or Perl as programming language.

[…]

Until a solution is correct you can submit your program as often as you want (but please don’t spam our server).

[…]

The event is open to everyone. If you want to participate, please register at http://mooshak.nes.aau.at/

[…]

This is an individuals competition, not a team contest – be fair!
You can also join the contest after 1st December, registration is possible until December 24[th].

– Wilfried Elmenreich

Source: Advent Programming Contest 2014 [Self-Organizing Networked Systems, 1st December 2014]


Mini Ludum Dare 54, organised by Arthur Ward Jr., has begun; the theme is “it’s a race!”


Trappy Tomb

28Aug14
I beat Trappy Tomb :)

I beat Trappy Tomb 🙂

A link to Trappy Tomb by James Paulin, an entry for Ludum Dare 30, the theme of which is “Connected Worlds”.


Ludum Dare 30

23Aug14

Ludum Dare 30 has begun; the theme is “Connected Worlds”.


Level 2

19Aug14
Level 2: Solved 50 problems

Level 2: Solved 50 problems


Mini Ludum Dare 53, organised by Richard Pretzelhands, has begun; the theme is “the future is now”.


Follower

18Jul14

I’ve just achieved the rank of “follower” at CodeAbbey for solving forty-five problems.


I’ve just received my statement of accomplishment – with Distinction (105.6%) – for Programming for Everybody (Python).