demo07.pas 10 KB

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