Advertisement
Guest User

Untitled

a guest
Aug 17th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1.  
  2. ////////////////////////////////////////////////////////////
  3. // Headers
  4. ////////////////////////////////////////////////////////////
  5.  
  6.  
  7. #define SFML_DYNAMIC
  8.  
  9. #include <iostream>
  10. #include <sstream>
  11. #include <string>
  12.  
  13.  
  14. #include <ctime>
  15. #include <string.h>
  16. #include <map>
  17.  
  18. #include <SFML/System.hpp>
  19. #include <SFML/Window.hpp>
  20. #include <SFML/Graphics.hpp>
  21. #include <SFML/Graphics/RenderWindow.hpp>
  22. #include <windows.h>
  23.  
  24.  
  25.  
  26. using namespace sf;
  27.  
  28.  
  29.  
  30. #define TANK1_FORWARD 10
  31. #define TANK1_BACK 11
  32. #define TANK1_LEFT 12
  33. #define TANK1_RIGHT 13
  34. #define TANK1_FIRE 14
  35.  
  36. #define TANK2_FORWARD 20
  37. #define TANK2_BACK 21
  38. #define TANK2_LEFT 22
  39. #define TANK2_RIGHT 23
  40. #define TANK2_FIRE 24
  41.  
  42.  
  43. int width, height;
  44.  
  45.  
  46. sf::Clock Clock;
  47.  
  48. void initWindow () {
  49.  
  50. width = 1024;
  51. height = 810;
  52. }
  53.  
  54. std::string ftos ( float x ) {
  55.  
  56. std::ostringstream oss;
  57. oss << x;
  58. return oss.str();
  59. }
  60.  
  61. void defaultControll ( std::map<int, int>& pole ) {
  62.  
  63. pole.insert( std::make_pair( TANK1_FORWARD, Key::Up ));
  64. pole.insert( std::make_pair( TANK1_BACK, Key::Down ));
  65. pole.insert( std::make_pair( TANK1_LEFT, Key::Left ));
  66. pole.insert( std::make_pair( TANK1_RIGHT, Key::Right ));
  67. pole.insert( std::make_pair( TANK1_FIRE, Key::Space ));
  68.  
  69. pole.insert( std::make_pair( TANK2_FORWARD, Key::W ));
  70. pole.insert( std::make_pair( TANK2_BACK, Key::S ));
  71. pole.insert( std::make_pair( TANK2_LEFT, Key::A ));
  72. pole.insert( std::make_pair( TANK2_RIGHT, Key::D ));
  73. pole.insert( std::make_pair( TANK2_FIRE, Key::LControl ));
  74.  
  75. }
  76.  
  77. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  78. {
  79.  
  80. initWindow ();
  81. //std::map< Key::H ,int> pole;
  82.  
  83.  
  84. // Create main window
  85. RenderWindow App;
  86. //App.Create(sf::VideoMode(width, height), "SFML Fonts");
  87. App.Create(VideoMode(width, height, 16), "SFML Test", Style::Fullscreen );
  88.  
  89.  
  90. App.Clear( sf::Color( 0, 255, 255) );
  91.  
  92. //sf::RenderWindow::SetBackgroundColor ( const Color & Col )
  93. Event input;
  94. bool fullscreen = true;
  95. float timeCounter = 0;
  96. float framesCounter = 0;
  97. float frameLatencyCounter = 0;
  98. float fNumber = 0.f;
  99. int speed = 5;
  100. // Load a font from a file
  101. Font MyFont;
  102. if (! MyFont.LoadFromFile("Veloped.ttf", 50))
  103. return EXIT_FAILURE;
  104.  
  105.  
  106. sf::Clock Clock;
  107.  
  108.  
  109. // Create a graphical string
  110. String Hello;
  111. Hello.SetText("Hello !\nHow are you ?");
  112. Hello.SetFont( MyFont );
  113. Hello.SetColor( Color(0, 128, 128));
  114. Hello.SetPosition(100.f, 100.f);
  115. //Hello.SetRotation(15.f);
  116. Hello.SetSize(50.f);
  117.  
  118.  
  119. String TimeText;
  120. TimeText.SetText( "abcd");
  121.  
  122. TimeText.SetColor( Color(128, 128, 128));
  123. TimeText.SetPosition(0.f, 0.f);
  124. TimeText.SetSize(20.f);
  125.  
  126. float Framerate = 0.f;
  127. float msecSave = 0;
  128.  
  129. unsigned int odStartuSecSave = 0;
  130. unsigned int odStartuSec = (unsigned int) ( ((int) clock()) / 1000);
  131.  
  132. // Start game loop
  133. while (App.IsOpened())
  134. {
  135. // Process events
  136. App.Clear( sf::Color(255,255,255) );
  137.  
  138. while (App.GetEvent(input)) {
  139. //Exit
  140. if (input.Type == Event::Closed) App.Close();
  141. if ( (input.Type == Event::KeyPressed) && (input.Key.Code == Key::Escape) )
  142. App.Close();
  143.  
  144. if ( (input.Type == Event::KeyPressed) && (input.Key.Code == Key::H ) )
  145. App.Window::Show ( false );
  146.  
  147. if ( (input.Type == Event::KeyPressed) && (input.Key.Code == Key::S ) )
  148. App.Window::Show ( true );
  149.  
  150.  
  151. //Fullscreen toggle
  152. if (( input.Type == Event::KeyPressed ) && (input.Key.Code == Key::F ) ) {
  153.  
  154. fullscreen = !fullscreen;
  155. if ( fullscreen )
  156. App.Create(VideoMode(width, height, 16), "SFML Test", Style::Fullscreen );
  157. else
  158. App.Create(VideoMode(width, height, 16), "SFML Test", Style::Resize|Style::Close );
  159. }
  160. }
  161.  
  162. App.Clear( sf::Color( 0, 255, 255) );
  163. TimeText.SetPosition(30.f, 30.f);
  164.  
  165.  
  166.  
  167. float Framerate = 1.f / Clock.GetElapsedTime();
  168. Clock.Reset();
  169.  
  170.  
  171. framesCounter++;
  172. frameLatencyCounter += Framerate;
  173. odStartuSec = (unsigned int) (clock()/ 1000);
  174.  
  175. if ( odStartuSecSave != odStartuSec ) {
  176. odStartuSecSave = odStartuSec;
  177. speed = ( speed + 2 ) % 30;
  178.  
  179. Framerate = frameLatencyCounter / framesCounter;
  180. framesCounter = 0;
  181. frameLatencyCounter = 0;
  182.  
  183. timeCounter = 0;
  184.  
  185. TimeText.SetText( ftos ( Framerate ) );
  186. std::cout << "====" << odStartuSec << std::endl;
  187. } else {
  188.  
  189. std::cout << "<" << odStartuSec << std::endl;
  190. }
  191.  
  192.  
  193.  
  194. // std::count << Time ;
  195.  
  196. const Vector2f& positionVector = Hello.GetPosition ();
  197. if ( positionVector.x + speed <= width )
  198. Hello.SetPosition(positionVector.x + speed, positionVector.y);
  199. else
  200. Hello.SetPosition( 0, positionVector.y);
  201.  
  202.  
  203.  
  204.  
  205. App.Draw(Hello);
  206. App.Draw(TimeText);
  207.  
  208. App.Display();
  209.  
  210. }
  211.  
  212. return EXIT_SUCCESS;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement