栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在C / Linux中显示连续更新图像的简便方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在C / Linux中显示连续更新图像的简便方法

如果您只想显示数据(即不需要GUI),则可能需要看一下SDL:直接从像素数据创建表面,然后将其显示在屏幕上很简单。

受Artelius的回答启发,我还破解了一个示例程序:

#include <SDL/SDL.h>#include <assert.h>#include <stdint.h>#include <stdlib.h>#define WIDTH 256#define HEIGHT 256static _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;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412438.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号