fonts.pp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. (*
  2. Easy GL2D
  3. Relminator 2011
  4. Richard Eric M. Lope BSN RN
  5. Http://Rel.Phatcode.Net
  6. A very small, simple, yet very fast DS 2D rendering lib using the DS' 3D core.
  7. --
  8. Translated in Object Pascal by Francesco Lombardi - 2012
  9. http://itaprogaming.free.fr
  10. *)
  11. program fonts;
  12. {$L build/font_si.o}
  13. {$L build/font_16x16.o}
  14. {$mode objfpc}
  15. {$H+}
  16. uses
  17. ctypes, nds9, gl2d, uvcoord_font_si, uvcoord_font_16x16;
  18. const
  19. font_16x16BitmapLen = 32768;
  20. font_siBitmapLen = 8192;
  21. font_siPalLen = 512;
  22. var
  23. font_siBitmap: array [0..0] of cuint; cvar; external;
  24. font_16x16Bitmap: array [0..0] of cuint; cvar; external;
  25. font_siPal: array [0..0] of cushort; cvar; external;
  26. type
  27. // Our font class
  28. Tglfont = class
  29. private
  30. font_sprite: pglimage;
  31. str: array [1..256] of cchar;
  32. str2: array [1..256] of cchar;
  33. public
  34. function Load(_font_sprite: pglImage; const numframes: cuint; texcoords: pcuint;
  35. _type: GL_TEXTURE_TYPE_ENUM; sizeX, sizeY, param, pallette_width: cint;
  36. const palette: pcuint16; const texture: pcuint8): cint;
  37. procedure Print(x, y: integer; const text: PAnsiChar); overload;
  38. procedure Print(x, y: integer; value: cint); overload;
  39. procedure PrintCentered(x, y: integer; const text: PAnsiChar); overload;
  40. procedure PrintCentered(x, y: integer; value: cint); overload;
  41. end;
  42. function TglFont.Load(_font_sprite: pglImage; const numframes: cuint; texcoords: pcuint;
  43. _type: GL_TEXTURE_TYPE_ENUM; sizeX, sizeY, param, pallette_width: cint;
  44. const palette: pcuint16; const texture: pcuint8): cint;
  45. var
  46. textureID: cint;
  47. begin
  48. font_sprite := _font_sprite;
  49. textureID := glLoadSpriteSet(font_sprite,
  50. numframes,
  51. texcoords,
  52. _type,
  53. sizeX,
  54. sizeY,
  55. param,
  56. pallette_width,
  57. palette,
  58. texture );
  59. result := textureID;
  60. end;
  61. procedure TglFont.Print(x, y: integer; const text: PAnsiChar);
  62. var
  63. font_char: cuchar;
  64. i: integer;
  65. begin
  66. for i := 0 to length(Text) do
  67. begin
  68. font_char := Ord(text[i]) - 32;
  69. glSprite( x, y, GL_FLIP_NONE, @font_sprite[font_char] );
  70. x := x + font_sprite[font_char].width;
  71. end;
  72. end;
  73. procedure TglFont.Print(x, y: integer; value: cint);
  74. begin
  75. sprintf(@str, '%i', value);
  76. Print(x, y, @str);
  77. end;
  78. procedure TglFont.PrintCentered(x, y: integer; const text: PAnsiChar);
  79. var
  80. font_char: cuchar;
  81. total_width: integer;
  82. o_text: PAnsiChar;
  83. i: integer;
  84. begin
  85. total_width := 0;
  86. o_text := text;
  87. for i := 0 to length(Text) do
  88. begin
  89. font_char := Ord(text[i]) - 32;
  90. total_width := total_width + font_sprite[font_char].width;
  91. end;
  92. x := (SCREEN_WIDTH - total_width) div 2;
  93. // text := o_text;
  94. for i := 0 to length(o_Text) do
  95. begin
  96. font_char := Ord(o_text[i]) - 32;
  97. glSprite( x, y, GL_FLIP_NONE, @font_sprite[font_char] );
  98. x := x + font_sprite[font_char].width;
  99. end;
  100. end;
  101. procedure TglFont.PrintCentered(x, y: integer; value: cint);
  102. begin
  103. sprintf(@str, '%i', value);
  104. PrintCentered(x, y, @str);
  105. end;
  106. var
  107. // This imageset would use our texture packer generated coords so it's kinda
  108. // safe and easy to use
  109. // FONT_SI_NUM_IMAGES is a value #defined from "uvcoord_font_si.h"
  110. FontImages: array [1..FONT_SI_NUM_IMAGES] of glImage;
  111. FontBigImages: array [1..FONT_16X16_NUM_IMAGES] of glimage;
  112. // Our fonts
  113. Font, FontBig: Tglfont;
  114. textureSize: cint;
  115. frame: integer;
  116. x: integer;
  117. opacity: integer;
  118. begin
  119. defaultExceptionHandler();
  120. Font := TglFont.Create;
  121. FontBig := TglFont.Create;
  122. videoSetMode(MODE_5_3D);
  123. consoleDemoInit();
  124. // Initialize GL in 3d mode
  125. glScreen2D();
  126. // Set Bank A to texture (128 kb)
  127. vramSetBankA( VRAM_A_TEXTURE );
  128. vramSetBankE(VRAM_E_TEX_PALETTE); // Allocate VRAM bank for all the palettes
  129. // Load our font texture
  130. // We used glLoadSpriteSet since the texture was made
  131. // with my texture packer.
  132. // no need to save the return value since
  133. // we don't need it at all
  134. Font.Load(@FontImages, // pointer to glImage array
  135. FONT_SI_NUM_IMAGES, // Texture packer auto-generated #define
  136. @font_si_texcoords, // Texture packer auto-generated array
  137. GL_RGB256, // texture type for glTexImage2D() in videoGL.h
  138. TEXTURE_SIZE_64, // sizeX for glTexImage2D() in videoGL.h
  139. TEXTURE_SIZE_128, // sizeY for glTexImage2D() in videoGL.h
  140. GL_TEXTURE_WRAP_S or GL_TEXTURE_WRAP_T or TEXGEN_OFF or GL_TEXTURE_COLOR0_TRANSPARENT, // param for glTexImage2D() in videoGL.h
  141. 256, // Length of the palette (256 colors)
  142. @font_siPal, // Palette Data
  143. @font_siBitmap // image data generated by GRIT
  144. );
  145. // Do the same with our bigger texture
  146. FontBig.Load(@FontBigImages,
  147. FONT_16X16_NUM_IMAGES,
  148. @font_16x16_texcoords,
  149. GL_RGB256,
  150. TEXTURE_SIZE_64,
  151. TEXTURE_SIZE_512,
  152. GL_TEXTURE_WRAP_S or GL_TEXTURE_WRAP_T or TEXGEN_OFF or GL_TEXTURE_COLOR0_TRANSPARENT,
  153. 256,
  154. @font_siPal,
  155. @font_16x16Bitmap
  156. );
  157. iprintf(#$1b'[1;1HEasy GL2D Font Example');
  158. iprintf(#$1b'[3;1HFonts by Adigun A. Polack');
  159. iprintf(#$1b'[6;1HRelminator');
  160. iprintf(#$1b'[7;1HHttp://Rel.Phatcode.Net');
  161. // calculate the amount of
  162. // memory uploaded to VRAM in KB
  163. TextureSize := font_siBitmapLen + font_16x16BitmapLen;
  164. iprintf(#$1b'[10;1HTotal Texture size= %i kb', TextureSize div 1024);
  165. // our ever present frame counter
  166. frame := 0;
  167. while true do
  168. begin
  169. // increment frame counter and rotation offsets
  170. inc(frame);
  171. // set up GL2D for 2d mode
  172. glBegin2D();
  173. // fill the whole screen with a gradient box
  174. glBoxFilledGradient( 0, 0, 255, 191,
  175. RGB15( 31, 0, 0 ),
  176. RGB15( 0, 31, 0 ),
  177. RGB15( 31, 0, 31 ),
  178. RGB15( 0, 31, 31 )
  179. );
  180. // Center print the title
  181. glColor( RGB15(0,0,0) );
  182. FontBig.PrintCentered( 0, 0, 'EASY GL2D' );
  183. glColor( RGB15((frame*6) and 31,(-frame*4) and 31, (frame*2) and 31) );
  184. FontBig.PrintCentered( 0, 20, 'FONT EXAMPLE');
  185. // Fixed-point sinusoidal movement
  186. x := ( sinLerp( frame * 400 ) * 30 ) shr 12;
  187. // Make the fonts sway left and right
  188. // Also change coloring of fonts
  189. glColor( RGB15(31,0,0) );
  190. FontBig.Print( 25 + x, 50, 'hfDEVKITPROfh' );
  191. glColor( RGB15(31,0,31) );
  192. glColor( RGB15(x, 31 - x, x * 2) );
  193. FontBig.Print( 50 - x, 70, 'dcLIBNDScd' );
  194. // change fontsets and print some spam
  195. glColor( RGB15(0,31,31) );
  196. Font.PrintCentered( 0, 100, 'FONTS BY ADIGUN A. POLACK' );
  197. Font.PrintCentered( 0, 120, 'CODE BY RELMINATOR' );
  198. // Restore normal coloring
  199. glColor( RGB15(31,31,31) );
  200. // Change opacity relative to frame
  201. opacity := abs( sinLerp( frame * 245 ) * 30 ) shr 12;
  202. // translucent mode
  203. // Add 1 to opacity since at 0 we will get into wireframe mode
  204. glPolyFmt(POLY_ALPHA(1 + opacity) or POLY_CULL_NONE or POLY_ID(1));
  205. FontBig.Print( 35 + x, 140, 'ANYA THERESE' );
  206. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_ID(2));
  207. // Print the number of frames
  208. Font.Print( 10, 170, 'FRAMES = ');
  209. Font.Print( 10 + 72, 170, frame );
  210. glEnd2D();
  211. glFlush(0);
  212. swiWaitForVBlank();
  213. end;
  214. Font.free;
  215. FontBig.free;
  216. end.