16
Graphics / RenderWindow not showing OpenGL elements
« on: June 04, 2008, 12:17:37 am »
Upgrading to the latest SVN yesterday seems to have got rid of the unhandled exception I was getting when pressing a key to end the program.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
You've found the solution, so... what's the purpose of this post ?
Which SFML version ? Which OS ?
If you create the window in main() (using App.Create(...)), do the problems disappear ?
Works just great.
Got a new little problem...
I have a lot of tiles in one texture and only wants to draw one at a time instead of the whole image. After some digging around in the docs I came up with this. But the result is a bit wrong.
The left one is the one produced by SFML, the right one is what I was expecting.
As you can see, the left one only got half of the edge pixels drawn.
The relevant code used:Code: [Select]
tiles_gfx = Image.new("tiles.png")
tiles_gfx.setSmooth( false )
tiles = Sprite.new( tiles_gfx )
# ... later in the source
tiles.setSubRect( IntRect.new(0,0,8,8) )
win.draw( tiles )
Looking forward to this game...
I think i would also try something similar using SFML....
I've just fixed it. Thank you very much for your feedback.
#include <SFML/Graphics.hpp>
#include <fstream>
#include <iostream>
int main()
{
// Create main window
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL");
// Create a sprite for the background
sf::Image BackgroundImage;
if (!BackgroundImage.LoadFromFile("data/sky2.png"))
return EXIT_FAILURE;
sf::Sprite Background(BackgroundImage);
// Load an OpenGL texture.
// We could directly use a sf::Image 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
GLuint Texture = 0;
{
sf::Image Image;
if (!Image.LoadFromFile("data/crate.png"))
return EXIT_FAILURE;
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());
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);
// Set The Viewport To The Top Left 1/4
glViewport (16, 284, 400, 300); // was 300
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
gluPerspective(60.f, 1.f, 0.1f, 500.f); // was 45 degrees
// Bind our texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture);
glColor4f(1.f, 1.f, 1.f, 1.f);
// Create a clock for measuring the time elapsed
sf::Clock Clock;
// Start game loop
bool Running = true;
while (Running)
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
Running = false;
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Running = false;
}
// Draw background
App.Draw(Background);
// App.BeginOpenGL();
// Clear depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
// Draw a cube
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f(50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f(50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, 50.f);
glEnd();
// App.EndOpenGL();
// Draw some text on top of our OpenGL object
sf::String Text("This is a rotating cube");
Text.SetSize(20.f);
Text.SetFont("data/Celtic Garamond.ttf");
Text.SetPosition(230.f, 300.f);
Text.SetColor(sf::Color(255, 255, 255));
App.Draw(Text);
// Finally, display rendered frame on screen
App.Display();
}
// Don't forget to destroy our texture
glDeleteTextures(1, &Texture);
return EXIT_SUCCESS;
}