demo07.dpr 10 KB

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