
-----------------------------------
The 10
Sun Jan 22, 2006 12:56 pm

Need Some Explanation on View.Update
-----------------------------------
Hi, i need help understanding this program, its the smooth animation example from the turing examples.

% The "SmoothAnimateHouse" program.

% This program demonstrates the View.Update and setscreen ("offscreenonly")

% Draw a house
drawbox (30, 30, 100, 100, black)
drawbox (50, 30, 80, 60, brightred)
drawbox (35, 70, 55, 90, green)
drawline (45, 70, 45, 90, green)
drawline (35, 80, 55, 80, green)
drawbox (75, 70, 95, 90, green)
drawline (85, 70, 85, 90, green)
drawline (75, 80, 95, 80, green)
drawline (30, 100, 65, 135, brightblue)
drawline (65, 135, 100, 100, brightblue)
drawline (90, 110, 90, 150, black)
drawline (75, 125, 75, 150, black)
drawline (75, 150, 90, 150, black)
locate (maxrow, 2)
put "Home sweet home" ..

% Create the picture
const PIC_WIDTH := 130
const PIC_HEIGHT := 160
var pic := Pic.New (0, 0, PIC_WIDTH, PIC_HEIGHT)

% Animate 10 of them around the screen
var x, y, dx, dy : array 1 .. 10 of int
cls
for i : 1 .. 10
    x (i) := Rand.Int (10, maxx - 10 - PIC_WIDTH)
    y (i) := Rand.Int (10, maxy - 10 - PIC_HEIGHT)
    dx (i) := Rand.Int (-3, 3)
    dy (i) := Rand.Int (-3, 3)
end for

loop
    cls
    locate (maxrow, 10)
    put "Old Animation Technique.  Press any key to see the new technique" ..
    for i : 1 .. 10
        if x (i) + dx (i) < 0 or x (i) + dx (i) + PIC_WIDTH > maxx then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < 0 or y (i) + dy (i) + PIC_HEIGHT > maxy then
            dy (i) := -dy (i)
        end if
        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)
        Pic.Draw (pic, x (i), y (i), picMerge)
    end for
    exit when hasch
end loop

% Read the character from the buffer.
var ch : string (1)
getch (ch)

% Now, any drawing to the screen won't appear until a View.Update is
% given.  Note that you can turn this off with 
setscreen ("offscreenonly")
loop
    cls
    locate (maxrow, 10)
    put "New Animation Technique.  Press any key to quit" ..
    for i : 1 .. 10
        if x (i) + dx (i) < 0 or x (i) + dx (i) + PIC_WIDTH > maxx then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < 0 or y (i) + dy (i) + PIC_HEIGHT > maxy then
            dy (i) := -dy (i)
        end if
        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)
        
        % Note this only draws on the offscreen window.  Nothing appears
        % in the visible window.
        Pic.Draw (pic, x (i), y (i), picMerge)
    end for
    % All the houses have been drawn.  Now update the screen.
    View.Update
    exit when hasch
end loop
setscreen ("nooffscreenonly")


I dont understand what the array is doing in this program, whats the use of it?
I also dont understand how they made the animation?

Im trying to use this same tecnique in my final project so i would appreciate if someone could just explain this program for me.

-----------------------------------
Tony
Sun Jan 22, 2006 1:34 pm


-----------------------------------
the array just holds random locations

otherwise the example is quite well commented

you don't need to consern yourself with details of how animation is made, the important points are that the screen is set to

View.Set("offscreenonly")


and that after you're done drawing your entire frame you update the buffer

View.Update


so your program would look like

View.Set("offscreenonly")
loop
   Draw
   Draw
   Draw
   View.Update
   cls
end loop


-----------------------------------
The 10
Sun Jan 22, 2006 2:46 pm


-----------------------------------
the array just holds random locations

Thx for the reply Tony, but i still dont understand why you need the array there?  I know it sounds like silly question, but i really wanna know.

Also i understand this is the way animation should be done.


View.Set("offscreenonly")
loop
   Draw
   Draw
   Draw
   View.Update
   cls
end loop


but i want to make the animation similar to the house one, so that's why i'd like to understand what happens in this part.

loop
    cls
    locate (maxrow, 10)
    put "New Animation Technique.  Press any key to quit" ..
    for i : 1 .. 10
        if x (i) + dx (i) < 0 or x (i) + dx (i) + PIC_WIDTH > maxx then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < 0 or y (i) + dy (i) + PIC_HEIGHT > maxy then
            dy (i) := -dy (i)
        end if
        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)
       
        % Note this only draws on the offscreen window.  Nothing appears
        % in the visible window.
        Pic.Draw (pic, x (i), y (i), picMerge)
    end for
    % All the houses have been drawn.  Now update the screen.
    View.Update
    exit when hasch
end loop

so if someone could explain this to me? :cry:

-----------------------------------
Rasta Fella
Sun Jan 22, 2006 4:45 pm


-----------------------------------
Note the only difference between both techniques is that the later one has View.Update stuck into the loop. That's what makes the transition of all the "houses" smooth. As for learning [url=http://www.compsci.ca/v2/viewtopic.php?t=366]Arrays...

Or for further explanation on "flexible arrays" and more check the Turing Walkthrough.

-----------------------------------
The 10
Sun Jan 22, 2006 6:09 pm


-----------------------------------
Ok i understand now that the array is there to hold different values in 1 variable name.

Now i want to understand what's going on in this part of the program - the animation


for i : 1 .. 10
        if x (i) + dx (i) < 0 or x (i) + dx (i) + PIC_WIDTH > maxx then
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < 0 or y (i) + dy (i) + PIC_HEIGHT > maxy then
            dy (i) := -dy (i)
        end if
        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)
       
        % Note this only draws on the offscreen window.  Nothing appears
        % in the visible window.
        Pic.Draw (pic, x (i), y (i), picMerge)
    end for 

I want to understand what's going on, as i dont understand how the houses move the way they do, and how they bounce of the edge.
