Advertisement
Guest User

Class StringInput

a guest
Dec 5th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <SDL/SDL.h>
  4. #include <SDL_ttf.h>
  5. #include <string>
  6.  
  7. SDL_Event event;
  8.  
  9. SDL_Surface* screen;
  10. SDL_Surface* message;
  11. SDL_Surface* background;
  12.  
  13. TTF_Font *font = NULL;
  14. SDL_Color textColor = {255, 255, 255};
  15.  
  16. SDL_Surface* load_image(std::string filename)
  17. {
  18. SDL_Surface* loadedImage = NULL;
  19. SDL_Surface* optimizedImage = NULL;
  20. loadedImage = SDL_LoadBMP(filename.c_str());
  21. if (loadedImage != NULL)
  22. {
  23. optimizedImage = SDL_DisplayFormat(loadedImage);
  24. SDL_FreeSurface(loadedImage);
  25. }
  26. return optimizedImage;
  27. }
  28.  
  29. void apply_surface (int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
  30. {
  31. SDL_Rect offset;
  32. offset.x = x;
  33. offset.y = y;
  34.  
  35. SDL_BlitSurface(source, clip, destination, &offset);
  36. }
  37.  
  38. class StringInput
  39. {
  40. private:
  41. std::string str;
  42. SDL_Surface* text;
  43.  
  44. public:
  45. StringInput();
  46. ~StringInput();
  47. void handle_input();
  48. void show_centered();
  49. };
  50.  
  51. StringInput::StringInput()
  52. {
  53. str = "";
  54. text = NULL;
  55. SDL_EnableUNICODE(SDL_ENABLE);
  56. }
  57.  
  58. StringInput::~StringInput()
  59. {
  60. SDL_FreeSurface(text);
  61. SDL_EnableUNICODE(SDL_DISABLE);
  62. }
  63.  
  64. void StringInput::handle_input()
  65. {
  66. if(event.type == SDL_KEYDOWN)
  67. {
  68. std::string temp = str;
  69. if(str.length() <= 16)
  70. {
  71. if(event.key.keysym.unicode == (Uint16)' ')
  72. {
  73. str += (char)event.key.keysym.unicode;
  74. }
  75. if((event.key.keysym.unicode >= (Uint16)'0') && (event.key.keysym.unicode <= (Uint16)'9'))
  76. {
  77. str += (char)event.key.keysym.unicode;
  78. }
  79. if((event.key.keysym.unicode >= (Uint16)'A') && (event.key.keysym.unicode <= (Uint16)'Z'))
  80. {
  81. str += (char)event.key.keysym.unicode;
  82. }
  83. if((event.key.keysym.unicode >= (Uint16)'a') && (event.key.keysym.unicode <= (Uint16)'z'))
  84. {
  85. str += (char)event.key.keysym.unicode;
  86. }
  87. if((event.key.keysym.unicode == SDLK_BACKSPACE) && (str.length() != 0))
  88. {
  89. str.erase(str.length() - 1);
  90. }
  91. if(str != temp)
  92. {
  93. SDL_FreeSurface(text);
  94. text = TTF_RenderText_Solid(font, str.c_str(), textColor);
  95. }
  96. }
  97. }
  98. }
  99.  
  100. void StringInput::show_centered()
  101. {
  102. if(text != NULL)
  103. {
  104. apply_surface((640 - text->w) / 2, (480 - text->h) / 2, text, screen);
  105. }
  106. }
  107.  
  108. int main(int, char**)
  109. {
  110. bool quit = false;
  111. bool nameEntered = false;
  112.  
  113. StringInput name;
  114.  
  115. TTF_Init();
  116.  
  117. SDL_Init(SDL_INIT_EVERYTHING);
  118. screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
  119.  
  120. font = TTF_OpenFont("lazy.ttf", 28);
  121.  
  122. message = TTF_RenderText_Solid(font, "Nouveau record ! Entrez votre nom :", textColor);
  123.  
  124. background = load_image("background.bmp");
  125.  
  126. //SDL_EnableUNICODE(SDL_ENABLE);
  127.  
  128. while(quit == false)
  129. {
  130. while(SDL_PollEvent(&event))
  131. {
  132. if(nameEntered == false)
  133. {
  134. name.handle_input();
  135. if((event.type == SDL_KEYDOWN) && (event.key.keysym.sym == SDLK_RETURN))
  136. {
  137. //SDL_EnableUNICODE(SDL_DISABLE);
  138. nameEntered = true;
  139. SDL_FreeSurface(message);
  140. message = TTF_RenderText_Solid(font, "Rang : 1er", textColor);
  141. }
  142. }
  143. if(event.type == SDL_QUIT)
  144. {
  145. quit = true;
  146. }
  147. }
  148. apply_surface(0, 0, background, screen);
  149. apply_surface((640 - message->w) / 2, ((480 / 2) - message->h) / 2, message, screen);
  150.  
  151. name.show_centered();
  152.  
  153. SDL_Flip(screen);
  154. }
  155.  
  156. SDL_Quit();
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement