2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Graphics.hpp>
|
2009-03-29 00:22:58 +08:00
|
|
|
#include <SFML/OpenGL.hpp>
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2012-03-08 06:29:54 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
2009-11-05 00:04:18 +08:00
|
|
|
// Create the main window
|
2011-04-05 05:37:20 +08:00
|
|
|
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
|
2012-03-08 06:29:54 +08:00
|
|
|
window.SetVerticalSyncEnabled(true);
|
2009-09-24 15:50:08 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
// Create a sprite for the background
|
2011-07-23 04:31:27 +08:00
|
|
|
sf::Texture backgroundTexture;
|
|
|
|
if (!backgroundTexture.LoadFromFile("resources/background.jpg"))
|
2009-01-29 00:18:34 +08:00
|
|
|
return EXIT_FAILURE;
|
2011-07-23 04:31:27 +08:00
|
|
|
sf::Sprite background(backgroundTexture);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Load an OpenGL texture.
|
2011-07-23 04:31:27 +08:00
|
|
|
// We could directly use a sf::Texture as an OpenGL texture (with its Bind() member function),
|
|
|
|
// but here we want more control on it (generate mipmaps, ...) so we create a new one from an image
|
2009-07-12 06:17:24 +08:00
|
|
|
GLuint texture = 0;
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::Image image;
|
2010-08-19 23:59:24 +08:00
|
|
|
if (!image.LoadFromFile("resources/texture.jpg"))
|
2009-01-29 00:18:34 +08:00
|
|
|
return EXIT_FAILURE;
|
2009-07-12 06:17:24 +08:00
|
|
|
glGenTextures(1, &texture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, image.GetPixelsPtr());
|
2009-01-29 00:18:34 +08:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Bind our texture
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
2009-07-12 06:17:24 +08:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
2009-01-29 00:18:34 +08:00
|
|
|
glColor4f(1.f, 1.f, 1.f, 1.f);
|
|
|
|
|
|
|
|
// Create a clock for measuring the time elapsed
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::Clock clock;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Start game loop
|
2012-01-13 21:53:36 +08:00
|
|
|
while (window.IsOpen())
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Process events
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::Event event;
|
2011-04-12 00:20:21 +08:00
|
|
|
while (window.PollEvent(event))
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Close window : exit
|
2009-07-12 06:17:24 +08:00
|
|
|
if (event.Type == sf::Event::Closed)
|
|
|
|
window.Close();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Escape key : exit
|
2011-07-04 14:21:40 +08:00
|
|
|
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Keyboard::Escape))
|
2009-07-12 06:17:24 +08:00
|
|
|
window.Close();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Adjust the viewport when the window is resized
|
2009-07-12 06:17:24 +08:00
|
|
|
if (event.Type == sf::Event::Resized)
|
|
|
|
glViewport(0, 0, event.Size.Width, event.Size.Height);
|
2009-09-24 15:50:08 +08:00
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-09-24 15:50:08 +08:00
|
|
|
// Draw the background
|
2011-12-02 06:24:58 +08:00
|
|
|
window.PushGLStates();
|
2009-07-12 06:17:24 +08:00
|
|
|
window.Draw(background);
|
2011-12-02 06:24:58 +08:00
|
|
|
window.PopGLStates();
|
2009-09-24 15:50:08 +08:00
|
|
|
|
2009-11-05 00:04:18 +08:00
|
|
|
// 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
|
2009-09-24 15:50:08 +08:00
|
|
|
window.SetActive();
|
|
|
|
|
|
|
|
// Clear the depth buffer
|
2009-01-29 00:18:34 +08:00
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2009-02-06 21:54:39 +08:00
|
|
|
// We get the position of the mouse cursor, so that we can move the box accordingly
|
2012-03-08 06:29:54 +08:00
|
|
|
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;
|
2009-02-06 21:54:39 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
// Apply some transformations
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
2009-07-12 06:17:24 +08:00
|
|
|
glTranslatef(x, y, -100.f);
|
2012-01-25 17:27:29 +08:00
|
|
|
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);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Draw a cube
|
2009-07-12 06:17:24 +08:00
|
|
|
float size = 20.f;
|
2009-01-29 00:18:34 +08:00
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
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);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
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);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
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);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
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);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
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);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
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);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
// Draw some text on top of our OpenGL object
|
2011-12-02 06:24:58 +08:00
|
|
|
window.PushGLStates();
|
2009-11-26 16:07:38 +08:00
|
|
|
sf::Text text("SFML / OpenGL demo");
|
2009-07-12 06:17:24 +08:00
|
|
|
text.SetColor(sf::Color(255, 255, 255, 170));
|
2011-12-02 06:24:58 +08:00
|
|
|
text.SetPosition(250.f, 450.f);
|
2009-07-12 06:17:24 +08:00
|
|
|
window.Draw(text);
|
2011-12-02 06:24:58 +08:00
|
|
|
window.PopGLStates();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Finally, display the rendered frame on screen
|
2009-07-12 06:17:24 +08:00
|
|
|
window.Display();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't forget to destroy our texture
|
2009-07-12 06:17:24 +08:00
|
|
|
glDeleteTextures(1, &texture);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|