前回のメッセージ表示では背景上に直接文字を表示しましたが、今回は、ウインドウを開いて、文字を表示したいと思います。
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
/* ウインドウ設定 */
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define BPP 32
/* キャラクタ設定 */
#define CHAR_WIDTH 32
#define CHAR_HEIGHT 32
/* マップ設定 */
#define MAP_WIDTH 20
#define MAP_HEIGHT 15
/* フォント設定 */
#define FONT_NAME "ipag-mona.ttf"
#define FONT_SIZE 24
/* メッセージウインドウ */
#define MESSAGE_WIN_LEFT 0 /* メッセージウインドウ左端 */
#define MESSAGE_WIN_TOP 400 /* メッセージウインドウ上端 */
#define MESSAGE_WIN_WIDTH (WINDOW_WIDTH) /* メッセージウインドウ幅 */
#define MESSAGE_WIN_HEIGHT (WINDOW_HEIGHT - MESSAGE_WIN_TOP) /* メッセージウインドウ高さ */
/* メッセージ */
#define MESSAGE_PADDING 5 /* メッセージパディング */
#define MESSAGE_LEFT (MESSAGE_PADDING + MESSAGE_WIN_LEFT) /* メッセージ左端 */
#define MESSAGE_TOP (MESSAGE_PADDING + MESSAGE_WIN_TOP) /* メッセージ上端 */
/* 共通フォント */
TTF_Font *font;
/* 色 */
const SDL_Color color_white = {0xff, 0xff, 0xff};
/* レイヤー構造体 */
typedef struct{
SDL_Surface *surface;
int use;
int visible;
SDL_Rect src_rect;
SDL_Rect dst_rect;
} type_layer;
/* レイヤー番号列挙体 */
enum enm_layer{
lyrBackground = 0, /* 背景 */
lyrCharacter, /* キャラクタ */
lyrMessageWindow, /* メッセージウインドウ */
lyrMessage, /* メッセージ */
lyrEnd /* 「ダミー」 */
};
type_layer g_game_layers[lyrEnd];
char g_map[MAP_HEIGHT][MAP_WIDTH];
int getpos(SDL_Rect *from, SDL_Rect *to){
to->x = from->x * CHAR_WIDTH;
to->y = from->y * CHAR_HEIGHT;
return 0;
}
int update_layer_rect_size(type_layer *layers, int index){
(layers + index)->src_rect.w = (layers + index)->surface->w;
(layers + index)->src_rect.h = (layers + index)->surface->h;
return 0;
}
int init_rect(SDL_Rect* rect){
rect->x = 0;
rect->y = 0;
rect->w = 0;
rect->h = 0;
return 0;
}
int init_layer_rect(type_layer* layer, int index){
init_rect(&((layer + index)->src_rect));
init_rect(&((layer + index)->dst_rect));
return 0;
}
int set_layer_visible(type_layer *layers, int index, int flag){
(layers + index)->visible = flag;
return 0;
}
int drop_surface(SDL_Surface *surface){
SDL_FreeSurface(surface);
return 0;
}
int drop_layer(type_layer *layer, int index){
drop_surface((layer + index)->surface);
(layer + index)->use = 0;
set_layer_visible(layer, index, 0);
return 0;
}
int update_display(){
SDL_Surface *video_surface = SDL_GetVideoSurface();
int iLoop;
for(iLoop = 0; iLoop < lyrEnd; iLoop ++){
if(g_game_layers[iLoop].visible == 1){
/* サーフェスの複写 */
SDL_BlitSurface(
g_game_layers[iLoop].surface,
&g_game_layers[iLoop].src_rect,
video_surface,
&g_game_layers[iLoop].dst_rect
);
}
}
/* サーフェスフリップ */
SDL_Flip(video_surface);
return 0;
}
int show_message(const char* msg){
SDL_Event event;
int close_msg = 0;
g_game_layers[lyrMessage].surface = TTF_RenderUTF8_Blended(font, msg, color_white);
update_layer_rect_size(g_game_layers, lyrMessage);
set_layer_visible(g_game_layers, lyrMessageWindow, 1);
set_layer_visible(g_game_layers, lyrMessage, 1);
SDL_EnableKeyRepeat(0, 0);
/* 描画 */
update_display();
/* メッセージが閉じるまで、元のイベントループには戻らない */
while(close_msg == 0){
if(SDL_PollEvent(&event)){
switch(event.type){
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_SPACE){
close_msg = 1;
}
break;
default:
break;
}
}
SDL_Delay(1);
}
set_layer_visible(g_game_layers, lyrMessageWindow, 0);
set_layer_visible(g_game_layers, lyrMessage, 0);
update_display();
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_INTERVAL, SDL_DEFAULT_REPEAT_INTERVAL);
drop_layer(g_game_layers, lyrMessage);
return 0;
}
int init_layers(){
int iLoop;
/* 矩形初期化 */
for(iLoop = 0; iLoop < lyrEnd; iLoop ++){
init_layer_rect(g_game_layers, iLoop);
g_game_layers[iLoop].use = 0;
}
/* バックグラウンド */
g_game_layers[lyrBackground].surface =
SDL_CreateRGBSurface(SDL_HWSURFACE, WINDOW_WIDTH, WINDOW_HEIGHT, 32, 0, 0, 0, 0);
update_layer_rect_size(g_game_layers, lyrBackground);
SDL_FillRect(
g_game_layers[lyrBackground].surface,
&g_game_layers[lyrBackground].src_rect,
SDL_MapRGB(g_game_layers[lyrBackground].surface->format, 60, 60, 60)
);
g_game_layers[lyrBackground].use = 1;
set_layer_visible(g_game_layers, lyrBackground, 1);
/* キャラクタ(カーソルキーで動かすやつ) */
g_game_layers[lyrCharacter].surface = IMG_Load("char1.png");
update_layer_rect_size(g_game_layers, lyrCharacter);
g_game_layers[lyrCharacter].use = 1;
set_layer_visible(g_game_layers, lyrCharacter, 1);
/* メッセージウインドウ */
g_game_layers[lyrMessageWindow].surface = IMG_Load("msg_window.png");
update_layer_rect_size(g_game_layers, lyrMessageWindow);
set_layer_visible(g_game_layers, lyrMessageWindow, 0);
g_game_layers[lyrMessageWindow].dst_rect.x = MESSAGE_WIN_LEFT;
g_game_layers[lyrMessageWindow].use = 1;
g_game_layers[lyrMessageWindow].dst_rect.y = MESSAGE_WIN_TOP;
/* メッセージ */
set_layer_visible(g_game_layers, lyrMessage, 0);
g_game_layers[lyrMessage].dst_rect.x = MESSAGE_LEFT;
g_game_layers[lyrMessage].dst_rect.y = MESSAGE_TOP;
return 0;
}
int drop_layers(){
int iLoop;
for(iLoop = 0; iLoop < lyrEnd; iLoop ++){
if(g_game_layers[iLoop].use == 1){
drop_layer(g_game_layers, iLoop);
}
}
return 0;
}
int main(int argc, char* argv[]){
SDL_Surface* image;
SDL_Rect scr_rect, rect_tmp;
SDL_Event event;
int exit_prg = 0;
memset(g_map, 0x00, sizeof(g_map));
SDL_Init(SDL_INIT_EVERYTHING);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_INTERVAL, SDL_DEFAULT_REPEAT_INTERVAL);
TTF_Init();
SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, BPP, SDL_HWSURFACE);
/* フォント読み込み */
font = TTF_OpenFont(FONT_NAME, FONT_SIZE);
/* 画像読み込み */
init_layers();
/* 画像配置位置情報の設定 */
init_rect(&scr_rect);
/* 描画 */
update_display();
rect_tmp = scr_rect;
/* イベントループ */
while(exit_prg == 0){
if(SDL_PollEvent(&event)){
switch(event.type){
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_UP:
rect_tmp.y -= 1;
break;
case SDLK_DOWN:
rect_tmp.y += 1;
break;
case SDLK_RIGHT:
rect_tmp.x += 1;
break;
case SDLK_LEFT:
rect_tmp.x -= 1;
break;
case SDLK_ESCAPE:
exit_prg = 1;
break;
case SDLK_SPACE:
show_message("そこにはなにもありません。");
break;
default:
break;
}
/* 移動可能範囲の判定 */
if(
rect_tmp.x >= 0 &&
rect_tmp.x < MAP_WIDTH &&
rect_tmp.y >= 0 &&
rect_tmp.y < MAP_HEIGHT &&
g_map[rect_tmp.y][rect_tmp.x] == 0
){
scr_rect = rect_tmp;
/* 描画 */
getpos(&scr_rect, &g_game_layers[lyrCharacter].dst_rect);
update_display();
}
else {
rect_tmp = scr_rect;
}
break;
default:
break;
}
}
SDL_Delay(1);
}
drop_layers();
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return 0;
}
前回のと少し変えましたが、大きくは変わっていないと思います。
メッセージウインドウにはpngファイルをそのまま使用するという手抜きをしていますが、これでちょっとはRPGっぽくなったと思います。あいかわらず、「そこにはなにもありません。」ですが。

今回、レイヤーという概念を用いました。要は、サーフェスを重ねる(描画する)順番のことです。サーフェスが多くなってくると、サーフェスの上下関係を間違えると予期せぬバグになります。それを防ぐため、あらかじめ描画順を決めておきました。描画の際はこの順番にしたがって一括描画されるようになっています(update_layer()で行っています)。
今回のプログラムの描画順は、バックグラウンド、キャラクタ、メッセージウインドウ、メッセージの順になっています。
レイヤーの構造体はtype_layerです。描画するかしないかは、type_layerのメンバvisibleが1か0かによって判定されます。また、surfaceを使用しているか、使用していないか(解放されているか)はuseによって判定され、2重にSDL_FreeSurface()が呼ばれないようにしています。
画像読み込み等は現在init_layers()で行っているので、背景が一色なのは嫌だという場合は、SDL_FillRect()の代わりにIMG_Load()などを呼んで背景画像にすればいいかと思います。