TheGameCreators / AGK-Studio

3 stars 1 forks source link

`SaveImage()`: specifying file path does not work #1124

Closed dezashibi closed 7 months ago

dezashibi commented 7 months ago

Hello

TLDR; how can I change Write folder to an absolute path from the read folder or to the document folder?

I'm trying to make this code work which it's purpose is to save sprites in an array drawn in a grid together in a file

function MapInfo_save_as_image(mi ref as MapInfo)
    img as integer
    img = CreateRenderImage(DEFAULT_SCREEN_HEIGHT, DEFAULT_SCREEN_HEIGHT, FALSE, FALSE)
    SetRenderToImage(img, FALSE)
    i as integer
    j as integer
    for i = 0 to MAP_SIZE_X-1
        for j = 0 to MAP_SIZE_Y-1
            SetSpriteX(mi.grid_sprites[0, i, j], i*CURSOR_SIZE)
            SetSpriteY(mi.grid_sprites[0, i, j], j*CURSOR_SIZE)
                        DrawSprite(mi.grid_sprites[0, i, j])
        next j
    next i
    SetRenderToScreen()
    SaveImage(img, "test.png")
    DeleteImage(img)
endfunction

The problem: The first time the .png file created in media folder but was empty, so I deleted it, but after that it doesn't even created, or if it does it's not media folder nor the project root folder.

UPDATE: It saves the image in appdata folder, following SaveImage Documentation, it says "Alternatively you can specify a path beginning with a forward slash to choose a location from the root of the write folder, ignoring any SetFolder folder." but trying /test.png or ./test.png doesn't seem to work.

UPDATE2: using "/" + GetReadPath() + "test.png" and GetReadPath() + "test.png" didn't work

UPDATE3: As I understood adding / means to specify relative path from the Write folder, in the other hand, SetFolder also accepts only relative path so I cannot do something like SetFolder(GetReadPath()), I've tried and it said it needs a relative path.

dezashibi commented 7 months ago

Fixed finally using this code:

first I got the copy file function

// got from: https://www.appgamekit.com/documentation-studio/Reference/File/WriteByte.htm
function copy_file(file_name$ as string, new_name$ as string)
    // open the two files
    source_file as integer
    dest_file as integer
    source_file = OpenToRead(file_name$)
    dest_file = OpenToWrite(new_name$)

    // while we are not at the end of the source file
    while FileEOF(source_file) = 0
        // write the next byte in the source file to the dest file
        // by going through it byte by byte, this function will be able to copy any file, regardless of format
        WriteByte(dest_file, ReadByte(source_file))
    endwhile

    // close the files
    CloseFile(source_file)
    CloseFile(dest_file)
endfunction

then for copying the file I would use this:

function MapInfo_save_as_image(mi ref as MapInfo)
    img as integer
    img = CreateRenderImage(DEFAULT_SCREEN_HEIGHT, DEFAULT_SCREEN_HEIGHT, FALSE, FALSE)
    SetRenderToImage(img, FALSE)
    i as integer
    j as integer
    for i = 0 to MAP_SIZE_X-1
        for j = 0 to MAP_SIZE_Y-1
        SetSpriteX(mi.grid_sprites[0, i, j], i*CURSOR_SIZE)
        SetSpriteY(mi.grid_sprites[0, i, j], j*CURSOR_SIZE)
                DrawSprite(mi.grid_sprites[0, i, j])
    next j
    next i
    SetRenderToScreen()

    file$ as string
    file$ = "test.png"
    SaveImage(img, file$)

    source_path$ as string
    source_path$ = "raw:" + JoinPaths(GetWritePath() + "media", file$)

    copy_file(source_path$, "raw:" + JoinPaths(GetReadPath(), file$))
    Message("image is saved as '" + file$ + "'")

    DeleteImage(img)
    DeleteFile(source_path$)
endfunction
dezashibi commented 7 months ago

Fixed with the solution in the previous comment