forked from Pakz001/blitzplusexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPainting program - Line drawing Example.bb
288 lines (270 loc) · 6.02 KB
/
Painting program - Line drawing Example.bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
Global win = CreateWindow("Painting program - Line drawing Example",100,100,800,600,0,1)
Global can = CreateCanvas(0,20,800,600,win)
Dim cols(10,3);the colors
Global mw = 48;wapwidth
Global mh = 48;height
Global tw = 640/mw;tilewidth
Global th = 480/mh;height
Dim map(mw,mh);create map
Global canim = CreateImage(800,600);the image (speedup)
Global brushindex=5;brushcolor
Global brushsize=4
Global cmx;mouse x pos
Global cmy; mouse y pos
Global tileim = CreateImage(tw,th,11)
Global tileimbig = CreateImage(32,32,11)
; create the colors and images
refreshtileimages(True)
; our area of operation
Global screen$="canvas"
Global explode = False
Global linemode = False
Global linedrawn = False
Global lsx1
Global lsy1
Global lsx2
Global lsy2
updateinterface
.main
Repeat
we = WaitEvent()
If we=$102;keyup
If screen = "canvas"
If EventData()=38;l
If linemode = True Then linemode = False Else linemode = True
If linemode = True Then linedrawn=True
updateinterface
End If
If EventData()>=2 And EventData()<=10
brushsize = EventData()-1
updateinterface
End If
If EventData()=18;e
If explode = True Then explode = False Else explode = True
End If
End If
End If
If we=$201;mosuedown
If screen = "canvas"
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
lsx1 = cmx
lsy1 = cmy
linedrawn=False
End If
End If
End If
If we=$202;mouseup
If EventSource() = can
If EventData() = 1
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
If linemode = True
lsx2 = cmx
lsy2 = cmy
makeline
updateinterface
linedrawn=True
End If
Else
linedrawn=True
End If
If RectsOverlap(cmx,cmy,1,1,680,0,32,11*32)
brushindex=cmy/32
updateinterface
End If
If linemode=False
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,brushindex)
updateinterface
End If
End If
End If
If EventData() = 2
If RectsOverlap(cmx,cmy,1,1,680,0,32,11*32)
If RequestColor()=True
brushindex=cmy/32
cols(brushindex,0) = RequestedRed()
cols(brushindex,1) = RequestedGreen()
cols(brushindex,2) = RequestedBlue()
refreshtileimages
updateinterface
End If
End If
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,0)
updateinterface
End If
End If
End If
End If
If we=$203;mousemove
If EventSource()=can
cmx = EventX()
cmy = EventY()
If MouseDown(1) = True
If linemode=False
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,brushindex)
updateinterface
End If
End If
End If
If MouseDown(2) = True
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,0)
updateinterface
End If
End If
updateinterface
End If
End If
If we=$401;gadgetaction
End If
If we=$4001
End If
If we=$803 Then Exit
Forever
End
.funcs
Function updateinterface()
SetBuffer ImageBuffer(canim)
Cls
Color 255,255,255
For y=0 To mh-1
For x=0 To mw-1
DrawImage tileim,x*tw,y*th,map(x,y)
Next
Next
For y=0 To 10
DrawImage tileimbig,680,y*32,y
If brushindex = y
Rect 681,y*32,31,31,False
End If
Next
For y=0 To 47
For x=0 To 47
a = map(x,y)
Color cols(a,0),cols(a,1),cols(a,2)
Rect x*2+660,y*2+370,2,2
Next
Next
; draw the brush view
Color 255,255,255
Rect cmx-brushsize*tw/2,cmy-brushsize*th/2,brushsize*tw,brushsize*th,False
; draw the line
If linemode = True And linedrawn = False
Color 255,255,0
Line lsx1,lsy1,cmx,cmy
End If
If explode = True
Text 10,480,"Explode brush is On (key e)"
Else
Text 10,480,"Explode brush is Off (key e)"
End If
If linemode = True
Text 10,500,"Line mode is on (l)"
Else
Text 10,500,"Line mode is off (l)"
End If
SetBuffer CanvasBuffer(can)
Cls
DrawImage canim,0,0
FlipCanvas can
End Function
Function refreshtileimages(init=False)
For i = 0 To 10
SetBuffer ImageBuffer(tileim,i)
If init=True
Color 200+i*3,10+i*20,10
cols(i,0) = ColorRed()
cols(i,1) = ColorGreen()
cols(i,2) = ColorBlue()
End If
Color cols(i,0),cols(i,1),cols(i,2)
Rect 0,0,tw,th,True
SetBuffer ImageBuffer(tileimbig,i)
Color cols(i,0),cols(i,1),cols(i,2)
Rect 0,0,32,32
For x=-1 To 1
For y=-1 To 1
Color 4,4,4
Text tw/2+x,th/2+y,i,1,1
Next
Next
Color 255,255,255
Text tw/2,th/2,i,1,1
Next
End Function
.brushfuncs
Function makeline()
Local x1=lsx1/tw
Local y1=lsy1/th
Local x2=lsx2/tw
Local y2=lsy2/th
Local dx
Local dy
Local sx
Local sy
Local e
dx = Abs(x2 - x1)
sx = -1
If x1 < x2 Then sx = 1
dy = Abs(y2 - y1)
sy = -1
If y1 < y2 Then sy = 1
If dx < dy Then
e = dx / 2
Else
e = dy / 2
End If
Local exitloop=False
While exitloop = False
; map(x1,y1) = brushindex
brushdown(x1*tw,y1*th,brushindex)
If x1 = x2
If y1 = y2
exitloop = True
End If
End If
If dx > dy Then
x1 = x1 + sx : e = e - dy
If e < 0 Then e = e + dx : y1 = y1 + sy
Else
y1 = y1 + sy : e = e - dx
If e < 0 Then e = e + dy : x1 = x1 + sx
EndIf
Wend
End Function
Function brushdown(cmx,cmy,ind)
If brushsize=1 Then map(cmx/tw,cmy/th) = ind
If brushsize>1 Then
If explode = True
For y=-brushsize/2 To brushsize/2
For x=-brushsize/2 To brushsize/2
If cmx/tw+x >=0 And cmx/tw+x <=mw
If cmy/th+y >=0 And cmy/th+y <=mh
If Rnd(brushsize)<2
b = Rnd(cmx/tw-brushsize/2,cmx/tw+brushsize/2)
c = Rnd(cmy/th-brushsize/2,cmy/th+brushsize/2)
If b>=0 And b<=mw And c>=0 And c<=mh
a = map(b,c)
map(cmx/tw+x,cmy/th+y) = a
End If
End If
End If
End If
Next
Next
End If
If explode = False
For y=-brushsize/2 To brushsize/2
For x=-brushsize/2 To brushsize/2
If cmx/tw+x >=0 And cmx/tw+x <=mw
If cmy/th+y >=0 And cmy/th+y <=mh
map(cmx/tw+x,cmy/th+y) = ind
End If
End If
Next
Next
End If
End If
End Function