void InputNode::renderText()
	{
		if ( _image != NULL )
			SDL_FreeSurface(_image);

		_image = TTF_RenderUNICODE_Blended(_font, _str, _foreground);

		// Account for input text scroll
		if ( _image != NULL && _image->clip_rect.w > _pos.w - 10 )
			_scroll.x = _image->clip_rect.w - _pos.w + 10;
		else
			_scroll.x = 0;

		// Get width of the text string by placing
		// a null byte character up to the point we
		// want to count, then putting the original
		// back. Nice.
		int w;
		Uint16 t = _str[_curr_index];
		_str[_curr_index] = '\0';
		TTF_SizeUNICODE(_font, _str, &w, NULL);
		_str[_curr_index] = t;

		_cursor_rect.x = w - _scroll.x;

		if ( _cursor_rect.x < 0 )
		{
			_scroll.x += _cursor_rect.x;
			_cursor_rect.x = 0;
		}
	}
Exemple #2
0
SDL_Surface * MyTTF_Render_BlendedU( SDL_Surface * render_text, \
				     Uint16 *text, int size, \
				     Uint8 style, SDL_Color color )
{
  SDL_Surface * render_tmp;
  char tmpstr[512];
  TTF_Font *font;
  SDLGuiTK_Theme * theme;

  if( render_text!=NULL ) {
    SDL_FreeSurface( render_text );
    render_text = NULL;
  }

  theme = PROT__theme_get_and_lock();
  font = TTF_OpenFont( theme->font_file, size );

  if ( font==NULL ) {
    sprintf( tmpstr, "Couldn't load %d pt font from %s: %s\n",
	     size, theme->font_file, SDL_GetError());
    SDLGUITK_ERROR( tmpstr );
    exit(2);
  }
  TTF_SetFontStyle( font, style );
  render_tmp = TTF_RenderUNICODE_Blended( font, text, \
					  color );
  //render_text = SDL_DisplayFormatAlpha( render_tmp );
  render_text = render_tmp;
/*   render_text = render_tmp; */
  MySDL_FreeSurface( render_tmp );
  PROT__theme_unlock( theme );
  TTF_CloseFont(font);

  return render_text;
}
Exemple #3
0
void draw_text(GuiCanvas *canvas, GuiText *text)
{
#ifdef USE_TTF
	SDL_Surface *surface;
	SDL_Color color;
	SDL_Rect rect;

	if (text == NULL || gui == NULL) return;
	if (text->font == NULL) return;

	surface = (SDL_Surface *)canvas;
	COLOR_SDL(color, text->color);
	RECT_SDL (rect, text->rect);

	if (text->font->fonthandle == NULL)
		text->font->fonthandle = TTF_OpenFont(text->font->filename, FONT_SIZE);
	if (text->font->fonthandle == NULL) return;

	if (text->surface == NULL && text->unicode) {
		uint16_t buffer[BUF_LEN];
		UTF8_to_UNICODE(buffer, text->unicode);
		text->surface = TTF_RenderUNICODE_Blended((TTF_Font *)text->font->fonthandle, buffer, color);
	}
	if (text->surface)
		SDL_BlitSurface(text->surface, NULL, surface, &rect);
#endif
}
Exemple #4
0
void Console::writeText(const Uint16 * text, int size, SDL_Color color){
	wprintf((const wchar_t*)text);
	printf("\n");
	/* font start*/
	SDL_Surface * tempsurface = 0;
	TTF_Font *font;
	font=TTF_OpenFont(this->m_font.c_str(), size);
	if(!font) {
		printf("TTF_OpenFont: %s\n", TTF_GetError());
		// handle error
	}
	/*do stuff with font*/
	int height, width;
	SDL_Rect r;
	r.x = 0;
	r.y = 0;
	int mul = this->m_lines.size();
	//if (mul<1) mul=1;
	if (font) {
		TTF_SizeUNICODE(font, text, &width, &height);					
		tempsurface = TTF_RenderUNICODE_Blended(font, text, color);
		r.w = width;
		r.h = height;
		r.y = height * mul;
		//printf("%d %d %d %d\n", r.w, r.h, r.x, m_lines.size());
		SDL_FillRect(this->m_psurface, &r, SDL_MapRGBA(this->m_psurface->format, 0,0,0,0));
		SDL_BlitSurface(tempsurface, NULL, this->m_psurface, &r);
		TTF_CloseFont(font);
		font=NULL; // to be safe...
		}
	delete (tempsurface);
	/* font end */
}
Exemple #5
0
SDL_Surface *image_sdl_create_from_text(const char *text, enum text_encoding enc,
										struct rgba8888 color, const char *font_name,
										uint32_t font_size, int font_style)
{
	const SDL_Color RGB_Noname = {color.r, color.g, color.b, color.a};
	SDL_Surface 	*surf = NULL;
	TTF_Font		*font;

	assert(text);
	assert(enc != TEXTENCODING_UNKNOWN);
	assert(font_name);
	assert(strlen(font_name) > 0);
	assert(font_size < 1000);
	if(strlen(text) == 0)
		return NULL;

	/* Load font and draw text. */
	font = font_lib_seek(font_name, font_size);
	if(font == NULL) {
		sg_log_err("font_lib_get_font(%s, %d) returns null.",
				   font_name, font_size);
		return NULL;
	}
	font_set_style(font, font_style);
	if(enc == TEXTENCODING_UTF8)
		surf = TTF_RenderUTF8_Blended(font, text, RGB_Noname);
	else if(enc == TEXTENCODING_ANSI)
		surf = TTF_RenderText_Blended(font, text, RGB_Noname);
	else if(enc == TEXTENCODING_UNICODE)
		surf = TTF_RenderUNICODE_Blended(font, (Uint16 *)text, RGB_Noname);
	if(!surf)
		sg_log_err("TTF_Render_Blended returns null.");

	return surf;
}
Exemple #6
0
extern DECLSPEC SDL_Surface * SDLCALL
  TTF_RenderUNICODE_Blended_p(
    TTF_Font *font,
    uint16_t *text,
    SDL_Color *fg) {

  return TTF_RenderUNICODE_Blended(font, text, *fg);
}
Exemple #7
0
Image* Graphics::NewImage(std::wstring &text, SDL_Color &color, bool fast){
	std::basic_string<Uint16> utext (text.begin(),text.end());
	SDL_Surface* tmp = fast ? TTF_RenderUNICODE_Solid(font, utext.c_str(), color) : TTF_RenderUNICODE_Blended(font, utext.c_str(), color);
	if( tmp == NULL ) std::cout << std::endl << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError();
	Image* image = new Image();
	image->texture = SDL_CreateTextureFromSurface(renderer, tmp);
	image->h=tmp->h;
	image->w=tmp->w;
	SDL_FreeSurface(tmp);
	return image;
}
Exemple #8
0
/**************************************************************************
  Create Text Surface from SDL_String16
**************************************************************************/
static SDL_Surface *create_str16_surf(SDL_String16 * pString)
{
  SDL_Surface *pText = NULL; /* FIXME: possibly uninitialized */

  if (!pString) {
    return NULL;
  }

  if (!((pString->style & 0x0F) & TTF_STYLE_NORMAL)) {
    TTF_SetFontStyle(pString->font, (pString->style & 0x0F));
  }

  switch (pString->render) {
  case 0:
    pText = TTF_RenderUNICODE_Shaded(pString->font,
				     pString->text, pString->fgcol,
				     pString->bgcol);
    break;
  case 1:
  {
    SDL_Surface *pTmp = TTF_RenderUNICODE_Solid(pString->font,
				    pString->text, pString->fgcol);

    if ((pText = SDL_DisplayFormat(pTmp)) == NULL) {
      log_error("SDL_create_str16_surf: couldn't convert text "
                "to display format: %s", SDL_GetError());
      pText = pTmp;
    } else {
      FREESURFACE( pTmp );
    }
    
  }
  break;
  case 2:
    pText = TTF_RenderUNICODE_Blended(pString->font,
				      pString->text, pString->fgcol);
    break;
  }

  if (pText != NULL) {
    log_debug("SDL_create_str16_surf: Font is generally %d big, and "
              "string is %hd big", TTF_FontHeight(pString->font), pText->h);
    log_debug("SDL_create_str16_surf: String is %d length", pText->w);
  } else {
    log_debug("SDL_create_str16_surf: pText NULL");
    pText = create_surf_alpha(0, 0, SDL_SWSURFACE);
  }

  if (!((pString->style & 0x0F) & TTF_STYLE_NORMAL)) {
    TTF_SetFontStyle(pString->font, TTF_STYLE_NORMAL);
  }

  return pText;
}
Exemple #9
0
static mrb_value
mrb_sdl2_ttf_font_render_UNICODE_blended(mrb_state *mrb, mrb_value self)
{
  mrb_value text; 
  mrb_int r, g, b, a;
  SDL_Surface * c;
  SDL_Color color;
  mrb_get_args(mrb, "Siiii", &text, &r, &g, &b, &a);
  color.r = r;
  color.g = g;
  color.b = b;
  color.a = a;
  c = TTF_RenderUNICODE_Blended(mrb_sdl2_font_get_ptr(mrb, self), (Uint16 *)RSTRING_PTR(text), color);
  if (c == NULL) {
    mruby_sdl2_raise_error(mrb);
    return mrb_false_value();
  }
  return mrb_sdl2_video_surface(mrb, c, 0);
}
JNIEXPORT jlong JNICALL Java_sdljava_x_swig_SWIG_1SDLTTFJNI_TTF_1RenderUNICODE_1Blended(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jlong jarg3) {
    jlong jresult = 0 ;
    TTF_Font *arg1 = (TTF_Font *) 0 ;
    Uint16 *arg2 = (Uint16 *) 0 ;
    SDL_Color arg3 ;
    SDL_Surface *result;
    SDL_Color *argp3 ;
    
    (void)jenv;
    (void)jcls;
    arg1 = *(TTF_Font **)&jarg1; 
    arg2 = *(Uint16 **)&jarg2; 
    argp3 = *(SDL_Color **)&jarg3; 
    if (!argp3) {
        SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "Attempt to dereference null SDL_Color");
        return 0;
    }
    arg3 = *argp3; 
    result = (SDL_Surface *)TTF_RenderUNICODE_Blended(arg1,(Uint16 const *)arg2,arg3);
    
    *(SDL_Surface **)&jresult = result; 
    return jresult;
}
Exemple #11
0
void SDLFontGL::ensureCacheLine(int fnt, int slot)
{

	assert(fnt >= 0 && fnt < nFonts);
	assert(slot >= 0 && slot < MAX_CHARSETS);
	assert(fnts && cols && GlyphCache && haveCacheLine);

	bool & have = hasCacheLine(fnt, slot);
	if (have) {
		return;
	}
	have = true;

	// Lookup requested font
	TTF_Font * font = fnts[fnt];
	assert(font);

	// Grab the native video surface (so we can match its bpp)
	SDL_Surface* videoSurface = SDL_GetVideoSurface();
	assert(videoSurface);
	assert(videoSurface->format->BitsPerPixel == 32);

	// Create a surface for all the characters
	Uint32 rmask, gmask, bmask, amask;
	getRGBAMask(rmask, gmask, bmask, amask);
	SDL_Surface* mainSurface =
		SDL_CreateRGBSurface(SDL_SWSURFACE,
				nChars*nWidth, nHeight,
				videoSurface->format->BitsPerPixel,
				rmask, gmask, bmask, amask);
	assert(mainSurface);

	// Render font in white, will colorize on-the-fly
	SDL_Color fg = { 255, 255, 255 };

	// Set texture to entirely clear
	// TODO: Needed?
	Uint32 fillColor = SDL_MapRGBA(mainSurface->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
	SDL_FillRect(mainSurface, NULL, fillColor);

	// For each character, render the glyph, and put in the appropriate location
	for(int i = 0; i < nChars; ++i)
	{
		// Lookup this character, and make a single-char string out of it.
		Uint16 C = charMappings[slot].map[i];
		if (!C) C = (Uint16)i;
		Uint16 buf[2] = { C, 0 };

		SDL_Surface* surface = TTF_RenderUNICODE_Blended(font, (const Uint16*)buf, fg);
		if (surface)
		{
			SDL_SetAlpha(surface, 0, 0);

			SDL_Rect dstRect = { 0, 0, nWidth, nHeight };
			dstRect.x = i*nWidth;

			SDL_BlitSurface(surface, 0, mainSurface, &dstRect);

			SDL_FreeSurface(surface);
		}
	}

	// Now upload the big set of characters as a single texture:
	{
		int nMode = getGLFormat();

		glBindTexture(GL_TEXTURE_2D, GlyphCache);

		// Upload this font to its place in the big texture
		glTexSubImage2D(GL_TEXTURE_2D, 0,
				0, (slot*nFonts + fnt)*nHeight,
				mainSurface->w, mainSurface->h,
				nMode, GL_UNSIGNED_BYTE, mainSurface->pixels);
		glFlush();
	}

	SDL_FreeSurface(mainSurface);
}
Exemple #12
0
int main(int argc, char *argv[])
{
    char *argv0 = argv[0];
    SDL_Window *window;
    SDL_GLContext context;
    TTF_Font *font;
    SDL_Surface *text;
    int ptsize;
    int i, done;
    SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
    SDL_Color black = { 0x00, 0x00, 0x00, 0 };
    SDL_Color *forecol;
    SDL_Color *backcol;
    GLenum gl_error;
    GLuint texture;
    int x, y, w, h;
    GLfloat texcoord[4];
    GLfloat texMinX, texMinY;
    GLfloat texMaxX, texMaxY;
        float color[8][3]= {{ 1.0,  1.0,  0.0},
                { 1.0,  0.0,  0.0},
                { 0.0,  0.0,  0.0},
                { 0.0,  1.0,  0.0},
                { 0.0,  1.0,  1.0},
                { 1.0,  1.0,  1.0},
                { 1.0,  0.0,  1.0},
                { 0.0,  0.0,  1.0}};
    float cube[8][3]= {{ 0.5,  0.5, -0.5},
               { 0.5, -0.5, -0.5},
               {-0.5, -0.5, -0.5},
               {-0.5,  0.5, -0.5},
               {-0.5,  0.5,  0.5},
               { 0.5,  0.5,  0.5},
               { 0.5, -0.5,  0.5},
               {-0.5, -0.5,  0.5}};
    SDL_Event event;
    int renderstyle;
    int dump;
    enum {
        RENDER_LATIN1,
        RENDER_UTF8,
        RENDER_UNICODE
    } rendertype;
    char *message;

    /* Look for special execution mode */
    dump = 0;
    /* Look for special rendering types */
    renderstyle = TTF_STYLE_NORMAL;
    rendertype = RENDER_LATIN1;
    /* Default is black and white */
    forecol = &black;
    backcol = &white;
    for ( i=1; argv[i] && argv[i][0] == '-'; ++i ) {
        if ( strcmp(argv[i], "-utf8") == 0 ) {
            rendertype = RENDER_UTF8;
        } else
        if ( strcmp(argv[i], "-unicode") == 0 ) {
            rendertype = RENDER_UNICODE;
        } else
        if ( strcmp(argv[i], "-b") == 0 ) {
            renderstyle |= TTF_STYLE_BOLD;
        } else
        if ( strcmp(argv[i], "-i") == 0 ) {
            renderstyle |= TTF_STYLE_ITALIC;
        } else
        if ( strcmp(argv[i], "-u") == 0 ) {
            renderstyle |= TTF_STYLE_UNDERLINE;
        } else
        if ( strcmp(argv[i], "-dump") == 0 ) {
            dump = 1;
        } else
        if ( strcmp(argv[i], "-fgcol") == 0 ) {
            int r, g, b;
            if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
                fprintf(stderr, Usage, argv0);
                return(1);
            }
            forecol->r = (Uint8)r;
            forecol->g = (Uint8)g;
            forecol->b = (Uint8)b;
        } else
        if ( strcmp(argv[i], "-bgcol") == 0 ) {
            int r, g, b;
            if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
                fprintf(stderr, Usage, argv0);
                return(1);
            }
            backcol->r = (Uint8)r;
            backcol->g = (Uint8)g;
            backcol->b = (Uint8)b;
        } else {
            fprintf(stderr, Usage, argv0);
            return(1);
        }
    }
    argv += i;
    argc -= i;

    /* Check usage */
    if ( ! argv[0] ) {
        fprintf(stderr, Usage, argv0);
        return(1);
    }

    /* Initialize the TTF library */
    if ( TTF_Init() < 0 ) {
        fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
        SDL_Quit();
        return(2);
    }

    /* Open the font file with the requested point size */
    ptsize = 0;
    if ( argc > 1 ) {
        ptsize = atoi(argv[1]);
    }
    if ( ptsize == 0 ) {
        i = 2;
        ptsize = DEFAULT_PTSIZE;
    } else {
        i = 3;
    }
    font = TTF_OpenFont(argv[0], ptsize);
    if ( font == NULL ) {
        fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",
                    ptsize, argv[0], SDL_GetError());
        cleanup(2);
    }
    TTF_SetFontStyle(font, renderstyle);

    if( dump ) {
        for( i = 48; i < 123; i++ ) {
            SDL_Surface* glyph = NULL;

            glyph = TTF_RenderGlyph_Shaded( font, i, *forecol, *backcol );

            if( glyph ) {
                char outname[64];
                sprintf( outname, "glyph-%d.bmp", i );
                SDL_SaveBMP( glyph, outname );
            }

        }
        cleanup(0);
    }

    /* Set a 640x480 video mode */
    window = SDL_CreateWindow("glfont",
                                SDL_WINDOWPOS_UNDEFINED,
                                SDL_WINDOWPOS_UNDEFINED,
                                WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
    if ( window == NULL ) {
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
        cleanup(2);
    }

    context = SDL_GL_CreateContext(window);
    if ( context == NULL ) {
        fprintf(stderr, "Couldn't create OpenGL context: %s\n", SDL_GetError());
        cleanup(2);
    }

    /* Render and center the message */
    if ( argc > 2 ) {
        message = argv[2];
    } else {
        message = DEFAULT_TEXT;
    }
    switch (rendertype) {
        case RENDER_LATIN1:
        text = TTF_RenderText_Blended(font, message, *forecol);
        break;

        case RENDER_UTF8:
        text = TTF_RenderUTF8_Blended(font, message, *forecol);
        break;

        case RENDER_UNICODE:
        {
            /* This doesn't actually work because you can't pass
               UNICODE text in via command line, AFAIK, but...
             */
            Uint16 unicode_text[BUFSIZ];
            int index;
            for ( index = 0; (message[0] || message[1]); ++index ) {
                unicode_text[index]  = ((Uint8 *)message)[0];
                unicode_text[index] <<= 8;
                unicode_text[index] |= ((Uint8 *)message)[1];
                message += 2;
            }
            text = TTF_RenderUNICODE_Blended(font,
                    unicode_text, *forecol);
        }
        break;
        default:
        text = NULL; /* This shouldn't happen */
        break;
    }
    if ( text == NULL ) {
        fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError());
        TTF_CloseFont(font);
        cleanup(2);
    }
    x = (WIDTH - text->w)/2;
    y = (HEIGHT - text->h)/2;
    w = text->w;
    h = text->h;
    printf("Font is generally %d big, and string is %hd big\n",
                        TTF_FontHeight(font), text->h);

    /* Convert the text into an OpenGL texture */
    glGetError();
    texture = SDL_GL_LoadTexture(text, texcoord);
    if ( (gl_error = glGetError()) != GL_NO_ERROR ) {
        /* If this failed, the text may exceed texture size limits */
        printf("Warning: Couldn't create texture: 0x%x\n", gl_error);
    }

    /* Make texture coordinates easy to understand */
    texMinX = texcoord[0];
    texMinY = texcoord[1];
    texMaxX = texcoord[2];
    texMaxY = texcoord[3];

    /* We don't need the original text surface anymore */
    SDL_FreeSurface(text);

    /* Initialize the GL state */
    glViewport( 0, 0, WIDTH, HEIGHT );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );

    glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );

    glEnable(GL_DEPTH_TEST);

    glDepthFunc(GL_LESS);

    glShadeModel(GL_SMOOTH);

    /* Wait for a keystroke, and blit text on mouse press */
    done = 0;
    while ( ! done ) {
        while ( SDL_PollEvent(&event) ) {
            switch (event.type) {
                case SDL_MOUSEMOTION:
                x = event.motion.x - w/2;
                y = event.motion.y - h/2;
                break;

                case SDL_KEYDOWN:
                case SDL_QUIT:
                done = 1;
                break;
                default:
                break;
            }
        }

        /* Clear the screen */
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        /* Draw the spinning cube */
        glBegin( GL_QUADS );

            glColor3fv(color[0]);
            glVertex3fv(cube[0]);
            glColor3fv(color[1]);
            glVertex3fv(cube[1]);
            glColor3fv(color[2]);
            glVertex3fv(cube[2]);
            glColor3fv(color[3]);
            glVertex3fv(cube[3]);

            glColor3fv(color[3]);
            glVertex3fv(cube[3]);
            glColor3fv(color[4]);
            glVertex3fv(cube[4]);
            glColor3fv(color[7]);
            glVertex3fv(cube[7]);
            glColor3fv(color[2]);
            glVertex3fv(cube[2]);

            glColor3fv(color[0]);
            glVertex3fv(cube[0]);
            glColor3fv(color[5]);
            glVertex3fv(cube[5]);
            glColor3fv(color[6]);
            glVertex3fv(cube[6]);
            glColor3fv(color[1]);
            glVertex3fv(cube[1]);

            glColor3fv(color[5]);
            glVertex3fv(cube[5]);
            glColor3fv(color[4]);
            glVertex3fv(cube[4]);
            glColor3fv(color[7]);
            glVertex3fv(cube[7]);
            glColor3fv(color[6]);
            glVertex3fv(cube[6]);

            glColor3fv(color[5]);
            glVertex3fv(cube[5]);
            glColor3fv(color[0]);
            glVertex3fv(cube[0]);
            glColor3fv(color[3]);
            glVertex3fv(cube[3]);
            glColor3fv(color[4]);
            glVertex3fv(cube[4]);

            glColor3fv(color[6]);
            glVertex3fv(cube[6]);
            glColor3fv(color[1]);
            glVertex3fv(cube[1]);
            glColor3fv(color[2]);
            glVertex3fv(cube[2]);
            glColor3fv(color[7]);
            glVertex3fv(cube[7]);
        glEnd( );

        /* Rotate the cube */
        glMatrixMode(GL_MODELVIEW);
        glRotatef(5.0, 1.0, 1.0, 1.0);

        /* Show the text on the screen */
        SDL_GL_Enter2DMode(WIDTH, HEIGHT);
        glBindTexture(GL_TEXTURE_2D, texture);
        glBegin(GL_TRIANGLE_STRIP);
        glTexCoord2f(texMinX, texMinY); glVertex2i(x,   y  );
        glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y  );
        glTexCoord2f(texMinX, texMaxY); glVertex2i(x,   y+h);
        glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
        glEnd();
        SDL_GL_Leave2DMode();

        /* Swap the buffers so everything is visible */
        SDL_GL_SwapWindow(window);
    }
    SDL_GL_DeleteContext(context);
    TTF_CloseFont(font);
    cleanup(0);

    /* Not reached, but fixes compiler warnings */
    return 0;
}
Exemple #13
0
enum Edit_Return_Codes edit_field(struct widget *pEdit_Widget)
{
  struct EDIT pEdt;
  struct UniChar ___last;
  struct UniChar *pInputChain_TMP = NULL;
  enum Edit_Return_Codes ret;
  void *backup = pEdit_Widget->data.ptr;
  
  pEdt.pWidget = pEdit_Widget;
  pEdt.ChainLen = 0;
  pEdt.Truelength = 0;
  pEdt.Start_X = adj_size(5);
  pEdt.InputChain_X = 0;
  
  pEdit_Widget->data.ptr = (void *)&pEdt;
  
  SDL_EnableUNICODE(1);

  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

  pEdt.pBg = create_bcgnd_surf(pEdit_Widget->theme, 2,
			       pEdit_Widget->size.w, pEdit_Widget->size.h);

  /* Creating Chain */
  pEdt.pBeginTextChain = text2chain(pEdit_Widget->string16->text);


  /* Creating Empty (Last) pice of Chain */
  pEdt.pInputChain = &___last;
  pEdt.pEndTextChain = pEdt.pInputChain;
  pEdt.pEndTextChain->chr[0] = 32;	/*spacebar */
  pEdt.pEndTextChain->chr[1] = 0;	/*spacebar */
  pEdt.pEndTextChain->next = NULL;
  pEdt.pEndTextChain->prev = NULL;
  
  /* set font style (if any ) */
  if (!((pEdit_Widget->string16->style & 0x0F) & TTF_STYLE_NORMAL)) {
    TTF_SetFontStyle(pEdit_Widget->string16->font,
		     (pEdit_Widget->string16->style & 0x0F));
  }


  pEdt.pEndTextChain->pTsurf =
      TTF_RenderUNICODE_Blended(pEdit_Widget->string16->font,
			      pEdt.pEndTextChain->chr,
			      pEdit_Widget->string16->fgcol);
  
  /* create surface for each font in chain and find chain length */
  if (pEdt.pBeginTextChain) {
    pInputChain_TMP = pEdt.pBeginTextChain;
    while (TRUE) {
      pEdt.ChainLen++;

      pInputChain_TMP->pTsurf =
	  TTF_RenderUNICODE_Blended(pEdit_Widget->string16->font,
				    pInputChain_TMP->chr,
				    pEdit_Widget->string16->fgcol);

      pEdt.Truelength += pInputChain_TMP->pTsurf->w;

      if (pInputChain_TMP->next == NULL) {
	break;
      }

      pInputChain_TMP = pInputChain_TMP->next;
    }
    /* set terminator of list */
    pInputChain_TMP->next = pEdt.pInputChain;
    pEdt.pInputChain->prev = pInputChain_TMP;
    pInputChain_TMP = NULL;
  } else {
    pEdt.pBeginTextChain = pEdt.pInputChain;
  }

  redraw_edit_chain(&pEdt);
  
  set_wstate(pEdit_Widget, FC_WS_PRESSED);
  {
    /* local loop */  
    Uint16 rety = gui_event_loop((void *)&pEdt, NULL,
  	edit_key_down, NULL, edit_mouse_button_down, NULL, NULL);
    
    if (pEdt.pBeginTextChain == pEdt.pEndTextChain) {
      pEdt.pBeginTextChain = NULL;
    }
    
    if (rety == MAX_ID) {
      ret = ED_FORCE_EXIT;
    } else {
      ret = (enum Edit_Return_Codes) rety;
      
      /* this is here becouse we have no knowladge that pEdit_Widget exist
         or nor in force exit mode from gui loop */
  
      /* reset font settings */
      if (!((pEdit_Widget->string16->style & 0x0F) & TTF_STYLE_NORMAL)) {
        TTF_SetFontStyle(pEdit_Widget->string16->font, TTF_STYLE_NORMAL);
      }
      
      if(ret != ED_ESC) {
        FC_FREE(pEdit_Widget->string16->text);
        pEdit_Widget->string16->text =
  	    chain2text(pEdt.pBeginTextChain, pEdt.ChainLen);
        pEdit_Widget->string16->n_alloc = (pEdt.ChainLen + 1) * sizeof(Uint16);
      }
      
      pEdit_Widget->data.ptr = backup;
      set_wstate(pEdit_Widget, FC_WS_NORMAL);    
    }
  }
  
  FREESURFACE(pEdt.pEndTextChain->pTsurf);
   
  del_chain(pEdt.pBeginTextChain);
  
  FREESURFACE(pEdt.pBg);
    
  /* disable repeate key */
  SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);

  /* disable Unicode */
  SDL_EnableUNICODE(0);
    
  return ret;
}
Exemple #14
0
/**************************************************************************
  This functions are pure madness :)
  Create Edit Field surface ( with Text) and blit them to Main.screen,
  on position 'pEdit_Widget->size.x , pEdit_Widget->size.y'

  Main role of this functions are been text input to GUI.
  This code allow you to add, del unichar from unistring.

  Graphic is taken from 'pEdit_Widget->theme'
  OldText is taken from	'pEdit_Widget->sting16'

  NewText is returned to 'pEdit_Widget->sting16' ( after free OldText )

  if flag 'FW_DRAW_THEME_TRANSPARENT' is set theme will be blit
  transparent ( Alpha = 128 )

  NOTE: This functions can return NULL in 'pEdit_Widget->sting16->text' but
        never free 'pEdit_Widget->sting16' struct.
**************************************************************************/
static Uint16 edit_key_down(SDL_keysym Key, void *pData)
{
  struct EDIT *pEdt = (struct EDIT *)pData;
  struct UniChar *pInputChain_TMP;
  bool Redraw = FALSE;
      
  /* find which key is pressed */
  switch (Key.sym) {
    case SDLK_ESCAPE:
      /* exit from loop without changes */
      return ED_ESC;
    case SDLK_RETURN:
    case SDLK_KP_ENTER:
      /* exit from loop */
      return ED_RETURN;
    case SDLK_KP6:
      if(Key.mod & KMOD_NUM) {
	goto INPUT;
      }
    case SDLK_RIGHT:
    {
      /* move cursor right */
      if (pEdt->pInputChain->next) {
	
       if (pEdt->InputChain_X >= (pEdt->pWidget->size.x + pEdt->pBg->w - adj_size(10))) {
	pEdt->Start_X -= pEdt->pInputChain->pTsurf->w -
		(pEdt->pWidget->size.x + pEdt->pBg->w - adj_size(5) - pEdt->InputChain_X);
       }

	pEdt->pInputChain = pEdt->pInputChain->next;
	Redraw = TRUE;
      }
    }
    break;
    case SDLK_KP4:
      if(Key.mod & KMOD_NUM) {
	goto INPUT;
      }
    case SDLK_LEFT:
    {
      /* move cursor left */
      if (pEdt->pInputChain->prev) {
        pEdt->pInputChain = pEdt->pInputChain->prev;
	if ((pEdt->InputChain_X <=
	       (pEdt->pWidget->size.x + adj_size(9))) && (pEdt->Start_X != adj_size(5))) {
	  if (pEdt->InputChain_X != (pEdt->pWidget->size.x + adj_size(5))) {
	      pEdt->Start_X += (pEdt->pWidget->size.x - pEdt->InputChain_X + adj_size(5));
	  }

	  pEdt->Start_X += (pEdt->pInputChain->pTsurf->w);
	}
	Redraw = TRUE;
      }
    }
    break;
    case SDLK_KP7:
      if(Key.mod & KMOD_NUM) {
	goto INPUT;
      }  
    case SDLK_HOME:
    {
      /* move cursor to begin of chain (and edit field) */
      pEdt->pInputChain = pEdt->pBeginTextChain;
      Redraw = TRUE;
      pEdt->Start_X = adj_size(5);
    }
    break;
    case SDLK_KP1:
      if(Key.mod & KMOD_NUM) {
	goto INPUT;
      }
    case SDLK_END:
    {
	/* move cursor to end of chain (and edit field) */
      pEdt->pInputChain = pEdt->pEndTextChain;
      Redraw = TRUE;

      if (pEdt->pWidget->size.w - pEdt->Truelength < 0) {
	  pEdt->Start_X = pEdt->pWidget->size.w - pEdt->Truelength - adj_size(5);
      }
    }
    break;
    case SDLK_BACKSPACE:
    {
	/* del element of chain (and move cursor left) */
      if (pEdt->pInputChain->prev) {

	if ((pEdt->InputChain_X <=
	       (pEdt->pWidget->size.x + adj_size(9))) && (pEdt->Start_X != adj_size(5))) {
	  if (pEdt->InputChain_X != (pEdt->pWidget->size.x + adj_size(5))) {
	      pEdt->Start_X += (pEdt->pWidget->size.x - pEdt->InputChain_X + adj_size(5));
	  }
	  pEdt->Start_X += (pEdt->pInputChain->prev->pTsurf->w);
	}

	if (pEdt->pInputChain->prev->prev) {
	  pEdt->pInputChain->prev->prev->next = pEdt->pInputChain;
	  pInputChain_TMP = pEdt->pInputChain->prev->prev;
	  pEdt->Truelength -= pEdt->pInputChain->prev->pTsurf->w;
	  FREESURFACE(pEdt->pInputChain->prev->pTsurf);
	  FC_FREE(pEdt->pInputChain->prev);
	  pEdt->pInputChain->prev = pInputChain_TMP;
	} else {
	  pEdt->Truelength -= pEdt->pInputChain->prev->pTsurf->w;
	  FREESURFACE(pEdt->pInputChain->prev->pTsurf);
	  FC_FREE(pEdt->pInputChain->prev);
	  pEdt->pBeginTextChain = pEdt->pInputChain;
	}
	
	pEdt->ChainLen--;
	Redraw = TRUE;
      }
    }
    break;
    case SDLK_KP_PERIOD:
      if(Key.mod & KMOD_NUM) {
	goto INPUT;
      }  
    case SDLK_DELETE:
    {
	/* del element of chain */
      if (pEdt->pInputChain->next && pEdt->pInputChain->prev) {
	pEdt->pInputChain->prev->next = pEdt->pInputChain->next;
	pEdt->pInputChain->next->prev = pEdt->pInputChain->prev;
	pInputChain_TMP = pEdt->pInputChain->next;
	pEdt->Truelength -= pEdt->pInputChain->pTsurf->w;
	FREESURFACE(pEdt->pInputChain->pTsurf);
	FC_FREE(pEdt->pInputChain);
	pEdt->pInputChain = pInputChain_TMP;
	pEdt->ChainLen--;
	Redraw = TRUE;
      }

      if (pEdt->pInputChain->next && !pEdt->pInputChain->prev) {
	pEdt->pInputChain = pEdt->pInputChain->next;
	pEdt->Truelength -= pEdt->pInputChain->prev->pTsurf->w;
	FREESURFACE(pEdt->pInputChain->prev->pTsurf);
	FC_FREE(pEdt->pInputChain->prev);
	pEdt->pBeginTextChain = pEdt->pInputChain;
	pEdt->ChainLen--;
	Redraw = TRUE;
      }
    }
    break;
    default:
    {
INPUT:/* add new element of chain (and move cursor right) */
      if (Key.unicode) {
	if (pEdt->pInputChain != pEdt->pBeginTextChain) {
	  pInputChain_TMP = pEdt->pInputChain->prev;
	  pEdt->pInputChain->prev = fc_calloc(1, sizeof(struct UniChar));
	  pEdt->pInputChain->prev->next = pEdt->pInputChain;
	  pEdt->pInputChain->prev->prev = pInputChain_TMP;
	  pInputChain_TMP->next = pEdt->pInputChain->prev;
	} else {
	  pEdt->pInputChain->prev = fc_calloc(1, sizeof(struct UniChar));
	  pEdt->pInputChain->prev->next = pEdt->pInputChain;
	  pEdt->pBeginTextChain = pEdt->pInputChain->prev;
	}
        
        pEdt->pInputChain->prev->chr[0] = Key.unicode;        
	pEdt->pInputChain->prev->chr[1] = '\0';

	if (pEdt->pInputChain->prev->chr) {
	  if (get_wflags(pEdt->pWidget) & WF_PASSWD_EDIT) {
	    Uint16 passwd_chr[2] = {'*', '\0'};
	    
	    pEdt->pInputChain->prev->pTsurf =
	      TTF_RenderUNICODE_Blended(pEdt->pWidget->string16->font,
					  passwd_chr,
					  pEdt->pWidget->string16->fgcol);
	  } else {
	    pEdt->pInputChain->prev->pTsurf =
	      TTF_RenderUNICODE_Blended(pEdt->pWidget->string16->font,
					  pEdt->pInputChain->prev->chr,
					  pEdt->pWidget->string16->fgcol);
	  }
	  pEdt->Truelength += pEdt->pInputChain->prev->pTsurf->w;
	}

	if (pEdt->InputChain_X >= pEdt->pWidget->size.x + pEdt->pBg->w - adj_size(10)) {
	  if (pEdt->pInputChain == pEdt->pEndTextChain) {
	    pEdt->Start_X = pEdt->pBg->w - adj_size(5) - pEdt->Truelength;
	  } else {
	    pEdt->Start_X -= pEdt->pInputChain->prev->pTsurf->w -
		  (pEdt->pWidget->size.x + pEdt->pBg->w - adj_size(5) - pEdt->InputChain_X);
	  }
	}
	
	pEdt->ChainLen++;
	Redraw = TRUE;
      }
    }
    break;
  }				/* key pressed switch */
    
  if (Redraw) {
    redraw_edit_chain(pEdt);
  }
    
  return ID_ERROR;
}
Exemple #15
0
surface font::render_blended(std::basic_string<uint16_t> str, color c) {
    return surface().surface::build(TTF_RenderUNICODE_Blended(get_low()
                                    ,str.c_str()
                                    ,color::exchange_color<SDL_Color>(c)));
}
int ttf_font_info::_draw_text(SDL_Surface *s, const char *text, size_t length, int x, int y, uint32 pixel, uint16 style, bool utf8) const
{
	int clip_top, clip_bottom, clip_left, clip_right;
	if (draw_clip_rect_active) {
		clip_top = draw_clip_rect.top;
		clip_bottom = draw_clip_rect.bottom;
		clip_left = draw_clip_rect.left;
		clip_right = draw_clip_rect.right;
	} else {
		clip_top = clip_left = 0;
		clip_right = s->w;
		clip_bottom = s->h;
	}

	SDL_Color c;
	SDL_GetRGB(pixel, s->format, &c.r, &c.g, &c.b);
	c.a = 0xff;
	SDL_Surface *text_surface = 0;
	if (utf8) 
	{
		char *temp = process_printable(text, length);
		if (environment_preferences->smooth_text)
			text_surface = TTF_RenderUTF8_Blended(get_ttf(style), temp, c);	
		else
			text_surface = TTF_RenderUTF8_Solid(get_ttf(style), temp, c);
	}
	else
	{
		uint16 *temp = process_macroman(text, length);
		if (environment_preferences->smooth_text)
			text_surface = TTF_RenderUNICODE_Blended(get_ttf(style), temp, c);
		else
			text_surface = TTF_RenderUNICODE_Solid(get_ttf(style), temp, c);
	}
	if (!text_surface) return 0;
	
	SDL_Rect dst_rect;
	dst_rect.x = x;
	dst_rect.y = y - TTF_FontAscent(get_ttf(style));

	if (draw_clip_rect_active)
	{
		SDL_Rect src_rect;
		src_rect.x = 0;
		src_rect.y = 0;

		if (clip_top > dst_rect.y)
		{
			src_rect.y += dst_rect.y - clip_top;
		}

		if (clip_left > dst_rect.x)
		{
			src_rect.x += dst_rect.x - clip_left;
		}

		src_rect.w = (clip_right > dst_rect.x) ? clip_right - dst_rect.x : 0;
		src_rect.h = (clip_bottom > dst_rect.y) ? clip_bottom - dst_rect.y : 0;

		SDL_BlitSurface(text_surface, &src_rect, s, &dst_rect);
	}
	else
		SDL_BlitSurface(text_surface, NULL, s, &dst_rect);

	if (s == MainScreenSurface())
		MainScreenUpdateRect(x, y - TTF_FontAscent(get_ttf(style)), text_width(text, style, utf8), TTF_FontHeight(get_ttf(style)));

	int width = text_surface->w;
	SDL_FreeSurface(text_surface);
	return width;
}
Exemple #17
0
	SDL_Surface *
	Font::UnicodeTextRender(const char16_t *text, SDL_Color color)
	{
		TTF_RenderUNICODE_Blended(mFont, (const Uint16 *)text, color);
	}
// This should be rewritten
void ScoreScrollNode::renderText()
{
	// Init variables
	SDL_Surface *name, *points, *place, *surface;
	surface = _screen->game()->buffer();

	Highscore *hs;
	std::stringstream str, str_place;

	// The texts position on text
	SDL_Rect position;
	position.x = 0;

	// Get scores
	highscore_list &scores = _screen->menu()->manager.get();

	// Create surface on which to draw stuff
	int num_scores = scores.size();
	int height = num_scores * 2 * _font_height;

	_height = height;

	// Kill old stuff
	for ( score_triplet_list::iterator iter = _scores.begin(); iter != _scores.end(); iter++ )
	{
		SDL_Surface **surfaces = (*iter);
		SDL_FreeSurface(surfaces[0]);
		SDL_FreeSurface(surfaces[1]);
		SDL_FreeSurface(surfaces[2]);
		delete [] surfaces;
	}

	_scores.erase(_scores.begin(), _scores.end());

	int i = 0;
	for ( highscore_list::iterator iter = scores.begin(); iter != scores.end(); iter++, i++ )
	{
		hs = *iter;
		str << hs->score << " points";
		str_place << (i + 1) << ". ";

		// Render texts
		place = TTF_RenderText_Blended(_font, str_place.str().c_str(), _name_color);
		name = TTF_RenderUNICODE_Blended(_font, hs->name, _name_color);
		points = TTF_RenderText_Blended(_font, str.str().c_str(), _score_color);

		SDL_Surface **surfaces = new SDL_Surface*[3];
		surfaces[0] = place;
		surfaces[1] = name;
		surfaces[2] = points;
		_scores.push_back(surfaces);

		str.str("");
		str_place.str("");
	}

	// draw()
	// the area on which we draw ourselves
	SDL_Rect rect = _screen->makeRect(_pos);

	SDL_FreeSurface(_image);
	_image = SDL_CreateRGBSurface(surface->flags, _pos.w, _pos.h, surface->format->BitsPerPixel, 0, 0, 0, 0);
	SDL_BlitSurface(_screen->menu()->overlay(), &rect, _image, NULL);

	// unit height is the height of one score element
	int unit_height = _font_height * 2;

	int start_score = _scroll_rect.y / unit_height;

	// Check bounds for end_score
	int end_score = _pos.h / unit_height + 2 + start_score;
	end_score = (end_score > _scores.size()) ? _scores.size() : end_score;

	SDL_Rect cp_position;
	position.x = 0;
	position.y = start_score * unit_height - _scroll_rect.y;

	score_triplet_list::iterator iter, to;
	iter = _scores.begin() + start_score;
	to = _scores.begin() + end_score;

	for (; iter != to; iter++)
	{
		SDL_Surface **surfaces = (*iter);

		// Place
		cp_position = position;
		SDL_BlitSurface(surfaces[0], NULL, _image, &cp_position);

		// Name
		position.x += surfaces[0]->clip_rect.w;
		cp_position = position;
		SDL_BlitSurface(surfaces[1], NULL, _image, &cp_position);

		// Points
		position.x -= surfaces[0]->clip_rect.w;
		position.y += _font_height;
		cp_position = position;
		SDL_BlitSurface(surfaces[2], NULL, _image, &cp_position);

		position.y += _font_height;
	}
}
Exemple #19
0
void SDL_GL_RenderTextCyrillic(char *text, 
                      int fontId,
                      SDL_Color color,
                      SDL_Rect *location)
{
	SDL_Surface *initial;
	SDL_Surface *intermediary;

	int w,h;
	GLuint texture;

	TTF_Font *fnt = NULL;
	switch (fontId)
	{
		case 12:
			fnt = fnt_12pt;
		break;
		case 16:
			fnt = fnt_16pt;
		break;
		case 24:
			fnt = fnt_24pt;
		break;
		case 32:
			fnt = fnt_32pt;
		break;
		case 42:
			fnt = fnt_42pt;
		break;
		default:
			fnt = fnt_16pt;
		break;
	}
		
	 wchar_t *utext = MultiCharToUniChar(text);

	/* Use SDL_TTF to render our text */
	Uint16* stext = new Uint16[strlen(text)+1];
		for (size_t i = 0; i < strlen(text); ++i) {
			stext[i] = utext[i]; 
		}
	stext[strlen(text)] = 0;

	initial = TTF_RenderUNICODE_Blended(fnt,stext,color);
	delete[] stext;

	/* Convert the rendered text to a known format */
	w = nextpoweroftwo(initial->w);
	h = nextpoweroftwo(initial->h);
	
	intermediary = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);

	SDL_BlitSurface(initial, 0, intermediary, 0);
	
	/* Tell GL about our new texture */
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA, 
			GL_UNSIGNED_BYTE, intermediary->pixels );
	
	/* GL_NEAREST looks horrible, if scaled... */
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	

	/* prepare to render our texture */
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture);
	glColor3f(1.0f, 1.0f, 1.0f);
	
	/* Draw a quad at location */
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f); 
			glVertex2f(location->x    , location->y);
		glTexCoord2f(1.0f, 1.0f); 
			glVertex2f((float)(location->x + w), location->y);
		glTexCoord2f(1.0f, 0.0f); 
			glVertex2f((float)(location->x + w), (float)(location->y - h));
		glTexCoord2f(0.0f, 0.0f); 
			glVertex2f(location->x    , (float)(location->y - h));
	glEnd();
		
	/* Clean up */
	SDL_FreeSurface(initial);
	SDL_FreeSurface(intermediary);
	glDeleteTextures(1, &texture);
}