1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
* gldgBitfield.c
*
* OpenGL Graph Display bitfield utility module implementation
*
* Written by: Shawn Taras
*/
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include "glgd.h"
/*
* External public functions
*/
GLboolean
glgdBitfieldInit(glgdBitfield *bits)
{
if (bits != NULL)
{
return glgdBitfieldClear(bits);
}
return GL_FALSE;
}
GLboolean
glgdBitfieldFini(glgdBitfield *bits)
{
return glgdBitfieldInit(bits);
}
GLboolean
glgdBitfieldClear(glgdBitfield *bits)
{
int i;
if (bits != NULL)
{
for (i=0; i<GLGDBITFIELD_BYTE_COUNT; i++)
{
bits->bits[i] = 0x00;
}
return GL_TRUE;
}
return GL_FALSE;
}
GLboolean
glgdBitfieldSet(glgdBitfield *bits, int bitNdx)
{
int byteNdx;
GLubyte mask;
if (bits != NULL && bitNdx >= 0 && bitNdx < GLGDBITFIELD_BIT_COUNT)
{
byteNdx = bitNdx / GLGDBITFIELD_BITS_PER_BYTE;
mask = 0x1 << (bitNdx % GLGDBITFIELD_BITS_PER_BYTE);
if ((bits->bits[byteNdx] & mask) == 0x00)
{
bits->bits[byteNdx] |= mask;
/* Only return GL_TRUE if it was actually set! */
return GL_TRUE;
}
}
return GL_FALSE;
}
GLboolean
glgdBitfieldToggle(glgdBitfield *bits, int bitNdx)
{
int byteNdx;
GLubyte mask;
if (bits != NULL && bitNdx >= 0 && bitNdx < GLGDBITFIELD_BIT_COUNT)
{
byteNdx = bitNdx / GLGDBITFIELD_BITS_PER_BYTE;
mask = 0x1 << (bitNdx % GLGDBITFIELD_BITS_PER_BYTE);
bits->bits[byteNdx] ^= mask;
return GL_TRUE;
}
return GL_FALSE;
}
GLboolean
glgdBitfieldReset(glgdBitfield *bits, int bitNdx)
{
int byteNdx;
GLubyte mask;
if (bits != NULL && bitNdx >= 0 && bitNdx < GLGDBITFIELD_BIT_COUNT)
{
byteNdx = bitNdx / GLGDBITFIELD_BITS_PER_BYTE;
mask = 0x1 << (bitNdx % GLGDBITFIELD_BITS_PER_BYTE);
if (bits->bits[byteNdx] & mask)
{
bits->bits[byteNdx] &= ~mask;
/* Only return GL_TRUE if it was actually reset! */
return GL_TRUE;
}
}
return GL_FALSE;
}
GLboolean
glgdBitfieldIsSet(glgdBitfield *bits, int bitNdx)
{
int byteNdx;
GLubyte mask;
if (bits != NULL && bitNdx >= 0 && bitNdx < GLGDBITFIELD_BIT_COUNT)
{
byteNdx = bitNdx / GLGDBITFIELD_BITS_PER_BYTE;
mask = 0x1 << (bitNdx % GLGDBITFIELD_BITS_PER_BYTE);
if (bits->bits[byteNdx] & mask)
{
return GL_TRUE;
}
}
return GL_FALSE;
}
GLboolean
glgdBitfieldCompare(glgdBitfield *a, glgdBitfield *b)
{
int i;
if (a && b)
{
for (i=0; i<GLGDBITFIELD_BYTE_COUNT; i++)
{
if (a->bits[i] & b->bits[i])
{
/* Something matches! */
return GL_TRUE;
}
}
}
return GL_FALSE;
}
|