#includeint main() { sf::RenderWindow window(sf::VideoMode(800, 600), "SFML animation"); sf::CircleShape circle(50); circle.setFillColor(sf::Color::Red); sf::Clock clock; while (window.isOpen()) { sf::Time elapsed = clock.restart(); float delta = elapsed.asSeconds(); // move the circle circle.move(100 * delta, 0); // check for window events sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } // draw the circle to the window window.clear(); window.draw(circle); window.display(); } return 0; }
#includeIn this example, OpenGL library is used to create a simple 3D animation where a rotating triangle is displayed on the screen. The `glutInit()` and `glutCreateWindow()` functions are used to create a window and set up the context. The `glEnable()` function is used to enable depth testing. The `glBegin()` and `glEnd()` functions are used to define the triangle's vertices and colors. The rotation angle is updated in the `update()` function using `glutTimerFunc()`, which also schedules the next update. The `display()` function is used to draw the scene and the `glutSwapBuffers()` function is used to swap the front and back buffers. Lastly, the main loop is started using `glutMainLoop()`. Package libraries used in these examples are SFML and OpenGL.float angle = 0.0f; void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -5.0f); glRotatef(angle, 0.0f, 1.0f, 0.0f); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, 0.0f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 1.0f, 0.0f); glEnd(); glutSwapBuffers(); } void update(int value) { angle += 0.5f; glutPostRedisplay(); glutTimerFunc(16, update, 0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 600); glutCreateWindow("OpenGL animation"); glEnable(GL_DEPTH_TEST); glutDisplayFunc(display); glutTimerFunc(0, update, 0); glutMainLoop(); return 0; }