graphics.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <event.h>
  13. #include "sokoban.h"
  14. void
  15. drawscreen(void)
  16. {
  17. draw(screen, screen->r, img, nil, ZP);
  18. flushimage(display, 1);
  19. }
  20. void
  21. drawglenda(void)
  22. {
  23. Rectangle r;
  24. Point p;
  25. p = level.glenda;
  26. p.x *= BoardX;
  27. p.y *= BoardY;
  28. /* leave some room from the edge of the window */
  29. p = addpt(p, Pt(1, 1));
  30. r = Rpt(p, Pt(p.x + BoardX, p.y+BoardY));
  31. draw(img, r, glenda, nil, ZP);
  32. }
  33. void
  34. drawwin(void)
  35. {
  36. Rectangle r;
  37. Point p;
  38. p = level.glenda;
  39. p.x *= BoardX;
  40. p.y *= BoardY;
  41. p = addpt(p, Pt(6, 6));
  42. p = addpt(p, Pt(1, 1));
  43. r = Rpt(p, Pt(p.x + BoardX, p.y+BoardY));
  44. draw(img, r, text, win, ZP);
  45. }
  46. void
  47. drawboard(Point p)
  48. {
  49. Rectangle r;
  50. uint square = level.board[p.x][p.y];
  51. p.x *= BoardX;
  52. p.y *= BoardY;
  53. /* leave some room from the edge of the window */
  54. p = addpt(p, Pt(1, 1));
  55. r = Rpt(p, Pt(p.x + BoardX, p.y+BoardY));
  56. switch(square) {
  57. case Background:
  58. draw(img, r, bg, nil, ZP);
  59. break;
  60. case Empty:
  61. draw(img, r, empty, nil, ZP);
  62. break;
  63. case Wall:
  64. draw(img, r, wall, nil, ZP);
  65. break;
  66. case Cargo:
  67. draw(img, r, cargo, nil, ZP);
  68. break;
  69. case Goal:
  70. draw(img, r, goal, nil, ZP);
  71. break;
  72. case GoalCargo:
  73. draw(img, r, goalcargo, nil, ZP);
  74. break;
  75. }
  76. }
  77. void
  78. resize(Point p)
  79. {
  80. /* resize to the size of the current level */
  81. int fd;
  82. fd = open("/dev/wctl", OWRITE);
  83. if(fd >= 0){
  84. fprint(fd, "resize -dx %d -dy %d", p.x*BoardX+10, p.y*BoardY+10);
  85. close(fd);
  86. }
  87. }
  88. Point
  89. boardsize(Point p)
  90. {
  91. return Pt(p.x*BoardX+2, p.y*BoardY+2);
  92. }
  93. void
  94. drawlevel(void)
  95. {
  96. int x, y;
  97. resize(level.max);
  98. if(img)
  99. freeimage(img);
  100. img = eallocimage(Rpt(Pt(0, 0), boardsize(level.max)), 0, 0);
  101. draw(img, insetrect(img->r, 1), empty, nil, ZP);
  102. for(x = 0; x < MazeX; x++) {
  103. for(y = 0; y < MazeY; y++) {
  104. drawboard(Pt(x, y));
  105. }
  106. }
  107. drawglenda();
  108. }