Simple DirectMedia Layer 이하 SDL은 OpenGL 및 Direct3D를 통해 오디오, 키보드, 마우스, 조이스틱 및 그래픽 하드웨어에 대한 저수준 액세스를 제공하도록 설계된 크로스 플랫폼 개발 라이브러리다.
SDL은 1.x버전대와 2.x 버전대가 각각 공존하여 둘 다 사용되는데 라이센스가 다르다. SDL 1.2의 라이센스는 LGPL이고 SDL 2.0 이상은 zlib 라이센스에 따라 사용할 수 있다.
리눅스에서 apt를 이용하면 SDL2 패키지를 쉽게 설치 할 수 있지만 이 글은 최신 소스코드를 직접 컴파일 후 Qt Creator로 개발하는 방법을 설명한다.
OS: 우분투 18.04
IDE: Qt Creator 4.10
필수 패키지 설치
$ sudo apt update
$ sudo apt install build-essential pkg-config libgl1-mesa-dev
Qt 및 Qt Creator의 설치 방법은 다음 글을 참고: https://makersweb.net/qt/963
SDL2 소스코드 다운로드와 컴파일 및 설치
https://www.libsdl.org를 방문하면 최신 소스코드를 다운로드받을 수 있다.
wget명령으로 소스코드 다운로드를 쉽게 할 수 있다.
$ wget https://www.libsdl.org/release/SDL2-2.0.10.tar.gz
$ tar xvf SDL2-2.0.10.tar.gz
$ cd SDL2-2.0.10
$ ./configure
다음은 필자의 SDL2 Configure Summary 이다. Video drivers의 내용이 다음과 비슷해야한다.
$ make all
$ sudo make install
이미지 및 폰트를 나타내기위해 추가 라이브러리가 필요하다.
주요 SDL 라이브러리
SDL 모듈 |
설명 |
사이트 주소 |
SDL |
비디오 및 오디오 처리를 위한 기본 API 제공 |
http://www.libsdl.org |
SDL_mixer |
사운드 처리를 위한 모듈 |
http://www.libsdl.org/projects/SDL_mixer |
SDL_ttf |
트루타입 폰트 출력 |
https://www.libsdl.org/projects/SDL_ttf |
SDL_net |
네트워킹 라이브러리 |
http://www.libsdl.org/projects/SDL_net |
SDL_rtf |
서식 있는 텍스트 포맷 렌더링 |
http://www.libsdl.org/projects/SDL_rtf |
SDL_image |
이미지 파일 처리 지원 |
https://www.libsdl.org/projects/SDL_image |
SDL_gfx |
점, 선 등의 기본 프리미티브 렌더링 |
https://github.com/ferzkopp/SDL_gfx http://sourceforge.net/projects/sdlgfx |
https://www.libsdl.org/projects/ 를 방문하면 더 많은 라이브러리 최신 소스코드를 다운로드 받을 수 있다.
SDL_image 소스코드 다운로드와 컴파일 및 설치
$ wget https://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.5.tar.gz
$ tar xvf SDL2_image-2.0.5.tar.gz
$ cd SDL2_image-2.0.5/
$ ./configure
$ make all
$ sudo make install
새로운 Qt Creator 프로젝트 만들기
Qt Creator실행 후 New Project 클릭
Non-Qt Project - Plain C Application으로 프로젝트 생성
빌드시스템(Build System) 선택
qmake로 생성한 경우
프로젝트 생성 완료 후 .pro 파일에 다음과 같이 라이브러리 사용을 명시한다.
Qbs를 선택한 경우
*.qbs파일에 다음과 같이 Depends를 추가한다.
SDL은 기본적으로 다음과 같은 헤더를 포함해야한다.
#include <SDL2/SDL.h>
추가로 이미지를 출력하기 위해 다음 헤더를 포함한다.
#include <SDL2/SDL_image.h>
다음은 윈도우를 생성하고 이미지를 출력하는 간단한 예제이다.
main.c
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_image.h>
int main(void)
{
// attempt to initialize graphics and timer system
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
{
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window* window = SDL_CreateWindow("Hello, makersweb",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480, 0);
if (!window)
{
printf("error creating window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// create a renderer, which sets up the graphics hardware
Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, render_flags);
if (!renderer)
{
printf("error creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// load the image into memory using SDL_image library function
SDL_Surface* surface = IMG_Load("resources/makersweb.png");
if (!surface)
{
printf("error creating IMG_Load surface\n");
goto destroy;
}
// load the image data into the graphics hardware's memory
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (!texture)
{
printf("error creating texture: %s\n", SDL_GetError());
goto destroy;
}
// clear the window
SDL_RenderClear(renderer);
// draw the image to the window
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
// delay a exiting.
SDL_Delay(5000);
// clean up resources before exiting.
SDL_DestroyTexture(texture);
destroy:
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
Run을 실행하면 윈도우와 함께 이미지가 출력된다.
qmake 프로젝트 압축파일: appsdl.tar.gz
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
15 | C++를 위한 Lottie 라이브러리 with SDL2 | makersweb | 2021.08.15 | 996 |
14 | NAppGUI, C언어용 크로스 플랫폼 GUI 라이브러리 | makersweb | 2022.10.10 | 917 |
13 | Protocol Buffers 를 이용한 직렬화 with Conan Package Manager | makersweb | 2021.02.24 | 888 |
12 | ZeroMQ 를 이용한 Qt 응용프로그램 간 통신 | makersweb | 2021.08.28 | 887 |
11 | VSCode 와 Qbs 플러그인으로 C/C++ 개발환경 구성 | makersweb | 2021.09.12 | 881 |
10 | 윈도우에서 안드로이드 flutter 프로그래밍 개발환경 구축(with Visual Studio Code) | makersweb | 2020.09.16 | 811 |
9 | Chromium과 Ozone 층 | makersweb | 2022.03.03 | 763 |
8 | Windows에서 Qt Creator + CMake + vcpkg 로 C++ 개발환경 구성 (POCO 라이브러리 DirectoryWatcher 예제) | makersweb | 2023.01.14 | 733 |
7 | 라즈베리파이에서 Redis의 Pub/Sub 패턴을 사용하는 Electron 응용프로그램 | makersweb | 2021.01.31 | 702 |
6 | 라즈베리파이4에서 openFrameworks 예제 실행 | makersweb | 2020.12.13 | 569 |
5 | CopperSpice 에 대해서 (C++ Gui 라이브러리) | makersweb | 2022.01.02 | 562 |
4 | OTA 오픈소스 프로젝트 | makersweb | 2022.08.03 | 515 |
3 | 도커(docker)설치 및 기본 명령어 | makersweb | 2019.12.02 | 494 |
2 | Flutter 위젯의 상태관리에 대해서 | makersweb | 2023.04.06 | 472 |
1 | openFrameworks 한글 폰트 설정 및 출력하기 | makersweb | 2023.02.19 | 235 |