demo07.lpr 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. library demo07;
  2. {$I zglCustomConfig.cfg}
  3. {$I zgl_config.cfg}
  4. uses
  5. zgl_application,
  6. zgl_file,
  7. zgl_screen,
  8. zgl_window,
  9. zgl_timers,
  10. zgl_camera_2d,
  11. zgl_render_2d,
  12. zgl_fx,
  13. zgl_textures,
  14. zgl_textures_png,
  15. zgl_textures_jpg,
  16. zgl_sprite_2d,
  17. zgl_primitives_2d,
  18. zgl_font,
  19. zgl_text,
  20. zgl_types,
  21. zgl_log,
  22. gegl_color,
  23. zgl_utils
  24. ;
  25. type
  26. TTux = record
  27. Texture : zglPTexture;
  28. Frame : Integer; // указатель на "подтекстуру"
  29. Pos : zglTPoint2D;
  30. end;
  31. var
  32. dirRes : UTF8String = 'assets/';
  33. fntMain : LongWord;
  34. texLogo : zglPTexture;
  35. texBack : zglPTexture;
  36. texGround : zglPTexture;
  37. texTuxWalk : zglPTexture;
  38. texTuxStand : zglPTexture;
  39. tux : array[ 0..20 ] of TTux;
  40. time : Integer;
  41. camMain : zglTCamera2D;
  42. newColor : LongWord;
  43. correctColor: LongWord;
  44. TimeStart: LongWord;
  45. procedure Init;
  46. var
  47. i : Integer;
  48. begin
  49. zgl_Enable( CORRECT_RESOLUTION );
  50. scr_CorrectResolution( 800, 600 );
  51. // RU: Т.к. по умолчанию вся структура камеры заполняется нулями, следует инициализировать её стандартными значениями.
  52. // EN: Camera must be initialized, because camera structure is zero-filled by default.
  53. cam2d_DefInit( camMain );
  54. file_OpenArchive( PAnsiChar( zgl_Get( DIRECTORY_APPLICATION ) ) );
  55. // RU: Загружаем текстуру.
  56. // $FF000000 - указывает на то, что бы использовать альфа-канал из изображения.
  57. // TEX_DEFAULT_2D - комплекс флагов, необходимых для 2D-спрайтов. Описание есть в справке.
  58. // EN: Load the texture.
  59. // $FF000000 - means that alpha channel must be used from file, without colorkey.
  60. // TEX_DEFAULT_2D - complex of flags that needed for 2D sprites. Description can be found in help.
  61. texLogo := tex_LoadFromFile( dirRes + 'zengl.png', $FF000000, TEX_DEFAULT_2D );
  62. texBack := tex_LoadFromFile( dirRes + 'back01.jpg' );
  63. texGround := tex_LoadFromFile( dirRes + 'ground.png' );
  64. // RU: Указываем размер кадра в текстуре.
  65. // EN: Set the size of single frame for texture.
  66. tex_SetFrameSize( texGround, 32, 32 );
  67. texTuxWalk := tex_LoadFromFile( dirRes + 'tux_walking.png' );
  68. tex_SetFrameSize( texTuxWalk, 64, 64 );
  69. texTuxStand := tex_LoadFromFile( dirRes + 'tux_stand.png' );
  70. tex_SetFrameSize( texTuxStand, 64, 64 );
  71. for i := 0 to 9 do
  72. begin
  73. tux[ i ].Texture := texTuxWalk;
  74. tux[ i ].Frame := random( 19 ) + 2;
  75. tux[ i ].Pos.X := i * 96;
  76. tux[ i ].Pos.Y := 32;
  77. end;
  78. for i := 10 to 19 do
  79. begin
  80. tux[ i ].Texture := texTuxWalk;
  81. tux[ i ].Frame := random( 19 ) + 2;
  82. tux[ i ].Pos.X := ( i - 9 ) * 96;
  83. tux[ i ].Pos.Y := 600 - 96;
  84. end;
  85. tux[ 20 ].Texture := texTuxStand;
  86. tux[ 20 ].Frame := random( 19 ) + 2;
  87. tux[ 20 ].Pos.X := 400 - 32;
  88. tux[ 20 ].Pos.Y := 300 - 64 - 4;
  89. // RU: Загружаем шрифт.
  90. // EN: Load the font.
  91. fntMain := font_LoadFromFile( dirRes + 'font.zfi' );
  92. file_CloseArchive();
  93. setFontTextScale(20, fntMain);
  94. // Rus: задаём новый цвет. Это чёрный и немного прозрачный.
  95. // Eng: set a new color. It is black and slightly transparent.
  96. newColor := Color_FindOrAdd(200);
  97. // Rus: задаём новый цвет, без проверки на существование. Это чёрный не прозрачный.
  98. // Eng: we set a new color, without checking for existence. It's black and not transparent.
  99. correctColor := Color_UAdd(255);
  100. end;
  101. procedure Draw;
  102. var
  103. i : Integer;
  104. t : Single;
  105. ScaleF: LongWord;
  106. begin
  107. ScaleF := 20; // условно указываем размеры фонта, но можно использовать -> ScaleF := getTextScale(fntMain)
  108. batch2d_Begin();
  109. if time > 255 Then
  110. begin
  111. // RU: Рисуем задний фон с размерами 800х600 используя текстуру back.
  112. // EN: Render the background with size 800x600 and using texture "back".
  113. ssprite2d_Draw( texBack, 0, 0, 800, 600, 0 );
  114. // RU: Установить текущую камеру.
  115. // EN: Set the current camera.
  116. cam2d_Set( @camMain );
  117. // RU: Рисуем землю.
  118. // EN: Render the ground.
  119. for i := -2 to 800 div 32 + 1 do
  120. asprite2d_Draw( texGround, i * 32, 96 - 12, 32, 32, 0, 2 );
  121. for i := -2 to 800 div 32 + 1 do
  122. asprite2d_Draw( texGround, i * 32, 600 - 32 - 12, 32, 32, 0, 2 );
  123. // RU: Рисуем шагающих пингвинов.
  124. // EN: Render penguins
  125. for i := 0 to 9 do
  126. if i = 2 Then
  127. begin
  128. // RU: Рисуем надпись в "рамочке" над пингвином.
  129. // EN: Render the text in frame over penguins.
  130. t := text_GetWidth( fntMain, 'I''m so red...' ) * 0.75;
  131. pr2d_Rect( tux[ i ].Pos.X - 2, tux[ i ].Pos.Y - ScaleF + 4, t, ScaleF, newColor, PR2D_FILL );
  132. pr2d_Rect( tux[ i ].Pos.X - 2, tux[ i ].Pos.Y - ScaleF + 3, t + 2, ScaleF + 2, cl_White );
  133. text_DrawEx( fntMain, tux[ i ].Pos.X, tux[ i ].Pos.Y - ScaleF + 5, 1, 0, 'I''m so red...' );
  134. // RU: Рисуем красного пингвина используя fx2d-функцию и флаг FX_COLOR.
  135. // EN: Render red penguin using fx2d-function and flag FX_COLOR.
  136. fx2d_SetColor( $FF0000 );
  137. asprite2d_Draw( tux[ i ].Texture, tux[ i ].Pos.X, tux[ i ].Pos.Y, 64, 64, 0, tux[ i ].Frame div 2, 255, FX_BLEND or FX_COLOR );
  138. end else
  139. if i = 7 Then
  140. begin
  141. t := text_GetWidth( fntMain, '???' )+ 4;
  142. pr2d_Rect( tux[ i ].Pos.X + 32 - t / 2, tux[ i ].Pos.Y - ScaleF + 4, t, ScaleF, newColor, PR2D_FILL );
  143. pr2d_Rect( tux[ i ].Pos.X + 32 - t / 2 - 1, tux[ i ].Pos.Y - ScaleF + 3, t + 2, ScaleF + 2, cl_White );
  144. text_DrawEx( fntMain, tux[ i ].Pos.X + 32, tux[ i ].Pos.Y - ScaleF + 5, 1, 0, '???', cl_White, TEXT_HALIGN_CENTER );
  145. // RU: Рисуем пингвина приведение используя флаг FX_COLOR установив режим в FX_COLOR_SET :)
  146. // EN: Render penguin ghost using flag FX_COLOR and mode FX_COLOR_SET :)
  147. fx_SetColorMode( FX_COLOR_SET );
  148. fx2d_SetColor( $FFFFFF );
  149. asprite2d_Draw( tux[ i ].Texture, tux[ i ].Pos.X, tux[ i ].Pos.Y, 64, 64, 0, tux[ i ].Frame div 2, 155, FX_BLEND or FX_COLOR );
  150. // RU: Возвращаем обычный режим.
  151. // EN: Return default mode.
  152. fx_SetColorMode( FX_COLOR_MIX );
  153. end else
  154. asprite2d_Draw( tux[ i ].Texture, tux[ i ].Pos.X, tux[ i ].Pos.Y, 64, 64, 0, tux[ i ].Frame div 2 );
  155. // RU: Рисуем пингвинов шагающих в обратную сторону используя флаг отражения текстуры FX2D_FLIPX.
  156. // EN: Render penguins, that go another way using special flag for flipping texture - FX2D_FLIPX.
  157. for i := 10 to 19 do
  158. if i = 13 Then
  159. begin
  160. t := text_GetWidth( fntMain, 'I''m so big...' ) * 0.75;
  161. pr2d_Rect( tux[ i ].Pos.X - 2, tux[ i ].Pos.Y - ScaleF - 10, t, ScaleF, newColor, PR2D_FILL );
  162. pr2d_Rect( tux[ i ].Pos.X - 2, tux[ i ].Pos.Y - ScaleF - 11, t + 2, ScaleF + 2, cl_White );
  163. text_DrawEx( fntMain, tux[ i ].Pos.X, tux[ i ].Pos.Y - ScaleF - 9, 1, 0, 'I''m so big...' );
  164. // RU: Рисуем "большего" пингвина. Т.к. FX2D_SCALE увеличивает спрайт относительно центра, то пингвина следует немного "поднять".
  165. // EN: Render "big" penguin. It must be shifted up, because FX2D_SCALE scale sprite relative to the center.
  166. fx2d_SetScale( 1.25, 1.25 );
  167. asprite2d_Draw( tux[ i ].Texture, tux[ i ].Pos.X, tux[ i ].Pos.Y - 8, 64, 64, 0, tux[ i ].Frame div 2, 255, FX_BLEND or FX2D_FLIPX or FX2D_SCALE );
  168. end else
  169. if i = 17 Then
  170. begin
  171. // RU: Рисуем "высокого" пингвина используя вместо флага FX2D_SCALE флаг FX2D_VCHANGE и функцию fx2d_SetVertexes для смещения координат двух верхних вершин спрайта.
  172. // EN: Render "tall" penguin using flag FX2D_VCHANGE instead of FX2D_SCALE, and function fx2d_SetVertexes for shifting upper vertexes of sprite.
  173. fx2d_SetVertexes( 0, -16, {0, -16, 0, 0,} 0, 0 );
  174. asprite2d_Draw( tux[ i ].Texture, tux[ i ].Pos.X, tux[ i ].Pos.Y, 64, 64, 0, tux[ i ].Frame div 2, 255, FX_BLEND or FX2D_FLIPX or FX2D_VCHANGE );
  175. end else
  176. asprite2d_Draw( tux[ i ].Texture, tux[ i ].Pos.X, tux[ i ].Pos.Y, 64, 64, 0, tux[ i ].Frame div 2, 255, FX_BLEND or FX2D_FLIPX );
  177. // RU: Сбросить камеру.
  178. // EN: Reset the camera.
  179. cam2d_Set( nil );
  180. // RU: Рисуем участок земли по центру экрана.
  181. // EN: Render piece of ground in the center of screen.
  182. asprite2d_Draw( texGround, 11 * 32, 300 - 16, 32, 32, 0, 1 );
  183. asprite2d_Draw( texGround, 12 * 32, 300 - 16, 32, 32, 0, 2 );
  184. asprite2d_Draw( texGround, 13 * 32, 300 - 16, 32, 32, 0, 3 );
  185. t := text_GetWidth( fntMain, 'o_O' ) * 0.75;
  186. pr2d_Rect( tux[ 20 ].Pos.X + 32 - t / 2, tux[ 20 ].Pos.Y - ScaleF + 3, t + 2, ScaleF + 2, newColor, PR2D_FILL );
  187. pr2d_Rect( tux[ 20 ].Pos.X + 32 - t / 2, tux[ 20 ].Pos.Y - ScaleF + 2, t + 4, ScaleF + 4, cl_White );
  188. text_DrawEx( fntMain, tux[ 20 ].Pos.X + 32, tux[ 20 ].Pos.Y - ScaleF + 5, 1, 0, 'o_O', cl_White, TEXT_HALIGN_CENTER );
  189. asprite2d_Draw( tux[ 20 ].Texture, tux[ 20 ].Pos.X, tux[ 20 ].Pos.Y, 64, 64, 0, tux[ 20 ].Frame div 2 );
  190. end;
  191. if time <= 255 Then
  192. ssprite2d_Draw( texLogo, 400 - 256, 300 - 128, 512, 256, 0, time )
  193. else
  194. if time < 510 Then
  195. begin
  196. // Rus: получаем значение цвета.
  197. // Eng: Get the color value.
  198. i := Get_Color(correctColor);
  199. pr2d_Rect( 0, 0, 800, 600, correctColor, PR2D_FILL );
  200. dec(i);
  201. if i < 0 then
  202. i := 0;
  203. // Rus: корректируем значение цвета.
  204. // Eng: adjusting the color value.
  205. Correct_Color(correctColor, i);
  206. ssprite2d_Draw( texLogo, 400 - 256, 300 - 128, 512, 256, 0, 510 - time );
  207. end;
  208. if time > 255 Then
  209. text_Draw( fntMain, 0, 0, 'FPS: ' + u_IntToStr( zgl_Get( RENDER_FPS ) ) );
  210. batch2d_End();
  211. end;
  212. procedure Timer;
  213. var
  214. i : Integer;
  215. begin
  216. INC( time, 2 );
  217. camMain.Angle := camMain.Angle + cos( time / 1000 ) / 10;
  218. for i := 0 to 20 do
  219. begin
  220. INC( tux[ i ].Frame );
  221. if tux[ i ].Frame > 20 Then
  222. tux[ i ].Frame := 2;
  223. end;
  224. for i := 0 to 9 do
  225. begin
  226. tux[ i ].Pos.X := tux[ i ].Pos.X + 1.5;
  227. if tux[ i ].Pos.X > 864 Then
  228. tux[ i ].Pos.X := -96;
  229. end;
  230. for i := 10 to 19 do
  231. begin
  232. tux[ i ].Pos.X := tux[ i ].Pos.X - 1.5;
  233. if tux[ i ].Pos.X < -96 Then
  234. tux[ i ].Pos.X := 864;
  235. end;
  236. end;
  237. procedure Restore;
  238. begin
  239. file_OpenArchive( PAnsiChar( zgl_Get( DIRECTORY_APPLICATION ) ) );
  240. tex_RestoreFromFile( texLogo, dirRes + 'zengl.png' );
  241. tex_RestoreFromFile( texBack, dirRes + 'back01.jpg' );
  242. tex_RestoreFromFile( texGround, dirRes + 'ground.png' );
  243. tex_RestoreFromFile( texTuxWalk, dirRes + 'tux_walking.png' );
  244. tex_RestoreFromFile( texTuxStand, dirRes + 'tux_stand.png' );
  245. font_RestoreFromFile( fntMain, dirRes + 'font.zfi' );
  246. file_CloseArchive();
  247. end;
  248. procedure Java_zengl_android_ZenGL_Main( var env; var thiz ); cdecl;
  249. begin
  250. randomize();
  251. TimeStart := timer_Add( @Timer, 16, t_Start );
  252. zgl_Reg( SYS_LOAD, @Init );
  253. zgl_Reg( SYS_DRAW, @Draw );
  254. zgl_Reg( SYS_ANDROID_RESTORE, @Restore );
  255. scr_SetOptions();
  256. end;
  257. exports
  258. Java_zengl_android_ZenGL_Main,
  259. {$I android_export.inc}
  260. End.