Recent

Author Topic: Help with types  (Read 5036 times)

thenarrator

  • New Member
  • *
  • Posts: 14
Help with types
« on: October 05, 2015, 08:31:03 am »
Having issues with types...

Am intending the BunSelect function to check which button the user selects and then display the corresponding image(s) on the screen.

Code: Pascal  [Select][+][-]
  1. program SandwichTrainer;
  2. uses SwinGame, sgTypes, SysUtils;
  3.  
  4. type
  5.         BaseKind = (Bun, Rye);
  6.         SauceKind = (DorothyS, Ketchup, Mayo, Mustard);
  7.         SaladKind = (Lettuce, Tomato, Beetroot, Pickles, Slaw, Strings, None);
  8.         MeatKind = (Chicken, Pork, SaucyBeef, SliceBeef, Pastrami, CAndL);
  9.         CheeseKind = (Tasty, Swiss, Scamorza, No);
  10.         SandwichKind = (Carolina, Dorothy, ZMan, NewYork, Aussie, TwoMeat, Kids);
  11.  
  12.         BaseData = record
  13.                 base: BaseKind;
  14.                 topBmp: Bitmap;
  15.                 botBmp: Bitmap;
  16.         end;
  17.  
  18.         InputData = record
  19.                 bun: BaseData;
  20.                 sauce: array of SauceKind;
  21.                 botSal: array of SaladKind;
  22.                 meat: MeatKind;
  23.                 topSal: SaladKind;
  24.                 cheese: CheeseKind;
  25.                 SAndS: Boolean;
  26.         end;
  27.  
  28.         TargetData = record
  29.                 base: BaseKind;
  30.                 sauce: array of SauceKind;
  31.                 botSal: array of SaladKind;
  32.                 meat: MeatKind;
  33.                 topSal: SaladKind;
  34.                 cheese: CheeseKind;
  35.                 SAndS: Boolean;
  36.         end;
  37.  
  38.         GameData = record
  39.                 input: InputData;
  40.                 target: TargetData;
  41.                 score: Integer;
  42.         end;
  43.  
  44. procedure LoadResources();
  45. begin
  46.         LoadBitmapNamed('BottomBun',    'BottomBun.png');
  47.         LoadBitmapNamed('TopBun',               'TopBun.png');
  48.         LoadBitmapNamed('Rye',          'RyeBread.png');
  49. end;
  50.  
  51. //procedure Setup(var game: SandwichData);
  52. //begin
  53. //      game.base := Bun;
  54. //      game.topBmp := BitmapNamed('TopBun');
  55. //      game.botBmp := BitmapNamed('BottomBun');
  56. //      DrawBitmap(game.botBmp, Round((ScreenWidth() - BitmapWidth(game.botBmp) - 200) / 2), Round((ScreenHeight() - BitmapHeight(game.botBmp))));
  57. //      DrawBitmap(game.topBmp, Round((ScreenWidth() - BitmapWidth(game.topBmp) - 200) / 2), 0);
  58. //end;
  59.  
  60. procedure BunSelect(var game: GameData);
  61. begin
  62.         repeat
  63.                 ProcessEvents();
  64.         UpdateInterface();
  65.                 HidePanel('MenuPanel');
  66.                 ShowPanel('BreadPanel');
  67.                 DrawInterface();
  68.         RefreshScreen(60);
  69.         if ButtonClicked('BunButton') then
  70.         begin
  71.                 game.input.bun.base := Bun;
  72.                 game.input.bun.topBmp := BitmapNamed('TopBun');
  73.                 game.input.bun.botBmp := BitmapNamed('BottomBun');
  74.                 DrawBitmap(game.input.bun.topBmp, Round((ScreenWidth() - BitmapWidth('BottomBun')) / 2), Round((ScreenHeight() - BitmapHeight('BottomBun')) / 2));
  75.                 DrawBitmap(game.input.bun.botBmp, Round((ScreenWidth() - BitmapWidth('TopBun')) / 2), Round((ScreenHeight() - BitmapHeight('TopBun')) / 2)));
  76.         end;
  77.  
  78.         if ButtonClicked('RyeButton') then
  79.         begin
  80.                 game.input.bun.base := Rye;
  81.                 game.input.bun.topBmp := BitmapNamed('Rye');
  82.                 game.input.bun.botBmp := BitmapNamed('Rye');
  83.                 DrawBitmap(game.input.bun.topBmp , Round((ScreenWidth() - BitmapWidth('Rye')) / 2), Round((ScreenHeight() - BitmapHeight('Rye')) / 2)))
  84.  
  85.         until WindowCLoseRequested();
  86. end;
  87.  
  88. procedure SandwichSelect(var game: GameData);
  89. begin
  90.                 SetLength(game.target.botSal, 3);
  91.                 SetLength(game.target.sauce, 2);
  92.  
  93.         repeat
  94.         ProcessEvents();
  95.     UpdateInterface();
  96.                 if ButtonClicked('AussieButton') then
  97.                 begin
  98.                         game.target.base := Bun;
  99.                         game.target.sauce[0] := DorothyS;
  100.                         game.target.sauce[1] := Mayo;
  101.                         game.target.botSal[0] := Lettuce;
  102.                         game.target.botSal[1] := Beetroot;
  103.                         game.target.botSal[2] := Tomato;
  104.                         game.target.meat := Chicken;
  105.                         game.target.SAndS := True;
  106.                         game.target.topSal := None;
  107.                         game.target.cheese := Tasty;
  108.  
  109.                         HidePanel('MenuPanel');
  110.                 end;
  111.  
  112.                 if ButtonClicked('DorothyButton') then
  113.                 begin
  114.                         game.target.base := Bun;
  115.                         game.target.sauce[0] := DorothyS;
  116.                         game.target.botSal[0] := Pickles;
  117.                         game.target.meat := SaucyBeef;
  118.                         game.target.SAndS := False;
  119.                         game.target.topSal := Slaw;
  120.                         game.target.cheese := No;
  121.                 end;
  122.  
  123.                 if ButtonClicked('CarolinaButton') then
  124.                 begin
  125.                         game.target.base := Bun;
  126.                         game.target.sauce[0] := DorothyS;
  127.                         game.target.botSal[0] := None;
  128.                         game.target.meat := Pork;
  129.                         game.target.SAndS := True;
  130.                         game.target.topSal := Slaw;
  131.                         game.target.cheese := No;
  132.                 end;
  133.  
  134.                 if ButtonClicked('ZManButton') then
  135.                 begin
  136.                         game.target.base := Bun;
  137.                         game.target.sauce[0] := DorothyS;
  138.                         game.target.botSal[0] := None;
  139.                         game.target.meat := SliceBeef;
  140.                         game.target.SAndS := True;
  141.                         game.target.topSal := Strings;
  142.                         game.target.cheese := Scamorza;
  143.                 end;
  144.  
  145.                 if ButtonClicked('NewYorkButton') then
  146.                 begin
  147.                         game.target.base := Rye;
  148.                         game.target.sauce[0] := Mustard;
  149.                         game.target.botSal[0] := Pickles;
  150.                         game.target.meat := Pastrami;
  151.                         game.target.SAndS := True;
  152.                         game.target.topSal := Slaw;
  153.                         game.target.cheese := Swiss;
  154.                 end;
  155.  
  156.                 if ButtonClicked('KidsButton') then
  157.                 begin
  158.                         game.target.base:= Bun;
  159.                         game.target.sauce[0] := Ketchup;
  160.                         game.target.botSal[0] := None;
  161.                         game.target.meat := Chicken;
  162.                         game.target.SAndS := True;
  163.                         game.target.topSal := None;
  164.                         game.target.cheese := No;
  165.                 end;
  166.  
  167.                 if ButtonClicked('TwoMeatButton') then
  168.                 begin
  169.                         game.target.base := Bun;
  170.                         game.target.sauce[0] := DorothyS;
  171.                         game.target.botSal[0] := Lettuce;
  172.                         game.target.botSal[1] := Tomato;
  173.                         game.target.meat := CAndL;
  174.                         game.target.SAndS := True;
  175.                         game.target.topSal := None;
  176.                         game.target.cheese := No;
  177.                 end;
  178.  
  179.         DrawInterface();
  180.     RefreshScreen(60);
  181.  
  182.         until(ButtonClicked('AussieButton') or WindowCLoseRequested());
  183. end;
  184.  
  185. procedure Main();
  186. var
  187.         game: GameData;
  188. begin
  189.         OpenAudio();
  190.     OpenGraphicsWindow('SandwichTrainer', 1000, 800);
  191.     LoadDefaultColors();
  192.     LoadResources();
  193.         LoadResourceBundle( 'NumberBundle.txt' );
  194.    
  195.  
  196.     GUISetForegroundColor( ColorBlack );
  197.     GUISetBackgroundColor( ColorWhite );
  198.    
  199.         ClearScreen(ColorWhite);
  200.  
  201.         ShowPanel('MenuPanel');
  202.     //Setup(gameData);
  203.    
  204.     SandwichSelect(game);
  205.     BunSelect(game);
  206.    
  207. end;
  208.  
  209. begin
  210.         Main();
  211. end.
  212.  

and the code for DrawBitmap is below

Code: Pascal  [Select][+][-]
  1.   procedure DrawBitmap(src: Bitmap; x: Single; y: Single); overload;
  2.   begin
  3.     sgImages.DrawBitmap(src,x,y);
  4.   end;

the error code:
Code: Pascal  [Select][+][-]
  1. (74,87) Error: Incompatible type for arg no. 1: Got "Constant String", expected "BitmapCell"

how is this occurring when game.input.bun.topBmp is defined as a Bitmap type in the initial declarations?

Thanks.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Help with types
« Reply #1 on: October 05, 2015, 09:15:40 am »
Show the function header of BitmapWidth and BitmapHeight

thenarrator

  • New Member
  • *
  • Posts: 14
Re: Help with types
« Reply #2 on: October 05, 2015, 12:51:54 pm »
Code: Pascal  [Select][+][-]
  1.  function BitmapWidth(bmp: Bitmap): Longint; overload;
Code: Pascal  [Select][+][-]
  1. function BitmapHeight(bmp: Bitmap): Longint; overload;

 not sure how they could be a problem if the error is for argument one though?

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Help with types
« Reply #3 on: October 05, 2015, 01:20:21 pm »
Code: Pascal  [Select][+][-]
  1.  function BitmapWidth(bmp: Bitmap): Longint; overload;
Code: Pascal  [Select][+][-]
  1. function BitmapHeight(bmp: Bitmap): Longint; overload;

 not sure how they could be a problem if the error is for argument one though?
Well, look at these lines:
Code: Pascal  [Select][+][-]
  1. DrawBitmap(game.input.bun.topBmp, Round((ScreenWidth() - BitmapWidth('BottomBun')) / 2), Round((ScreenHeight() - BitmapHeight('BottomBun')) / 2));
  2. DrawBitmap(game.input.bun.botBmp, Round((ScreenWidth() - BitmapWidth('TopBun')) / 2), Round((ScreenHeight() - BitmapHeight('TopBun')) / 2)));
  3. ...
  4. DrawBitmap(game.input.bun.topBmp , Round((ScreenWidth() - BitmapWidth('Rye')) / 2), Round((ScreenHeight() - BitmapHeight('Rye')) / 2)))
Within these lines you call BitmapWidth and BitmapHeight with a string as 1st parameter.
That's what the compiler is complaining about. You should call them with the bitmap as 1st parameter.

(note: the error isn't about the 1st parameter of DrawBitmap but the 1st parameter of BitmapWidth/Height)
« Last Edit: October 05, 2015, 01:23:12 pm by rvk »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Help with types
« Reply #4 on: October 05, 2015, 02:25:45 pm »
That's what the compiler is complaining about. You should call them with the bitmap as 1st parameter.
(note: the error isn't about the 1st parameter of DrawBitmap but the 1st parameter of BitmapWidth/Height)

To: thenarrator
You supplied string parameter (exp: 'BottomBun', 'TopBun') for BitmapWidth/BitmapHeight.

My guess for the correct code:

Code: Pascal  [Select][+][-]
  1.    begin
  2.       with game.input.bun do begin
  3.          base := Bun;
  4.          topBmp := BitmapNamed('TopBun');
  5.          botBmp := BitmapNamed('BottomBun');
  6.          DrawBitmap(topBmp, Round((ScreenWidth() - BitmapWidth(botBmp)) / 2), Round((ScreenHeight() - BitmapHeight(botBmp)) / 2));
  7.          DrawBitmap(botBmp, Round((ScreenWidth() - BitmapWidth(topBmp)) / 2), Round((ScreenHeight() - BitmapHeight(topBmp)) / 2)));
  8.       end;
  9.    end;

Note: using "with" keyword usually makes code easier to read.
« Last Edit: October 05, 2015, 02:27:58 pm by Handoko »

thenarrator

  • New Member
  • *
  • Posts: 14
Re: Help with types
« Reply #5 on: October 06, 2015, 02:56:36 am »
Thanks for the help guys, embarrassing error...  ::)

the code now looks like this
Code: Pascal  [Select][+][-]
  1. program SandwichTrainer;
  2. uses SwinGame, sgTypes, SysUtils;
  3.  
  4. type
  5.         FoodKind = (Bun, Rye, DorothyS, Ketchup, Mayo, Mustard, Lettuce, Tomato, Beetroot, Pickles, Slaw, Strings, None, Chicken, Pork, SaucyBeef, SliceBeef, Pastrami, CAndL, Tasty, Swiss, Scamorza);
  6.         SandwichKind = (Carolina, Dorothy, ZMan, NewYork, Aussie, TwoMeat, Kids);
  7.  
  8.         BaseData = record
  9.                 name: FoodKind;
  10.                 topBmp: Bitmap;
  11.                 botBmp: Bitmap;
  12.         end;
  13.  
  14.         FoodData = record
  15.                 name: FoodKind;
  16.                 bmp: Bitmap;
  17.         end;
  18.  
  19.         InputData = record
  20.                 base: BaseData;
  21.                 toppings: array of FoodData;
  22.                 SAndS: Boolean;
  23.         end;
  24.  
  25.         TargetData = record
  26.                 base: BaseData;
  27.                 toppings: array of FoodData;
  28.                 SAndS: Boolean;
  29.         end;
  30.  
  31.         GameData = record
  32.                 input: InputData;
  33.                 target: TargetData;
  34.                 score: Integer;
  35.         end;
  36.  
  37. procedure LoadResources();
  38. begin
  39.         LoadBitmapNamed('BottomBun',    'BottomBun.png');
  40.         LoadBitmapNamed('TopBun',               'TopBun.png');
  41.         LoadBitmapNamed('Rye',          'RyeBread.png');
  42. end;
  43.  
  44. function BunBitmap(var base: BaseData): BaseData;
  45. begin
  46.         result.name := base.name;
  47.  
  48.         case base.name of
  49.                 Bun: begin
  50.                                 result.topBmp := BitmapNamed('TopBun');
  51.                         result.botBmp := BitmapNamed('BottomBun');     
  52.                  end;
  53.         Rye: begin
  54.                         result.topBmp := BitmapNamed('Rye');
  55.                         result.botBmp := BitmapNamed('Rye');
  56.                 end;
  57.     end;
  58. end;
  59.  
  60. procedure DrawBun(const base: BaseData);
  61. begin
  62.         DrawBitmap(base.topBmp, Round((ScreenWidth() - BitmapWidth(base.topBmp)) / 2), 10);
  63.         DrawBitmap(base.botBmp, Round((ScreenWidth() - BitmapWidth(base.botBmp)) / 2), Round((ScreenHeight() - BitmapHeight(base.botBmp)) + 10));
  64.         RefreshScreen(60);
  65.         repeat
  66.                
  67.         until((WindowCloseRequested));
  68. end;
  69.  
  70. procedure BunSelect(var game: GameData);
  71. begin
  72.         repeat
  73.                 ProcessEvents();
  74.         UpdateInterface();
  75.                 HidePanel('MenuPanel');
  76.                 ShowPanel('BreadPanel');
  77.                 DrawInterface();
  78.         RefreshScreen(60);
  79.         if ButtonClicked('BunButton') then
  80.         begin
  81.                 game.input.base.name := Bun;
  82.         end;
  83.  
  84.         if ButtonClicked('RyeButton') then
  85.         begin
  86.                 game.input.base.name := Rye;
  87.         end;
  88.  
  89.         until(WindowCloseRequested() or ButtonClicked('BunButton') or ButtonClicked('RyeButton'));
  90.  
  91.         DrawBun(BunBitmap(game.input.base));
  92. end;
  93.  
  94. procedure SandwichSelect(var game: GameData);
  95. begin
  96.  
  97.         repeat
  98.         ProcessEvents();
  99.     UpdateInterface();
  100.                 if ButtonClicked('AussieButton') then
  101.                 begin
  102.                         SetLength(game.target.toppings, 8);
  103.                         SetLength(game.input.toppings, 10);
  104.  
  105.                         game.target.base.name := Bun;
  106.                         game.target.toppings[0].name := DorothyS;
  107.                         game.target.toppings[1].name := Mayo;
  108.                         game.target.toppings[2].name := Lettuce;
  109.                         game.target.toppings[3].name := Beetroot;
  110.                         game.target.toppings[4].name := Tomato;
  111.                         game.target.toppings[5].name := Chicken;
  112.                         game.target.SAndS := True;
  113.                         game.target.toppings[6].name := None;
  114.                         game.target.toppings[7].name := Tasty;
  115.                 end;
  116.  
  117.                 if ButtonClicked('DorothyButton') then
  118.                 begin
  119.                         SetLength(game.target.toppings, 5);
  120.                         SetLength(game.input.toppings, 7);
  121.  
  122.                         game.target.base.name := Bun;
  123.                         game.target.toppings[0].name := DorothyS;
  124.                         game.target.toppings[1].name := Pickles;
  125.                         game.target.toppings[2].name := SaucyBeef;
  126.                         game.target.SAndS := False;
  127.                         game.target.toppings[3].name := Slaw;
  128.                         game.target.toppings[4].name := None;
  129.                 end;
  130.  
  131.                 if ButtonClicked('CarolinaButton') then
  132.                 begin
  133.                         SetLength(game.target.toppings, 5);
  134.                         SetLength(game.input.toppings, 7);
  135.  
  136.                         game.target.base.name := Bun;
  137.                         game.target.toppings[0].name := DorothyS;
  138.                         game.target.toppings[1].name := None;
  139.                         game.target.toppings[2].name := Pork;
  140.                         game.target.SAndS := True;
  141.                         game.target.toppings[3].name := Slaw;
  142.                         game.target.toppings[4].name := None;
  143.                 end;
  144.  
  145.                 if ButtonClicked('ZManButton') then
  146.                 begin
  147.                         SetLength(game.target.toppings, 5);
  148.                         SetLength(game.input.toppings, 7);
  149.  
  150.                         game.target.base.name := Bun;
  151.                         game.target.toppings[0].name := DorothyS;
  152.                         game.target.toppings[1].name := None;
  153.                         game.target.toppings[2].name := SliceBeef;
  154.                         game.target.SAndS := True;
  155.                         game.target.toppings[3].name := Strings;
  156.                         game.target.toppings[4].name := Scamorza;
  157.                 end;
  158.  
  159.                 if ButtonClicked('NewYorkButton') then
  160.                 begin
  161.                         SetLength(game.target.toppings, 5);
  162.                         SetLength(game.input.toppings, 7);
  163.  
  164.                         game.target.base.name := Rye;
  165.                         game.target.toppings[0].name := Mustard;
  166.                         game.target.toppings[1].name := Pickles;
  167.                         game.target.toppings[2].name := Pastrami;
  168.                         game.target.SAndS := True;
  169.                         game.target.toppings[3].name := Slaw;
  170.                         game.target.toppings[4].name := Swiss;
  171.                 end;
  172.  
  173.                 if ButtonClicked('KidsButton') then
  174.                 begin
  175.                         SetLength(game.target.toppings, 5);
  176.                         SetLength(game.input.toppings, 7);
  177.  
  178.                         game.target.base.name := Bun;
  179.                         game.target.toppings[0].name := Ketchup;
  180.                         game.target.toppings[1].name := None;
  181.                         game.target.toppings[2].name := Chicken;
  182.                         game.target.SAndS := True;
  183.                         game.target.toppings[3].name := None;
  184.                         game.target.toppings[4].name := None;
  185.                 end;
  186.  
  187.                 if ButtonClicked('TwoMeatButton') then
  188.                 begin
  189.                         SetLength(game.target.toppings, 6);
  190.                         SetLength(game.input.toppings, 8);
  191.  
  192.                         game.target.base.name := Bun;
  193.                         game.target.toppings[0].name := DorothyS;
  194.                         game.target.toppings[1].name := Lettuce;
  195.                         game.target.toppings[2].name := Tomato;
  196.                         game.target.toppings[3].name := CAndL;
  197.                         game.target.SAndS := True;
  198.                         game.target.toppings[4].name := None;
  199.                         game.target.toppings[5].name := None;
  200.                 end;
  201.  
  202.         DrawInterface();
  203.     RefreshScreen(60);
  204.  
  205.         until(ButtonClicked('AussieButton') or WindowCloseRequested());
  206. end;
  207.  
  208. procedure Main();
  209. var
  210.         game: GameData;
  211. begin
  212.         OpenAudio();
  213.     OpenGraphicsWindow('SandwichTrainer', 1000, 800);
  214.     LoadDefaultColors();
  215.     LoadResources();
  216.         LoadResourceBundle( 'NumberBundle.txt' );
  217.    
  218.  
  219.     GUISetForegroundColor( ColorBlack );
  220.     GUISetBackgroundColor( ColorWhite );
  221.    
  222.         ClearScreen(ColorWhite);
  223.  
  224.         ShowPanel('MenuPanel');
  225.     //Setup(gameData);
  226.    
  227.     SandwichSelect(game);
  228.     BunSelect(game);
  229.    
  230. end;
  231.  
  232. begin
  233.         Main();
  234. end.
  235.  

It compiles and allows me to press aussie which opens the bun menu. when i click bun the image appears however the program pinwheels after around 3-4 seconds and returns this when I force quit it.
"./run.sh: line 77: 12699 Killed: 9               "$EXE_PATH"
Im guessing I am somehow creating an infinite loop? but I can't see how if the data is being passed to DrawBun?

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Help with types
« Reply #6 on: October 06, 2015, 04:08:07 am »
I'm not very sure, but my guess is you put a loop without give it chances to process messages. See the line no. 65 - 67 of your code:

...
   repeat
   until ((WindowCloseRequested));
...

For more information, see here:
http://forum.lazarus.freepascal.org/index.php/topic,29850.msg189258.html#msg189258

thenarrator

  • New Member
  • *
  • Posts: 14
Re: Help with types
« Reply #7 on: October 07, 2015, 03:20:43 am »
Yes, the lack of process events was the problem, thanks.

Am now trying to add in the latest menu which selects salads, am having issues organizing the arrays so they can be accessed properly.

this compiles and runs but crashes when the bun button is pressed... returning "An unhandled exception occurred at $0024A65B :
EAccessViolation : Access violation
  $0024A65B
  $0024A867
  $0024AD59"

Is there any way to tell where in the code it is referring to?

Code: Pascal  [Select][+][-]
  1. program SandwichTrainer;
  2. uses SwinGame, sgTypes, SysUtils;
  3.  
  4. type
  5.         FoodKind = (Bun, Rye, DorothyS, Ketchup, Mayo, Mustard, Lettuce, Tomato, Beetroot, Pickles, Slaw, Strings, None, Chicken, Pork, SaucyBeef, SliceBeef, Pastrami, CAndL, Tasty, Swiss, Scamorza);
  6.         SandwichKind = (Carolina, Dorothy, ZMan, NewYork, Aussie, TwoMeat, Kids);
  7.  
  8.         BaseData = record
  9.                 name: FoodKind;
  10.                 topBmp: Bitmap;
  11.                 botBmp: Bitmap;
  12.         end;
  13.  
  14.         //FoodData = record
  15.         //      name: FoodKind;
  16.         //      bmp: Bitmap;
  17.         //end;
  18.  
  19.         InputData = record
  20.                 base: BaseData;
  21.                 toppings: array of FoodKind;
  22.                 SAndS: Boolean;
  23.         end;
  24.  
  25.         TargetData = record
  26.                 base: BaseData;
  27.                 toppings: array of FoodKind;
  28.                 SAndS: Boolean;
  29.         end;
  30.  
  31.         GameData = record
  32.                 input: InputData;
  33.                 target: TargetData;
  34.                 score: Integer;
  35.         end;
  36.  
  37. procedure LoadResources();
  38. begin
  39.         LoadBitmapNamed('BottomBun',    'BottomBun.png');
  40.         LoadBitmapNamed('TopBun',               'TopBun.png');
  41.         LoadBitmapNamed('Rye',          'RyeBread.png');
  42.         LoadBitmapNamed('Lettuce',              'Lettuce.png');
  43. end;
  44.  
  45. function BunBitmap(var base: BaseData): BaseData;
  46. begin
  47.         result.name := base.name;
  48.  
  49.         case base.name of
  50.                 Bun: begin
  51.                                 result.topBmp := BitmapNamed('TopBun');
  52.                         result.botBmp := BitmapNamed('BottomBun');     
  53.                  end;
  54.         Rye: begin
  55.                         result.topBmp := BitmapNamed('Rye');
  56.                         result.botBmp := BitmapNamed('Rye');
  57.                 end;
  58.     end;
  59. end;
  60.  
  61. function FoodBitmap(input: FoodKind): Bitmap;
  62. begin
  63.         case input of
  64.                 DorothyS: result := BitmapNamed('DorothyS');
  65.                 Ketchup: result := BitmapNamed('Ketchup');
  66.                 Mayo: result := BitmapNamed('Mayo');
  67.                 Mustard: result := BitmapNamed('Mustard');
  68.                 Lettuce: result := BitmapNamed('Lettuce');
  69.                 Tomato: result := BitmapNamed('Tomato');
  70.                 Beetroot: result := BitmapNamed('Beetroot');
  71.                 Pickles: result := BitmapNamed('Pickles');
  72.                 Slaw: result := BitmapNamed('Slaw');
  73.                 Strings: result := BitmapNamed('Strings');
  74.         end;
  75. end;
  76.  
  77. procedure DrawBun(const base: BaseData);
  78. begin
  79.         DrawBitmap(base.topBmp, Round((ScreenWidth() - BitmapWidth(base.topBmp)) / 2), 10);
  80.         DrawBitmap(base.botBmp, Round((ScreenWidth() - BitmapWidth(base.botBmp)) / 2), Round((ScreenHeight() - BitmapHeight(base.botBmp)) - 20));
  81.         RefreshScreen(60);
  82. end;
  83.  
  84. procedure DrawSandwich(const input: InputData);
  85. var
  86.         i: Integer;
  87. begin
  88.         for i := 0 to Length(input.toppings) do
  89.         begin
  90.                 DrawBitmap(FoodBitmap(input.toppings[i]), Round((ScreenWidth() - BitmapWidth(FoodBitmap(input.toppings[i]))) / 2), Round((ScreenHeight() - BitmapHeight(FoodBitmap(input.toppings[i]))) - 20));
  91.         end;
  92. end;
  93.  
  94. procedure BunSelect(var input: InputData);
  95. begin
  96.         repeat
  97.                 ProcessEvents();
  98.         UpdateInterface();
  99.                 HidePanel('MenuPanel');
  100.                 ShowPanel('BreadPanel');
  101.                 DrawInterface();
  102.         RefreshScreen(60);
  103.         if ButtonClicked('BunButton') then
  104.         begin
  105.                 input.base.name := Bun;
  106.         end;
  107.  
  108.         if ButtonClicked('RyeButton') then
  109.         begin
  110.                 input.base.name := Rye;
  111.         end;
  112.  
  113.         until(WindowCloseRequested() or ButtonClicked('BunButton') or ButtonClicked('RyeButton'));
  114.  
  115.         DrawBun(BunBitmap(input.base));
  116. end;
  117.  
  118. procedure SaladSelect(var input: InputData);
  119. var
  120.         i: Integer;
  121. begin
  122.  
  123.         SetLength(input.toppings, 0);
  124.  
  125.         repeat
  126.         ProcessEvents();
  127.     UpdateInterface();
  128.         HidePanel('BreadPanel');
  129.         ShowPanel('SaladPanel');
  130.         DrawInterface();
  131.     RefreshScreen(60);
  132.  
  133.     if ButtonClicked('LettuceButton') then
  134.     begin
  135.         SetLength(input.toppings, (Length(input.Toppings) +1));
  136.         input.toppings[0] := Lettuce;
  137.     end;
  138.  
  139.     DrawSandwich(input);
  140.  
  141.     until WindowCloseRequested();
  142. end;
  143.  
  144. procedure SandwichSelect(var game: GameData);
  145. begin
  146.  
  147.         repeat
  148.         ProcessEvents();
  149.     UpdateInterface();
  150.                 if ButtonClicked('AussieButton') then
  151.                 begin
  152.                         SetLength(game.target.toppings, 8);
  153.  
  154.                         game.target.base.name := Bun;
  155.                         game.target.toppings[0] := DorothyS;
  156.                         game.target.toppings[1] := Mayo;
  157.                         game.target.toppings[2] := Lettuce;
  158.                         game.target.toppings[3] := Beetroot;
  159.                         game.target.toppings[4] := Tomato;
  160.                         game.target.toppings[5] := Chicken;
  161.                         game.target.SAndS := True;
  162.                         game.target.toppings[6] := None;
  163.                         game.target.toppings[7] := Tasty;
  164.                 end;
  165.  
  166.                 if ButtonClicked('DorothyButton') then
  167.                 begin
  168.                         SetLength(game.target.toppings, 5);
  169.                        
  170.  
  171.                         game.target.base.name := Bun;
  172.                         game.target.toppings[0] := DorothyS;
  173.                         game.target.toppings[1] := Pickles;
  174.                         game.target.toppings[2] := SaucyBeef;
  175.                         game.target.SAndS := False;
  176.                         game.target.toppings[3] := Slaw;
  177.                         game.target.toppings[4] := None;
  178.                 end;
  179.  
  180.                 if ButtonClicked('CarolinaButton') then
  181.                 begin
  182.                         SetLength(game.target.toppings, 5);
  183.                        
  184.  
  185.                         game.target.base.name := Bun;
  186.                         game.target.toppings[0] := DorothyS;
  187.                         game.target.toppings[1] := None;
  188.                         game.target.toppings[2] := Pork;
  189.                         game.target.SAndS := True;
  190.                         game.target.toppings[3] := Slaw;
  191.                         game.target.toppings[4] := None;
  192.                 end;
  193.  
  194.                 if ButtonClicked('ZManButton') then
  195.                 begin
  196.                         SetLength(game.target.toppings, 5);
  197.                        
  198.  
  199.                         game.target.base.name := Bun;
  200.                         game.target.toppings[0] := DorothyS;
  201.                         game.target.toppings[1] := None;
  202.                         game.target.toppings[2] := SliceBeef;
  203.                         game.target.SAndS := True;
  204.                         game.target.toppings[3] := Strings;
  205.                         game.target.toppings[4] := Scamorza;
  206.                 end;
  207.  
  208.                 if ButtonClicked('NewYorkButton') then
  209.                 begin
  210.                         SetLength(game.target.toppings, 5);
  211.                        
  212.  
  213.                         game.target.base.name := Rye;
  214.                         game.target.toppings[0] := Mustard;
  215.                         game.target.toppings[1] := Pickles;
  216.                         game.target.toppings[2] := Pastrami;
  217.                         game.target.SAndS := True;
  218.                         game.target.toppings[3] := Slaw;
  219.                         game.target.toppings[4] := Swiss;
  220.                 end;
  221.  
  222.                 if ButtonClicked('KidsButton') then
  223.                 begin
  224.                         SetLength(game.target.toppings, 5);
  225.                        
  226.  
  227.                         game.target.base.name := Bun;
  228.                         game.target.toppings[0] := Ketchup;
  229.                         game.target.toppings[1] := None;
  230.                         game.target.toppings[2] := Chicken;
  231.                         game.target.SAndS := True;
  232.                         game.target.toppings[3] := None;
  233.                         game.target.toppings[4] := None;
  234.                 end;
  235.  
  236.                 if ButtonClicked('TwoMeatButton') then
  237.                 begin
  238.                         SetLength(game.target.toppings, 6);
  239.                        
  240.  
  241.                         game.target.base.name := Bun;
  242.                         game.target.toppings[0] := DorothyS;
  243.                         game.target.toppings[1] := Lettuce;
  244.                         game.target.toppings[2] := Tomato;
  245.                         game.target.toppings[3] := CAndL;
  246.                         game.target.SAndS := True;
  247.                         game.target.toppings[4] := None;
  248.                         game.target.toppings[5] := None;
  249.                 end;
  250.  
  251.         DrawInterface();
  252.     RefreshScreen(60);
  253.  
  254.         until(ButtonClicked('AussieButton') or WindowCloseRequested());
  255. end;
  256.  
  257. procedure Main();
  258. var
  259.         game: GameData;
  260. begin
  261.         OpenAudio();
  262.     OpenGraphicsWindow('SandwichTrainer', 1000, 800);
  263.     LoadDefaultColors();
  264.     LoadResources();
  265.         LoadResourceBundle( 'NumberBundle.txt' );
  266.    
  267.  
  268.     GUISetForegroundColor( ColorBlack );
  269.     GUISetBackgroundColor( ColorWhite );
  270.    
  271.         ClearScreen(ColorWhite);
  272.  
  273.         ShowPanel('MenuPanel');
  274.     //Setup(gameData);
  275.    
  276.     SandwichSelect(game);
  277.     BunSelect(game.input);
  278.     SaladSelect(game.input);
  279.    
  280. end;
  281.  
  282. begin
  283.         Main();
  284. end.
  285.  

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Help with types
« Reply #8 on: October 07, 2015, 10:30:34 am »
this compiles and runs but crashes when the bun button is pressed... returning "An unhandled exception occurred at $0024A65B :
EAccessViolation : Access violation
  $0024A65B
  $0024A867
  $0024AD59"

Is there any way to tell where in the code it is referring to?

Short answer: no.

You might try to enable range check, overflow check and io check for debugging. To do it: on main menu > Project > Project Options ... (Ctrl+Shift+F1) > left panel: Debugging.

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Help with types
« Reply #9 on: October 07, 2015, 10:33:03 am »
Ik you are using Lazarus you can just set a breakpoint (F5) and let the code run to it. Stepping through the code with F8 (over procedure) or F7 (into a procedure) will guide you to your problem area.

If you're not using Lazarus.... why not? It's easier with debugging  :D

 

TinyPortal © 2005-2018