Recent

Author Topic: [SOLVED] Write/Read to a file with specific x and y coordinates  (Read 8649 times)

howardpc

  • Hero Member
  • *****
  • Posts: 3613
Re: Write/Read to a file with specific x and y coordinates
« Reply #15 on: September 13, 2016, 09:48:29 pm »
If I have understood manu12x correctly I think he is looking for something like this to be incorporated in his program:

Code: Pascal  [Select][+][-]
  1. program pTextFileState;
  2.  
  3. {$mode objfpc}{H+}
  4.  
  5. uses sysutils, classes;
  6.  
  7. const
  8.   DataFileName = 'data' + PathDelim + 'Options.txt';
  9.  
  10. procedure SaveStateToTextFile(aState, aLineNumber: integer);
  11. var
  12.   sl: TStringList;
  13. begin
  14.   if not FileExists(GetCurrentDir + PathDelim + DataFileName) then
  15.     WriteLn('Missing data file "', DataFileName, '"')
  16.   else begin
  17.     sl:=TStringList.Create;
  18.     try
  19.       sl.LoadFromFile(DataFileName);
  20.       sl[aLineNumber - 1]:=IntToStr(aState);
  21.       sl.SaveToFile(DataFileName);
  22.     finally
  23.       sl.Free;
  24.     end;
  25.   end;
  26. end;
  27.  
  28. function ReadStateFromTextFile(aLineNumber: integer): integer;
  29. var
  30.   sl: TStringList;
  31. begin
  32.   if not FileExists(GetCurrentDir + PathDelim + DataFileName) then
  33.     WriteLn('Missing data file "', DataFileName, '"')
  34.   else begin
  35.     sl:=TStringList.Create;
  36.     try
  37.       sl.LoadFromFile(DataFileName);
  38.       if not TryStrToInt(sl[aLineNumber - 1], Result)
  39.         then Result:= -1;
  40.     finally
  41.       sl.Free;
  42.     end;
  43.   end;
  44. end;
  45.  
  46. procedure SDL_WriteCheckBox( Name : String; x, y : integer; Number : integer );
  47. var
  48.   Rect : PSDL_Rect;
  49.   Clip : PSDL_Rect;
  50.   State : Integer;
  51. begin
  52.   New( Rect );
  53.   New( Clip );
  54.  
  55.   State:=ReadStateFromTextFile(Number);
  56.  
  57.   // use or change State, Name, x, y ...
  58.  
  59.   SaveStateToTextFile(State, Number);
  60. end;
  61.  
  62. begin
  63.   SDL_WriteCheckBox('show fps', 300, 50, 2);
  64.   SDL_WriteCheckBox('show ...', 300, 70, 3);
  65.   SDL_WriteCheckBox('show dpi', 300, 90, 4);
  66.   // etc.
  67. end.


DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Write/Read to a file with specific x and y coordinates
« Reply #16 on: September 14, 2016, 06:58:33 am »
If I have understood manu12x correctly I think he is looking for something like this to be incorporated in his program:

Code: Pascal  [Select][+][-]
  1. program pTextFileState;
  2.  
  3. {$mode objfpc}{H+}
  4.  
  5. uses sysutils, classes;
  6.  
  7. const
  8.   DataFileName = 'data' + PathDelim + 'Options.txt';
  9.  
  10. procedure SaveStateToTextFile(aState, aLineNumber: integer);
  11. var
  12.   sl: TStringList;
  13. begin
  14.   if not FileExists(GetCurrentDir + PathDelim + DataFileName) then
  15.     WriteLn('Missing data file "', DataFileName, '"')
  16.   else begin
  17.     sl:=TStringList.Create;
  18.     try
  19.       sl.LoadFromFile(DataFileName);
  20.       sl[aLineNumber - 1]:=IntToStr(aState);
  21.       sl.SaveToFile(DataFileName);
  22.     finally
  23.       sl.Free;
  24.     end;
  25.   end;
  26. end;
  27.  
  28. function ReadStateFromTextFile(aLineNumber: integer): integer;
  29. var
  30.   sl: TStringList;
  31. begin
  32.   if not FileExists(GetCurrentDir + PathDelim + DataFileName) then
  33.     WriteLn('Missing data file "', DataFileName, '"')
  34.   else begin
  35.     sl:=TStringList.Create;
  36.     try
  37.       sl.LoadFromFile(DataFileName);
  38.       if not TryStrToInt(sl[aLineNumber - 1], Result)
  39.         then Result:= -1;
  40.     finally
  41.       sl.Free;
  42.     end;
  43.   end;
  44. end;
  45.  
  46. procedure SDL_WriteCheckBox( Name : String; x, y : integer; Number : integer );
  47. var
  48.   Rect : PSDL_Rect;
  49.   Clip : PSDL_Rect;
  50.   State : Integer;
  51. begin
  52.   New( Rect );
  53.   New( Clip );
  54.  
  55.   State:=ReadStateFromTextFile(Number);
  56.  
  57.   // use or change State, Name, x, y ...
  58.  
  59.   SaveStateToTextFile(State, Number);
  60. end;
  61.  
  62. begin
  63.   SDL_WriteCheckBox('show fps', 300, 50, 2);
  64.   SDL_WriteCheckBox('show ...', 300, 70, 3);
  65.   SDL_WriteCheckBox('show dpi', 300, 90, 4);
  66.   // etc.
  67. end.
Yes something like! I won't write "try" and "finally" because,it says me there's no identifier..
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Thaddy

  • Hero Member
  • *****
  • Posts: 10695
Re: Write/Read to a file with specific x and y coordinates
« Reply #17 on: September 14, 2016, 09:22:09 am »
The try/finally's are correct programming: that ensures the lists get free'd. What is you problem?
Plz explain?

 

TinyPortal © 2005-2018