2

I want to place a pixel on the window in accordance to the result of a calculation. The calculation works, but no part of the graph I want to generate ever shows up. Compiler and linker both find no error and I have no idea why no pixels are put on the window. Since I can't figure out what the problem is and not narrow it down either I am posting the entire code:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Button.H>
#include <Fl/fl_Input.H>
#include <Fl/fl_Output.H>
#include <Physics.h>
#include <string.h>
#include <stdlib.h>
#include <Windows.h>

using namespace std;

#define WINSIZE 700
#define TIMESTEP 0.02f

class Graph : public Fl_Widget
{
public:
    Graph(int X, int Y, int W, int H) : Fl_Widget(X, Y, W, H) {}

    void draw(void) 
    {
        fl_color(FL_RED);
        fl_point(pos_x, (500 - pos_y));
    }

    int pos_x, pos_y;
};
//------------------------------------------------------------------------    

class WindowReq : public Fl_Window
{
public:
    WindowReq(int W, int H, const char* Title);

    Fl_Input*   InVel;
    Fl_Input*   InDeg;
    Fl_Output*  OutT;
    Fl_Output*  OutX;
    Fl_Output*  OutY;
    Fl_Button*  Calc;
    Graph*      graph;

private:
    static void calc_cb(Fl_Widget* o, void* v);
    inline void copyValueToX(char* c);
    inline void copyValueToY(char* c);
    inline void copyValueToT(char* c);
};

WindowReq::WindowReq(int W, int H, const char* Title) : Fl_Window(W, H, Title)
{
    begin();
        Calc = new Fl_Button((WINSIZE - 150), 50, 100, 30, "Calculate path");
        Calc->callback(&WindowReq::calc_cb, this);

        InVel = new Fl_Input(70, 50, 100, 30, "Velocity:");

        InDeg = new Fl_Input(((WINSIZE / 3) + 50), 50, 100, 30, "Angle:");

        OutX = new Fl_Output(70, 100, 100, 30, "Distance:");

        OutY = new Fl_Output(((WINSIZE / 3) + 50), 100, 100, 30, "Height:");

        OutT = new Fl_Output((WINSIZE - 150), 100, 100, 30, "Time:");

        graph = new Graph(0, 150, 700, 500);

        redraw();
    end();

    show();
}

void WindowReq::calc_cb(Fl_Widget* o, void* v)
{
    Projectile Stone;
    WindowReq* win = (WindowReq*)v;
    Coordinates2D Position = { 0,500 }, Buffer = Position;
    double velocity;
    double Degree;
    double Time;
    char Stuff[1000];

    velocity = atof(win->InVel->value());
    Degree = atof(win->InDeg->value());

    Stone.initialize(velocity, Degree, TIMESTEP);

    for (int i = 0; i < 700; i++)
    {
        fl_point(i, 400);
    }

    do
    {
        Position = Stone.getpos();
        Time = Stone.gettime();
        sprintf(Stuff, "%.2f", Time);
        win->copyValueToT(Stuff);
        sprintf(Stuff, "%d", Position.x);
        win->copyValueToX(Stuff);
        sprintf(Stuff, "%d", Position.y);
        win->copyValueToY(Stuff);
        Stone.simulatestep();
        win->graph->pos_x = Position.x;
        win->graph->pos_y = Position.y;
        win->graph->draw();
        Fl::check();
        Sleep(100);
    }while(Position.y >= 0);
}

void WindowReq::copyValueToX(char c[1000])
{
    OutX->value(c);
}

void WindowReq::copyValueToY(char c[1000])
{
    OutY->value(c);
}

void WindowReq::copyValueToT(char c[1000])
{
    OutT->value(c);
}

//============================================================
int main()
{
    WindowReq win(WINSIZE, WINSIZE, "Balistics Simulator");
    Fl::run();
}
Ziezi
  • 6,375
  • 3
  • 39
  • 49
Neko
  • 45
  • 5
  • You probably need a win->graph->redraw after your win->graph->draw. It has already put your red dot in the internal buffer: you need to tell fltk to draw the buffer to the screen, which is what the redraw does. – cup Dec 26 '15 at 23:17
  • What are the values returned from lines: `sprintf(Stuff, "%d", Position.x);` and `sprintf(Stuff, "%d", Position.y);` ? Are they valid? – Ziezi Dec 27 '15 at 00:05

1 Answers1

2

The problem is most likely related either to your callback function called in the constructor body of WindowReq:

Calc->callback(&WindowReq::calc_cb, this);

try changing it to:

Calc->callback(calc_cb, this);

or in the action function calc_cb(), in the line:

win->graph->draw();

after the above line try adding:

redraw();

and replace the explicit type casting:

 WindowReq* win = (WindowReq*)v;

with:

 WindowReq* win = reinterpret_cast<WindowReq*>(v);

or in the definition of the function draw(), within class Graph. Those are the places related with not displaying the fl_point()s, representing pos_x and pos_y.

Side note:

there are some C-style lines in your code that should be replaced with their C++ alternatives, for example:

 copyValueToX(char c[1000]) 

could be replace with std::string c and become:

 copyValueToX(string c) 

similarly:

 sprintf(Stuff, "%d", Position.x);

could become:

std::cout << Position.x << std::endl;

Finally, add few comments in your member functions to clarify statements and expressions in them.

Ziezi
  • 6,375
  • 3
  • 39
  • 49