跳到主要內容

[SDL] First Program: Show Image

This is my first time using SDL.
Install SDL2 for my Linux system:

sudo apt-get install libsdl2-dev libsdl2-image-dev

Now we can test our first SDL program.
This program is pretty simple, I want to show an image as the background of a main window.
  1. In Linux system, #include <SDL2/SDL.h> as well as #include <SDL2/SDL_image.h>
  2. SDL_Init(SDL_INIT_VIDEO) should be called before initializing the main window.
  3. Using SDL_Surface for loading image, SDL_Renderer and SDL_Texture for rendering operation.
  4. Free memory.
Code:

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

using namespace std;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

int main( int argc, char** argv)
{
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        cout << " [Error] SDL initialization fail: " << SDL_GetError() << endl;
        exit(1);
    }

    SDL_Window* main_win = SDL_CreateWindow( "Practice_01",
                           SDL_WINDOWPOS_UNDEFINED,
                           SDL_WINDOWPOS_UNDEFINED,
                           SCREEN_WIDTH,
                           SCREEN_HEIGHT,
                           SDL_WINDOW_SHOWN);

    if(main_win == NULL)
    {
        cout << " [Error] Creating main window fail: " << SDL_GetError() << endl;
    }
    SDL_Event event;
    SDL_Surface* bg = IMG_Load(argv[1]);
    SDL_Renderer* renderer = SDL_CreateRenderer(main_win, -1, 0);
    SDL_Texture*  texture  = SDL_CreateTextureFromSurface(renderer, bg);

    while(true)
    {
       SDL_WaitEvent(&event);
       if(event.type == SDL_QUIT)
       {
           break;
       }
       SDL_RenderClear(renderer);
       SDL_RenderCopy(renderer, texture, NULL, NULL);
       SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_FreeSurface(bg);
    SDL_DestroyWindow(main_win);
    SDL_Quit();
    return 0;
}



How to compile:
g++ ../show_img.cpp -w -lSDL2 -lSDL2_image -o ./show_img


留言

這個網誌中的熱門文章

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[Vim] 我的 .vimrc

我用過的文字編輯器有 sublime text/atom/notepad++/gedit 等等。 後來我在 window 和 Linux 寫程式還是比較慣用 vim。 以下整理我常用的 .vimrc:  set nu "show line number. set rnu "related line number. nmap ; : "replace ';' with ':' in normal mode. set ai "auto indent set mouse=a "activate mouse set expandtab "replace tab with space. set tabstop=4 "replace tab with 4 spaces. set bg=dark "background set to dark. color desert "color scheme. syntax on "enable syntax highlight. " auto complete: inoremap ( ()<Esc>i inoremap ' ''<Esc>i inoremap " ""<Esc>i inoremap [ []<Esc>i inoremap { {}<Esc>i set cursorline " active cursorline 到此 vim 搭配 Ctrl + N 當自動補全,已經很好用了,之後有好用設定會再補上來。

[開箱] Filco Majestouch 2 忍者鍵盤第二代

最近買了一個新款的 Filco 忍者鍵盤第二代,NINJA Majestouch 2: 鍵盤對我來說蠻重要的,以前就很喜歡 Filco 品牌的鍵盤,我大學使用的第一支茶軸鍵盤就是 Filco 的牌子,現在已經充滿了灰塵,使用了至少已有 8 年了吧: 畢業後有買另一個 irock 鍵盤: 這禮拜拆開新鍵盤來看看: 內部包裝 內部包裝 採用按鍵的文字採用側印 鍵帽與拔鍵器 整體外觀 整體而言,美感簡約低調,按鍵的反饋力也很好,是一個適合寫程式的鍵盤。 相較於我的另一個也是德國櫻桃茶軸鍵盤 irock 品牌,我覺得 Filco 的按鍵回饋比較綿密,有漸層的回饋感,且按鍵聲音比較安靜。irock 則比較像機械打字的回饋感,敲字時也都蠻舒壓。