PongHighScores.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include <math.h>
  5. #include <memory.h>
  6. #include <stdlib.h>
  7. #include "PongHighScores.h"
  8. #include "PongApp.h"
  9. PongHighScores::PongHighScores( PongApp *app, unsigned int new_score_entry ) {
  10. // initialize new entry
  11. pongApp = app;
  12. createDisplayGrid();
  13. fade = 0;
  14. visible = true;
  15. angle = 0.0f;
  16. textInputEnabled = false;
  17. char fileOut[1024];
  18. // Load the highscore data
  19. FILE *file = fopen(pongApp->getGameEngine()->adjustPath("configdata:qtepong.dat", fileOut, 1024), "rb");
  20. //FILE *file = 0;
  21. if (file) {
  22. fread( scoreTags, sizeof(char),SCORE_TAG_CHARACTERS*NOF_HIGH_SCORES, file );
  23. fread( scores, sizeof (unsigned int), NOF_HIGH_SCORES, file );
  24. fclose(file);
  25. } else {
  26. for (int f=0; f<NOF_HIGH_SCORES; f++) {
  27. sprintf( scoreTags[f], "beatme%d", f+1 );
  28. scores[f] = 20+f*f*300;
  29. };
  30. };
  31. orderScores(-1);
  32. // For debug purposes,.. override highscores
  33. //new_score_entry = scores[0]+100;
  34. if (new_score_entry>0 && new_score_entry>scores[0]) {
  35. scores[0] = new_score_entry;
  36. #ifndef USE_VKB
  37. app->getGameEngine()->queryTextInput("Enter Your Name", scoreTags[0], SCORE_TAG_CHARACTERS );
  38. #else
  39. app->getEngine()->sendEvent( GF::VIRTUAL_KEYBOARD, 1);
  40. strcpy( scoreTags[0], "");
  41. textInputEnabled = true;
  42. #endif
  43. // PREPROCESS QUERIED TEXT.
  44. for (int f=0; f<SCORE_TAG_CHARACTERS; f++) {
  45. // drop to low-case
  46. if (scoreTags[0][f]>='A' && scoreTags[0][f]<='Z') scoreTags[0][f] = scoreTags[0][f] - 'A' + 'a';
  47. };
  48. newScorePos = 0;
  49. } else newScorePos = -1; // DONT TRACK IT.
  50. newScorePos = orderScores(newScorePos);
  51. buildScreen();
  52. }
  53. void PongHighScores::buildScreen()
  54. {
  55. char testr[64];
  56. // build the screen.
  57. for (int f=0; f<NOF_HIGH_SCORES; f++) {
  58. memset( displayGrid[f], '.', SCORE_DISPLAY_WIDTH );
  59. char *name = scoreTags[f];
  60. if (name[0]==0) name = "ERROR";
  61. memcpy( displayGrid[f], name, strlen(name));
  62. sprintf( testr, "%d", scores[f]);
  63. int l = strlen( testr );
  64. memcpy( displayGrid[f]+SCORE_DISPLAY_WIDTH-l,testr, l);
  65. }
  66. }
  67. int PongHighScores::orderScores(int followPos) {
  68. char switchTemp[64];
  69. unsigned int i;
  70. for (int f=0; f<NOF_HIGH_SCORES-1; f++)
  71. for (int g=f+1; g<NOF_HIGH_SCORES; g++) {
  72. if (scores[f]>scores[g]) {
  73. if (f == followPos) followPos = g;
  74. else if (g==followPos) followPos = f;
  75. i = scores[f];
  76. memcpy( switchTemp, scoreTags[f], SCORE_TAG_CHARACTERS );
  77. scores[f] = scores[g];
  78. memcpy( scoreTags[f], scoreTags[g], SCORE_TAG_CHARACTERS );
  79. scores[g] = i;
  80. memcpy( scoreTags[g], switchTemp, SCORE_TAG_CHARACTERS );
  81. };
  82. }
  83. return followPos;
  84. }
  85. PongHighScores::~PongHighScores() {
  86. char fileOut[1024];
  87. // save highscores
  88. FILE *file = fopen(pongApp->getGameEngine()->adjustPath("configdata:qtepong.dat", fileOut, 1024), "wb");
  89. if (file) {
  90. fwrite( scoreTags, sizeof(char),SCORE_TAG_CHARACTERS*NOF_HIGH_SCORES, file );
  91. fwrite( scores, sizeof (unsigned int), NOF_HIGH_SCORES, file );
  92. fclose(file);
  93. };
  94. }
  95. bool PongHighScores::enterCharacter( unsigned char character )
  96. {
  97. #ifndef USE_VKB
  98. return true;
  99. #endif
  100. if (!textInputEnabled) {
  101. pongApp->getEngine()->sendEvent( GF::VIRTUAL_KEYBOARD, 0);
  102. return false;
  103. }
  104. if (newScorePos<0) return false; // no new score requires a name.
  105. int curlength = strlen( scoreTags[ newScorePos ] );
  106. if (curlength<SCORE_TAG_CHARACTERS-1) {
  107. if (character == 13 || character == 10) {
  108. textInputEnabled = false;
  109. pongApp->getEngine()->sendEvent( GF::VIRTUAL_KEYBOARD, 0);
  110. return false;
  111. }
  112. else if(character==8 || character==127 && curlength>0) // backspace or delete
  113. {
  114. scoreTags[newScorePos][ curlength-1 ] = 0;
  115. buildScreen();
  116. }
  117. else // Enter new character into the string and rebuild the screen.
  118. {
  119. scoreTags[newScorePos][ curlength ] = character;
  120. scoreTags[newScorePos][ curlength+1 ] = 0;
  121. buildScreen();
  122. }
  123. }
  124. }
  125. void PongHighScores::createDisplayGrid() {
  126. for (int y=0; y<SCORE_DISPLAY_HEIGHT; y++) {
  127. for (int x=0; x<SCORE_DISPLAY_WIDTH; x++) {
  128. displayGrid[y][x] = 'a' + x;
  129. displayGridRandom[y][x] = (rand()&255);
  130. }
  131. }
  132. }
  133. int PongHighScores::update( const float frameTime ) {
  134. if (visible)
  135. fade += frameTime;
  136. else {
  137. if (textInputEnabled) {
  138. pongApp->getEngine()->sendEvent( GF::VIRTUAL_KEYBOARD, 0);
  139. textInputEnabled = false;
  140. }
  141. fade-=frameTime;
  142. }
  143. angle += frameTime;
  144. if (fade>2.0f) fade = 2.0f;
  145. if (!visible && fade<=0.0f) {
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. void PongHighScores::render() {
  151. FontWriter *w = pongApp->getFontWriter();
  152. char te[2];
  153. te[1] =0;
  154. float lsize = 0.08;
  155. float v = fade * ( 0.499f*cosf( angle*14.0f ) + 0.5f);
  156. for (int y=0; y<SCORE_DISPLAY_HEIGHT; y++) {
  157. if (y==newScorePos) {
  158. te[0]='#';
  159. w->setColor( v, v, 1.0f, v );
  160. w->writeText( te,
  161. -((SCORE_DISPLAY_WIDTH+1)*lsize/2.0f),
  162. -((SCORE_DISPLAY_HEIGHT-1)*lsize/2.0f) +(float)y*lsize,
  163. (lsize)/2.0f );
  164. w->writeText( te,
  165. +((SCORE_DISPLAY_WIDTH+1)*lsize/2.0f),
  166. -((SCORE_DISPLAY_HEIGHT-1)*lsize/2.0f) +(float)y*lsize,
  167. (lsize)/2.0f );
  168. };
  169. for (int x=0; x<SCORE_DISPLAY_WIDTH; x++) {
  170. te[0] = displayGrid[y][x];
  171. v = (float)displayGridRandom[y][x] / 255.0f;
  172. v = (fade-v)*2.0f;
  173. v-=(rand()&255)/1024.0f;
  174. if (v>1.0) v = 1.0f;
  175. if (v<0.0f) v = 0.0f;
  176. w->setColor( v, v, 1.0f, v );
  177. w->writeText( te,
  178. -((SCORE_DISPLAY_WIDTH-1)*lsize/2.0f) + (float)x * lsize,
  179. -((SCORE_DISPLAY_HEIGHT-1)*lsize/2.0f) +(float)y*lsize,
  180. (lsize)/2.0f );
  181. }
  182. }
  183. // Draw string under edit
  184. if (textInputEnabled) {
  185. v = fade * ( 0.499f*cosf( angle*14.0f ) + 0.5f);
  186. w->writeText(scoreTags[newScorePos],-((SCORE_DISPLAY_WIDTH-1)*lsize/2.0f), pongApp->getTop()-lsize/2.0f, lsize/2.0f );
  187. // cursor
  188. int l = strlen( scoreTags[ newScorePos ]);
  189. w->setColor( v, v, 1.0f, v );
  190. te[0]='#';
  191. w->writeText( te,
  192. -((SCORE_DISPLAY_WIDTH+1)*lsize/2.0f) + lsize*(l+1)*CHAR_EXTEND_MUL/2.0f + lsize/2.0f,
  193. pongApp->getTop()-lsize/2.0f,(lsize)/2.0f );
  194. }
  195. }