gpt4 book ai didi

c - 在 C/Linux 中显示不断更新的图像的简便方法

转载 作者:IT王子 更新时间:2023-10-29 00:15:30 25 4
gpt4 key购买 nike

我是一名科学家,非常熟悉 C 语言的数值计算,但我需要一些帮助来显示结果。我希望能够在一个窗口中显示一个不断更新的位图,它是根据实时数据计算出来的。我希望能够非常快速地更新图像(例如,快于 1 帧/秒,最好是 100 fps)。例如:

char image_buffer[width*height*3];//rgb data
initializewindow();

for (t=0;t<t_end;t++)
{
getdata(data);//get some realtime data
docalcs(image_buffer, data);//process the data into an image
drawimage(image_buffer);//draw the image
}

在 Linux (Ubuntu) 上执行此操作的最简单方法是什么?我应该为 initializewindow() 和 drawimage() 使用什么?

最佳答案

如果您只想显示数据(即不需要 GUI),您可能想看看 SDL : 直接转到create a surface from your pixel data然后 display it on screen .

灵感来自 Artelius' answer ,我还破解了一个示例程序:

#include <SDL/SDL.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>

#define WIDTH 256
#define HEIGHT 256

static _Bool init_app(const char * name, SDL_Surface * icon, uint32_t flags)
{
atexit(SDL_Quit);
if(SDL_Init(flags) < 0)
return 0;

SDL_WM_SetCaption(name, name);
SDL_WM_SetIcon(icon, NULL);

return 1;
}

static uint8_t * init_data(uint8_t * data)
{
for(size_t i = WIDTH * HEIGHT * 3; i--; )
data[i] = (i % 3 == 0) ? (i / 3) % WIDTH :
(i % 3 == 1) ? (i / 3) / WIDTH : 0;

return data;
}

static _Bool process(uint8_t * data)
{
for(SDL_Event event; SDL_PollEvent(&event);)
if(event.type == SDL_QUIT) return 0;

for(size_t i = 0; i < WIDTH * HEIGHT * 3; i += 1 + rand() % 3)
data[i] -= rand() % 8;

return 1;
}

static void render(SDL_Surface * sf)
{
SDL_Surface * screen = SDL_GetVideoSurface();
if(SDL_BlitSurface(sf, NULL, screen, NULL) == 0)
SDL_UpdateRect(screen, 0, 0, 0, 0);
}

static int filter(const SDL_Event * event)
{ return event->type == SDL_QUIT; }

#define mask32(BYTE) (*(uint32_t *)(uint8_t [4]){ [BYTE] = 0xff })

int main(int argc, char * argv[])
{
(void)argc, (void)argv;
static uint8_t buffer[WIDTH * HEIGHT * 3];

_Bool ok =
init_app("SDL example", NULL, SDL_INIT_VIDEO) &&
SDL_SetVideoMode(WIDTH, HEIGHT, 24, SDL_HWSURFACE);

assert(ok);

SDL_Surface * data_sf = SDL_CreateRGBSurfaceFrom(
init_data(buffer), WIDTH, HEIGHT, 24, WIDTH * 3,
mask32(0), mask32(1), mask32(2), 0);

SDL_SetEventFilter(filter);

for(; process(buffer); SDL_Delay(10))
render(data_sf);

return 0;
}

关于c - 在 C/Linux 中显示不断更新的图像的简便方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1391314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com