Hey there,
I just made a little program using the sieve of Eratosthenes to create an image which show if a number is prime or not (just like
that).
It works just fine, but if the size of the window is over 498*498. There's a stack overflow, the error is supposed to be on the 16th line (where I'm creating the window).
Using SFML 2.1, and working with Visual Studio 2012.
Here's the source code :
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <cmath>
#define WIDTH 500
#define HEIGHT 500
#define MAX WIDTH*HEIGHT
#define SCALE 1
int WinMain()
{
sf::RenderWindow window(sf::VideoMode(WIDTH*SCALE, HEIGHT*SCALE), "Primes");
window.setVerticalSyncEnabled(true);
sf::Event event;
sf::Image image;
image.create(WIDTH, HEIGHT);
sf::Texture texture;
texture.create(WIDTH, HEIGHT);
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setScale(SCALE, SCALE);
unsigned int numbers[MAX], primeBase = 2;
// Initializing the array
for(size_t i = 0 ; i < MAX ; ++i)
numbers[i] = i+1;
// Finding primes
while(primeBase < sqrt(MAX))
{
// Replacing non-primes by 0
for(size_t i = primeBase ; i*primeBase <= MAX ; ++i)
numbers[i*primeBase-1] = 0;
// Changing the value of primeBase
for(size_t i = primeBase ; i < MAX ; ++i)
{
if(numbers[i] != 0)
{
primeBase = numbers[i];
break;
}
}
}
// Creating the image
for(size_t i = 0 ; i < MAX ; ++i)
{
if(numbers[i] > 1)
image.setPixel(i%WIDTH, i/WIDTH, sf::Color::Green);
}
texture.update(image);
// Main loop
while(window.isOpen())
{
// Event loop
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
// Displaying the image
window.clear();
window.draw(sprite);
window.display();
}
}
Thank you for the help. \o