Ubuntu-14.04.3下SDL2测试
最近突然蛋痛想入门Linux,想写个跨平台的游戏,各种坑,备忘,稍微记点笔记。
主要参考这个文章
下载与安装
到http://www.libsdl.org/下载
http://www.libsdl.org/release/SDL2-2.0.3.zip
解压后
./configure
make
sudo make install
检查
头文件:cd /usr/local/include/SDL2
库位置:cd /usr/local/lib
加入环境变量中
sudo vim /etc/ld.so.conf #修改系统共享库搜索路径
在第一行增加
include /usr/local/lib
再运行命令:
sudo ldconfig
CodeBlocks中测试Hello world
1 #include <iostream> 2 #include <SDL2/SDL.h> 3 4 #define WIDTH 640 5 #define HEIGHT 480 6 #define BPP 4 7 #define DEPTH 32 8 9 using namespace std; 10 11 int main(int argc, char* argv[]) 12 { 13 SDL_Surface *screen; 14 SDL_Event event; 15 16 int keypress = 0; 17 int h = 0; 18 19 if (SDL_Init(SDL_INIT_EVERYTHING) == -1 ) { 20 cout << "error: " << SDL_GetError() << endl; 21 return 1; 22 } 23 24 SDL_Window* window = SDL_CreateWindow("hello", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); 25 if (window == NULL){ 26 cout << "Error: " << SDL_GetError() << endl; 27 return 1; 28 } 29 SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); 30 if (renderer == NULL){ 31 cout << "Error: " << SDL_GetError() << endl; 32 return 1; 33 } 34 35 SDL_Surface* surface = SDL_LoadBMP("bk.bmp"); 36 SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); 37 SDL_FreeSurface(surface); 38 39 SDL_RenderClear(renderer); //清除屏幕 40 SDL_RenderCopy(renderer, texture, 0, 0); //把texture画到renderer上 41 SDL_RenderPresent(renderer); 42 43 while(!keypress) 44 { 45 //DrawScreen(screen, h++); 46 while(SDL_PollEvent(&event)) 47 { 48 switch (event.type) 49 { 50 case SDL_QUIT: 51 keypress = 1; 52 break; 53 case SDL_KEYDOWN: 54 keypress = 1; 55 break; 56 } 57 } 58 } 59 60 SDL_Quit(); 61 return 0; 62 }
链接库