アットウィキロゴ

SDLのまとめ

自分なりにSDLをまとめてみる


合計: -
今日: -
昨日: -

IrrlichtとSDLを組み合わせて使う方法が分からない…
どうやればできるのだろうか

SDL_GetMouseState

int x, y;
SDL_GetMouseState(&x,&y);
で現在のマウスの位置が分かる

SDL_TTFでのテキスト整形

うまくいくか分からないがTTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h)を使えばいけそな感じがする。
  1. まず、空のSurfaceをSDL_CreateRGBSurfaceでつくる
  2. 次に、TTF_SizeUTF8を使って適当な長さの文字列を探す
  3. それをTTF_Render関係の関数でレンダリングする
  4. できたやつを始めに作ったやつに貼り付ける
  5. 2〜4を繰り返す
  6. 完成

サンプル(動くか分からないけど)

SDL_Surface *DrawText(TTF_Font *font, const char *str, SDL_Rect *size)
{

	char tmp[512];

	// フォントがないと困るので
	if(font == NULL){return NULL;}

	SDL_Surface *ts;
	SDL_Surface *bg;

	Uint32 rm, gm, bm, am;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	rm = 0xff000000;
	gm = 0x00ff0000;
	bm = 0x0000ff00;
	am = 0x000000ff;
#else
	rm = 0x000000ff;
	gm = 0x0000ff00;
	bm = 0x00ff0000;
	am = 0xff000000;
#endif

	bg = SDL_CreateRGBSurface(SDL_SWSURFACE, size->w, size->h,
							32, rm, gm, bm, am);
	if(bg == NULL){return NULL;}

	int i=0;
	int len = strlen(str);
	int buf = 0;
	SDL_Rect dest;
	dest.x = 0; dest.y = 0;
	SDL_Color color = {0,0,0,0};
	if(len == 0){return bg;}
	while(i < len){
		int wid, hei, utf;
		tmp[i] = str[buf+i];
		utf = 0;
		if(tmp[i] >= 0xc0){	//2byte 以上のUTF8
			utf++;
			if(tmp[i] >= 0xe0)utf++;
			if(tmp[i] >= 0xf0)utf++;
			if(tmp[i] >= 0xf8)utf++;
			if(tmp[i] >= 0xfc)utf++;
			for(int j=i+1;j<=utf;i++){tmp[j] = str[buf+j];}
			i+=utf;
		}
		tmp[i+1] = '\0';
		TTF_SizeUTF8(font, tmp, &wid, &hei);
		if(wid > size->w || tmp[i] == '\n'){
			if(i == 0){break;}
			if(tmp[i] == '\n')i++;
			tmp[i] = '\0';
			i -= utf;
			buf+=i; len -= i; i = -1;
			ts = TTF_RenderUTF8_Solid(font,tmp,color);
			SDL_BlitSurface(ts, NULL, bg, &dest);
			dest.y+=hei;
		}
		i++;
	}
	ts = TTF_RenderUTF8_Solid(font,tmp,color);
	SDL_BlitSurface(ts, NULL, bg, &dest);
			
	SDL_FreeSurface(ts);
	return bg;
}
最終更新:2009年11月24日 22:18