Perşembe, Eylül 30, 2010

SDL İle Ekrana Nokta(Pixel) Basma

Allegroda bu işi kütüphane ile birlikte gelen "putpixel()" fonksiyon yardımı ile yapabiliyoduk. Ama SDL için aynı durum söz konusu değil. Bizde oyun programlama dersinde internetten hazır olarak bulduğumuz aşağıdaki algoritmayı kullandık. Bu fonksiyon yardımı ile geometrik şekiller(daire,kare,çizgi) çizebilirsiniz.

Söz dizimi:
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)

Parametreler:
surface: Noktanın basılacağı yüzey.(Ör:screen)
x ve y: Noktanın koordinatları.
pixel: Noktanın rengi.(Ör:SDL_MapRGB(screen->format,255,255,255))

Kullanım Şekli:
putpixel(screen,100,200,SDL_MapRGB(screen->format,255,255,255));


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;
}
}

Hiç yorum yok: