		  Phobos' Turbo Pascal Demo Coding Tutors
                           [C++ Update by Karma]
                           Tutor 4 - The palette

Decemember 30th 1996

     Hi, this is a conversion of Phobos Pascal tutes to C++.
     I've converted all of his pascal code into C++ but I have left
     the original text unaltered.  So I take *no* credit for this
     tutorial, so you still have to mail questions to phobos ;-)
     Anyway the code was written and tested in Borland C++ 3.1
     Any comments by me will be in [ ]. -Karma-

Introduction
-------------

Hello, and welcome to the first tutor of 1997!  I expect you've had enough of 
people say Happy New Year so I won't do it again.  Sorry this tutor has 
taken so long to arrive, certain things out of my control caused this.  Also 
people I'm a lazy bastard ;-)  Okay, this tutor is all to do with the palette. 
How to change the colours, and a few interesting things to do with it.

The palette
------------

As you probably know, you have 256 colours at your disposal.  Those of you 
that have been fiddling with some code yourselfs after reading my previous 
tutors will have realised, that the base 256 colour palette ain't all that 
great.  So how can we change it?  For this we need to know how the colours are 
constructed.

From your art lessons at school, you might remember that the primary colours 
are red, yellow, and blue.  You might also know that computer screens don't 
work like this, their primary colours are red, _green_, and blue.  So all the 
colours on your computer screen are made from these three primary colours.  
The intensity of these three colours gives us new colours.  The intensity of 
each primary colour ranges from 0 (least intesity) to 63 (most intesity).  
Here's some examples.  

	Red   Green   Blue
        ---   -----   ----
         0      0      63      =  Bright Blue
         63     0      63      =  Purple
 	 0      0      0       =  Black
         40     40     40      =  Medium grey

Okay, so know you how the colours are made up, how do we write some code that 
changes the colours?

Changing the colours
---------------------

For this we use a procedure from the GFX3 tpu called 'pal'.  This is how we'd 
set colour 5 to purple.

			pal (5, 63, 0, 63)
                            /   /   \   \
                          /    /     \    \
  The colour we're using/     /       \     \The blue intensity
                             /         \
           The red intensity/           \The green intensity

Lets write a little program that colours the screen purple then.

------------------------------------------------------------------
[Pascal]

USES crt,gfx3;
BEGIN
  setmcga;
  pal (5,63,0,63);
  cls (vga,5);
  READKEY;
END.

[C++]

#include <conio.h>
#include "gfx.cpp"

void main() {
  setmcga();
  pal(5,63,0,63);
  cls(vga,5);
  getch();
 }
-------------------------------------------------------------------

That works, and we've also learnt a new command - cls.  Cls is another
procedure from the gfx3 tpu.  It's similair to clrscr in that it clears the
screen, but cls only works in vga mode, and allows you to say what colour to
clear the screen to.  Like line and putpixel we have to say where to we want
clear the screen.  For now we'll just put VGA. i.e the vga screen.

Okay, an interesting thing to do with the purple screen would be to veiry it's
intensity, to add some movement.  We can do this by using a loop, changing the
red and blue values each time.  Look at this :

-------------------------------------------------------------------
[Pascal]

{$X+}
USES crt,gfx3;

PROCEDURE purple;
VAR loop : INTEGER;
BEGIN
  REPEAT
    FOR loop := 0 TO 63 DO BEGIN
      pal (5,loop,0,loop);
      cls (vga,5);
    END;
    FOR loop := 63 DOWNTO 0 DO BEGIN
      pal (5,loop,0,loop);
      cls (vga,5);
    END;
  UNTIL keypressed;
END;

BEGIN
  setmcga;
  purple;
  settext;
END.

[C++]

#include <conio.h>
#include "gfx.cpp"

void purple();

void main() {
  setmcga();
  purple();
  settext();
 }

void purple() {
  int loop;
   do {
    for(loop=0;loop<63;loop++) {
      pal(5,loop,0,loop);
       cls(vga,5);
      }
     for(loop=63;loop>0;loop--) {
       pal(5,loop,0,loop);
	cls(vga,5);
       }
     } while (!kbhit());
  }
-----------------------------------------------------------

Pallette rotation
------------------

Okay, the above program rotated the palette for just a single colour.  Colour
5.  However, we could rotate a whole spectrum of colours all at the same
time to make a nice effect.  Take a look at the following code.

----------------------------------------------
[Pascal]

FOR loop := 0 DO 63 DO BEGIN
  pal (loop,loop,0,loop);
  line (0,loop,319,loop,loop,vga);
END;

[C++]

 for(loop=0;loop<63;loop++) {
  pal(loop,loop,0,loop);
   line(0,loop,319,loop,loop,vga);
  }
----------------------------------------------

This loop is repeated 64 times (0 to 63).  The first thing it does it set up
the relevant colour, somewhere between black and bright purple.  The next line
draws a line in the current colour.  The position of the line is also decided
by the loop variable.  The y coords of the line is between 0 to 63.

So now that we've setup our colour bar, lets get it rotating!

--------------------------------------------------------------
[Pascal]

{$X+}
USES crt,gfx3;

PROCEDURE rotate;
VAR loop : INTEGER;
    r,g,b : BYTE;
BEGIN
  REPEAT
    FOR loop := 0 TO 63 DO BEGIN
      getpal (loop+1,r,g,b);
      pal (loop+1,r+1,0,b+1);
      DELAY(1);
    END;
  UNTIL KEYPRESSED;
END;

PROCEDURE draw_bar;
VAR loop : INTEGER;
BEGIN
  FOR loop := 0 TO 63 DO BEGIN
    pal (loop+1,loop,0,loop);
    line (0,loop,319,loop,loop,vga);
  END;
END;

BEGIN
  setmcga;
  draw_bar;
  rotate;
  settext;
END.

[C++]

#include <conio.h>
#include "gfx.cpp"

void rotate();
void draw_bar();

void main() {
  setmcga();
  draw_bar();
  rotate();
  settext();
 }

void rotate() {
  int loop;
  byte r, g, b;
    do {
       for(loop=0;loop<63;loop++) {
	getpal(loop+1,r,g,b);
	 pal(loop+1,r+1,0,b+1);
	delay(1);
	}
      } while (!kbhit());
  }

void draw_bar() {
  int loop;
   for(loop=0;loop<63;loop++) {
    pal(loop+1,loop,0,loop);
     line(0,loop,319,loop,loop,vga);
    }
  }
--------------------------------------------------

GETPAL
-------
This gets the red, green, and blue value of a particular colour.

In Closing
-----------

Okay, this tutor has hopefully shown you how to manipulate the palette to your
hearts content!  Remember, the point of these tutors is to give you the
knowledge to create your own stuff.  My code is just the starting point.  My
aim is to break down the barrier of _lack of knowledge_ which prevents people
from realising their true coding potential, by given you the knowledge.
Hopefully by the end of these tutors the only thing restricting you will be
your imagination!

Things to do
-------------
Write a program that makes raster bars move up/down the screen.  As usual,
just mail me if you need help.  Also, if you code this, please send me a copy
as I'd love to see your creation!

Next Week...
-------------
Erm....  I dunno, any ideas?  Is there anything people want me to cover?  Mail
me with suggestions.  If not, I think I'll do some stuff on vertual screens
nextweek.
[ If its okay with phobos, I will continue to convert these tutors to C++ ]
  
K, Phobos is outta here!
[Me too!]

Email : ci.phobos@dial.pipex.com
