📜  如何在 C++ 中使用 SDL 显示分数(1)

📅  最后修改于: 2023-12-03 15:24:08.132000             🧑  作者: Mango

在C++中使用SDL显示分数

SDL是一种用于开发游戏、多媒体和图形应用的跨平台库,它提供了许多操作视频、音频、输入和网络的功能。在这篇文章中,我们将介绍怎样在C++中使用SDL来显示分数。

准备工作

在开始编写代码之前,需要在计算机上安装SDL库。具体安装方法可以参考官方指南。

显示分数

下面是一个使用SDL显示分数的简单示例程序。该程序首先创建了一个窗口,然后创建了一个纹理(用于显示分数),最后在循环中绘制分数并刷新窗口。

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> //需要引入SDL TTF库

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
TTF_Font* gFont = NULL;
SDL_Texture* gTextTexture = NULL;

bool init()
{
    bool success = true;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
    {
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
        success = false;
    }
    else 
    {
        gWindow = SDL_CreateWindow("Score", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (gWindow == NULL)
        {
            printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
            success = false;
        }
        else 
        {
            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
            if (gRenderer == NULL)
            {
                printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
                success = false;
            }
            else 
            {
                SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

                //初始化SDL TTF库
                if(TTF_Init()==-1)
                {
                    printf("SDL TTF could not initialize! SDL TTF Error: %s\n", TTF_GetError());
                    success = false;
                }
            }
        }
    }

    return success;
}

bool loadMedia()
{
    bool success = true;

    //设置字体
    gFont = TTF_OpenFont("example.ttf", 28);
    if (gFont == NULL)
    {
        printf("Failed to load font! SDL TTF Error: %s\n", TTF_GetError());
        success = false;
    }
    else 
    {
        //设置文本颜色
        SDL_Color textColor = {0, 0, 0, 255};

        //创建文本纹理
        SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Score: 0", textColor);
        if (textSurface == NULL)
        {
            printf("Failed to render text surface! SDL TTF Error: %s\n", TTF_GetError());
            success = false;
        }
        else 
        {
            gTextTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
            if (gTextTexture == NULL)
            {
                printf("Failed to create text texture! SDL Error: %s\n", SDL_GetError());
                success = false;
            }

            //销毁表面
            SDL_FreeSurface(textSurface);
        }
    }

    return success;
}

void close()
{
    //释放纹理、字体、渲染器和窗口
    SDL_DestroyTexture(gTextTexture);
    gTextTexture = NULL;
    TTF_CloseFont(gFont);
    gFont = NULL;
    SDL_DestroyRenderer(gRenderer);
    gRenderer = NULL;
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    //退出SDL和SDL TTF
    TTF_Quit();
    SDL_Quit();
}

int main(int argc, char* argv[])
{
    if (!init())
    {
        printf("Failed to initialize!\n");
    }
    else 
    {
        if (!loadMedia())
        {
            printf("Failed to load media!\n");
        }
        else 
        {
            bool quit = false;
            SDL_Event e;

            int score = 0; //分数变量

            while (!quit)
            {
                while (SDL_PollEvent(&e) != 0)
                {
                    if (e.type == SDL_QUIT)
                    {
                        quit = true;
                    }
                    else if (e.type == SDL_KEYDOWN)
                    {
                        if (e.key.keysym.sym == SDLK_SPACE)
                        {
                            score++; //分数加一
                            char scoreText[50]; //分数字符串
                            sprintf(scoreText, "Score: %d", score);

                            //销毁之前的纹理
                            SDL_DestroyTexture(gTextTexture);

                            //创建新的纹理
                            SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, scoreText, {0, 0, 0, 255});
                            gTextTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);

                            //销毁表面
                            SDL_FreeSurface(textSurface);
                        }
                    }
                }

                //渲染
                SDL_RenderClear(gRenderer);
                SDL_RenderCopy(gRenderer, gTextTexture, NULL, NULL);
                SDL_RenderPresent(gRenderer);
            }
        }
    }

    close();

    return 0;
}
Markdown格式代码片段
```cpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> //需要引入SDL TTF库

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
TTF_Font* gFont = NULL;
SDL_Texture* gTextTexture = NULL;

bool init()
{
    bool success = true;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
    {
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
        success = false;
    }
    else 
    {
        gWindow = SDL_CreateWindow("Score", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (gWindow == NULL)
        {
            printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
            success = false;
        }
        else 
        {
            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
            if (gRenderer == NULL)
            {
                printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
                success = false;
            }
            else 
            {
                SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

                //初始化SDL TTF库
                if(TTF_Init()==-1)
                {
                    printf("SDL TTF could not initialize! SDL TTF Error: %s\n", TTF_GetError());
                    success = false;
                }
            }
        }
    }

    return success;
}

bool loadMedia()
{
    bool success = true;

    //设置字体
    gFont = TTF_OpenFont("example.ttf", 28);
    if (gFont == NULL)
    {
        printf("Failed to load font! SDL TTF Error: %s\n", TTF_GetError());
        success = false;
    }
    else 
    {
        //设置文本颜色
        SDL_Color textColor = {0, 0, 0, 255};

        //创建文本纹理
        SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Score: 0", textColor);
        if (textSurface == NULL)
        {
            printf("Failed to render text surface! SDL TTF Error: %s\n", TTF_GetError());
            success = false;
        }
        else 
        {
            gTextTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
            if (gTextTexture == NULL)
            {
                printf("Failed to create text texture! SDL Error: %s\n", SDL_GetError());
                success = false;
            }

            //销毁表面
            SDL_FreeSurface(textSurface);
        }
    }

    return success;
}

void close()
{
    //释放纹理、字体、渲染器和窗口
    SDL_DestroyTexture(gTextTexture);
    gTextTexture = NULL;
    TTF_CloseFont(gFont);
    gFont = NULL;
    SDL_DestroyRenderer(gRenderer);
    gRenderer = NULL;
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    //退出SDL和SDL TTF
    TTF_Quit();
    SDL_Quit();
}

int main(int argc, char* argv[])
{
    if (!init())
    {
        printf("Failed to initialize!\n");
    }
    else 
    {
        if (!loadMedia())
        {
            printf("Failed to load media!\n");
        }
        else 
        {
            bool quit = false;
            SDL_Event e;

            int score = 0; //分数变量

            while (!quit)
            {
                while (SDL_PollEvent(&e) != 0)
                {
                    if (e.type == SDL_QUIT)
                    {
                        quit = true;
                    }
                    else if (e.type == SDL_KEYDOWN)
                    {
                        if (e.key.keysym.sym == SDLK_SPACE)
                        {
                            score++; //分数加一
                            char scoreText[50]; //分数字符串
                            sprintf(scoreText, "Score: %d", score);

                            //销毁之前的纹理
                            SDL_DestroyTexture(gTextTexture);

                            //创建新的纹理
                            SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, scoreText, {0, 0, 0, 255});
                            gTextTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);

                            //销毁表面
                            SDL_FreeSurface(textSurface);
                        }
                    }
                }

                //渲染
                SDL_RenderClear(gRenderer);
                SDL_RenderCopy(gRenderer, gTextTexture, NULL, NULL);
                SDL_RenderPresent(gRenderer);
            }
        }
    }

    close();

    return 0;
}