graphics.c 1.8 KB

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