SDL学习记录(01-04)

这篇博客记录了作者学习SDL库的过程,重点介绍了如何利用SDL库通过键盘输入来控制窗口中图像的位置,是SDL入门的一个基础实践。
摘要由CSDN通过智能技术生成

原教程
通过键盘控制窗口的图片

#include <SDL2/SDL.h>
#include <iostream>
#include <string>

const int SCR_WIDTH = 800;
const int SCR_HEIGHT = 600;

enum KeyPressSurfaces {
    KEY_PRESS_SURFACE_DEFAULT,
    KEY_PRESS_SURFACE_UP,
    KEY_PRESS_SURFACE_DOWN,
    KEY_PRESS_SURFACE_LEFT,
    KEY_PRESS_SURFACE_RIGHT,
    KEY_PRESS_SURFACE_TOTAL
};

bool Init();
bool LoadMedia();
void Close();
SDL_Surface* LoadSurface(std::string path);

SDL_Window *gWindow = NULL;
SDL_Surface *gScreenSurface = NULL;
SDL_Surface *gKeyPressSurfaces[KEY_PRESS_SURFACE_TOTAL];
SDL_Surface *gCurrentSurface = NULL;
    

int main(int argc, char* args[])
{
    if (!Init()) {
       std::cout << "Failed to initialize!\n";
    } 
    else {
        if (!LoadMedia()) {
            std::cout << "Failed to load media!\n";
        } 
        else {
            //Main_loop_flag
            bool quit = false;
            //Event handler
            SDL_Event e;
            //While application is running
            while (!quit) {
                //Handle events on queue
                while (SDL_PollEvent(&e) !=0) {
                    //User requests quit 
                    if (e.type == SDL_QUIT) {
                        quit = true;
                    }
                    //User presses a key
                    else if (e.type == SDL_KEYDOWN) {
                        //Select surfaces based on key press
                        switch (e.key.keysym.sym) {
                            case SDLK_UP:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
                            break;

                            case SDLK_DOWN:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
                            break;

                            case SDLK_LEFT:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
                            break;

                            case SDLK_RIGHT:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
                            break;

                            default:
                            gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
                            break;
                        }
                    }
                }
                SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, NULL);
                SDL_UpdateWindowSurface(gWindow);
            }
        }
    }


    Close();
    return 0;
}

bool Init() 
{
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "SLD could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
        success = false;
    } 
    else {
        gWindow = SDL_CreateWindow("SDL_Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        SCR_WIDTH, SCR_HEIGHT, SDL_WINDOW_SHOWN);
    }

    if (gWindow == NULL) {
        std::cout << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
        success = false;
    } 
    else {
        gScreenSurface = SDL_GetWindowSurface(gWindow);
    }
    return success;
}

SDL_Surface* LoadSurface(std::string path) 
{
    SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
    if (loadedSurface == NULL) {
        std::cout << "Unable to load image! SDL Error: " << path.c_str() << ' ' << SDL_GetError() << std::endl;
    }
    return loadedSurface;
}


bool LoadMedia() 
{
    bool success = true;
    //Load default surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] = LoadSurface("04_key_presses/press.bmp");
    if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT] == NULL) {
        std::cout << "Failed to lead default image!\n";
        success = false;
    }
    //Load up surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] = LoadSurface("04_key_presses/up.bmp");
    if (gKeyPressSurfaces[KEY_PRESS_SURFACE_UP] == NULL) {
        std::cout << "Failed to load up image!\n";
        success = false;
    }
    //Load down surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] = LoadSurface("04_key_presses/down.bmp");
    if (gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN] == NULL) {
        std::cout << "Failed to load down image!\n";
        success = false;
    }
    //Load left surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] = LoadSurface("04_key_presses/left.bmp");
    if (gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT] == NULL) {
        std::cout << "Failed to load left image!\n";
        success = false;
    }
    //Load right surface
    gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] = LoadSurface("04_key_presses/right.bmp");
    if (gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT] == NULL) {
        std::cout << "Failed to load right image!\n";
        success = false;
    }
    return success;
}

void Close()
{
    SDL_FreeSurface(gCurrentSurface);
    gCurrentSurface = NULL;
     
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    SDL_Quit();
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值