\ VP-globals
\ Global variables and save/restore
\ definitions
\ 
\ $Id: VP-globals.txt,v 1.4 2000/10/25 00:17:43 kris_johnson Exp $

\ Copyright 2000
\ Kristopher D. Johnson
\ 
\ See LICENSE-JacksOrBetter for the
\ conditions under which you may
\ use, redistribute, or modify this 
\ code, or create derived works.

docneeds VP-lib
docneeds VP-resources

.( VP-globals... )

\ Cards go from deuce to Ace
2 constant MinRank
14 constant MaxRank

MaxRank MinRank - 1+
constant #Ranks

4 constant #Suits

\ Number of cards in deck
#Ranks #Suits *
constant #Deck

5 constant #Hand

0 constant PrefGlobals
1 constant PrefStatistics

1 constant v1.0
2 constant v1.1
3 constant v1.2

\ Current version
v1.2 constant AppVersion

\ This structure contains all the
\ data elements that are saved
\ when the program is exited and
\ restored when the program is
\ restarted
struct
  \ Version of app that stored
  \ globals
  1 cells field >version

  \ See GameState in VP-game
  1 cells field >gameState

  \ Player's chips
  1 cells field >chips

  \ Player's bet
  1 cells field >bet

  \ See LastScore in VP-game
  1 cells field >lastScore

  \ See LastPayout in VP-game
  1 cells field >lastPayout

  \ Array of 52 cards
  #Deck cells field >deck

  \ Index of next card to be dealt
  1 cells field >iDeck

  \ Player's hand
  #Hand cells field >hand

  \ Which cards player wants to hold
  #Hand cells field >hold

  \ Future use
  8 cells field >reserved
end-struct Globals

sizeof Globals constant /Globals

Globals g
g /Globals 0 fill

-1 constant noPreferenceFound

variable prefsSize

\ Save global variables for
\ restoration at next run
: save-globals ( -- )
  AppVersion [ g >version ] literal !
  true /Globals g >abs
  AppVersion PrefGlobals AppID
  ( saved? size &buf. vers id creator. )
  PrefSetAppPreferences
;

\ Restore global variable values
\ that were saved by save-globals
: restore-globals ( -- )
  /Globals prefsSize !
  true prefsSize >abs g >abs
  PrefGlobals AppID
  ( saved? &size. &buf. id creator. )
  PrefGetAppPreferences ( n )
  noPreferenceFound = if
    false exit
  then
  prefsSize @ /Globals >=
;

