demo07.cpp 10 KB

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