您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > SDL讲解,图形基本概念非常到位
SDL–2Video静宜大学信息工程系蔡奇伟副教授©2007纲要绘图系统设定显示模式绘图页BLITColorkeyAlpha值Clipping显示到荧幕SDL_image函式库图形显示framebuffer像素(Pixel,PictureElement)Pixel&Resolution荧幕上的一个光点称为一个像素(pictureelement,pixel)。分辨率(resolution)是荧幕像素的个数,通常用水平像素个数垂直像素个数的格式来表示,譬如:640480代表荧幕水平方向有640个像素以及垂直方向有480个像素。注:数位图片的最小组成单位也称为像素。图片的大小也常用上述的乘积方式来表示,譬如:一张640480的图片,水平方向有640个像素以及垂直方向有480个像素。CRTRetrace(回归讯号)DirectColor(直接色)FrameBufferRGB注:通常每个像素有16bitsor24bits。IndirectColor(间接色)FrameBuffer注:通常每个像素有8bits。ScreenMode设定显示模式SDL_Surface*SDL_SetVideoMode(intwidth,//宽度intheight,//高度intbpp,//bitsperpixel,像素位元数Uint32flags//旗标);SDL_SWSURFACE在系统存储器中建立视讯绘图页SDL_HWSURFACE在视讯存储器中建立视讯绘图页SDL_ASYNCBLITEnablestheuseofasynchronousupdatesofthedisplaysurface.ThiswillusuallyslowdownblittingonsingleCPUmachines,butmayprovideaspeedincreaseonSMPsystems.SDL_ANYFORMAT*使用荧幕目前的bpp。注:有星号的旗标只能用于荧幕设定。SDL_HWPALETTEGiveSDLexclusivepaletteaccess.WithoutthisflagyoumaynotalwaysgetthethecolorsyourequestwithSDL_SetColorsorSDL_SetPalette.SDL_DOUBLEBUF*Enablehardwaredoublebuffering;onlyvalidwithSDL_HWSURFACE.CallingSDL_Flipwillflipthebuffersandupdatethescreen.Alldrawingwilltakeplaceonthesurfacethatisnotdisplayedatthemoment.IfdoublebufferingcouldnotbeenabledthenSDL_FlipwilljustperformaSDL_UpdateRectontheentirescreen.SDL_FULLSCREEN*SDLwillattempttouseafullscreenmode.Ifahardwareresolutionchangeisnotpossible(forwhateverreason),thenexthigherresolutionwillbeusedandthedisplaywindowcenteredonablackbackground.SDL_OPENGL*CreateanOpenGLrenderingcontext.YoushouldhavepreviouslysetOpenGLvideoattributeswithSDL_GL_SetAttribute.SDL_OPENGLBLIT*CreateanOpenGLrenderingcontext,likeabove,butallownormalblittingoperations.Thescreen(2D)surfacemayhaveanalphachannel,andSDL_UpdateRectsmustbeusedforupdatingchangestothescreensurface.SDL_RESIZABLE*Createaresizablewindow.WhenthewindowisresizedbytheuseraSDL_VIDEORESIZEeventisgeneratedandSDL_SetVideoModecanbecalledagainwiththenewsize.SDL_NOFRAME*Ifpossible,SDL_NOFRAMEcausesSDLtocreateawindowwithnotitlebarorframedecoration.Fullscreenmodesautomaticallyhavethisflagset.范例SDL_Surface*screen;//640480,8bpp,存于系统存储器screen=SDL_SetVideoMode(640,480,8,SDL_SWSURFACE);//640480,32bpp,存于系统存储器screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);//1024768,与幕萤目前的bpp相同,存于系统存储器screen=SDL_SetVideoMode(1024,768,0,SDL_SWSURFACE|SDL_ANYFORMAT);//800600,32bpp,存于视讯存储器、用双缓冲区、全荧幕模式screen=SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREN);Surface(绘图页)何谓绘图页SDL_Surface结构SDL_PixelFormat结构SDL_GetRGB()与SDL_MapRGB()Pitch直接存取绘图页的像素资料建立绘图页释放绘图页转换绘图页的像素格式何谓绘图页?绘图页(Surface)是一个抽象结构,用来描述一块储存图片的存储器区域,其中包含图片的宽度、高度、像素格式、储存位址、……、等等相关信息。SDL把framebuffer视为一种特殊的绘图页,称之为荧幕绘图页(DisplaySurface)。其他的绘图页称为幕后绘图页(Off-screenSurface)。某些绘图页的旗标仅能用于设定荧幕绘图页。SDL用SDL_Surface结构来定义绘图页。SDL_Surface结构typedefstructSDL_Surface{Uint32flags;/*旗标*/SDL_PixelFormat*format;/*像素格式*/intw,h;/*图片宽度与高度(pixels)*/Uint16pitch;/*绘图页实际宽度(bytes)*/void*pixels;/*像素资料的储存位址*//*clippinginformation*/SDL_Rectclip_rect;/*剪裁矩形*//*Referencecount--usedwhenfreeingsurface*/intrefcount;/*参照此绘图页的指标个数*//*Thisstructurealsocontainsprivatefieldsnotshownhere*/}SDL_Surface;SDL_PixelFormat结构typedefstruct{SDL_Palette*palette;Uint8BitsPerPixel;Uint8BytesPerPixel;Uint32Rmask,Gmask,Bmask,Amask;Uint8Rshift,Gshift,Bshift,Ashift;Uint8Rloss,Gloss,Bloss,Aloss;Uint32colorkey;Uint8alpha;}SDL_PixelFormat;范例32bpp:A8R8G8B8ARGB08162431成分maskshiftlossAlpha0xFF000000240Red0x00FF0000160Green0x0000FF0080Blue0x000000FF00范例16bpp:R5G6B5ARGB051115成分maskshiftlossRed0xF800=11111000000000002113Green0x07E0=0000011111100000252Blue0x001F=0000000000011111203范例//底下的函式用像素格式中的mask,shift,和loss的值,//取出对应的Red,Green,或Blue成份值。Uint32Get_PrimaryColor(Uint32,pixel,Uint32mask,Uint8shift,Uintloss){Uint32pc=((pixel&mask)shift))loss;returnpc;}假定变量screen是代表荧幕的绘图页,变量pv已经存入其中的某一像素值,则下列的函式呼叫可计算出此像素的三原色成分值:SDL_PixelFormat*fmt=screen-format;Uint32red,green,blue;red=Get_PrimaryColor(pv,fmt-Rmask,fmt-Rshift,fmt-Rloss);green=Get_PrimaryColor(pv,fmt-Gmask,fmt-Gshift,fmt-Gloss);blue=Get_PrimaryColor(pv,fmt-Bmask,fmt-Bshift,fmt-Bloss);SDL_GetRGB()voidSDL_GetRGB(Uint32pixel,SDL_PixelFormat*fmt,Uint8*r,Uint8*g,Uint8*b);依据像素格式fmt,取出像素pixel中三原色的值。SDL_MapRGB()Uint32SDL_MapRGB(SDL_PixelFormat*fmt,Uint8r,Uint8g,Uint8b);依据像素格式fmt,传回三原色合成的像素值。Pitch我们可以用pitch的值来求取下一列像素的起始位址。譬如:假定指标start指到第一个像素,则第k列像素的起始位址是start+k*pitchk=0,1,…,h-1。pitch可能不等于wBytesPerPixel。hw(pixels)pitch(bytes)gappixel/**Returnthepixelvalueat(x,y)*NOTE:Thesurfacemustbelockedbeforecallingthis!*/Uint32getpixel(SDL_Surface*surface,intx,inty){intbpp=surface-format-BytesPerPixel;/*Herepistheaddresstothepixelwewanttoretrieve*/Uint8*p=(Uint8*)surface-pixels+y*surface-pitch+x*bpp;switch(bpp){case1:return*p;case2:return*(Uint16*)p;case3:if(SDL_BYTEORDER==SDL_BIG_ENDIAN)returnp[0]16|p[1]8|p[2];elsereturnp[0]|p[1]8|p[2]16;case4:return*(Uint32*)p;default:return0;/*shouldn'thappen,butavoidswarnings*/}}/**Setthepixelat(x,y)tothegivenvalue*NOTE:Thesurfacemustbelockedbeforecallingthis!*/voidputpixel(SDL_Surface*surface,intx,inty,Uint32pixel){i
本文标题:SDL讲解,图形基本概念非常到位
链接地址:https://www.777doc.com/doc-3519091 .html