易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
SDL Linux下的使用 计算机图形学
使用它来画一条直线Ultility.h
#ifndef ULTILITY_H_INCLUDED
#define ULTILITY_H_INCLUDED
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#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 <SDL/SDL.h>
#else
#include <SDL.h>
#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 "
, 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 ;)
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图