How to put 2 different color in a grid?

247 views Asked by At

I try to do a little exercise for myself, but I'm stuck. I want to do a grid (that is ok) where one shot in two, the color changes (like in this picture : http://puu.sh/tfgHm/b07843323d.jpg ). I already have this :

    ORG $800
START: 
    MOVE.w #050,D2
    MOVE.w #100,D4
    MOVE.w #0,D6
GRIDLOOP:  
    MOVE.w #0,D5                
    MOVE.w #050,D1        
    MOVE.w #100,D3      
LINE:
    JSR DRAW_FILL_RECT *task 87
    ADD.w  #050,D1
    ADD.w  #050,D3
    ADD.w #1,D5
    CMP.w #6,D5
    BNE LINE
    ADD.w #50,D2
    ADD.w #50,D4
    ADD.w #1,D6
    CMP.w #6,D6
    BNE GRIDLOOP
    RTS

    INCLUDE 'DIRECTORY.X68'

    END START

I tried few things, but well, it didn't work correctly...

1

There are 1 answers

4
Ped7g On

Added "fill" colours alternating (and made it compilable in Easy68k without include):

    ORG $800
BOARD_SIZE_X    EQU     6
BOARD_SIZE_Y    EQU     8
GRID_OFFSET_X   EQU     100
GRID_OFFSET_Y   EQU     50
GRID_SIZE_X     EQU     50
GRID_SIZE_Y     EQU     25
FIRST_SQUARE_COLOR  EQU $004040FF
SECOND_SQUARE_COLOR EQU $0040FF40

START:
    LEA     GridColorsTable,A0
    MOVEQ   #BOARD_SIZE_Y-1,D6              ; rows counter
    MOVE.w  #GRID_OFFSET_Y,D2               ; Y1
    MOVE.w  #GRID_OFFSET_Y+GRID_SIZE_Y,D4   ; Y2
GridLoopRows:  
    MOVEQ   #BOARD_SIZE_X-1,D5              ; cells per row counter (columns)
    MOVE.w  #GRID_OFFSET_X,D7               ; X1 will reside in D7 (color is using D1 too)
    MOVE.w  #GRID_OFFSET_X+GRID_SIZE_X,D3   ; X2
GridLoopCells:
    ; set fill color
    MOVE.w  D5,D1
    EOR.w   D6,D1
    AND.w   #1,D1                           ; D1 = 0 or 1
    LSL.w   #2,D1                           ; *4
    MOVE.L  (A0,D1.w),D1                    ; fetch color value from table by that index
    MOVEQ   #81,D0                          ; set fill color (task 81)
    TRAP    #15
    MOVE.w  D7,D1                           ; restore X1 before rectangle draw

    ;JSR DRAW_FILL_RECT *task 87
    MOVEQ   #87,D0
    TRAP    #15

    ADD.w   #GRID_SIZE_X,D7
    ADD.w   #GRID_SIZE_X,D3
    DBRA    D5,GridLoopCells
    ADD.w   #GRID_SIZE_Y,D2
    ADD.w   #GRID_SIZE_Y,D4
    DBRA    D6,GridLoopRows
    SIMHALT             ; halt simulator
    RTS

;    INCLUDE 'DIRECTORY.X68'

GridColorsTable:        ; two colors for the grid
    DC.L    FIRST_SQUARE_COLOR, SECOND_SQUARE_COLOR
    ; when grid size is like 7x6, then first square is the second value (swap order in this table)

    END START

(the grid definition is intentionally 6x8 and not-square, to verify I put the X/Y constants at correct places (with symmetrical values a mistake would go unnoticed)).

Looks like this:

chessboard


Second version without memory table, as you are alternating between two values, which means XOR is all you need...

    ORG $800
BOARD_SIZE_X    EQU     6
BOARD_SIZE_Y    EQU     8
GRID_OFFSET_X   EQU     100
GRID_OFFSET_Y   EQU     50
GRID_SIZE_X     EQU     50
GRID_SIZE_Y     EQU     25
FIRST_SQUARE_COLOR  EQU $004040FF
SECOND_SQUARE_COLOR EQU $0040FF40
SWITCH_COLOR    EQU     (FIRST_SQUARE_COLOR^SECOND_SQUARE_COLOR)

START:
    MOVE.l  #FIRST_SQUARE_COLOR,D1          ; color for first square
    MOVEQ   #BOARD_SIZE_Y-1,D6              ; rows counter
    MOVE.w  #GRID_OFFSET_Y,D2               ; Y1
    MOVE.w  #GRID_OFFSET_Y+GRID_SIZE_Y,D4   ; Y2
GridLoopRows:  
    MOVEQ   #BOARD_SIZE_X-1,D5              ; cells per row counter (columns)
    MOVE.w  #GRID_OFFSET_X,D7               ; X1 will reside in D7 (color is using D1 too)
    MOVE.w  #GRID_OFFSET_X+GRID_SIZE_X,D3   ; X2
GridLoopCells:
    MOVEQ   #81,D0                          ; set fill color (task 81)
    TRAP    #15
    EOR.l   #SWITCH_COLOR,D1                ; alternate between two colors
    EXG.l   D7,D1                           ; D1 = X1, D7 = color
    MOVEQ   #87,D0      ;JSR DRAW_FILL_RECT *task 87
    TRAP    #15
    EXG.l   D7,D1                           ; D1 = color, D7 = X1
    ADD.w   #GRID_SIZE_X,D7
    ADD.w   #GRID_SIZE_X,D3
    DBRA    D5,GridLoopCells
    EOR.l   #SWITCH_COLOR,D1                ; alternate color for "even" board_size_x
        ; for "odd" board_size_x comment out the EOR, color is already "alternative" then
    ADD.w   #GRID_SIZE_Y,D2
    ADD.w   #GRID_SIZE_Y,D4
    DBRA    D6,GridLoopRows
    SIMHALT             ; halt simulator
    RTS

    END START