?
Solved

256 Color in 'char Image[]' array

Posted on 2004-03-22
3
Medium Priority
?
897 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

777 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