ubuntu14.04+sdl2版本开发实战

xingyun86 2018-8-21 2174

1 下载与安装

到http://www.libsdl.org/下载

http://www.libsdl.org/release/SDL2-2.0.8.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) == -1 ) {

    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, -1, 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  #修改系统共享库搜索路径

在第一行增加

include /usr/local/lib

再运行命令:

sudo ldconfig  

运行./a.out

结果运行正常


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回