예제 #1
0
SDL_Renderer *
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
{
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    SDL_DisplayMode *displayMode = &display->current_mode;
    SDL_Renderer *renderer;
    SW_RenderData *data;
    int i, n;
    int bpp;
    Uint32 Rmask, Gmask, Bmask, Amask;
    Uint32 renderer_flags;
    const char *desired_driver;

    if (!SDL_PixelFormatEnumToMasks
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
        SDL_SetError("Unknown display format");
        return NULL;
    }

    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    if (!renderer) {
        SDL_OutOfMemory();
        return NULL;
    }

    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
    if (!data) {
        SW_DestroyRenderer(renderer);
        SDL_OutOfMemory();
        return NULL;
    }
    renderer->ActivateRenderer = SW_ActivateRenderer;
    renderer->DisplayModeChanged = SW_DisplayModeChanged;

    renderer->RenderPoint = SW_RenderPoint;
    renderer->RenderLine = SW_RenderLine;
    renderer->RenderFill = SW_RenderFill;
    renderer->RenderCopy = SW_RenderCopy;
    renderer->RenderPresent = SW_RenderPresent;
    renderer->DestroyRenderer = SW_DestroyRenderer;
    renderer->info.name = SW_RenderDriver.info.name;
    renderer->info.flags = 0;
    renderer->window = window->id;
    renderer->driverdata = data;
    Setup_SoftwareRenderer(renderer);

    if (flags & SDL_RENDERER_PRESENTFLIP2) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
        n = 2;
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
        n = 3;
    } else {
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
        n = 1;
    }
    data->format = displayMode->format;

    /* Find a render driver that we can use to display data */
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
                      SDL_RENDERER_PRESENTDISCARD);
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
    }
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
    for (i = 0; i < display->num_render_drivers; ++i) {
        SDL_RenderDriver *driver = &display->render_drivers[i];
        if (driver->info.name == SW_RenderDriver.info.name) {
            continue;
        }
        if (desired_driver
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
            continue;
        }
        data->renderer = driver->CreateRenderer(window, renderer_flags);
        if (data->renderer) {
            break;
        }
    }
    if (i == display->num_render_drivers) {
        SW_DestroyRenderer(renderer);
        SDL_SetError("Couldn't find display render driver");
        return NULL;
    }
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
    }

    /* Create the textures we'll use for display */
    for (i = 0; i < n; ++i) {
        data->texture[i] =
            CreateTexture(data->renderer, data->format, window->w, window->h);
        if (!data->texture[i]) {
            SW_DestroyRenderer(renderer);
            return NULL;
        }
    }
    data->current_texture = 0;

    /* Create a surface we'll use for rendering */
    data->surface.flags = SDL_PREALLOC;
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    if (!data->surface.format) {
        SW_DestroyRenderer(renderer);
        return NULL;
    }
    SDL_SetSurfacePalette(&data->surface, display->palette);

    /* Set up a palette watch on the display palette */
    if (display->palette) {
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
    }

    return renderer;
}
SDL_Renderer *
SDL_DUMMY_CreateRenderer(SDL_Window * window, Uint32 flags)
{
    SDL_VideoDisplay *display = window->display;
    SDL_DisplayMode *displayMode = &display->current_mode;
    SDL_Renderer *renderer;
    SDL_DUMMY_RenderData *data;
    int i, n;
    int bpp;
    Uint32 Rmask, Gmask, Bmask, Amask;

    if (!SDL_PixelFormatEnumToMasks
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
        SDL_SetError("Unknown display format");
        return NULL;
    }

    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    if (!renderer) {
        SDL_OutOfMemory();
        return NULL;
    }

    data = (SDL_DUMMY_RenderData *) SDL_malloc(sizeof(*data));
    if (!data) {
        SDL_DUMMY_DestroyRenderer(renderer);
        SDL_OutOfMemory();
        return NULL;
    }
    SDL_zerop(data);

    renderer->RenderDrawPoints = SDL_DUMMY_RenderDrawPoints;
    renderer->RenderDrawLines = SDL_DUMMY_RenderDrawLines;
    renderer->RenderDrawRects = SDL_DUMMY_RenderDrawRects;
    renderer->RenderFillRects = SDL_DUMMY_RenderFillRects;
    renderer->RenderCopy = SDL_DUMMY_RenderCopy;
    renderer->RenderReadPixels = SDL_DUMMY_RenderReadPixels;
    renderer->RenderWritePixels = SDL_DUMMY_RenderWritePixels;
    renderer->RenderPresent = SDL_DUMMY_RenderPresent;
    renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer;
    renderer->info.name = SDL_DUMMY_RenderDriver.info.name;
    renderer->info.flags = 0;
    renderer->window = window;
    renderer->driverdata = data;
    Setup_SoftwareRenderer(renderer);

    if (flags & SDL_RENDERER_PRESENTFLIP2) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
        n = 2;
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
        n = 3;
    } else {
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
        n = 1;
    }
    for (i = 0; i < n; ++i) {
        data->screens[i] =
            SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
                                 Bmask, Amask);
        if (!data->screens[i]) {
            SDL_DUMMY_DestroyRenderer(renderer);
            return NULL;
        }
        SDL_SetSurfacePalette(data->screens[i], display->palette);
    }
    data->current_screen = 0;

    return renderer;
}