Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test.php 4.1 KiB

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