characterRgba function
Implementation
int characterRgba(Pointer<SdlRenderer> renderer, int x, int y, int c, int r,
int g, int b, int a) {
Pointer<SdlRect> srect = calloc<SdlRect>();
Pointer<SdlRect> drect = calloc<SdlRect>();
int result;
int ix, iy;
int patt;
Pointer<Uint32> pixels;
int linepos;
Pointer<SdlSurface> character;
Pointer<SdlSurface> rotatedCharacter;
int ci;
/* Character index in cache */
ci = c;
/*
* Create new charWidth x charHeight bitmap surface if not already present.
* Might get rotated later.
*/
if (gfxPrimitivesFont[ci] == nullptr) {
/*
* Redraw character into surface
*/
character = sdlCreateRgbSurface(SDL_SWSURFACE, charWidth, charHeight, 32,
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
if (character == nullptr) {
return -1;
}
var charpos = ci * charSize;
pixels = character.ref.pixels.cast<Uint32>();
/*
* Drawing loop
*/
patt = 0;
linepos = 0;
for (iy = 0; iy < charHeight; iy++) {
patt = currentFontdata[charpos + iy];
for (ix = 0; ix < charWidth; ix++) {
var pixel = pixels + linepos;
if (patt & 0x80 != 0) {
pixel.value = 0xffffffff;
} else {
pixel.value = 0;
}
linepos++;
patt = patt << 1;
}
}
/* Maybe rotate and replace cached image */
if (charRotation > 0) {
rotatedCharacter = rotateSurface90Degrees(character, charRotation);
sdlFreeSurface(character);
character = rotatedCharacter;
}
/* Convert temp surface into texture */
gfxPrimitivesFont[ci] = sdlCreateTextureFromSurface(renderer, character);
sdlFreeSurface(character);
/*
* Check pointer
*/
if (gfxPrimitivesFont[ci] == nullptr) {
return -1;
}
}
/*
* Set color
*/
result = 0;
result |= sdlSetTextureColorMod(gfxPrimitivesFont[ci], r, g, b);
result |= sdlSetTextureAlphaMod(gfxPrimitivesFont[ci], a);
/*
* Setup source rectangle
*/
srect.ref.x = 0;
srect.ref.y = 0;
srect.ref.w = charWidthLocal;
srect.ref.h = charHeightLocal;
/*
* Setup destination rectangle
*/
drect.ref.x = x;
drect.ref.y = y;
drect.ref.w = charWidthLocal;
drect.ref.h = charHeightLocal;
/*
* Draw texture onto destination
*/
result |= sdlRenderCopy(renderer, gfxPrimitivesFont[ci], srect, drect);
calloc.free(srect);
calloc.free(drect);
return result;
}