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 代码示例
实现的功能
- 按上下左右键,可以实现图片的上下左右移动
- 点击鼠标左键,图片出现的鼠标所点击的位置
- 点击鼠标右键,换图片。
#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;
}