Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. #include "object.h"
  6.  
  7. void D_DrawText(sf::RenderWindow &win, sf::String &str, float x, float y, std::string text);
  8. sf::String D_CreateText(sf::Font font, sf::Color color = sf::Color(255,255,255), float size = 16.f);
  9.  
  10. template <typename T>
  11. std::string toStr(T input)
  12. {
  13.     std::stringstream stream;
  14.     stream << input;
  15.     return stream.str();
  16. }
  17.  
  18. int main()
  19. {
  20.     // Create the main rendering window
  21.     sf::RenderWindow App(sf::VideoMode(800, 600, 32), "C++ peli", sf::Style::Close);
  22.  
  23.     sf::Font myFont;
  24.     if (!myFont.LoadFromFile(getenv("windir") + std::string("\\Fonts\\arial.ttf")))
  25.         return EXIT_FAILURE;
  26.  
  27.     sf::String fps = D_CreateText(myFont, sf::Color(255, 0, 0));
  28.  
  29.     Object player(App);
  30.     player.Load("img/player.png");
  31.  
  32.     // Start game loop
  33.     while (App.IsOpened())
  34.     {
  35.         // Process events
  36.         sf::Event Event;
  37.         while (App.GetEvent(Event))
  38.         {
  39.             // Close window : exit
  40.             if (Event.Type == sf::Event::Closed)
  41.                 App.Close();
  42.         }
  43.  
  44.         // Clear the screen (fill it with black color)
  45.         App.Clear(sf::Color(255, 255, 255));
  46.  
  47.         //D_DrawText(App, 10, 10, "Hello World!", myFont);
  48.         D_DrawText(App, fps, 10, 40, "FPS: " + toStr(1 / App.GetFrameTime()));
  49.  
  50.         // Display window contents on screen
  51.         App.Display();
  52.     }
  53.  
  54.     return EXIT_SUCCESS;
  55. }
  56.  
  57. sf::String D_CreateText(sf::Font font, sf::Color color, float size)
  58. {
  59.     sf::String str;
  60.     str.SetFont(font);
  61.     str.SetColor(color);
  62.     str.SetSize(size);
  63.  
  64.     return str;
  65. }
  66.  
  67. void D_DrawText(sf::RenderWindow &win, sf::String &str, float x, float y, std::string text)
  68. {
  69.     str.SetText(text);
  70.     str.SetPosition(x, y);
  71.  
  72.     win.Draw(str);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement