I've had a bit of a time away from actively working with FLTK, and
was surprised to find I couldn't get fl_draw(s, x, y) to work today.
I reckon I've made a really obvious howler, but I've been staring
at it all day and can't see what I've done wrong. I know that I'm
going to kick myself when someone points out a beginner's mistake.

So I've incorporated the fl_draw() call in an old example that I
posted several years ago. Basically I want something simple like:

    char buffer[32];
    sprintf(buffer, "%d", value);
    fl_draw(buffer, x() + 30, y() + 30);

but it keeps giving a segmentation fault, and I can't see it. I've
tried changing the declaration of buffer from a local variable to a
member variable, I've tried using fixed text, and even replacing
buffer with "hello" in the fl_draw call. See the // # comments.
I can't see the wood for the trees. Where have I gone wrong?

This is fltk-1.1.10, on Lunar Linux, with gcc-4.4.3.
It also failed at work today (CentOS 5.4 and gcc-4.4.1 ?).

Cheers
D.


/*
  While trying to find a stupid error (Doh! max uchar is 255 and not 256!)
  I've written a simple fl_color demo that someone might find useful.
*/

// Just compile using 'fltk-config --compile filename.cxx'
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>

#include <cmath>
#include <cstdio>
#include <cstring>

void
setGrey (double ratio)
{
  uchar grey = (uchar) (255 * ratio);
  fl_color (grey);
}

void
setBlueToCyan (double ratio)
{
  uchar red = 0;
  uchar green = (uchar) (255 * ratio);
  uchar blue = 255;
  fl_color (red, green, blue);
}

void
setCyanToGreen (double ratio)
{
  uchar red = 0;
  uchar green = 255;
  uchar blue = (uchar) (255 * (1.0 - ratio));
  fl_color (red, green, blue);
}

void
setGreenToYellow (double ratio)
{
  uchar red = (uchar) (255 * ratio);
  uchar green = 255;
  uchar blue = 0;
  fl_color (red, green, blue);
}

void
setYellowToRed (double ratio)
{
  uchar red = 255;
  uchar green = (uchar) (255 * (1.0 - ratio));
  uchar blue = 0;
  fl_color (red, green, blue);
}

void
setColor (double min, double max, double value)
{
  double range = max - min;
  double delta = value - min;
  double ratio = delta / range;

  if (ratio <= 0.0) {
      setGrey (0.3);
  }
  else if (ratio < 0.25) {
      setBlueToCyan (4.0 * (ratio - 0.00));
  }
  else if (ratio < 0.50) {
      setCyanToGreen (4.0 * (ratio - 0.25));
  }
  else if (ratio < 0.75) {
      setGreenToYellow (4.0 * (ratio - 0.50));
  }
  else if (ratio < 1.00) {
      setYellowToRed (4.0 * (ratio - 0.75));
  }
  else {
      setGrey (0.7);
  }
}

void
drawCircle (int x, int y, int r)
{
  fl_pie (x - r, y - r, 2 * r, 2 * r, 0.0, 360.0);
  fl_color (FL_BLACK);
  fl_arc (x - r, y - r, 2 * r, 2 * r, 0.0, 360.0);
}

class Box:public Fl_Box
{
public:
char m_buffer[32];      // 1

Box (int x, int y, int w, int h, const char *t = 0):Fl_Box (x, y, w, h, t)
  {
    strcpy(m_buffer, "");  // 1a
  }

   ~Box ()
  {
  }

  void draw ()
  {
    // char m_buffer[32];       // 2
    int n = sprintf(m_buffer, "hello");
    // printf("sprintf() returns %d\n", n);

    // fl_draw(m_buffer, x() + 30, y() + 30);   // 3
    fl_draw("hello", x() + 30, y() + 30);

    int xc = x () + w () / 2;
    int yc = y () + h () / 2;
    int rc = (w () < h ())? w () / 3 : h () / 3;

    drawCircle (xc, yc, rc);
    for (double angle = 0.0; angle < 360.0; angle += 10.0) {
        int xx = (int) (xc + rc * sin (angle * M_PI / 180.0));
        int yy = (int) (yc - rc * cos (angle * M_PI / 180.0));
        setColor (0.0, 360.0, angle);
        drawCircle (xx, yy, rc / 10);
    }

    for (int i = 0; i < w (); ++i) {
        setColor (0, w (), i);
        fl_line (x () + i, y (), x () + i, y () + 20);
    }
  }
};

int
main (int argc, char *argv[])
{
  Fl_Window *w = new Fl_Window (300, 300);
  Box *b = new Box (5, 5, 290, 290);
  w->end ();

  w->show (argc, argv);
  Fl::run ();
  return (0);
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to