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

Author Topic: Drawing to and displaying a window causes divide by zero exception?  (Read 1567 times)

0 Members and 1 Guest are viewing this topic.

WaterNode

  • Newbie
  • *
  • Posts: 8
    • View Profile
I'm getting a divide by zero exception when I try to draw to and display a window.

Main Class:
#include "stdafx.h"

bool keys[];
stickman* player;
sf::RenderWindow window(sf::VideoMode(640, 640), "StickRPG");

int main()
{
        player = new stickman(150, 300, 300);
        sf::Clock clock;
        window.setFramerateLimit(30);
    while (window.isOpen())
    {
                sf::Event event;
                while (window.pollEvent(event))

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

                        // Drawing Code
                        window.clear();
                        player->draw(&window);
                        window.display(); // Sometimes error occurs here
                }
        }

        return 0;
}

stdafx.h:
#pragma once

#include "targetver.h"

#include <iostream>
#include <stdio.h>
#include <tchar.h>

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

#include "stickman.h"

stickman.h:
#ifndef __STICKMAN_H_INCLUDED__
#define __STICKMAN_H_INCLUDED__

class stickman
{
public:
        stickman(int size, int x, int y);
        virtual ~stickman();
        void draw(sf::RenderWindow *window);
protected:
        sf::Sprite cs;
        sf::Sprite llr;
        sf::Sprite lll;
        sf::Sprite lal;
        sf::Sprite lar;
        sf::Sprite ull;
        sf::Sprite ulr;
        sf::Sprite ual;
        sf::Sprite uar;
        sf::Vector2f armr;
        sf::Vector2f arml;
        sf::Vector2f legr;
        sf::Vector2f legl;
        sf::Vector2f p;
        sf::Vector2f n;
};

#endif

stickman.cpp:
#include "stdafx.h"

stickman::stickman(int size, int x, int y)
{
        double chest = size*.34;
        double lowerarm = size*.255;
        double lowerleg = size*.29;
        double upperarm = size*.16;
        double upperleg = size*.245;

        sf::Image ig;
        sf::Image ig1;
        sf::Image ig2;
        sf::Image ig3;
        sf::Image ig4;

        sf::Texture t;
        sf::Texture t1;
    sf::Texture t2;
        sf::Texture t3;
        sf::Texture t4;

        ig.create(2, floor(chest), sf::Color(255, 255, 255, 150));
        t.loadFromImage(ig);
        this->cs.setTexture(t);

        ig1.create(2, floor(lowerarm), sf::Color(255, 255, 255, 150));
        t1.loadFromImage(ig1);
        this->lal.setTexture(t1);
        this->lar.setTexture(t1);

        ig2.create(2, floor(lowerleg), sf::Color(255, 255, 255, 150));
        t3.loadFromImage(ig2);
        this->lll.setTexture(t2);
        this->llr.setTexture(t2);

        ig3.create(2, floor(upperarm), sf::Color(255, 255, 255, 150));
        t3.loadFromImage(ig3);
        this->uar.setTexture(t3);
        this->ual.setTexture(t3);

        ig4.create(2, floor(upperleg), sf::Color(255, 255, 255, 150));
        t4.loadFromImage(ig4);
        this->ull.setTexture(t4);

        this->n.x = x;
        this->n.y = y;
}


stickman::~stickman(void)
{
}
int round(double number);
void stickman::draw(sf::RenderWindow* window)
{
        this->cs.setPosition(this->n.x, this->n.y);
        this->ual.setPosition(this->n.x, this->n.y);
        this->uar.setPosition(this->n.x, this->n.y);
        this->uar.setRotation(45.0f);
        this->ual.setRotation(315.0f);
        this->lal.setPosition(this->ual.getPosition().x-round(this->ual.getGlobalBounds().height), this->ual.getPosition().y+round(this->ual.getGlobalBounds().width));
        this->lar.setPosition(this->uar.getPosition().x+round(this->uar.getGlobalBounds().height), this->uar.getPosition().y+round(this->uar.getGlobalBounds().width));
        window->draw(this->cs);
        window->draw(this->uar); // Error occurs here
        window->draw(this->ual);
        window->draw(this->lal);
        window->draw(this->lar); // Through here
}

int round(double number)
{
        int remainder = number-number;
        if (remainder >= 0.5) {
                return floor(number);
        } else {
                return ceil(number);
        }
}
 

I have been working on this the entire day, I have no idea what is the problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing to and displaying a window causes divide by zero exception?
« Reply #1 on: January 03, 2013, 08:44:54 am »
There are several errors in your code.

1) The sprites' textures are local to the constructor, which means that they don't exist anymore when the sprites are drawn -- which of course uses the textures.

2) Yoiu should never create a window or any "heavy" SFML object at global scope, behaviour is undefined becvause SFML uses globals too.

3) I don't think that your round function works as expected.
« Last Edit: January 03, 2013, 08:46:34 am by Laurent »
Laurent Gomila - SFML developer

 

anything