Linux SDL2 installation and C program design

本版討論Raspberry PI相關技術開發、設計與應用
回覆文章
inho
文章: 42
註冊時間: 2017-12-08, 23:00

Linux SDL2 installation and C program design

文章 inho » 2018-01-27, 15:49

安裝之前先做以下幾件事:
/***************************************************************
// Linux Update and Upgrade
//***************************************************************
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade

/*****************************************************
// 安裝 SDL2 Source Code (OK)
// http://blog.shahada.abubakar.net/post/h ... spberry-pi
//*****************************************************
$ cd ~
$ wget https://www.libsdl.org/release/SDL2-2.0.7.tar.gz
$ tar zxvf SDL2-2.0.7.tar.gz
$ cd SDL2-2.0.7 && mkdir build && cd build
$ ../configure
$ make -j 4
$ sudo make install
// 安裝之後,在 /usr/local/include 目錄下會看到一個新安裝的 SDL2目錄 (/usr/local/includesdl/SDL2/),目錄之下包含SDL2相關的 .h header file
// 同時在 /usr/local/lib 目錄下會看到幾個新安裝的 SDL2 相關函式庫檔案:libSDL2XXXX等,表示 SDL2安裝成功
// 接下來還要安裝幾個SDL2相關的配件函式庫:SDL2_image、SDL2_ttf、SDL2_net等
//**************************************************
// download, build and install SDL_image: (SDL2_image-2.0.2 ERROR ?) (SDL2_image-2.0.1 OK)
//*********************************************************
$ cd ~
$ wget http://www.libsdl.org/projects/SDL_imag ... 0.2.tar.gz
$ wget http://www.libsdl.org/projects/SDL_imag ... 0.1.tar.gz
$ tar zxvf SDL2_image-2.0.2.tar.gz
$ tar zxvf SDL2_image-2.0.1.tar.gz
$ cd SDL2_image-2.0.2
$ ./autogen.sh
$ ./configure
$ make -j4
$ sudo make install
$ cd ..
// 安裝之後,在 /usr/local/includesdl/SDL2/目錄之下看到一個SDL2_image .h header file
// 同時在 /usr/local/lib 目錄下會看到幾個新安裝的 libSDL2_image.a .la .so 等函式庫,表示 SDL2_image 安裝成功

//*********************************************************
// download, build and install SDL_mixer: (OK)
//*******************************************************************
$ cd ~
$ wget http://www.libsdl.org/projects/SDL_mixe ... 0.2.tar.gz
$ tar zxvf SDL2_mixer-2.0.2.tar.gz
$ cd SDL2_mixer-2.0.2 && mkdir build && cd build
$ ../configure
$ make -j4
$ sudo make install
// 安裝之後,在 /usr/local/includesdl/SDL2/目錄之下看到一個SDL2_mixer .h header file
// 同時在 /usr/local/lib 目錄下會看到幾個新安裝的 libSDL2_mixer.a .la .so 等函式庫,表示 SDL2_mixer 安裝成功

//**************************************************************
// download, build and install SDL_net: (OK)
//******************************************************************
$ cd ~
$ wget http://www.libsdl.org/projects/SDL_net/ ... 0.1.tar.gz
$ tar zxvf SDL2_net-2.0.1.tar.gz
$ cd SDL2_net-2.0.1 && mkdir build && cd build
$ ../configure
$ make -j 4
$ sudo make install

//********************************************************
// download, build and install SDL_smpeg2: (OK)
//  /usr/local/include/smpeg2, /usr/local/lib
//****************************************************************
$ cd ~
$ wget http://www.libsdl.org/projects/smpeg/re ... 0.0.tar.gz
$ tar zxvf smpeg2-2.0.0.tar.gz
$ cd smpeg2-2.0.0 && mkdir build && cd build
$ ../configure
$ make -j 4
$ sudo make install

//****************************************************
// download, build and install SDL_ttf: (ERROR)
// 安裝這個函式庫時,可能會失敗,可以上網Google一下解決方案
//*************************************************************
$ cd ~
$ wget http://www.libsdl.org/projects/SDL_ttf/ ... .14.tar.gz
$ tar zxvf SDL2_ttf-2.0.14.tar.gz
$ cd SDL2_ttf-2.0.14
//*************
$ mkdir build && cd build
$ ../configure
//******************* OR
$ cd SDL2_ttf-2.0.14
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install
// 如果 SDL2_ttf 安裝失敗 (檢查 /usr/local/include/SDL2/ 目錄底下有沒有出現 SDL2_ttf.h Header file,沒有這個檔案就是安裝失敗 )
//*************************************************************
// 用其他方案安裝 SDL2_ttf 函式庫:
//************************************************************
$ sudo apt-get install libsdl2-ttf-dev
// 安裝完成後 會在 /usr/include/ 底下看到一個新的 SDL2目錄,裡面包含前述 SDL2相關的 header file,包括 SDL2_ttf.h、SDL2.h 等header file,這種安裝// 方式預設的SDL2並非最新版本,所以,把其中的 SDL2_ttf.h Copy 到 /usr/local/include/SDL2/ 目錄下即可,
// 但是,SDL2_ttf相關的函式庫 仍然安裝在 /usr/local/lib 目錄下。
$ sudo cp /usr/include/SDL2/SDL_ttf.h /usr/local/include/SDL2

// 以上的安裝在 ubuntu 16.0.4 及 17.0.4都是成功的
// 再來,測試 SDL2 的應用程式,回到 user的 home,或建造一個 testSDL目錄,編輯一個 C Program
$ cd ~
$ vi SDLimage.c
#include <SDL2/SDL.h>

int main()
{
SDL_Window *window;
SDL_Surface *surface;
SDL_Rect rect = {0, 0, 200, 200};

SDL_Init(SDL_INIT_EVERYTHING);

window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
if (!window) {
fprintf(stderr, "main: %s\n", SDL_GetError());
return -1;
}

surface = SDL_GetWindowSurface(window);
if (!surface) {
fprintf(stderr, "main: %s\n", SDL_GetError());
return -1;
}

SDL_FillRect(surface, &rect, 0xffff0000);
SDL_UpdateWindowSurface(window);
SDL_Delay(1000);

SDL_DestroyWindow(window);
SDL_Quit();
}
// 存檔之後, 編譯這個程式:
$ gcc -o SDLimage sdlimage.c `sdl2-config --cflags --libs`
// 其中,`sdl2-config --cflags --libs` option將自動設定 SDL2的 include path指向相關目錄位置,及 linker 的 lib path,如果想知道相關檔案及目錄位置,可執行以下指令看看結果:
$ sdl2-config --cflags --libs
// 將得到以下結果:
$ -I/usr/local/include/SDL2 -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -D_REENTRANT
-L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2
// -I 是指示 Compiler 讀取 SDL2 相關 include header file 的路徑,-L 是指示 Linker 連結 SDL2 相關 library file 的路徑及函式庫。
$ ls -las
// Compile 成功會看到 SDLimage.o 執行檔,執行這個檔案:
$ ./SDLimage
// 如果看到一個新的SDL視窗,顯示紅色方塊 1000 msec.(1 second) 就消失,表示 SDL2 測試成功。
// 關於 SDL2 詳細資訊,可google 一下就有很多資料,用來開發 game、跨平台Real-time 應用程式,非常好用,...

// 以上安裝方式在raspberry pi 上執行時會出現問題,出現:SDL2 error: No available video device
// 在raspberry pi 上要使用 SDL2 開發應用程式,必須 安裝 X11 才可以顯示 SDL2 視窗
最後由 inho 於 2018-03-01, 23:29 編輯,總共編輯了 1 次。

inho
文章: 42
註冊時間: 2017-12-08, 23:00

Re: Raspberry PI SDL2 installation 自動執行檔設計

文章 inho » 2018-02-23, 20:53

以下為 自動執行安裝SDL2的shell program
編輯後存成檔案:install_SDL207_rpi.sh (儲存在user 跟目錄底下)
完成存檔後,設為可執行檔案屬性:
$ chmod 755 install_SDL207_rpi.sh
=================================================================================
#!/bin/bash

# --------------------------------------------------------
# Installs SDL2 from source on the Raspberry Pi (Raspbian)
#
# Run from the web:
# bash <(curl -s raw_script_url_here)
# --------------------------------------------------------

bold="\033[1m"
normal="\033[0m"

# Welcome message
echo -e "\n${bold}This will install SDL2 from source."
echo -e "It will take about an hour to compile on the Raspberry Pi.${normal}"

# Prompt to continue
read -p " Continue? (y/n) " ans
if [[ $ans != "y" ]]; then
echo -e "\nQuitting...\n"
exit
fi
echo

# Time the install process
START_TIME=$SECONDS

# Setting up SDL2 variables
url="http://www.libsdl.org"

sdl="SDL2-2.0.7"
sdl_url="${url}/release/${sdl}.tar.gz"

image="SDL2_image-2.0.2"
image_url="${url}/projects/SDL_image/release/${image}.tar.gz"

mixer="SDL2_mixer-2.0.2"
mixer_url="${url}/projects/SDL_mixer/release/${mixer}.tar.gz"

ttf="SDL2_ttf-2.0.14"
ttf_url="${url}/projects/SDL_ttf/release/${ttf}.tar.gz"

get () {
cd /tmp
wget -N $1
tar -xzf $2.tar.gz
cd $2
./configure && make && sudo make install
rm /tmp/$2.tar.gz
rm -rf /tmp/$2
}

get ${sdl_url} ${sdl}
get ${image_url} ${image}
get ${mixer_url} ${mixer}

sudo apt-get install -y libfreetype6-dev # ttf dependency
get ${ttf_url} ${ttf}

# Print the time elapsed
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo -e "\n${bold}Finished in $(($ELAPSED_TIME/60/60)) hr, $(($ELAPSED_TIME/60%60)) min, and $(($ELAPSED_TIME%60)) sec${normal}\n"
=======================================================================================================
存檔後,執行以下指令:(會需要半個鐘頭以上的時間)
$ ./install_SDL207_rpi.sh

SDL2_net 未包含在裡面,可以自己加進去。完成安裝後,檢視一下: /usr/local/include/ 目錄下,應該會有一個 SDL2子目錄,包括所有相關SDL2的Header Files,但是,要特別注意, 是否有 SDL2_ttf.h file,還有,在 /usr/local/lib/目錄下,也應該會有 SDL2 相關的 Library
最後由 inho 於 2018-02-23, 23:30 編輯,總共編輯了 2 次。

inho
文章: 42
註冊時間: 2017-12-08, 23:00

Re: Linux SDL2 C 及 C++ program Examples

文章 inho » 2018-02-23, 21:08

編輯以下程式,SDL_Ex1.c
===============================================
#include <SDL2/SDL.h>

int main()
{
SDL_Window *window;
SDL_Surface *surface;
SDL_Rect rect = {0, 0, 200, 200};

SDL_Init(SDL_INIT_EVERYTHING);

window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
if (!window) {
fprintf(stderr, "main Create Window Error : %s\n", SDL_GetError());
return -1;
}

surface = SDL_GetWindowSurface(window);
if (!surface) {
fprintf(stderr, "main: Surface Error %s\n", SDL_GetError());
return -1;
}

SDL_FillRect(surface, &rect, 0xffff0000);
SDL_UpdateWindowSurface(window);
SDL_Delay(1000);

SDL_DestroyWindow(window);
SDL_Quit();
}
============================================
存檔後,編譯指令為:
$ gcc -o SDL_Ex1 SDL_Ex1.c `sdl2-config --cflags --libs`

以下為cpp範例 (sdl2_test.cpp)
======================================================================
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>

// Manage error messages
void check_error_sdl(bool check, const char* message);
void check_error_sdl_img(bool check, const char* message);

// Load an image from "fname" and return an SDL_Texture with the content of the image
SDL_Texture* load_texture(const char* fname, SDL_Renderer *renderer);


int main(int argc, char** argv) {
// Initialize SDL
check_error_sdl(SDL_Init(SDL_INIT_VIDEO) != 0, "Unable to initialize SDL");

// Create and initialize a 800x600 window
SDL_Window* window = SDL_CreateWindow("Test SDL 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
check_error_sdl(window == nullptr, "Unable to create window");

// Create and initialize a hardware accelerated renderer that will be refreshed in sync with your monitor (at approx. 60 Hz)
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
check_error_sdl(renderer == nullptr, "Unable to create a renderer");

// Set the default renderer color to corn blue
SDL_SetRenderDrawColor(renderer, 100, 149, 237, 255);

// Initialize SDL_img
int flags=IMG_INIT_JPG | IMG_INIT_PNG;
int initted = IMG_Init(flags);
check_error_sdl_img((initted & flags) != flags, "Unable to initialize SDL_image");

// Load the image in a texture
SDL_Texture *texture = load_texture("img_test.png", renderer);

// We need to create a destination rectangle for the image (where we want this to be show) on the renderer area
SDL_Rect dest_rect;
dest_rect.x = 50; dest_rect.y = 50;
dest_rect.w = 337; dest_rect.h = 210;

// Clear the window content (using the default renderer color)
SDL_RenderClear(renderer);

// Copy the texture on the renderer
SDL_RenderCopy(renderer, texture, NULL, &dest_rect);

// Update the window surface (show the renderer)
SDL_RenderPresent(renderer);

// Wait for 10 seconds
SDL_Delay(10000);


// Clear the allocated resources
SDL_DestroyTexture(texture);
IMG_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

return 0;
}

// In case of error, print the error code and close the application
void check_error_sdl(bool check, const char* message) {
if (check) {
std::cout << message << " " << SDL_GetError() << std::endl;
SDL_Quit();
std::exit(-1);
}
}

// In case of error, print the error code and close the application
void check_error_sdl_img(bool check, const char* message) {
if (check) {
std::cout << message << " " << IMG_GetError() << std::endl;
IMG_Quit();
SDL_Quit();
std::exit(-1);
}
}

// Load an image from "fname" and return an SDL_Texture with the content of the image
SDL_Texture* load_texture(const char* fname, SDL_Renderer *renderer) {
SDL_Surface *image = IMG_Load(fname);
check_error_sdl_img(image == nullptr, "Unable to load image");

SDL_Texture *img_texture = SDL_CreateTextureFromSurface(renderer, image);
check_error_sdl_img(img_texture == nullptr, "Unable to create a texture from the image");
SDL_FreeSurface(image);
return img_texture;
}

========================================================================
編譯指令:
$ g++ -std=c++0x -Wall -pedantic sdl2_test.cpp -o sdl2_test `sdl2-config --cflags --libs` -lSDL2_image

inho
文章: 42
註冊時間: 2017-12-08, 23:00

Re: Linux Ubuntu SDL2 installation and C program design

文章 inho » 2018-03-01, 23:13

Reference Page:
https://wiki.libsdl.org/Installation
=====================================
$ hg clone https://hg.libsdl.org/SDL SDL
if no hg command, run:
$ sudo apt install mercurial
then, again
$ hg clone https://hg.libsdl.org/SDL SDL
$ cd SDL
$ mkdir build
$ cd build
$ ../configure
$ make -j4
$ sudo make install

回覆文章