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.
pointer new_HISCORE(int n)pointer load_HISCORE(string name)pointer load_HISCOREplus(string name, pointer cfg, int n)pointer load_HISCOREdbOpen()delete_HISCORE(pointer p)save_HISCORE(pointer p, string name)save_HISCOREplus(pointer p, string name, pointer cfg, int n)save_HISCOREdbOpen(pointer p)string getscore_HISCORE(pointer hiscore, int offset)string getname_HISCORE(pointer hiscore,int offset)int insert_HISCORE(pointer hiscore, int score, pointer name)
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 |
|
| [ source ] |