Understanding the typical hollywood loop

Find quick help here to get you started with Hollywood
dewindatws
Posts: 12
Joined: Wed Sep 06, 2023 8:54 pm
Location: Wales
Contact:

Understanding the typical hollywood loop

Post by dewindatws »

Hello everyone, after eyeing up Hollywood for quite sometime, I've finally decided to purchase it and I've been really enjoying it so far!

I have some programming experience, as a child and a teenager I spent countless hours with GML with gamemaker 7, lua in 2008 roblox studio, I was taught VB.NET 2010 in middle school and as an adult I've messed around with both C and Pico8, although I wouldn't say I am a programmer by any stretch of the imagination, simply that I know how to bodge things together looking at an API reference manual..

Which leads me to my question, reading the documentation for HW, I can see that everything is based on events similar to VB.NET, but when reading through the documentation, the examples for the event-based code seem to be contridictory and mention legacy functions.

For example, here I have wrote a very basic paint program, however if you have a high enough DPI mouse, the application cannot reference the input quickly enough to draw a continious line as well as the increase of the size of the radius is not very stable.

Code: Select all

@DISPLAY {Title = "Whiteboard", X = #LEFT, Y = #TOP, Width = 640, Height = 480, Color = #WHITE, Sizeable = False}
SetFormStyle(#NORMAL)
SetFillStyle(#FILLCOLOR)
StartTimer(1)
X=64
Y=64
R=5
Function logic()
    X=MouseX()
    Y=MouseY()
    If IsKeyDown("A") Or IsKeyDown("a") Then R = R + 1
    If IsKeyDown("S") Or IsKeyDown("s") And R > 5 Then R = R - 1
;COLOUR
If IsKeyDown("C") Or IsKeyDown("c")
        COLOUR = ColorRequest("Select a colour")
        If COLOUR = -1
          Print("Requester cancelled!")
    EndIf
If IsKeyDown("N") Or IsKeyDown("n")  Then Cls(#WHITE)
EndIf

EndFunction
Function redrawdisplay()
If IsLeftMouse() = True Then Circle(X,Y,R,COLOUR)
    If IsRightMouse() = True Then Circle(X,Y,R,#WHITE)
EndFunction
Function p_MainLoop()
logic()
redrawdisplay()
EndFunction
    SetInterval(1,p_MainLoop,1)
    While IsKeyDown("ESC") = False
        WaitEvent()
Wend       
So I was wondering, if someone could walk me through how I should be managing the loop and events for buttons and key-presses.

Any information would be greatly appricated. :)

[EDITED 2023/09/12 : Fixed a poor copy and paste job of code]
Bugala
Posts: 1181
Joined: Sun Feb 14, 2010 7:11 pm

Re: Understanding the typical hollywood loop

Post by Bugala »

Couple of comments.

StartTimer(1) is unnecessary. You are not using it for anything. I suppose you think you need it for the SetInterval, but you don't. SetInterval will use its own Timer to do its thing, you don't need to worry about that.

Your mainloop should only contain redrawdisplay()

In this current state, you dont actually need to use function p_MainLoop() at all, but you can just use SetInterval directly for Redrawdisplay, since that is only thing you currently need setinterval to do.

This gets to the next point. Things you do in Logic, you would do in waitevent.

Right now you dont actually use WaitEvent() at all. You are running p_mainloop in WaitEvent(), but as it currently is, you could have as well used Repeat-Until loop instead, without using WaitEvent().

However, using waitevent was a good practice, since this how you usually do things in Hollywood.


What you should do is instead of checking things every cycle, make them into events that waitevent can wait for.

As an example the mousebuttons:

Code: Select all

Function p_HandlerFunc(msg)
  Switch(msg.action)
  Case "OnMouseDown":
    Circle(X,Y,R,COLOUR)
  Case "OnRightMouseDown":
    Circle(X,Y,R,#WHITE)

  EndSwitch
EndFunction

InstallEventHandler({OnMouseDown = p_HandlerFunc,
   OnRightMouseDown})

Repeat
  WaitEvent
Forever

This way these lines wouldnt be executed every cycle, but only when left or right mousebutton is pressed, as WaitEvents idea is.

Check InstallEventHandler() from manual to find more details about how to wait for "OnKeyDown" etc.
dewindatws
Posts: 12
Joined: Wed Sep 06, 2023 8:54 pm
Location: Wales
Contact:

Re: Understanding the typical hollywood loop

Post by dewindatws »

Thank you very much! This was a perfect explaination and has really helped me understand the documation more now.

I've rewritten my application now using the methods you described. :)

I am really enjoying hollywood so far. <3
User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Understanding the typical hollywood loop

Post by jPV »

Here's a more complete version what Bugala started, with few tips:

Code: Select all

@DISPLAY {Title = "Whiteboard", X = #LEFT, Y = #TOP, Width = 640, Height = 480, Color = #WHITE, Sizeable = False}

SetFormStyle(#NORMAL)
SetFillStyle(#FILLCOLOR)

Global R = 5
Global COLOUR = 0

Function p_HandlerFunc(msg)
    Switch(msg.Action)
    Case "OnMouseDown":
        While IsLeftMouse()
            Circle(MouseX(), MouseY(), R, COLOUR)
            VWait()
        Wend
    Case "OnRightMouseDown":
        While IsRightMouse()
            Circle(MouseX(), MouseY(), R, #WHITE)
            VWait()
        Wend
    Case "OnKeyUp":
        Switch msg.Key
        Case "a":
            R = R + 1
        Case "s":
            If R > 5 Then R = R - 1
        Case "c":
            Local result = ColorRequest("Select a colour")
            If result = -1 Then DebugPrint("Requester cancelled!") Else COLOUR = result
        Case "n":
            Cls(#WHITE)
        Case "ESC":
            End
        EndSwitch
    EndSwitch
EndFunction

InstallEventHandler({OnMouseDown = p_HandlerFunc, OnRightMouseDown = p_HandlerFunc, OnKeyUp = p_HandlerFunc})

Repeat
    WaitEvent()
Forever
For the continuous line you would need to draw with lines rather than circles, because it can't be avoided that mouse positions jump depending of how fast you drag the mouse. This example also draws several times when you try to do a single click, so it might be needed to check if the mouse has moved, or do the whole thing around the OnMouseMove event.
dewindatws
Posts: 12
Joined: Wed Sep 06, 2023 8:54 pm
Location: Wales
Contact:

Re: Understanding the typical hollywood loop

Post by dewindatws »

Thank you very much jPV, yeah I've tried to do lines but I am a little stumped on how to calculate the src and dst differences.

My persumation would be to check when left mouse is down and start a timer and store SRC.XY and DST.XY and every tick of the timer update the line function, but that sounds quite wasteful.

I never thought about how wasteful the draw calls will be with this. I've been running it on MorphOS on a top of its range iBook G4, so it's been buttersmooth for me so far.. (well until you run the openGL demos :lol: )
User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Understanding the typical hollywood loop

Post by jPV »

Yeah,it's good to think about the CPU load and performance always. Usually there are simple solutions.

In this case I would do something this simple:

Code: Select all

    Case "OnMouseDown":
        Local oldx, oldy = MouseX(), MouseY()
        While IsLeftMouse()
            Local newx = MouseX()
            Local newy = MouseY()
            If oldx = newx And oldy = newy
                ; Mouse wasn't moved. Do a small wait to not hog the CPU while user presses the button.
                VWait()
            Else
                ; Mouse moved, let's draw a new line.
                Line(oldx, oldy, newx, newy, COLOUR, {Thickness = R})
            EndIf
            ; Remember the previous position
            oldx = newx
            oldy = newy
        Wend 
So, just keep track of the previous mouse position and compare it to the new position if there's need to draw anything. Local variables are also more efficient than global, so try to avoid using global variables whenever you can and define variables Local.
amyren
Posts: 361
Joined: Thu May 02, 2019 11:53 am

Re: Understanding the typical hollywood loop

Post by amyren »

This could be an excellent candidate for a post in the tutorial section, "How to make a basic paint program"
dewindatws
Posts: 12
Joined: Wed Sep 06, 2023 8:54 pm
Location: Wales
Contact:

Re: Understanding the typical hollywood loop

Post by dewindatws »

It was honestly a really great learning experience that has got me a solid basis of Hollywood, I've finished my paint program and submitting into the amiga tool jam right now. :)

I've now been making my own tiling engine with page/chunk loading and unloading and I worked on a simple racing game on Hollywood. :)

I'd highly recommend someone who is more skilled at teaching than me, to make that tutorial.
amyren
Posts: 361
Joined: Thu May 02, 2019 11:53 am

Re: Understanding the typical hollywood loop

Post by amyren »

dewindatws wrote: Mon Oct 16, 2023 4:02 pm It was honestly a really great learning experience that has got me a solid basis of Hollywood, I've finished my paint program and submitting into the amiga tool jam right now. :)

I've now been making my own tiling engine with page/chunk loading and unloading and I worked on a simple racing game on Hollywood. :)

I'd highly recommend someone who is more skilled at teaching than me, to make that tutorial.
You are to modest :)
But the reason why I think it could be a nice tutorial is because it is quite simple. Its a short piece of code that does something cool, and you dont need to be a very skilled programmer to understand how it work.
Once posted as a tutorial, others can use it to improve it and add more features into it and share with others.

My only question to the code itself is the cls(#white) command. For some reason it will always clear the screen to white, whatever colour you put as an argument.
Now could someone explain why, and how to cls the screen into another chosen colour?
I had to "cheat" and just draw a coloured box instead to get another background colour.
User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Understanding the typical hollywood loop

Post by jPV »

dewindatws wrote: Mon Oct 16, 2023 4:02 pm It was honestly a really great learning experience that has got me a solid basis of Hollywood, I've finished my paint program and submitting into the amiga tool jam right now. :)
Oh damn, I have missed such a jam, and I happened to create RNOSlides within the timeframe :) There would have been some nice prizes...

amyren wrote: Mon Oct 16, 2023 4:37 pm My only question to the code itself is the cls(#white) command. For some reason it will always clear the screen to white, whatever colour you put as an argument.
Now could someone explain why, and how to cls the screen into another chosen colour?
I had to "cheat" and just draw a coloured box instead to get another background colour.
Ah right, the color option in Cls() only works for brushes, so it's useless to define the color in this case. I guess a box is an OK solution... or create a new BGPic, but I guess drawing a box is pretty fast still.
Post Reply