25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

test.php 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/php5
  2. <?php
  3. /*
  4. * Test PHP bindings test program
  5. * Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>
  6. *
  7. * This file is a Php port of "caca-sharp/test.cs"
  8. * which is:
  9. * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  10. * 2007 Sam Hocevar <sam@hocevar.net>
  11. * All Rights Reserved
  12. *
  13. * This program is free software. It comes without any warranty, to
  14. * the extent permitted by applicable law. You can redistribute it
  15. * and/or modify it under the terms of the Do What the Fuck You Want
  16. * to Public License, Version 2, as published by Sam Hocevar. See
  17. * http://www.wtfpl.net/ for more details.
  18. */
  19. if (php_sapi_name() != "cli") {
  20. die("You have to run this program with php-cli!\n");
  21. }
  22. include dirname($argv[0]) . '/../caca.php';
  23. class DemoCanvas extends Canvas
  24. {
  25. private $image;
  26. private $startTime;
  27. private $d;
  28. private $scroll;
  29. function __construct()
  30. {
  31. parent::__construct();
  32. $this->startTime = gettimeofday(true);
  33. $message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN";
  34. $this->scroll = new Canvas(strlen($message), 1);
  35. $this->scroll->setColorAnsi(AnsiColor::WHITE, AnsiColor::TRANSPARENT);
  36. $this->scroll->putStr(0, 0, $message);
  37. $fontList = Font::getList();
  38. $f = new Font($fontList[1]);
  39. $w = $f->getWidth() * strlen($message);
  40. $h = $f->getHeight();
  41. $this->image = imagecreatetruecolor($w, $h);
  42. imagealphablending($this->image, false);
  43. imagesavealpha($this->image, true);
  44. $this->d = new Dither($this->image);
  45. $f->Render($this->scroll, $this->image);
  46. }
  47. function Draw()
  48. {
  49. $barCount = 6;
  50. $t = (gettimeofday(true) - $this->startTime) * 1000;
  51. $this->Clear();
  52. $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLACK);
  53. for($i = 0; $i < $barCount; $i++)
  54. {
  55. $v = ((sin(($t / 500.0)
  56. + (1.0 * $i / $barCount)) + 1) / 2) * $this->getHeight();
  57. $p1_x = 0; $p1_y = intval($v);
  58. $p2_x = $this->getWidth() - 1; $p2_y = intval($v);
  59. $this->setColorAnsi(($i + 9), AnsiColor::BLACK);
  60. /* drawLine is already clipped, we don't care about overflows */
  61. $this->drawLine($p1_x + 0, $p1_y - 2, $p2_x + 0, $p2_y - 2, ord('-'));
  62. $this->drawLine($p1_x + 0, $p1_y - 1, $p2_x + 0, $p2_y - 1, ord('*'));
  63. $this->drawLine($p1_x, $p1_y, $p2_x, $p2_y, ord('#'));
  64. $this->drawLine($p1_x + 0, $p1_y + 1, $p2_x + 0, $p2_y + 1, ord('*'));
  65. $this->drawLine($p1_x + 0, $p1_y + 2, $p2_x + 0, $p2_y + 2, ord('-'));
  66. }
  67. $w = $this->getWidth();
  68. $h = $this->getHeight();
  69. $x = intval(($t / 10) % (12 * $w));
  70. $y = intval($h * (2.0 + sin($t / 200.0)) / 4);
  71. $this->d->bitmap($this, - $x, $h / 2 - $y, $w * 12, $y * 2);
  72. $this->d->bitmap($this, 12 * $w - $x, $h / 2 - $y, $w * 12, $y * 2);
  73. $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLUE);
  74. $this->putStr($this->getWidth() - 30, $this->getHeight() - 2, " -=[ Powered by libcaca ]=- ");
  75. $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLACK);
  76. }
  77. }
  78. class DemoDisplay extends Display
  79. {
  80. private $cv;
  81. function __construct(DemoCanvas $_cv)
  82. {
  83. parent::__construct($_cv);
  84. $this->setTitle("libcaca PHP Bindings test suite");
  85. $this->setDisplayTime(20000); // Refresh every 20 ms
  86. $this->cv = $_cv;
  87. }
  88. function EventLoop()
  89. {
  90. while(! ($ev = $this->getEvent(EventType::KEY_RELEASE, 10)))
  91. {
  92. $this->cv->Draw();
  93. $this->Refresh();
  94. }
  95. if($ev->getKeyCh() > 0x20 && $ev->getKeyCh() < 0x7f)
  96. printf("Key pressed: %c\n", $ev->getKeyCh());
  97. else
  98. printf("Key pressed: 0x%x\n", $ev->getKeyCh());
  99. }
  100. }
  101. class Test
  102. {
  103. static function Main()
  104. {
  105. printf("libcaca %s PHP test\n", Libcaca::getVersion());
  106. printf("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>\n");
  107. printf("(c) 2007 Sam Hocevar <sam@hocevar.net>\n");
  108. printf("(c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>\n");
  109. /* Instanciate a caca canvas */
  110. $cv = new DemoCanvas();
  111. /* We have a proper canvas, let's display it using Caca */
  112. $dp = new DemoDisplay($cv);
  113. /* Random number. This is a static method,
  114. not to be used with previous instance */
  115. printf("A random number: %d\n", Libcaca::Rand(0, 1337));
  116. $dp->EventLoop();
  117. }
  118. }
  119. Test::Main();
  120. ?>