Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C SDL2 tilemap-太慢_C_Sdl_Sdl 2 - Fatal编程技术网

C SDL2 tilemap-太慢

C SDL2 tilemap-太慢,c,sdl,sdl-2,C,Sdl,Sdl 2,我正在使用SDL2编写一个游戏,在每一帧显示一个tilemap,但是性能太慢了。我写了一个小程序来解决这个问题。考虑“TEMP.BMP”是16x16的图像。< /P> #include <stdio.h> #include "SDL2/SDL.h" #include "SDL2/SDL_timer.h" #include "SDL2/SDL_image.h" int main() { SDL_Window* win; SDL_Renderer* ren;

我正在使用SDL2编写一个游戏,在每一帧显示一个tilemap,但是性能太慢了。我写了一个小程序来解决这个问题。考虑“TEMP.BMP”是16x16的图像。< /P>
#include <stdio.h>

#include "SDL2/SDL.h"
#include "SDL2/SDL_timer.h"
#include "SDL2/SDL_image.h"

int main()
{
    SDL_Window* win;
    SDL_Renderer* ren;
    int x, y;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(800, 600, 0, &win, &ren);
    SDL_Surface* sf = IMG_Load("temp.bmp");
    SDL_Texture* tx = SDL_CreateTextureFromSurface(ren, sf);

    for(;;) {
        Uint32 t = SDL_GetTicks();
        for(x=0; x<800; x+=16) {
            for(y=0; y<600; y+=16) {
                SDL_Rect src = { 0, 0, 16, 16 };
                SDL_Rect dst = { x, y, 16, 16 };
                SDL_RenderCopy(ren, tx, &src, &dst);
            }
        }
        SDL_RenderPresent(ren);
        printf("%ld ms\n", SDL_GetTicks() - t);
    }
}
#包括
#包括“SDL2/SDL.h”
#包括“SDL2/SDL_timer.h”
#包括“SDL2/SDL_image.h”
int main()
{
SDL_窗口*win;
SDL_*ren;
int x,y;
SDL_Init(SDL_Init_视频);
SDL_CreateWindowandRender(800、600、0、win和ren);
SDL_表面*sf=IMG_载荷(“温度bmp”);
SDL_Texture*tx=SDL_CreateTextureFromSurface(ren,sf);
对于(;;){
Uint32 t=SDL_GetTicks();
对于第页的(x=0;x,它提到SDL_RENDERER_PRESENTVSYNC标志表示您已同步到刷新率。
试试这个


您可以先使用渲染到大纹理。然后,该纹理每帧仅绘制一次

例如:

Uint32 fmt;
SDL_QueryTexture(tx, &fmt, NULL, NULL, NULL);
SDL_Texture* tm = SDL_CreateTexture(ren, fmt, SDL_TEXTUREACCESS_TARGET, 800, 600);
SDL_SetRenderTarget(ren, tm);
// Your draw loop here
SDL_SetRenderTarget(ren, NULL);
// Real draw loop only needs to RenderCopy tm

它是由图形驱动程序(opengl后端)强制执行的垂直同步。它由用户控制,并且应用程序本身不可更改。

在使用vsync的系统上,在
SDL\u RenderPresent()之前,我有大量的额外时间(~15ms):

#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
SDL_窗口*win;
SDL_*ren;
int x,y;
SDL_Init(SDL_Init_视频);
win=SDL\U创建窗口
( 
“窗口”,
SDL_WINDOWPOS_居中,SDL_WINDOWPOS_居中,
800, 600, 
0
);
ren=SDL_
( 
赢
-1,
SDL_渲染器| SDL_渲染器|呈现VSync
);
对于(;;)
{
Uint32 t=SDL_GetTicks();

对于(x=0;x是否可以在
SDL\u RenderPresent
之前添加一个性能测量,以了解您是否在那里浪费了时间。我不知道SDL2,但可能渲染受到vsync的限制?您是否尝试过在全屏模式下运行?据我所知,这些标志可以被视频驱动程序覆盖。可能
SDL\u getrenderinfo
有助于你知道你的显卡是用的还是只用你的CPU(它后面用的是opengl吗?)测试VSYNC是否被代码、GFX卡/驱动程序或其他东西锁定的最佳方法是只绘制一个平铺。你应该获得超过100+fps的速度。如果它保持在60,那么你可以非常确定VSYNC是否处于打开状态。通过使用SDL_GetRenderInfo,我知道我正在使用硬件加速(设置了SDL_RENDERER_ACCELERATED,未设置SDL_RENDERER_软件)。我还知道我没有被VSYNC阻止(未设置SDL_RENDERER_PRESENTVSYNC)。很抱歉,我以为它已设置。
Uint32 fmt;
SDL_QueryTexture(tx, &fmt, NULL, NULL, NULL);
SDL_Texture* tm = SDL_CreateTexture(ren, fmt, SDL_TEXTUREACCESS_TARGET, 800, 600);
SDL_SetRenderTarget(ren, tm);
// Your draw loop here
SDL_SetRenderTarget(ren, NULL);
// Real draw loop only needs to RenderCopy tm
#include <stdio.h>
#include <stdlib.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>

int main( int argc, char** argv )
{
    SDL_Window* win;
    SDL_Renderer* ren;
    int x, y;

    SDL_Init( SDL_INIT_VIDEO );

    win = SDL_CreateWindow
        ( 
        "Window", 
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
        800, 600, 
        0 
        );

    ren = SDL_CreateRenderer
        ( 
        win, 
        -1,
        SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
        );

    for(;;) 
    {
        Uint32 t = SDL_GetTicks();
        for(x=0; x<800; x+=16) 
        {
            for(y=0; y<600; y+=16) 
            {
                SDL_Rect dst = { x, y, 16, 16 };
                SDL_SetRenderDrawColor( ren, rand()%255, rand()%255, rand()%255, 255 );
                SDL_RenderFillRect( ren, &dst );
            }
        }

        // adjust this up/down to figure out how much extra time you have
        // stop when you start seeing frame times exceeding ~16ms
        SDL_Delay( 15 );

        SDL_RenderPresent(ren);
        printf("%ld ms\n", SDL_GetTicks() - t);
    }
}