有没有办法将二维数组的 x 和 y 映射到屏幕的分辨率?

Is there a way to map a 2D array's x and y to a screen's resolution?

我有这个 SDL 和 C++ 代码来将 24 x 24 数组渲染到屏幕上。

int main(int argc, char *argv[])
{
    // Init
    SDL_Window *mainwindow = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED, SCREEN_W, SCREEN_H, SDL_WINDOW_SHOWN);
    SDL_Renderer *mainrenderer = SDL_CreateRenderer(mainwindow, -1,
            SDL_RENDERER_PRESENTVSYNC);
    SDL_Texture *maintexture = SDL_CreateTexture(mainrenderer, SDL_PIXELFORMAT_RGBA8888,
            SDL_TEXTUREACCESS_STATIC, SCREEN_W, SCREEN_H);
    Uint32 *buffer = new Uint32[SCREEN_W * SCREEN_H];
    memset(buffer, 0, SCREEN_H * SCREEN_W * sizeof(Uint32));



    //Loop

    while(true){
        if(quit_event() == false){break;}

        for(int y = 0; y < MAP_H; y++){
            for(int x = 0; x < MAP_W; x++){
                if(worldMap[y][x] == 0){
                    Uint32 colour = 0;

                    colour += 0xFF;
                    colour <<= 8;
                    colour += 0xFF;
                    colour <<= 8;
                    colour += 0xFF;
                    colour <<= 8;
                    colour += 0;

                    buffer[(y * SCREEN_W) + x] = colour;
                }
            }
        }
        SDL_UpdateTexture(maintexture, NULL, buffer,
                SCREEN_W * sizeof(Uint32));
        SDL_RenderClear(mainrenderer);
        SDL_RenderCopy(mainrenderer, maintexture, NULL, NULL);
        SDL_RenderPresent(mainrenderer);
    }
    delete[] buffer;
    SDL_DestroyRenderer(mainrenderer);
    SDL_DestroyTexture(maintexture);
    SDL_DestroyWindow(mainwindow);
    SDL_Quit();

}

当运行程序显示时。

如何将 worldMap 的 X 和 Y 值映射到屏幕上的 X 和 Y 值而不使 worldMap 数组与屏幕大小相同?

编辑:我已经更改了程序,现在可以使用了。

#include <iostream>
#include "SDL.h"

#define MAP_W 8
#define MAP_H 8
#define MAP_A 64
#define GRID_S 64
#define SCREEN_W 640
#define SCREEN_H 480
using namespace std;
int worldMap[]=
{
 1,1,1,1,1,1,1,1,
 1,0,1,0,0,0,0,1,
 1,0,1,0,0,0,0,1,
 1,0,1,0,0,0,0,1,
 1,0,0,0,0,0,0,1,
 1,0,0,0,0,1,0,1,
 1,0,0,0,0,0,0,1,
 1,1,1,1,1,1,1,1,
};

bool quit_event(){
    SDL_Event event;

    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {

            return false;
        }
    }
    return true;
}



int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    // Init
    SDL_Window *mainwindow = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED, SCREEN_W, SCREEN_H, SDL_WINDOW_RESIZABLE);
    SDL_Renderer *mainrenderer = SDL_CreateRenderer(mainwindow, -1,
            SDL_RENDERER_PRESENTVSYNC);



    //Loop

    while(true){
        SDL_SetRenderDrawColor(mainrenderer,0,0,255,255);

        SDL_RenderClear(mainrenderer);

         SDL_SetRenderDrawColor(mainrenderer,0,255,255,255);
        if(quit_event() == false){break;}
        SDL_Rect rect;
        rect.w = GRID_S;
        rect.h = GRID_S;
        for(int y = 0; y < MAP_H; y++){
            rect.y = 0 + (y * GRID_S);
            for(int x = 0; x < MAP_W; x++){
                rect.x = 0 + (x * GRID_S);
                if(worldMap[y*MAP_W+x] == 1){
                    SDL_RenderDrawRect(mainrenderer,&rect);
                    SDL_RenderFillRect(mainrenderer,&rect);
                }
            }
        }




        SDL_RenderPresent(mainrenderer);
    }

    SDL_DestroyRenderer(mainrenderer);

    SDL_DestroyWindow(mainwindow);
    SDL_Quit();

}

现在我只是对矩形如何呈现不稳定感到困惑...

这似乎是最近未解决的问题。 https://github.com/libsdl-org/SDL/issues/4001

在你的情况下,问题显然出在顶部,而不是底部,但总体上是同一个问题。

尝试省略 SDL_RenderDrawRect,只使用 SDL_RenderFillRect