Esempio n. 1
0
static VALUE
Font_initialize(VALUE self, VALUE rbRealFilePath, VALUE rbSize,
                VALUE rbBold, VALUE rbItalic, VALUE rbTtcIndex)
{
  const char* path   = StringValueCStr(rbRealFilePath);
  const int size     = NUM2INT(rbSize);
  const bool bold    = RTEST(rbBold);
  const bool italic  = RTEST(rbItalic);
  const int ttcIndex = NUM2INT(rbTtcIndex);

  Font* font;
  Data_Get_Struct(self, Font, font);
  font->size = size;
  font->sdlFont = TTF_OpenFontIndex(path, size, ttcIndex);
  if (!font->sdlFont) {
    rb_raise(strb_GetStarRubyErrorClass(), "%s (%s)", TTF_GetError(), path);
  }
  const int style = TTF_STYLE_NORMAL |
    (bold ? TTF_STYLE_BOLD : 0) | (italic ? TTF_STYLE_ITALIC : 0);
  TTF_SetFontStyle(font->sdlFont, style);

  return Qnil;
}
Esempio n. 2
0
void TextView::setText(string s){

	if(s.empty()){
		if(textSurface != NULL)
			SDL_FreeSurface( textSurface );
		textSurface = NULL;
		return;
	}
	text = s;
	SDL_Surface * newTextSurface;
	newTextSurface = TTF_RenderText_Blended(font, text.c_str(), textColor);

	if(newTextSurface == NULL)
	{
      cerr << "TTF_RenderText_Blended() Failed: " << TTF_GetError() << endl;
      TTF_Quit();
	  return;
	}
	if(textSurface != NULL)
		SDL_FreeSurface( textSurface );
	textSurface = newTextSurface;
	resize(textSurface->w,textSurface->h);
}
Esempio n. 3
0
bool init() {
	bool success=true;
	if(SDL_Init(SDL_INIT_VIDEO)<0) {
		std::cout<<"SDL could not initialize! SDL Error: "<<SDL_GetError()<<std::endl;
		success=false;
	}
	else {
		if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
			std::cout<<"Warning: Linear texture filtering not enabled!";
		}
		gWindow=SDL_CreateWindow("Snake II", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
		if(gWindow==NULL) {
			std::cout<<"Window could not be created! SDL Error: "<<SDL_GetError()<<std::endl;
			success=false;
		}
		else {
			gRenderer=SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
			if(gRenderer==NULL) {
				std::cout<<"Renderer could not be created! SDL Error: "<<SDL_GetError()<<std::endl;
				success=false;
			}
			else {
				SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
				int imgFlags = IMG_INIT_PNG;
				if(!(IMG_Init(imgFlags) & imgFlags)) {
					std::cout<<"SDL_image could not initialize! SDL_image Error: "<<IMG_GetError()<<std::endl;
					success=false;
				}
				if(TTF_Init()==-1) {
					std::cout<<"SDL_ttf could not initialize! SDL_ttf Error: "<<TTF_GetError()<<std::endl;
					success=false;
				}
			}
		}
	}
	return success;
}
Esempio n. 4
0
/** Constructor
 *  Creates a texture instance for rendering text.
 *
 *  @param p_id The string identifier of the resource
 *  @param p_renderer The window renderer used to create the text texture
 *  @param p_font Pointer to a TTF_Font object used to render the text
 *  @param p_textureText The string to render
 *  @param p_textColor The color to render the text as
 */
n8::Texture::Texture(std::string p_id, SDL_Renderer* p_renderer, TTF_Font* p_font, std::string p_textureText, SDL_Color p_textColor ) : Resource(p_id)
{
    
    SDL_Surface* textSurface = TTF_RenderText_Solid( p_font, p_textureText.c_str(), p_textColor );
    if( textSurface == nullptr )
    {
        std::string msg( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
        n8::Log::Error(TAG, msg);
        
        if(p_font == nullptr)
            n8::Log::Error( TAG, "   font was null");
        else
            n8::Log::Error( TAG, "   font wasn't null");
        
        string textMessage( "   texture text was: %s", p_textureText.c_str());
        n8::Log::Error(TAG, textMessage);
    }
    else
    {
        //Create texture from surface pixels
        m_texture = SDL_CreateTextureFromSurface( p_renderer, textSurface );
        if( m_texture == nullptr )
        {
            string textureCreationFailedMsg("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
            n8::Log::Error(TAG, textureCreationFailedMsg);
        }
        else
        {
            //Get image dimensions
            m_width = textSurface->w;
            m_height = textSurface->h;
        }
        
        //Get rid of old surface
        SDL_FreeSurface( textSurface );
    }
}
Esempio n. 5
0
  bool Drawable::loadFromRenderedText( std::string textureText, SDL_Color textColor )
  {
      //Get rid of preexisting texture
      free();

      TTF_Font * gFont = TTF_OpenFont(GlobalDefs::getResource(RESOURCE_FONT, "zorque.ttf").c_str(),40);

      //Render text surface
      SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor );
      TTF_CloseFont(gFont);

      if( textSurface == NULL )
      {
          printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
      }
      else
      {
          //Create texture from surface pixels
          mTexture = SDL_CreateTextureFromSurface( renderer, textSurface );
          if( mTexture == NULL )
          {
              printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
          }
          else
          {
              //Get image dimensions
              image_size = {textSurface->w, textSurface->h};
              render_size = image_size;
          }

          //Get rid of old surface
          SDL_FreeSurface( textSurface );
      }

      //Return success
      return mTexture != NULL;
  }
Esempio n. 6
0
bool Text::LoadText(std::string _textureText, SDL_Color _textColor, TTF_Font* _textureFont)
{
	// Make sure to delete any previous surfaces (unlikely but just in case)
	this->FreeText();

	// Create the new surface by using the SDL_TTF plugin, blended so we have the lovely alpha values of the text
	SDL_Surface* textSurface = TTF_RenderText_Blended(_textureFont, _textureText.c_str(), _textColor);
	if (textSurface == NULL)
	{
		// If we can't make it then error
		printf("Unable to render text surface. SDL_ttf Error: %s\n", TTF_GetError());
	}
	else
	{
		// Otherwise create and store our values for later use
		this->m_textString = _textureText;
		this->m_textColor = _textColor;
		this->m_textFont = _textureFont;
		// Create the texture we're going to draw from the surface we just created
		this->m_text = SDL_CreateTextureFromSurface(this->m_render, textSurface);
		if (m_text == NULL)
		{
			printf("Unable to create texture from rendered text. SDL Error: %s\n", SDL_GetError());
		}
		else
		{
			// Assign the sizes from the surface and store them for drawing
			m_width = textSurface->w;
			m_height = textSurface->h;
			SDL_Rect rect = { 0, 0, m_width, m_height };
			m_rect = rect;
		}
		SDL_FreeSurface(textSurface);
	}

	return this->m_text != NULL;
}
Esempio n. 7
0
TTF_Font* FontManager::getTTF_Font(std::string filename, int pointsize)
{
	TTFId id;
	id.filename = filename;
	id.pointsize = pointsize;

	std::map<TTFId, TTF_Font*>::iterator iter;
	iter = ttf_fonts.find(id);

	if (iter != ttf_fonts.end())
		return iter->second;

	IDataSource* fontids;
	fontids = FileSystem::get_instance()->ReadFile("@data/" + filename);
	if (!fontids) {
		perr << "Failed to open TTF: @data/" << filename << std::endl;
		return 0;
	}

	// open font using SDL_RWops.
	// Note: The RWops and IDataSource will be deleted by the TTF_Font
	TTF_Font* font = TTF_OpenFontRW(fontids->getRWops(), 1, pointsize);

	if (!font) {
		perr << "Failed to open TTF: @data/" << filename
			 << ": " << TTF_GetError() << std::endl;
		return 0;
	}

	ttf_fonts[id] = font;

#ifdef DEBUG
	pout << "Opened TTF: @data/" << filename << "." << std::endl;
#endif

	return font;
}
Esempio n. 8
0
bool NXFont::InitChars(TTF_Font *font, uint32_t color)
{
SDL_Color fgcolor;
SDL_Surface *letter;

	fgcolor.r = (uint8_t)(color >> 16);
	fgcolor.g = (uint8_t)(color >> 8);
	fgcolor.b = (uint8_t)(color);

#ifdef _L10N_CP1251
    char utf8_str[2];
#endif
	
	char str[2];
	str[1] = 0;
	
	for(int i=1;i<NUM_LETTERS_RENDERED;i++)
	{
		str[0] = i;
#ifndef _L10N_CP1251
        letter = TTF_RenderUTF8_Solid(font, str, fgcolor);
#else
        win1251_to_utf8(str, utf8_str);
        letter = TTF_RenderUTF8_Solid(font, utf8_str, fgcolor);
#endif
		if (!letter)
		{
			staterr("InitChars: failed to render character %d: %s", i, TTF_GetError());
			return 1;
		}
		
		letters[i] = SDL_DisplayFormat(letter);
		SDL_FreeSurface(letter);
	}
	
	return 0;
}
Esempio n. 9
0
bool TextureManager::load_text(const std::string & text,
                               const std::string & id,
                               const SDL_Color & color,
                               SDL_Renderer * renderer)
{
    if (textures[id] != NULL)
        SDL_DestroyTexture(textures[id]);
    std::string rendered_text = " ";
    TTF_Font * f = Game::get_instance() -> get_font();
    //SDL_Color color = {152, 255, 255, 0};
    if (text.length() > 0)
    {
        rendered_text = text;
    }
    SDL_Surface * tmp = TTF_RenderText_Solid(f, rendered_text.c_str(), color);
    if (!tmp)
    {
        std::cerr << "Failure loading text: "
                  << TTF_GetError() << std::endl;
            
        return false;
    }
    SDL_Texture * new_texture = SDL_CreateTextureFromSurface(renderer, tmp);
    SDL_FreeSurface(tmp);

    if (!new_texture)
    {
        std::cerr << "Failure loading texture: "
                  << SDL_GetError() << std::endl;
            
        return false;
    }

    textures[id] = new_texture;

    return true;
}
Esempio n. 10
0
bool 
texture_load_font_string(texture* img, char* text, SDL_Color text_color) {
	
	// rm preexisting texture
	texture_free(img);
	
	// https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf_43.html
	s_surface* text_surface = TTF_RenderText_Solid(g_font, text, text_color);
	
	if ( !text_surface ) 
	{
		printf("texture_load_font_string: unable to render text, ttf err: %s\n", TTF_GetError());
	} 
	else 
	{
		
		// create texture
		img->ref = SDL_CreateTextureFromSurface(g_render, text_surface);
		
		if ( !img->ref ) 
		{
			printf("texture_load_font_string: unable to create text texture, err: %s\n", SDL_GetError());
		} 
		else 
		{	
			// get image dimensions
			img->w = text_surface->w;
			img->h = text_surface->h;
		}
		
		// flush surface
		SDL_FreeSurface(text_surface);
	}
	
	return img->ref != 0;
}
Esempio n. 11
0
void
TopWindow::set(LPCTSTR cls, LPCTSTR text,
                int left, int top, unsigned width, unsigned height)
{
#ifdef ENABLE_SDL
  int ret;

  ret = ::SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
  if (ret != 0)
    fprintf(stderr, "SDL_Init() has failed\n");

  ret = ::TTF_Init();
  if (ret != 0)
    fprintf(stderr, "TTF_Init() has failed: %s\n", TTF_GetError());

  screen.set();
  ContainerWindow::set(NULL, 0, 0, width, height);

  char text2[512];
  unicode2ascii(text, text2);
  ::SDL_WM_SetCaption(text2, NULL);
#else /* !ENABLE_SDL */
  Window::set(NULL, cls, text, left, top, width, height,
              (DWORD)(WS_SYSMENU|WS_CLIPCHILDREN|WS_CLIPSIBLINGS));

#if defined(GNAV) && !defined(PCGNAV)
  // TODO code: release the handle?
  HANDLE hTmp = LoadIcon(XCSoarInterface::hInst,
                         MAKEINTRESOURCE(IDI_XCSOARSWIFT));
  SendMessage(hWnd, WM_SETICON,
	      (WPARAM)ICON_BIG, (LPARAM)hTmp);
  SendMessage(hWnd, WM_SETICON,
	      (WPARAM)ICON_SMALL, (LPARAM)hTmp);
#endif
#endif /* !ENABLE_SDL */
}
Esempio n. 12
0
CDebugger::CDebugger(CDisplay& Display, CSettingsManager& Settings): Display(Display)
{
    /* Load the main font to be used throughout
     * the debugger information.
     */
    std::string font_name(Settings.GetValueAt("DebugFont"));
    if(font_name == "")
        /* Erroneous font name, hopefully we can
         * load a safe alternative.
         */
        font_name == "C:\\Windows\\Fonts\\Arial.ttf";
    
    std::string size(Settings.GetValueAt("DebugFontSize"));
    if(size == "")
        /* No font size given! Most likely the font name
         * check failed as well, so a 12pt Arial font
         * would be just fine for debug info.
         */
        size = "12";
    
    
    
    this->debug_font = TTF_OpenFont(font_name.c_str(), atoi(size.c_str()));

    if(!this->debug_font)
        handleError(TTF_GetError());

    /* For debug builds of the program, 
     * debugging is automatically on by default.
     */
#ifdef _DEBUG
    this->ToggleDebugMode(true);
#else
    this->ToggleDebugMode(false);
#endif // _DEBUG
}
Esempio n. 13
0
PREFIX int spFontReload(spFontPointer font,const char* fontname,Uint32 size)
{
	spLetterDelete( font->root );
	font->root = NULL;
	spLetterDelete( font->buttonRoot );
	font->buttonRoot = NULL;
	if ( font->cache.cache )
		free( font->cache.cache );
	TTF_CloseFont( font->font );
	font->font = TTF_OpenFont( fontname, size );
	if ( !(font->font) )
	{
		printf( "Failed to load Font \"%s\", dude...\n", fontname );
		printf( "	Error was: \"%s\"\n", TTF_GetError() );
		free(font);
		return 1;
	}
	font->size = size;
	font->maxheight = 0;
	font->cacheOffset = 0;
	font->cache.cache = NULL;
	spFontChangeCacheSize( font, SP_FONT_DEFAULT_CACHE );
	return 0;
}
Esempio n. 14
0
GLuint loadTextureFromFont(const std::string& fontFilename, int pointSize, const std::string& text)
{
	GLuint textureID = 0;
	TTF_Font * font = TTF_OpenFont(fontFilename.c_str(), pointSize);
	if (!font)
	{
		std::cout << "Unable to load font " << fontFilename << " " << TTF_GetError();
		return textureID;
	}

	SDL_Surface *textSurface = TTF_RenderText_Blended(font, text.c_str(), { 255, 255, 255 });

	textureID = convertSDLSurfaceToGLTexture(textSurface);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


	TTF_CloseFont(font);

	return textureID;
}
Esempio n. 15
0
Spaceinvader * Spaceinvader_opprett() { 
    
    /* Opprett Spaceinvader-objekt. */
    
    Spaceinvader *s = (Spaceinvader*)malloc(sizeof(Spaceinvader));
                    
    /* Initier SDL */
    
    if (SDL_Init(SDL_INIT_VIDEO) != 0){
        printf ("SDL_Init Error: %s", SDL_GetError());
        return NULL;        
    }
    
    /* Initier TTF */
    
    if (TTF_Init() != 0) {
        printf ("TTF_Init Error: %s", TTF_GetError());
        return NULL;        
    }
    
    /* Opprett skjerm-objekt. */
    
    s->skjerm = Skjerm_opprett(s);
    
    if (s->skjerm == NULL) {
        printf ("Skjerm_opprett Error: Systemfeil.");
        return NULL;
    }
    
    s->modell = Modell_opprett(s);
            
    s->kontrollsentral = Kontrollsentral_opprett(s);                        

    return s;
        
}
Esempio n. 16
0
bool STexture::loadFromRenderedText(std::string textureText, SDL_Color textColor)
{
	//Get rid of preexisting texture
	free();

	TTF_Font *gFont = NULL;

	//Render text surface
	SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, textureText.c_str(), textColor);
	if (textSurface != NULL)
	{
		//Create texture from surface pixels
		mTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
		if (mTexture == NULL)
		{
			printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
		}
		else
		{
			//Get image dimensions
			mWidth = textSurface->w;
			mHeight = textSurface->h;
		}

		//Get rid of old surface
		SDL_FreeSurface(textSurface);
	}
	else
	{
		printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
	}


	//Return success
	return mTexture != NULL;
}
Esempio n. 17
0
bool MapView::initialize(Map * model)
{
    mapModel = model;
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        std::cout << "SDL_Init error." << std::endl;
        return false;
    }
    if (TTF_Init() == -1)
    {
        std::cout << TTF_GetError() << std::endl;
        return false;
    }
    window = SDL_CreateWindow(WINDOW_NAME.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth,
            screenHeight, SDL_WINDOW_SHOWN);
    if (window == nullptr)
    {
        std::cout << "SDL_CreateWindow error." << std::endl;
        return false;
    }
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == nullptr)
    {
        std::cout << "SDL_CreateRenderer error." << std::endl;
        return false;
    }
    texture.setRenderer(renderer);
    texture.loadAllTextures();
    text.setRenderer(renderer);
    text.loadFontPaths();

    loadViewComponents();

    std::cout << "MapView initialization complete!" << std::endl;
    return true;
}
Esempio n. 18
0
bool LTexture::loadFromRenderedText(std::string textureText, SDL_Color textColor)
{
	//Get rid of preexisting texture
	free();

	//Render text surface
	SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, textureText.c_str(), textColor);
	if (textSurface != nullptr)
	{
		//Create texture from surface pixels
		mTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
		if (mTexture == nullptr)
		{
			std::string err = "Unable to create texture from rendered text!! SDL_ttf Error: " + std::string(SDL_GetError());
			logger.error(err.c_str());
		}
		else
		{
			//Get image dimensions
			mWidth = textSurface->w;
			mHeight = textSurface->h;
		}

		//Get rid of old surface
		SDL_FreeSurface(textSurface);
	}
	else
	{
		std::string err = "Unable to render text surface! SDL_ttf Error: " + std::string(TTF_GetError());
		logger.error(err.c_str());
	}


	//Return success
	return mTexture != nullptr;
}
Esempio n. 19
0
void Game::init() {
  if (initialized) {
    return;
  }
  initGlobals();
  initialized = true;
  
  // SDL
  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
    Log::message(Log::Error, SDL_GetError());
    exit(0);
  }
  
  // TTF
  if (TTF_Init()) {
    Log::message(Log::Error, TTF_GetError());
    exit(0);
  }
  
  // SDLNet
  if (SDLNet_Init()) {
    Log::message(Log::Error, SDLNet_GetError());
    exit(0);
  }
  
  // OpenAL
  if (!initOpenAL()) {
    Log::message(Log::Error, alError);
    exit(0);
  }
  
  initLocks();
  
  Graphics::init();
  Audio::init();
}
Esempio n. 20
0
//=============================================================================
void InitSDL()
{
	printf("INFO: Initializing SLD.\n");

	if(SDL_Init(SDL_INIT_VIDEO) < 0)
		throw Format("ERROR: Failed to initialize SDL (%d).", SDL_GetError());

	window = SDL_CreateWindow("Monster Outrage v 0", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, SDL_WINDOW_SHOWN);
	if(!window)
		throw Format("ERROR: Failed to create window (%d).", SDL_GetError());

	renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if(!renderer)
		throw Format("ERROR: Failed to create renderer (%d).", SDL_GetError());

	int imgFlags = IMG_INIT_PNG;
	if(!(IMG_Init(imgFlags) & imgFlags))
		throw Format("ERROR: Failed to initialize SDL_image (%d).", IMG_GetError());

	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);

	if(TTF_Init() == -1)
		throw Format("ERROR: Failed to initialize SDL_ttf (%d).", TTF_GetError());
}
Esempio n. 21
0
static TTF_Font* open_font(const std::string& fname, int size)
{
	std::string name;
	if(!game_config::path.empty()) {
		name = game_config::path + "/fonts/" + fname;
		if(!filesystem::file_exists(name)) {
			name = "fonts/" + fname;
			if(!filesystem::file_exists(name)) {
				name = fname;
				if(!filesystem::file_exists(name)) {
					ERR_FT << "Failed opening font: '" << name << "': No such file or directory" << std::endl;
					return NULL;
				}
			}
		}

	} else {
		name = "fonts/" + fname;
		if(!filesystem::file_exists(name)) {
			if(!filesystem::file_exists(fname)) {
				ERR_FT << "Failed opening font: '" << name << "': No such file or directory" << std::endl;
				return NULL;
			}
			name = fname;
		}
	}

	SDL_RWops *rwops = filesystem::load_RWops(name);
	TTF_Font* font = TTF_OpenFontRW(rwops, true, size); // SDL takes ownership of rwops
	if(font == NULL) {
		ERR_FT << "Failed opening font: TTF_OpenFont: " << TTF_GetError() << std::endl;
		return NULL;
	}

	return font;
}
Esempio n. 22
0
TTF_Font *read_and_alloc_font(const char *path, int pt_size)
{
	TTF_Font *out;
	SDL_RWops *rw;
	Uint8 *data;
	FILE *fp = fopen(path, "r");
	size_t r;

	if (!fp) {
		fprintf(stderr, "Could not open font %s\n", path);
		return NULL;
	}
	data = (Uint8*)xmalloc(1 * 1024*1024);
	r = fread(data, 1, 1 * 1024 * 1024, fp);
	if (r == 0 || ferror(fp))
	{
		free(data);
		return NULL;
	}
	rw = SDL_RWFromMem(data, 1 * 1024 * 1024);
	if (!rw)
	{
		fprintf(stderr, "Could not create RW: %s\n", SDL_GetError());
		free(data);
		return NULL;
	}
	out = TTF_OpenFontRW(rw, 1, pt_size);
	if (!out)
	{
		fprintf(stderr, "TTF: Unable to create font %s (%s)\n",
				path, TTF_GetError());
	}
	fclose(fp);

	return out;
}
Esempio n. 23
0
void Window::initialize()
{
	showDebugInfo();

    fprintf(stdout, "Initializing SDL system\r\n");
    if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER) != 0)
    {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
    }

    fprintf(stdout, "Initializing font system\r\n");
    if(TTF_Init() == -1)
    {
        fprintf(stderr, "Unable to init SDL_ttf: %s\n", TTF_GetError());
        exit(2);
    }
    fprintf(stdout, "Initializing audio system\r\n");
    if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
    {
    	fprintf(stderr, "Unable to open audio channel: %s\n", Mix_GetError());
        exit(3);
    }
}
Esempio n. 24
0
void		aff_choose_map(SDL_Surface *win)
{
  SDL_Rect pos;
  SDL_Surface *texte;
  TTF_Font *police;
  SDL_Color red;

  red.r = 255;
  red.g = 0;
  red.b = 0;
  if (TTF_Init() == -1)
    {
      fprintf(stderr,
	      "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
      exit(EXIT_FAILURE);
    }
  pos.x = 250;
  pos.y = 540;
  police = TTF_OpenFont("./times.ttf", 30);
  texte = TTF_RenderText_Blended(police, "Choississez votre map :", red);
  choose_map(win, police, red, pos, texte);
  TTF_CloseFont(police);
  TTF_Quit();
}
Esempio n. 25
0
Engine::Graphics::GraphicsRenderer::GraphicsRenderer(void)
	: _Window(NULL), _GLContext(NULL)
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		throw std::exception(SDL_GetError());
	}

	if (TTF_Init() < 0)
	{
		throw std::exception(TTF_GetError());
	}

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
}
FontRenderer::FontRenderer() {
	properties = fileLoader.loadConfigFile();

	std::istringstream in(properties.find("FONT_COLOR_R")->second);
	in >> color.r;
	in.clear();

	in.str(properties.find("FONT_COLOR_G")->second);
	in >> color.g;
	in.clear();

	in.str(properties.find("FONT_COLOR_B")->second);

	fontpath = "resources/action_force_normal.ttf";
	fontSize = 12;

	TTF_Init();
	atexit(TTF_Quit);

	if (!(font = TTF_OpenFont(fontpath.c_str(), fontSize))) {
		std::cout << "Error loading font: " << TTF_GetError() << std::endl;
		exit(1);
	}
}
Esempio n. 27
0
int init_graphics(int argc, char *argv[])
{
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {   
		printf("Unable to initialize SDL: %s \n", SDL_GetError());
		exit(1);
	}

	if (TTF_Init() == -1)
	{
		printf("Unable to initialize SDL_ttf:%s \n", TTF_GetError());
		exit(1);
	}  

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_DEPTH, SDL_DOUBLEBUF); //| SDL_FULLSCREEN);
	if (screen == NULL) {
		printf("Unable to set video mode: %s \n", SDL_GetError());
		exit(1);
	}

	SDL_ShowCursor(SDL_DISABLE); //remove cursor

	/*When the application exits, make sure everything is shutdown.*/
	atexit(cleanup);

	/*Load our media, like images and fonts*/
	loadmedia();

	/*Set some variables so our mainloop does the right thing*/
	running = true;
	gameovermode = true;

	/*Enter the main loop, and when we return, exit*/
	pthread_t graphics_thread;
	pthread_create( &graphics_thread, NULL, mainloop, NULL);
	return 0;
}
Esempio n. 28
0
void text_layer_set_text(TextLayer *text_layer, const char *text) {
  text_layer->text = text;
  SDL_Surface *text_surface;
  SDL_Color bgcolor = getColor(text_layer->background_color);
  if(!(text_surface = TTF_RenderText(text_layer->font, text,
                                     getColor(text_layer->text_color),
                                     bgcolor))) {
    printf("[ERROR] TTF_RenderText_Solid: %s\n", TTF_GetError());
  } else {
    SDL_Rect dst;
    dst.x = text_layer->layer.frame.origin.x;
    dst.y = text_layer->layer.frame.origin.y;
    if(bgcolor.unused == 255) {
      SDL_Surface *text_bgsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, text_surface->w, text_surface->h, 32,
                                                        0,0,0,0);
      SDL_FillRect(text_bgsurface, NULL, SDL_MapRGBA(screen->format, bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.unused));
      SDL_BlitSurface(text_bgsurface, NULL, screen, &dst);
      SDL_FreeSurface(text_bgsurface);
    }
    SDL_BlitSurface(text_surface, NULL, screen, &dst);
    SDL_FreeSurface(text_surface);
  }

}
Esempio n. 29
0
bool LTexture::loadFromRenderedText( Window &gWindow, std::string textureText, SDL_Color textColor, int fontSize )
{
	//Get rid of preexisting texture
	free();

    TTF_Font *gFont = TTF_OpenFont("fonts/MOZART_0.ttf", fontSize);
    //TTF_SetFontStyle(gFont, TTF_STYLE_BOLD);
	//Render text surface
	SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor);
	if( textSurface != NULL )
	{
		//Create texture from surface pixels
        mTexture = SDL_CreateTextureFromSurface( gWindow.getRender(), textSurface );
		if( mTexture == NULL )
		{
			printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
		}
		else
		{
			//Get image dimensions
			mWidth = textSurface->w;
			mHeight = textSurface->h;
		}

		//Get rid of old surface
		SDL_FreeSurface( textSurface );
	}
	else
	{
		printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
	}


	//Return success
	return mTexture != NULL;
}
Esempio n. 30
0
MessageArea::MessageArea ( SDL_Surface* dest_surface, int sizex, int sizey, int originx, int originy ) : Widget( dest_surface, sizex, sizey, originx, originy )
{
	dest.x = originx;
	dest.y = originy;
	dest.w = sizex;
	dest.h = sizey;

	font = TTF_OpenFont ( "client/data/Vera.ttf", 12 );
	if ( !font )
	{
		std::cerr << "Error creating TTF_Font: " << TTF_GetError() << "\n";
		exit ( 1 );
	}
	font_height = TTF_FontLineSkip ( font );

	message_color.r = 255;
	message_color.g = 255;
	message_color.b = 0;
	message_color.unused = 0;

	input_received = true;
	blocked = 0;
	current_message = -1;
}