2
« le: Avril 09, 2012, 11:56:13 am »
Salut!
J'ai un problème quand j'éssaille de créer une deuxième fenetre sous os x, le display ne marche pas correctement. Tout marche bien si on ferme la fenetre avant d'ouvrir une nouvelle et puis quand on ferme cette deuxieme on reouvre la 1ere. Voila un code minimal, j'utilise de l'OpenGL
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
void glDrawCube(float size);
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
glColor4f(1.f, 1.f, 1.f, 1.f);
// Create a clock for measuring the time elapsed
sf::Clock clock;
// Start game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
// Escape key : exit
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::W)
{
//window.close();
sf::RenderWindow window2(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
window2.setVerticalSyncEnabled(true);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
glColor4f(1.f, 1.f, 1.f, 1.f);
while (window2.isOpen())
{
sf::Event event2;
while (window2.pollEvent(event2))
{
// Close window : exit
if (event2.type == sf::Event::Closed)
window2.close();
// Escape key : exit
if ((event2.type == sf::Event::KeyPressed) && (event2.key.code == sf::Keyboard::Escape))
window2.close();
// Adjust the viewport when the window is resized
if (event2.type == sf::Event::Resized)
glViewport(0, 0, event2.size.width, event2.size.height);
}
window2.setActive();
// Clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// We get the position of the mouse cursor, so that we can move the box accordingly
float x = sf::Mouse::getPosition(window2).x * 200.f / window2.getSize().x - 100.f;
float y = -sf::Mouse::getPosition(window2).y * 200.f / window2.getSize().y + 100.f;
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, -100.f);
glRotatef(clock.getElapsedTime().asSeconds() * 50.f, 1.f, 0.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 30.f, 0.f, 1.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 90.f, 0.f, 0.f, 1.f);
glDrawCube(20.f);
// Finally, display the rendered frame on screen
window2.display();
}
//window.create(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
glColor4f(1.f, 1.f, 1.f, 1.f);
}
// Adjust the viewport when the window is resized
if (event.type == sf::Event::Resized)
glViewport(0, 0, event.size.width, event.size.height);
}
// Activate the window before using OpenGL commands.
// This is useless here because we have only one window which is
// always the active one, but don't forget it if you use multiple windows
window.setActive();
// Clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// We get the position of the mouse cursor, so that we can move the box accordingly
float x = sf::Mouse::getPosition(window).x * 200.f / window.getSize().x - 100.f;
float y = -sf::Mouse::getPosition(window).y * 200.f / window.getSize().y + 100.f;
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, -100.f);
glRotatef(clock.getElapsedTime().asSeconds() * 50.f, 1.f, 0.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 30.f, 0.f, 1.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 90.f, 0.f, 0.f, 1.f);
glDrawCube(20.f);
// Finally, display the rendered frame on screen
window.display();
}
return EXIT_SUCCESS;
}
void glDrawCube(float size)
{
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
glTexCoord2f(0, 1); glVertex3f(-size, size, -size);
glTexCoord2f(1, 1); glVertex3f( size, size, -size);
glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
glTexCoord2f(0, 1); glVertex3f(-size, size, size);
glTexCoord2f(1, 1); glVertex3f( size, size, size);
glTexCoord2f(1, 0); glVertex3f( size, -size, size);
glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
glTexCoord2f(0, 1); glVertex3f(-size, size, -size);
glTexCoord2f(1, 1); glVertex3f(-size, size, size);
glTexCoord2f(1, 0); glVertex3f(-size, -size, size);
glTexCoord2f(0, 0); glVertex3f(size, -size, -size);
glTexCoord2f(0, 1); glVertex3f(size, size, -size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, -size, size);
glTexCoord2f(0, 1); glVertex3f(-size, -size, size);
glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
glTexCoord2f(1, 1); glVertex3f( size, -size, size);
glTexCoord2f(0, 1); glVertex3f(-size, size, size);
glTexCoord2f(0, 0); glVertex3f(-size, size, -size);
glTexCoord2f(1, 0); glVertex3f( size, size, -size);
glTexCoord2f(1, 1); glVertex3f( size, size, size);
glEnd();
}
Si on enleve les commentaires pour fermer et ouvrir les fenetres tout va bien!
merci!