Ubuntu14.04下C++开发SDL2应用
来源:程序员人生 发布时间:2015-01-21 09:08:11 阅读次数:5117次
1 下载与安装
到http://www.libsdl.org/下载http://www.libsdl.org/release/SDL2⑵.0.3.zip源码
解压后
./configure
make
sudo make install
检查
头文件:/usr/local/include/SDL2
库位置:/usr/local/lib
2 建立测试程序:
#include <iostream>
#include <SDL.h>
#define WIDTH 640
#define HEIGHT 480
#define BPP 4
#define DEPTH 32
using namespace std;
int main(int argc, char* argv[])
{
SDL_Surface *screen;
SDL_Event event;
int keypress = 0;
int h = 0;
if (SDL_Init(SDL_INIT_EVERYTHING) == ⑴ ) {
cout << "error: " << SDL_GetError() << endl;
return 1;
}
SDL_Window* window = SDL_CreateWindow("hello", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
if (window == NULL){
cout << "Error: " << SDL_GetError() << endl;
return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, ⑴, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
cout << "Error: " << SDL_GetError() << endl;
return 1;
}
SDL_Surface* surface = SDL_LoadBMP("bk.bmp");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
SDL_RenderClear(renderer); //清除屏幕
SDL_RenderCopy(renderer, texture, 0, 0); //把texture画到renderer上
SDL_RenderPresent(renderer);
while(!keypress)
{
//DrawScreen(screen, h++);
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
keypress = 1;
break;
case SDL_KEYDOWN:
keypress = 1;
break;
}
}
}
SDL_Quit();
return 0;
}
3 编写编译命令build文件,内容以下:
g++ sdl_app1.cpp -I/usr/local/include/SDL2 -L/usr/local/lib -lSDL2 -lpthread
设置该文件的履行权限:
chmod +x build
运行编译命令:
./build
产生履行文件a.out
4 运行程序
./a.out
若出现找不到.so.0...,即找不到动态链接库的情况
运行命令:
sudo vim /etc/ld.so.conf #修改系统同享库搜索路径
在第1行增加
include /usr/local/lib
再运行命令:
sudo ldconfig
运行./a.out
结果运行正常
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠