微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ubuntu14.04下C++开发SDL2应用

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,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); //把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

结果运行正常





版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐