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

Author Topic: Zero division issue and empty window  (Read 1077 times)

0 Members and 1 Guest are viewing this topic.

streamholder

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Zero division issue and empty window
« on: October 05, 2012, 08:46:55 pm »
Hello everybody!
I recently came across this amazing library which I will use for my new 2D game.

I have got two issues, both of them regarding this little program:
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <math.h>
#include <stdlib.h>
#include <time.h>

long double angle = 0;
int rectPos = 0;

int main()
{
        sf::RenderWindow window(sf::VideoMode(1280, 780), "SFML works!");

        sf::CircleShape circle(150, 200);
        circle.setOutlineColor(sf::Color(0, 0, 255, 255));
        circle.setOutlineThickness(2);
        circle.setFillColor(sf::Color(0, 0, 0, 0));
        circle.setPosition(sf::Vector2f(5, 5));

        sf::RectangleShape rect(sf::Vector2f(50.0, 10.0));
        rect.setFillColor(sf::Color(255, 0, 0, 255));
        rect.setPosition(sf::Vector2f(0, 350));

        sf::Text text;
        text.setPosition(sf::Vector2f(10, 400));

        sf::Clock clock;

        int cycle = 0;
        float rt = 0;

        sf::String fpsStr;

        //window.setFramerateLimit(60);

        while (window.isOpen())
        {
                sf::Time time = clock.restart();
                rt += time.asMilliseconds();
               
                if (++cycle == 100)
                {
                        cycle = 0;
                        std::stringstream wss;
                        wss << "Frame rendering time (avg. of 100 frames): ";
                        wss << rt / 100;
                        rt = 0;
                        fpsStr.clear();
                        fpsStr.insert(0, sf::String(wss.str()));
                }

                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();

                // drawing!

                window.draw(circle);

                angle += 0.02;

                sf::Vertex vertices[] =
                {
                        sf::Vertex(sf::Vector2f(155.0, 155.0), sf::Color::Red),
                        sf::Vertex(sf::Vector2f((int)(150.0 + cos(angle) * 150.0) + 5, (int)(150.0 - sin(angle) * 150.0) + 5), sf::Color::Red)
                };

                window.draw(vertices, 2, sf::PrimitiveType::Lines);
               
                rectPos++;
               
                if (rectPos > (int)window.getSize().x) rectPos = -50;
                rect.setPosition(sf::Vector2f((float)rectPos, 350));
                window.draw(rect);

                text.setString(fpsStr);
                window.draw(text);

                unsigned int random = rand() % 100 + 100;

                for (int i = 0; i < random; i++)
                {
                        sf::CircleShape tmpCircle(rand() % 20 + 10);
                        tmpCircle.setFillColor(sf::Color(rand() % 256, rand() % 256, rand() % 256, rand() % 256));
                        tmpCircle.setOutlineColor(sf::Color(rand() % 256, rand() % 256, rand() % 256, rand() % 256));
                        tmpCircle.setPosition(sf::Vector2f(rand() % (int)window.getSize().x, rand() % (int)window.getSize().y));
                        window.draw(tmpCircle);
                }

                // end drawing!

                window.display();
        }

        return 0;
}
 

The first issue is that when I exit the program I get a zero division exception (if I'm attached to Visual Studio's debugger) or the classic crash report window, if I'm running a Release build.

The second issue is that, when I sent my program (statically linked with SFML libraries, built with Release profile) to a friend, when he tries to open it, it can see two windows on the taskbar: one for the console, which shows normally, and one for the window that should be the RenderWindow... But it is like "invisible", it exists in the taskbar but there's no way to see it :(

FRex

  • Hero Member
  • *****
  • Posts: 1839
  • Not doing gamedev/using SFML lately
    • View Profile
    • Email
Re: Zero division issue and empty window
« Reply #1 on: October 05, 2012, 10:20:16 pm »
This video mode is too effin huge, use desktop mode or something like 600x600, my laptop is 1366x768, this video mode is too huge for me. Windows with video modes too huge dont appear within desktop unless you set position manualy. Just avoid it.

event loop when close is clicked:
window.close();//close the window, nonexistent window is 0x0..
a bit later:
tmpCircle.setPosition(sf::Vector2f(rand() % (int)window.getSize().x, rand() % (int)window.getSize().y));
//doh!

Also, don't use xxx.h, use cxxx, don't use c styled casts, don't introduce signed unsigned comparsions ect.
« Last Edit: October 06, 2012, 12:00:06 am by FRex »
I'm not using SFML nor doing any gamedev lately.

 

anything