SOLVED An issue I've been having for a long time... having my own file format with surface buffers

mikix

Member
As you can see in the code, I am trying to lock down the surfaces from duplicating themselves when they load from my file format. But without success. Why does it duplicate the last surface layer on to the first surface layer?

The Saving

GML:
           var filesave;
filesave = get_save_filename("Shadowforge Project File|*.sf", "");


global.final_surf = s4
if global.final_surf = s4
{
     buffer_get_surface(global.bufferlay4, s4, 0)
            global.bufferlay4 = buffer_save(global.bufferlay4,filesave);
}

global.final_surf = s5

if global.final_surf = s5
{
     buffer_get_surface(global.bufferlay5, s5, 0)
            global.bufferlay5 = buffer_save(global.bufferlay5,filesave);
}
       
     
       global.bufferlay4 = buffer_save(global.bufferlay4,filesave);
       global.bufferlay5 = buffer_save(global.bufferlay5,filesave);

The Loading

GML:
       var fileload;

fileload = get_open_filename("Shadowforge Project File|*.sf", "");
   //buffer_seek(buffersurf, buffer_seek_start, 0);




global.final_surf = s4
if global.final_surf = s4
{
global.bufferlay4 = buffer_load(fileload)
    buffer_set_surface(global.bufferlay4,s4,0)
}

global.final_surf = s5
if global.final_surf = s5
{
global.bufferlay5 = buffer_load(fileload)
    buffer_set_surface(global.bufferlay5,s5,0)
}
 

chamaeleon

Member
I can't speak to the problem at hand directly, but if I were to assign a value/variable to another variable on one line and then compare the two values on the following line, I would expect the condition to be true 100% of the time regardless of what the value was that was assigned. For the code posted I would expect the code to execute as if the if condition was not there and the code it protects was just written directly after assignment.
 

mikix

Member
I remade it. It saves and loads all the buffers but only if you are not exiting the app. Could anyone help me out? Is it not allowed to make your own file extensions in GMS? Or am I doing something wrong in the code?


Saving

GML:
           var filesave;
filesave = get_save_filename("Shadowforge Project File|*.sf", "");

pos4 = buffer_seek(global.bufferlay4,buffer_seek_start,0)
pos5 = buffer_seek(global.bufferlay5,buffer_seek_start,0)
pos6 = buffer_seek(global.bufferlay6,buffer_seek_start,0)
pos7 = buffer_seek(global.bufferlay7,buffer_seek_start,0)
pos8 = buffer_seek(global.bufferlay8,buffer_seek_start,0)
pos9 = buffer_seek(global.bufferlay9,buffer_seek_start,0)
pos10 = buffer_seek(global.bufferlay10,buffer_seek_start,0)
pos11 = buffer_seek(global.bufferlay11,buffer_seek_start,0)
pos12 = buffer_seek(global.bufferlay12,buffer_seek_start,0)
pos13 = buffer_seek(global.bufferlay13,buffer_seek_start,0)

     buffer_get_surface(global.bufferlay4, s4, 0)



     buffer_get_surface(global.bufferlay5, s5, 0)

     buffer_get_surface(global.bufferlay6, s6, 0)
    
    buffer_get_surface(global.bufferlay7, s7, 0)     
      
      buffer_get_surface(global.bufferlay8, s8, 0) 

 buffer_get_surface(global.bufferlay9, s9, 0)

 buffer_get_surface(global.bufferlay10, s10, 0)

 buffer_get_surface(global.bufferlay11, s11, 0)
 
  buffer_get_surface(global.bufferlay12, s12, 0)
 
   buffer_get_surface(global.bufferlay13, s13, 0)

       if filesave != ""
    {
    buffer_save_ext(global.bufferlay4,filesave,pos4,16834)
    buffer_save_ext(global.bufferlay5,filesave,pos5,16834)
    buffer_save_ext(global.bufferlay6,filesave,pos6,16834)
    buffer_save_ext(global.bufferlay7,filesave,pos7,16834)
    buffer_save_ext(global.bufferlay8,filesave,pos8,16834)
    buffer_save_ext(global.bufferlay9,filesave,pos9,16834)
    buffer_save_ext(global.bufferlay10,filesave,pos10,16834)
    buffer_save_ext(global.bufferlay11,filesave,pos11,16834)
    buffer_save_ext(global.bufferlay12,filesave,pos12,16834)
    buffer_save_ext(global.bufferlay13,filesave,pos13,16834)
    }

Loading

GML:
         var fileload;

fileload = get_open_filename("Shadowforge Project File|*.sf", "");
   //buffer_seek(buffersurf, buffer_seek_start, 0);





    buffer_set_surface(global.bufferlay4,s4,0)




    buffer_set_surface(global.bufferlay5,s5,0)
    
        buffer_set_surface(global.bufferlay6,s6,0)
        
            buffer_set_surface(global.bufferlay7,s7,0)
            
                buffer_set_surface(global.bufferlay8,s8,0)

    buffer_set_surface(global.bufferlay9,s9,0)
    
    buffer_set_surface(global.bufferlay10,s10,0)
    
    buffer_set_surface(global.bufferlay11,s11,0)
    
    buffer_set_surface(global.bufferlay12,s12,0)
    
    buffer_set_surface(global.bufferlay13,s13,0)


pos4 = buffer_seek(global.bufferlay4,buffer_seek_start,0)
pos5 = buffer_seek(global.bufferlay5,buffer_seek_start,0)
pos6 = buffer_seek(global.bufferlay6,buffer_seek_start,0)
pos7 = buffer_seek(global.bufferlay7,buffer_seek_start,0)
pos8 = buffer_seek(global.bufferlay8,buffer_seek_start,0)
pos9 = buffer_seek(global.bufferlay9,buffer_seek_start,0)
pos10 = buffer_seek(global.bufferlay10,buffer_seek_start,0)
pos11 = buffer_seek(global.bufferlay11,buffer_seek_start,0)
pos12 = buffer_seek(global.bufferlay12,buffer_seek_start,0)
pos13 = buffer_seek(global.bufferlay13,buffer_seek_start,0)


       if fileload != ""
    {
    buffer_load_ext(global.bufferlay4,fileload,pos4)
    buffer_load_ext(global.bufferlay5,fileload,pos5)
    buffer_load_ext(global.bufferlay6,fileload,pos6)
    buffer_load_ext(global.bufferlay7,fileload,pos7)
    buffer_load_ext(global.bufferlay8,fileload,pos8)
    buffer_load_ext(global.bufferlay9,fileload,pos9)
    buffer_load_ext(global.bufferlay10,fileload,pos10)
    buffer_load_ext(global.bufferlay11,fileload,pos11)
    buffer_load_ext(global.bufferlay12,fileload,pos12)
    buffer_load_ext(global.bufferlay13,fileload,pos13)
    }
 

chamaeleon

Member
I would imagine buffer_save_ext() rewrites the file for every write operation. It doesn't append each buffer to the file, unless I'm mistaken. You might want to have a single load/save buffer into which you copy all your buffers to/from (at appropriate offset within the save/load buffer), which is used for the saving and loading.
 

mikix

Member
Unfortunately, I can't test it because it bugs my layer system. At the start, I go up to 100 layers and then instantly to layer 4 (because I need my layers to be as "existing"). This code makes it go up forever.
 

chamaeleon

Member
Unfortunately, I can't test it because it bugs my layer system. At the start, I go up to 100 layers and then instantly to layer 4 (because I need my layers to be as "existing"). This code makes it go up forever.
If you have a set of buffers to save, I fail to see how creating one more buffer to hold their combined content would be a problem (besides temporarily using twice the amount of memory). If you need a set of buffers for loading purposes, in the same manner I fail to see how using a temporary buffer for the file content is a problem.

I'm not addressing any issues related to buffers and layers and drawing, only file serialization.
 

mikix

Member
Sorry, the bug was just recently squashed. Well, making the way you told me did not work. Can you maybe show it in a GML code snippet?

Did you mean this?

GML:
       if fileload != ""
    {
    global.bufferlay4=buffer_load(fileload)
    global.bufferlay5=buffer_load(fileload)
    global.bufferlay6=buffer_load(fileload)
    global.bufferlay7=buffer_load(fileload)
    global.bufferlay8=buffer_load(fileload)
    global.bufferlay9=buffer_load(fileload)
    global.bufferlay10=buffer_load(fileload)
    global.bufferlay11=buffer_load(fileload)
    global.bufferlay12=buffer_load(fileload)
    global.bufferlay13=buffer_load(fileload)
    }
 

chamaeleon

Member
I mean you need a single buffer_save()/buffer_save_ext() call and a single buffer_load()/buffer_load_ext() call, using a single buffer in each case. Assemble this buffer from all your "layer" buffers before the save call, and extract the "layer" buffers from the single load buffer after the load call. Both the assembly and extraction would use buffer_copy(), I'd imagine to/from the save/load buffer. You would have as many buffer_copy() calls as you have these layers, coupled with a single save/load operation of this buffer.
 

mikix

Member
It gives the same result. All I want is to load my own file extension into the software even after I exit the app and open it up again. Feels bad having worked on a painting app for over a year and this important feature can not be used. I'd rather not use sprite_replace as that is heavy on performance.


GML:
  if mouse_check_button_pressed(mb_left) && point_in_rectangle(device_mouse_x_to_gui(0),device_mouse_y_to_gui(0),16,32,32,48)
   {
      
buffer_copy(global.bufferlay4,0,16834,buffersurf,0)
buffer_copy(global.bufferlay5,0,16834,buffersurf,0)
buffer_copy(global.bufferlay6,0,16834,buffersurf,0)
buffer_copy(global.bufferlay7,0,16834,buffersurf,0)
buffer_copy(global.bufferlay8,0,16834,buffersurf,0)
buffer_copy(global.bufferlay9,0,16834,buffersurf,0)
buffer_copy(global.bufferlay10,0,16834,buffersurf,0)
buffer_copy(global.bufferlay11,0,16834,buffersurf,0)
buffer_copy(global.bufferlay12,0,16834,buffersurf,0)
buffer_copy(global.bufferlay13,0,16834,buffersurf,0)
      
                  var filesave;
filesave = get_save_filename("Shadowforge Project File|*.sf", "");
       if filesave != ""
    {
buffer_save(buffersurf,filesave)

    }





     buffer_get_surface(global.bufferlay4, s4, 0)



     buffer_get_surface(global.bufferlay5, s5, 0)

     buffer_get_surface(global.bufferlay6, s6, 0)
    
    buffer_get_surface(global.bufferlay7, s7, 0)     
      
      buffer_get_surface(global.bufferlay8, s8, 0) 

 buffer_get_surface(global.bufferlay9, s9, 0)

 buffer_get_surface(global.bufferlay10, s10, 0)

 buffer_get_surface(global.bufferlay11, s11, 0)
 
  buffer_get_surface(global.bufferlay12, s12, 0)
 
   buffer_get_surface(global.bufferlay13, s13, 0)

GML:
      if mouse_check_button_pressed(mb_left) && point_in_rectangle(device_mouse_x_to_gui(0),device_mouse_y_to_gui(0),16,16,32,32)
   {





if file_exists("savestate.dat")
{
with (all) instance_destroy();

         var fileload;

fileload = get_open_filename("Shadowforge Project File|*.sf", "");
   //buffer_seek(buffersurf, buffer_seek_start, 0);


                
    buffer_set_surface(global.bufferlay4,s4,0)




    buffer_set_surface(global.bufferlay5,s5,0)
    
        buffer_set_surface(global.bufferlay6,s6,0)
        
            buffer_set_surface(global.bufferlay7,s7,0)
            
                buffer_set_surface(global.bufferlay8,s8,0)

    buffer_set_surface(global.bufferlay9,s9,0)
    
    buffer_set_surface(global.bufferlay10,s10,0)
    
    buffer_set_surface(global.bufferlay11,s11,0)
    
    buffer_set_surface(global.bufferlay12,s12,0)
    
    buffer_set_surface(global.bufferlay13,s13,0)


buffer_copy(buffersurf,0,16834,global.bufferlay4,0)
buffer_copy(buffersurf,0,16834,global.bufferlay5,0)
buffer_copy(buffersurf,0,16834,global.bufferlay6,0)
buffer_copy(buffersurf,0,16834,global.bufferlay7,0)
buffer_copy(buffersurf,0,16834,global.bufferlay8,0)
buffer_copy(buffersurf,0,16834,global.bufferlay9,0)
buffer_copy(buffersurf,0,16834,global.bufferlay10,0)
buffer_copy(buffersurf,0,16834,global.bufferlay11,0)
buffer_copy(buffersurf,0,16834,global.bufferlay12,0)
buffer_copy(buffersurf,0,16834,global.bufferlay13,0)

       if fileload != ""
    {
       buffersurf=buffer_load(fileload)

    }
 

chamaeleon

Member
Well, of course it won't work if you overwrite each layer with the next by starting the copy operation at offset 0 (the last argument) instead of using an offset that represent how much data you have copied to the buffer so far.
 

mikix

Member
I read the manual about it, so this is similiar to the manual:
Code:
buffer_copy(global.bufferlay4,0,16834,buffersurf,16834)
But, when I exit my app and launch it again, it still does not show the surface layers.
 

chamaeleon

Member
I read the manual about it, so this is similiar to the manual:
Code:
buffer_copy(global.bufferlay4,0,16834,buffersurf,16834)
But, when I exit my app and launch it again, it still does not show the surface layers.
Last offset should be 0 for the first buffer, the second 16K, the third 32K, and so on, given that it looks like your buffers are 16K each (if not, adjust s necessary), when you perform the copy operation. Again, I'm only talking about buffers, their content, and the writing and reading to and from disk. I have not spent any time thinking about how your buffers interact with layers or surfaces. I have no doubt that you can accomplish what you want given that gms supports synchronizing data between buffers and surfaces though. If you have a non-file operation problem with that, you might want have a separate thread for that, but it would have to rely on the file operation stuff being correct first.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
Marking as solved. But I didn't do it with buffers.
Would be nice if you told us what you DID do... ;) Keep in mind that this forum is also a type of knowledge base and as such people can search it and find solutions to their own issues. Just saying you solved it is great for you but sucks for anyone else coming to the topic hoping to find a solution themselves.
 

mikix

Member
Sure. I saved the surfaces in file and replaced them with a sprite on surface set target. Not the most efficient way, but it'll do for now.
 
Top