demo07.pas 11 KB

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