[英]SDL2 How can I render pixels from raw format?
我正在尝试渲染自定义图像,它要求我将文件加载到内存中并通过SDL渲染出来。 图片是原始格式,我想是否可以渲染
我的代码可能是垃圾,因此我可以对其进行更改。
void Create_SDL_Window()
{
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, 0);
printf("Window And Renderer Created!\n");
}
int main(){
FILE* customImage = fopen(Path, "rb");
Create_SDL_Window();
while (!quit){
void *p;
p = customImage;
SDL_Texture* buffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGRA8888,SDL_TEXTUREACCESS_STREAMING, 800, 600);
SDL_LockTexture(buffer, NULL, &p, &pitch);
SDL_UnlockTexture(buffer);
SDL_RenderCopy(renderer, buffer, NULL, NULL);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&e)){
//If user closes the window
if (e.type == SDL_QUIT){
quit = true;
}
//If user presses any key
if (e.type == SDL_KEYDOWN){
// quit = true;
}
//If user clicks the mouse
if (e.type == SDL_MOUSEBUTTONDOWN){
/// quit = true;
}
}
SDL_RenderPresent(renderer);
}
你事情倒退了。 您应注意, SDL_LockTexture
采用了指向指针的指针。 这是因为SDL已经具有适合纹理大小的缓冲区,并且它需要告诉您地址(和间距),以便您可以写入此缓冲区。
还存在您认为可以将FILE*
用作像素缓冲区的问题。 这根本不是真的。 FILE*
是指向描述文件而不是其内容的结构的指针。
您需要做的是:
// create your empty texture
...
int pitch = 0;
char* p = NULL;
SDL_LockTexture(buffer, NULL, &p, &pitch);
... // Error checking
// now open your file and mmap it
int fd = open(Path, O_RDONLY);
struct stat sb;
fstat(fd, &sb);
const char* memblock = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
... // Error checking
// now you need to copy the data from the file to the pixel buffer
// you claim you are working with 800x600 32bit image data
for (int y = 0; y < 600; ++y)
{
const char* src = &memblock[y * pitch]; // is this really the pitch of the file? you didn't specify....
char* dst = &p[y * pitch];
memcpy(dst, src, 800*4); // width * size of a pixel
}
此代码假定您没有在其他地方犯错,例如纹理的大小或像素的格式。 您还将注意到您需要找出的代码中的一些未知数。
您也可以尝试SDL_UpdateTexture
,它将像在代码中尝试的那样接受指向像素的指针。 但是,它可能比SDL_LockTexture
慢得多,并且您仍然需要实际读取文件(或者最好是mmap
它)以使像素通过。
如果SDL_Image
知道如何读取“ RAW”文件,那么第三个选择是使用IMG_Load
获取图像的SDL_Surface
,然后使用SDL_CreateTextureFromSurface
从该表面创建纹理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.