Permalink
Browse files

Surface: Added Masks struct to give the possibility to adapt the masks.

Update Color4b: The conversion of the hex values depends now on the
endianness (byte order) of the machine.
  • Loading branch information...
Dgame committed Apr 29, 2015
1 parent f0e8557 commit 3784d98f8f8823301bced71b1ef139ab98d67c33
@@ -75,15 +75,20 @@ struct Color4b {
/**
* CTor
*
* Expect ABGR as format
*/
@nogc
this(uint hexValue) pure nothrow {
this.alpha = (hexValue >> 24) & 0xff;
this.blue = (hexValue >> 16) & 0xff;
this.green = (hexValue >> 8) & 0xff;
this.red = hexValue & 0xff;
version (LittleEndian) {
this.alpha = (hexValue >> 24) & 0xff;
this.blue = (hexValue >> 16) & 0xff;
this.green = (hexValue >> 8) & 0xff;
this.red = hexValue & 0xff;
} else {
this.red = (hexValue >> 24) & 0xff;
this.green = (hexValue >> 16) & 0xff;
this.blue = (hexValue >> 8) & 0xff;
this.alpha = hexValue & 0xff;
}
}
/**
@@ -141,11 +146,14 @@ struct Color4b {
}
/**
* Returns the RGBA color information as hex value (in ABGR format)
* Returns the RGBA color information as hex value
*/
@nogc
uint asHex() const pure nothrow {
return ((this.alpha & 0xff) << 24) + ((this.blue & 0xff) << 16) + ((this.green & 0xff) << 8) + (this.red & 0xff);
version (LittleEndian)
return ((this.alpha & 0xff) << 24) + ((this.blue & 0xff) << 16) + ((this.green & 0xff) << 8) + (this.red & 0xff);
else
return ((this.red & 0xff) << 24) + ((this.green & 0xff) << 16) + ((this.blue & 0xff) << 8) + (this.alpha & 0xff);
}
}
@@ -35,8 +35,56 @@ import Dgame.Graphic.Color;
import Dgame.Internal.Error;
@nogc
void verifyDepth(ubyte depth) pure nothrow {
assert(depth == 8 || depth == 16 || depth == 24 || depth == 32, "Invalid depth.");
}
public:
/**
* The RGBA-Mask are the bitmasks used to extract that color from a pixel.
* It is used to e.g. define the background of a newly created Surface.
* Using zeros for the RGB-Masks sets a default value, based on the depth
* But using zero for the Aalpha-Mask results in an Alpha-Mask of 0.
* By default Surfaces with an Alpha-Mask are set up for blending.
* You can change the blend mode with Surface.setBlendMode.
*/
struct Masks {
/**
* The default value. Same as Masks.init.
* The RGBA-Masks depend on the endianness (byte order) of the machine
*/
static immutable Masks Default = Masks.init;
/**
* The RGBA-Masks are zero
*/
static immutable Masks Zero = Masks(0, 0, 0, 0);
version (LittleEndian) {
uint red = 0x000000ff; /// the red mask, default is 0x000000ff
uint green = 0x0000ff00; /// the green mask, default is 0x0000ff00
uint blue = 0x00ff0000; /// the blue mask, default is 0x00ff0000
uint alpha = 0xff000000; /// the alpha mask, default is 0xff000000
} else {
uint red = 0xff000000; /// the red mask, default is 0xff000000
uint green = 0x00ff0000; /// the green mask, default is 0x00ff0000
uint blue = 0x0000ff00; /// the blue mask, default is 0x0000ff00
uint alpha = 0x000000ff; /// the alpha mask, default is 0x000000ff
}
/**
* CTor
*/
@nogc
this(uint red, uint green, uint blue, uint alpha) pure nothrow {
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
}
}
/**
* Surface is a wrapper for a SDL_Surface and can load and save images.
*
@@ -63,44 +111,33 @@ struct Surface {
Alpha = 8 /// Alpha Mask
}
version (LittleEndian) {
enum uint RMask = 0x000000ff;
enum uint GMask = 0x0000ff00;
enum uint BMask = 0x00ff0000;
enum uint AMask = 0xff000000;
} else {
enum uint RMask = 0xff000000;
enum uint GMask = 0x00ff0000;
enum uint BMask = 0x0000ff00;
enum uint AMask = 0x000000ff;
}
private:
SDL_Surface* _surface;
@nogc
static SDL_Surface* create(uint width, uint height, ubyte depth = 32) nothrow {
assert(depth == 8 || depth == 16 || depth == 24 || depth == 32, "Invalid depth.");
static SDL_Surface* create(uint width, uint height, ubyte depth, ref const Masks mask) nothrow {
verifyDepth(depth);
return SDL_CreateRGBSurface(0, width, height, depth, RMask, GMask, BMask, AMask);
return SDL_CreateRGBSurface(0, width, height, depth, mask.red, mask.green, mask.blue, mask.alpha);
}
@nogc
static SDL_Surface* create(void* memory, uint width, uint height, ubyte depth = 32) nothrow {
static SDL_Surface* create(void* memory, uint width, uint height, ubyte depth, ref const Masks mask) nothrow {
verifyDepth(depth);
assert(memory, "Memory is empty.");
assert(depth == 8 || depth == 16 || depth == 24 || depth == 32, "Invalid depth.");
return SDL_CreateRGBSurfaceFrom(memory, width, height, depth, (depth / 8) * width, RMask, GMask, BMask, AMask);
return SDL_CreateRGBSurfaceFrom(memory, width, height, depth,
(depth / 8) * width, mask.red, mask.green, mask.blue, mask.alpha);
}
public:
/**
* CTor
*/
@nogc
this(SDL_Surface* srfc) pure nothrow {
assert(srfc, "Invalid SDL_Surface.");
assert(srfc.pixels, "Invalid pixel data.");
this(SDL_Surface* srfc) nothrow {
assert_fmt(srfc !is null, "Invalid SDL_Surface. Error: %s\n", SDL_GetError());
assert_fmt(srfc.pixels !is null, "Invalid pixel data. Error: %s\n", SDL_GetError());
_surface = srfc;
}
@@ -117,22 +154,22 @@ public:
* Make a new Surface of the given width, height and depth.
*/
@nogc
this(uint width, uint height, ubyte depth = 32) nothrow {
_surface = Surface.create(width, height, depth);
this(uint width, uint height, ubyte depth = 32, const Masks mask = Masks.init) nothrow {
_surface = Surface.create(width, height, depth, mask);
assert(_surface, "Invalid SDL_Surface.");
assert(_surface.pixels, "Invalid pixel data.");
assert_fmt(_surface !is null, "Invalid SDL_Surface. Error: %s\n", SDL_GetError());
assert_fmt(_surface.pixels !is null, "Invalid pixel data. Error: %s\n", SDL_GetError());
}
/**
* Make an new Surface of the given memory, width, height and depth.
*/
@nogc
this(void* memory, uint width, uint height, ubyte depth = 32) nothrow {
_surface = Surface.create(memory, width, height, depth);
this(void* memory, uint width, uint height, ubyte depth = 32, const Masks mask = Masks.init) nothrow {
_surface = Surface.create(memory, width, height, depth, mask);
assert(_surface, "Invalid SDL_Surface.");
assert(_surface.pixels, "Invalid pixel data.");
assert_fmt(_surface !is null, "Invalid SDL_Surface. Error: %s\n", SDL_GetError());
assert_fmt(_surface.pixels !is null, "Invalid pixel data. Error: %s\n", SDL_GetError());
}
/**
@@ -199,13 +236,14 @@ public:
* Load from memory.
*/
@nogc
bool loadFromMemory(void* memory, ushort width, ushort height, ubyte depth = 32) nothrow {
bool loadFromMemory(void* memory, ushort width, ushort height, ubyte depth = 32,
const Masks mask = Masks.init) nothrow
{
assert(memory, "Memory is empty.");
assert(depth == 8 || depth == 16 || depth == 24 || depth == 32, "Invalid depth.");
SDL_FreeSurface(_surface); // free old surface
_surface = SDL_CreateRGBSurfaceFrom(memory, width, height, depth, (depth / 8) * width, RMask, GMask, BMask, AMask);
_surface = Surface.create(memory, width, height, depth, mask);
if (!_surface) {
print_fmt("Could not load image. Error: %s.\n", SDL_GetError());
return false;
@@ -611,7 +649,6 @@ public:
if (!result)
print_fmt("Could not blit surface: %s\n", SDL_GetError());
return result;
}
@@ -118,15 +118,19 @@ public:
* CTor
* Position of the Window is default 100x, 100y and the VerticalSync is disabled
*/
this(uint width, uint height, string title, uint style = Style.Default, GLSettings gl_settings = GLSettings.init) {
this(uint width, uint height, string title, uint style = Style.Default,
GLSettings gl_settings = GLSettings.init)
{
this(Rect(DefPosX, DefPosY, width, height), title, style, gl_settings);
}
/**
* CTor
* Position is at 100x, 100y and the VerticalSync is enabled, if mode.refreshRate > 0
*/
this()(auto ref const DisplayMode mode, string title, uint style = Style.Default, GLSettings gl_settings = GLSettings.init) {
this()(auto ref const DisplayMode mode, string title, uint style = Style.Default,
GLSettings gl_settings = GLSettings.init)
{
this(Rect(DefPosX, DefPosY, mode.width, mode.height), title, style, gl_settings);
if (mode.refreshRate > 0)
@@ -137,7 +141,9 @@ public:
* CTor
* Position is specifiable and the VerticalSync is disabled
*/
this()(auto ref const Rect rect, string title, uint style = Style.Default, GLSettings gl_settings = GLSettings.init) {
this()(auto ref const Rect rect, string title, uint style = Style.Default,
GLSettings gl_settings = GLSettings.init)
{
if (_count == 0)
_initSDL();
@@ -84,9 +84,7 @@ <h1>Dgame.Graphic.Color</h1>
</dd>
<dt><big><a name="Color4b.this.2"></a>pure nothrow @nogc this(uint <i>hexValue</i>);
</big></dt>
<dd>CTor
<br><br>
Expect ABGR as format<br><br>
<dd>CTor<br><br>
</dd>
<dt><big><a name="Color4b.this.3"></a>pure nothrow @nogc this()(auto ref const Color4f <i>col</i>);
@@ -119,7 +117,7 @@ <h1>Dgame.Graphic.Color</h1>
</dd>
<dt><big><a name="Color4b.asHex"></a>const pure nothrow @nogc uint <u>asHex</u>();
</big></dt>
<dd>Returns the RGBA color information as hex value (in ABGR format)<br><br>
<dd>Returns the RGBA color information as hex value<br><br>
</dd>
</dl>
@@ -5,7 +5,54 @@
<h1>Dgame.Graphic.Surface</h1>
<!-- Generated by Ddoc from ..\Graphic\Surface.d -->
<br><br>
<dl><dt><big><a name="Surface"></a>struct <u>Surface</u>;
<dl><dt><big><a name="Masks"></a>struct <u>Masks</u>;
</big></dt>
<dd>The RGBA-Mask are the bitmasks used to extract that color from a pixel.
It is used to e.g. define the background of a newly created Surface.
Using zeros for the RGB-<u>Masks</u> sets a default value, based on the depth
But using zero for the Aalpha-Mask results in an Alpha-Mask of 0.
By default Surfaces with an Alpha-Mask are set up for blending.
You can change the blend mode with Surface.setBlendMode.<br><br>
<dl><dt><big><a name="Masks.Default"></a>static immutable Masks <u>Default</u>;
</big></dt>
<dd>The default value. Same as Masks.init.
The RGBA-Masks depend on the endianness (byte order) of the machine<br><br>
</dd>
<dt><big><a name="Masks.Zero"></a>static immutable Masks <u>Zero</u>;
</big></dt>
<dd>The RGBA-Masks are zero<br><br>
</dd>
<dt><big><a name="Masks.red"></a>uint <u>red</u>;
</big></dt>
<dd>the <u>red</u> mask, default is 0x000000ff<br><br>
</dd>
<dt><big><a name="Masks.green"></a>uint <u>green</u>;
</big></dt>
<dd>the <u>green</u> mask, default is 0x0000ff00<br><br>
</dd>
<dt><big><a name="Masks.blue"></a>uint <u>blue</u>;
</big></dt>
<dd>the <u>blue</u> mask, default is 0x00ff0000<br><br>
</dd>
<dt><big><a name="Masks.alpha"></a>uint <u>alpha</u>;
</big></dt>
<dd>the <u>alpha</u> mask, default is 0xff000000<br><br>
</dd>
<dt><big><a name="Masks.this"></a>pure nothrow @nogc this(uint <i>red</i>, uint <i>green</i>, uint <i>blue</i>, uint <i>alpha</i>);
</big></dt>
<dd>CTor<br><br>
</dd>
</dl>
</dd>
<dt><big><a name="Surface"></a>struct <u>Surface</u>;
</big></dt>
<dd><u>Surface</u> is a wrapper for a SDL_Surface and can load and save images.
<br><br>
@@ -56,7 +103,7 @@ <h1>Dgame.Graphic.Surface</h1>
</dd>
</dl>
</dd>
<dt><big><a name="Surface.this"></a>pure nothrow @nogc this(SDL_Surface* <i>srfc</i>);
<dt><big><a name="Surface.this"></a>nothrow @nogc this(SDL_Surface* <i>srfc</i>);
</big></dt>
<dd>CTor<br><br>
@@ -66,12 +113,12 @@ <h1>Dgame.Graphic.Surface</h1>
<dd>CTor<br><br>
</dd>
<dt><big><a name="Surface.this.3"></a>nothrow @nogc this(uint <i>width</i>, uint <i>height</i>, ubyte <i>depth</i> = 32);
<dt><big><a name="Surface.this.3"></a>nothrow @nogc this(uint <i>width</i>, uint <i>height</i>, ubyte <i>depth</i> = 32, const Masks <i>mask</i> = Masks.init);
</big></dt>
<dd>Make a new Surface of the given <i>width</i>, <i>height</i> and <i>depth</i>.<br><br>
</dd>
<dt><big><a name="Surface.this.4"></a>nothrow @nogc this(void* <i>memory</i>, uint <i>width</i>, uint <i>height</i>, ubyte <i>depth</i> = 32);
<dt><big><a name="Surface.this.4"></a>nothrow @nogc this(void* <i>memory</i>, uint <i>width</i>, uint <i>height</i>, ubyte <i>depth</i> = 32, const Masks <i>mask</i> = Masks.init);
</big></dt>
<dd>Make an new Surface of the given <i>memory</i>, <i>width</i>, <i>height</i> and <i>depth</i>.<br><br>
@@ -91,7 +138,7 @@ <h1>Dgame.Graphic.Surface</h1>
<dd>Load from <i>filename</i>. If any data is already stored, the data will be freed.<br><br>
</dd>
<dt><big><a name="Surface.loadFromMemory"></a>nothrow @nogc bool <u>loadFromMemory</u>(void* <i>memory</i>, ushort <i>width</i>, ushort <i>height</i>, ubyte <i>depth</i> = 32);
<dt><big><a name="Surface.loadFromMemory"></a>nothrow @nogc bool <u>loadFromMemory</u>(void* <i>memory</i>, ushort <i>width</i>, ushort <i>height</i>, ubyte <i>depth</i> = 32, const Masks <i>mask</i> = Masks.init);
</big></dt>
<dd>Load from <i>memory</i>.<br><br>
Oops, something went wrong.

0 comments on commit 3784d98

Please sign in to comment.