首页 > 其他分享 >How to enable Vsync with Software Rendering in SDL [SOLVED]. 如何在SDL2下实现垂直同步

How to enable Vsync with Software Rendering in SDL [SOLVED]. 如何在SDL2下实现垂直同步

时间:2023-05-24 13:11:26浏览次数:34  
标签:onepix enable rendering SDL2 How renderer SDL rect software

How to enable Vsync with Software Rendering in SDL [SOLVED].

Hi, I just figured out the dirty way to enable vsync with software rendering on SDL.

This was bothering me for a week now & I've seen many questions on the net without answer. But I finally figured it out and it's plain and stupid but it works.

It basically uses the Accelerated Renderer only to provide vsync'ed delay for software renderer. So I'm not certain if this will also work on machines with absolutely no acceleration (I have some OpenGl context enabled on my laptop, allthough I have no proper graphics card - have no idea how it works ). But It's worth a try. If the accelerated renderer won't crash your app (it does not have to display anything - just work in the background) - this should work for you.

This is how :

  1. The INIT section:

You create your main Window & then create a accelerated renderer with vsync enabled:

Main_Window = SDL_CreateWindow ("hello void",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, w, h,SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS);

renderer = SDL_CreateRenderer(Main_Window,-1,SDL_RENDERER_ACCELERATED|

SDL_RENDERER_PRESENTVSYNC);

Then, as you do in software rendering, you assign a surface from your main window:

SDL Surface * window = SDL_GetWindowSurface(Main_Window);

This surface is your main rendering target for software rendering. You may further create a proper software renderer (if you want to use SDL rendering functions) like this:

SDL_Renderer* soft_renderer = SDL_CreateSoftwareRenderer(window);

Or you can directly access the window surface pixel data, f.ex. for pixel by pixel fast software editing, blitting surfaces, or your invented rendering functions.

Then you create a tiny empty texture (1 pix by 1pix) to update to accelerated renderer:

texture = SDL_CreateTexture(renderer, window->format->format, SDL_TEXTUREACCESS_STREAMING, 1, 1);

Then you create a tiny 1 pix SDL_Rect structure like this:

SDL_Rect onepix_rect;

onepix_rect.h=1;

onepix_rect.w=1;

onepix_rect.x=0;

onepix_rect.y=0;

2. Then what you do in the PICTURE UPDATE loop:

Normally for software rendering you would use

SDL_UpdateWindowSurface(Main_Window);

 

Now - to get it to Vsync you put it right after SDL_RenderPresent(renderer) :

SDL_RenderCopy(renderer, texture, NULL, &onepix_rect);

SDL_RenderPresent(renderer);

SDL_UpdateWindowSurface(Main_Window);

In this way accelerated renderer provides a delay for the update loop & prevents picture tearing.

I guess how it works is the Acc_Renderer triggers on vsync but after it finishes uptade of 1 tiny texture there is still enough time for the UpdateWindow function to fit in the VBlank period of the screen.

You no longer have to put any framerate caps on the update loop.

If you want to further halve the number of frames (from the usual 60fps to 30fps),

You add another line like this which provides another delay:

SDL_RenderCopy(renderer, texture, NULL, &onepix_rect);

SDL_RenderPresent(renderer);

SDL_RenderPresent(renderer);

SDL_UpdateWindowSurface(Main_Window);

Or you can simply add any chosen delay in the loop.

If you manage to squeeze anything else in the picture update loop below 11ms or so, you will always reach 60 fps. Any more delay in the loop will simply lower your frame rate but won't ever cause picture tearing.

I know it's stupid and dirty hack, but it works and it enables you to edit surface pixels & update them to the window without conversion to textures - without picture tearing - and this is what was missing in software rendering for me.

Now I can confidently say the full functionality of Acc_Rendering can be applied to soft_rendering. And this is somehow pleasing to me.

Maybe this is something irrelevant (who the hell uses soft_rendering anyway - right?) but I just wanted to leave this solution somewhere if anyone was wondering like I did how to do it, because they are trying out SDL on computer without proper graphics card, or want to simplify their app by using just surfaces. Now You can do it without picture tearing. So cheers!

If you found this helpful future person, please leave a comment, it will make my day!

标签:onepix,enable,rendering,SDL2,How,renderer,SDL,rect,software
From: https://www.cnblogs.com/zxdplay/p/17428003.html

相关文章

  • How to find the TLS used for the SQL Server connection
    本文是HowtofindtheTLSusedfortheSQLServerconnection这篇英语文章的翻译,此文出处请见于文章底部链接:原文出处[1]对于客户,我做了一些研究,如何找出SQLServer数据库会话连接使用了哪一种TLS协议。唯一的方式就是创建一个扩展事件,这个扩展事件有一个很大的限制就是只有......
  • How to Delete a Git Branch Both Locally and Remotely
    TL;DRversionhttps://www.freecodecamp.org/news/how-to-delete-a-git-branch-both-locally-and-remotely///deletebranchlocallygitbranch-dlocalBranchName//deletebranchremotelygitpushorigin--deleteremoteBranchName#ToMakeLinuxshowuser@h......
  • this.$refs.ref 不存在 undefined v-if和v-show 的区别
    <divv-if='true'>  <div>....<el-inputref='ref'/></div></div> 上面的代码,引用this.$refs.ref=undefined; 改为v-show解决。 网上有好多说法是:this.$nextTick(()=>{放这里});在这里不能解决的。原因:v-if条件不满足,document中是不存在的,v......
  • Development cannot be enabled while your device is locked.
    问题:Developmentcannotbeenabledwhileyourdeviceislocked.分析原因:由于第一次连接MAC时,在iOS设备上点击了"不信任"选项,所以XCode8不能访问手机,所以才出现这个问题解决方法:英文好的同学,可以看到原文章http://stackoverflow.com/questions/26791477/xcode-device-locked-whe......
  • ctfshow php特性
    web111源代码highlight_file(__FILE__);error_reporting(0);include("flag.php");functiongetFlag(&$v1,&$v2){eval("$$v1=&$$v2;");//这里是一个赋值语句把v2的值复制下面通过get获得的$$v1值var_dump($$v1);//打印$$v1的值}if(isset($_......
  • Show Psw
    <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title>NewDocument</title><metaname="Generator"content="Edi......
  • @EnableAsync @Async
    一.基本介绍@Async是spring为了方便开发人员进行异步调用的出现的,在方法上加入这个注解,spring会从线程池中获取一个新的线程来执行方法,实现异步调用@EnableAsync表示开启对异步任务的支持,可以放在springboot的启动类上,也可以放在自定义线程池的配置类上,具体看下文二.最简单的......
  • How do I ask a good question?
    Iseeagoodweb-word:HowdoIaskagoodquestion?-HelpCenter-StackOverflow ......
  • 全网最详细解读《GIN-HOW POWERFUL ARE GRAPH NEURAL NETWORKS》!!!
    Abstract+IntroductionGNNs大都遵循一个递归邻居聚合的方法,经过k次迭代聚合,一个节点所表征的特征向量能够捕捉到距离其k-hop邻域的邻居节点的特征,然后还可以通过pooling获取到整个图的表征(比如将所有节点的表征向量相加后用于表示一个图表征向量)。关于邻居聚合策略以及......
  • How to accelerate the process of preclinical studies of new medicines
    Accordingtothedrugdevelopmentprocess,newdrugdevelopmentcanbedividedintopreclinicalandclinicalstudies.Theformeristhesumofalltheresearchworkdoneonadrugbeforeitentersclinicaltrials,andthelatteristheresearchworkdone......