We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I'm almost finished with my app although it takes quite a while to initialize everything when I start it up, I've looked up how to create a splash screen but I've only seen people put a picture inside setup but that doesn't do it for me since when I open the app I see a grey screen after which I would see a splash screen from setup, but I want the splash screen to be visible from the beginning (before setup?) how would I go about this
Answers
AFAIK, that isn't possible. There are complex workarounds though (like what processing IDE uses), or slightly simpler but slower ones (multithreading). You need a bit of knowledge of things to do any of them. Ask if you need help.
You can achieve this with a state model in
draw()
. Afaik you can even display the image indraw()
and do the loading of images and setting up everything afterwards (because you wrote "although it takes quite a while to initialize everything").Your
setup()
is nearly empty in this approach. Except for splash=loadImage("....");Basically say
state=0;
in setupIn draw say
switch (state) {
In state 0 say display splash screen
image(splash);
say
state=1;
In state 1 load everything, then say
state = 2;
Just like in this recent post (ignore any arduino code... jump to the PAGE0/1/2 example): https://forum.processing.org/two/discussion/comment/99022/#Comment_99022
Notice you can flip pages based on an user event or using system's tools, a timer for example.
Kf
I went ahead and loaded everything in draw like @kfrajer and @Chrisir mentioned and this is the result (I also added a timer since at certain parts I slowed the footage down)
As you can see to make it obvious when it's loading I turned the load screen completely red for now and it works fine although it starts off with a gray screen and then switches to red I know I'm nitpicking here since the gray screen is only like 1/10 of a sec.
I'm assuming that the extra time is because of initializing all the variables before setup I could move some of the initializations inside the code but not al can work local since I need most of them "across" the code
Which is why I'm wondering is it possible to initialize all the variables inside the code without them becoming local? (except the ones I need for the load screen like the "state" value that @Chrisir mentioned)
Hard to tell without seeing your code
You need a background() in setup too
@Chrisir my bad. This isn't the whole code but only the part that I think is essential to the problem.
My approach is different see above
@Chrisir indeed although I fail to see how your approach would be faster? Since we both move the loading etc into draw() but before that we both draw something (in your example you load in an image and I just draw a background). But perhaps I'm missing something?
draw()
doesn't update the screen until it returns.loadImage()
can slow down draw returningtext()
dynamically loads the text library the first time it is called. this can slow down draw returning a lot.loadImage()
-- ortext()
for the first time -- there may be lag on that frame.So this sketch takes a big hit on its first draw frame -- the frame loads late:
...and this sketch loads the first draw frame just fine -- the big hit happens on the second draw frame:
@jeremydouglass Thank you! So the mistake I made in my first code (the one in the video) until @Chrisir corrected me by putting background() into setup. Would be that I call background() and after that I load everything in but the frame only gets drawn after the draw is complete right? And it would only complete if everything is loaded in or you return.
Just look at my original post and do this
That's right. Your code here:
setup()
(frame 0):background()
draw()
the first time time it runs (frame 1):resolution()
and loads a bunch of images (lag)textsize()
and loads the full text library (big lag)I think @chrisir wants you to implement the states he is reffering to in his original post.
By doing what Chrisir is suggesting, you are forcing 1 run through draw before you do anything else. Because you first draw the image (or background), then you load everything and then you continue to the code.
If I were you, I would not do background or image in setup or draw, I would just do
text("loading", width/2, height/2);
. Because the default background is a neutral grey tone and the text will get the point across.I am not sure, but I guess you could also make use of the thread() function.
BTW, you can make variable global by specifying that when you initialize it:
public int x = width/2; // this is accessible globally
Hello,
here is a similar version to what I described:
It uses a variable firstTime to note whether drawForStateSplashScreen() runs the first time or not.
In the first time, the splash screen is displayed by drawForStateSplashScreen().
Only the 2nd time your loading / heavy calculations take place in init1() while the splash screen is already visible.
Note that drawForStateSplashScreen() uses the same background color (255) as setup().
Best, Chrisir
@Chrisir thank you so much for your code although do your code and the code of @jeremydouglass not do the same in different ways? Since you go from drawing a splash screen to loading and after that initialize the game although your code also allows for instance, a game menu quite easy but with the code from @jeremydouglass it checks whether or not a frame has been drawn before if not it draws one (the splash screen) and finishes the draw with return (please correct me if I'm wrong) in the next cycle it "sees" that a frame has been drawn before and loads everything in if that finishes the code moves on to the rest of the code and next cycle into the draw it "sees" that everything has been loaded and directly moves on to the game.
@Eeyorelife thanks, the reason I wanted to know is so I can initialize it after setup so I can display the splash screen and then initialize everything except the variables I needed to run the splash screen of course (since those have been initialized before setup). But after googling I can't seem to find a way to initialize it publicly inside a void without making it final (which makes it unchangeable to my understanding). I also don't know if initializing the variables actually takes time so perhaps moving this after setup so I can display the splash screen before doesn't make any difference?
Why isn't anyone doing multithreading? It has the added advantage that you can show the progress of the loading process.
similar as my last sketch
instead of using variable
firstTime
now a state of its own namedstateSplashScreenAndLoadingStuff
Best, Chrisir ;-)
Belatedly: a final thought related to fast screen loading.
Calling
text()
for the first time introduces a big lag. Try these two sketches:Output:
and
Output:
Almost identical sketches, but one is almost two seconds slower (macOS 10.12, Processing 3.3.6).
If your goal is to draw something to the screen as fast as possible on startup, DO NOT call text for the first time until a later frame.
Kf