第15章 《乐趣》Page379 课堂作业 定时动画练习,白云左右随机飘动,小马更逼真的走动起来

发布时间:2023年12月19日

运行效果:Page379课堂作业定时动画练习-CSDN直播

//main.cpp
#include <iostream>
#include <SDL2/SDL.h>
#include "sdl_initiator.hpp"
#include "sdl_error.hpp"
#include "sdl_window.hpp"
#include "sdl_surface.hpp"
#include "sdl_renderer.hpp"
#include "sdl_texture.hpp"

#include <SDL2/SDL_timer.h>
#include <thread>

using namespace std;

//加载图片为纹理,并设置透明色
SDL_Texture* LoadBMPTexture(SDL_Renderer* renderer
                           , char const* filename
                           , Uint8 key_r, Uint8 key_g, Uint8 key_b)
{
    sdl2::BitmapSurface bmp(filename);
    if(!bmp)
    {
        return nullptr;
    }
    bmp.EnableColorKey(key_r, key_g, key_b, 0);
    return sdl2::Texture(renderer, bmp._surface).Release();
}
//加载图片为纹理,不透明,不混色
SDL_Texture* LoadBMPTexture(SDL_Renderer* renderer
                            , char const* filename)
{
    sdl2::BitmapSurface bmp(filename);
    if(!bmp)
    {
        return nullptr;
    }

    return sdl2::Texture(renderer, bmp._surface).Release();
}
//加载图片为纹理,并设置透明色和混色
SDL_Texture* LoadBMPTexture(SDL_Renderer* renderer
                , char const* filename
                , Uint8 key_r, Uint8 key_g, Uint8 key_b
                , Uint8 alpha_mod
                , SDL_BlendMode blend_mode = SDL_BLENDMODE_BLEND)
{
    sdl2::BitmapSurface bmp(filename);
    if(!bmp)
    {
        return nullptr;
    }
    bmp.EnableColorKey(key_r, key_g, key_b, 0);
    bmp.SetAlphaMod(alpha_mod);
    bmp.SetBlendMode(blend_mode);
    return sdl2::Texture(renderer, bmp._surface).Release();
}

//回调第二个入参param被注释掉,因为暂时不需要传递附加数据
Uint32 timer_callback(Uint32 interval, void* /*param*/)
{
    cout << "hello sdl timer\r\n";

    SDL_Event event;
    event.type = SDL_USEREVENT;
    event.user.code = 1;
    event.user.data1 = event.user.data2 = nullptr;
    SDL_PushEvent(&event);

    return interval;
}

int main(int argc, char* argv[])
{
    sdl2::Initiator::Instance().Init(SDL_INIT_VIDEO
                                     | SDL_INIT_AUDIO
                                     | SDL_INIT_EVENTS
                                     | SDL_INIT_TIMER);

    if(!sdl2::Initiator::Instance())//重载转换符
    {
        cerr << "初始化就出错,没得玩了!"
             << sdl2::last_error() << endl;
    }

    //创建并居中显示宽640,高480的游戏窗口
    sdl2::Window wnd("hello sdl"
                     , sdl2::WindowPosition()
                     , 640, 480
                     //去除原来的全屏标志
                     , sdl2::WindowFlags());

    if(!wnd)
    {
        cerr << sdl2::last_error() << endl;
        return -1;
    }

    //准备窗口的渲染器
    sdl2::Renderer renderer(wnd._window);
    if(!renderer)
    {
        cerr << sdl2::last_error() << endl;
        return -1;
    }
    //重要!修改缩放质量配置
    sdl2::RendererDriver::HintScaleQuality();
    //重要!设置虚拟大小
    renderer.SetLogicalSize(640, 480);

    //准备背景图(不需要透明和混色)
    sdl2::Texture bkgnd(LoadBMPTexture(renderer._renderer, "bkgnd.bmp"));

    if(!bkgnd)
    {
        cerr << sdl2::last_error() << endl;
        return -1;
    }

    //准备小马图,透明色为白色
    sdl2::Texture horse1(LoadBMPTexture(renderer._renderer
                                       , "sdl1.bmp"
                                       , 0xff, 0xff, 0xff));
    if(!horse1)
    {
        cerr << sdl2::last_error() << endl;
        return -1;
    }

    sdl2::Texture horse2(LoadBMPTexture(renderer._renderer
                                        , "sdl2.bmp"
                                        , 0xff, 0xff, 0xff));
    if(!horse2)
    {
        cerr << sdl2::last_error() << endl;
        return -1;
    }

    //准备白云图纹理,透明色为红色,不透明度188(0~255)
    sdl2::Texture cloud(LoadBMPTexture(renderer._renderer
                                       , "cloud.bmp"
                                       , 0xff, 0, 0, 188));
    if(!cloud)
    {
        cerr << sdl2::last_error() << endl;
        return -1;
    }

    //定时器创建标志
    SDL_TimerID timer_id = 0;
    //小马的宽度
    int w = 0;
    horse1.GetSize(&w, nullptr);
    //小马的起始位置,靠左侧站立
    SDL_Rect horse_rect{-w, 65, 468, 350};

    int flag = 0; //贴哪一张小马纹理的图标

    //白云的位置,随机数
    int cloud_x = 200;
    SDL_Rect cloud_rect_1{cloud_x, 6, 156, 78};
    SDL_Rect cloud_rect_2{cloud_x + 50, 6, 156, 78};

    //事件循环
    bool Q = false;
    while(!Q)//一直循环,直到Q为真
    {

        SDL_Event event;
        //会将队列中拖出的event数据存储到event中
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    Q = true;
                    break;

                case SDL_KEYDOWN:
                    if(event.key.keysym.sym == SDLK_t && 0 == timer_id)//按键 ctrl + t
                    {
                        timer_id = SDL_AddTimer(500, timer_callback, nullptr);
                        cout << "main thread id: " << std::this_thread::get_id() << endl;
                    }
                    break;
                case SDL_USEREVENT: //定时事件(用户自定义事件)
                    if(event.user.code == 1)
                    {
                        if(horse_rect.x <= 640)
                        {
                            horse_rect.x += 15;
                            int r = (rand() % 30 - 15);
                            cloud_rect_1.x += r;
                            cloud_rect_2.x += r;
                            flag += 1;
                        }
                        else //出了最右边
                            SDL_RemoveTimer(timer_id); //移除定时器
                    }
                    break;
            }
        }//内循环

        /*外循环:贴骏马图*/
        //贴背景->窗口
        renderer.CopyFrom(bkgnd._texture);

        //贴第一朵白云
//        SDL_Rect cloud_rect_1{200, 20, 156, 78};
        renderer.CopyFrom(cloud._texture, nullptr, &cloud_rect_1);

        //贴第二朵白云
//        SDL_Rect cloud_rect_2{340, 6, 156, 78};
        renderer.CopyFrom(cloud._texture, nullptr, &cloud_rect_2);
        //贴骏马
//        SDL_Rect dst_rect{86, 65, 468, 350};
        if(flag >= 10) //防止因循环次数过多,导致flag太大
        {
            flag = 0;
            (flag % 2 == 0)?
                renderer.CopyFrom(horse1._texture, nullptr, &horse_rect)
                :
                renderer.CopyFrom(horse2._texture, nullptr, &horse_rect);

        }else
        {
            (flag % 2 == 0)?
                renderer.CopyFrom(horse1._texture, nullptr, &horse_rect)
                :
                renderer.CopyFrom(horse2._texture, nullptr, &horse_rect);
        }

        renderer.Present();
        SDL_Delay(1);//防止cpu占用率太高
//        Q = true; //开发过程中,为方便程序退出,暂时这样
    }//外循环

    return 0;
}

文章来源:https://blog.csdn.net/yanzhenxi/article/details/135074026
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。