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

Author Topic: Crashes when drawing layers  (Read 2436 times)

0 Members and 1 Guest are viewing this topic.

mothmann

  • Newbie
  • *
  • Posts: 8
    • View Profile
Crashes when drawing layers
« on: February 11, 2011, 05:34:51 am »
Code: [Select]
//Main.cpp
#include "stdafx.h"
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <string>
#include "Layer.h"
#include <iostream>

int stagew = 1280;
int stageh = 720;
int stagebpp = 32;

int _tmain(int argc, _TCHAR* argv[])
{

Layer lay;
sf::Sprite * spr;
sf::Image ima;
if (ima.LoadFromFile("graphics/tiles.bmp")==true)
std::cout << "good";
spr->SetImage(ima);

lay.push_back(spr);

sf::RenderWindow App(sf::VideoMode(stagew,stageh,stagebpp), "x");

while(App.IsOpened())
{
App.Clear();
App.Draw(lay);// this nor App.Draw((*spr)); will work. they both crash.
App.Display();
}
return 0;
}


why is this crashing?? it post GOOD

here is my layer code

Code: [Select]
#pragma once
#include <vector>
#include <SFML/Graphics.hpp>

typedef std::vector<sf::Drawable*> FrameSet;
typedef FrameSet::const_iterator itFrame;

class Layer : public sf::Drawable, public FrameSet {
 public :
   Layer(  const sf::Vector2f& Position = sf::Vector2f(0, 0),
                const sf::Vector2f& Scale = sf::Vector2f(1, 1),
                float Rotation = 0.f,
                const sf::Color& Col = sf::Color(255, 255, 255, 255));
   virtual ~Layer();

   virtual void Render(sf::RenderTarget& Target) const;
};


Code: [Select]
#include "StdAfx.h"
#include "Layer.h"
#include <iostream>

Layer::Layer(   const sf::Vector2f& Position,
                const sf::Vector2f& Scale,
                float Rotation,
                const sf::Color& Col) :
sf::Drawable(Position,Scale,Rotation,Col)
{
};
 
Layer::~Layer(){
};

void Layer::Render(sf::RenderTarget& Target) const {
    for( itFrame i = begin() ;i!=end();i++)
            Target.Draw(*(*i));
};

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Crashes when drawing layers
« Reply #1 on: February 11, 2011, 08:04:59 am »
Your spr pointer doesn't point to a valid instance of sf::Sprite.
Laurent Gomila - SFML developer

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Crashes when drawing layers
« Reply #2 on: February 11, 2011, 09:15:13 am »
To clarify:

Code: [Select]
sf::Sprite * spr;
sf::Image ima;
if (ima.LoadFromFile("graphics/tiles.bmp")==true)
    std::cout << "good";
spr->SetImage(ima);

spr is unallocated - you have to new it (line 1 in the snippet above) if you for some reason want to keep using pointers.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Crashes when drawing layers
« Reply #3 on: February 11, 2011, 11:45:49 am »
Quote from: "devlin"
if you for some reason want to keep using pointers.
Since there is none (at least in this example), you should prefer automatic objects.
Code: [Select]
sf::Sprite spr;
// ...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mothmann

  • Newbie
  • *
  • Posts: 8
    • View Profile
Crashes when drawing layers
« Reply #4 on: February 11, 2011, 03:00:54 pm »
thats what i get for coding half asleep.