0

I have a struct "Layer" and class "LayerHandler". Layer consists only a texture, sprite and two constructors - one default and one with a reference parameter. LayerHandler class is a class that handles the drawing of all the layers we have. I add layers to this class and later I use win.draw(layerhandler_object) to draw everything. LayerHandler inherits from Drawable to do so and it overrides virtual void draw().

LayerHandler.h:

#ifndef LAYERHANDLER_H
#define LAYERHANDLER_H

#include <vector>
#include <SFML\Graphics.hpp>

using namespace std;
using namespace sf;

struct Layer {
    Texture tex;
    Sprite spr;

    Layer() { }

    Layer(Layer& l) {
        tex = l.tex;
        spr = l.spr;
    }
};

class LayerHandler : public Drawable {
    private:
        vector<Layer*> layers;

        virtual void draw(RenderTarget& target, RenderStates states) const {
            for (int i=0; i<layers.size(); i++)
                target.draw(layers[i]->spr, states);
        }

    public:
        LayerHandler();
        ~LayerHandler();
        void Add(Layer& layer);
};

#endif

LayerHandler.cpp:

#include "LayerHandler.h"

LayerHandler::LayerHandler() {

}

LayerHandler::~LayerHandler() {

}

void LayerHandler::Add(Layer& layer) { 
    layers.push_back(new Layer(layer)); 
}

and main.cpp:

#include <iostream>
#include <SFML\Graphics.hpp>
#include "LayerHandler.h"

using namespace std;
using namespace sf;

int main() {
    RenderWindow win(VideoMode(800, 600), "Raven", Style::Default);
    win.setFramerateLimit(60);
    win.setVerticalSyncEnabled(true);
    win.setMouseCursorVisible(false);

    LayerHandler lhandler;

    Layer back;
    back.tex.loadFromFile("bao/gfx/back.png");
    back.spr.setTexture(back.tex);
    back.spr.setPosition(0, 50);

    lhandler.Add(back);

    Event evt;

    float dt = 0.f;
    Clock clock;
    float dwticks = clock.getElapsedTime().asMilliseconds();
    float dwnewticks = 0.f;

    while (win.isOpen()) {
        if (win.pollEvent(evt)) {
            if (Keyboard::isKeyPressed(Keyboard::Key::Escape)) {
                win.close();
            }
        } else {
            dwnewticks = clock.getElapsedTime().asMilliseconds();
            dt = dwnewticks > dwticks ? (dwnewticks - dwticks) / 4000.f : 0.f;
            dwticks = dwnewticks;

            win.clear();
            win.draw(lhandler);
            win.display();
        }
    }

    return 0;
}

I think it's not complicated and that I did everything ok, but I get this "The value of ESP was not properly saved across the function call" error. I have no idea why I get this error. I know that it may be caused by mismatched calling conventions, but I don't see anything like that in my code... I've got to say that this is the first time ever I got this error and I'm completely out of ideas how to deal with it. Any help?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
mentor93
  • 309
  • 1
  • 5
  • 16
  • 1
    a few things that are probably not the cause of your trouble but still important to know about: [don't use both `setFramerateLimit` and `setVerticalSyncEnabled`](http://www.sfml-dev.org/tutorials/2.3/window-window.php#controlling-the-framerate) & your copy ctor for layer should a) take a const ref as parameter, b) set the texture of `spr` to `tex` and not keep `l.tex`. You also should avoid `using` statements in header files & use `/` instead of the non-standard `\` for paths. Finally, use smart pointers or at least free your memory. – Hiura Jun 21 '15 at 11:28
  • I cut out setFramerateLimit and left only setVerticalSyncEnabled. I also changed spr = l.spr to spr.setTexture(tex), but I must disagree that my copy ctor should take a const ref as a parameter, since reference already is a constant pointer. Thanks for your help, I'll try to free memory and I'll read about smart pointers. Hope it'll help. – mentor93 Jun 21 '15 at 12:04
  • ***but I get this "The value of ESP was not properly saved across the function call" error. I have no idea why I get this error.*** Stack corruption. – drescherjm Jun 21 '15 at 12:37
  • Yeah, I know it is stack corruption, but I don't know how to fix it. Perhaps I should use _stdcall? – mentor93 Jun 21 '15 at 12:40
  • I do not think calling convention has anything at all to do with the problem. Although I can not see you going out of bounds of any of your stack variables. – drescherjm Jun 21 '15 at 12:41
  • *but I must disagree that my copy ctor should take a const ref as a parameter, since reference already is a constant pointer* ----> no, that's not what I meant. When people say *const ref* we actually mean `T const&` (== reference to a const object) which is **not** equivalent (even modulo notation) to `T * const`. – Hiura Jun 21 '15 at 13:57

1 Answers1

0

Don't know why, but the problem was with SFML libraries. Here I'm using version 2.2 for 32-bit apps. I downloaded version 2.3 32-bit and compiled my app with the new 2.3 libraries and now it works perfect.

mentor93
  • 309
  • 1
  • 5
  • 16
  • For the libs that exhibited this problem did you use ones built for your compiler version or some other version of Visual Studio? Although with crt conflicts I would expect heap corruption would be more likely than stack corruption. – drescherjm Jun 21 '15 at 18:03
  • I'm sure I used libs for my visual studio version (2010). I never had any other version of visual c++ installed, so I'm sure I wouldn't download libs for visual 2013, for example. What's more, I wrote an arkanoid/breakout clone using the 2.2 libs and it runs fine, although when I tried to override drawable class I got the esp error again. But then I just wrote a method that returns object's sprite, so I could draw it like this: `window.draw(myobject.getSprite());`. – mentor93 Jun 22 '15 at 08:16