unitView.cpp

00001 /*
00002   ユニット表現のテスト
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include <SDL.h>
00008 #include <SDL_ttf.h>
00009 #include <stdlib.h>
00010 
00011 
00012 enum {
00013   SIZE = 32,
00014   OFFSET = 8,
00015   
00016   AT_MARK = 0xff20,             // @
00017   SOWRD_CH = 0x5263,            // 剣
00018   NIGHT_CH = 0x9a0e,            // 騎
00019   ARCHER_CH = 0x5f13,           // 弓
00020 };
00021 
00022 
00023 static int Y = OFFSET;
00024 
00025 
00026 static void drawColorUnit(SDL_Surface *scr, TTF_Font *ttf) {
00027   SDL_Color black = { 0x00, 0x00, 0x00 };
00028   SDL_Color colors[4];
00029   colors[0].r = 0xff; colors[0].g = 0x00; colors[0].b = 0x00;
00030   colors[1].r = 0x00; colors[1].g = 0x00; colors[1].b = 0xff;
00031   colors[2].r = 0xff; colors[2].g = 0x00; colors[2].b = 0xff;
00032   colors[3].r = 0xff; colors[3].g = 0xff; colors[3].b = 0xff;
00033   
00034   // @を色毎に表示
00035   for (int i = 0; i < 4; ++i) {
00036     Uint16 str[2] = { AT_MARK, 0x0000 };
00037     SDL_Surface *surface =
00038       TTF_RenderUNICODE_Shaded(ttf, str, colors[i], black);
00039     SDL_Rect pos;
00040     pos.x = OFFSET + i * (SIZE + OFFSET);
00041     pos.y = Y;
00042     SDL_BlitSurface(surface, NULL, scr, &pos);
00043     SDL_FreeSurface(surface);
00044   }
00045   
00046   Y += SIZE + OFFSET;
00047   SDL_UpdateRect(scr, 0, 0, 0, 0);
00048 }
00049 
00050 
00051 static void drawOtherUnit(SDL_Surface *scr, TTF_Font *ttf) {
00052   SDL_Color black = { 0x00, 0x00, 0x00 };
00053   SDL_Color red = { 0xff, 0x00, 0x00 };
00054 
00055   Uint16 chara[4] = { AT_MARK, SOWRD_CH, NIGHT_CH, ARCHER_CH };
00056   Uint16 str[2] = { 0x0000, 0x0000 };
00057   for (int i = 0; i < 4; ++i) {
00058     str[0] = chara[i];
00059     SDL_Surface *surface =
00060       TTF_RenderUNICODE_Shaded(ttf, str, red, black);
00061     SDL_Rect pos;
00062     pos.x = OFFSET + i * (SIZE + OFFSET);
00063     pos.y = Y;
00064     SDL_BlitSurface(surface, NULL, scr, &pos);
00065     SDL_FreeSurface(surface);
00066   }
00067 
00068   Y += SIZE + OFFSET;
00069   SDL_UpdateRect(scr, 0, 0, 0, 0);
00070 }
00071 
00072 
00073 static SDL_Surface *transparentSurface(SDL_Surface *surface) {
00074   if (surface->format->palette) {
00075     SDL_SetColorKey(surface, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
00076                     *(Uint8 *)surface->pixels);
00077   }
00078   SDL_Surface *ret = SDL_DisplayFormat(surface);
00079   SDL_FreeSurface(surface);
00080 
00081   return ret;
00082 }
00083 
00084 
00085 static void drawBoldUnit(SDL_Surface *scr, TTF_Font *ttf) {
00086   SDL_Color white = { 0xff, 0xff, 0xff };
00087   //SDL_Color yellow = { 0xff, 0xff, 0x00 };
00088   SDL_Color red = { 0xff, 0x00, 0x00 };
00089 
00090   Uint16 chara[4] = { AT_MARK, SOWRD_CH, NIGHT_CH, ARCHER_CH };
00091   Uint16 str[2] = { 0x0000, 0x0000 };
00092   for (int i = 0; i < 4; ++i) {
00093     str[0] = chara[i];
00094     SDL_Surface *surface =
00095       TTF_RenderUNICODE_Solid(ttf, str, white);
00096       //TTF_RenderUNICODE_Solid(ttf, str, yellow);
00097     surface = transparentSurface(surface);
00098     SDL_Rect pos;
00099 
00100     pos.y = Y - 2;
00101     for (int y = 0; y < 5; ++y) {
00102       pos.x = OFFSET + i * (SIZE + OFFSET) - 2;
00103       for (int x = 0; x < 5; ++x) {
00104         SDL_BlitSurface(surface, NULL, scr, &pos);
00105         ++pos.x;
00106       }
00107       ++pos.y;
00108     }
00109     SDL_FreeSurface(surface);
00110 
00111     surface = TTF_RenderUNICODE_Shaded(ttf, str, red, white);
00112     surface = transparentSurface(surface);
00113     pos.x = OFFSET + i * (SIZE + OFFSET);
00114     pos.y = Y;
00115     SDL_BlitSurface(surface, NULL, scr, &pos);
00116     SDL_FreeSurface(surface);
00117   }
00118 
00119   Y += SIZE + OFFSET;
00120   SDL_UpdateRect(scr, 0, 0, 0, 0);
00121 }
00122 
00123 
00124 static void drawBarUnit(SDL_Surface *scr, TTF_Font *ttf) {
00125   SDL_Color red = { 0xff, 0x00, 0x00 };
00126   
00127   for (int i = 0; i < 4; ++i) {
00128     Uint16 str[2] = { AT_MARK, 0x0000 };
00129     SDL_Surface *surface =
00130       TTF_RenderUNICODE_Solid(ttf, str, red);
00131     SDL_Rect pos;
00132     pos.x = OFFSET + i * (SIZE + OFFSET);
00133     pos.y = Y;
00134     SDL_BlitSurface(surface, NULL, scr, &pos);
00135 
00136     Uint32 green = SDL_MapRGB(scr->format, 0x00, 0xff, 0x00);
00137     SDL_Rect bar;
00138     switch (i) {
00139     case 0:
00140       bar.x = pos.x;
00141       bar.y = pos.y + SIZE;
00142       bar.w = SIZE-1;
00143       bar.h = 2;
00144       break;
00145 
00146     case 1:
00147       bar.x = pos.x;
00148       bar.y = pos.y + SIZE -2;
00149       bar.w = SIZE-1;
00150       bar.h = 2;
00151       break;
00152 
00153     case 2:
00154       bar.x = pos.x + 5;
00155       bar.y = pos.y + SIZE;
00156       bar.w = SIZE-1 - 12;
00157       bar.h = 2;
00158       break;
00159 
00160     case 3:
00161       bar.x = pos.x;
00162       bar.y = pos.y -2;
00163       bar.w = SIZE-1;
00164       bar.h = 2;
00165       break;
00166     }
00167     SDL_FillRect(scr, &bar, green);
00168     
00169     SDL_FreeSurface(surface);
00170   }
00171   
00172   Y += SIZE + OFFSET;
00173   SDL_UpdateRect(scr, 0, 0, 0, 0);
00174 }
00175 
00176 
00177 static SDL_Surface *createScreen(void) {
00178   if (SDL_Init(SDL_INIT_VIDEO) < 0) {
00179     fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
00180     exit(1);
00181   }
00182   atexit(SDL_Quit);
00183 
00184   return SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
00185 }
00186 
00187 
00188 static void waitInput(void) {
00189   while (1) {
00190     SDL_Event event;
00191     while (SDL_PollEvent(&event)) {
00192       switch (event.type) {
00193       case SDL_QUIT:
00194         exit(1);
00195         break;
00196         
00197       case SDL_KEYDOWN:
00198         return;
00199         break;
00200       }
00201     }
00202     SDL_Delay(100);
00203   }
00204 }
00205 
00206 
00207 static TTF_Font *TTFResource = NULL;
00208 static void deleteTTFResource(void) {
00209   TTF_CloseFont(TTFResource);
00210   TTF_Quit();
00211 }
00212 
00213 static TTF_Font *createTTFResource(void) {
00214   if (TTF_Init() < 0) {
00215     
00216   }
00217   atexit(deleteTTFResource);
00218 
00219   return (TTFResource = TTF_OpenFont("font.ttf", SIZE));
00220 }
00221 
00222 
00223 int main(int argc, char *argv[]) {
00224 
00225   // スクリーンの作成
00226   SDL_Surface *scr = createScreen();
00227   TTF_Font *ttf = createTTFResource();
00228 
00229   // 色別にユニットを描画
00230   drawColorUnit(scr, ttf);
00231 
00232   // 種類毎にユニットを描画
00233   drawOtherUnit(scr, ttf);
00234 
00235   // 文字に枠を付けて描画
00236   drawBoldUnit(scr, ttf);
00237 
00238   // アンダーバーで体力・魔力割合を表示
00239   drawBarUnit(scr, ttf);
00240   
00241   waitInput();
00242   exit(0);
00243 }
00244 

Generated on Mon Apr 13 22:52:01 2009 by  doxygen 1.5.7.1