Linux下图形学,SDL Linux下的使用 计算机图形学

使用它来画一条直线

Ultility.h

#ifndef ULTILITY_H_INCLUDED #define ULTILITY_H_INCLUDED #ifdef __APPLE__ #include #else #include #endif //init the screen int init(SDL_Surface *&screen,SDL_Surface *&pic,int w,int h); //draw a pixel void DrawPixel(SDL_Surface *screen, int x,int y,Uint8 R, Uint8 G, Uint8 B); //show a pic and free the resource void Load(SDL_Surface *screen,SDL_Surface *pic); #endif // DRAWPIXEL_H_INCLUDED

Example.h

#ifndef EXAMPLE_H_INCLUDED #define EXAMPLE_H_INCLUDED #ifdef __APPLE__ #include #else #include #endif //Line void DDA_Line(int x0,int y0,int x1,int y1,SDL_Surface *pic); void MidPoint_Line(int x0,int y0,int x1,int y1,SDL_Surface *pic); //end Line #endif // EXAMPLE_H_INCLUDED

Ultility.cpp

#include "Ultility.h" //init the screen int init(SDL_Surface *&screen,SDL_Surface *&pic,int w,int h) { // initialize SDL video if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "Unable to init SDL: %s/n", SDL_GetError() ); return 1; } // make sure SDL cleans up before exit atexit(SDL_Quit); // create a new window screen = SDL_SetVideoMode(w, h, 32,SDL_HWSURFACE|SDL_DOUBLEBUF); pic = SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,32, 0, 0,0,0); } //draw a pixel void DrawPixel(SDL_Surface *screen, int x,int y,Uint8 R, Uint8 G, Uint8 B) { Uint32 color = SDL_MapRGB(screen->format, R, G, B); if ( SDL_MUSTLOCK(screen) ) { if ( SDL_LockSurface(screen) < 0 ) { return; } } switch (screen->format->BytesPerPixel) { case 1: { /* 假定是8-bpp */ Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color; } break; case 2: { /* 可能是15-bpp 或者 16-bpp */ Uint16 *bufp; bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; } break; case 3: { /* 慢速的24-bpp模式,通常不用 */ Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *(bufp+screen->format->Rshift/8) = R; *(bufp+screen->format->Gshift/8) = G; *(bufp+screen->format->Bshift/8) = B; } break; case 4: { /* 可能是32-bpp */ Uint32 *bufp; bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; *bufp = color; } break; } if ( SDL_MUSTLOCK(screen) ) { SDL_UnlockSurface(screen); } SDL_UpdateRect(screen, x, y, 1, 1); } void Load(SDL_Surface *screen,SDL_Surface *pic) { // centre the bitmap on screen SDL_Rect dstrect; dstrect.x = (screen->w - pic->w) / 2; dstrect.y = (screen->h - pic->h) / 2; // DRAWING STARTS HERE // clear screen SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); // draw bitmap SDL_BlitSurface(pic, 0, screen, &dstrect); // DRAWING ENDS HERE // finally, update the screen :) SDL_Flip(screen); // free loaded bitmap SDL_FreeSurface(pic); // all is well ;) }

Line.cpp

#include "Ultility.h" //init the screen int init(SDL_Surface *&screen,SDL_Surface *&pic,int w,int h) { // initialize SDL video if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "Unable to init SDL: %s/n", SDL_GetError() ); return 1; } // make sure SDL cleans up before exit atexit(SDL_Quit); // create a new window screen = SDL_SetVideoMode(w, h, 32,SDL_HWSURFACE|SDL_DOUBLEBUF); pic = SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,32, 0, 0,0,0); } //draw a pixel void DrawPixel(SDL_Surface *screen, int x,int y,Uint8 R, Uint8 G, Uint8 B) { Uint32 color = SDL_MapRGB(screen->format, R, G, B); if ( SDL_MUSTLOCK(screen) ) { if ( SDL_LockSurface(screen) < 0 ) { return; } } switch (screen->format->BytesPerPixel) { case 1: { /* 假定是8-bpp */ Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color; } break; case 2: { /* 可能是15-bpp 或者 16-bpp */ Uint16 *bufp; bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; } break; case 3: { /* 慢速的24-bpp模式,通常不用 */ Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *(bufp+screen->format->Rshift/8) = R; *(bufp+screen->format->Gshift/8) = G; *(bufp+screen->format->Bshift/8) = B; } break; case 4: { /* 可能是32-bpp */ Uint32 *bufp; bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; *bufp = color; } break; } if ( SDL_MUSTLOCK(screen) ) { SDL_UnlockSurface(screen); } SDL_UpdateRect(screen, x, y, 1, 1); } void Load(SDL_Surface *screen,SDL_Surface *pic) { // centre the bitmap on screen SDL_Rect dstrect; dstrect.x = (screen->w - pic->w) / 2; dstrect.y = (screen->h - pic->h) / 2; // DRAWING STARTS HERE // clear screen SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); // draw bitmap SDL_BlitSurface(pic, 0, screen, &dstrect); // DRAWING ENDS HERE // finally, update the screen :) SDL_Flip(screen); // free loaded bitmap SDL_FreeSurface(pic); // all is well ;) }

main.cpp

#ifdef __cplusplus #include #else #include #endif #include "Ultility.h" #include "Example.h" int main ( int argc, char** argv ) { // create a new window SDL_Surface* screen; SDL_Surface* pic; init(screen,pic,640,480); MidPoint_Line(0,0,100,100,pic); Load(screen,pic); getchar(); printf("Exited cleanly/n"); return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值