天天看點

SDL的使用

建立視窗 

SDL_Init/SDL_Quit()

SDL_CreateWindow()/SDL_DestoryWindow()

SDL_CreateRender() 

渲染視窗

SDL_CreateRender/SDL_DestoryRenderer

SDL_RenderClear  //清空視窗

SDL_RenderPresent  //展示視窗 

SDL事件

SDL_WindowEvent 視窗事件

SDL_KeyboardEvent 鍵盤事件

SDL_MouseMotionEvent 滑鼠事件

SDL_PollEvent  輪詢事件

SDL_WaitEvent  等待事件

SDL渲染

圖像---渲染器--->紋理-----交換----->視窗顯示

紋理API:::SDL_CreateTexture()

                                format:YUV,RGB

                                 access:Texture類型,target,stream

                        SDL_DestoryTexture()

渲染API:     SDL_SetRenderTarget()  改變目标

                      SDL_RenderClear()

                       SDL_RenderCopy()  紋理複制給渲染器。

                        SDL_RenderPresnet()  顯示。

#include<SDL/SDL.h>
#include<iostream>
using namespace std;

int main(int argc, char* argv[])
{
	SDL_Window* w = nullptr;
	SDL_Renderer* r = nullptr;
	int quit = 1;
	SDL_Event e;
	SDL_Texture* t;

	SDL_Init(SDL_INIT_VIDEO);
	w = SDL_CreateWindow("123", 100, 100, 200, 400, SDL_WINDOW_RESIZABLE);

	r = SDL_CreateRenderer(w, -1, -0);
	SDL_RenderClear(r);
	SDL_RenderPresent(r);

	t=SDL_CreateTexture(r, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 600, 400);

	do
	{		
		SDL_WaitEvent(&e);
		switch (e.type)
		{
		case SDL_QUIT:quit = 0;break;
		}

		SDL_Rect re;///建立一個随機跳動方框
		re.w = 30;
		re.h = 30;
		re.x = rand() % 600;
		re.y = rand() % 300;
		SDL_SetRenderTarget(r, t);//輸出至紋理
		SDL_SetRenderDrawColor(r, 0, 0, 0, 0);
		SDL_RenderClear(r);
		SDL_RenderDrawRect(r, &re);
		SDL_SetRenderDrawColor(r, 255, 0, 0, 0);
		SDL_RenderFillRect(r, &re);

		SDL_SetRenderTarget(r, NULL);//改變渲染目标
		SDL_RenderCopy(r, t, nullptr, nullptr);
		SDL_RenderPresent(r);

	} while (quit);

	SDL_DestroyWindow(w);
	SDL_DestroyTexture(t);
	SDL_Quit();
	return 0;
}
           

        SDL_UpdateTexutre()  更新紋理

        SDL_UpdateYUVTexture() 

SDL音頻API

         SDL_OpenAudio/SDL_CloseAudio

        SDL_PauseAudio

        SDL_MixAudio

#include<SDL/SDL.h>
#include<iostream>
#define BLOCK_SIZE 4096000
static uint8_t* audio_buf = nullptr;
static uint8_t* audio_pos;
int buffer_len;

//callback function for audio devcie
void read_data(void* udata, Uint8* stream, int len) {

	if (buffer_len == 0) {
		return;
	}

	SDL_memset(stream, 0, len);

	len = (len < buffer_len) ? len : buffer_len;
	SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);

	audio_pos += len;
	buffer_len -= len;
}

int main(int argc, char* argv[])
{
	char path[] = "";
	SDL_Init(SDL_INIT_AUDIO);
	

	SDL_AudioSpec spec;
	spec.freq = 44100;
	spec.channels = 2;
	spec.format = AUDIO_S16SYS;
	spec.callback = read_data;      //回調函數
	spec.userdata = nullptr;

	FILE*audio=	fopen(path, "r");

	audio_buf= (Uint8*)malloc(BLOCK_SIZE);


	SDL_OpenAudio(&spec, nullptr);
	SDL_PauseAudio(0);//0:播放,1:暫停

	do
	{
		buffer_len =fread(audio_buf, 1, BLOCK_SIZE, audio);
		audio_pos = audio_buf;
		while (audio_pos < audio_buf + buffer_len)
		{
			SDL_Delay(1);
		}

	} while (buffer_len != 0);




	SDL_CloseAudio();
	free(audio_buf);
	fclose(audio);
	SDL_Quit();
}
           

SDL線程

 SDL_CreateThread()

      fn:線程執行函數

      name:線程名字

        data:執行函數參數

繼續閱讀