Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ; KOLIBRI PUZZLE CHALLENGE
  2.  
  3. TILE.W=64
  4. TILE.H=64
  5. MAP.X=TILE.W
  6. MAP.Y=TILE.H
  7. MAP.W=8
  8. MAP.H=8
  9. PUZZLE.W=MAP.W*TILE.W
  10. PUZZLE.H=MAP.H*TILE.H
  11.  
  12. WINDOW.W=PUZZLE.W+(TILE.W*2)
  13. WINDOW.H=PUZZLE.H+(TILE.H*2)
  14.  
  15. include 'a.inc'
  16.  
  17. text title(64)='Kolibri Puzzle Challenge'
  18.  
  19. align
  20.  
  21. integer scene
  22. numeric SCENE.*, TITLE, PLAY, SOLVED
  23.  
  24. integer solved, select.x, select.y
  25.  
  26. puzzle: db (MAP.W*MAP.H*4) dup(0)
  27. numeric NORMAL, ROTATE.R, INVERT.XY, ROTATE.L
  28.  
  29. IMAGE piece.i
  30. piece.pixels: db (TILE.W*TILE.H*4) dup(0)
  31.  
  32. BOX my.box, puzzle.box
  33. integer grid.color=WHITE
  34.  
  35. IMAGE kolibri.i='kolibri', logo.i='logo2',\
  36.  wood1.i='wood1', wood2.i='wood2',\
  37.  close.i='x', solved.i='solved'
  38.  
  39. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40.  
  41. get.random equ random.x ROTATE.R, ROTATE.L
  42.  
  43. function erase.puzzle
  44.   memory.zero piece.pixels, (TILE.W*TILE.H*4)
  45. endf
  46.  
  47. function randomize.puzzle
  48.   locals q, n
  49.   alias p=r0, x=r1
  50.   . q=puzzle, n=(MAP.W*MAP.H)
  51.   loop n
  52.     . x=ROTATE.L, p=q, (u32) *p=x, q+4
  53.   endl
  54. endf
  55.  
  56. function reset.game
  57.   randomize.puzzle
  58. endf
  59.  
  60. function on.create
  61.   erase.puzzle
  62.   . scene=SCENE.TITLE
  63.   set.box puzzle.box,\
  64.    MAP.X, MAP.Y, PUZZLE.W, PUZZLE.H
  65. endf
  66.  
  67. function is.solved
  68.   locals n
  69.   alias p=r0, x=r1
  70.   . p=puzzle, n=(MAP.W*MAP.H)
  71.   loop n, (u32) x=*p++
  72.     if x<>NORMAL, return 0, end
  73.   endl
  74. endf 1
  75.  
  76. function get.piece, x, y
  77.   . r0=y, r0*MAP.W, r0+x, r0*4, r0+puzzle
  78. endf
  79.  
  80. function get.piece.rotate, x, y
  81.   get.piece x, y
  82.   . (u32) r0=*r0
  83. endf
  84.  
  85. function set.piece.rotate, x, y, r
  86.   if r>ROTATE.L, r=0, end
  87.   get.piece x, y
  88.   . r1=r, (u32) *r0=r1
  89.   is.solved
  90.   if true
  91.     . scene=SCENE.SOLVED
  92.   end
  93. endf
  94.  
  95. function copy.piece, x, y
  96.   locals w, h, pw
  97.   alias p=r0, s=r1, n=r2
  98.   . p=piece.pixels, piece.i.p=p
  99.   . piece.i.w=TILE.W, piece.i.h=TILE.H
  100.   . n=PUZZLE.W, n*4, pw=n
  101.   . n=y, n*pw, n*TILE.W
  102.   . s=x, s*TILE.W, s*4, n+s
  103.   . s=kolibri.i.p, s+n
  104.   . h=TILE.H
  105.   loop h, w=TILE.W
  106.     loop w, (u32) *p++=*s++
  107.     endl
  108.     . n=TILE.W, n*4, s-n, s+pw
  109.   endl
  110. endf
  111.  
  112. function draw.piece, px, py, x, y
  113.   locals z
  114.   get z=get.piece.rotate px, py
  115.   copy.piece px, py
  116.   if z=NORMAL
  117.     draw.image piece.i, x, y
  118.   else.if z=ROTATE.R
  119.     draw.image.rr piece.i, x, y
  120.   else.if z=INVERT.XY
  121.     . r0=y, r0+TILE.H
  122.     draw.image.ixy piece.i, x, r0
  123.   else.if z=ROTATE.L
  124.     draw.image.rl piece.i, x, y
  125.   end
  126. endf
  127.  
  128. function draw.puzzle
  129.   locals x, y
  130.   . y=0
  131.   while y<8, x=0
  132.     while x<8
  133.       . r0=x, r0*TILE.W, r0+MAP.X
  134.       . r1=y, r1*TILE.H, r1+MAP.Y
  135.       draw.piece x, y, r0, r1
  136.       . x++
  137.     endw
  138.     . y++
  139.   endw
  140. endf
  141.  
  142. function draw.grid
  143.   locals x, y
  144.   . y=0
  145.   while y<8, x=0
  146.     while x<8
  147.       . r0=x, r0*TILE.W, r0+MAP.X
  148.       . r1=y, r1*TILE.H, r1+MAP.Y
  149.       draw.outline r0, r1,\
  150.        TILE.W, TILE.H, grid.color
  151.       . x++
  152.     endw
  153.   . y++
  154.   endw
  155. endf
  156.  
  157. function draw.wood.frame
  158.   draw.image wood1.i, 0, 0
  159.   draw.image wood1.i, 0, WINDOW.H-TILE.H
  160.   draw.image wood2.i, 0, TILE.H
  161.   draw.image wood2.i, WINDOW.W-TILE.W, TILE.H
  162.   draw.image close.i, WINDOW.W-40, 8
  163. endf
  164.  
  165. function on.draw
  166.   draw.wood.frame
  167.   draw.puzzle
  168.   if scene=SCENE.TITLE
  169.     draw.box.o puzzle.box, grid.color
  170.   end
  171.   if scene=SCENE.PLAY
  172.     draw.grid
  173.   end
  174.   if scene=SCENE.SOLVED
  175.     draw.image.v solved.i, 132, 13, WHITE
  176.   else
  177.     draw.image.v logo.i, 180, 13, WHITE
  178.   end
  179. endf
  180.  
  181. function on.key
  182.   ; ...
  183. endf
  184.  
  185. function get.select.xy
  186.   . r0=mouse.x, r0-MAP.X, r0-WINDOW.X
  187.   . r1=TILE.W, r0/r1, select.x=r0
  188.   . r0=mouse.y, r0-MAP.Y, r0-WINDOW.Y
  189.   . r1=TILE.H, r0/r1, select.y=r0
  190. endf
  191.  
  192. function on.mouse
  193.   locals r
  194.   if mouse.event='c'
  195.     . r0=&close.i.x
  196.     if.select r0
  197.       exit
  198.     end
  199.     if scene=SCENE.TITLE
  200.       reset.game
  201.       . scene=SCENE.PLAY
  202.       go .draw
  203.     end
  204.     if scene=SCENE.PLAY
  205.       if.select puzzle.box
  206.         get.select.xy
  207.         get r=get.piece.rotate \
  208.          select.x, select.y
  209.         . r++
  210.         set.piece.rotate \
  211.          select.x, select.y, r
  212.         go .draw
  213.       end
  214.     end
  215.     if scene=SCENE.SOLVED
  216.       reset.game
  217.       . scene=SCENE.TITLE
  218.       go .draw
  219.     end
  220.     .draw:
  221.     render
  222.   end
  223. endf
  224.  
  225. function on.timer
  226.   ; ...
  227. endf
  228.  
  229. function on.exit
  230.   ; ...
  231. endf