首页 > 其他分享 >sdl2实现简单的登录页面

sdl2实现简单的登录页面

时间:2023-04-23 11:15:43浏览次数:64  
标签:username 登录 sdl2 255 renderer SDL password rect 页面

#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;
}

标签:username,登录,sdl2,255,renderer,SDL,password,rect,页面
From: https://www.cnblogs.com/full-stack-linux-new/p/17345866.html

相关文章

  • sdl2实现窗口管理
    #include<SDL2/SDL.h>intmain(intargc,char*argv[]){  //初始化SDL2  SDL_Init(SDL_INIT_VIDEO);  //创建窗口  SDL_Window*window=SDL_CreateWindow(    "SDL2Window",      //窗口标题    SDL_WINDOWPOS_CENTERED,......
  • 微信小程序开发页面下拉刷新和scrollview
    问题背景本文主要介绍微信小程序开发过程实现页面下拉刷新以及scrollview组件实现下拉刷新,以及二者的关系。问题分析微信小程序开发过程,如果要实现页面刷新以及scrollerview下拉刷新,步骤如下:(1)页面独立配置在对应页面的json文件中配置属性:"enablePullDownRefresh":true(2)项目......
  • sdl2基本使用
    #include<SDL.h>intmain(intargc,char*args[]){  //初始化SDL  if(SDL_Init(SDL_INIT_VIDEO)<0)  {    printf("SDL初始化失败,错误信息:%s\n",SDL_GetError());    return-1;  }  //创建窗口  SDL_Window*window=SDL_Crea......
  • vue3 keep-alive实现tab页面缓存
    先上图 如何在我们切换tab标签的时候,缓存标签最后操作的内容,简单来说就是每个标签页中设置的比如搜索条件及结果、分页、新增、编辑等数据在切换回来的时候还能保持原样。看看keep-alive是如何实现该功能的。首先我们要了解keep-alive的基本使用。具体介绍请查看官方文档(htt......
  • 退出登录、认证配置详解、权限系统的作用、授权基本流程
    退出登录我们只需要定义一个登录接口,然后获取SecurityContextHolder中的认证信息,删除redis中对应的数据即可。LoginController控制层@RestControllerpublicclassLoginController{@AutowiredprivateLoginServiceloginService;@RequestMapping("/user/lo......
  • Vue设置默认加载页面,去掉地址栏#号
     {path:'/',component:Login,//想默认启动的页面},mode:"history"//去掉地址栏的#号 ......
  • 常用代码-接口交互+构建页面
    点击#search,无参请求,构造表格代码:functiongetDataSet(){$("body").on("click","#search",function(){$.ajax({type:"get",url:"http://114.67.241.121:8080/product/list",......
  • 权限登录实现
    数据库校验用户​从之前的分析我们可以知道,我们可以自定义一个UserDetailsService,让SpringSecurity使用我们的UserDetailsService。我们自己的UserDetailsService可以从数据库中查询用户名和密码数据库CREATETABLE`sys_user`(`id`BIGINT(20)NOTNULLAUTO_INC......
  • 认证详细流程 权限登录思路
     Authentication接口:它的实现类,表示当前访问系统的用户,封装了用户相关信息。AuthenticationManager接口:定义了认证Authentication的方法UserDetailsService接口:加载用户特定数据的核心接口。里面定义了一个根据用户名查询用户信息的方法。UserDetails接口:提供核心用户信息......
  • 权限登录准备工作
    1导入依赖<!--redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!--fastjson依赖-......