diff --git a/documentation/src/examples.dox b/documentation/src/examples.dox index 0bca5dcdd..d5031ee95 100644 --- a/documentation/src/examples.dox +++ b/documentation/src/examples.dox @@ -123,6 +123,12 @@ you build FLTK, unlike those in the 'test' directory shown below. \ref examples_valuators \ref examples_windowfocus + +New in FLTK 1.5 + + + \ref examples_gl_image + @@ -370,6 +376,12 @@ and double-buffered rendering, and take over the entire screen. More information in the source code. +\subsection examples_gl_image gl_image + +\par +\c gl_image shows how to draw most images supported by FLTK +in an Fl_Gl_Window. Fl_Bitmap and depth 1 and 2 Fl_RGB_Image aren't supported yet. + \subsection examples_gl_overlay gl_overlay \par diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f3f5a45ca..30a3969a1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -273,6 +273,7 @@ if(OPENGL_FOUND) if(NOT FLTK_BUILD_SHARED_LIBS) fl_create_example(glpuzzle glpuzzle.cxx "${GLDEMO_LIBS}") endif() + fl_create_example(gl_image gl_image.cxx "${GLDEMO_LIBS};fltk::images") fl_create_example(gl_overlay gl_overlay.cxx "${GLDEMO_LIBS}") fl_create_example(shape shape.cxx "${GLDEMO_LIBS}") endif(OPENGL_FOUND) diff --git a/test/gl_image.cxx b/test/gl_image.cxx new file mode 100644 index 000000000..c24f20b26 --- /dev/null +++ b/test/gl_image.cxx @@ -0,0 +1,111 @@ +// +// OpenGL image-drawing test program for the Fast Light Tool Kit (FLTK). +// +// Copyright 2026 by Bill Spitzak and others. +// +// This library is free software. Distribution and use rights are outlined in +// the file "COPYING" which should have been included with this file. If this +// file is missing or damaged, see the license at: +// +// https://www.fltk.org/COPYING.php +// +// Please see the following page on how to report bugs and issues: +// +// https://www.fltk.org/bugs.php +// + +#include +#include +#include +#include +#include + +class image_background_box : public Fl_Widget { +public: + image_background_box(int x, int y, int w, int h) : Fl_Widget(x, y, w, h, NULL) {} + void draw() { + gl_color(FL_WHITE); + gl_rectf(x(), y(), w(), h()); + gl_color(color()); + int startX; + for (int Y = y(); Y < y()+h(); Y += 5) { + startX = Y % 10; + for (int X = startX; X < w(); X += 10) { + gl_rectf(X, Y, 5, 5); + } + } + if (image()) image()->draw((w() - image()->w())/2, y() + (h() - image()->h())/2); + } +}; + + +void chooser_cb(Fl_Widget *, class Gl_Image_Window *mainwin); + +void close_cb(Fl_Widget *, void *) { + Fl::first_window()->hide(); +} + + +Fl_Menu_Item items[] = { + { "File", 0, 0, 0, FL_SUBMENU}, + { "Choose image file…", 0, (Fl_Callback*)chooser_cb}, + { "Quit", 0, close_cb}, + {0}, + {0}, +}; + + +class Gl_Image_Window : public Fl_Gl_Window { + const Fl_Image *img_; + image_background_box *box_; +public: + Gl_Image_Window(int w, int h, const char *t) : Fl_Gl_Window(w, h, t) { + img_ = NULL; + begin(); + Fl_Menu_Bar *bar = new Fl_Menu_Bar(0, 0, w, 30); + bar->menu(items); + items[1].user_data(this); + box_ = new image_background_box(0, 30, w, h - 30); + box_->color(FL_GRAY); + end(); + resizable(box_); + } + + void set_image(const Fl_Image *img) { + if (img_) ((Fl_Image*)img_)->release(); + img_ = img; + box_->image((Fl_Image*)img); + ((Fl_Image*)img)->scale(box_->w(), box_->h()); + redraw(); + } + + void resize(int x, int y, int w, int h) override { + Fl_Gl_Window::resize(x,y,w,h); + if (img_) ((Fl_Image*)img_)->scale(box_->w(), box_->h()); + } + + void draw() override { + Fl_Gl_Window::draw(); // Draw FLTK child widgets. + } +}; + + +void chooser_cb(Fl_Widget *, Gl_Image_Window *mainwin) { + char *fname = fl_file_chooser("select image file", "*.{png,jpg,gif,svg,svgz}", NULL, 0); + if (fname) { + Fl_Shared_Image *shared = Fl_Shared_Image::get(fname); + if (!shared->fail()) { + mainwin->copy_label(fname); + mainwin->set_image(shared); + } + } +} + + +int main(int argc, char **argv) { + Fl::use_high_res_GL(1); + fl_register_images(); + Gl_Image_Window mainwin(600, 600, "GL Image Viewer"); + mainwin.show(argc, argv); + return Fl::run(); +}