简体   繁体   English

SDL:如何设置 window 的像素大小(拉伸)

[英]SDL: How to set size of pixel (stretch) of a window

I'm making a retro pixel game so I want to make my window a very low resolution (256x256).我正在制作一个复古像素游戏,所以我想让我的 window 分辨率非常低(256x256)。 However when I try to make it fullscreen, the whole window was just rendered top left, while leaving all other areas black.但是,当我尝试使其全屏显示时,整个 window 只是呈现在左上角,而将所有其他区域保持为黑色。
I want to know a way of globally setting the size of each pixel in a window, in order to let it fit the fullscreen, or, a way of stretching the whole window (or a renderer?) to a specified size(and full screen) while having the (w, h) unchanged in parameter 2 and 3 in SDL_CreateWindow, and also whilst having the sizes proportional (so, if it was a square window, it should be a square window after stretched, not a rect after stretched into a rect displayer).我想知道一种全局设置 window 中每个像素大小的方法,以使其适合全屏,或者将整个 window(或渲染器?)拉伸到指定大小(和全屏)的方法) 同时在 SDL_CreateWindow 中的参数 2 和 3 中保持 (w, h) 不变,并且同时具有成比例的大小(因此,如果它是正方形 window,它应该是拉伸后的正方形 window,而不是拉伸后的矩形一个矩形显示器)。

First, render your game to a 256x256 texture.首先,将您的游戏渲染为 256x256 纹理。 This gist has an example, I will inline it below.这个要点有一个例子,我将在下面内联它。

Next, figure out the correct size and position of your game texture on your actual window, and render the texture there.接下来,在实际 window 上计算出游戏纹理的正确大小和 position,并在那里渲染纹理。 That will require modifications to the SDL_RenderCopyEx call, as the gist simply renders it stretched to the screen.这将需要修改SDL_RenderCopyEx调用,因为 gist 只是将其渲染到屏幕上。

#include <iostream>

#ifdef __linux__
#include <SDL2/SDL.h>
#elif defined(_WIN32)
#include <SDL.h>
#endif

const int WIN_WIDTH = 640;
const int WIN_HEIGHT = 480;

int main(int argc, char **argv){
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
        std::cerr << "SDL_Init failed: " << SDL_GetError() << "\n";
        return 1;
    }
    SDL_Window *win = SDL_CreateWindow("Rendering to a texture!", SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, 0);
    SDL_Renderer *renderer = SDL_CreateRenderer(win, -1,
        SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);

    //Put your own bmp image here
    SDL_Surface *bmpSurf = SDL_LoadBMP("../res/image.bmp");
    SDL_Texture *bmpTex = SDL_CreateTextureFromSurface(renderer, bmpSurf);
    SDL_FreeSurface(bmpSurf);

    //Make a target texture to render too
    SDL_Texture *texTarget = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
        SDL_TEXTUREACCESS_TARGET, WIN_WIDTH, WIN_HEIGHT);
    
    //Now render to the texture
    SDL_SetRenderTarget(renderer, texTarget);
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, bmpTex, NULL, NULL);
    //Detach the texture
    SDL_SetRenderTarget(renderer, NULL);

    //Now render the texture target to our screen, but upside down
    SDL_RenderClear(renderer);
    SDL_RenderCopyEx(renderer, texTarget, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
    SDL_RenderPresent(renderer);

    SDL_Delay(1000);
    SDL_DestroyTexture(texTarget);
    SDL_DestroyTexture(bmpTex);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM