demo07.pas 11 KB

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