SDL:无法翻转多个表面

发布于 2024-12-23 09:19:48 字数 1202 浏览 2 评论 0原文

我一直在尝试翻转表面,并且如果我只翻转单个表面(与我从 SDL_SetVideoMode 返回的同一表面),就会成功。如果我尝试翻转从 SDL_DisplayFormat 返回的表面,则不会发生任何事情。我附上了演示代码来演示我的问题:

#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"

void main()
{
    int i;
    SDL_Surface *mysurface1;
    SDL_Surface *mysurface2;
    char *pxl;

    SDL_Init( SDL_INIT_EVERYTHING );

    mysurface1 = SDL_SetVideoMode( 640, 480, 8, SDL_DOUBLEBUF|SDL_HWSURFACE );

    for (i = 0; i < 20; i++)
    {
            pxl = (char *)mysurface1->pixels + i*mysurface1->pitch + i;
            *pxl = 100; // Red Line
    }

    SDL_Flip(mysurface1); // Works, we see a red line

    sleep(5);
    printf("Sleeping for 5...\n");

    mysurface2 = SDL_DisplayFormat(mysurface1);

    for (i = 0; i < 20; i++)
    {
            pxl = (char *)mysurface2->pixels + i*mysurface2->pitch + i;
            *pxl = 255; // White line
    }

    SDL_Flip(mysurface2); // White line doesnt appear

    printf("Done... No white line\n");

    sleep(10);
}

以前有人见过这个吗?再次,我认为我追踪到了如果它是我从 SDL_DisplayFormat 返回的表面,则不会显示的表面。如果我在表面上执行此操作,我会从 SDL_SetVideoMode 返回,然后我会看到红线,并且一切正常。

I've been trying to flip surfaces and have been successful if I'm only flipping a single surface (the same surface I got back from SDL_SetVideoMode). If I try to flip the surface I get back from SDL_DisplayFormat, nothing happens. I've attached demo code that demonstrates my problem:

#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"

void main()
{
    int i;
    SDL_Surface *mysurface1;
    SDL_Surface *mysurface2;
    char *pxl;

    SDL_Init( SDL_INIT_EVERYTHING );

    mysurface1 = SDL_SetVideoMode( 640, 480, 8, SDL_DOUBLEBUF|SDL_HWSURFACE );

    for (i = 0; i < 20; i++)
    {
            pxl = (char *)mysurface1->pixels + i*mysurface1->pitch + i;
            *pxl = 100; // Red Line
    }

    SDL_Flip(mysurface1); // Works, we see a red line

    sleep(5);
    printf("Sleeping for 5...\n");

    mysurface2 = SDL_DisplayFormat(mysurface1);

    for (i = 0; i < 20; i++)
    {
            pxl = (char *)mysurface2->pixels + i*mysurface2->pitch + i;
            *pxl = 255; // White line
    }

    SDL_Flip(mysurface2); // White line doesnt appear

    printf("Done... No white line\n");

    sleep(10);
}

Has anyone ever seen this before? Again, I think I tracked it down to surfaces that wont display if its a surface I got back from SDL_DisplayFormat. If I do it on the surface I get back from SDL_SetVideoMode, then I see the red line and everything works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界和平 2024-12-30 09:19:49

您只能翻转主显示表面(使用 SDL_SetVideoMode 创建的显示表面)。为了使其他表面可见,您需要将其传输到主表面上。有关如何执行此操作的详细信息,请查找 SDL_BlitSurface

You can only flip the main display surface (the one created with SDL_SetVideoMode). In order to make your other surface visible, you need to blit it onto the main surface. Lookup SDL_BlitSurface for details on how to do that.

苍景流年 2024-12-30 09:19:49

将屏幕传递给 SDL_Flip 函数。 Flip 函数修改 screen->pixels 的值,使其指向屏幕上不可见的表面。

不过,这仅适用于 SVGA 和 DGA 等视频设备。在 X11 上,调用 SDL_Flip(screen) 相当于调用 SDL_UpdateRect(screen, 0, 0, 0, 0)

#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"

void main()
{
    int i;
    SDL_Surface *screen;
    char *pxl;

    SDL_Init( SDL_INIT_EVERYTHING );
    screen = SDL_SetVideoMode( 640, 480, 8, SDL_DOUBLEBUF|SDL_HWSURFACE );

    printf("Drawing the red line ...\n");
    printf("screen->pixels = %p\n", screen->pixels);
    for (i = 0; i < 100; i++)
    {
        pxl = (char *)screen->pixels + i*screen->pitch + i;
        *pxl = 100; // Red Line
    }

    printf("Flip screens\n");
    SDL_Flip(screen); // Display the red line

    printf("Drawing the white line ...\n");
    printf("screen->pixels = %p\n", screen->pixels);
    for (i = 0; i < 100; i++)
    {
        pxl = (char *)screen->pixels + i*screen->pitch + i;
        *pxl = 255; // White line
    }

    sleep(3);
    printf("Flip screens\n");
    SDL_Flip(screen); // Display the white line

    sleep(10);
}

在我的 Linux 笔记本上,打印:

Drawing the red line ...
screen->pixels = 0xb6c8c008
Flip screens
Drawing the white line ...
screen->pixels = 0xb6c8c008
Flip screens

screen->pixels 的值是相同的,但这只是因为在 X11 上翻转操作是无操作。在 SVGA 或 DGA 等视频设备上,这两个值会不同。

Pass the screen to the SDL_Flip function. The flip function modifies the value of screen->pixels so that it points to the surface that isn't visible on the screen.

However, this is only applicable to video devices such as SVGA and DGA. On X11, calling SDL_Flip(screen) is equivalent to calling SDL_UpdateRect(screen, 0, 0, 0, 0).

#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"

void main()
{
    int i;
    SDL_Surface *screen;
    char *pxl;

    SDL_Init( SDL_INIT_EVERYTHING );
    screen = SDL_SetVideoMode( 640, 480, 8, SDL_DOUBLEBUF|SDL_HWSURFACE );

    printf("Drawing the red line ...\n");
    printf("screen->pixels = %p\n", screen->pixels);
    for (i = 0; i < 100; i++)
    {
        pxl = (char *)screen->pixels + i*screen->pitch + i;
        *pxl = 100; // Red Line
    }

    printf("Flip screens\n");
    SDL_Flip(screen); // Display the red line

    printf("Drawing the white line ...\n");
    printf("screen->pixels = %p\n", screen->pixels);
    for (i = 0; i < 100; i++)
    {
        pxl = (char *)screen->pixels + i*screen->pitch + i;
        *pxl = 255; // White line
    }

    sleep(3);
    printf("Flip screens\n");
    SDL_Flip(screen); // Display the white line

    sleep(10);
}

On my Linux notebook, this prints:

Drawing the red line ...
screen->pixels = 0xb6c8c008
Flip screens
Drawing the white line ...
screen->pixels = 0xb6c8c008
Flip screens

The value of screen->pixels is the same, but this is only because on X11 the flip operation is a no-operation. On a video device such as SVGA or DGA, the two values would be different.

姜生凉生 2024-12-30 09:19:49

首先,SDL_Flip() 似乎仅适用于与屏幕或窗口相对应的表面,例如 SDL_SetVideoMode() 创建的表面。你的另一个表面在屏幕外;对它进行双缓冲(或翻转它)没有多大意义,而且它很可能无论如何都不是双缓冲的。作为一个离屏表面,它不会出现,直到您使用 SDL_BlitSurface() 或类似函数将其传输到显示表面 - 然后,下次翻转显示表面时,更改将可见。

本质上,mysurface2 实际上并不在您的显示器上,直到您将其位图传输到显示器上的表面上。如果您将以下内容替换

SDL_Flip(mysurface2); // White line doesnt appear

为:

SDL_BlitSurface(mysurface2,NULL,mysurface1,NULL);
SDL_Flip(mysurface1);

...那么您的代码可能会按您的预期工作。

First, it seems SDL_Flip() only works on surfaces that correspond to the screen or a window, like those created by SDL_SetVideoMode(). Your other surface is off-screen; it doesn't make much sense to double-buffer it (or flip it), and it most likely isn't double-buffered anyway. Being an off screen surface, it won't appear until you blit it to your display surface with SDL_BlitSurface() or a similar function -- then, the changes will be visible next time you flip the display surface.

Essentially, mysurface2 isn't actually on your display until you put it there, by blitting it onto a surface that is on your display. If you replace the following:

SDL_Flip(mysurface2); // White line doesnt appear

With this:

SDL_BlitSurface(mysurface2,NULL,mysurface1,NULL);
SDL_Flip(mysurface1);

...then your code will probably work as you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文