Tôi có các chức năng sau nằm xung quanh để đặt pixel trong SDL_Surface. Có hai phiên bản cho các bề mặt 32 bit, 24 bit, 16 bit và 8 bit. Nếu bạn chỉ muốn đặt một pixel, bạn sẽ sử dụng các phiên bản thông thường. Nhưng nếu bạn muốn thiết lập một loạt các điểm ảnh, trước tiên bạn khóa bề mặt, sau đó bạn sử dụng phiên bản nolock (có tên như vậy bởi vì nó không khóa bề mặt), sau đó bạn mở khóa. Bằng cách này bạn không liên tục khóa và mở khóa bề mặt, mà được coi là một hoạt động đắt tiền, mặc dù tôi không nghĩ rằng tôi đã bao giờ thực sự thử nghiệm nó.
void PutPixel32_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
Uint8 * pixel = (Uint8*)surface->pixels;
pixel += (y * surface->pitch) + (x * sizeof(Uint32));
*((Uint32*)pixel) = color;
}
void PutPixel24_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
Uint8 * pixel = (Uint8*)surface->pixels;
pixel += (y * surface->pitch) + (x * sizeof(Uint8) * 3);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
pixel[0] = (color >> 24) & 0xFF;
pixel[1] = (color >> 16) & 0xFF;
pixel[2] = (color >> 8) & 0xFF;
#else
pixel[0] = color & 0xFF;
pixel[1] = (color >> 8) & 0xFF;
pixel[2] = (color >> 16) & 0xFF;
#endif
}
void PutPixel16_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
Uint8 * pixel = (Uint8*)surface->pixels;
pixel += (y * surface->pitch) + (x * sizeof(Uint16));
*((Uint16*)pixel) = color & 0xFFFF;
}
void PutPixel8_nolock(SDL_Surface * surface, int x, int y, Uint32 color)
{
Uint8 * pixel = (Uint8*)surface->pixels;
pixel += (y * surface->pitch) + (x * sizeof(Uint8));
*pixel = color & 0xFF;
}
void PutPixel32(SDL_Surface * surface, int x, int y, Uint32 color)
{
if(SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
PutPixel32_nolock(surface, x, y, color);
if(SDL_MUSTLOCK(surface))
SDL_UnlockSurface(surface);
}
void PutPixel24(SDL_Surface * surface, int x, int y, Uint32 color)
{
if(SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
PutPixel24_nolock(surface, x, y, color);
if(SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
}
void PutPixel16(SDL_Surface * surface, int x, int y, Uint32 color)
{
if(SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
PutPixel16_nolock(surface, x, y, color);
if(SDL_MUSTLOCK(surface))
SDL_UnlockSurface(surface);
}
void PutPixel8(SDL_Surface * surface, int x, int y, Uint32 color)
{
if(SDL_MUSTLOCK(surface))
SDL_LockSurface(surface);
PutPixel8_nolock(surface, x, y, color);
if(SDL_MUSTLOCK(surface))
SDL_UnlockSurface(surface);
}
im không thay đổi bất kỳ điểm ảnh đặc biệt ở đây. Tôi chỉ đơn giản là sao chép tất cả mọi thứ ra và cố gắng để sao chép tất cả trở lại. Và theo định dạng, bạn có ý nghĩa gì? – nory
Bạn nhận ra mã của mình không có gì? 'screen-> pixel = screen-> pixel;'. –
tất nhiên.im chỉ rối tung xung quanh, làm quen với cú pháp và các công cụ – nory