demo07.pas 12 KB

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