forked from Pakz001/blitzplusexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Painting Brush - Smudge - Example.bb
237 lines (217 loc) · 4.78 KB
/
Painting Brush - Smudge - 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
Global win = CreateWindow("Painting Brush - Smudge - 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 lcmx ; previous mouse pos
Global lcmy
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 smudge=False
Global smudgeint = 5
updateinterface
.main
Repeat
we = WaitEvent()
If we=$102;keyup
If screen = "canvas"
If EventData()= 31 Then
If smudge=True Then smudge = False Else smudge=True
updateinterface
End If
If EventData()>=2 And EventData()<=10
brushsize = EventData()-1
updateinterface
End If
End If
End If
If we=$201;mosuedown
If screen = "canvas"
End If
End If
If we=$202;mouseup
If EventSource() = can
If EventData() = 1
If RectsOverlap(cmx,cmy,1,1,680,0,32,11*32)
brushindex=cmy/32
updateinterface
End If
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,brushindex)
updateinterface
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
lcmx = cmx
lcmy = cmy
cmx = EventX()
cmy = EventY()
If MouseDown(1) = True
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
brushdown(cmx,cmy,brushindex)
updateinterface
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 smudge = True
Text 10,480,"Smudge brush is On (key s)"
Else
Text 10,480,"Smudge brush is Off (key s)"
End If
Text 10,500,"Brushsize : "+brushsize
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 brushdown(cmx,cmy,ind)
Local cnt
Local brushbuffer[10*10]
If brushsize=1 Then map(cmx/tw,cmy/th) = ind
If brushsize>1 Then
If smudge = True Then
If lcmx<>cmx And lcmy<>cmy
cnt=0
For y=-brushsize/2 To brushsize/2
For x=-brushsize/2 To brushsize/2
brushbuffer[cnt]=-1
If lcmx/tw+x >=0 And lcmx/tw+x <=mw
If lcmy/th+y >=0 And lcmy/th+y <=mh
brushbuffer[cnt] = map(lcmx/tw+x,lcmy/th+y)
End If
End If
cnt=cnt+1
Next
Next
cnt=0
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 brushbuffer[cnt]>-1
If Rnd(smudgeint)<2
map(cmx/tw+x,cmy/th+y) = brushbuffer[cnt]
End If
End If
End If
End If
cnt=cnt+1
Next
Next
;
EndIf
Else
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