demo07.lpr 12 KB

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