demo.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. clear// A small test program to exercise most of TIGR's features.
  2. #include "tigr.h"
  3. #include <math.h>
  4. float playerx = 160, playery = 200;
  5. float playerxs = 0, playerys = 0;
  6. bool standing = true;
  7. float remaining = 0;
  8. Tigr *backdrop, *screen;
  9. // Some simple platformer-esque physics.
  10. // I do not necessarily recommend this as a good way of implementing a platformer :)
  11. void update(float dt) {
  12. if (remaining > 0)
  13. remaining -= dt;
  14. // Read the keyboard and move the player.
  15. if (standing && tigrKeyDown(screen, TK_SPACE))
  16. playerys -= 200;
  17. if (tigrKeyHeld(screen, TK_LEFT) || tigrKeyHeld(screen, 'A'))
  18. playerxs -= 10;
  19. if (tigrKeyHeld(screen, TK_RIGHT) || tigrKeyHeld(screen, 'D'))
  20. playerxs += 10;
  21. float oldx = playerx, oldy = playery;
  22. // Apply simply physics.
  23. playerxs *= exp(-10.0f * dt);
  24. playerys *= exp(-2.0f * dt);
  25. playerys += dt * 200.0f;
  26. playerx += dt * playerxs;
  27. playery += dt * playerys;
  28. // Apply collision.
  29. if (playerx < 8) {
  30. playerx = 8;
  31. playerxs = 0;
  32. }
  33. if (playerx > screen->w - 8) {
  34. playerx = screen->w - 8.0f;
  35. playerxs = 0;
  36. }
  37. // Apply playfield collision and stepping.
  38. float dx = (playerx - oldx) / 10;
  39. float dy = (playery - oldy) / 10;
  40. standing = false;
  41. for (int n = 0; n < 10; n++) {
  42. TPixel p = tigrGet(backdrop, (int)oldx, (int)oldy - 1);
  43. if (p.r == 0 && p.g == 0 && p.b == 0)
  44. oldy -= 1;
  45. p = tigrGet(backdrop, (int)oldx, (int)oldy);
  46. if (p.r == 0 && p.g == 0 && p.b == 0 && playerys > 0) {
  47. playerys = 0;
  48. dy = 0;
  49. standing = true;
  50. }
  51. oldx += dx;
  52. oldy += dy;
  53. }
  54. playerx = oldx;
  55. playery = oldy;
  56. }
  57. int main(int argc, char* argv[]) {
  58. // Load our sprite.
  59. Tigr* squinkle = tigrLoadImage("squinkle.png");
  60. if (!squinkle)
  61. tigrError(0, "Cannot load squinkle.png");
  62. // Load some UTF-8 text.
  63. char* greeting = (char*)tigrReadFile("greeting.txt", 0);
  64. if (!greeting)
  65. tigrError(0, "Cannot load greeting.txt");
  66. // Make a window and an off-screen backdrop.
  67. screen = tigrWindow(320, 240, greeting, TIGR_2X);
  68. backdrop = tigrBitmap(screen->w, screen->h);
  69. // Fill in the background.
  70. tigrClear(backdrop, tigrRGB(80, 180, 255));
  71. tigrFill(backdrop, 0, 200, 320, 40, tigrRGB(60, 120, 60));
  72. tigrFill(backdrop, 0, 200, 320, 3, tigrRGB(0, 0, 0));
  73. tigrLine(backdrop, 0, 201, 320, 201, tigrRGB(255, 255, 255));
  74. // Enable post fx
  75. tigrSetPostFX(screen, 1, 1, 1, 2.0f);
  76. int prevx = 0, prevy = 0, prev = 0;
  77. // Maintain a list of characters entered.
  78. int chars[16];
  79. for (int n = 0; n < 16; n++)
  80. chars[n] = '_';
  81. // Repeat till they close the window.
  82. while (!tigrClosed(screen) && !tigrKeyDown(screen, TK_ESCAPE)) {
  83. // Update the game.
  84. float dt = tigrTime();
  85. update(dt);
  86. // Read the mouse and draw lines when pressed.
  87. int x, y, b;
  88. tigrMouse(screen, &x, &y, &b);
  89. if (b & 1) {
  90. if (prev)
  91. tigrLine(backdrop, prevx, prevy, x, y, tigrRGB(0, 0, 0));
  92. prevx = x;
  93. prevy = y;
  94. prev = 1;
  95. } else {
  96. prev = 0;
  97. }
  98. // Composite the backdrop and sprite onto the screen.
  99. tigrBlit(screen, backdrop, 0, 0, 0, 0, backdrop->w, backdrop->h);
  100. tigrBlitAlpha(screen, squinkle, (int)playerx - squinkle->w / 2, (int)playery - squinkle->h, 0, 0, squinkle->w,
  101. squinkle->h, 1.0f);
  102. tigrPrint(screen, tfont, 10, 10, tigrRGBA(0xc0, 0xd0, 0xff, 0xc0), greeting);
  103. tigrPrint(screen, tfont, 10, 222, tigrRGBA(0xff, 0xff, 0xff, 0xff), "A D + SPACE");
  104. // Grab any chars and add them to our buffer.
  105. for (;;) {
  106. int c = tigrReadChar(screen);
  107. if (c == 0)
  108. break;
  109. for (int n = 1; n < 16; n++)
  110. chars[n - 1] = chars[n];
  111. chars[15] = c;
  112. }
  113. // Print out the character buffer too.
  114. char tmp[100], *p = tmp;
  115. for (int n = 0; n < 16; n++)
  116. p = tigrEncodeUTF8(p, chars[n]);
  117. *p = 0;
  118. tigrPrint(screen, tfont, 160, 222, tigrRGB(255, 255, 255), "Chars: %s", tmp);
  119. // Update the window.
  120. tigrUpdate(screen);
  121. }
  122. tigrFree(squinkle);
  123. tigrFree(backdrop);
  124. tigrFree(screen);
  125. return 0;
  126. }