Hi,
I am creating a game and have gotten to the point of drawing stuff on the screen now. However, I am getting crashes across multiple functions including RenderWindow::clear and RenderWindow::draw.
I have built SFML and my program in debug mode to trace where things are going wrong. (It's getting an access violation error trying to read memory at 0x00000008) Both functions seem to crash when calling a function called activate, seen here:
void RenderTarget::draw(const Vertex* vertices, unsigned int vertexCount,
PrimitiveType type, const RenderStates& states)
{
// Nothing to draw?
if (!vertices || (vertexCount == 0))
return;
if (activate(true)) // <-------- crashes here
{
////////////////////////////////////////////////////////////
void RenderTarget::clear(const Color& color)
{
if (activate(true)) // <----------- crashes here
{
Furthermore, I have traced that function down to a function called GlContext::setActive as seen here:
bool GlContext::setActive(bool active)
{
if (active)
{
if (this != currentContext)
{
// Activate the context
if (makeCurrent())
{
// Set it as the new current context for this thread
currentContext = this;
return true;
}
else
{
return false;
}
}
else
{
// This context is already the active one on this thread, don't do anything
return true;
}
}
else
{
if (this == currentContext)
{
// To deactivate the context, we actually activate another one so that we make
// sure that there is always an active context for subsequent graphics operations
return getInternalContext()->setActive(true);
}
else
{
// This context is not the active one on this thread, don't do anything
return true;
}
}
}
I'm assuming one of the checks here is wrong or something like that. Some more info:
Compiler: Visual C++ 2012
Version: Compiled from the git repository. (3/30/2013)
Graphics card: AMD Radeon HD 6850
The source code I am using to reproduce the error: (Uncomment the clear/draw calls to test each one, they both crash)
// untitled.cpp
// Written by Snail
#include <iostream>
#include <sfml/graphics.hpp>
#include <constants.hpp>
#include <untitled.hpp>
int main(int number_of_arguments, char **arguments)
{
sf::RenderWindow window(sf::VideoMode(800, 600), PROGRAM_NAME);
window.setVerticalSyncEnabled(true);
sf::Clock elapsed_time;
sf::Event event;
sf::Sprite player;
sf::Texture texture;
if(!texture.loadFromFile("sprite.png"))
std::cout << "Could not load sprite.png" << std::endl;
player.setTexture(texture);
player.setPosition(100, 100);
sf::Time fps;
sf::Time one_second = sf::seconds(1.0);
sf::Time zero = sf::seconds(0.0);
while(window.isOpen())
{
if(elapsed_time.getElapsedTime() >= one_second)
{
std::cout << fps.asSeconds() << std::endl;
elapsed_time.restart();
fps = zero;
}
else
fps += elapsed_time.getElapsedTime();
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch(event.key.code)
{
case sf::Keyboard::Escape:
window.close();
break;
default:
break;
}
break;
default:
break;
}
}
window.clear(sf::Color(255, 255, 255));
//window.draw(player);
window.display();
}
return 0;
}
If anyone has any insight into this, please let me know. Thanks!