[1.2+OpenGL] "#if SDL_BYTEORDER == SDL_BIGENDIAN" - HELP!

[1.2+OpenGL] “#if SDL_BYTEORDER == SDL_BIGENDIAN” - HELP!!!

Hi,

I’m having some troubles with SDL_TTF rendering
into a temporary SDL_Surface
and then placed into a new OpenGL texture
for final rendering to an SDL1.2+OpenGL screen.

I am writing a triple cross-platform video game called LettersFall[TM] 2.0
( Windows® / Linux / Mac® OS X® [Intel&AMD/PPC] )

My sources work 100% on Windows and Linux OSs (with Intel&AMD CPUs),
but on my friend’s old PPC Mac® with OS X®
the text rendering is not working correctly!
( seems I got color channels mixed up on PPC Mac® )

I know it has something to do with this:
#if SDL_BYTEORDER == SDL_BIGENDIAN
but I do not know how to fix it properly…

I’ve posted the relevant parts of the source.
Please look it over and tell me what is wrong…

Thanks in advance!

Jesse

Code:
//-------------------------------------------------------------------------------------------------
bool Visuals::SetupGameWindow(int width, int height)
{
putenv( strdup(“SDL_VIDEO_CENTERED=1”) );

if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) != 0 )///SDL_INIT_EVERYTHING) != 0 )
{
    printf("ERROR: Unable to initialize SDL: %s\n", SDL_GetError());
    return false;
}
else  printf("SDL (Everything) initialized\n");

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

SDL_WM_SetCaption("LettersFall[TM] - (C)2010 by 16BitSoft(R)", NULL);
SDL_WM_SetIcon(SDL_LoadBMP("data/graphics/Icon.bmp"), NULL);

Screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL | SDL_RESIZABLE);
if (!Screen)
{
    printf("ERROR: SDL+OpenGL resizable window create failed\n");
    return false;
}
else fprintf(stdout, "SDL+OpenGL resizable window created\n");

ScreenWidth = width;
ScreenHeight = height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_DEPTH);
glDisable(GL_DEPTH_TEST);
printf("OpenGL initialized\n");

if ( TTF_Init() == -1 )
{
    fprintf(stderr, "ERROR: TTF_Init: %s\n", TTF_GetError());
    return false;
}
else fprintf(stdout, "SDL_TTF initialized\n");

#if SDL_BYTEORDER == SDL_BIGENDIAN
    R_Mask  = 0xff000000;
    G_Mask  = 0x00ff0000;
    B_Mask  = 0x0000ff00;
    A_Mask  = 0x000000ff;
    ColorIsRGBA = true;
#else
    R_Mask  = 0x000000ff;
    G_Mask  = 0x0000ff00;
    B_Mask  = 0x00ff0000;
    A_Mask  = 0xff000000;
    ColorIsRGBA = false;
#endif
return true;

}
//-------------------------------------------------------------------------------------------------

Code:
//-------------------------------------------------------------------------------------------------
void Visuals::DrawTextOntoScreenBuffer(const char *textToDisplay, TTF_Font *font, GLfloat posX, GLfloat posY,
int XJustification, Uint8 textRed, Uint8 textGreen, Uint8 textBlue,
Uint8 outlineRed, Uint8 outlineGreen, Uint8 outlineBlue)
{
SDL_Color textColor;
SDL_Color outlineColor;
SDL_Surface *text;
SDL_Surface *textOutline;
SDL_Surface *textAndOutline;
int w,h;
SDL_Rect rect;
GLuint textTexture;

textColor.r = textRed;
textColor.g = textGreen;
textColor.b = textBlue;

outlineColor.r = outlineRed;
outlineColor.g = outlineGreen;
outlineColor.b = outlineBlue;

text = TTF_RenderText_Blended(font, textToDisplay, textColor);
textOutline = TTF_RenderText_Solid(font, textToDisplay, outlineColor);

if (XJustification == JustifyLeft)
{
}
else if (XJustification == JustifyCenter)
{
    posX = (640 / 2) - (text->w / 2) - 2;
}
else if (XJustification == JustifyRight)
{
    posX = (640 - posX) - text->w - 2;
}
else if (XJustification == JustifyCenterOnPoint)
{
    posX = posX - (text->w / 2) - 2;
}

w = NextPowerOfTwo(3+text->w);
h = NextPowerOfTwo(3+text->h);

textAndOutline = SDL_CreateRGBSurface(0, w, h, 32, R_Mask, G_Mask, B_Mask, A_Mask);

for (Sint16 y = 0; y < 5; y+=1)
{
    for (Sint16 x = 0; x < 5; x+=1)
    {
        rect.x = x; rect.y = y;
        SDL_BlitSurface(textOutline, 0, textAndOutline, &rect);
    }
}

rect.x = 2;
rect.y = 2;
SDL_BlitSurface(text, 0, textAndOutline, &rect);

glPushMatrix();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);

glGenTextures(1, &textTexture);
glBindTexture(GL_TEXTURE_2D, textTexture);

// if (ColorIsRGBA == false)
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, textAndOutline->pixels );
// else
// glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
// GL_UNSIGNED_BYTE, textAndOutline->pixels );

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textTexture);
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex2f(posX    , posY + h);

    glTexCoord2f(1.0f, 1.0f);
    glVertex2f(posX + w, posY + h);

    glTexCoord2f(1.0f, 0.0f);
    glVertex2f(posX + w, posY    );

    glTexCoord2f(0.0f, 0.0f);
    glVertex2f(posX    , posY    );
glEnd();

glPushMatrix();

SDL_FreeSurface(text);
SDL_FreeSurface(textOutline);
SDL_FreeSurface(textAndOutline);
glDeleteTextures(1, &textTexture);

}
//-------------------------------------------------------------------------------------------------

Code:
visuals->DrawTextOntoScreenBuffer(“©opyright 2010, By 16BitSoft®”, visuals->FontDefault[0]
, 0, 420, JustifyCenter, 255, 255, 255, 90, 90, 90);

Code:
#if SDL_BYTEORDER == SDL_BIGENDIAN
R_Mask = 0xff000000;
G_Mask = 0x00ff0000;
B_Mask = 0x0000ff00;
A_Mask = 0x000000ff;
ColorIsRGBA = true;
#else
R_Mask = 0x000000ff;
G_Mask = 0x0000ff00;
B_Mask = 0x00ff0000;
A_Mask = 0xff000000;
ColorIsRGBA = false;
#endif

If it’s working with those comments but not without them, the ColorIsRGBA is probably to fault. Just remove the variable, it’s useless and double-endian-correction.

Can’t say for sure though, I’ve never tried GL on big-endian hardware so perhaps GL is endian correcting on its own (meaning the entire endian correction is pointless).------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

Hello

I see this code.you always use data in GL_RGBA

and when you use this format you need nothing change.

// if (ColorIsRGBA == false)
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, textAndOutline->pixels );
// else
// glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
// GL_UNSIGNED_BYTE, textAndOutline->pixels );

have you try your code without that endian diffrence.i think then text work

#if SDL_BYTEORDER == SDL_BIGENDIAN

   R_Mask  = 0xff000000;
   G_Mask  = 0x00ff0000;
   B_Mask  = 0x0000ff00;
   A_Mask  = 0x000000ff;
   ColorIsRGBA = true;

#else
R_Mask = 0x000000ff;
G_Mask = 0x0000ff00;
B_Mask = 0x00ff0000;
A_Mask = 0xff000000;
ColorIsRGBA = false;
#endif

[1.2+OpenGL] “#if SDL_BYTEORDER == SDL_BIGENDIAN” - HELP!!!

Hi,

I’m having some troubles with SDL_TTF rendering
into a temporary SDL_Surface
and then placed into a new OpenGL texture
for final rendering to an SDL1.2+OpenGL screen.

I am writing a triple cross-platform video game called LettersFall[TM] 2.0
( Windows® / Linux / Mac® OS X® [Intel&AMD/PPC] )

My sources work 100% on Windows and Linux OSs (with Intel&AMD CPUs),
but on my friend’s old PPC Mac® with OS X®
the text rendering is not working correctly!
( seems I got color channels mixed up on PPC Mac® )

I know it has something to do with this:
#if SDL_BYTEORDER == SDL_BIGENDIAN
but I do not know how to fix it properly…

I’ve posted the relevant parts of the source.
Please look it over and tell me what is wrong…

Thanks in advance!

Jesse

Code:

//-------------------------------------------------------------------------------------------------

bool Visuals::SetupGameWindow(int width, int height) {
putenv( strdup(“SDL_VIDEO_CENTERED=1”) );

if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) != 0
)///SDL_INIT_EVERYTHING) != 0 )
{
printf(“ERROR: Unable to initialize SDL: %s\n”, SDL_GetError());
return false;
}
else printf(“SDL (Everything) initialized\n”);

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

SDL_WM_SetCaption(“LettersFall[TM] - ©2010 by 16BitSoft®”, NULL);
SDL_WM_SetIcon(SDL_LoadBMP(“data/graphics/Icon.bmp”), NULL);

Screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL | SDL_RESIZABLE);
if (!Screen)
{
printf(“ERROR: SDL+OpenGL resizable window create failed\n”);
return false;
}
else fprintf(stdout, “SDL+OpenGL resizable window created\n”);

ScreenWidth = width;
ScreenHeight = height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_DEPTH);
glDisable(GL_DEPTH_TEST);
printf(“OpenGL initialized\n”);

if ( TTF_Init() == -1 )
{
fprintf(stderr, “ERROR: TTF_Init: %s\n”, TTF_GetError());
return false;
}
else fprintf(stdout, “SDL_TTF initialized\n”);

#if SDL_BYTEORDER == SDL_BIGENDIAN
R_Mask = 0xff000000;
G_Mask = 0x00ff0000;
B_Mask = 0x0000ff00;
A_Mask = 0x000000ff;
ColorIsRGBA = true;
#else
R_Mask = 0x000000ff;
G_Mask = 0x0000ff00;
B_Mask = 0x00ff0000;
A_Mask = 0xff000000;
ColorIsRGBA = false;
#endif
return true;
}

//-------------------------------------------------------------------------------------------------

Code:

//-------------------------------------------------------------------------------------------------

void Visuals::DrawTextOntoScreenBuffer(const char *textToDisplay, TTF_Font *font, GLfloat posX,
GLfloat posY,
int XJustification, Uint8 textRed, Uint8 textGreen, Uint8
textBlue,
Uint8 outlineRed, Uint8 outlineGreen, Uint8 outlineBlue)
{
SDL_Color textColor;
SDL_Color outlineColor;
SDL_Surface *text;
SDL_Surface *textOutline;
SDL_Surface *textAndOutline;
int w,h;
SDL_Rect rect;
GLuint textTexture;

textColor.r = textRed;
textColor.g = textGreen;
textColor.b = textBlue;

outlineColor.r = outlineRed;
outlineColor.g = outlineGreen;
outlineColor.b = outlineBlue;

text = TTF_RenderText_Blended(font, textToDisplay, textColor);
textOutline = TTF_RenderText_Solid(font, textToDisplay, outlineColor);

if (XJustification == JustifyLeft)
{
}
else if (XJustification == JustifyCenter)
{
posX = (640 / 2) - (text->w / 2) - 2;
}
else if (XJustification == JustifyRight)
{
posX = (640 - posX) - text->w - 2;
}
else if (XJustification == JustifyCenterOnPoint)
{
posX = posX - (text->w / 2) - 2;
}

w = NextPowerOfTwo(3+text->w);
h = NextPowerOfTwo(3+text->h);

textAndOutline = SDL_CreateRGBSurface(0, w, h, 32, R_Mask, G_Mask, B_Mask, A_Mask);

for (Sint16 y = 0; y < 5; y+=1)
{
for (Sint16 x = 0; x < 5; x+=1)
{
rect.x = x; rect.y = y;
SDL_BlitSurface(textOutline, 0, textAndOutline, &rect);
}
}

rect.x = 2;
rect.y = 2;
SDL_BlitSurface(text, 0, textAndOutline, &rect);

glPushMatrix();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);

glGenTextures(1, &textTexture);
glBindTexture(GL_TEXTURE_2D, textTexture);
// if (ColorIsRGBA == false)
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, textAndOutline->pixels );
// else
// glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
// GL_UNSIGNED_BYTE, textAndOutline->pixels );

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textTexture);
glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(posX , posY + h);

   glTexCoord2f(1.0f, 1.0f);
   glVertex2f(posX + w, posY + h);

   glTexCoord2f(1.0f, 0.0f);
   glVertex2f(posX + w, posY    );

   glTexCoord2f(0.0f, 0.0f);
   glVertex2f(posX    , posY    );

glEnd();

glPushMatrix();

SDL_FreeSurface(text);
SDL_FreeSurface(textOutline);
SDL_FreeSurface(textAndOutline);
glDeleteTextures(1, &textTexture); }

//-------------------------------------------------------------------------------------------------

Code:
visuals->DrawTextOntoScreenBuffer(“©opyright 2010, By 16BitSoft®”, visuals->FontDefault[0]
, 0, 420, JustifyCenter, 255, 255, 255, 90, 90, 90);

RegardsOn 29.05.10, you wrote: