我在做一个拼图游戏中的c++。现在游戏的时候负载所有的瓷砖把自己的基础上:

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

tile_count_x

tile_count_y

我有以下变量:

desktop_width

desktop_height

game_window_width

game_window_height

tile_count_x

tile_count_y

基于这些价值观,我在寻找一种算法,将设置一个适当的窗的大小定的桌面和tile_count的约束。然后在此,我想我的瓷砖有一种抵消,将边界x%左右的窗口,这将基本上决定tilesize:

例如:如果我有10*3的砖然后:

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

我只是不确定的公式的要求做到这一点。

编辑(从意见):

  • Tilesize变化,tilecountx和y是静态的
  • 我想gamewindow要大,因为它可以给出桌面上的决议,但我也想要它方面的比率尊重tilecoutx和tilecounty

我发现一个例子是什么我的意思是,打开了扫雷,在Windows

有帮助吗?

解决方案 4

这里是我如何解决它...

        //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;

其他提示

我不是用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%周围的填充的所有侧面砖

注意,我没有测试过这个代码,但它 应该 的工作。如果你发现任何错误,试着去理解什么我试图做的修复。

简单线性代数:

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个未知数。仍然溶胶。

如果你的工作与Win32,然后你可以使用类似于以下最初的尺寸的客户机-区域窗口。

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

当窗口接待调整的事件只会stretchblt每块瓷砖(即放大或缩小每块瓷砖)通过将像素数,窗口已经扩大或缩小(假定轴是锁定那是你不能调整每个independetly).

换句话说分块大小会有所不同:

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top