Advertisement
Guest User

Untitled

a guest
May 30th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <SFML/Window.hpp>
  4.  
  5. // ============================================================================
  6.  
  7. bool gameActive = true;
  8. sf::Mutex mutexWindow;
  9.  
  10. // ============================================================================
  11.  
  12. class GameThread: public sf::Thread
  13. {
  14. public:
  15.    
  16.     GameThread(sf::Window& window)
  17.         :window(window)
  18.     {
  19.     }
  20.    
  21.     virtual ~GameThread() {}
  22.    
  23. private:
  24.    
  25.     sf::Window& window;
  26.    
  27.     virtual void Run()
  28.     {
  29.         const sf::Input& input = window.GetInput();
  30.         sf::Clock clock;
  31.         while (gameActive) {
  32.            
  33.             float elapsedTime = clock.GetElapsedTime();
  34.             if (elapsedTime < .01f) {
  35.                 sf::Sleep(.01f - elapsedTime);
  36.             }
  37.             elapsedTime = clock.GetElapsedTime();
  38.             clock.Reset();
  39.            
  40.            
  41.         }
  42.     }
  43. };
  44.  
  45. // ============================================================================
  46.  
  47. class RenderThread: public sf::Thread
  48. {
  49. public:
  50.    
  51.     RenderThread(sf::Window& window)
  52.         :window(window)
  53.     {
  54.     }
  55.    
  56.     virtual ~RenderThread() {}
  57.    
  58. private:
  59.    
  60.     sf::Window& window;
  61.    
  62.     virtual void Run()
  63.     {
  64.         while (gameActive) {
  65. //          sf::Lock lock(mutexWindow);
  66.             window.SetActive(true);
  67.             glClear(GL_COLOR_BUFFER_BIT);
  68.             window.Display();
  69. //          window.SetActive(false);
  70.         }
  71.     }
  72. };
  73.  
  74. // ============================================================================
  75.  
  76. int main(int argc, char **argv)
  77. {
  78.    
  79.     sf::Window window(sf::VideoMode(1024, 768, 32), "Gaya", sf::Style::Close);
  80.     window.SetActive(false);
  81.     window.UseVerticalSync(true);
  82.    
  83.     bool mouseCaptured = false;
  84.     int windowWidthHalf = window.GetWidth() >> 1;
  85.     int windowHeightHalf = window.GetHeight() >> 1;
  86.    
  87. //  GameThread gameThread(window);
  88. //  gameThread.Launch();
  89.     RenderThread renderThread(window);
  90.     renderThread.Launch();
  91.    
  92.     sf::Clock clock;
  93.     sf::Event event;
  94.     while (gameActive) {
  95.        
  96.         float elapsedTime = clock.GetElapsedTime();
  97.         if (elapsedTime < .01f) {
  98.             sf::Sleep(.01f - elapsedTime);
  99.         }
  100.         elapsedTime = clock.GetElapsedTime();
  101.         clock.Reset();
  102.        
  103.         {
  104. //          sf::Lock lock(mutexWindow);
  105. //          window.SetActive(true);
  106.            
  107.             while (window.GetEvent(event)) {
  108.                 if (event.Type == sf::Event::Closed) {
  109.                     gameActive = false;
  110.                 }
  111. //              if (mouseCaptured) {
  112. //                  if (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Escape) {
  113. //                      window.ShowMouseCursor(true);
  114. //                      mouseCaptured = false;
  115. //                  }
  116. //              } else {
  117. //                  if (event.Type == sf::Event::MouseButtonPressed && event.MouseButton.Button == 0) {
  118. //                      window.ShowMouseCursor(false);
  119. //                      mouseCaptured = true;
  120. //                  }
  121. //              }
  122.             }
  123.            
  124. //          if (mouseCaptured) {
  125. //              window.SetCursorPosition(windowWidthHalf, windowHeightHalf);
  126. //          }
  127.            
  128. //          window.SetActive(false);
  129.         }
  130.     }
  131.    
  132. //  gameThread.Wait();
  133.     renderThread.Wait();
  134.     window.Close();
  135.    
  136.     return EXIT_SUCCESS;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement