#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <stdio.h>
//=======================窗口属性=========================
#define WINDOWNAME "my window"
#define SCREEN_X 100
#define SCREEN_Y 100
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
//=======================色彩定义=========================
//========================初始化==========================
SDL_Window *Init()
{
SDL_Window *pScreen = NULL;
//SDL初始化
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf ("SDL initialize fail:%s\n", SDL_GetError());
return NULL;
}
pScreen = SDL_CreateWindow(WINDOWNAME, SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (NULL == pScreen)//检测是否初始化成功
printf ("SDL_CreateWindow fail:%s\n", SDL_GetError());
//初始化字体库
if (TTF_Init() == -1 )
printf ("TTF_Init fail:%s\n", SDL_GetError());
return pScreen;
}
//========================程序释放========================
void Quit()
{
//释放字体
TTF_Quit();
//退出SDL
SDL_Quit();
}
//========================加载图片========================
SDL_Texture *Load_image(const char *filename,SDL_Renderer *render)
{
//临时的空间 用来储存刚刚加载好的图片
SDL_Surface *loadedImage = NULL;
SDL_Texture *texture = NULL;
//加载图片
loadedImage=IMG_Load(filename);
//如果加载图片失败
if (loadedImage == NULL)
printf ("IMG_Load fail:%s", SDL_GetError());
texture = SDL_CreateTextureFromSurface(render, loadedImage);
if (texture == NULL)
printf("SDL_CreateTextureFromSurface fail:%s", SDL_GetError());
//释放临时的图片
SDL_FreeSurface(loadedImage);
return texture;
}
//===================加载字体(ASCII字符)===================
SDL_Surface *Load_ttfText(const char *filename, int ptsize, const char *text, SDL_Color color)
{
//临时的空间 用来储存刚刚加载好的字体
TTF_Font *font;
SDL_Surface *pText;
//加载字体
font = TTF_OpenFont(filename, 20);
//如果加载字体失败
if (font == NULL)
printf ("TTF_OpenFont fail:%s", SDL_GetError());
pText = TTF_RenderText_Solid(font, text, color);
//如果渲染失败
if (pText == NULL)
printf ("TTF_RenderText_Solid fail:%s", SDL_GetError());
//字体指针释放
TTF_CloseFont(font);
return pText;
}
//===================加载字体(UTF8编码)====================
SDL_Surface *Load_ttfUTF8(const char *filename, int ptsize, const char *text, SDL_Color color)
{
//临时的空间 用来储存刚刚加载好的字体
TTF_Font *font;
SDL_Surface *pText;
//加载字体
font = TTF_OpenFont(filename, 20);
//如果加载字体失败
if (font == NULL)
printf ("TTF_OpenFont fail:%s", SDL_GetError());
pText = TTF_RenderUTF8_Solid(font, text, color);
//如果渲染失败
if (pText == NULL)
printf ("TTF_RenderUTF8_Solid fail:%s", SDL_GetError());
//字体指针释放
TTF_CloseFont(font);
return pText;
}
//===================加载字体(UCS2编码)====================
SDL_Surface *Load_ttfUNICODE(const char *filename, int ptsize, const Uint16 *text, SDL_Color color)
{
//临时的空间 用来储存刚刚加载好的字体
TTF_Font *font;
SDL_Surface *pText;
//加载字体
font = TTF_OpenFont(filename, 20);
//如果加载字体失败
if (font == NULL)
printf ("TTF_OpenFont fail:%s", SDL_GetError());
pText = TTF_RenderUNICODE_Solid(font, text, color);
//如果渲染失败
if (pText == NULL)
printf ("TTF_RenderUNICODE_Solid fail:%s", SDL_GetError());
//字体指针释放
TTF_CloseFont(font);
return pText;
}
//========================复制渲染===========================
void Copy_render(SDL_Renderer * render, SDL_Texture *texture, int x = 0, int y = 0,
int w = SCREEN_WIDTH, int h = SCREEN_HEIGHT, double angle = 0.0, int p_x = 0, int p_y = 0)
{
SDL_Rect rect = {x, y, w, h};
SDL_Point center = {p_x, p_y};
SDL_RenderCopyEx(render, texture, NULL, &rect, angle, ¢er, SDL_FLIP_NONE);
}
//========================获取按键===========================
int ButtonRec () //每个按键区域大小为(h = 92, w = 128),共分为20个按键 各返回值如图对应: // | 0 | 1 | 2 | 3 | 4
{ // | 5 | 6 | 7 | 8 | 9
SDL_Event event; // |10 |11 |12 |13 |14
int x,y; // |15 |16|17 |18 |18 |14
if (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_MOUSEBUTTONDOWN: //如果发生了鼠标按键按下事件
if(event.button.button == SDL_BUTTON_LEFT)
//如果是左键按下的事件,就要判断按下鼠标的时候鼠标是否处在button区域中,所以要得到鼠标坐标
{
y=event.button.x; //宽
x=event.button.y-110; //高
if (x>2 && x<94 && y>0 && y<128)
return 0;
if (x>2 && x<94 && y>128 && y<256)
return 1;
if (x>2 && x<94 && y>256 && y<384)
return 2;
if (x>2 && x<94 && y>384 && y<512)
return 3;
if (x>2 && x<94 && y>512 && y<640)
return 4;
if (x>94 && x<186 && y>0 && y<128)
return 5;
if (x>94 && x<186 && y>128 && y<256)
return 6;
if (x>94 && x<186 && y>256 && y<384)
return 7;
if (x>94 && x<186 && y>384 && y<512)
return 8;
if (x>94 && x<186 && y>512 && y<640)
return 9;
if (x>186 && x<278 && y>0 && y<128)
return 10;
if (x>186 && x<278 && y>128 && y<256)
return 11;
if (x>186 && x<278 && y>256 && y<384)
return 12;
if (x>186 && x<278 && y>384 && y<512)
return 13;
if (x>186 && x<370 && y>512 && y<640)
return 14;
if (x>278 && x<370 && y>0 && y<64)
return 15;
if (x>278 && x<370 && y>64 && y<128)
return 16;
if (x>278 && x<370 && y>128 && y<256)
return 17;
if (x>278 && x<370 && y>256 && y<512)
return 18;
}
}
}
return -1;
}
//========================显示字符===========================
void ApplyString(SDL_Renderer *render, SDL_Texture *word, SDL_Texture *keyboard, SDL_Surface *letter, char *buf, SDL_Color color, int buf_count)
{
letter = Load_ttfText("msyh.ttf", 60, buf, color);
word = SDL_CreateTextureFromSurface(render, letter);
SDL_RenderClear(render);
Copy_render(render, word, 20, 30, 20*(buf_count+1),60);
Copy_render(render, keyboard, 0, 110, 640,370);
SDL_RenderPresent(render);
}
int main (int argc, char *argv[])
{
char buf[100] = {};
char buttonword[][4] = {{',','.','?','!'},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
SDL_Window *window = Init();
SDL_Renderer *render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(render,255,255,255,255); //设计底色为白
//清空渲染器
SDL_Texture *keyboard = NULL;
SDL_Texture *word = NULL;
SDL_Color black = {0,0,0};
SDL_Surface *letter = NULL;
SDL_Event event;
int flag, cnt, i = 0, j, buf_count = 0, buf_length = 1,quit = 0;
keyboard = Load_image("9key_e.bmp", render);
SDL_RenderClear(render);
Copy_render(render, keyboard, 0, 110, 640,370);
SDL_RenderPresent(render);
while (!quit)
{
flag = 1;
cnt = 0;
i = 0;
switch (ButtonRec())
{
//9键情况处理
case 1: //包含(, . ? !)
j = 0;
while (flag)
{
printf ("%d ",i);
cnt++;
SDL_Delay(10);
if (ButtonRec() == 1)
{
cnt = 0;
i++;
i = i%4;
}
if (cnt > 30)
flag = 0;
buf[buf_count] = buttonword[j][i];
ApplyString(render, word, keyboard, letter, buf, black, buf_count);
}
buf_count++;
printf ("ok! ");
break;
case 2: //包含(a b c)
j = 1;
while (flag)
{
printf ("%d ",i);
cnt++;
SDL_Delay(10);
if (ButtonRec() == 2)
{
cnt = 0;
i++;
i = i%3;
}
if (cnt > 30)
flag = 0;
buf[buf_count] = buttonword[j][i];
ApplyString(render, word, keyboard, letter, buf, black, buf_count);
}
buf_co