Recreating Stroustrup's PPP2 "Graphics Interface Library" with JUCE


#1

Hello,

So I want to try and recreate the "graphics interface library" that Bjarne Stroustrup has made for the book Programming Principles and Practice Using C++. He uses the FLTK library, but I'd like to try and give people the chance to use JUCE instead, while still being able to follow along in the book and have the custom classes and functions work the same. 

He has created a custom Window class that inherits from Fl_Window and you would create a Window by creating a variable in the main() function. Then you could call gui_main(), which just returns FL::run(). Mostly throughout the book though, we use this class called Simple_window, which inherents from Window and has a member function called wait_for_button(). This window has a button in the top right corner. Well wait_for_button() basically sits in a loop, calling Fl::wait() until that button is pushed. When the button is pushed, it renders the content using Fl::redraw(). This looks like this:


// Modified event loop 
// Handle all events (as per default), but quit when button_pushed becomes true // This allows graphics without control inversion 
void wait_for_button() 
{     
    while (!button_pushed) Fl::wait();     
    button_pushed = false;     
    Fl::redraw(); 
}

I want to somehow get this kind of behavior, but with JUCE and I have no idea where to even start. I know my explanation sucks pretty bad, but I'm really not sure how to explain this well. I'll post a link to the "interface library" if anyone wants to try and help me with this. I might not be experienced enough for a task like this, but I really want to try because I think people should have another choice and I really don't like FLTK or how it's graphics looks. 

Does anyone have any ideas of how I can get started with such a task? How should I make the Window and Simple_window class? How can I make the render/event loop in a simple way? Is there anything similar to Fl::wait() and Fl::run()?

There are some other questions I'll probably have later, like recreating the Widget and Shape classes, but I'm just trying to take it step by step. I'd really appreciate any help on this. If anyone would really like to help me, here's the "graphics interface library" below, as well as the example code that's from the book that uses this. The example code is a bit outdated (it's from the 1st edition), but it should be good enough for you to understand it. I've tried to update the "graphics interface library" with the updated code from the book though.

http://1drv.ms/1ev6qCm


#2

Hey Jordan, 

 

Check out the JUCEApplication Class http://learn.juce.com/doc/classJUCEApplication.php#details. 

 

I think if I'm understanding this rightly the JUCE "event loop" equivalent is started by calling the start application macro (as per example in the link above). 

 

//where MyJUCEApp is a user defined class derived from JUCE application. 

START_JUCE_APPLICATION (MyJUCEApp)

 

 

Then I would guess you need to wrap a component class instance within your own implementation of the window/simple window class. 

The component calss supports an addToDesktop function which is used to render the component / window to the desktop (http://learn.juce.com/doc/classComponent.php#ad717da76012d971197fd8cf943f3e721).

 

So your main window/main content component could call addToDesktop on creation / in the initialization of your JUCEApplication inherriting object. Looks like it would be a fair bit of work but I think the majority of the JUCE functions you need could be written into wrapper classes and methods with names corresponding to the Stroustrup functions. 

 

Im not sure how linking together the START_JUCE_APPLICATION (MyJUCEApp) code and your main function/thread would work. This may not be the way to go per say and it might be better to just create a window object (derived from the component class) and implement your own type of event loop. Someone more knowledgeable might be able to advise whether the components addToDesktop method starts up a message loop of some description (haven't got time to check myself right at this instance). 

 

Don't take any of this as gospel as I had 10 minutes spare at work and though I would add my two cents quickly ! Someone may well correct my ideas. 

 

Josh 


#3

Not sure when Stroustrup wrote his book, but it has been 20 years since people considered it OK to write code which blocks the GUI thread waiting for events, or which synchronously redraws things.

No modern toolkits are structured that way, and on modern OSes like Android it'd actually be impossible to write code that blocks the event thread, so I'm not sure if it's a good starting point to use as an example...


#4

Yeah, but in the context of this book that really doesn't matter. It's basically teaching things like language features (inheretence, virtual functions, etc.) through graphics, which in turn teaches a little about graphics. It's not about making a real app or anything. Just one that can draw and handle simple events. It actually doesn't focus on the library like at all. FLTK comes up every now and then, but it's mostly about creating derived classes from base classes. In this case, Shape is the base class and there's a bunch of classes that derive from it. Then there's a chapter all about making a Function class for graphing data and using that to do some graphing. It's quite an interesting and fun part of the book. I learned a good bit from it. I think his plan was to make it as simple as possible, and that it is.

I believe the 2nd edition was written in 2014. It has C++ 14 features. It's a book aimed at beginner programmers, particularly new programmers learning C++ as their first language. I just thought it might neat if I could kind of replicate the way it's used. I actually have a "working" implementation with a Window and the Window::wait_for_button() using JUCE. Not sure how much further I can get though. It's like JUCE is too good to be so bad. Ha.