#include <SDL.h>
#include <SDL_ttf.h>
#include <iostream>
#include <string>
int main(int argc, char* args[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "SDL 初始化失败,错误信息:" << SDL_GetError() << std::endl;
return -1;
}
if (TTF_Init() < 0)
{
std::cerr << "SDL_ttf 初始化失败,错误信息:" << TTF_GetError() << std::endl;
return -1;
}
SDL_Window* window = SDL_CreateWindow("登录页面", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
if (!window)
{
std::cerr << "窗口创建失败,错误信息:" << SDL_GetError() << std::endl;
return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer)
{
std::cerr << "渲染器创建失败,错误信息:" << SDL_GetError() << std::endl;
return -1;
}
TTF_Font* font = TTF_OpenFont("arial.ttf", 28);
if (!font)
{
std::cerr << "字体加载失败,错误信息:" << TTF_GetError() << std::endl;
return -1;
}
SDL_Surface* surface = TTF_RenderUTF8_Blended(font, "欢迎使用我的应用", { 255, 255, 255, 255 });
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect rect = { 0, 0, surface->w, surface->h };
SDL_FreeSurface(surface);
SDL_Rect bg_rect = { 100, 100, 400, 200 };
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &bg_rect);
SDL_Rect text_rect = { 250 - rect.w / 2, 130, rect.w, rect.h };
SDL_RenderCopy(renderer, texture, NULL, &text_rect);
SDL_RenderPresent(renderer);
SDL_Event event;
std::string username = "";
std::string password = "";
int cursor_x = 0;
int cursor_y = 0;
bool is_running = true;
bool is_username_inputting = true;
while (is_running)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
is_running = false;
}
else if (event.type == SDL_MOUSEBUTTONDOWN)
{
int x = event.button.x;
int y = event.button.y;
if (x >= 220 && x <= 420 && y >= 200 && y <= 240)
{
is_username_inputting = true;
}
else if (x >= 220 && x <= 420 && y >= 250 && y <= 290)
{
is_username_inputting = false;
}
}
else if (event.type == SDL_TEXTINPUT)
{
if (is_username_inputting)
{
username += event.text.text;
}
else
{
password += event.text.text;
}
}
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_BACKSPACE)
{
if (is_username_inputting)
{
if (!username.empty())
{
username.pop_back();
}
}
else
{
if (!password.empty())
{
password.pop_back();
}
}
}
else if (event.key.keysym.sym == SDLK_RETURN)
{
std::cout << "用户名:" << username << std::endl;
std::cout << "密码:" << password << std::endl;
}
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &bg_rect);
SDL_RenderCopy(renderer, texture, NULL, &text_rect);
SDL_Rect username_rect = { 220, 200, 200, 40 };
SDL_Rect password_rect = { 220, 250, 200, 40 };
SDL_SetRenderDrawColor(renderer, 200, 200, 200, 255);
SDL_RenderFillRect(renderer, &username_rect);
SDL_RenderFillRect(renderer, &password_rect);
if (is_username_inputting)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderDrawLine(renderer, 220 + cursor_x, 200 + cursor_y, 220 + cursor_x, 240 + cursor_y);
SDL_Rect username_text_rect = { 220, 200, 0, 0 };
SDL_Surface* username_surface = TTF_RenderUTF8_Blended(font, username.c_str(), { 0, 0, 0, 255 });
SDL_Texture* username_texture = SDL_CreateTextureFromSurface(renderer, username_surface);
username_text_rect.w = username_surface->w;
username_text_rect.h = username_surface->h;
SDL_RenderCopy(renderer, username_texture, NULL, &username_text_rect);
SDL_FreeSurface(username_surface);
SDL_DestroyTexture(username_texture);
}
else
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderDrawLine(renderer, 220 + cursor_x, 250 + cursor_y, 220 + cursor_x, 290 + cursor_y);
std::string masked_password(password.size(), '*');
SDL_Rect password_text_rect = { 220, 250, 0, 0 };
SDL_Surface* password_surface = TTF_RenderUTF8_Blended(font, masked_password.c_str(), { 0, 0, 0, 255 });
SDL_Texture* password_texture = SDL_CreateTextureFromSurface(renderer, password_surface);
password_text_rect.w = password_surface->w;
password_text_rect.h = password_surface->h;
SDL_RenderCopy(renderer, password_texture, NULL, &password_text_rect);
SDL_FreeSurface(password_surface);
SDL_DestroyTexture(password_texture);
}
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return 0;
}