OSDN Git Service

-vectorが足りなかったため、実装した。
[simplecms/utakata.git] / main.cpp.bak
1
2 /* Simple program:  Create a blank window, wait for keypress, quit.
3
4    Please see the SDL documentation for details on using the SDL API:
5    /Developer/Documentation/SDL/docs.html
6 */
7    
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12
13 #include "SDL.h"
14 #include "SDL_ttf.h"
15
16 int main(int argc, char *argv[])
17 {
18     Uint32 initflags = SDL_INIT_VIDEO;  /* See documentation for details */
19     SDL_Surface *screen;
20     Uint8  video_bpp = 0;
21     Uint32 videoflags = SDL_SWSURFACE;
22     int    done;
23     SDL_Event event;
24
25     /* Initialize the SDL library */
26     if ( SDL_Init(initflags) < 0 ) {
27         fprintf(stderr, "Couldn't initialize SDL: %s\n",
28             SDL_GetError());
29         exit(1);
30     }
31
32     /* Set 640x480 video mode */
33     screen=SDL_SetVideoMode(640,480, video_bpp, videoflags);
34     if (screen == NULL) {
35         fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
36                 video_bpp, SDL_GetError());
37         SDL_Quit();
38         exit(2);
39     }
40
41     done = 0;
42     SDL_Rect box = {sizeof(SDL_Rect)};
43     box.x = 100;
44     box.y = 100;
45     box.w = 100;
46     box.h = 100;
47
48     if (TTF_Init() == -1) {
49         SDL_Quit();
50         exit(1);
51     }
52
53     TTF_Font* font = TTF_OpenFont("font.ttf", 12);
54     if (font == NULL) {
55         SDL_Quit();
56         TTF_CloseFont(font);
57         exit(3);
58     }
59
60     SDL_Color color = {sizeof(SDL_Color)};
61     color.r = 0xff;
62     SDL_Surface* ttf = TTF_RenderUTF8_Blended(font, "@", color);
63     SDL_Rect dist = {sizeof(SDL_Rect)};
64     dist.x = 100;
65     dist.y = 100;
66     SDL_EnableUNICODE(1);
67     
68     while ( !done ) {
69
70         SDL_FillRect(screen, NULL, 0xffffffff);
71         SDL_BlitSurface(ttf, NULL, screen, &dist);
72         SDL_UpdateRect(screen, 0, 0, 0, 0);
73
74         /* キーによって文字列の位置を移動させる。*/
75         Uint8* keys = SDL_GetKeyState(NULL);
76         if (keys[SDLK_h] == SDL_PRESSED)
77         {
78             dist.x -= ttf->w;
79         }
80         if (keys[SDLK_j] == SDL_PRESSED)
81         {
82             dist.y -= ttf->h;
83         }
84         if (keys[SDLK_k] == SDL_PRESSED)
85         {
86             dist.y += ttf->h;
87         }
88         if (keys[SDLK_l] == SDL_PRESSED)
89         {
90             dist.x += ttf->w;
91         }
92         if (keys[SDLK_q] == SDL_PRESSED)
93         {
94             done = 1;
95         }
96
97         /* Check for events */
98         if ( SDL_PollEvent(&event) ) {
99             switch (event.type) {
100                 case SDL_MOUSEMOTION:
101                     break;
102                 case SDL_MOUSEBUTTONDOWN:
103                     break;
104                 case SDL_QUIT:
105                     done = 1;
106                     break;
107                 default:
108                     break;
109             }
110         }
111     }
112     
113     /* Clean up the SDL library */
114     SDL_FreeSurface(ttf);
115     TTF_CloseFont(font);
116     TTF_Quit();
117     SDL_Quit();
118     return(0);
119 }