Back to index

libsfml  1.6+dfsg2
Window.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Window.hpp>
00006 #include <fstream>
00007 
00008 
00015 int main()
00016 {
00017     // Create the main window
00018     sf::Window App(sf::VideoMode(640, 480, 32), "SFML Window");
00019  
00020     // Create a clock for measuring the time elapsed
00021     sf::Clock Clock;
00022 
00023     // Set the color and depth clear values
00024     glClearDepth(1.f);
00025     glClearColor(0.f, 0.f, 0.f, 0.f);
00026 
00027     // Enable Z-buffer read and write
00028     glEnable(GL_DEPTH_TEST);
00029     glDepthMask(GL_TRUE);
00030 
00031     // Setup a perspective projection
00032     glMatrixMode(GL_PROJECTION);
00033     glLoadIdentity();
00034     gluPerspective(90.f, 1.f, 1.f, 500.f);
00035 
00036     // Start the game loop
00037     while (App.IsOpened())
00038     {
00039         // Process events
00040         sf::Event Event;
00041         while (App.GetEvent(Event))
00042         {
00043             // Close window : exit
00044             if (Event.Type == sf::Event::Closed)
00045                 App.Close();
00046 
00047             // Escape key : exit
00048             if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
00049                 App.Close();
00050 
00051             // Resize event : adjust viewport
00052             if (Event.Type == sf::Event::Resized)
00053                 glViewport(0, 0, Event.Size.Width, Event.Size.Height);
00054         }
00055 
00056         // Set the active window before using OpenGL commands
00057         // It's useless here because the active window is always the same,
00058         // but don't forget it if you use multiple windows
00059         App.SetActive();
00060 
00061         // Clear color and depth buffer
00062         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00063 
00064         // Apply some transformations
00065         glMatrixMode(GL_MODELVIEW);
00066         glLoadIdentity();
00067         glTranslatef(0.f, 0.f, -200.f);
00068         glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
00069         glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
00070         glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
00071 
00072         // Draw a cube
00073         glBegin(GL_QUADS);
00074 
00075             glColor3f(1.f, 0.f, 0.f);
00076             glVertex3f(-50.f, -50.f, -50.f);
00077             glVertex3f(-50.f,  50.f, -50.f);
00078             glVertex3f( 50.f,  50.f, -50.f);
00079             glVertex3f( 50.f, -50.f, -50.f);
00080 
00081             glColor3f(1.f, 0.f, 0.f);
00082             glVertex3f(-50.f, -50.f, 50.f);
00083             glVertex3f(-50.f,  50.f, 50.f);
00084             glVertex3f( 50.f,  50.f, 50.f);
00085             glVertex3f( 50.f, -50.f, 50.f);
00086 
00087             glColor3f(0.f, 1.f, 0.f);
00088             glVertex3f(-50.f, -50.f, -50.f);
00089             glVertex3f(-50.f,  50.f, -50.f);
00090             glVertex3f(-50.f,  50.f,  50.f);
00091             glVertex3f(-50.f, -50.f,  50.f);
00092 
00093             glColor3f(0.f, 1.f, 0.f);
00094             glVertex3f(50.f, -50.f, -50.f);
00095             glVertex3f(50.f,  50.f, -50.f);
00096             glVertex3f(50.f,  50.f,  50.f);
00097             glVertex3f(50.f, -50.f,  50.f);
00098 
00099             glColor3f(0.f, 0.f, 1.f);
00100             glVertex3f(-50.f, -50.f,  50.f);
00101             glVertex3f(-50.f, -50.f, -50.f);
00102             glVertex3f( 50.f, -50.f, -50.f);
00103             glVertex3f( 50.f, -50.f,  50.f);
00104 
00105             glColor3f(0.f, 0.f, 1.f);
00106             glVertex3f(-50.f, 50.f,  50.f);
00107             glVertex3f(-50.f, 50.f, -50.f);
00108             glVertex3f( 50.f, 50.f, -50.f);
00109             glVertex3f( 50.f, 50.f,  50.f);
00110 
00111         glEnd();
00112 
00113         // Finally, display the rendered frame on screen
00114         App.Display();
00115     }
00116 
00117     return EXIT_SUCCESS;
00118 }