
-----------------------------------
BeepBeepLettuce
Wed Nov 29, 2017 9:45 pm

pic.free problems
-----------------------------------
im trying to make zelda 1 in turing and im having problems with pic.free
here is the code

setscreen ("graphics:1024,705,nobuttonbar,nocursor")
var chars : array char of boolean
var map : array 1 .. 16, 1 .. 11 of string
var pic : array 1 .. 18, 1 .. 8 of int
var link : array 1 .. 12 of int
var Aup, attack : boolean := false
var direction, tunic : string := "white"
var file, lank, X, Y, XPos, YPos : int := 0
var frame, frame2, Tmove, Twalk, Tattack : int := 0

direction := "D"
XPos := 320
YPos := 320
X := 1
Y := 1

proc setLank
    Sprite.SetPosition (lank, XPos, YPos, false)
    if attack = true then
        frame2 := 8 - frame
        Tattack += 1
    else
        frame2 := 0
    end if
    if Twalk = 6 then
        frame := 4
    elsif Twalk > 12 then
        Twalk := 0
        frame := 0
    end if
    if direction = "D" then
        Sprite.ChangePic (lank, link (1 + frame + frame2))
    elsif direction = "U" then
        Sprite.ChangePic (lank, link (2 + frame + frame2))
    elsif direction = "R" then
        Sprite.ChangePic (lank, link (3 + frame + frame2))
    elsif direction = "L" then
        Sprite.ChangePic (lank, link (4 + frame + frame2))
    end if
end setLank

proc getMap
    open : file, intstr (X) + '-' + intstr (Y) + ".txt", get
    for y : 1 .. 11
        for x : 1 .. 16
            get : file, map (x, y)
        end for
    end for
    close : file
end getMap

proc drawMap
    for y : 1 .. 11
        for x : 1 .. 16
            Pic.Draw (pic (strint (map (x, y) (1)), strint (map (x, y) (2))), x * 64 - 64, y * 64 - 64, picUnderMerge)
        end for
    end for
end drawMap

proc getLank
    for i : 1 .. 12
        Pic.Free (link (i))
    end for
    for i : 1 .. 3
        link (i * 4 - 3) := Pic.FileNew (tunic + "down" + intstr (i) + ".bmp")
        link (i * 4 - 2) := Pic.FileNew (tunic + "up" + intstr (i) + ".bmp")
        link (i * 4 - 1) := Pic.FileNew (tunic + "right" + intstr (i) + ".bmp")
        link (i * 4) := Pic.FileNew (tunic + "left" + intstr (i) + ".bmp")
        for I : i * 4 - 3 .. i * 4
            link (I) := Pic.Scale (link (I), 64, 64)
        end for
    end for
end getLank

for y : 1 .. 8
    for x : 1 .. 18
        pic (x, y) := Pic.FileNew (intstr (x) + '-' + intstr (y) + ".bmp")
        pic (x, y) := Pic.Scale (pic (x, y), 64, 64)
    end for
end for

for i : 1 .. 3
    link (i * 4 - 3) := Pic.FileNew (tunic + "down" + intstr (i) + ".bmp")
    link (i * 4 - 2) := Pic.FileNew (tunic + "up" + intstr (i) + ".bmp")
    link (i * 4 - 1) := Pic.FileNew (tunic + "right" + intstr (i) + ".bmp")
    link (i * 4) := Pic.FileNew (tunic + "left" + intstr (i) + ".bmp")
    for I : i * 4 - 3 .. i * 4
        link (I) := Pic.Scale (link (I), 64, 64)
    end for
end for
lank := Sprite.New (link (1))
getMap
drawMap
setLank
Sprite.Show (lank)

loop
    Input.KeyDown (chars)
    Tmove += 1
    if Tmove > 12 and attack = false and chars ('w') and chars ('s') = false then
        direction := "U"
        YPos += 4
        Twalk += 1
        Tmove := -4
    end if
    if Tmove > 12 and attack = false and chars ('s') and chars ('w') = false then
        direction := "D"
        YPos -= 4
        Twalk += 1
        Tmove := -4
    end if
    if Tmove > 12 and attack = false and chars ('a') and chars ('d') = false then
        direction := "L"
        XPos -= 4
        Twalk += 1
        Tmove := 0
    end if
    if Tmove > 12 and attack = false and chars ('d') and chars ('a') = false then
        direction := "R"
        XPos += 4
        Twalk += 1
        Tmove := 0
    end if
    if chars ('t') then
        tunic := "white"
        getLank
    end if
    if chars ('y') then
        tunic := "green"
        getLank
    end if
    if chars ('f') = false then
        Aup := true
    end if
    if attack = false and Aup = true and chars ('f') then
        attack := true
        Aup := false
    elsif Tattack > 250 then
        attack := false
        Tattack := 0
    end if
    setLank
    delay (1)
end loop


i made it so that pressing t and y will change the tunic color as a test.
the tunic changed color as expected but changing the color multiple times will cause the program to crash with the following error. "cannot allocate item. out of id numbers(max 1000)"
i know that this is caused by having more than 1000 images in memory but im using pic.free to get rid of the images im not using so i dont know why its crashing.
can someone tell me what i did wrong? i attached the .t file and the sprites incase you want to try it yourself.

-----------------------------------
Insectoid
Thu Nov 30, 2017 8:36 pm

RE:pic.free problems
-----------------------------------
Pic.Scale doesn't resize an image, it creates a brand new image without freeing the old one. Using a line like 'image := Pic.Scale (image)' creates a new image. But what happens to the unscaled image that 'image' used to refer to? It becomes an orphan- it still exists in memory, it still takes up one of your 1000 pic IDs, but you cannot access it or free it. That memory is gone until you close the program. 

You need a variable to hold that image while you scale it so you can free it when you're done with it, like so:

Var image := your picture
Var temp := image
Image := Pic.Scale (image)
Pic.Free (temp)

Now you're safely freed the old image. Note that all image manipulation functions, and some sprite functions, behave this way.

-----------------------------------
BeepBeepLettuce
Sat Dec 02, 2017 7:30 am

Re: pic.free problems
-----------------------------------
thanks! i tried what you said with the temporary variables and it works perfectly now.
i had no idea pic.scale made a new image
