Alright. Above you said
And that is 100% right – if you understand the language it will become obvious how you should handle most JUCE classes. However, why not starting to learn C++ by this? I’ll try to mention the topics you need to know to come up with that solution by yourself and I’d advise you to look them up step by step to understand each line of code you write instead of copy & pasting. Here we go.
For most applications, the Timer
class is what you need, so we will use it here. It is called from the message thread. The message thread is the thread, that keeps any GUI application up and running and is the only thread that should handle GUI actions. So first thing to put on your list for further research (not really C++ related but for GUI apps in general):
If we have a look at the Timer documentation we see that it is a class with one pure virtual function void timerCallback()
(you see that it is pure virtual by the = 0
). A pure virtual function is a function, that the implementation has to supply by inheriting from the base class that defines it. A base Timer
object on it’s own can’t be created, because the virtual function is not defined, so we need to define our own class, that inherits from timer. So the next thing you should look up is
Create an empty GUI application with the Projucer. For now, let’s put our custom class implementation in a new header file, which you should add to the project via the Projucer. In this example I named it MyTimer.h and it has the following content:
#pragma once
#include <JuceHeader.h>
class MyTimer : public Timer
{
public:
void timerCallback() override
{
constexpr bool shouldShowOkCancelButtons = false;
AlertWindow::showNativeDialogBox ("Timer callback",
"This dialogue has been created on the timer callback",
shouldShowOkCancelButtons);
}
};
So what is happening here? We declare a new class with the name MyTimer
. We say that it should inherit Timer
, this means that our class will be a variation of the base class Timer
. By declaring the inheritance public, each class outside can see that it inherits Timer
and call every member function the Timer
base class has.
To make the most interesting part work, the periodically called piece of code, we override
the pure virtual function void timerCallback()
. This function will be called over and over once the timer is running. In the function body, I simply placed a call to the JUCE AlertWindow
class that creates a dialog box window with a given text. The third argument makes sure that it only has an ok button, which you can click to make it disappear. By the way, the syntax AlertWindow::showNativeDialogBox
with the two ::
shows you that showNativeDialogBox
is a static function of the AlertWindow
class, this means you don’t need to create some AlertWindow
object to call it. So for your list of topics to learn add
- static vs. non-static member functions and member variables
Now after the class has been declared, the application actually needs an instance of it – because the timer callback is no static member
The easiest way is to add it as a private member to your Projucer-generated MainComponent class:
#pragma once
#include <JuceHeader.h>
#include "MyTimer.h"
class MainComponent : public Component
{
public:
//==============================================================================
MainComponent();
~MainComponent();
//==============================================================================
void paint (Graphics&) override;
void resized() override;
private:
//==============================================================================
MyTimer timer1;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};
I basically added two lines: #include "MyTimer.h"
, which makes sure that the declaration created above is “known” as we want to use it here. The second line is the private member MyTimer timer1;
so the Main component now has an instance of the timer class declared above we can now use. If we now add something like
timer1.startTimer (5000);
to the main components constructor and timer1.stopTimer();
to the destructor, you’ll start the timer when starting the application and stopping it when the app is closed.
Hope that helped you a bit to get started with all!