Solved

256 Color in 'char Image[]' array

Posted on 2004-03-22
3
867 Views
Last Modified: 2013-12-26
i have a sprite (4 x 4 pixels) defined in a char array "char Image[]" as given below for example, having colors Red (Value 12) and Yellow (Value 14).  I am writing directly to VGA memory in Mode 13h which can support upto 256 colors.  I know the colors having values from 1 to 16 which can be used in this array.  The question is if i want to choose any other color (other than 16 colours) from the range of 256 colors, then what value should i give it in the array so that i can get that displayed on the screen.  any lookup table? or something. pls. note that all of my sprites are defined using char arrays.  I am using Turbo C 3.0 DOS complier.

char Image[] =
{
  12,12,12,12,
  12,12,12,12,
 14,14,,14,14,
 14,14,,14,14,
};

T. P. Pramod Kumar
New Delhi
INDIA
0
Comment
Question by:tppramod
  • 2
3 Comments
 
LVL 5

Expert Comment

by:info_expert
ID: 10660596
Well,
There are actually 18 bit DAC registers, the values in the registers is set and mapped to the basic 16 registers. It is all done by the interrupts. So you can not simply show the color by giving its number.

One suggestion is that you better get rid of this kind of color programming in DOS and use some advance tools for graphics adventures..

Further questions?
0
 
LVL 1

Accepted Solution

by:
bredbored earned 100 total points
ID: 10683312
VGA mode 013h supports 256 simultaneous 18-bit colors.  You can display 256 out of 262,144 colors at once on the screen at any time.  These 256 colors are defined in a user-settable palette stored on the VGA.  The default VGA palette is reset whenever you set the display to mode 013h.  Writing a value from 0-255 into video memory selects a palette index for the corresponding pixel, and whatever the current color is at that index is displayed on the screen.  Therefore you can change the color of a pixel on the screen either by writing a new index to the appropriate memory location or by changing the color corresponding to the appropriate index.  This second method is called palette animation.

In other words, the numbers in your array are indices into the 256-color VGA palette.  You may assign any color you like to any of these indices.  The BIOS provides two methods for setting the colors in the palette: functions 01010h and 01012h of int 010h.

It's also possible to set the palette by accessing the VGA hardware directly.  However, you don't need to unless you're going to be changing the palette a lot and even then you may not care unless your processor is running in 32-bit addressing mode, and even then you may not care unless your computer is very old and slow.  Since you're using Turbo C++ 3.0, I think it's safe to assume that you're going to be writing DOS 16-bit code.  If you decide your computer is very old and slow you can look for information on the VGA registers.

#include <conio.h>

// AX = function                  (01010h)
// BX = palette index            (0 - 255)
// CH = intensity for green      (0 - 63)
// CL = intensity for blue      (0 - 63)
// DH = intensity for red      (0 - 63)

// You can write a simple function to do this
// in Turbo C++ 3.0 as follows:

typedef unsigned char Byte;

typedef struct
{
      // Alignment might be a problem in some compilers,
      // but not in this one!

      Byte red, green, blue;

}      Color;

void SetPaletteEntry( Byte index, Color color )
{
      // Note that for some reason, Turbo C++ 3.0 requires
      // that you place the opening brace on the same line
      // as the "asm" statement.

      asm {
            mov dh, byte ptr color
            mov cl, byte ptr color[ 2 ]
            mov ch, byte ptr color[ 1 ]
            mov bl, index
            xor bh, bh
            mov ax, 01010h
            int 010h
      }
}

// AX = function (01012h)
// BX = starting palette index
// CX = number of indices to set
// ES:DX ->      table of 3 * CX bytes where each 3 byte group
//                   represents one byte each of red, green and
//                  blue (0 - 63)

// This one's slighly more complicated because of the
// pointer, which must be declared as "far" in case this
// is a small memory model.

void SetPaletteEntries( Byte start, Byte count, Color far *pPalette )
{
      asm {
            mov ax, word ptr pPalette[ 2 ]
            mov es, ax
            mov dx, word ptr pPalette
            mov cl, count
            xor ch, ch
            mov bl, start
            xor bh, bh
            mov ax, 01012h
            int 010h
      }
}

int main()
{
      // Define a simple palette

      Color yellow = {
            63,
            63,
            0
      };

      Color palette[ 3 ] = {
            63,                              // Red
            0,
            0,

            0,
            63,                              // Green
            0,

            0,
            0,
            63                              // Blue
      };

      // Set mode 013h
      // and place 3 pixels in the top left corner using
      // the palette indices that will be set

      // Video memory starts at A000:0000 in 013h

      asm {
            mov ax, 00013h
            int 010h

            push ds

            mov ax, 0A000h
            mov ds, ax

            mov byte ptr [ 0 ], 16
            mov byte ptr [ 1 ], 17
            mov byte ptr [ 2 ], 18

            pop ds
      }

      // See the pixels in their default colors?
      // Nope, they're too close to black.

      getch();

      // Set the first 3 entries after the EGA palette

      SetPaletteEntries( 16, 3, palette );

      // See the pixels in bright red, green and blue!

      getch();

      // Set color 0 to yellow

      SetPaletteEntry( 0, yellow );

      // See the yellow background!

      getch();

      // Place pixels in the bottom right corner using
      // our favorite colors.

      asm {
            push ds

            mov ax, 0A000h
            mov ds, ax

            mov byte ptr [ 320 * 200 - 3 ], 18
            mov byte ptr [ 320 * 200 - 2 ], 17
            mov byte ptr [ 320 * 200 - 1 ], 16

            pop ds
      }

      // See the bright blue, green and red pixels!

      getch();

      // Set mode 003h

      asm {
            mov ax, 00003h
            int 010h
      }

      return 0;
}
0
 
LVL 1

Expert Comment

by:bredbored
ID: 10880945
SetPaletteEntries should be

void SetPaletteEntries( Byte start, unsigned short count, Color far *pPalette )
{
     asm {
          mov ax, word ptr pPalette[ 2 ]
          mov es, ax
          mov dx, word ptr pPalette
          mov cx, count
          mov bl, start
          xor bh, bh
          mov ax, 01012h
          int 010h
     }
}

just in case 255 colors aren't enough.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now