• SDL 使用


                                                                                          http://www.libsdl.org/

                     

    // 代码是雷神写的 我只是拿过来使用而已 
    
    #include "stdafx.h"
    #include <string.h>
    #include <iostream>
    
    extern "C"
    {
     #include "SDL.h"
    };
    
    
    #undef main   // 注意这个 不然SDL库报一个错误
    
    const int bpp = 12;
    int screen_w = 500, screen_h = 500;
    const int pixel_w = 320, pixel_h = 180;
    
    unsigned char buffer[pixel_w*pixel_h*bpp / 8];
    
    //Refresh Event
    #define REFRESH_EVENT  (SDL_USEREVENT + 1)
    //Break
    #define BREAK_EVENT  (SDL_USEREVENT + 2)
    
    int thread_exit = 0;
    
    //每隔40毫秒向SDL系统发送一个REFRESH_EVENT事件
    int refresh_video(void *opaque) {
        thread_exit = 0;
        while (thread_exit == 0) {
            SDL_Event event;
            event.type = REFRESH_EVENT;
            SDL_PushEvent(&event);
            SDL_Delay(40);
        }
        thread_exit = 0;
        //Break
        SDL_Event event;
        event.type = BREAK_EVENT;
        SDL_PushEvent(&event);
        return 0;
    }
    
    int main(int argc, char* argv[])
    {
        if (SDL_Init(SDL_INIT_VIDEO)) {
            printf("Could not initialize SDL - %s
    ", SDL_GetError());
            return -1;
        }
    
        SDL_Window *screen;
        //SDL 2.0 Support for multiple windows
        screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
            screen_w, screen_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
        if (!screen) {
            printf("SDL: could not create window - exiting:%s
    ", SDL_GetError());
            return -1;
        }
        SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
    
        Uint32 pixformat = 0;
        //IYUV: Y + U + V  (3 planes)
        //YV12: Y + V + U  (3 planes)
        pixformat = SDL_PIXELFORMAT_IYUV;
    
        SDL_Texture* sdlTexture = SDL_CreateTexture(sdlRenderer, pixformat, SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h);
    
        FILE *fp = NULL;
        fp = fopen("D:\pthread\Pre-built.2\111.yuv", "rb+");
    
        if (fp == NULL) {
            while(1)
            { 
                printf("cannot open this file
    ");
            }
            
            return -1;
        }
    
        SDL_Rect sdlRect;
    
        SDL_Thread *refresh_thread = SDL_CreateThread(refresh_video, NULL, NULL);
        SDL_Event event;
        while (1) {
            //收取SDL系统里面的事件
            SDL_WaitEvent(&event);
            if (event.type == REFRESH_EVENT) {
                if (fread(buffer, 1, pixel_w*pixel_h*bpp / 8, fp) != pixel_w*pixel_h*bpp / 8) {
                    // Loop
                    fseek(fp, 0, SEEK_SET);
                    fread(buffer, 1, pixel_w*pixel_h*bpp / 8, fp);
                }
    
                SDL_UpdateTexture(sdlTexture, NULL, buffer, pixel_w);
    
                //FIX: If window is resize
                sdlRect.x = 0;
                sdlRect.y = 0;
                sdlRect.w = screen_w;
                sdlRect.h = screen_h;
    
                SDL_RenderClear(sdlRenderer);
                SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
                SDL_RenderPresent(sdlRenderer);
    
            }
            else if (event.type == SDL_WINDOWEVENT) {
                //If Resize
                SDL_GetWindowSize(screen, &screen_w, &screen_h);
            }
            else if (event.type == SDL_QUIT) {
                thread_exit = 1;
            }
            else if (event.type == BREAK_EVENT) {
                break;
            }
        }
        SDL_Quit();
        return 0;
    }

      

    一勤天下无难事。
  • 相关阅读:
    【Python进阶】用 Python 统计字数
    【Python进阶】无论API怎么变,SDK都可以根据URL实现完全动态的调用
    【机器学习_吴恩达_笔记】(一)机器学习的动机和应用
    【Python入门总结】
    【Python入门学习】列表生成和函数生成器的方式实现杨辉三角
    【Python入门学习】闭包&装饰器&开放封闭原则
    【Pthon入门学习】利用slice实现str的strip函数,类似C#中的string.trim
    【Pthon入门学习】99乘法表
    【Pthon入门学习】多级菜单小例子
    要素图层范围查询属性arcgis api for js(featuretable根据上篇的优化)原创
  • 原文地址:https://www.cnblogs.com/nowroot/p/12687385.html
Copyright © 2020-2023  润新知