【问题标题】:FLTK fl_line and fl_point don't workFLTK fl_line 和 fl_point 不起作用
【发布时间】:2016-04-01 05:22:53
【问题描述】:

我想根据计算结果在窗口上放置一个像素。计算有效,但我想要生成的图表的任何部分都没有出现。编译器和链接器都没有发现错误,我不知道为什么没有像素放在窗口上。由于我无法弄清楚问题是什么,也没有缩小范围,所以我发布了整个代码:

#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();
}

【问题讨论】:

  • 你可能需要一个 win->graph->在你 win->graph->draw 之后重绘。它已经将你的红点放在内部缓冲区中:你需要告诉 fltk 将缓冲区绘制到屏幕上,这就是重绘所做的。
  • sprintf(Stuff, "%d", Position.x);sprintf(Stuff, "%d", Position.y); 行返回的值是什么?它们有效吗?

标签: c++ fltk


【解决方案1】:

问题很可能与您在WindowReq 的构造函数主体中调用的回调函数有关:

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

尝试将其更改为:

Calc->callback(calc_cb, this);

或在动作函数中calc_cb(),在行中:

win->graph->draw();

在上面的行之后尝试添加:

redraw();

并替换显式类型转换:

 WindowReq* win = (WindowReq*)v;

与:

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

或在函数draw() 的定义中,在类Graph 中。这些是与不显示fl_point()s相关的地方,分别代表pos_xpos_y

旁注:

您的代码中有一些 C 风格的行应该用它们的 C++ 替代品替换,例如:

 copyValueToX(char c[1000]) 

可以替换为std::string c 并变为:

 copyValueToX(string c) 

类似:

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

可能变成:

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

最后,在您的成员函数中添加一些 cmets 以阐明其中的语句和表达式。

【讨论】:

  • 很高兴能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 2012-01-31
  • 1970-01-01
  • 2014-04-19
相关资源
最近更新 更多