Recent

Author Topic: Segmentation Fault?  (Read 2871 times)

thenarrator

  • New Member
  • *
  • Posts: 14
Segmentation Fault?
« on: November 12, 2015, 06:22:38 am »
Hi,

Am compiling a program that tests the knowledge of sandwich hands so I want the user to select a target sandwich, build the ingredients that they think goes on it and then have it compared to the target.

Currently it functions as intended up to the point where a cheese is selected but when you select a cheese no cheese is drawn and the program crashes, leaving the error

"line 77: 26327 Segmentation fault: 11  "$EXE_PATH""

Which is one I have not encountered before.
I can see that this must apply to the drawing of the cheese but cannot see why it doesn't work when  CheeseSelect and AddCheese are setup in the same way as SaladSelect and AddTopping? Is the dynamic array creating an infinite loop somewhere?

Code is below

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

EDIT: Forgot to say working on mac with sublime and FPC

EDIT II: Code for DrawBitmap if needed

Code: Pascal  [Select][+][-]
  1. procedure DrawBitmap(src: Bitmap; x: Single; y: Single); overload;
  2.   begin
  3.     sgImages.DrawBitmap(src,x,y);
  4.   end;
« Last Edit: November 12, 2015, 06:27:10 am by thenarrator »

 

TinyPortal © 2005-2018