Close

Drawing a map

A project log for 1 kB Arduino Roguelike

1 kB roguelike using an Arduino and 0.96" OLED

radekRadek 12/03/2016 at 18:550 Comments

The map currently is 8x8 bits with a floor and wall tile. 8 bytes are used to represent a map. Each byte codes for one line horizontally. The map shown in the main project image is the following:

wall:		.db 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
floor:		.db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
main_map:	.db 0xff, 0x81, 0x81, 0x1, 0x81, 0x81, 0x81, 0xfb

To copy this map to the display buffer the following code is used:

draw_map:
	ldi		xl, low(buffer)
	ldi		xh, high(buffer)
	ldi		yl, low(buffer)
	ldi		yh, high(buffer)
	ldi		zl, low(2*main_map)
	ldi		zh, high(2*main_map)
	movw	        r25:r24,z

	ldi		r21, 128
	ldi		r22,0
	ldi		r18, 8		
tile_no:
	movw	        y,x	
	movw	        z,r25:r24	; load map
	lpm		r16, z	    ; bit map/line value
	adiw	        r25:r24, 1
	ldi		r17, 8	    ; 8 bit/tiles per line
draw_line:	

	lsl		r16
	brcs	        draw_wall
draw_floor:
	ldi		zl, low(2*floor)
	ldi		zh, high(2*floor)
	lpm		r19, z
	rcall	        draw_tile
	rjmp	        end_tile
draw_wall:
	ldi		zl, low(2*wall)
	ldi		zh, high(2*wall)
	lpm		r19, z
	rcall	        draw_tile
end_tile:

	dec		r17
	brne	        draw_line

	rcall	        next_line

	dec		r18
	brne	        tile_no
	ret

Discussions