0

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...

Cœur
  • 37,241
  • 25
  • 195
  • 267
A.Arthur
  • 3
  • 4
  • So.. set the fill color for every square from table of colors[(x^y)1]. You want to keep the grid lines visible, or not? – Ped7g Jan 08 '17 at 14:40
  • Well, just like the picture, if we put differents color, we can still see the lines of the grid, isnt it ? I thought about doing something like when D5 and D6 are pair, I put one color, and when its odd I put another one color. (with AND) – A.Arthur Jan 08 '17 at 14:53
  • The picture looks like small thumbnail, I don't see any lines there. Why I'm asking... visible thin grid = your current solution. Invisible grid = needs to set also "pen" color, and then the 1 pixel overlap of squares is probably not wanted to have all of them of equal size. Anyway, meanwhile I did something... will add it as answer soon. .. about even/odd: Yes, that's what (m^n)&1 does (XOR "oddness" of column/row, then keeps only "crossed oddness" by AND). – Ped7g Jan 08 '17 at 15:09
  • Ok, that is what I thought to how to do it – A.Arthur Jan 08 '17 at 15:16

1 Answers1

0

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
Ped7g
  • 16,236
  • 3
  • 26
  • 63
  • Thank you ! I thought it was easier than this (just adding few lines) but yeah, I still understand it ! – A.Arthur Jan 08 '17 at 15:48
  • @A.Arthur well, the XOR version is just few lines, but it may be somewhat tricky to understand if you are not familiar with `a = b xor (a xor b)` and `b = a xor (a xor b)` trick to alternate between two values. Then again, watching it few times in debugger, or doing the calculation on the paper should fix that. :) – Ped7g Jan 08 '17 at 15:52
  • yeah, because this question was into an exam 3 years ago, and it feels like impossible with what we learn at school to do that (we do the code on paper without computer or something else) – A.Arthur Jan 08 '17 at 15:54
  • @A.Arthur If you can't recall the xor trick, you can still do good old `CMP #color1,D1` ... branching to set alternate colours. But that would mean ~6 lines replacement instead of single `EOR`, so lot more writing on paper. But the logic is brain-dead obvious? You should be able to produce at least this. And another branch-init ahead of every row, to start either with first or second colour, depending on isOdd(row_index). I mean, if you can SOMEHOW do it on paper in high-level, then you can write it in ASM too. The elegance comes from working first on the high-level to find some formula. – Ped7g Jan 08 '17 at 16:03