青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

SDL入門(轉,方便自己)

Posted on 2010-06-11 17:00 莫失莫忘 閱讀(563) 評論(0)  編輯 收藏 引用

第一章 基礎

初始化

SDL由八個子系統組成——音頻、CDROM、事件處理、文件I/O、游戲桿、線程、記時器和視頻。使用前必須調用SDL_Init或 SDL_InitSubSystem初始化。SDL_Init必須早于其他所有SDL調用,它將自動初始化事件處理、文件I/O和線程子系統,并根據參數 選擇啟動其他子系統。例如,初始化缺省和視頻子系統:

SDL_Init(SDL_INIT_VIDEO);初始化缺省、視頻和記時器子系統:

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);SDL_Init對應SDL_Quit(和SDL_QuitSubSystem)。SDL_Quit關閉所有子系統,必須在程序關閉前調用。

除此之外,我們還必須進行錯誤處理。很多SDL函數返回一個值指示成功與否。例如SDL_Init失敗時返回-1。每當SDL出錯時,錯誤信息被保存,并可用SDL_GetError取得。

例1-1 初始化SDL

#include "SDL.h" /* All SDL App's need this */

#include <stdio.h>

int main() {

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */

if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {

printf("Could not initialize SDL: %s.\n", SDL_GetError());

exit(-1);

}

printf("SDL initialized.\n");

printf("Quiting SDL.\n");

/* Shutdown all subsystems */

SDL_Quit();

printf("Quiting....\n");

exit(0);

}

第二章 圖像和視頻

SDL Vidow顯示

初始化SDL Video顯示

視頻是最常用的部分,也是SDL最完整的子系統。下面的初始化過程是每個SDL程序都要做的,即使可能有些不同。

例2-1 初始化視頻

SDL_Surface *screen;

/* Initialize the SDL library */

if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {

fprintf(stderr,

"Couldn't initialize SDL: %s\n", SDL_GetError());

exit(1);

}

/* Clean up on exit */

atexit(SDL_Quit);

/*

* Initialize the display in a 640x480 8-bit palettized mode,

* requesting a software surface

*/

screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);

if ( screen == NULL ) {

fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",

SDL_GetError());

exit(1);

}

初始化最佳視頻模式

如果你希望某種色深(顏色數)但如果用戶的顯示器不支持也可以接受其他色深,使用加SDL_ANYFORMAT參數的SDL_SetVideoMode。您還可以用SDL_VideoModeOK來找到與請求模式最接近的模式。

例2-2 初始化最佳視頻模式

/* Have a preference for 8-bit, but accept any depth */

screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_ANYFORMAT);

if ( screen == NULL ) {

fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",

SDL_GetError());

exit(1);

}

printf("Set 640x480 at %d bits-per-pixel mode\n",

screen->format->BitsPerPixel);

讀取并顯示BMP文件

當SDL已經初始化,視頻模式已經選擇,下面的函數將讀取并顯示指定的BMP文件。

例2-3 讀取并顯示BMP文件

void display_bmp(char *file_name)

{

SDL_Surface *image;

/* Load the BMP file into a surface */

image = SDL_LoadBMP(file_name);

if (image == NULL) {

fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError());

return;

}

/*

* Palettized screen modes will have a default palette (a standard

* 8*8*4 colour cube), but if the image is palettized as well we can

* use that palette for a nicer colour matching

*/

if (image->format->palette && screen->format->palette) {

SDL_SetColors(screen, image->format->palette->colors, 0,

image->format->palette->ncolors);

}

/* Blit onto the screen surface */

if(SDL_BlitSurface(image, NULL, screen, NULL) < 0)

fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

SDL_UpdateRect(screen, 0, 0, image->w, image->h);

/* Free the allocated BMP surface */

SDL_FreeSurface(image);

}

直接在顯示上繪圖

下面兩個函數實現在圖像平面的像素讀寫。它們被細心設計成可以用于所有色深。記住在使用前要先鎖定圖像平面,之后要解鎖。

在像素值和其紅、綠、藍值間轉換,使用SDL_GetRGB()和SDL_MapRGB()。

例2-4 getpixel()

/*

* Return the pixel value at (x, y)

* NOTE: The surface must be locked before calling this!

*/

Uint32 getpixel(SDL_Surface *surface, int x, int y)

{

int bpp = surface->format->BytesPerPixel;

/* Here p is the address to the pixel we want to retrieve */

Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {

case 1:

return *p;

case 2:

return *(Uint16 *)p;

case 3:

if(SDL_BYTEORDER == SDL_BIG_ENDIAN)

return p[0] << 16 | p[1] << 8 | p[2];

else

return p[0] | p[1] << 8 | p[2] << 16;

case 4:

return *(Uint32 *)p;

default:

return 0; /* shouldn't happen, but avoids warnings */

}

}

例2-5 putpixel()

/*

* Set the pixel at (x, y) to the given value

* NOTE: The surface must be locked before calling this!

*/

void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)

{

int bpp = surface->format->BytesPerPixel;

/* Here p is the address to the pixel we want to set */

Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {

case 1:

*p = pixel;

break;

case 2:

*(Uint16 *)p = pixel;

break;

case 3:

if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {

p[0] = (pixel >> 16) & 0xff;

p[1] = (pixel >> 8) & 0xff;

p[2] = pixel & 0xff;

} else {

p[0] = pixel & 0xff;

p[1] = (pixel >> 8) & 0xff;

p[2] = (pixel >> 16) & 0xff;

}

break;

case 4:

*(Uint32 *)p = pixel;

break;

}

}

例2-6 使用上面的putpixel()在屏幕中心畫一個黃點

/* Code to set a yellow pixel at the center of the screen */

int x, y;

Uint32 yellow;

/* Map the color yellow to this display (R=0xff, G=0xFF, B=0x00)

Note: If the display is palettized, you must set the palette first.

*/

yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);

x = screen->w / 2;

y = screen->h / 2;

/* Lock the screen for direct access to the pixels */

if ( SDL_MUSTLOCK(screen) ) {

if ( SDL_LockSurface(screen) < 0 ) {

fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());

return;

}

}

putpixel(screen, x, y, yellow);

if ( SDL_MUSTLOCK(screen) ) {

SDL_UnlockSurface(screen);

}

/* Update just the part of the display that we've changed */

SDL_UpdateRect(screen, x, y, 1, 1);

return;


并用SDL和OpenGL

SDL可以在多種平臺(Linux/X11, Win32, BeOS, MacOS Classic/Toolbox, MacOS X, FreeBSD/X11 and Solaris/X11)上創建和使用OpenGL上下文。這允許你在OpenGL程序中使用SDL的音頻、事件、線程和記時器,而這些通常是GLUT的任務。

初始化

和普通的初始化類似,但有三點不同:必須傳SDL_OPENGL參數給SDL_SetVideoMode;必須使用SDL_GL_SetAttribute指定一些GL屬性(深度緩沖區位寬,幀緩沖位寬等);如果您想使用雙緩沖,必須作為GL屬性指定

例2-7 初始化SDL加OpenGL

/* Information about the current video settings. */

const SDL_VideoInfo* info = NULL;

/* Dimensions of our window. */

int width = 0;

int height = 0;

/* Color depth in bits of our window. */

int bpp = 0;

/* Flags we will pass into SDL_SetVideoMode. */

int flags = 0;

/* First, initialize SDL's video subsystem. */

if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {

/* Failed, exit. */

fprintf( stderr, "Video initialization failed: %s\n",

SDL_GetError( ) );

quit_tutorial( 1 );

}

/* Let's get some video information. */

info = SDL_GetVideoInfo( );

if( !info ) {

/* This should probably never happen. */

fprintf( stderr, "Video query failed: %s\n",

SDL_GetError( ) );

quit_tutorial( 1 );

}

/*

* Set our width/height to 640/480 (you would

* of course let the user decide this in a normal

* app). We get the bpp we will request from

* the display. On X11, VidMode can't change

* resolution, so this is probably being overly

* safe. Under Win32, ChangeDisplaySettings

* can change the bpp.

*/

width = 640;

height = 480;

bpp = info->vfmt->BitsPerPixel;

/*

* Now, we want to setup our requested

* window attributes for our OpenGL window.

* We want *at least* 5 bits of red, green

* and blue. We also want at least a 16-bit

* depth buffer.

*

* The last thing we do is request a double

* buffered window. '1' turns on double

* buffering, '0' turns it off.

*

* Note that we do not use SDL_DOUBLEBUF in

* the flags to SDL_SetVideoMode. That does

* not affect the GL attribute state, only

* the standard 2D blitting setup.

*/

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );

SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );

SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );

SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

/*

* We want to request that SDL provide us

* with an OpenGL window, in a fullscreen

* video mode.

*

* EXERCISE:

* Make starting windowed an option, and

* handle the resize events properly with

* glViewport.

*/

flags = SDL_OPENGL | SDL_FULLSCREEN;

/*

* Set the video mode

*/

if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {

/*

* This could happen for a variety of reasons,

* including DISPLAY not being set, the specified

* resolution not being available, etc.

*/

fprintf( stderr, "Video mode set failed: %s\n",

SDL_GetError( ) );

quit_tutorial( 1 );

}

posts - 15, comments - 0, trackbacks - 0, articles - 0

Copyright © 莫失莫忘

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产伦精品一区二区| 久久超碰97人人做人人爱| 性一交一乱一区二区洋洋av| 亚洲一卡久久| 亚洲欧美影音先锋| 欧美一区二区三区视频| 久久精品国产一区二区电影| 久久久久网址| 亚洲国产欧美一区二区三区同亚洲| 亚洲第一二三四五区| 91久久视频| 亚洲一区日本| 久久精品网址| 欧美国内亚洲| 国产精品欧美风情| 一区国产精品| 亚洲——在线| 免费观看在线综合| 99av国产精品欲麻豆| 欧美影院午夜播放| 欧美欧美全黄| 国产综合18久久久久久| 日韩视频在线观看免费| 欧美一级淫片aaaaaaa视频| 欧美成人免费全部| 亚洲伊人伊色伊影伊综合网| 欧美电影资源| 国产亚洲一区在线| 一区二区三区欧美视频| 免费观看在线综合色| 在线一区二区三区做爰视频网站 | 亚洲一级网站| 裸体丰满少妇做受久久99精品| 亚洲精品乱码久久久久久按摩观| 性欧美videos另类喷潮| 欧美人与性动交a欧美精品| 国产偷久久久精品专区| 一本久久知道综合久久| 欧美96在线丨欧| 国产精品日韩欧美| 亚洲人成网站999久久久综合| 欧美在线免费看| 日韩视频三区| 欧美精品导航| 亚洲欧洲综合另类| 欧美大片91| 欧美在线综合视频| 国产欧美日韩91| 先锋影音国产一区| 正在播放亚洲一区| 亚洲激情视频| 久久视频在线免费观看| 国产午夜精品在线观看| 亚洲欧美一区二区原创| 亚洲最快最全在线视频| 欧美伦理91i| 一本色道久久综合亚洲二区三区| 欧美激情视频网站| 久久综合福利| 亚洲国内精品在线| 欧美电影免费网站| 免费高清在线视频一区·| 国产真实久久| 蜜桃伊人久久| 欧美高清不卡| 一区二区日韩欧美| 99国产精品久久久| 欧美日韩在线影院| 亚洲欧美区自拍先锋| 中国成人在线视频| 国产日本欧美一区二区| 久久久久国产精品午夜一区| 欧美一区二视频在线免费观看| 国产乱子伦一区二区三区国色天香| 亚洲欧美日韩另类| 亚洲主播在线观看| 一区二区三区在线高清| 欧美黄色一区二区| 欧美日韩a区| 欧美在线观看视频在线| 久久九九全国免费精品观看| 亚洲国产aⅴ天堂久久| 亚洲精品精选| 国产欧美一区在线| 美女精品自拍一二三四| 欧美高清视频一区| 亚洲曰本av电影| 久久国产精品久久国产精品| 亚洲日本中文| 亚洲女人小视频在线观看| 精品动漫3d一区二区三区免费版| 亚洲国产成人av| 国产精品久久久久9999高清| 久久久国际精品| 欧美大色视频| 欧美在线视频一区二区三区| 乱中年女人伦av一区二区| 亚洲色诱最新| 久久久久国产一区二区三区四区| 亚洲毛片在线观看| 香蕉久久一区二区不卡无毒影院 | 夜夜嗨一区二区三区| 亚洲免费视频网站| 亚洲国产成人精品久久| 亚洲自拍都市欧美小说| 亚洲精品免费在线| 欧美在线观看视频在线| 亚洲免费成人| 欧美xart系列高清| 国产精品分类| 亚洲国产天堂久久国产91| 国产精品视频午夜| 亚洲日本视频| 亚洲高清在线视频| 欧美中文字幕第一页| 中文在线不卡视频| 欧美/亚洲一区| 卡通动漫国产精品| 国产亚洲成av人在线观看导航| 日韩小视频在线观看| 亚洲激情在线激情| 久久成人精品一区二区三区| 午夜激情综合网| 欧美日韩国产一区二区| 欧美激情黄色片| 亚洲国产91| 老司机免费视频一区二区三区| 久久久久久久久一区二区| 国产精品日本精品| 亚洲少妇自拍| 亚洲欧美大片| 国产精品高潮呻吟久久| 99精品国产99久久久久久福利| 亚洲人成人一区二区三区| 麻豆亚洲精品| 亚洲黄色免费| 日韩午夜在线播放| 欧美精品久久久久a| 亚洲国产专区校园欧美| 亚洲人午夜精品| 欧美99久久| 亚洲精品久久久久久久久久久| 日韩视频一区二区| 欧美了一区在线观看| 亚洲免费播放| 亚洲视频精选| 国产精品影音先锋| 欧美一区二区三区视频在线观看| 欧美中文在线视频| 黄色一区二区在线观看| 久久精品国产精品亚洲精品| 久久亚洲私人国产精品va媚药| 国产综合久久久久影院| 久久久久久穴| 日韩特黄影片| 欧美在线综合| 最新高清无码专区| 欧美日韩国产精品自在自线| 99国产精品久久久久久久成人热| 亚洲欧美日韩国产精品| 国产日韩欧美91| 久久这里只有| 9l国产精品久久久久麻豆| 欧美伊人久久大香线蕉综合69| 狠狠爱www人成狠狠爱综合网| 亚洲性感美女99在线| 欧美性大战xxxxx久久久| 亚洲欧美日韩爽爽影院| 男女激情视频一区| 亚洲一区美女视频在线观看免费| 国产日韩亚洲| 欧美激情一区二区| 亚洲欧美国产77777| 亚洲高清在线视频| 午夜精品久久99蜜桃的功能介绍| 激情成人综合网| 欧美亚一区二区| 麻豆freexxxx性91精品| 亚洲丝袜av一区| 亚洲巨乳在线| 国产拍揄自揄精品视频麻豆| 老司机精品久久| 亚洲欧美国产另类| 亚洲大胆视频| 久久久99久久精品女同性| 99在线|亚洲一区二区| 国产一区二区| 国产精品黄视频| 欧美激情精品久久久久久| 欧美一二三视频| 日韩一区二区精品葵司在线| 欧美电影免费观看| 久久久久久久欧美精品| 亚洲免费婷婷| 一区二区欧美日韩视频| 亚洲国产成人tv| 精品不卡一区| 含羞草久久爱69一区| 国产精品影片在线观看| 国产精品久久久久久亚洲毛片|