Ubuntu-14.04.3下SDL2测试

时间:2023-03-08 16:02:26

最近突然蛋痛想入门Linux,想写个跨平台的游戏,各种坑,备忘,稍微记点笔记。

主要参考这个文章

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

下载与安装

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

 #include <iostream>
#include <SDL2/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 = ;
int h = ; if (SDL_Init(SDL_INIT_EVERYTHING) == - ) {
cout << "error: " << SDL_GetError() << endl;
return ;
} SDL_Window* window = SDL_CreateWindow("hello", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, , , SDL_WINDOW_SHOWN);
if (window == NULL){
cout << "Error: " << SDL_GetError() << endl;
return ;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
cout << "Error: " << SDL_GetError() << endl;
return ;
} SDL_Surface* surface = SDL_LoadBMP("bk.bmp");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface); SDL_RenderClear(renderer); //清除屏幕
SDL_RenderCopy(renderer, texture, , ); //把texture画到renderer上
SDL_RenderPresent(renderer); while(!keypress)
{
//DrawScreen(screen, h++);
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
keypress = ;
break;
case SDL_KEYDOWN:
keypress = ;
break;
}
}
} SDL_Quit();
return ;
}

链接库

Ubuntu-14.04.3下SDL2测试

Ubuntu-14.04.3下SDL2测试