{$mode objfpc}{$modeswitch objectivec1}unit SnakeScore;interfaceuses	ctypes, MacOSAll, CocoaAll, TransSkel5, SkelViewUnit, QDCG,	SkelTextView, BaseUnix, SysUtils;type	ScoreRec = record		score: Longint;		name: ShortString;	end;var	highscore: array[1..20] of ScoreRec;const	kHighScoreName = 'highscores.txt';procedure InitScore;procedure WriteHighscores(fileName: String);procedure ReadHighscores(fileName: String);procedure EnterHighscore(score: Longint);procedure CreateScoreWindow;implementationprocedure Home;var	mainBundle: CFBundleRef;	resourcesURL: CFURLRef;	path: AnsiString;	success: Boolean;begin	mainBundle := CFBundleGetMainBundle();	resourcesURL := CFBundleCopyResourcesDirectoryURL(mainBundle);	SetLength(path, PATH_MAX);	success := CFURLGetFileSystemRepresentation(resourcesURL, TRUE, PChar(path), PATH_MAX);	CFRelease(resourcesURL);	if success then		chdir(path);	WriteLn('Current Path: ', path);end;procedure WriteHighscores(fileName: String);var	F : text;	i: Longint;begin	Home;	assign(F, fileName);			{ associate it }	rewrite(F);						{ open it	}	for i := Low(highscore) to High(highscore) do	begin		WriteLn(F, highscore[i].name);		WriteLn(F, highscore[i].score);	end;	close(F);						{ close it }end;procedure ReadHighscores(fileName: String);var	F : text;	i: Longint;begin	Home;	assign(F, fileName);		{ associate it }{$I-}	reset(F);                   { open it without breaking for error }{$I+}	if IOResult = 0 then	begin		i := 0;		for i := Low(highscore) to High(highscore) do		begin			ReadLn(F, highscore[i].name);			ReadLn(F, highscore[i].score);		end;		close(F);						{ close it }	end	else	begin // No file - zero		for i := Low(highscore) to High(highscore) do		begin			highscore[i].name := '';			highscore[i].score := 0;		end;	end;end;	procedure ModalDialog(w: NSWindow);	var		app     : NSApplication;		session : NSModalSession;	begin		app := NSApplication.sharedApplication;		session := app.beginModalSessionForWindow(w);		w.setReleasedWhenClosed(False);		while w.isVisible do		begin			app.runModalSession(session);			Sleep(100);		//To avoid tight loop that eats CPU time.		end;		w.setReleasedWhenClosed(True);		app.endModalSession(session);	end;	var		closeBtn: NSButton;		tf: NSTextField;		gScore: Longint;		gScoreWindow: NSWindow;procedure SkelShowWindow(w: NSWindow);var	wc: NSWindowController;begin	wc := NSWindowController.alloc;	wc.initWithWindow(w);	if wc <> nil then		wc.showWindow(nil);	wc.release;end;procedure HitCloseButton(s: PChar);var	i: Longint;begin	// Get name from tf	// Enter in high score list		if AnsiString(s) = 'OK' then	begin		WriteLn('OK!');		i := High(highscore);		while (highscore[i].score < gScore) and (i >= 1) do		begin			if i > 1 then				highscore[i] := highscore[i-1];			i := i - 1;		end;		highscore[i+1].score := gScore;		highscore[i+1].name := SkelGetStringValue(tf);				if gScoreWindow = nil then			CreateScoreWindow		else		begin			SkelInvalWindow(gScoreWindow);			SkelBringToFront(gScoreWindow);		end;		WriteHighscores(kHighScoreName);	end	else		WriteLn(s^, 'Is not OK');		closeBtn.window.close;end;procedure EnterHighscore(score: Longint);var	w: NSWindow;begin	gScore := score;	if score > highscore[High(highscore)].score then	begin		w := SkelNewWindow(100, 300, 240, 120, 'New high score!', true);		w.center;				tf := SkelNewTextField(GetWindowContentView(w), 20, 20,150,30, 'Player name');		closeBtn := SkelNewButton(GetWindowContentView(w), 'OK',			120, 70, 80, 25, 'OK', @HitCloseButton);		closeBtn := SkelNewButton(GetWindowContentView(w), 'Cancel',			30, 70, 80, 25, 'Cancel', @HitCloseButton);				ModalDialog(w);		w.close;	end;end;procedure DrawScores(theView: NSView; cgContext: CGContextRef; viewRect: CGRect; userData: Pointer);var	i: Longint;	s: String;begin	UseViewContext(cgContext, theView.bounds.size.height);	MoveTo(0,0);	LineTo(viewRect.size.width,0);	MoveTo(0,18);	LineTo(viewRect.size.width,18);	MoveTo(30, 15);	DrawString('Name');	MoveTo(200, 15);	DrawString('Score');	for i := Low(highscore) to High(highscore) do	begin		MoveTo(10, (i-2)*15+50);		Str(i, s);		DrawString(s);		MoveTo(30, (i-2)*15+50);		DrawString(highscore[i].name);		MoveTo(200, (i-2)*15+50);		Str(highscore[i].score, s);		DrawString(s);	end;end;function CloseWindow(theWindow: NSWindow; userData: Pointer): Longint; {1 = OK, 0 = later, -1 = cancel}begin	WriteLn('close score');	result := 1; // Do close	gScoreWindow := nil;end;procedure InitScore;begin	ReadHighscores(kHighScoreName);end;procedure CreateScoreWindow;var	sv: SkelView;begin	WriteLn('Create score');		gScoreWindow := SkelNewWindow(100, 300, 240, 340, 'TransSnake high scores', true);	SkelSetWindowHandler(gScoreWindow, nil, nil,  @DrawScores, @CloseWindow, nil, nil);end;end.
