Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] RenderWindow - Stack Overflow  (Read 4025 times)

0 Members and 1 Guest are viewing this topic.

Akrotrili

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Bonjour
[Solved] RenderWindow - Stack Overflow
« on: August 28, 2013, 02:04:07 am »
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
« Last Edit: August 28, 2013, 12:14:16 pm by Akrotrili »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: RenderWindow - Stack Overflow
« Reply #1 on: August 28, 2013, 02:26:37 am »
Use std::vector for numbers, you ran out of stack space probably.
Back to C++ gamedev with SFML in May 2023

Akrotrili

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Bonjour
Re: RenderWindow - Stack Overflow
« Reply #2 on: August 28, 2013, 12:14:03 pm »
The shame of this is that I thought about that, but the debugger has troubled me. :P
Anyway, thanks.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Solved] RenderWindow - Stack Overflow
« Reply #3 on: August 28, 2013, 02:55:52 pm »
#define WIDTH 500
#define HEIGHT 500
#define MAX WIDTH*HEIGHT
#define SCALE 1
Prefer constants over macros; they have several advantages, most notably the scope:
const unsigned int WIDTH = 500;
const unsigned int HEIGHT = 500;
const unsigned int MAX = WIDTH*HEIGHT;
const float SCALE = 1.f;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: