;; Example 10 - Tile Maps
;;
;; RAPTOR USER FILE
;;
;; Note: 	This file, and all other files, are tabulated for Notepad++
;;			Tabulation might be incorrect in other editors.
;;			use Notepad++ for maximum readability. 
;;


;; DO NOT MODIFY THE FOLLOWING LINES

			include				"../../JAGUAR.INC"										; Include JAGUAR system labels
			include				"../../RAPTOR/INCS/RAPTOR.INC"							; Include RAPTOR library labels
			include				"../../U235SE.021/U235SE.INC"							; Include U235SE library labels

			.text							

;; YOUR APPLICATION GOES HERE
;; MODIFY AWAY!
						
;; VIDEO SETUP EQUATES
			
raptor_video_mode				equ				vidRGB16								; tell RAPTOR we want RGB 16 mode
raptor_video_enabled			equ				vidENABLE								; tell RAPTOR we want to Video ENABLED
raptor_video_VARMOD				equ				vidVARMOD_OFF							; tell RAPTOR we want VARMOD disabled
			
;; MAP MODULE SETUP EQUATES
			
raptor_first_map_object			equ				0										; Point RAPTOR to the first object of the map data
raptor_map_tiles_per_y			equ				8										; tell RAPTOR the map is 8 tiles high
raptor_map_tiles_per_x			equ				11										; tell RAPTOR the map is 11 tiles 
raptor_map_height				equ				18										; tell RAPTOR the map height is 659 tiles
raptor_map_width				equ				22										; tell RAPTOR the map width is 14 tiles
raptor_tilesize_x				equ				32										; tell RAPTOR the map tilesize (x) is 32 pixels
raptor_tilesize_y				equ				32										; tell RAPTOR the map tilesize (y) is 32 pixels
raptor_tilelinesz				equ				raptor_tilesize_x/2						; calculate the offset to next line of tile data
raptor_tilesize					equ				raptor_tilelinesz*raptor_tilesize_y		; calculate the byte size of a single tile

;; PARTICLE MODULE SETUP EQUATES

raptor_particle_buffer_width	equ				368										; tell RAPTOR the Particle / Text Buffer is 368 pixels wide
raptor_particle_buffer_height	equ				240										; tell RAPTOR the Particle / Text Buffer is 240 pixels high
raptor_particle_pixels			equ				50										; tell RAPTOR the maximum number of particles
raptor_particle_drift_x			equ				0										; tell RAPTOR the Particle Drift Factor (x)
raptor_particle_drift_y			equ				0										; tell RAPTOR the Particle Drift Factor (y)

;; some human friendly names

LIST_display					equ				0										; the first display list
CLUT_map						equ				0										; the palette for the map is CLUT 0 (col 0-15)
ID_map							equ				0										; the first object in the map has a RAPTOR ObjID of 0

;;
;; The following lines **MUST** be at the start of EVERY RAPTOR APPLICATION
;; They configure the .OBJ file for use
;;	
			move.l	#raptor_video_mode,raptor_vidmode
			move.l	#raptor_video_enabled,raptor_videnable
			move.l	#raptor_video_VARMOD,raptor_varmod
			move.l	#raptor_first_map_object,raptor_maptop_obj
			move.l	#raptor_tilesize_x,raptor_tiles_x
			move.l	#raptor_tilesize_y,raptor_tiles_y
			move.l	#raptor_map_tiles_per_x+2,raptor_tilesperx
			move.l	#raptor_map_tiles_per_y+1,raptor_tilespery
			move.l	#raptor_map_width,raptor_mapwidth
			move.l	#raptor_map_height,raptor_mapheight
			move.l	#raptor_particle_pixels,raptor_maxparts
			move.l	#raptor_tilesize_x-1,raptor_tilerem_mask
			move.l	#raptor_particle_buffer_width,raptor_partbuf_x
			move.l	#raptor_particle_buffer_height,raptor_partbuf_y
			move.l	#raptor_particle_drift_x,raptor_pdriftx
			move.l	#raptor_particle_drift_y,raptor_pdrifty
			move.l	#top_of_bss,raptor_top_of_bss
			move.l	#_trashram,raptor_trashram
			move.l	#RAPTOR_MTtrash,raptor_MTwork
			move.l	#RAPTOR_user_vbi,raptor_uvbi_jmp
			move.l	#RAPTOR_POST_Object_List,raptor_poobjl
			move.l	#RAPTOR_PRE_Object_List,raptor_probjl
			move.l	#RAPTOR_font8x8,raptor_8x8_addr
			move.l	#RAPTOR_font8x16,raptor_8x16_addr
			move.l	#RAPTOR_font16x16,raptor_16x16_addr
			move.l	#RAPTOR_particle_palette,raptor_partipal
			move.l	#RAPTOR_particle_gfx,raptor_pgfx
			move.l	#RAPTOR_particle_gfxe,raptor_pgfxe
			move.l	#RAPTOR_sprite_table,raptor_spritetab
			move.l	#RAPTOR_particle_table,raptor_partitab
			move.l	#RAPTOR_MT_app_name,raptor_mtapp
			move.l	#RAPTOR_MT_file_name,raptor_mtfn
			move.l	#raptor_init_table,raptor_inittab
			move.l	#RAPTOR_samplebank,raptor_samplebank_ptr
			move.l	#RAPTOR_bmp_tileset,raptor_mapbmptiles		; <-------------------------- NOW WE SET THIS <-------------------------
			jsr		RAPTOR_HWinit														; Setup Jaguar hardware / install RAPTOR library

;; Convert the tiles for the map from Windows BMP to Jaguar BMP format
			
			lea		RAPTOR_bmp_tileset,a0												; point to tileset BMP
			lea		_trashram,a1														; work ram
			jsr		RAPTOR_GFXConvert													; convert to Jaguar format
			
;; Copy the palette to the correct CLUT
			
			lea		$f00400+(CLUT_map*32),a1											; CLUT 0
			jsr		RAPTOR_move_palette													; and copy the palette to correct CLUT
			
;; Init U235
			
			jsr		RAPTOR_U235init														; init the U235 Sound Engine
			lea		mymodule,a0															; point it at a module
			jsr		RAPTOR_U235setmodule												; setup the module			
			move.l	#48,U235SE_music_vol												; set the volume
			jsr		RAPTOR_U235gomodule_mono											; and start it playing

			clr.l	U235SE_pad1															; clear pad input flags

;; Configure the tile map
			
			move.l	#map1,raptor_mapindex												; tell RAPTOR where the tile list for the map is located		
			jsr		RAPTOR_init_map_objs												; populate the RAPTOR objects correctly for a map
	
;; get something on the screen
	
			jsr		RAPTOR_start_video													; start video processing
			move.l	#LIST_display,d0													; set RAPTOR to display initial RAPTOR list
			jsr		RAPTOR_setlist														; tell RAPTOR which list to process
									
;; set the initial map position (in pixels)
									
			move.l	#$00000000,raptor_map_position_x									; 16.16 value for X
			move.l	#$00000000,raptor_map_position_y									; 16.16 value for Y
			jsr		RAPTOR_map_set_position												; tell RAPTOR where the top left of the map needs to be (in the logical map)
	
;; Main Loop
			
LOOP:		jsr		RAPTOR_wait_frame_UPDATE_ALL										; Sync to the frame

.up:		move.l	U235SE_pad1,d0														; get PAD1 status (from U235)
			btst	#U235SE_BBUT_UP,d0													; UP?
			beq		.down
			sub.l	#$00040000,raptor_map_position_y									; subtract from Y position
			
.down:		btst	#U235SE_BBUT_DOWN,d0												; DOWN?
			beq		.left
			add.l	#$00040000,raptor_map_position_y									; add to Y position
			
.left:		btst	#U235SE_BBUT_LEFT,d0												; LEFT?
			beq		.right
			sub.l	#$00040000,raptor_map_position_x									; subtract from X position

.right:		btst	#U235SE_BBUT_RIGHT,d0												; RIGHT?
			beq		.done
			add.l	#$00040000,raptor_map_position_x									; add to X position

.done:		tst.l	raptor_map_position_y												; gone off top of map?
			bpl		.chk_upper_limit_y
			clr.l	raptor_map_position_y												; then reset!
.chk_upper_limit_y:
			cmp.w	#raptor_tilesize_y*(raptor_map_height-raptor_map_tiles_per_y+2),raptor_map_position_y			; gone off bottom of map?
			blt		.chk_x
			move.l	#raptor_tilesize_y*(raptor_map_height-raptor_map_tiles_per_y+2)<<16,raptor_map_position_y		; then reset!
			
.chk_x:		tst.l	raptor_map_position_x												; gone off left of map?
			bpl		.chk_upper_limit_x
			clr.l	raptor_map_position_x												; then reset!
			
.chk_upper_limit_x:
			cmp.w	#raptor_tilesize_x*(raptor_map_width-raptor_map_tiles_per_x),raptor_map_position_x			; gone off right of map?
			blt		.chk_done
			move.l	#raptor_tilesize_x*(raptor_map_width-raptor_map_tiles_per_x)<<16,raptor_map_position_x		; then reset!
.chk_done:
			jsr		RAPTOR_map_set_position												; tell RAPTOR to update the map display
			
			bra		LOOP																; Loop around!
	
;;
;; RAPTOR user VBI vector
;;

RAPTOR_user_vbi:	rts																	; we're not using this, nothing tied to the vblank

;;
;; Creating Non-RAPTOR objects around the list
;;		

RAPTOR_PRE_Object_List:																	; No unmanaged objects before the list
			rts

RAPTOR_POST_Object_List:																; No unmanaged Objects after the list									
			rts
						
							include		"_RAPPIXL.S"									; RAPTOR particle module user data
							include 	"_RAPINIT.S"									; RAPTOR object user data
							include 	"_RAPU235.S"									; RAPTOR u235se user data

;;
;; The music
;;			

							.dphrase
mymodule:					incbin	"../DATAFILE/AUDIO/BEEBRIS.MOD" 					; module to play
							.dphrase
				
;;
;; The Map Data
;;
				
							.dphrase
map1:						include	"../DATAFILE/maps/mapindex.s"						; list of tile values
							.dphrase
RAPTOR_bmp_tileset:			incbin	"../DATAFILE/MAPS/town16.bmp"						; Windows BMP with map tiles
							.dphrase
								
;;
;; Graphics
;;
							.dphrase
							dc.l	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
				
RAPTOR_font8x8:				incbin	"../../RAPTOR/FONTS/F_8x8.BMP"						; User defined 8x8 fonts
							.dphrase
RAPTOR_font8x16:			incbin	"../../RAPTOR/FONTS/F_8x16.BMP"						; User defined 8x16 fonts
							.dphrase
RAPTOR_font16x16:			incbin	"../../RAPTOR/FONTS/F_16x16.BMP"					; User defined 16x16 fonts
							.dphrase
RAPTOR_particle_palette:	incbin	"../../RAPTOR/FONTS/PARTIPAL.BMP"					; User defined palette for fonts and particles
							.dphrase
									
;;
;; BSS SECTION
;;

							.bss
top_of_bss:

RAPTOR_MTtrash:				.ds.b	16384																		; Workspace for MemoryTrack routines

							.dphrase
RAPTOR_particle_gfx:		.ds.b	(raptor_particle_buffer_width/2)*raptor_particle_buffer_height				; particle FX buffer
RAPTOR_particle_gfxe:			
							.dphrase
RAPTOR_sprite_table:		.ds.b	sprite_max*sprite_tabwidth													; RAPTOR sprite database
							.dphrase
RAPTOR_particle_table:		.ds.b	raptor_particle_pixels*particle_tabwidth									; RAPTOR particle database
							.dphrase
							
_trashram:
								
;END OF FILE

				
