forked from Pakz001/blitzplusexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D Map Editor Data to Code.bb
329 lines (313 loc) · 6.63 KB
/
2D Map Editor Data to Code.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
; This tool is a map editor that turns the data into a
; piece of copyable code. Its in the form of an
; multidimensional array.
;
;
;
SeedRnd MilliSecs()
.setup
Global win = CreateWindow("Make Monkey array (tilemap) Example",100,100,800,600,0,1)
Global txt = CreateTextArea(0,20,800,600,win)
Global txt2 = CreateTextArea(0,20,800,600,win)
Global tab = CreateTabber(0,0,800,20,win)
Global can = CreateCanvas(0,20,800,600,win)
HideGadget txt2
Global mw = 40
Global mh = 30
Global tw = 15
Global th = 15
Dim map(mw,mh)
Global canim = CreateImage(800,600)
SetBuffer ImageBuffer(canim)
font=LoadFont ("verdana.ttf",12)
SetFont font
Dim cols(16,3)
For i=0 To 15
cols(i,0) = Rand(0,255)
cols(i,1) = Rand(0,255)
cols(i,2) = Rand(0,255)
Next
Global screen$="txt"
Global lastscreen$="txt"
Global brushindex=1
Global cmx
Global cmy
Global tileim = CreateImage(32,32,16)
Global selectindexim = CreateImage(32,16*32)
updategraphics
InsertGadgetItem tab,0,"Monkey code",0
InsertGadgetItem tab,1,"Blitz basic code",1
InsertGadgetItem tab,2,"Canvas",2
Global mytxt$
makemonkeycode
Global timer = CreateTimer(60)
updateinterface
.mainloop
Repeat
we = WaitEvent()
If we=$102 ; keyup
If screen="canvas"
If EventData()=46;c - clear
For y=0 To mh
For x=0 To mw
map(x,y)=0
Next
Next
updateinterface
End If
If EventData()=200;keu up
If brushindex>0 Then brushindex = brushindex-1
updateinterface
End If
If EventData()=208;key down
If brushindex<15 Then brushindex = brushindex+1
updateinterface
End If
End If
End If
If we=$201;MouseDown
If EventSource() = can
If EventData() = 1
End If
End If
End If
If we=$202;mouseup
If EventSource() = can
If EventData() = 1
If RectsOverlap(cmx,cmy,1,1,680,0,32,16*32)
brushindex=cmy/32
updateinterface
End If
End If
If EventData() = 2
If RectsOverlap(cmx,cmy,1,1,680,0,32,16*32)
brushindex=cmy/32
If RequestColor( cols(brushindex,0) , cols(brushindex,1) , cols(brushindex,2)) Then
cols(brushindex,0) = RequestedRed()
cols(brushindex,1) = RequestedGreen()
cols(brushindex,2) = RequestedBlue()
updategraphics
End If
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 RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
map(cmx/tw,cmy/th) = brushindex
updateinterface
End If
End If
If MouseDown(2) = True
If RectsOverlap(cmx,cmy,1,1,0,0,(mw+1)*tw,(mh+1)*th)
map(cmx/tw,cmy/th) = 0
updateinterface
End If
End If
End If
End If
If we=$401;gadgetaction
If EventSource() = tab
sg = SelectedGadgetItem(tab)
If sg = 0
makemonkeycode
makeblitzcode
HideGadget txt2
HideGadget can
ShowGadget txt
screen="txt"
End If
If sg = 1
makeblitzcode
makemonkeycode
HideGadget txt
HideGadget can
ShowGadget txt2
screen="txt2"
End If
If sg = 2
If screen="txt" Then readmonkeycode
If screen="txt2" Then readblitzcode
HideGadget txt
HideGadget txt2
ShowGadget can
updateinterface
FlipCanvas can
screen="canvas"
End If
End If
End If
If we=$4001
End If
If we=$803 Then Exit
Forever
End
.screendraw
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
DrawImage selectindexim,680,0
Color 255,255,0
Rect 681,brushindex*32,31,31,False
Rect 682,brushindex*32,29,29,False
Text 5,480,"Mapw:"+mw+" maph:"+mh
Text 5,490,"Left mouse = put"
Text 5,500,"Right mouse = put 0"
Text 5,510,"c to clear"
Text 5,520,"curs up/down brush index"
SetBuffer CanvasBuffer(can)
Cls
DrawImage canim,0,0
FlipCanvas can
End Function
Function updategraphics()
; palette index choice
SetBuffer ImageBuffer(selectindexim)
font = LoadFont("verdana.ttf",21,1)
SetFont font
For i = 0 To 16-1
For y=0 To 32
Color cols(i,0)/32*y,cols(i,1)/32*y,cols(i,2)/32*y
Line 0,y+i*32,32,y+i*32
Next
Color cols(i,0),cols(i,1),cols(i,2)
; Rect 0,i*32,32,32,True
Color 0,0,0
Rect 0,i*32,14,14
Color 255,255,255
Text 4,i*32+4,i
Next
; tiles on the map
SetBuffer ImageBuffer(tileim)
For i = 0 To 15
SetBuffer ImageBuffer(tileim,i)
font=LoadFont ("verdana.ttf",16,1)
SetFont font
For y=0 To th
Color cols(i,0)/th*y,cols(i,1)/th*y,cols(i,2)/th*y
Line 0,y,tw,y
Next
Color cols(i,0),cols(i,1),cols(i,2)
Rect 0,0,tw,th,False
Color 0,0,0
Rect 0,0,14,14
Color 255,255,255
Text 4,4,i
Next
End Function
.codeinputoutput
Function readblitzcode()
mytxt$ = TextAreaText(txt2)
Local cnt=0
Local stp=1
Local exitloop=False
While exitloop=False
stp=Instr(mytxt$,",",stp)
If stp=0 Then exitloop=True
stp=stp+1
cnt=cnt+1
Wend
; DebugLog ((mw)*(mh))+"--"+cnt+"---"+mh
If cnt <> ((mw)*(mh))-(-1+mh) Then Notify "Not valid map data"
Local mytxt2$
Local a$=""
Local b$=""
Local c$=""
s = Instr(Lower(mytxt),"data")
s=s+4
For i = s To Len(mytxt$)
a$=Mid(mytxt$,i,1)
If a$="," Then b$=b$+a$
If a$=Chr(10) Then b$=b$+","
If Asc(a$) >= 48 And Asc(a$)<= 57 Then b$=b$+a$
Next
b$=b$+","
x=0
For i=1 To Len(b$)
a$=Mid(b$,i,1)
If Asc(a$)>=48 And Asc(a$)<=57
c$=c$+a$
End If
If a$="," Then
map(x,y) = Int(c)
c$=""
x=x+1
If x>=mw Then x=0:y=y+1
End If
Next
End Function
Function makeblitzcode()
mytxt$=".label"+Chr(13)+Chr(10)
For y=0 To mh-1
mytxt=mytxt+"Data "
a$=""
For x=0 To mw-1
a$=a$+map(x,y)+","
Next
mytxt=mytxt+Left(a$,Len(a$)-1)
mytxt=mytxt+Chr(13)+Chr(10)
Next
SetTextAreaText txt2,mytxt
End Function
Function makemonkeycode()
mytxt$="Global map:Int[][] = ["+Chr(13)+Chr(10)
For y=0 To mh-1
mytxt$=mytxt$+"["
For x=0 To mw-1
mytxt$=mytxt$+map(x,y)
mytxt$=mytxt$+","
Next
mytxt$=Left(mytxt$,Len(mytxt$)-1)
mytxt$=mytxt$+"]"
mytxt$=mytxt$+","
mytxt$=mytxt$+Chr(13)+Chr(10)
Next
mytxt$=Left(mytxt$,Len(mytxt$)-3)
mytxt$=mytxt$+"]"
SetTextAreaText txt,mytxt$
End Function
Function readmonkeycode()
mytxt$ = TextAreaText(txt)
Local cnt=0
Local stp=1
Local exitloop=False
While exitloop=False
stp=Instr(mytxt$,",",stp)
If stp=0 Then exitloop=True
stp=stp+1
cnt=cnt+1
Wend
If cnt <> ((mw)*(mh)) Then Notify "Not valid map data"
Local mytxt2$
Local a$=""
Local b$=""
Local c$=""
For i = 1 To Len(mytxt$)
a$=Mid(mytxt$,i,1)
If a$="," Then b$=b$+a$
If Asc(a$) >= 48 And Asc(a$)<= 57 Then b$=b$+a$
Next
For i=1 To Len(b$)
a$=Mid(b$,i,1)
If Asc(a$)>=48 And Asc(a$)<=57
c$=c$+a$
End If
If a$="," Then
map(x,y) = Int(c)
c$=""
x=x+1
If x>=mw Then x=0:y=y+1
End If
Next
End Function