-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblit.asm
407 lines (361 loc) · 11.9 KB
/
blit.asm
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
; #########################################################################
;
; blit.asm - Assembly file for EECS205 Assignment 3
;
; Crystal Gong
; #########################################################################
.586
.MODEL FLAT,STDCALL
.STACK 4096
option casemap :none ; case sensitive
include stars.inc
include lines.inc
include trig.inc
include blit.inc
.DATA
;; If you need to, you can place global variables here
.CODE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DrawDinoPixel PROC USES ebx ecx x:DWORD, y:DWORD, color:DWORD
;; screen is 640 (x) by 480 (y)
cmp x, 0 ; 0<= x<=639
jl the_end
cmp x, 639
jg the_end
cmp y, 0 ; 0<=y<=479
jl the_end
cmp y, 479
jg the_end
;; pixel at ((y * dwWidth) + x)
mov eax, 640 ; eax = dwWidth
imul eax, y ; eax = eax*y
add eax, x ; eax = y*dwWidth + x
;; the first pixel of the first row is at (0,0) at the address held in ScreenBitsPtr,
;; the next pixel of the first row (1,0) is at the next byte address and subsequent pixels in that row are at increasing addresses.
;; after (639, 0) the next byte contains the pixel color for (0,1)
add eax, ScreenBitsPtr ;; eax = ScreenBitsPtr + eax
mov ebx, color ;; ebx = color
;;;checking
mov ecx, 0
mov cl, BYTE PTR[eax] ;; ecx now has the old color
;;checking ;;;
mov BYTE PTR[eax], bl ;; color the byte
the_end:
;; return the old color of the pixel
mov eax, ecx
ret ; Don't delete this line!!!
DrawDinoPixel ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;; a more advanced collison detector;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; returns if game is over
BasicBlitDino PROC USES ebx ecx edx edi esi ptrBitmap:PTR EECS205BITMAP , xcenter:DWORD, ycenter:DWORD
; ptrBitmap holds the address of a EECS205BITMAP
; draw it so that the center of the bitmap appears at (xcenter, ycenter)
; use nested loops with DrawPixel
LOCAL dw_width: DWORD, l_bound: DWORD, r_bound: DWORD, t_bound: DWORD, b_bound: DWORD
LOCAL outer_loops: DWORD, inner_loops: DWORD, outer_counter: DWORD, inner_counter: DWORD, t_color: BYTE
LOCAL isOver: DWORD
mov isOver, 0;; setting isOver to 0
;; edx is placeholder register
;; ebx holds ptrBitmap
mov ebx, ptrBitmap ; ebx = ptrBitmap
mov edx, (EECS205BITMAP PTR [ebx]).dwWidth ; edx = dwWidth
mov dw_width, edx ; dw_width = dwWidth
;; setting width bounds
mov ecx, edx ; ecx = dwWidth
shr ecx, 1 ; ecx = dwWidth/2
mov edx, xcenter
mov l_bound, edx ;l_bound = xcenter
sub l_bound, ecx ;l_bound = xcenter - dwWidth/2
mov r_bound, edx ;r_bound = xcenter
add r_bound, ecx ;r_bound = xcenter + dwWidth/2
;; setting height bounds
mov ecx, (EECS205BITMAP PTR [ebx]).dwHeight
shr ecx, 1 ;ecx = dwHeight/2
mov edx, ycenter
mov t_bound, edx ;t_bound = ycenter
sub t_bound, ecx ;t_bound = ycenter - dwHeight/2
mov b_bound, edx ;b_bound = ycenter
add b_bound, ecx ;b_bound = ycenter + dwHeight/2
;; loops
mov outer_loops, 0 ; outer_loops = 0
mov edx, t_bound
mov outer_counter, edx ; outer_counter = t_bound
jmp outer_eval
outer_loop:
mov inner_loops, 0 ; inner_loops = 0
mov edx, l_bound
mov inner_counter, edx ; inner_counter = l_bound
jmp inner_eval
inner_loop:
mov eax, outer_loops
mul dw_width ;eax = outer_loops * dw_width
add eax, inner_loops ; eax = outer_loops * dw_width + inner_loops
mov cl, (EECS205BITMAP PTR [ebx]).bTransparent
mov t_color, cl ; color = bTransparent
mov edi, (EECS205BITMAP PTR [ebx]).lpBytes
mov cl, (BYTE PTR [edi + eax]) ;color at the current position
cmp t_color, cl
je dont_draw
INVOKE DrawDinoPixel, inner_counter, outer_counter, ecx
;; returns old color
cmp eax, 0; compare old color to white
;; if they are equal there is a collision
jne dont_draw
mov isOver, 1
dont_draw:
inc inner_counter
inc inner_loops
inner_eval:
mov edx, r_bound
cmp inner_counter, edx
jl inner_loop ; if (inner_counter<r_bound) jump
inc outer_counter ; outer_counter++
inc outer_loops ; outer_loops++
outer_eval:
mov edx, b_bound
cmp outer_counter, edx
jl outer_loop ; if (outer_counter<b_bound) outer_loop
mov eax, isOver ;; return eax
ret ; Don't delete this line!!!
BasicBlitDino ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DrawPixel PROC USES eax ebx x:DWORD, y:DWORD, color:DWORD
;; screen is 640 (x) by 480 (y)
cmp x, 0 ; 0<= x<=639
jl the_end
cmp x, 639
jg the_end
cmp y, 0 ; 0<=y<=479
jl the_end
cmp y, 479
jg the_end
;; pixel at ((y * dwWidth) + x)
mov eax, 640 ; eax = dwWidth
imul eax, y ; eax = eax*y
add eax, x ; eax = y*dwWidth + x
;; the first pixel of the first row is at (0,0) at the address held in ScreenBitsPtr,
;; the next pixel of the first row (1,0) is at the next byte address and subsequent pixels in that row are at increasing addresses.
;; after (639, 0) the next byte contains the pixel color for (0,1)
add eax, ScreenBitsPtr
mov ebx, color
mov BYTE PTR[eax], bl ;; color the byte
the_end:
ret ; Don't delete this line!!!
DrawPixel ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BasicBlit PROC USES eax ebx ecx edx edi ptrBitmap:PTR EECS205BITMAP , xcenter:DWORD, ycenter:DWORD
; ptrBitmap holds the address of a EECS205BITMAP
; draw it so that the center of the bitmap appears at (xcenter, ycenter)
; use nested loops with DrawPixel
LOCAL dw_width: DWORD, l_bound: DWORD, r_bound: DWORD, t_bound: DWORD, b_bound: DWORD
LOCAL outer_loops: DWORD, inner_loops: DWORD, outer_counter: DWORD, inner_counter: DWORD, t_color: BYTE
;; edx is placeholder register
;; ebx holds ptrBitmap
mov ebx, ptrBitmap ; ebx = ptrBitmap
mov edx, (EECS205BITMAP PTR [ebx]).dwWidth ; edx = dwWidth
mov dw_width, edx ; dw_width = dwWidth
;; setting width bounds
mov ecx, edx ; ecx = dwWidth
shr ecx, 1 ; ecx = dwWidth/2
mov edx, xcenter
mov l_bound, edx ;l_bound = xcenter
sub l_bound, ecx ;l_bound = xcenter - dwWidth/2
mov r_bound, edx ;r_bound = xcenter
add r_bound, ecx ;r_bound = xcenter + dwWidth/2
;; setting height bounds
mov ecx, (EECS205BITMAP PTR [ebx]).dwHeight
shr ecx, 1 ;ecx = dwHeight/2
mov edx, ycenter
mov t_bound, edx ;t_bound = ycenter
sub t_bound, ecx ;t_bound = ycenter - dwHeight/2
mov b_bound, edx ;b_bound = ycenter
add b_bound, ecx ;b_bound = ycenter + dwHeight/2
;; loops
mov outer_loops, 0 ; outer_loops = 0
mov edx, t_bound
mov outer_counter, edx ; outer_counter = t_bound
jmp outer_eval
outer_loop:
mov inner_loops, 0 ; inner_loops = 0
mov edx, l_bound
mov inner_counter, edx ; inner_counter = l_bound
jmp inner_eval
inner_loop:
mov eax, outer_loops
mul dw_width ;eax = outer_loops * dw_width
add eax, inner_loops ; eax = outer_loops * dw_width + inner_loops
mov cl, (EECS205BITMAP PTR [ebx]).bTransparent
mov t_color, cl ; color = bTransparent
mov edi, (EECS205BITMAP PTR [ebx]).lpBytes
mov cl, (BYTE PTR [edi + eax]) ;color at the current position
cmp t_color, cl
je dont_draw
INVOKE DrawPixel, inner_counter, outer_counter, ecx
dont_draw:
inc inner_counter
inc inner_loops
inner_eval:
mov edx, r_bound
cmp inner_counter, edx
jl inner_loop ; if (inner_counter<r_bound) jump
inc outer_counter ; outer_counter++
inc outer_loops ; outer_loops++
outer_eval:
mov edx, b_bound
cmp outer_counter, edx
jl outer_loop ; if (outer_counter<b_bound) outer_loop
ret ; Don't delete this line!!!
BasicBlit ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
RotateBlit PROC USES eax edx ebx esi lpBmp:PTR EECS205BITMAP, xcenter:DWORD, ycenter:DWORD, angle:FXPT
LOCAL cosa:FXPT, sina: FXPT
LOCAL t_color: BYTE, lp_bytes: DWORD, width_: DWORD, height_: DWORD, shiftX: DWORD, shiftY: DWORD
LOCAL dstWidth: DWORD, dstHeight: DWORD, dstX: DWORD, dstY: DWORD
LOCAL srcX: DWORD, srcY: DWORD, x_bit: DWORD, y_bit: DWORD
; set cosa and sina
INVOKE FixedCos, angle
mov cosa, eax ; cosa = FixedCos(angle)
INVOKE FixedSin, angle
mov sina, eax ; sina = FixedSin(angle)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;Special draw
INVOKE FixedCos, 0
cmp cosa, eax
jne continue
INVOKE FixedSin, 0
cmp sina, eax
jne continue
INVOKE BasicBlit, lpBmp, xcenter, ycenter
jmp the_end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
continue:
;; store color bitmap info
mov esi, lpBmp ; get pointer to bitmap
mov al, (EECS205BITMAP PTR[esi]).bTransparent
mov t_color, al ; set t_color
mov eax, (EECS205BITMAP PTR[esi]).lpBytes
mov lp_bytes, eax ; set pointer to colors
mov eax, (EECS205BITMAP PTR[esi]).dwWidth
mov width_, eax ; set width
mov eax, (EECS205BITMAP PTR[esi]).dwHeight
mov height_, eax ; set height
; shiftX = (EECS205BITMAP PTR [esi]).dwWidth * cosa / 2
; - (EECS205BITMAP PTR [esi]).dwHeight * sina / 2
; shiftY = (EECS205BITMAP PTR [esi]).dwHeight * cosa / 2
; + (EECS205BITMAP PTR [esi]).dwWidth * sina / 2
mov eax, width_
sal eax, 16 ; convert to FXPT
mov ebx, cosa
sar ebx, 1 ; ebx = cosa/2
imul ebx ; eax = width * cos(a)/2
mov shiftX, edx ; keeping the integer part
mov eax, height_
sal eax, 16 ; convert to FXPT
imul ebx ; eax = height * cos(a)/2
mov shiftY, edx ; keeping the integer part
mov eax, height_
sal eax, 16 ; convert to FXPT
mov ebx, sina
sar ebx, 1 ; ebx = sina /2
imul ebx
sub shiftX, edx ; shiftX = width*cosa - height*sina
mov eax, width_
sal eax, 16 ; convert to FXPT
imul ebx
add shiftY, edx ; shiftY = height*cosa + width*sina
; set dstHeight and dsWidth
mov eax, width_
add eax, height_
mov dstWidth, eax ; dstWidth = width + height
mov dstHeight, eax ; dstHeight = width + height
; loops
; outer loop init: for(dstX = -dstWidth;
neg eax
mov dstX, eax ; dstX = -dstWidth
jmp outer_eval
inner_init:
; inner loop init: for(dstY = -dstHeight; dstY < dstHeight; dstY++)
mov eax, dstHeight
neg eax ; eax = -dstHeight
mov dstY, eax
jmp inner_eval
inner_loop:
; srcX = dstX*cosa + dstY*sina
mov eax, dstX
sal eax, 16 ; convert to FXPT
imul cosa ; {edx, eax} = cosa*dstX
mov srcX, edx ; get the integer part only, srcX = dstX*cosa
mov eax, dstY
sal eax, 16 ; covert to FXPT
imul sina ; {edx, eax} = sina*dstY
add srcX, edx ; add the integer part only to srcX
; srcY = dstY*cosa – dstX*sina
mov eax, dstY
sal eax, 16 ; convert to FXPT
imul cosa ; {edx, eax} = cosa*dstY
mov srcY, edx ; get the integer part only, srcY = dstX*cosa
mov eax, dstX
sal eax, 16 ; covert to FXPT
imul sina ; {edx, eax} = sina*dstY
sub srcY, edx ; add the integer part only to srcX
; all the inner loop if statement conditions
cmp srcX, 0 ; srcX >= 0
jl inner_incr
mov ebx, width_ ; srcX < (EECS205BITMAP PTR [esi]).dwWidth
cmp srcX, ebx
jge inner_incr
cmp srcY, 0 ; srcY >= 0
jl inner_incr
mov ebx, height_ ; srcY < (EECS205BITMAP PTR [esi]).dwHeight
cmp srcY, ebx
jge inner_incr
mov ebx, xcenter ; (xcenter+dstX-shiftX) >= 0
add ebx, dstX
sub ebx, shiftX
mov x_bit, ebx ; x_bit = xcenter+dstX-shiftX
cmp ebx, 0
jl inner_incr
cmp ebx, 639 ; (xcenter+dstX-shiftX) < 639
jge inner_incr
mov ebx, ycenter ; (ycenter+dstY-shiftY) >= 0
add ebx, dstY
sub ebx, shiftY
mov y_bit, ebx ; y_bit = ycenter+dstY-shiftY
cmp ebx, 0
jl inner_incr
cmp ebx, 479 ; (ycenter+dstY-shiftY) < 479
jge inner_incr
; bitmap pixel (srcX,srcY) is not transparent
; posn = srcY*width + srcX
mov eax, srcY
mov ebx, width_
imul ebx
add eax, srcX
; get the color
add eax, lp_bytes
mov al, (BYTE PTR [eax])
and eax, 0ffh ; last byte
; check for transparency
mov dh, t_color
cmp al, dh
je inner_incr
INVOKE DrawPixel, x_bit, y_bit, eax ; finally draw the pixel
inner_incr: ; the inner loop increment, where we go if we don't draw
inc dstY
inner_eval: ; dstY < dstHeight
mov ebx, dstHeight
cmp dstY, ebx
jl inner_loop
inc dstX ; outer loop increment
outer_eval: ; dstX < dstWidth
mov ebx, dstWidth
cmp dstX, ebx
jl inner_init
the_end:
ret ; Don't delete this line!!!
RotateBlit ENDP
END