平铺大小算法

时间:2009-12-13 16:50:35

标签: c++ algorithm tiles

我正在用c ++制作瓷砖游戏。现在,当游戏根据以下内容加载所有牌块时自己放置:

tilesize - >它们是正方形,所以这是宽度和高度

tile_count_x

tile_count_y

我有以下变量:

desktop_width

desktop_height

game_window_width

game_window_height

tile_count_x

tile_count_y

基于这些值,我正在寻找一种算法,该算法将根据desktop和tile_count约束设置适当的窗口大小。然后在这个内部,我希望我的瓷砖有一个偏移量,它将围绕窗口边界x%,这也基本上决定了瓷砖尺寸:

实施例: 如果我有10 * 3的瓷砖,那么:

______________________________
Window Title              _[]X
------------------------------
|                            |
|    [][][][][][][][][][]    | 
|    [][][][][][][][][][]    |
|    [][][][][][][][][][]    |
|                            |
------------------------------

我只是不确定这样做的公式。

编辑(来自评论):

  • Tilesize更改,tilecountx和y是静态的
  • 我希望游戏窗口与桌面分辨率一样大,但我也希望它的宽高比能够尊重tilecoutx和tilecounty

我找到了一个我的意思的例子,在Windows中打开Minesweeper

4 个答案:

答案 0 :(得分:1)

我不是C ++程序员,但你应该从中得到一些东西:

// Amount of padding to leave around tiles (of window size)
int percentage = 10;

tile_ratio = tile_count_x / tile_count_y;
desktop_ratio = desktop_width / desktop_height;

// Determine the maximum window width and height
// according to tile and desktop aspect ratios
if(tile_ratio >= desktop_ratio) {
    window_width = desktop_width;
    window_height = window_width * (1 / tile_ratio);
} else {
    window_height = desktop_height;
    window_width = window_height * tile_ratio;
}

// Determine maximum width and height for tiles,
// taking account x% of padding on both sides, hence the * 2
tile_width = window_width * ((100 - (percentage * 2)) / 100);
tile_height = window_height * ((100 - (percentage * 2)) / 100);

// As the tiles must be square, determine the smaller side as the size
tile_size = tile_width < tile_height ? tile_width : tile_height;

// To maintain the x% padding, we must calculate the window size again as we just changed the tilesize
factor = (100 / (100 - (percentage * 2)));
window_width = tile_size * tile_count_x * factor;
window_height = tile_size * tile_count_y * factor;

现在您拥有最大窗口宽度和高度,以便:

  1. 窗口与瓷砖的宽高比相同
  2. 窗口不比桌面大
  3. 窗口在瓷砖的所有边上都有x%的填充
  4. 请注意,我根本没有测试过此代码,但 应该。如果您发现任何错误,请尝试了解我尝试做的事情并进行相应的修复。

答案 1 :(得分:0)

简单线性代数:

game_window_width = (tile_count_x * TILE_WIDTH) + 2*(game_window_width * X / 100)

我们有2个变量(TILE_WIDTHX),只有1个等式,所以嗯,你运气不好。

您需要指定TILE_WIDTHX(保证金)。假设您指定X,那么您可以求解等式:

TILE_WIDTH = (game_window_width - (2*(game_window_width * X / 100))) / 
             tile_count_x

如果我们考虑到高度,并且需要相同的边界,那么我们有2个方程和3个未知数。还是SOL。

答案 2 :(得分:0)

如果您正在使用Win32,那么您可以使用以下内容来初始调整窗口的客户区域。

initial_width = (tile_count_x * tile_size) + (border_width * 2);
initial_height = (tile_count_y * tile_size) + (border_width * 2);

当窗口接收调整大小事件时,您只需将每个图块(即放大或缩小每个图块)拉伸窗口已扩展或缩小的像素数(假设轴已锁定,即可以' t独立调整大小。)

换句话说,tile_size会有所不同:

tile_size += window_resize_amount;

此代码来自内存,但它可能会提供一个想法。

DWORD dwStyle = WS_POPUP | WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_CAPTION;
DWORD dwExStyle = 0;

int border_width = tile_size; // make border 1 tile

RECT r, w;
SetRect(&r, 0, 0, (tile_count_x * tile_size) + (border_width * 2), (tile_count_y * tile_size) + (border_width * 2));
AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
SystemParametersInfo(SPI_GETWORKAREA, 0, &w, 0);
int width = r.right - r.left;
int height = r.bottom - r.top;
int x = ((w.right - w.left) / 2) - (width / 2);
int y = ((w.bottom - w.top) / 2) - (height / 2);

hWnd = CreateWindowEx(dwExStyle, szClassName, szAppName, dwStyle, x, y, width, height, NULL, NULL, hInstance, 0);

答案 3 :(得分:0)

以下是我如何解决它......

        //WINDOW SIZE SETUP

        //choose the smaller TILE_SIZE
        if (DESKTOP_WIDTH / TILE_COUNT_X > DESKTOP_HEIGHT / TILE_COUNT_Y)
        {
            TILE_SIZE = DESKTOP_HEIGHT / TILE_COUNT_Y;
        }
        else
        {   
            TILE_SIZE = DESKTOP_WIDTH / TILE_COUNT_X;
        }
        //Set screen size and consider a 5 tile border
        SCREEN_WIDTH = TILE_SIZE * (TILE_COUNT_X + 5);
        SCREEN_HEIGHT = TILE_SIZE * (TILE_COUNT_Y + 5);

        //resize window until it satisfies resolution constraints
        while( SCREEN_WIDTH > (DESKTOP_WIDTH - (DESKTOP_WIDTH * 0.07)))
        {
            TILE_SIZE --;
        SCREEN_WIDTH = TILE_SIZE * (TILE_COUNT_X + 5);
        SCREEN_HEIGHT = TILE_SIZE * (TILE_COUNT_Y + 5);
        }

        while( SCREEN_HEIGHT > (DESKTOP_HEIGHT - (DESKTOP_HEIGHT * 0.15)))
        {
            TILE_SIZE -- ;
        SCREEN_WIDTH = TILE_SIZE * (TILE_COUNT_X + 5);
        SCREEN_HEIGHT = TILE_SIZE * (TILE_COUNT_Y + 5);
        }
for(int i = 0; i < 8; ++i)     //Ensure resolution is multiple of 8
{
    if (SCREEN_WIDTH % 8 != 0) //remainder means not multiple of 8
    {
        SCREEN_WIDTH += 1;
    }

    if (SCREEN_HEIGHT % 8 != 0)
    {
        SCREEN_HEIGHT += 1;
    }
}
        X_OFFSET = (SCREEN_WIDTH - (TILE_SIZE * TILE_COUNT_X)) / 2;
        Y_OFFSET = (SCREEN_HEIGHT - (TILE_SIZE * TILE_COUNT_Y)) / 2;