FontWriter.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include <memory.h>
  5. #include "PongApp.h"
  6. #include "FontWriter.h"
  7. FontWriter::FontWriter( SpriteBatch *batch, unsigned int textID, int xdiv, int ydiv, char *characters ) {
  8. texture = textID;
  9. xdivision = xdiv;
  10. ydivision = ydiv;
  11. //pongApp = app;
  12. spriteBatch = batch;
  13. currentColor[0] = 1.0f;
  14. currentColor[1] = 1.0f;
  15. currentColor[2] = 1.0f;
  16. currentColor[3] = 1.0f;
  17. for (int f=0; f<128; f++) charMap[f].enabled = false;
  18. float xscale = 1.0f / (float)xdiv;
  19. float yscale = 1.0f / (float)ydiv;
  20. int index =0 ;
  21. while (*characters) {
  22. if (*characters>0 && *characters<=128) {
  23. charMap[ *characters ].enabled = true;
  24. int ypos = index / xdiv;
  25. int xpos = index - (ypos*xdiv);
  26. charMap[ *characters ].offsetScale[0] = xscale * (float)xpos;
  27. charMap[ *characters ].offsetScale[1] = yscale * (float)ypos;
  28. charMap[ *characters ].offsetScale[2] = xscale;
  29. charMap[ *characters ].offsetScale[3] = yscale;
  30. };
  31. characters++;
  32. index++;
  33. };
  34. }
  35. FontWriter::~FontWriter() {
  36. }
  37. float FontWriter::getTextWidth( const char *text, float scale ) {
  38. int tlen = strlen( text );
  39. return (float)(tlen-1) * scale * CHAR_EXTEND_MUL;
  40. }
  41. void FontWriter::setColor( float *c ) {
  42. memcpy( currentColor, c, sizeof( float ) * 4 );
  43. }
  44. void FontWriter::setColor( float r, float g, float b, float a ) {
  45. currentColor[0] = r;
  46. currentColor[1] = g;
  47. currentColor[2] = b;
  48. currentColor[3] = a;
  49. }
  50. void FontWriter::writeText( const char *text, float xpos, float ypos, float scale ) {
  51. SpriteDrawInfo sdi;
  52. sdi.setColor( currentColor );
  53. sdi.setScale( 2.0f*scale, 2.0f*scale );
  54. sdi.textureHandle = texture;
  55. SCharacter *ch;
  56. while (*text!=0) {
  57. if (*text>0 && *text<128) {
  58. ch = charMap + (*text);
  59. if (ch->enabled) {
  60. sdi.setSourceRect( ch->offsetScale );
  61. sdi.setTargetPos( xpos, ypos );
  62. spriteBatch->draw(&sdi);
  63. }
  64. xpos+=scale*CHAR_EXTEND_MUL;
  65. }
  66. text++;
  67. };
  68. }