前提・実現したいこと
SDL2でゲームを作っていたのですが、メッセージを表示する機能を実装中に以下のエラーが発生しました。
発生している問題・エラーメッセージ
日本語を入力すると文字化けしてしまいます。
英語ではできました。
]
該当のソースコード
C
1#include "SDL2/SDL.h" 2#include "SDL2/SDL_image.h" 3#include "SDL2/SDL_ttf.h" 4 5typedef struct 6{ 7 SDL_Surface *image_f, *image_b, *image_r, *image_l; 8 SDL_Rect rect, rect_pos, rect_tmp; 9 SDL_Texture *texture_f, *texture_b, *texture_r, *texture_l; 10 SDL_Texture *texture_use; 11 int direction; 12} layer_dir; 13 14typedef struct 15{ 16 SDL_Surface *image_small, *image_middle, *image_large; 17 SDL_Rect rect, rect_pos, rect_tmp; 18 SDL_Texture *texture_small, *texture_middle, *texture_large; 19 SDL_Texture *texture_use; 20} layer_ani; 21 22int Object[27][15] = { 23 {0, 0}, 24 {0, 0}}; 25 26int GetPos(SDL_Rect *from, SDL_Rect *to) 27{ 28 to->x = from->x * 23; 29 to->y = from->y * 31; 30 return 0; 31} 32 33void maketex_dir(layer_dir *, SDL_Renderer *); 34void maketex_ani(layer_ani *undefined, SDL_Renderer *renderer); 35 36void LoadIMG_dir(const char front[20], const char back[20], const char right[20], const char left[20], layer_dir *undefined); 37void LoadIMG_ani(const char small[20], const char middle[20], const char large[20], layer_ani *undefined); 38 39void OpenBox(SDL_Renderer *); 40void PrintText(SDL_Renderer *, const char *, TTF_Font *, SDL_Color color); 41 42void destex_dir(layer_dir *); 43void destex_ani(layer_ani *); 44 45int main(int argc, const char *argv[]) 46{ 47 SDL_Window *Window; 48 SDL_Renderer *renderer; 49 50 int isFinish = 0; 51 int img = 0; 52 int scene = 1; 53 int Box = 0; 54 int msg = 0; 55 56 layer_dir charcter; 57 58 layer_ani enemy; 59 60 TTF_Font *font; 61 SDL_Color white = {255, 255, 0, 255}; 62 63 SDL_Init(SDL_INIT_EVERYTHING); 64 TTF_Init(); 65 66 Window = SDL_CreateWindow("Key", 0, 0, 640, 480, SDL_WINDOW_SHOWN); 67 68 font = TTF_OpenFont("rounded-x-mplus-1c-black.ttf", 24); 69 70 renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED); 71 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 72 73 LoadIMG_dir("char/front.png", "char/back.png", "char/right.png", "char/left.png", &charcter); 74 LoadIMG_ani("enemy/small.png", "enemy/middle.png", "enemy/large.png", &enemy); 75 76 charcter.rect.x = 2; 77 charcter.rect.y = 0; 78 charcter.rect.w = charcter.image_f->w; 79 charcter.rect.h = charcter.image_f->h; 80 81 enemy.rect.x = 0; 82 enemy.rect.y = 0; 83 enemy.rect.h = enemy.image_small->h; 84 enemy.rect.w = enemy.image_small->w; 85 86 maketex_ani(&enemy, renderer); 87 maketex_dir(&charcter, renderer); 88 89 Object[0][0] = 1; 90 Object[0][1] = 1; 91 92 charcter.texture_use = charcter.texture_f; 93 enemy.texture_use = enemy.texture_small; 94 95 charcter.rect_pos = charcter.rect; 96 charcter.rect_tmp = charcter.rect; 97 98 enemy.rect_pos = enemy.rect; 99 while (isFinish == 0) 100 { 101 102 SDL_Event event; 103 104 while (SDL_PollEvent(&event)) 105 { 106 switch (event.type) 107 { 108 case SDL_KEYDOWN: 109 { 110 switch (event.key.keysym.sym) 111 { 112 case SDLK_UP: 113 charcter.rect_tmp.y -= 1; 114 charcter.texture_use = charcter.texture_b; 115 break; 116 117 case SDLK_DOWN: 118 charcter.rect_tmp.y += 1; 119 charcter.texture_use = charcter.texture_f; 120 break; 121 122 case SDLK_RIGHT: 123 charcter.rect_tmp.x += 1; 124 charcter.texture_use = charcter.texture_r; 125 break; 126 127 case SDLK_LEFT: 128 charcter.rect_tmp.x -= 1; 129 charcter.texture_use = charcter.texture_l; 130 break; 131 132 case SDLK_ESCAPE: 133 isFinish = 1; 134 break; 135 136 case SDLK_SPACE: 137 if (Box == 0) 138 { 139 Box = 1; 140 } 141 else 142 { 143 Box = 0; 144 } 145 146 if (msg == 0) 147 { 148 msg = 1; 149 } 150 else 151 { 152 msg = 0; 153 } 154 break; 155 } 156 } 157 } 158 159 if (charcter.rect_tmp.y < 0 || 160 charcter.rect_tmp.y > 14 || 161 charcter.rect_tmp.x < 0 || 162 charcter.rect_tmp.x > 26 || 163 Object[charcter.rect_tmp.y][charcter.rect_tmp.x] == 1) 164 { 165 charcter.rect_tmp = charcter.rect; 166 } 167 else 168 { 169 charcter.rect = charcter.rect_tmp; 170 } 171 172 GetPos(&charcter.rect, &charcter.rect_pos); 173 } 174 175 if (img < 500) 176 { 177 enemy.texture_use = enemy.texture_small; 178 } 179 else if (img < 1000) 180 { 181 enemy.texture_use = enemy.texture_middle; 182 } 183 else if (img < 1500) 184 { 185 enemy.texture_use = enemy.texture_large; 186 } 187 else 188 { 189 img = 0; 190 } 191 192 SDL_RenderClear(renderer); 193 SDL_RenderCopy(renderer, charcter.texture_use, NULL, &charcter.rect_pos); 194 SDL_RenderCopy(renderer, enemy.texture_use, NULL, &enemy.rect_pos); 195 196 if (Box == 1) 197 { 198 OpenBox(renderer); 199 } 200 201 switch (msg) 202 { 203 case 1: 204 PrintText(renderer, u8"あいうえお", font, white); 205 break; 206 207 default: 208 break; 209 } 210 211 img++; 212 SDL_RenderPresent(renderer); 213 } 214 215 SDL_DestroyRenderer(renderer); 216 destex_ani(&enemy); 217 destex_dir(&charcter); 218 SDL_DestroyWindow(Window); 219 220 SDL_Quit(); 221 222 return 0; 223} 224 225void maketex_dir(layer_dir *undefined, SDL_Renderer *renderer) 226{ 227 undefined->texture_f = SDL_CreateTextureFromSurface(renderer, undefined->image_f); 228 undefined->texture_b = SDL_CreateTextureFromSurface(renderer, undefined->image_b); 229 undefined->texture_r = SDL_CreateTextureFromSurface(renderer, undefined->image_r); 230 undefined->texture_l = SDL_CreateTextureFromSurface(renderer, undefined->image_l); 231 232 SDL_FreeSurface(undefined->image_f); 233 SDL_FreeSurface(undefined->image_b); 234 SDL_FreeSurface(undefined->image_r); 235 SDL_FreeSurface(undefined->image_l); 236} 237 238void LoadIMG_dir(const char front[20], const char back[20], const char right[20], const char left[20], layer_dir *undefined) 239{ 240 undefined->image_f = IMG_Load(front); 241 undefined->image_b = IMG_Load(back); 242 undefined->image_r = IMG_Load(right); 243 undefined->image_l = IMG_Load(left); 244} 245 246void maketex_ani(layer_ani *undefined, SDL_Renderer *renderer) 247{ 248 undefined->texture_small = SDL_CreateTextureFromSurface(renderer, undefined->image_small); 249 undefined->texture_middle = SDL_CreateTextureFromSurface(renderer, undefined->image_middle); 250 undefined->texture_large = SDL_CreateTextureFromSurface(renderer, undefined->image_large); 251 252 SDL_FreeSurface(undefined->image_small); 253 SDL_FreeSurface(undefined->image_middle); 254 SDL_FreeSurface(undefined->image_large); 255} 256 257void LoadIMG_ani(const char small[20], const char middle[20], const char large[20], layer_ani *undefined) 258{ 259 260 undefined->image_small = IMG_Load(small); 261 undefined->image_middle = IMG_Load(middle); 262 undefined->image_large = IMG_Load(large); 263} 264 265void OpenBox(SDL_Renderer *renderer) 266{ 267 SDL_Surface *surface = SDL_CreateRGBSurface(0, 608, 128, 32, 0, 0, 0, 255); 268 SDL_Texture *texture; 269 SDL_Rect rect; 270 271 rect.h = surface->h; 272 rect.w = surface->w; 273 rect.x = 16; 274 rect.y = 320; 275 276 texture = SDL_CreateTextureFromSurface(renderer, surface); 277 SDL_FreeSurface(surface); 278 279 SDL_RenderCopy(renderer, texture, NULL, &rect); 280} 281 282void PrintText(SDL_Renderer *renderer, const char *text, TTF_Font *font, SDL_Color color) 283{ 284 SDL_Surface *surface = TTF_RenderText_Blended(font, text, color); 285 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); 286 SDL_Rect rect; 287 288 rect.h = surface->h; 289 rect.w = surface->w; 290 rect.x = 16; 291 rect.y = 320; 292 293 SDL_FreeSurface(surface); 294 SDL_RenderCopy(renderer, texture, NULL, &rect); 295} 296 297void destex_dir(layer_dir *layer) 298{ 299 300 SDL_DestroyTexture(layer->texture_f); 301 SDL_DestroyTexture(layer->texture_b); 302 SDL_DestroyTexture(layer->texture_r); 303 SDL_DestroyTexture(layer->texture_l); 304 SDL_DestroyTexture(layer->texture_use); 305} 306 307void destex_ani(layer_ani *layer) 308{ 309 310 SDL_DestroyTexture(layer->texture_small); 311 SDL_DestroyTexture(layer->texture_middle); 312 SDL_DestroyTexture(layer->texture_large); 313 SDL_DestroyTexture(layer->texture_use); 314}
試したこと
関数化せず、main関数の中で処理を行っても同様の結果となりました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/24 00:36