瓷砖尺寸算法

问题描述:

我在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

+0

根据窗口/桌面大小是否平铺尺寸变化或者你想改变瓦数,以适应窗口还是什么?你可以编辑你的帖子而不是评论它。 – 2009-12-13 17:01:40

+0

我认为那是对我提出的评论的回复。我删除了评论,因为我认为我读了问题中的答案。 – keyboardP 2009-12-13 17:03:17

+0

Tilesize变化,tilecountx和y是静态的 – jmasterx 2009-12-13 17:03:17

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

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

简单的线性代数开辟扫雷:

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。

我不是一个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%的填充度

请注意,我还没有测试过这个代码,但它应该工作。如果您发现任何错误,请尝试了解我尝试执行的操作并相应地进行修复。

如果您使用的是Win32,那么您可以使用类似下面的内容来初始调整窗口的客户区大小。

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

当窗口临危调整事件则只需stretchblt每一瓦片(即放大或缩小每个瓦片)通过该窗口已经膨胀或由(假设轴被锁定收缩的像素的数量,即你无法调整每个独立的)。

换句话说,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);