hiscorelib.h

HISCORELIB is a piece of reusable source code for PocketC on the PalmOS, designed to make the common function of providing a high-score table for a game as easy as possible. The source code can be found at the end of this document.

CREATING AND DESTROYING A HIGH SCORE TABLE

pointer new_HISCORE(int n)

Returns a brand spanking new hiscore table that is big enough to hold 'n' records. All the names are set to "noname" and all the scores are set to zero.

pointer load_HISCORE(string name)

Loads & returns a new highscore table from a named database.

pointer load_HISCOREplus(string name, pointer cfg, int n)

Loads & returns a new highscore table from a named database, plus an array of "n" integers into cfg.

pointer load_HISCOREdbOpen()

Loads & returns a new highscore table from an open database.

delete_HISCORE(pointer p)

Deletes (frees) a highscore table created with new_HISCORE, or one of the load_ functions.

SAVING A HIGH SCORE TABLE

save_HISCORE(pointer p, string name)

Saves a highscore table to a named database.

save_HISCOREplus(pointer p, string name, pointer cfg, int n)

Saves & returns a new highscore table to a named database, plus an array of "n" integers from cfg.

save_HISCOREdbOpen(pointer p)

Saves a highscore table to an open database.

READING & WRITING SCORES TO & FROM A HIGH SCORE TABLE

string getscore_HISCORE(pointer hiscore, int offset)

Returns the SCORE at position "offset" in the hiscore table.

string getname_HISCORE(pointer hiscore,int offset)

Returns the NAME at position "offset" in the hiscore table.

int insert_HISCORE(pointer hiscore, int score, pointer name)

Inserts a name & score into the table, returns non zero if the score was high enough to get in.

Source Code

Cut-n-paste into a memo, and #include "hiscorelib.h" in your code before the first time you use it. Requires sort.h, which can be downloaded separately.

/$hiscorelib.h
//(c) T.Frogley 2001
//codemonkey_uk@hotmail.com

#ifndef HISCORE_H
#define HISCORE_H

#define HISCORE_NAME 0
#define HISCORE_SCORE 1
#define SIZEOF_HISCORE 2

#include "sort.h"

//returns a brand spanking new hiscore table
//that is big enough to hold 'n' records
pointer new_HISCORE(int n)
{
  pointer result,p;
  int i;
  if (n < 1) return 0;
  result = malloc(SIZEOF_HISCORE*n+1);
  if (result){
    p=result;
    settype(p,1,'i');
    *p++ = n;
    for(i=0;i < n;i++){
      settype(p,1,'i');
      *p++ = 0;
      settype(p,1,'s');
      *p++ = "noname";
    }
  }
  return result;
}

delete_HISCORE(pointer p)
{
  free(p);
}

string getscore_HISCORE(pointer hiscore,int offset)
{
  int result = 0;
  if (offset < *hiscore){
    result = hiscore[1+offset*2];
  }
  return result;
}

string getname_HISCORE(pointer hiscore,int offset)
{
  string result = "noname";
  if (offset < *hiscore){
    result = hiscore[2+offset*2];
  }
  return result;
}

int insert_HISCORE(pointer hiscore, int score, pointer name)
{
  int n,o,result=0;
  n=*hiscore;
  o=(n-1)*2;
  hiscore++;
  if (hiscore[o] < score){
    hiscore[o]=score;
    hiscore[o+1]=(*name)();
    sort(hiscore,n,2,compare);
    result = 1;
  }
  return result;
}

pointer load_HISCOREdbOpen()
{
  int i;
  pointer result,p;
  i=dbread('i');
  result=new_HISCORE(i);
  if (result){
    p=result+1;
    while (i--){
      *p = dbread('i'); p++;
      *p = dbread('s'); p++;
    }
  }
  return result;
}

pointer load_HISCORE(string name)
{  
  pointer result;
  if (!dbopen(name)) return 0;
  result = load_HISCOREdbOpen();
  dbclose();
  return result;
}

pointer load_HISCOREplus(string name, pointer cfg, int n)
{  
  pointer result;
  if (!dbopen(name)) return 0;
  result = load_HISCOREdbOpen();
  while(n-- && dbpos()!=-1){    
    *cfg = dbread('i'); cfg++;
  }
  dbclose();
  return result;
}

int save_HISCOREdbOpen(pointer p)
{
  int i;
  i=*p; p++;
  dbwrite(i);
  while (i--){
    dbwrite(*p); p++;
    dbwrite(*p); p++;
  }
}

save_HISCORE(pointer p, string name)
{
  if (!dbcreate(name)) 
    return 0;
  save_HISCOREdbOpen(p);
  dbbackup(1);
  dbclose();
}

save_HISCOREplus(pointer p, string name, pointer cfg, int n)
{
  if (!dbcreate(name)) 
    return 0;
  save_HISCOREdbOpen(p);
  while(n--){    
    dbwrite(*cfg); cfg++;
  }
  dbbackup(1);
  dbclose();
}

#endif//HISCORE_H

Thad's Homepage (c) Wednesday 17th May 2006 T.Frogley
With thanks to notagoth.org
Valid HTML 4.01!
[ source ]