第15章 《乐趣》Page378~379 转成定时事件,小马走动起来

2023-12-18 18:32:30

运行效果? :? ? ??小马走动起来-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 horse(LoadBMPTexture(renderer._renderer
                                       , "sdl.bmp"
                                       , 0xff, 0xff, 0xff));
    if(!horse)
    {
        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;
    horse.GetSize(&w, nullptr);
    //小马的起始位置,靠左侧站立
    SDL_Rect horse_rect{-w, 65, 468, 350};

    //事件循环
    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;
                        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};
        renderer.CopyFrom(horse._texture, nullptr, &horse_rect);

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

    return 0;
}

sdl_texture.hpp第30~34行,增加GetSize()函数,其他的代码和文件不变

//sdl_texture.hpp
#ifndef SDL_TEXTURE_HPP_INCLUDED
#define SDL_TEXTURE_HPP_INCLUDED

namespace sdl2
{

struct Texture
{
    Texture(SDL_Texture* texture)
        : _texture(texture)
    {

    }

    Texture(SDL_Renderer* renderer, SDL_Surface* surface)
        : _texture(SDL_CreateTextureFromSurface(renderer, surface))
    {

    }

    ~Texture()
    {
//        SDL_assert(_texture != nullptr);
        //_texture不一定就不空,所以需要修改
        if(_texture)
        SDL_DestroyTexture(_texture);
    }

    bool GetSize(int* w, int* h) const
    {
        SDL_assert(_texture != nullptr);
        return 0 == SDL_QueryTexture(_texture, nullptr, nullptr, w, h);
    }

    SDL_Texture* Release()
    {
        SDL_Texture* tmp;
        tmp = _texture;
//        *tmp = *_texture; //tmp和_texture已经指向同一数据了
        _texture = nullptr;
        return tmp;
    }

    explicit operator bool() const
    {
        return _texture != nullptr;
    }

    SDL_Texture* _texture;
};//Texture

}//sdl2

#endif // SDL_TEXTURE_HPP_INCLUDED

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