Subversion Repositories Kolibri OS

Rev

Rev 5305 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5305 codemaster 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
5311 yogev_ezra 52
    get x=get.random
53
    . p=q, (u32) *p=x, q+4
5305 codemaster 54
  endl
55
endf
56
 
57
function reset.game
58
  randomize.puzzle
59
endf
60
 
61
function on.create
62
  erase.puzzle
63
  . scene=SCENE.TITLE
64
  set.box puzzle.box,\
65
   MAP.X, MAP.Y, PUZZLE.W, PUZZLE.H
66
endf
67
 
68
function is.solved
69
  locals n
70
  alias p=r0, x=r1
71
  . p=puzzle, n=(MAP.W*MAP.H)
72
  loop n, (u32) x=*p++
73
    if x<>NORMAL, return 0, end
74
  endl
75
endf 1
76
 
77
function get.piece, x, y
78
  . r0=y, r0*MAP.W, r0+x, r0*4, r0+puzzle
79
endf
80
 
81
function get.piece.rotate, x, y
82
  get.piece x, y
83
  . (u32) r0=*r0
84
endf
85
 
86
function set.piece.rotate, x, y, r
87
  if r>ROTATE.L, r=0, end
88
  get.piece x, y
89
  . r1=r, (u32) *r0=r1
90
  is.solved
91
  if true
92
    . scene=SCENE.SOLVED
93
  end
94
endf
95
 
96
function copy.piece, x, y
97
  locals w, h, pw
98
  alias p=r0, s=r1, n=r2
99
  . p=piece.pixels, piece.i.p=p
100
  . piece.i.w=TILE.W, piece.i.h=TILE.H
101
  . n=PUZZLE.W, n*4, pw=n
102
  . n=y, n*pw, n*TILE.W
103
  . s=x, s*TILE.W, s*4, n+s
104
  . s=kolibri.i.p, s+n
105
  . h=TILE.H
106
  loop h, w=TILE.W
107
    loop w, (u32) *p++=*s++
108
    endl
109
    . n=TILE.W, n*4, s-n, s+pw
110
  endl
111
endf
112
 
113
function draw.piece, px, py, x, y
114
  locals z
115
  get z=get.piece.rotate px, py
116
  copy.piece px, py
117
  if z=NORMAL
118
    draw.image piece.i, x, y
119
  else.if z=ROTATE.R
120
    draw.image.rr piece.i, x, y
121
  else.if z=INVERT.XY
122
    . r0=y, r0+TILE.H
123
    draw.image.ixy piece.i, x, r0
124
  else.if z=ROTATE.L
125
    draw.image.rl piece.i, x, y
126
  end
127
endf
128
 
129
function draw.puzzle
130
  locals x, y
131
  . y=0
132
  while y<8, x=0
133
    while x<8
134
      . r0=x, r0*TILE.W, r0+MAP.X
135
      . r1=y, r1*TILE.H, r1+MAP.Y
136
      draw.piece x, y, r0, r1
137
      . x++
138
    endw
139
    . y++
140
  endw
141
endf
142
 
143
function draw.grid
144
  locals x, y
145
  . y=0
146
  while y<8, x=0
147
    while x<8
148
      . r0=x, r0*TILE.W, r0+MAP.X
149
      . r1=y, r1*TILE.H, r1+MAP.Y
150
      draw.outline r0, r1,\
151
       TILE.W, TILE.H, grid.color
152
      . x++
153
    endw
154
  . y++
155
  endw
156
endf
157
 
158
function draw.wood.frame
159
  draw.image wood1.i, 0, 0
160
  draw.image wood1.i, 0, WINDOW.H-TILE.H
161
  draw.image wood2.i, 0, TILE.H
162
  draw.image wood2.i, WINDOW.W-TILE.W, TILE.H
163
  draw.image close.i, WINDOW.W-40, 8
164
endf
165
 
166
function on.draw
167
  draw.wood.frame
168
  draw.puzzle
169
  if scene=SCENE.TITLE
170
    draw.box.o puzzle.box, grid.color
171
  end
172
  if scene=SCENE.PLAY
173
    draw.grid
174
  end
175
  if scene=SCENE.SOLVED
176
    draw.image.v solved.i, 132, 13, WHITE
177
  else
178
    draw.image.v logo.i, 180, 13, WHITE
179
  end
180
endf
181
 
182
function on.key
183
  ; ...
184
endf
185
 
186
function get.select.xy
187
  . r0=mouse.x, r0-MAP.X, r0-WINDOW.X
188
  . r1=TILE.W, r0/r1, select.x=r0
189
  . r0=mouse.y, r0-MAP.Y, r0-WINDOW.Y
190
  . r1=TILE.H, r0/r1, select.y=r0
191
endf
192
 
193
function on.mouse
194
  locals r
195
  if mouse.event='c'
196
    . r0=&close.i.x
197
    if.select r0
198
      exit
199
    end
200
    if scene=SCENE.TITLE
201
      reset.game
202
      . scene=SCENE.PLAY
203
      go .draw
204
    end
205
    if scene=SCENE.PLAY
206
      if.select puzzle.box
207
        get.select.xy
208
        get r=get.piece.rotate \
209
         select.x, select.y
210
        . r++
211
        set.piece.rotate \
212
         select.x, select.y, r
213
        go .draw
214
      end
215
    end
216
    if scene=SCENE.SOLVED
217
      reset.game
218
      . scene=SCENE.TITLE
219
      go .draw
220
    end
221
    .draw:
222
    render
223
  end
224
endf
225
 
226
function on.timer
227
  ; ...
228
endf
229
 
230
function on.exit
231
  ; ...
232
endf