2017-07-30 123 views
0

我目前在我的遊戲中有兩個視口。一個是頂部的信息(健康,黃金,魔法等),另一個視口顯示地圖區域。我使用圖像創建了自己的自定義鼠標光標,並且我像顯示其他圖像一樣顯示該圖像,並基於鼠標光標位置更新位置。我唯一的問題是,我只能在視口上繪製該圖像。我已經在地圖區域設置了顯示,當我將鼠標移動到頂部時;它不會顯示在信息區域。它只是離開窗戶。所以我做的第一件事(我知道它不會工作,但無論如何測試它)是在兩個視口上繪製鼠標。但是,當移動到屏幕頂部(LOL)時,會顯示兩個鼠標光標。我的問題是,如何使用屏幕(或窗口)座標在視口外部使鼠標圖像在屏幕上繪製。那有意義嗎?我希望能夠將鼠標移動到屏幕上的任何位置,以便我可以單擊地圖視口中的項目以及信息視口。SDL使用多個視口的屏幕座標在屏幕上繪製圖像

我視類是非常簡單的:

Viewport.h

#pragma once 

#include <SDL.h> 

class Viewport 
{ 
public: 
    Viewport(int x, int y, int width, int height); 
    ~Viewport(); 

    SDL_Rect GetViewport(); 

private: 
    SDL_Rect viewport; 
}; 

Viewport.cpp

#include "Viewport.h" 

Viewport::Viewport(int x, int y, int width, int height) 
{ 
    viewport.x = x; 
    viewport.y = y; 
    viewport.w = width; 
    viewport.h = height; 
} 

Viewport::~Viewport() 
{ 

} 

SDL_Rect Viewport::GetViewport() 
{ 
    return viewport; 
} 

在我的遊戲類我初始化兩個變量對每個視窗

Viewport hud; 
Viewport arena; 

在構造函數中,我將它們初始化爲適當的大小。然後,在我的遊戲繪圖函數中,我相應地設置了視口並在相應的視口中繪製。

// gfx is my Graphics class. Everything that has to do with drawing is inside that class; including setting the viewport for the render. 
gfx.SetViewport(hud.GetViewport()); 
// This is where I would draw stuff on the hud viewport 

gfx.SetViewport(arena.GetViewport()); 
// Where I draw the map, enemies, etc. 

我SetViewport功能設置是這樣的:

void Graphics::SetViewport(SDL_Rect viewport) 
{ 
    SDL_RenderSetViewport(renderer, &viewport); 
} 

我怎樣才能得出我的鼠標圖像(與視沒有初步認識)。以下是我正在談論的一些圖像。

只顯示在舞臺上,不會顯示在hud上。 enter image description here

在鼠標上的兩個,如果我畫的鼠標在兩個視窗 enter image description here

編輯:我做了一個解決辦法,將這個遊戲 我只是擔心它贏得了」工作t在更大更大的遊戲上表現更好

我創建了與屏幕(窗口)一樣大的另一個視口,並將鼠標拖到該視口。

回答

0

超晚的答案,但在這裏。調用SDL_RenderSetViewport(渲染器,NULL)將重置視口到整個窗口。

在調用SDL_PollEvent之前,您應該這樣做,因爲如果您在視口處於活動狀態時輪詢鼠標事件,則鼠標移動事件座標將相對於視口而不是主窗口。