天下之事,必先处之难,而后易之。

SDL2画一个简单的矩形

SDL2画矩形很简单,不需要SDL_Draw来辅助。下面的函数定义了一个画矩形的方法。

        SDL.SDL_Rect rectangle= new SDL.SDL_Rect();;

        /// <summary>
        /// SDL画矩形长方形
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="color">0:红,1:绿,2:蓝 (默认值为1:绿色)</param>
        public void DrawRect(int width, int height,int color=1)
        {
            if (color == 0)
            {
                SDL.SDL_SetRenderDrawColor(sdlrenderer, 255, 0, 0,1);// 红
            }
            else if (color == 1)
            {
                SDL.SDL_SetRenderDrawColor(sdlrenderer, 0, 255, 0, 1);// 绿
            }
            else if (color == 2)
            {
                SDL.SDL_SetRenderDrawColor(sdlrenderer, 0, 0, 255, 1);// 蓝
            }
            rectangle.x = 0;
            rectangle.y = 0;
            rectangle.w = width;
            rectangle.h = height;
            SDL.SDL_RenderFillRect(sdlrenderer, ref rectangle);
        }

注意:SDL是一层一层往上添加的,跟地图的覆盖物是同一个道理。例如下面的代码,标识了绘制图像的位置,防止影响其他操作。

 // 画矩形边框(位置一定要靠前不然会遮挡视频)
                    if (DRAW_BORDER_IN_SDL)
                    {
                        DrawRect(windowWidth, windowHeight);
                        //重新设置YUV大小====开始(边框效果)
                        yuv_srcrect = new SDL.SDL_Rect();
                        yuv_srcrect.x = 0;
                        yuv_srcrect.y = 0;
                        yuv_srcrect.w = windowWidth;
                        yuv_srcrect.h = windowHeight;

                        yuv_destrect = yuv_srcrect;
                        yuv_destrect.x = 1;
                        yuv_destrect.y = 1;
                        yuv_destrect.w = windowWidth - 2;
                        yuv_destrect.h = windowHeight - 2;
                        //重新设置YUV大小====开始(边框效果)
                    }

                    //dlgIClientChange main_thread_run = InvokeMethodRender;
                    //main_thread_run.Invoke();

                    //画最后一张图或者画背景
                    ret = SDL.SDL_UpdateTexture(sdltexture, IntPtr.Zero, m_yuv_buffer, m_video_width);

                    ret = SDL.SDL_RenderClear(sdlrenderer);

                    // 渲染视频图像
                    //if (DRAW_BORDER_IN_SDL)
                    //{
                    //    // 边框样式渲染方式
                    //    ret = SDL.SDL_RenderCopy(sdlrenderer, sdltexture, ref yuv_srcrect,ref yuv_destrect);
                    //}
                    //else
                    {
                        // YUV自适应窗口大小
                        ret = SDL.SDL_RenderCopy(sdlrenderer, sdltexture, IntPtr.Zero, IntPtr.Zero);
                    }

 

如何清除矩形,可以自己了解下。

posted @ 2024-09-29 13:30  boonya  阅读(60)  评论(0)    收藏  举报  来源
我有佳人隔窗而居,今有伊人明月之畔。
轻歌柔情冰壶之浣,涓涓清流梦入云端。
美人如娇温雅悠婉,目遇赏阅适而自欣。
百草层叠疏而有致,此情此思怀彼佳人。
念所思之唯心叩之,踽踽彳亍寤寐思之。
行云如风逝而复归,佳人一去莫知可回?
深闺冷瘦独自徘徊,处处明灯影还如只。
推窗见月疑是归人,阑珊灯火托手思忖。
庐居闲客而好品茗,斟茶徐徐漫漫生烟。

我有佳人在水之畔,瓮载渔舟浣纱归还。
明月相照月色还低,浅近芦苇深深如钿。
庐山秋月如美人衣,画堂春阁香气靡靡。
秋意幽笃残粉摇曳,轻轻如诉画中蝴蝶。
泾水潺潺取尔浇园,暮色黄昏如沐佳人。
青丝撩弄长裙翩翩,彩蝶飞舞执子手腕。
香带丝缕缓缓在肩,柔美体肤寸寸爱怜。
如水之殇美玉成欢,我有佳人清新如兰。
伊人在水我在一边,远远相望不可亵玩。