How to make a point appear using fltk? - c++

After creating a window and drawing some shapes in it, I realized I cant make a point and just appear it on the window. I've searched the manual but I cant make anything out of it. Im using the fltk 1.3.0. How can I do it ?

Fltk comes with a bunch of example projects that are helpful. If you look at the line_style example you can easily reduce it to something drawing points like this:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
class TestWindow : public Fl_Window {
void draw()
{
fl_color(255, 0, 0);
fl_begin_points();
fl_point(50, 50);
fl_point(51, 51);
fl_end_points();
}
public:
TestWindow(int w, int h, const char *l = 0)
: Fl_Window(w, h, l) {}
};
int main(int argc, char ** argv) {
Fl_Window *window = new TestWindow(200, 200);
window->end();
window->show(argc, argv);
return Fl::run();
}
But just as a word of advice, drawing single points directly onto the window is rarely the smart thing to do. Drawing into images/buffers and then displaying them is the better alternative most of the time.
edit:
here's is an example of putting the drawing code in the main function.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
class TestWindow : public Fl_Window {
void draw() {}
public:
TestWindow(int w, int h, const char *l = 0) : Fl_Window(w, h, l) {}
};
int main(int argc, char ** argv) {
Fl_Window *window = new TestWindow(200, 200);
window->end();
window->show(argc, argv);
window->make_current();
fl_color(255, 0, 0);
fl_begin_points();
fl_point(50, 50);
fl_point(51, 51);
fl_end_points();
return Fl::run();
}
You should take notice of the disclaimer for make_current in the manual
Danger: incremental update is very hard to debug and maintain!
None of this is good practise beyond using it for simple exercises.

Based on the previous answer to this question, I found this in the documentation:
fl_begin_points()
Starts drawing a list of points.
Points are added to the list with fl_vertex()
So this is some code that shows some points (I added more to really see the points):
#include <FL/fl_draw.H>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
class Drawing : public Fl_Window {
void draw(){
fl_begin_points();
//adding cushion to points to be able to see them.
//center at 10,10
fl_vertex(9,9);
fl_vertex(9,10);
fl_vertex(9,11);
fl_vertex(10,9);
fl_vertex(10,10);
fl_vertex(10,11);
fl_vertex(11,9);
fl_vertex(11,10);
fl_vertex(11,11);
fl_end_points();
fl_color(FL_BLACK);
}
public:
Drawing(int w, int h, const char *l = 0) : Fl_Window(w, h, l){}
};
int main(int argc, char **argv){
Fl_Window *window = new Drawing(340,180);
window->end();
window->show(argc, argv);
return Fl::run();
}

Related

Why cannot I see QGraphicsLineItem when added to QGraphicsScene in QGraphicsView?

I am trying to add QGraphicsLineItem to QGraphicsScene and display it in QGraphicsView. But when I compile and run the below code, it just shows white blank screen and I am not able to see any line on it. What do you think I am doing wrong?
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsLineItem>
class MyGraphicsView : public QGraphicsView
{
public:
MyGraphicsView() {
_scene = new QGraphicsScene(0,0,1024,800);
setScene(_scene);
}
virtual void paintEvent(QPaintEvent * e)
{
QGraphicsLineItem *line = new QGraphicsLineItem(100.0,100.0,500.0,500.0);
line->setPen(QPen(Qt::black, 3, Qt::SolidLine));
line->setFlag( QGraphicsItem::ItemIsMovable );
_scene->addItem(line);
}
private:
QGraphicsScene* _scene;
};
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
MyGraphicsView cw;
cw.show();
return app.exec();
}

C++ FLTK FL_INPUT detect when enter pressed then add text to FL_TEXT_DISPLAY

I want to detect when enter is pressed in FL_INPUT and add its text to FL_Text_Display using C++
please help me I dont know what to do this is all I got
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Input.H>
using namespace std;
int main(int argc,char **argv) {
Fl_Window win(320,240,"BIC");
Fl_Text_Buffer txtbuf;
win.begin();
Fl_Text_Display ted(2,2,320-2,240-2-32-2);
Fl_Input inp(2,240-2-32,320-2,32);
win.end();
ted.buffer(txtbuf);
win.resizable(ted);
win.show();
return Fl::run();
}
You would have to inherit Fl_Input and override the virtual int handle(int) method.
class MyInput : public Fl_Input {
public:
MyInput(int x, int y, int w, int h, const char* title=0) : Fl_Input(x, y, w, h, title) {}
virtual int handle(int e) override {
switch(e) {
case FL_ENTER: foo(); return 1;
default: return 0;
}
}
};
Deriving a new class is possible but the OP's question is easier to solve with a callback. That's what callbacks are designed for. Modified example code follows:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Input.H>
// input callback (called when ENTER is pressed)
void input_cb(Fl_Widget *w, void *v) {
Fl_Input *inp = (Fl_Input *)w;
Fl_Text_Display *ted = (Fl_Text_Display *)v;
Fl_Text_Buffer *tbuf = ted->buffer();
tbuf->append(inp->value());
tbuf->append("\n"); // append newline (optional)
ted->insert_position(tbuf->length());
ted->show_insert_position();
inp->value("");
inp->take_focus();
}
int main(int argc, char **argv) {
Fl_Window win(320, 240, "BIC");
Fl_Text_Buffer txtbuf;
win.begin();
Fl_Text_Display ted(2, 2, 320 - 2, 240 - 2 - 32 - 2);
Fl_Input inp(2, 240 - 2 - 32, 320 - 2, 32);
win.end();
inp.callback(input_cb, (void *)&ted); // set callback
inp.when(FL_WHEN_ENTER_KEY); // when ENTER is pressed
ted.buffer(txtbuf);
txtbuf.append("first line\n");
win.resizable(ted);
win.show();
return Fl::run();
}
There's a little more code involved to position to the end of the buffer to display new text when entered and to set the input focus back to the input widget, but I tried to make the example complete. There's no error handling though.

How to draw multiple rectangles FLTK C++

I am trying to create a program in fltk and I followed this example
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
int main(int argc, char **argv) {
Fl_Window *window = new Fl_Window(340,180);
Fl_Box *box = new Fl_Box(20,40,300,100,"Hello, World!");
box->box(FL_UP_BOX);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labelsize(36);
box->labeltype(FL_SHADOW_LABEL);
window->end();
window->show(argc, argv);
return Fl::run();
}
from FLTK docs. The problem is I'm trying to draw multiple rectangle shapes to the window and it seem very tedious to create multiple boxed in order to have multiple rectangles. I tried looking up a lot of tutorials on drawing shapes on FLTK but I can't find anything simple enough to show me.
My code looks like this so far
#include <FL/Fl.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Window.H>
// #include <FL/fl_draw.H>
#include <iostream>
int main() {
Fl_Window *window = new Fl_Window(900, 600);
window->position(0, 0);
window->color(FL_BLACK);
window->end();
window->show();
while (1) {
int ev = Fl::event();
if (ev == FL_SHORTCUT) {
if (Fl::event_key() == FL_Escape)
break;
}
Fl::check();
}
return 0;
}
and I would like to implement the drawing inside the loop (continuously).
As an important side note: You should really consider using Fl::run() instead of your custom while loop, I ran into many problems with a similar approach like yours.
Now, to answer your question:
Take the example and wrap the Box creation inside a loop. You can take for example an index for assigning different positions to each Box.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
int main(int argc, char **argv) {
Fl_Window *window = new Fl_Window(340,500);
// 3 Fl_Boxes in a loop
for (int i = 0; i < 3; ++i) {
Fl_Box *box = new Fl_Box(20, 40 + i*120, 300, 100,"Hello, World!");
box->box(FL_UP_BOX);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labelsize(36);
box->labeltype(FL_SHADOW_LABEL);
}
window->end();
window->show(argc, argv);
return Fl::run();
}
You control the layout with the formula 20, 40 + i*120, 300, 100 in the box Constructor. You have to adjust that to your requirements.
With FLTK one basic rule is: All widgets you initialize between the window constructor and window->end() or any other widget that works the same way, for example Fl_Group, will become children of the surrounding element and will show, if their parent is shown.
I am not entirely sure, if Fl_Box is what you are looking for. I found Erco's FLTK Cheat Page extremely helpful. You might find inspiration and other approaches to your problem there.

Fl_Window subclass doesn't work

As titled above, i'm trying to get an extremely simple FLTK 1.3.3 application working.
But, even with only a simple Fl_Window and 1 Fl_Button, nothing seems to work. Can anyone help?
class MainEditorWindow : public Fl_Window
{
public:
MainEditorWindow(int _width, int _height, std::string _title);
~MainEditorWindow();
virtual void draw();
virtual int handle(int _event);
private:
Fl_Button* m_btnExit;
};
And here is the Implementation
MainEditorWindow::~MainEditorWindow()
{
}
int MainEditorWindow::handle(int _event)
{
return 1;
}
void MainEditorWindow::draw()
{
m_btnExit->redraw();
}
MainEditorWindow::MainEditorWindow(int _width, int _height, std::string _title) : Fl_Window(_width, _height, _title.c_str())
{
this->begin();
m_btnExit = new Fl_Button(0, 0, 40, 40, "EXIT");
m_btnExit->color(FL_RED);
this->color(FL_WHITE);
this->end();
}
But when simply running the application like this:
int main(int argc, char** argv)
{
MainEditorWindow* mw = new MainEditorWindow(800, 600, "SHIP Editor");
mw->show(argc,argv);
return Fl::run();
}
The window shows up fine, its resizable movable etc, the draw() - function is being called and all that. But the window itself is just blank. It simply shows nothing, especially not the Fl_Button. Can anybody tell me why this occurs? As far as i can tell, there should be nothing particularily wrong with my code.
You need to call Fl_Window::draw()
void MainEditorWindow::draw()
{
m_btnExit->redraw();
Fl_Window::draw();
}
And maybe you want the button is clickable too
int MainEditorWindow::handle(int _event)
{
//return 1;
return(Fl_Window::handle(_event));
}
Try this in your MainEditorWindow constructor:
MainEditorWindow(int _width, int _height, const std::string& _title)
: Fl_Window(_width, _height, _title.c_str()) {
// begin grouped GUI object creation
Fl_Group::begin();
// alter x,y coords of button as necessary
m_btnExit = new Fl_Button(0,0,40,40,"EXIT");
m_btnExit->color(FL_RED);
// end grouped GUI object creation
Fl_Group::end();
// defines resizable widget for group
Fl_Group::resizable(this);
this->color(FL_WHITE);
// display window
Fl_Window::show();
}
Then in main:
int main(int argc, char** argv) {
MainEditorWindow mw(800, 600, "SHIP Editor");
return Fl::run();
}
Here we have added the button to a group and then invoked Fl_Window::show() inside the constructor to display it.
Note there is no need to make mw a pointer to MainEditorWindow in main.

Fastest code changing pixels in Qt for animation

I want to animate small (100x20) image by changing the color of its pixels by the same value. For example, increase red-channel value by 1 every frame and then decrease back. The image has alpha channel, the animation speed is 30...100 fps (platform dependent; 30 is enough for linux, but windows requires ~70 to look smooth).
As i know, drawing is faster when done in QImage, but displaying is faster with QPixmap.
I like QGraphicsEffects, and QPropertyAnimations. White doesn't colorize, but black does.
#include <QLabel>
#include <QPixmap>
#include <QGraphicsColorizeEffect>
#include <QTimerEvent>
#include <QPropertyAnimation>
#include <QShowEvent>
#include <QDebug>
class Widget : public QLabel
{
Q_OBJECT
Q_PROPERTY(qreal redness READ getRedness WRITE setRedness)
public:
Widget(QWidget *parent = 0)
{
QPixmap p(300, 300);
p.fill(Qt::black);
this->setPixmap(p);
colorize = new QGraphicsColorizeEffect();
colorize->setColor(Qt::red);
redness = 0;
colorize->setStrength(redness);
this->setGraphicsEffect(colorize);
animation = new QPropertyAnimation(this,"redness");
animation->setDuration(2000);
animation->setLoopCount(10);
animation->setStartValue(0.0);
animation->setEndValue(1.0);
animation->setEasingCurve(QEasingCurve::CosineCurve);
animation->start();
}
~Widget(){}
qreal getRedness()
{
return redness;
}
void setRedness(qreal val)
{
redness = val;
colorize->setStrength(redness);
this->update();
// qDebug() << redness;
}
public slots:
void showEvent(QShowEvent *)
{
animation->start();
}
private:
qreal redness;
QGraphicsColorizeEffect * colorize;
QPropertyAnimation * animation;
};
and here is the main.cpp
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Hope that helps.