demo04.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. program demo04;
  2. {$I zglCustomConfig.cfg}
  3. uses
  4. zgl_main,
  5. zgl_screen,
  6. zgl_window,
  7. zgl_timers,
  8. zgl_touch,
  9. zgl_font,
  10. zgl_text,
  11. zgl_primitives_2d,
  12. zgl_sprite_2d,
  13. zgl_textures,
  14. zgl_textures_png,
  15. zgl_textures_jpg,
  16. zgl_math_2d,
  17. zgl_collision_2d,
  18. zgl_utils
  19. ;
  20. var
  21. dirRes : UTF8String = 'data/';
  22. fntMain : zglPFont;
  23. texBack : zglPTexture;
  24. correctAspect : Boolean = TRUE;
  25. useLandscape : Boolean = TRUE;
  26. usePortrait : Boolean = TRUE;
  27. correctRect : zglTRect;
  28. landscapeRect : zglTRect;
  29. portraitRect : zglTRect;
  30. procedure Init;
  31. begin
  32. zgl_Enable( CORRECT_RESOLUTION );
  33. scr_CorrectResolution( 800, 600 );
  34. fntMain := font_LoadFromFile( dirRes + 'font.zfi' );
  35. texBack := tex_LoadFromFile( dirRes + 'back03.jpg' );
  36. end;
  37. procedure Draw;
  38. var
  39. w : Single;
  40. str : UTF8String;
  41. begin
  42. ssprite2d_Draw( texBack, 0, 0, 800, 600, 0 );
  43. str := 'Tap here to toggle' + #10 + 'Correction of aspect';
  44. correctRect.X := 64;
  45. correctRect.Y := 100;
  46. correctRect.W := text_GetWidth( fntMain, str ) + 8;
  47. correctRect.H := 64;
  48. if correctAspect Then
  49. begin
  50. pr2d_Rect( correctRect.X, correctRect.Y, correctRect.W, correctRect.H, $FFFFFF, 25, PR2D_FILL );
  51. pr2d_Rect( correctRect.X, correctRect.Y, correctRect.W, correctRect.H, $00FF00, 255 );
  52. end else
  53. begin
  54. pr2d_Rect( correctRect.X, correctRect.Y, correctRect.W, correctRect.H, $000000, 155, PR2D_FILL );
  55. pr2d_Rect( correctRect.X, correctRect.Y, correctRect.W, correctRect.H, $FFFFFF, 255 );
  56. end;
  57. text_DrawInRect( fntMain, correctRect, str, TEXT_HALIGN_CENTER or TEXT_VALIGN_CENTER );
  58. str := 'Tap here to toggle support of' + #10 + 'Landscape mode';
  59. landscapeRect.X := 260;
  60. landscapeRect.Y := 100;
  61. landscapeRect.W := text_GetWidth( fntMain, str ) + 8;
  62. landscapeRect.H := 64;
  63. if useLandscape Then
  64. begin
  65. pr2d_Rect( landscapeRect.X, landscapeRect.Y, landscapeRect.W, landscapeRect.H, $FFFFFF, 25, PR2D_FILL );
  66. pr2d_Rect( landscapeRect.X, landscapeRect.Y, landscapeRect.W, landscapeRect.H, $00FF00, 255 );
  67. end else
  68. begin
  69. pr2d_Rect( landscapeRect.X, landscapeRect.Y, landscapeRect.W, landscapeRect.H, $000000, 155, PR2D_FILL );
  70. pr2d_Rect( landscapeRect.X, landscapeRect.Y, landscapeRect.W, landscapeRect.H, $FFFFFF, 255 );
  71. end;
  72. text_DrawInRect( fntMain, landscapeRect, str, TEXT_HALIGN_CENTER or TEXT_VALIGN_CENTER );
  73. str := 'Tap here to toggle support of' + #10 + 'Portrait mode';
  74. portraitRect.W := text_GetWidth( fntMain, str ) + 8;
  75. portraitRect.H := 64;
  76. portraitRect.X := 800 - portraitRect.W - 64;
  77. portraitRect.Y := 100;
  78. if usePortrait Then
  79. begin
  80. pr2d_Rect( portraitRect.X, portraitRect.Y, portraitRect.W, portraitRect.H, $FFFFFF, 25, PR2D_FILL );
  81. pr2d_Rect( portraitRect.X, portraitRect.Y, portraitRect.W, portraitRect.H, $00FF00, 255 );
  82. end else
  83. begin
  84. pr2d_Rect( portraitRect.X, portraitRect.Y, portraitRect.W, portraitRect.H, $000000, 155, PR2D_FILL );
  85. pr2d_Rect( portraitRect.X, portraitRect.Y, portraitRect.W, portraitRect.H, $FFFFFF, 255 );
  86. end;
  87. text_DrawInRect( fntMain, portraitRect, str, TEXT_HALIGN_CENTER or TEXT_VALIGN_CENTER );
  88. end;
  89. procedure Timer;
  90. begin
  91. if touch_Tap( 0 ) Then
  92. begin
  93. // RU: Данный пример использует соотношение сторон 4:3, что стандартно для iPad'а в альбомной ориентации и при масштабировании(с 800х600 до 1024х768 или 2048х1536) проблем не вызывает.
  94. // На iPhone же использование соотношения 4:3 без коррекции по ширине и высоте можно наблюдать эффект растягивания. То же самое и для портретной ориентации.
  95. // EN: This demo uses aspect 4:3, which is standard aspect for iPad'а in landscape orientation and scaling(from 800х600 to 1024х768 or 2048х1536) won't cause a problem.
  96. // Using aspect 4:3 without correction for width and height will cause a stretching effect for iPhone or portrait orientation.
  97. if col2d_PointInRect( touch_X( 0 ), touch_Y( 0 ), correctRect ) Then
  98. begin
  99. correctAspect := not correctAspect;
  100. if correctAspect Then
  101. begin
  102. zgl_Enable( CORRECT_WIDTH );
  103. zgl_Enable( CORRECT_HEIGHT );
  104. scr_SetOptions( 800, 600, REFRESH_MAXIMUM, TRUE, TRUE );
  105. end else
  106. begin
  107. zgl_Disable( CORRECT_WIDTH );
  108. zgl_Disable( CORRECT_HEIGHT );
  109. scr_SetOptions( 800, 600, REFRESH_MAXIMUM, TRUE, TRUE );
  110. end;
  111. end;
  112. // RU: Помимо стандартных настроек для iOS приложения в Info.plist, поддерживаемыми режимами ориентации можно управлять непосредственно через zgl_Enable/zgl_Disable.
  113. // EN: Besides the standard options for iOS application in Info.plist, support of orientations can be controlled using zgl_Enable/zgl_Disable.
  114. if col2d_PointInRect( touch_X( 0 ), touch_Y( 0 ), landscapeRect ) Then
  115. begin
  116. useLandscape := not useLandscape;
  117. if useLandscape Then
  118. zgl_Enable( SCR_ORIENTATION_LANDSCAPE )
  119. else
  120. zgl_Disable( SCR_ORIENTATION_LANDSCAPE );
  121. end;
  122. if col2d_PointInRect( touch_X( 0 ), touch_Y( 0 ), portraitRect ) Then
  123. begin
  124. usePortrait := not usePortrait;
  125. if usePortrait Then
  126. zgl_Enable( SCR_ORIENTATION_PORTRAIT )
  127. else
  128. zgl_Disable( SCR_ORIENTATION_PORTRAIT );
  129. end;
  130. end;
  131. touch_ClearState();
  132. end;
  133. Begin
  134. timer_Add( @Timer, 16 );
  135. zgl_Reg( SYS_LOAD, @Init );
  136. zgl_Reg( SYS_DRAW, @Draw );
  137. scr_SetOptions( 800, 600, REFRESH_MAXIMUM, TRUE, TRUE );
  138. zgl_Init();
  139. End.