Qt5.5 SDL2.0 鼠标事件,键盘事件,BMP图片加载

1Q5.5 +SDL环境搭建

1.1pro 文件配置
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp


include(deployment.pri)
qtcAddDeployment()

# -L后面不能有空格  #LIBS += -L./lib/x86 -lSDL2
#LIBS += -L./lib/x86 -lSDL2main
#LIBS += -L./lib/x86 -lSDL2test

LIBS += -L$$_PRO_FILE_PWD_/lib/x86 -lSDL2
                    -lSDL2main
                    -lSDL2test
1.1用到的图片

这里写图片描述
这里写图片描述

2 代码示例

实现的功能

  1. 按上下左右键,可以实现图片的上下左右移动
  2. 点击鼠标左键,图片出现的鼠标所点击的位置
  3. 点击鼠标右键,换图片。
#include <iostream>
#include "include/SDL.h"
#undef main
using namespace std;

int main()
{
    bool bQuit = false;

    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window * window = SDL_CreateWindow("first",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,600,400,SDL_WINDOW_RESIZABLE);
    SDL_Renderer * render = SDL_CreateRenderer(window,-1,SDL_RENDERER_SOFTWARE);

    SDL_Surface *bmp = SDL_LoadBMP("../abc.bmp");
    if(bmp == NULL)
        cout<<"surface error"<<endl;

    SDL_Surface *bmp2 = SDL_LoadBMP("../aa.bmp");
    if(bmp == NULL)
        cout<<"surface error"<<endl;

    SDL_Texture * texture  = SDL_CreateTextureFromSurface(render,bmp);
    if(texture == NULL)
        cout<<"texture error"<<endl;

    static bool flag = true;



    SDL_Rect srcRet;


    SDL_Rect dstRect;

    dstRect.x = 30;
    dstRect.y = 40;
    dstRect.w = 200;
    dstRect.h = 200;


    SDL_Event ev;
    while(!bQuit)
    {

        while(SDL_PollEvent(&ev))
        {
            switch(ev.type)
            {
                case SDL_KEYDOWN:
                    switch(ev.key.keysym.sym)
                    {
                        case SDLK_DOWN:
                            cout<<"down"<<endl;
                            dstRect.y -= 10;
                            break;
                        case SDLK_UP:
                            cout<<"up"<<endl;
                            dstRect.y += 10;
                            break;

                        case SDLK_LEFT:
                            cout<<"left"<<endl;
                            dstRect.x -= 10;
                            break;

                        case SDLK_RIGHT:
                            cout<<"right"<<endl;
                            dstRect.x += 10;
                            break;

                    }
                    break;

                case SDL_MOUSEBUTTONDOWN:
                    cout<<"mouse"<<endl;
                    switch(ev.button.button)
                    {
                        case SDL_BUTTON_LEFT:
                            dstRect.x = ev.button.x;
                            dstRect.y = ev.button.y;
                            break;

                        case SDL_BUTTON_RIGHT:
                            if(flag)
                                texture  = SDL_CreateTextureFromSurface(render,bmp2);
                            else
                                texture  = SDL_CreateTextureFromSurface(render,bmp);
                            flag = !flag;
                            break;

                    }

                    break;

                case SDL_QUIT:
                    bQuit == true;
                    return 0;

            }
        }
        SDL_RenderClear(render);
        SDL_RenderCopy(render,texture,NULL,&dstRect);
        SDL_RenderPresent(render);


    }

    SDL_Quit();
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

developer_wgl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值