Abdulg

Random bits of SDL code

Jan 21st, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. //String input
  2.  
  3. class StringInput
  4. {
  5.     private:
  6.     std::string str;
  7.     SDL_Surface *Text;
  8.     public:
  9.     StringInput();
  10.     ~StringInput();
  11.     void HandleInput();
  12.     void ShowCentered();
  13. };
  14.  
  15.  
  16. StringInput::StringInput()
  17. {
  18.     str = "";
  19.     Text = NULL;
  20.     SDL_EnableUNICODE( SDL_ENABLE );
  21. }
  22.  
  23. StringInput::~StringInput()
  24. {
  25.     SDL_FreeSurface( Text );
  26.     SDL_EnableUNICODE( SDL_DISABLE );
  27. }
  28.  
  29. void StringInput::HandleInput()
  30. {
  31.     if( event.type == SDL_KEYDOWN )
  32.     {
  33.         std::string temp = str;
  34.         if( str.length() <= 6 )
  35.         {
  36.             if( event.key.keysym.unicode == (Uint16)' ' )
  37.             {
  38.                 str += (char)event.key.keysym.unicode;
  39.             }
  40.             else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
  41.             {
  42.                 str += (char)event.key.keysym.unicode;
  43.             }
  44.             else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
  45.             {
  46.                 str += (char)event.key.keysym.unicode;
  47.             }
  48.             else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
  49.             {
  50.                 str += (char)event.key.keysym.unicode;
  51.             }
  52.         }
  53.         if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
  54.         {
  55.             str.erase( str.length() - 1 );
  56.         }
  57.         if( str != temp )
  58.         {
  59.             SDL_FreeSurface( Text );
  60.             text = TTF_RenderText_Solid( font, str.c_str(), TextColour );
  61.         }
  62.     }
  63. }
  64.  
  65. void StringInput::ShowCentered()
  66. {
  67.     if( Text != NULL )
  68.     {
  69.         ApplySurface( ( ScreenWidth - BlackRectangle -> w) / 2, (ScreenHeight - BlackRectangle->h)/2, BlackRectangle, Screen);
  70.         ApplySurface( ( ScreenWidth - Text->w ) / 2, ( ScreenHeight - Text->h ) / 2, Text, screen );
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment