Basic animation doesn't fire - c++

I'm trying to follow this example to create an animation:
http://seriss.com/people/erco/fltk/#Animate
Except for the fact that instead of changing the image I'm moving it. There's a car image that should move in a down-right direction every half second, for 10 times:
void func(void* data)
{
static int counter=0;
counter++;
Fl_PNG_Image* image= static_cast<Fl_PNG_Image*>(data);
Fl_Box* box= new Fl_Box(counter*10, counter*10,100,100);
box->image(image);
//delete box;
window->redraw();
if(counter==10)
{
Fl::remove_timeout(func,data);
}
else
{
Fl::repeat_timeout(.5,func,data);
}
}
int main(int argc, char **argv)
{
window = new Fl_Double_Window(width, height);
Fl_PNG_Image* image= new Fl_PNG_Image("car-down.png");
Fl_Box* box= new Fl_Box(0,0,100,100);
box->image(image);
Fl::add_timeout(.5, func, image);
//delete box;
window->end();
window->show(argc, argv);
return Fl::run();
}
I have two problems:
I can't understand when I should delete the box. I'm using it to draw the image, and if I delete it immediately after having drawn the image, the image disappears.
The image doesn't move, and it always stays on the initial position.

Instead of creating new boxes and images, move the box. The box will be deleted when the window is closed
void func(void* data)
{
static int counter=0;
counter++;
//Fl_PNG_Image* image= static_cast<Fl_PNG_Image*>(data);
Fl_Box* box= static_cast<Fl_Box*) data;
box->position(counter*10, counter*10);
window->redraw();
...
}
int main(int argc, char **argv)
{
...
Fl::add_timeout(.5, func, box);
window->end();
window->show(argc, argv);
return Fl::run();
}

Related

How can I set the display range of a QGraphicItemGroup?

I have a QGraphicsItemGroup aggregating several child items, and I want to show only part of the group.(not the numbers of child items, area). Just like the image here.
I want to show the display area.
To do that, I have tried the override the QGraphicsItemGroup::boundingRect(). However, nothing have happened. And i find this in QT docs, maybe this is the reason why doesn't work.
The boundingRect() function of QGraphicsItemGroup returns the bounding rectangle of all items in the item group.
Also, I know I can change the size of QGraphicsView to make it work. However I put the View as CentralWidget, as I also need to display other object in the View, I can not change the size of the View.
How can I set the display range of a QGraphicItemGroup?
To perform this task we can overwrite shape() by returning a QPainterPath that defines the visible region, so that it spreads to its children we enable the flag ItemClipsChildrenToShape:
class GraphicsItemGroup: public QGraphicsItemGroup{
public:
GraphicsItemGroup(QGraphicsItem * parent = 0):QGraphicsItemGroup(parent){
setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
}
QPainterPath shape() const
{
if(mShape.isEmpty())
return QGraphicsItemGroup::shape();
return mShape;
}
void setShape(const QPainterPath &shape){
mShape = shape;
update();
}
private:
QPainterPath mShape;
};
Example:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.setLayout(new QVBoxLayout);
QGraphicsView view;
QPushButton button("click me");
w.layout()->addWidget(&view);
w.layout()->addWidget(&button);
view.setScene(new QGraphicsScene);
GraphicsItemGroup group;
view.scene()->addItem(&group);
auto ellipse = new QGraphicsEllipseItem(QRectF(0, 0, 100, 100));
ellipse->setBrush(Qt::red);
auto rect = new QGraphicsRectItem(QRect(150, 150, 100, 100));
rect->setBrush(Qt::blue);
group.addToGroup(ellipse);
group.addToGroup(rect);
QObject::connect(&button, &QPushButton::clicked, [&group](){
QPainterPath shape;
if(group.shape().boundingRect() == group.boundingRect()){
shape.addRect(0, 50, 250, 150);
}
group.setShape(shape);
});
w.show();
return a.exec();
}
Output:
The complete example can be found in the following link.

How to change Gtk::Image after object creation when PixBuf is given

I am attempting to change Gtk::Image-derived object by giving it pixbuf, but i cannot figure out how to approach that.
The simple setup can be mimicked as:
#include <gtkmm.h>
#include <iostream>
class MyImage : public Gtk::Image
{
public:
void setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf);
};
void MyImage::setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
{
// How can i override the existing pixbuf here?
}
void freeImagePixelData(const guint8* data)
{
delete[] data;
}
Glib::RefPtr<Gdk::Pixbuf> generateTestImage()
{
guint8 *data = new guint8[40*40*4];
for(int i=0; i<40*40*4; )
{
data[i++] = (guint8)255; // R
data[i++] = (guint8)255; // G
data[i++] = (guint8)0; // B
data[i++] = (guint8)255; // A
}
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data(
data, Gdk::Colorspace::COLORSPACE_RGB, true, 8, 40, 40, 40*4, sigc::ptr_fun(&freeImagePixelData));
return pixbuf;
}
int main(int argc, char** argv)
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.almost-university.gtkmm.image.pixbuf");
Gtk::Window window;
MyImage im1;
im1.setPixBuf(generateTestImage());
window.add(im1);
window.show_all_children();
app->run(window);
return 0;
}
(Please note that this is an oversimplified version of what i am trying to figure out, i do know that i should be using Gtk::manage and not add things directly to the window without another container, this is just a mock-up).
I know that if i were to generate the image using a constructor as so:
Gtk::Image im2(generateTestImage());
window.add(im2);
then i would in fact be getting a yellow square.
Somehow i refuse to believe that one can only use pixbuf at the time of object creation. There must be a way to set the image data somehow, and i just cannot find the needed function.
To set Pixbuf in an Gtk::Image you can use Gtk::Image::set(const Glib::RefPtr< Gdk::Pixbuf >& pixbuf) method:
void MyImage::setPixBuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
{
set(pixbuf);
}

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.

How to refresh gtk widget (gtk_widget_queue_draw error)

I have a simple gtk window with one image in it. After making some modification on that image (taken from OpenCV) I want to make window refresh by expose-event. I use gtk2 and it is not possible to change to gtk3. There is no errors but image is not redrawn, the old one still persist.
class TestApp {
public:
GtkWidget *frameWindow;
GInputStream *inStr;
GtkWidget *image;
GdkPixbuf *pixBuff;
cv::Mat *frame;
TestApp(int argc, char *argv[]) : frameWindow(NULL), image(NULL), pixBuff(NULL), inStr(NULL),frame(NULL){
gtk_init(&argc, &argv);
}
int refresh(cv::Mat *f){
frame=f;
int sz = f->dataend - f->datastart;
memcpy((uchar*)gdk_pixbuf_get_pixels(pixBuff),f->datastart,sz);
gtk_widget_queue_draw(frameWindow);
return 0;
}
void imshow(cv::Mat *im){
/* main window */
frame = im;
frameWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width(GTK_CONTAINER(frameWindow), 1);
gtk_window_set_title(GTK_WINDOW(frameWindow), "image");
gtk_window_set_default_size(GTK_WINDOW(frameWindow), 1280, 720);
gtk_window_set_position(GTK_WINDOW(frameWindow), GTK_WIN_POS_CENTER);
gtk_window_fullscreen(GTK_WINDOW(frameWindow));
pixBuff=gdk_pixbuf_new_from_data((guchar*)im->datastart,GDK_COLORSPACE_RGB,FALSE,8,im->size().width,im->size().height,(im->channels()*im->cols), NULL,NULL);
image = gtk_image_new_from_pixbuf(pixBuff);
gtk_container_add(GTK_CONTAINER(frameWindow), image);
g_object_ref_sink(G_OBJECT(frameWindow));
g_signal_connect(G_OBJECT(frameWindow), "expose_event", G_CALLBACK((void*)exposeCb), (gpointer)this);
g_signal_connect(G_OBJECT(frameWindow), "key_press_event", G_CALLBACK((void*)buttonCb), (gpointer)this);
gtk_widget_realize(frameWindow);
gtk_widget_show_all(frameWindow);
gtk_main();
}
static gboolean buttonCb(GtkWidget *eventBox, GdkEventKey *event, gpointer data){
char c = event->keyval;
switch(c){
case 'q':
gtk_widget_destroy(eventBox);
gtk_main_quit();
break;
}
return true;
}
static gboolean exposeCb(GtkWidget *eventBox, GtkWidget *event, gpointer data){
return false;
}
};
int main (int argc, char *argv[]) {
TestApp gtkObj(argc,argv);
cv::Mat im=cv::imread("colour256.png");
cv::Mat imOld=im.clone();
cv::cvtColor(im,im,CV_BGR2RGB);
gtkObj.imshow(&im);
gtkObj.refresh(&imOld);
return 0;
}
Should I redraw image somehow?
FALSE is correct return value. Returning TRUE means "I have dealt with this event" so no further callbacks for particular event ("signal" in GTK+ parlance) are called, in particular default callback is not called so window is not painted. Returning FALSE doesn't stop other callbacks.
I don't think there's much difference. Id' leave it in refresh in order to avoid doing too much work in callbacks.
Quoted warning means that pointer passed to gtk_widget_queue_draw does not contain a GtkWidget. Maybe it hot corrupt? Maybe refresh is called prior to imshow?

Segfault when creating smartpointer on CairoContext

I got some problems when creating a Cairo::RefPtr on a Cairo-Context.
I really can't imagine why this segfaults, except the pointer ist pointing on something completely wrong.
This is my code.
int main(int argc, char * argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::DrawingArea drawarea;
window.add(drawarea);
Cairo::RefPtr<Cairo::Context> ccontext = drawarea.get_window()->create_cairo_context();
Gtk::Allocation allocation = drawarea.get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
ccontext->set_source_rgb(1.0, 0.0, 0.0);
ccontext->set_line_width(2.0);
ccontext->move_to(0,0);
ccontext->line_to(width, height);
Gtk::Main::run(window);
}
And this is what GDB says:
Starting program: /home/marian/Desktop/C++/Langton/Langton [Thread
debugging using libthread_db enabled]
Program received signal SIGSEGV, Segmentation fault. 0xb7be852e in
Gdk::Window::create_cairo_context() () from /usr/lib/libgdkmm-3.0.so.1
I compiled this with gcc (GCC) 4.6.1 20110819 (prerelease).
Thanks in advance
Gtk::Widget::get_window() returns a null Glib::RefPtr, since the widget has not been realized just yet.
Based on the GtkDrawingArea documentation, you need to hook onto the "draw" signal to handle drawing, where your Cairo context is already created and handed to you. Going back to the Gtkmm reference, you would use Gtk::Widget::signal_draw() to hook onto that, or you could overload the virtual on_draw() function to handle your drawing.
Additionally, you also need to call .show() on each widget, i.e. your DrawingArea and your Window, and call ccontext->stroke() to get the line actually drawn.
The result would look something like:
#include <gtkmm.h>
bool draw (const Cairo::RefPtr<Cairo::Context> &ccontext, Gtk::DrawingArea *drawarea)
{
Gtk::Allocation allocation = drawarea->get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
ccontext->set_source_rgb(1.0, 0.0, 0.0);
ccontext->set_line_width(2.0);
ccontext->move_to(0,0);
ccontext->line_to(width, height);
ccontext->stroke ();
return true;
}
int main(int argc, char * argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::DrawingArea drawarea;
drawarea.signal_draw ().connect (sigc::bind (sigc::ptr_fun (&draw),
&drawarea));
window.add(drawarea);
window.show_all ();
Gtk::Main::run(window);
return 0;
}
or alternatively:
#include <gtkmm.h>
class LineBox : public Gtk::DrawingArea
{
protected:
virtual bool on_draw (const Cairo::RefPtr<Cairo::Context> &ccontext);
};
bool LineBox::on_draw (const Cairo::RefPtr<Cairo::Context> &ccontext)
{
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
ccontext->set_source_rgb(1.0, 0.0, 0.0);
ccontext->set_line_width(2.0);
ccontext->move_to(0,0);
ccontext->line_to(width, height);
ccontext->stroke ();
return true;
}
int main(int argc, char * argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
LineBox drawarea;
window.add(drawarea);
window.show_all ();
Gtk::Main::run(window);
return 0;
}