thomasmoelhave / tpie

Templated Portable I/O Environment
Other
112 stars 24 forks source link

Tpie rendering examples #236

Closed gijsde1ste closed 4 years ago

gijsde1ste commented 4 years ago

Hi all,

I generally use SDL2 to render examples of what I'm working on but it seems incompatible with tpie. I can initialize a renderer and window and draw some very basic figures but immediatly after tpie throws random errors like "corrupted size vs. prev_size" or "Segmentation fault (core dumped)" and occasionally (I think sdl2 throws those) a floating point error.

Does anyone know a simple graphics library (ubuntu) for very simple drawing of lines that works well with tpie?

Mortal commented 4 years ago

Hi @gijsde1ste, if you can provide a small example of combining TPIE and SDL2 in an application where you get this error, then we can take a look. I only have limited experience with SDL2, but I don't think the two libraries should be fundamentally incompatible.

For a "simple graphics library for very simple drawing of lines", I don't have any good suggestion, but depending on what you need, you could go for something that outputs a vector graphics file that you can look at afterwards, e.g.

#include <fstream>

class SvgOut {
private:
    std::ofstream out;
public:
    SvgOut(std::string path): out(path) {
        out << "<!DOCTYPE html><svg width=\"300px\" height=\"150px\"><path fill=\"none\" stroke=\"black\" d=\"";
    }
    void move(float x, float y) {
        out << "M " << x << " " << y << " ";
    }
    void line(float x, float y) {
        out << "L " << x << " " << y << " ";
    }
    ~SvgOut() {
        out << "\"></path></svg>\n";
    }
};

int main() {
    SvgOut svg("foo.html");
    svg.move(20, 10);
    svg.line(120, 20);
    svg.line(110, 110);
    svg.line(10, 100);
    svg.line(20, 10);
}

(Compile, run, and open foo.html in a browser with e.g. firefox foo.html)

gijsde1ste commented 4 years ago

Hi @Mortal,

Sorry for getting back to you so late. After creating a minimal example it seems I'm not necessarily having issues with the rendering but instead with destruction of data structures. See the simple example attached, even when the draw call is commented it will give a segmentation error at the end of the main method, I assume when the stack gets destroyed.

I'm still pretty new to tpie, should I call any explicit method to close/clean a stack before c++ automatically destroys it because it went out of scope?

main.zip

Mortal commented 4 years ago

Hi @gijsde1ste, yes indeed it's likely the stack being destroyed after tpie_finish() that's the issue. I would try changing your code to something like the below, where the stack is destroyed before tpie_finish is called.

Also, the definition of draw() probably shouldn't take the stack by-value (void draw(tpie::stack<Point_2> s){), but instead by-reference (void draw(tpie::stack<Point_2> & s){). Actually I think it shouldn't let you copy a tpie::stack - I wonder why that works.

void draw_stack() {
    tpie::stack<Point_2> s = tpie::stack<Point_2>();
    srand(time(NULL));
    for (int i = 0; i < 100; i++){
        s.push(Point_2{rand() % 1280, rand() % 960});
    }
    draw(s);
}

int main() {
    tpie::tpie_init();
    size_t available_memory_mb = 128;
    tpie::get_memory_manager().set_limit(available_memory_mb*1024*1024);
    draw_stack();
    tpie::tpie_finish();
    std::cout << "finished" << std::endl;
    return 0;
}
gijsde1ste commented 4 years ago

@Mortal thanks for solving, should've known it was something like this.