Shop OBEX P1 Docs P2 Docs Learn Events
A few Subs to make Color Graphics a bit easier — Parallax Forums

A few Subs to make Color Graphics a bit easier

Jim FouchJim Fouch Posts: 395
edited 2006-10-09 16:33 in Propeller 1
Here's a few subs I added to my Top Spin file to make working with the Color Palettes a bit easier

Pub SetAreaColor(X1,Y1,X2,Y2,ColorIndex)|DX,DY
  Repeat DX from X1 to X2
    Repeat DY from Y1 to Y2
      SetTileColor(DX,DY,ColorIndex)
    
Pub SetTileColor( x, y, ColorIndex)
   screen[noparse][[/noparse]y * tv_hc + x] := display_base >> 6 + y + x * tv_vc + ((ColorIndex & $3F) << 10)
 
Pub SetColorPalette(ColorIndex,Color1,Color2,Color3,Color4)
  colors[noparse][[/noparse]ColorIndex] := (Color1) + (Color2 << 8) +  (Color3 << 16) + (Color4 << 24)       


Here's an example of how they are called...

CON
 
  ' Colors
  CL_Black      = $02
  CL_Grey1      = $03
  CL_Grey2      = $04
  CL_Grey3      = $05
  CL_Grey4      = $06
  CL_White      = $07
  CL_Blue       = $0A
  CL_DarkBlue   = $3A
  CL_DarkGreen  = $4A
  CL_Red        = $48
  CL_Brown      = $28
  CL_Yellow     = $9E
  CL_Purple     = $88
  CL_DarkPurple = $EA
  ' Use Graphics_Palette to find more colors
 
PUB Start....
 
  SetColorPalette(0,CL_Black,CL_White,CL_Red,CL_Blue)
  SetColorPalette(1,CL_Black,$F8,CL_Red,CL_Blue)
  SetColorPalette(2,CL_Yellow,$F8,CL_Red,CL_Blue)          
  SetColorPalette(3,CL_Black,CL_White,$48,$AA) ' Valentine One          
  ' Can have up to 64 Color Palettes
 
  'init tile screen
  SetAreaColor(0,0,TV_HC-1,TV_VC-1,0) ' Defaults all Screen to Color Palette #0
  SetAreaColor(0, 0, 1,1,1) ' C0R0..C1..R1 to Palette #1
  SetAreaColor(6,1,13,5,2)  ' Sets Tiles C6R1..C15R5 to Palette #2
  SetAreaColor(14,0,15,1,1) ' C14R0..C15R1 to Palette #1
  SetAreaColor(6,6,11,8,3)  ' C6R6..C11R8 to Palette #3



Hope this helps make the colors a bit easier to understand. I know it took me a day just to figure how the examples worked. All the information is there. You just have to look at the examples and the objects to find the details.

Basically, you can have 64 different palettes, and 192 tiles. Any Tile can point to any one of the Palettes you have defined. Each Palette defines 4 colors. So you could have a screen with up to 256 different colors. These functions make it a bit easier to understand and read/change you code.

Here is another little sub I wrote to place a Grid over your display to help define the tiles
Pub ShowTiles| x, y
  ' This Sub Will Draw a Grid Outlining each Tile
  gr.color(1)
  Repeat X From 0 to X_tiles-1
      gr.Plot(X * 16,0)
      gr.line(X * 16,y_Tiles * 16)  
  Repeat Y From 0 to y_Tiles-1
      gr.Plot(0,Y*16)
      gr.line(X_Tiles * 16,Y * 16)

·I normally have the line just before I do Gr.Copy call it if I have a button pressed, so I can bring it over the current version of what I have on the screen
Main code....

    if INA[noparse][[/noparse]1]==1 
      ShowTiles                                       
    
    gr.copy(display_base) 'copy bitmap to display



All I can say is this Propeller is one awsome chip. I'll have a pretty cool sample video in the new few weeks that will show pretty good use of the Propeller's power and ease of use. I attached a screen sample.· ;-)



Jim
1200 x 900 - 109K
1200 x 900 - 137K

Comments

  • APStech-AttilaAPStech-Attila Posts: 38
    edited 2006-09-15 08:45
    Hi Jim,

    ·This was the thing that was missing from thr Graphics unit!

    · Excellent work! Thanks.



    Attila
  • Jim FouchJim Fouch Posts: 395
    edited 2006-09-15 15:28
    I stand corrected. You can have more than 64 Palettes. The graphics example used 64. But you can have as little or upto 192 if you wanted. It is just basically a memory location holding the color information for the 4 colors.

    To save memory, just reserve the number of Palettes you are going to use. In my application I lowered it to 5 and saved ~58 Longs. I'm at the point that almost every long counts...· :-)

    Jim
  • Ym2413aYm2413a Posts: 630
    edited 2006-09-18 18:59
    Wow, Now this is truely useful.
  • MC-BerndMC-Bernd Posts: 8
    edited 2006-10-09 16:33
    Hello Jim,

    would you post the whole Spin-program with the grid !

    Thanks
    Bernd

    hop.gif
Sign In or Register to comment.