/* * MotorJ in-game text to surface subroutines * Header file * (C) 2007-2010 Alejandro Valenzuela Roca * * This file is part of MotorJ, a free framework for videogame creation. * * * * MotorJ is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MotorJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with MotorJ. If not, see . */ #include "textrender.h" #ifndef NINTENDO_DS #ifndef NINTENDO_WII // FIXME: Should not be auto-compiled in Nintendo DS MJTextSurface * mjRenderText(bool Blended, char * UTF8txt, int maxwidth, TTF_Font * font_ttf, vector3 * color, MJTextSurface * srfc) { //GLubyte * copy; SDL_Color color_in; SDL_Surface * txsurface = NULL; SDL_Surface * containertx = NULL; bool deleteTexture = 1; // Either create a new text surface or use an existing one if (srfc == NULL) { srfc = new MJTextSurface; srfc->width = srfc->height = 0; deleteTexture = 0; } // Set internal color.. color_in.r = 255*color->x; color_in.g = 255*color->y; color_in.b = 255*color->z; color_in.unused = 255; // Render the text onto a temporary surface if (Blended) txsurface = TTF_RenderUTF8_Blended(font_ttf, UTF8txt, color_in); else { //SDL_Surface * tmp; txsurface = TTF_RenderUTF8_Solid(font_ttf, UTF8txt, color_in); //tmp = SDL_CreateRGBSurface( } // Set the size of the container surface (Must be square and a power of two // in order to be used with OpenGL) if ((txsurface->w < 64) && (txsurface->h < 64)) { srfc->width = srfc->height = 64; } else if ((txsurface->w < 128) && (txsurface->h < 128)) { srfc->width = srfc->height = 128; } else if ((txsurface->w < 256) && (txsurface->h < 256)) { srfc->width = srfc->height = 256; } else if ((txsurface->w < 512) && (txsurface->h < 512)) { srfc->width = srfc->height = 512; } else if ((txsurface->w < 1024) && (txsurface->h < 1024)) { srfc->width = srfc->height = 1024; } else return NULL; //too big! :S srfc->Bpp = txsurface->format->BytesPerPixel; // Make sure both container and text are in the same format (RGBA) //if (txsurface->format->BytesPerPixel != 4) //{ // txsurface = SDL_ConvertSurface(txsurface, containertx->format, SDL_SWSURFACE); // printf("Convert\n"); //} // Set the inner width and height (the rendered text's dimensions) srfc->innerheight = txsurface->h; srfc->innerwidth = txsurface->w; srfc->ratio = (float)srfc->innerheight/(float)srfc->innerwidth; // Set the textcoords so we can use it later with our program srfc->texcoords[0] = (float)txsurface->w/(float)srfc->width; srfc->texcoords[1] = (float)txsurface->h/(float)srfc->height; // If the texture existed previously, let's free it so we won't hog up // the video card's memory if (deleteTexture) { glDeleteTextures(1, &srfc->glTexName); } glGenTextures(1,&srfc->glTexName); // Set the texture name for binding glBindTexture(GL_TEXTURE_2D, srfc->glTexName); // Lock the SDL surface in order to access its pixels SDL_LockSurface(txsurface); // Build the mipmaps (This will also scale the textures to OpenGL's preferences) gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, txsurface->w, txsurface->h, GL_RGBA, GL_UNSIGNED_BYTE, txsurface->pixels); // Unlock and free the surface SDL_UnlockSurface(txsurface); SDL_FreeSurface(txsurface); // All done! return the TextSurface.. return srfc; } GLubyte * copySFC_RGBAToGLUBYTEARRAY(SDL_Surface * src, int w, int h) { int x, y; Uint8 r, g, b, a; GLubyte * dst; //printf("new.. %x \n", dst->junk); //dst = new mj_BigBagOfFilth; //(mj_BigBagOfFilth *) malloc(sizeof(mj_BigBagOfFilth)); dst = new GLubyte [w*h*4];//(GLubyte *) malloc(sizeof(GLubyte)*src->w*src->h*4); //printf("postnew.. %x\n", dst->junk); // Lock surfaces so we can edit pixels directly SDL_LockSurface(src); //SDL_LockSurface(dst); // Overwrite the destination texture for (y = 0; y < src->h; y++){ for (x = 0; x < src->w; x++){ // Here p is the address to the pixel we want to set Uint8 *p = (Uint8 *)src->pixels + y * src->pitch + x * 4; SDL_GetRGBA(*(Uint32 *)p, src->format, &r, &g, &b, &a); dst[(y*4)+(x*h) ] = r; dst[(y*4)+(x*h)+ 1] = g; dst[(y*4)+(x*h)+ 2] = b; dst[(y*4)+(x*h)+ 3] = a; } } //SDL_UnlockSurface(dst); SDL_UnlockSurface(src); return dst; } void copySFCToSFC_RGBA(SDL_Surface * src, SDL_Surface * dst) { int x, y; // Lock surfaces so we can edit pixels directly SDL_LockSurface(src); SDL_LockSurface(dst); // Overwrite the destination texture for (y = 0; y < src->h; y++){ for (x = 0; x < src->w; x++){ // Here p is the address to the pixel we want to set Uint8 *p = (Uint8 *)src->pixels + y * src->pitch + x * 4; Uint8 *pdst = (Uint8 *)dst->pixels + y * dst->pitch + x * 4; * (Uint32 *) pdst = * (Uint32 * ) p; //SDL_GetRGBA(*(Uint32 *)p, src->format, &dst[(x*4)+(y*src->w)], //&dst[(x*4)+(y*src->w)+1], //&dst[(x*4)+(y*src->w)+2], //&dst[(x*4)+(y*src->w)+3]); } } SDL_UnlockSurface(dst); SDL_UnlockSurface(src); //return dst; } #endif #endif