|
下面是我的测试程序,当我把象素位由32改为24时!
它显示的是不再是一个矩形,而是一条竖线!
不知道是什么原因?
谢谢指导!
( draw_pixel() 是我从www.libsdl.org上取下来的程序,只做了一点点修改)
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<sys/types.h>
#include"SDL/SDL.h"
int err_exit(const char* str);
void draw_pixel(SDL_Surface *screen,int x,int y, Uint8 R, Uint8 G, Uint8 B);
int main(int argc,char* argv[]){
int i,j;
SDL_Surface* screen;
SDL_Rect src,dest;
if(SDL_Init(SDL_INIT_VIDEO)==-1) //initializes the SDL
err_exit(SDL_GetError());
atexit(SDL_Quit);
screen=SDL_SetVideoMode(800,600,32,0);
if(screen==NULL)
err_exit(SDL_GetError());
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
err_exit(SDL_GetError());
}
}
for(i=0;i<50;i++){
for(j=0;j<50;j++){
draw_pixel(screen,j+50,i+50,0,255,0);
}
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen,50,50,100,100);
SDL_Delay(5000);
return 0;
}
void draw_pixel(SDL_Surface *screen,int x,int y, Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel) {
case 1: {
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: {
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: {
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*(bufp+screen->format->Rshift/ = R;
*(bufp+screen->format->Gshift/ = G;
*(bufp+screen->format->Bshift/ = B;
}
break;
case 4: {
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
}
int err_exit(const char* str){
fprintf(stderr,"%s",str);
if(errno!=0)
fprintf(stderr,":%d",errno);
exit(1);
} |
|