comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@bix.com
Subject: Re: More C
Date: 2000/01/28
Date: 2000-01-28T00:00:00+00:00	[thread overview]
Message-ID: <3B9k4.122$Ro5.7388@nnrp3-w.snfc21.pbi.net> (raw)
In-Reply-To: vhizotrc4x7.fsf@grotte.ifi.uio.no

type SDL_Color is record
        R, G, B: C.Unsigned_Char;
end record;
for SDL_Color'size use 4*8;
for SDL_Color use record
  R at 0 range 0 .. 7;
  G at 1 range 0 .. 7;
  B at 2 range 0 .. 7;
end record;
-- You might want to replace C.Unsigned_Char with something like
-- type Intensity is range 0 .. 255;
-- for Intensity'size use 8;

type SDL_Color_Access is access SDL_Color;

type Color_List is array(C.Int range <>) of aliased SDL_Color;
type Color_List_Access is access all Color_List;

type SDL_Palette is record
        Number_Of_Colors: C.Int;
        Colors: Color_List_Access;
end record;

I presume you really want Colors to point to a Color_List, not to a
single color.  Note that Colors will point to a list of N colors,
where N may, or may not, equal Number_Of_Colors.  So you can either
ignore Number_Of_Colors (using Palette.Colors.all'length if you need
the count of colors in the list) or you can have your "Indexed"
function do a check that "Index in 1 ..  Number_Of_Colors" or
"Index in 0 ..  Number_Of_Colors-1" or whatever, in addition to the
automatic index check.

function Indexed_Color_Access(Palette: SDL_Palette;
                              Index: C.Int) return SDL_Color_Access is
begin
  if Index in 1 .. Palette.Number_Of_Colors then
      return Palette.Colors(Index)'access;
  else
      raise Constraint_Error;
  end if;
end Indexed_Color_Access;

Depending on what you're doing, you might want:

function Indexed_Color(Palette: SDL_Palette;
                       Index: C.Int) return SDL_Color is
begin
  if Index in 1 .. Palette.Number_Of_Colors then
      return Palette.Colors(Index);
  else
      raise Constraint_Error;
  end if;
end Indexed_Color;

In both of the above functions, if you really want to have
  type SDL_Palette_Access is access SDL_Palette;
and have the functions take a parameter of
    Palette : SDL_Palette_Access;
instead of
    Palette : SDL_Palette;
then the rest of the function still works as written because
    Palette.Number_Of_Colors  and   Palette.Colors(Index)
are equivalent to
    (Palette.all).Number_Of_Colors  and   (Palette.all).Colors(Index)

Note that I've changed Color_Array to Palette, since an SDL Palette
is more than just an array.

If the structure of SDL_Palette as a count and a pointer isn't forced
on you, of course, it would be simpler to just use the Color_List
array (perhaps renaming Color_List to Palette).  Then just refer to
   Color_Array(Index)
where, e.g.,
   Color_Array : Color_List(1 .. 17);

If you really must match the memory layout of the C SDL_Palette,

type Arbitrary_Color_List is array(C.Int range) of aliased SDL_Color;
type Arbitrary_Color_List_Access is access all Color_List;

type SDL_Palette is record
        Number_Of_Colors: C.Int;
        Colors: Arbitrary_Color_List_Access;
end record;

is probably your best bet.  You then have to do your own index
checking, just as in C, and you'll want a representation clause.

It's possible in Ada to do dangerous things like address arithmetic
to look at storage a certain number of bytes beyond where a
particular SDL_Color is stored, but it should rarely be needed.
If it is really needed, look at packages System.Storage_Elements
and System.Address_To_Access_Conversions

For some examples of records laid out to communicate with Windows
about colors and bitmaps, see Claw (internal package Colors) and
Claw.Bitmaps in the $0 version of Claw at www.rrsoftware.com




       reply	other threads:[~2000-01-28  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <vhizotrc4x7.fsf@grotte.ifi.uio.no>
2000-01-28  0:00 ` tmoran [this message]
2000-01-28  0:00   ` More C David Starner
2000-01-28  0:00     ` Pascal Obry
2000-01-28  0:00       ` David Starner
2000-01-28  0:00         ` tmoran
2000-01-28  0:00 ` Gautier
2000-01-28  0:00 ` Jeff Carter
2000-01-30  0:00 ` Nick Roberts
     [not found]   ` <vhioga1r2j8.fsf@grotte.ifi.uio.no>
2000-02-01  0:00     ` tmoran
2000-02-02  0:00     ` Nick Roberts
2000-02-03  0:00     ` Keith Thompson
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox