第15章 《乐趣》Page518~520 图片光标,简化代码

//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>
#include "sdl_timer.hpp"

#include "sdl_cursor.hpp"

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();
}

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

    //定时器
    sdl2::Timer timer;

    //小马的宽度
    int w = 0;
    horse.GetSize(&w, nullptr);
    //小马的起始位置,靠左侧站立
    SDL_Rect horse_rect{-w, 65, 468, 350};

    //创建以白色作为透明色,并和背景略为混色的光标
    //热点在50,50位置上
    sdl2::Cursor cursor("cursor.bmp", 50, 50
                        , true, 0xFF, 0xFF, 0xFF);
    if(!cursor)
    {
        cerr << sdl2::last_error() << endl;
        return -1;
    }

    cursor.Apply(); //生效

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

    //事件循环
    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:
                                                    //即timer为空
                    if(event.key.keysym.sym == SDLK_t && !timer)//按键 ctrl + t,
                    {
                        int const horse_run_timer_code = 1;
                        timer.Start(500, horse_run_timer_code);
                    }
                    break;
                case SDL_USEREVENT: //定时事件(用户自定义事件)
                    if(event.user.code == 1)
                    {
                        if(horse_rect.x <= 640)
                            horse_rect.x += 15;
                        else //出最右边了
                            timer.Stop();
                    }
                    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();
        sdl2::Timer::Delay(1);//防止cpu占用率太高
//        Q = true; //开发过程中,为方便程序退出,暂时这样
    }//外循环

    return 0;
}

//sdl_cursor.hpp
#ifndef SDL_CURSOR_HPP_INCLUDED
#define SDL_CURSOR_HPP_INCLUDED

namespace sdl2
{

struct Cursor
{
public:

    Cursor(char const* filename
                    , int hot_x, int hot_y
                    , bool flag
                    , Uint8 key_r, Uint8 key_g, Uint8 key_b)
    {
        Surface surface(filename);
        if(flag)
        {
            surface.EnableColorKey(key_r, key_g, key_b, 188);
        }
        _cursor = SDL_CreateColorCursor(surface._surface, hot_x, hot_y);
    }

    ~Cursor()
    {
        if(_cursor)
        {
            SDL_FreeCursor(_cursor);
        }
    }

    //判断应用程序是不是在使用本光标
    bool IsApplied() const
    {
        return _cursor == SDL_GetCursor();
    }

    //生效为应用程序的当前光标
    void Apply()
    {
        SDL_assert(_cursor != nullptr);
        SDL_SetCursor(_cursor);
    }

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

    SDL_Cursor* _cursor;
};

}//sdl2

#endif // SDL_CURSOR_HPP_INCLUDED

sdl_surface.hpp   19~22行增加一个从图片构造表层的构造函数,这个构造函数在sdl_cursor.hpp中用到

//sdl_surface.hpp
#ifndef SDL_SURFACE_HPP_INCLUDED
#define SDL_SURFACE_HPP_INCLUDED

namespace sdl2
{

struct Surface
{

    //代管外部创建好的surface指针
    explicit Surface(SDL_Surface* surface)
        : _surface(surface)
    {

    }

    //直接从图片构建一个表层
    Surface(char const* filename)
    {
        _surface = SDL_LoadBMP(filename);
    }
    /*父类的析构函数使用虚函数的主要原因是为了确保多态时的正确清理。
      在C++中,析构函数主要用于释放动态分配的资源。如果父类的析构函数不是虚函数,
      那么当使用子类指针删除父类对象时,由于没有动态绑定(晚绑定),
      只会调用父类的析构函数,而不会调用子类的析构函数。
      这样,子类中的资源可能不会被正确释放,导致内存泄漏或其他问题。

     如果父类的析构函数是虚函数,那么当使用子类指针删除父类对象时,
     会根据实际对象的类型动态调用相应的析构函数。这样,既可以释放父类占用的资源,
     又可以释放子类占用的资源,确保资源的正确释放。
     因此,为了确保多态时的正确清理,父类的析构函数应该声明为虚函数。*/
    virtual ~Surface()
    {
        SDL_assert(_surface != nullptr);
        SDL_FreeSurface(_surface);
    };

    bool SetAlphaMod(Uint8 alpha)
    {
        SDL_assert(_surface != nullptr);
        return 0 == SDL_SetSurfaceAlphaMod(_surface, alpha);
    }

    bool SetBlendMode(SDL_BlendMode const& mode)
    {
        SDL_assert(_surface != nullptr);
        return 0 == SDL_SetSurfaceBlendMode(_surface, mode);
    }

    //    EnableColorKey(Uint32 r, Uint32 g, Uint32 b, Uint32 a)
    bool EnableColorKey(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
    {
        SDL_assert(_surface != nullptr);
        Uint32 key = SDL_MapRGBA(_surface->format, r, g, b, a);
        return 0 == SDL_SetColorKey(_surface, SDL_TRUE, key);
    }

    bool BlitTo(SDL_Surface* dst_surface, SDL_Rect* src_rect, SDL_Rect* dst_rect)
    {
        SDL_assert(_surface != nullptr);
        return 0 == SDL_BlitSurface(_surface, src_rect, dst_surface, dst_rect);
    }

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

    SDL_Surface* _surface;
};

//来自位图的表层
struct BitmapSurface : public Surface
{
    explicit BitmapSurface (char const* filename)
        : Surface(SDL_LoadBMP(filename))
    {

    }
};

}//sdl2

#endif // SDL_SURFACE_HPP_INCLUDED

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值