summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-01-21 12:30:36 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-01-22 11:12:17 +0100
commitc4bdf324d7d464bc7fabbf88e1a6e66701c6e7cb (patch)
treed87655455c0a331c73bf5b762701fafaa5348688
parent0d613d75b732c0d057acc3004f9b4322cf73d449 (diff)
downloadMana-c4bdf324d7d464bc7fabbf88e1a6e66701c6e7cb.tar.gz
Mana-c4bdf324d7d464bc7fabbf88e1a6e66701c6e7cb.tar.bz2
Mana-c4bdf324d7d464bc7fabbf88e1a6e66701c6e7cb.tar.xz
Mana-c4bdf324d7d464bc7fabbf88e1a6e66701c6e7cb.zip
Allow resizing of the game in windowed mode
Window positions are semi-smartly corrected as a result of the resize. Not supported when using OpenGL on Windows for now. Reviewed-by: Yohann Ferreira
-rw-r--r--src/client.cpp53
-rw-r--r--src/client.h4
-rw-r--r--src/game.cpp16
-rw-r--r--src/game.h2
-rw-r--r--src/graphics.cpp27
-rw-r--r--src/graphics.h5
-rw-r--r--src/gui/gui.cpp14
-rw-r--r--src/gui/gui.h5
-rw-r--r--src/gui/widgets/window.cpp85
-rw-r--r--src/gui/widgets/window.h15
-rw-r--r--src/gui/widgets/windowcontainer.cpp10
-rw-r--r--src/gui/widgets/windowcontainer.h6
-rw-r--r--src/openglgraphics.cpp11
13 files changed, 178 insertions, 75 deletions
diff --git a/src/client.cpp b/src/client.cpp
index 768fae1e..058c113b 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -184,7 +184,7 @@ Client *Client::mInstance = 0;
Client::Client(const Options &options):
mOptions(options),
- mRootDir(""),
+ mGame(0),
mCurrentDialog(0),
mQuitDialog(0),
mDesktop(0),
@@ -487,15 +487,14 @@ int Client::exec()
{
int lastTickTime = tick_time;
- Game *game = 0;
SDL_Event event;
while (mState != STATE_EXIT)
{
- if (game)
+ if (mGame)
{
// Let the game handle the events while it is active
- game->handleInput();
+ mGame->handleInput();
}
else
{
@@ -510,6 +509,10 @@ int Client::exec()
case SDL_KEYDOWN:
break;
+
+ case SDL_VIDEORESIZE:
+ resizeVideo(event.resize.w, event.resize.h);
+ break;
}
guiInput->pushInput(event);
@@ -522,8 +525,8 @@ int Client::exec()
while (get_elapsed_time(lastTickTime) > 0)
{
gui->logic();
- if (game)
- game->logic();
+ if (mGame)
+ mGame->logic();
sound.logic();
@@ -579,10 +582,7 @@ int Client::exec()
- 3, 3);
top->add(mSetupButton);
- int screenWidth = config.getIntValue("screenwidth");
- int screenHeight = config.getIntValue("screenheight");
-
- mDesktop->setSize(screenWidth, screenHeight);
+ mDesktop->setSize(graphics->getWidth(), graphics->getHeight());
}
if (mState == STATE_SWITCH_LOGIN && mOldState == STATE_GAME)
@@ -601,8 +601,8 @@ int Client::exec()
if (mOldState == STATE_GAME)
{
- delete game;
- game = 0;
+ delete mGame;
+ mGame = 0;
}
mOldState = mState;
@@ -866,7 +866,7 @@ int Client::exec()
mCurrentDialog = NULL;
logger->log("State: GAME");
- game = new Game;
+ mGame = new Game;
break;
case STATE_LOGIN_ERROR:
@@ -1382,3 +1382,30 @@ void Client::accountLogin(LoginData *loginData)
config.setValue("username", loginData->username);
config.setValue("remember", loginData->remember);
}
+
+void Client::resizeVideo(int width, int height)
+{
+ // Keep a minimum size. This isn't adhered to by the actual window, but
+ // it keeps some window positions from getting messed up.
+ width = std::max(640, width);
+ height = std::max(480, height);
+
+ if (graphics->getWidth() == width && graphics->getHeight() == height)
+ return;
+
+ if (graphics->resize(width, height))
+ {
+ gui->videoResized();
+
+ if (mDesktop)
+ mDesktop->setSize(width, height);
+
+ if (mSetupButton)
+ mSetupButton->setPosition(width - mSetupButton->getWidth() - 3, 3);
+
+ if (mGame)
+ mGame->videoResized(width, height);
+
+ gui->draw();
+ }
+}
diff --git a/src/client.h b/src/client.h
index 8b6af127..e2b1c264 100644
--- a/src/client.h
+++ b/src/client.h
@@ -33,6 +33,7 @@
#include <string>
+class Game;
class Button;
class Desktop;
class LoginData;
@@ -202,6 +203,8 @@ public:
void event(Event::Channel channel, const Event &event);
void action(const gcn::ActionEvent &event);
+ void resizeVideo(int width, int height);
+
private:
void initRootDir();
void initHomeDir();
@@ -225,6 +228,7 @@ private:
ServerInfo mCurrentServer;
+ Game *mGame;
Window *mCurrentDialog;
QuitDialog *mQuitDialog;
Desktop *mDesktop;
diff --git a/src/game.cpp b/src/game.cpp
index 77ccfc44..1c24d5d1 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -217,8 +217,7 @@ Game::Game():
// Create the viewport
viewport = new Viewport;
- viewport->setDimension(gcn::Rectangle(0, 0, graphics->getWidth(),
- graphics->getHeight()));
+ viewport->setSize(graphics->getWidth(), graphics->getHeight());
gcn::Container *top = static_cast<gcn::Container*>(gui->getTop());
top->add(viewport);
@@ -417,8 +416,13 @@ void Game::handleInput()
{
bool used = false;
+ if (event.type == SDL_VIDEORESIZE)
+ {
+ // Let the client deal with this one (it'll pass down from there)
+ Client::instance()->resizeVideo(event.resize.w, event.resize.h);
+ }
// Keyboard events (for discontinuous keys)
- if (event.type == SDL_KEYDOWN)
+ else if (event.type == SDL_KEYDOWN)
{
gcn::Window *requestedWindow = NULL;
@@ -996,3 +1000,9 @@ int Game::getCurrentTileHeight() const
return DEFAULT_TILE_LENGTH;
}
+
+void Game::videoResized(int width, int height)
+{
+ viewport->setSize(width, height);
+ mWindowMenu->setPosition(width - 3 - mWindowMenu->getWidth(), 3);
+}
diff --git a/src/game.h b/src/game.h
index 9e38f928..65b68c04 100644
--- a/src/game.h
+++ b/src/game.h
@@ -80,6 +80,8 @@ class Game
void updateWindowMenuCaptions()
{ mWindowMenu->updatePopUpCaptions(); }
+ void videoResized(int width, int height);
+
private:
int mLastTarget;
bool mDisconnected;
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 24f92544..13acc0a7 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -61,6 +61,8 @@ bool Graphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel)
if (fs)
displayFlags |= SDL_FULLSCREEN;
+ else
+ displayFlags |= SDL_RESIZABLE;
if (hwaccel)
displayFlags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
@@ -112,6 +114,31 @@ bool Graphics::setFullscreen(bool fs)
return setVideoMode(mWidth, mHeight, mBpp, fs, mHWAccel);
}
+bool Graphics::resize(int width, int height)
+{
+ if (mWidth == width && mHeight == height)
+ return true;
+
+ const int prevWidth = mWidth;
+ const int prevHeight = mHeight;
+
+ _endDraw();
+
+ bool success = setVideoMode(width, height, mBpp, mFullscreen, mHWAccel);
+
+ // If it didn't work, try to restore the previous size. If that didn't
+ // work either, bail out (but then we're in deep trouble).
+ if (!success)
+ {
+ if (!setVideoMode(prevWidth, prevHeight, mBpp, mFullscreen, mHWAccel))
+ return false;
+ }
+
+ _beginDraw();
+
+ return success;
+}
+
int Graphics::getWidth() const
{
return mWidth;
diff --git a/src/graphics.h b/src/graphics.h
index 4fec02f8..5e39d510 100644
--- a/src/graphics.h
+++ b/src/graphics.h
@@ -93,6 +93,11 @@ class Graphics : public gcn::SDLGraphics
bool setFullscreen(bool fs);
/**
+ * Resize the window to the specified size.
+ */
+ bool resize(int width, int height);
+
+ /**
* Blits an image onto the screen.
*
* @return <code>true</code> if the image was blitted properly
diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp
index 593bed10..70086eb6 100644
--- a/src/gui/gui.cpp
+++ b/src/gui/gui.cpp
@@ -102,8 +102,7 @@ Gui::Gui(Graphics *graphics):
// Initialize top GUI widget
WindowContainer *guiTop = new WindowContainer;
guiTop->setFocusable(true);
- guiTop->setDimension(gcn::Rectangle(0, 0,
- graphics->getWidth(), graphics->getHeight()));
+ guiTop->setSize(graphics->getWidth(), graphics->getHeight());
guiTop->setOpaque(false);
Window::setWindowContainer(guiTop);
setTop(guiTop);
@@ -221,6 +220,17 @@ void Gui::draw()
mGraphics->popClipArea();
}
+void Gui::videoResized()
+{
+ WindowContainer *top = static_cast<WindowContainer*>(getTop());
+
+ int oldWidth = top->getWidth();
+ int oldHeight = top->getHeight();
+
+ top->setSize(graphics->getWidth(), graphics->getHeight());
+ top->adjustAfterResize(oldWidth, oldHeight);
+}
+
void Gui::setUseCustomCursor(bool customCursor)
{
if (customCursor != mCustomCursor)
diff --git a/src/gui/gui.h b/src/gui/gui.h
index 6b40282f..b8aee7b5 100644
--- a/src/gui/gui.h
+++ b/src/gui/gui.h
@@ -63,6 +63,11 @@ class Gui : public gcn::Gui
*/
void draw();
+ /**
+ * Called when the application window has been resized.
+ */
+ void videoResized();
+
gcn::FocusHandler *getFocusHandler() const
{ return mFocusHandler; }
diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp
index aa8e6df3..88c6da8a 100644
--- a/src/gui/widgets/window.cpp
+++ b/src/gui/widgets/window.cpp
@@ -328,7 +328,7 @@ void Window::setVisible(bool visible, bool forceSticky)
// Check if the window is off screen...
if (visible)
- checkIfIsOffScreen();
+ ensureOnScreen();
gcn::Window::setVisible((!forceSticky && isSticky()) || visible);
}
@@ -547,7 +547,7 @@ void Window::loadWindowState()
}
// Check if the window is off screen...
- checkIfIsOffScreen();
+ ensureOnScreen();
}
void Window::saveWindowState()
@@ -666,6 +666,22 @@ void Window::resetToDefaultSize()
saveWindowState();
}
+void Window::adjustPositionAfterResize(int oldScreenWidth, int oldScreenHeight)
+{
+ gcn::Rectangle dimension = getDimension();
+
+ // If window was aligned to the right or bottom, keep it there
+ const int rightMargin = oldScreenWidth - (getX() + getWidth());
+ const int bottomMargin = oldScreenHeight - (getY() + getHeight());
+ if (getX() > 0 && getX() > rightMargin)
+ dimension.x = graphics->getWidth() - rightMargin - getWidth();
+ if (getY() > 0 && getY() > bottomMargin)
+ dimension.y = graphics->getHeight() - bottomMargin - getHeight();
+
+ setDimension(dimension);
+ ensureOnScreen();
+}
+
int Window::getResizeHandles(gcn::MouseEvent &event)
{
int resizeHandles = 0;
@@ -761,62 +777,25 @@ void Window::center()
setLocationRelativeTo(getParent());
}
-void Window::checkIfIsOffScreen(bool partially, bool entirely)
+void Window::ensureOnScreen()
{
- // Move the window onto screen if it has become off screen
- // For instance, because of resolution change...
-
- // First of all, don't deal when a window hasn't got
- // any size initialized yet...
+ // Skip when a window hasn't got any size initialized yet
if (getWidth() == 0 && getHeight() == 0)
return;
- // Made partially the default behaviour
- if (!partially && !entirely)
- partially = true;
-
- // Keep guichan window inside screen (supports resizing any side)
+ gcn::Rectangle dimension = getDimension();
- gcn::Rectangle winDimension = getDimension();
+ // Check the left and bottom screen boundaries
+ if (dimension.x + dimension.width > graphics->getWidth())
+ dimension.x = graphics->getWidth() - dimension.width;
+ if (dimension.y + dimension.height > graphics->getHeight())
+ dimension.y = graphics->getHeight() - dimension.height;
- if (winDimension.x < 0)
- {
- winDimension.width += winDimension.x;
- winDimension.x = 0;
- }
- if (winDimension.y < 0)
- {
- winDimension.height += winDimension.y;
- winDimension.y = 0;
- }
+ // But never allow the windows to disappear in to the right and top
+ if (dimension.x < 0)
+ dimension.x = 0;
+ if (dimension.y < 0)
+ dimension.y = 0;
- // Look if the window is partially off-screen limits...
- if (partially)
- {
- if (winDimension.x + winDimension.width > graphics->getWidth())
- {
- winDimension.x = graphics->getWidth() - winDimension.width;
- }
-
- if (winDimension.y + winDimension.height > graphics->getHeight())
- {
- winDimension.y = graphics->getHeight() - winDimension.height;
- }
- setDimension(winDimension);
- return;
- }
-
- if (entirely)
- {
- if (winDimension.x > graphics->getWidth())
- {
- winDimension.x = graphics->getWidth() - winDimension.width;
- }
-
- if (winDimension.y > graphics->getHeight())
- {
- winDimension.y = graphics->getHeight() - winDimension.height;
- }
- }
- setDimension(winDimension);
+ setDimension(dimension);
}
diff --git a/src/gui/widgets/window.h b/src/gui/widgets/window.h
index 64631287..945e3276 100644
--- a/src/gui/widgets/window.h
+++ b/src/gui/widgets/window.h
@@ -295,6 +295,13 @@ class Window : public gcn::Window, gcn::WidgetListener
virtual void resetToDefaultSize();
/**
+ * Adjusts the window position after the application window has been
+ * resized.
+ */
+ void adjustPositionAfterResize(int oldScreenWidth,
+ int oldScreenHeight);
+
+ /**
* Gets the layout handler for this window.
*/
Layout &getLayout();
@@ -352,11 +359,11 @@ class Window : public gcn::Window, gcn::WidgetListener
};
/**
- * Check if the window is off-screen and then move it to be visible
- * again. This is internally used by loadWindowState
- * and setVisible(true) members.
+ * Ensures the window is on the screen, moving it if necessary. This is
+ * used by loadWindowState and setVisible(true), and when the screen
+ * is resized.
*/
- void checkIfIsOffScreen(bool partially = true, bool entirely = true);
+ void ensureOnScreen();
/**
* Determines if the mouse is in a resize area and returns appropriate
diff --git a/src/gui/widgets/windowcontainer.cpp b/src/gui/widgets/windowcontainer.cpp
index 7d5ecd37..99bf7b9f 100644
--- a/src/gui/widgets/windowcontainer.cpp
+++ b/src/gui/widgets/windowcontainer.cpp
@@ -21,6 +21,8 @@
#include "gui/widgets/windowcontainer.h"
+#include "gui/widgets/window.h"
+
#include "utils/dtor.h"
WindowContainer *windowContainer = NULL;
@@ -37,3 +39,11 @@ void WindowContainer::scheduleDelete(gcn::Widget *widget)
{
mDeathList.push_back(widget);
}
+
+void WindowContainer::adjustAfterResize(int oldScreenWidth,
+ int oldScreenHeight)
+{
+ for (WidgetListIterator i = mWidgets.begin(); i != mWidgets.end(); ++i)
+ if (Window *window = dynamic_cast<Window*>(*i))
+ window->adjustPositionAfterResize(oldScreenWidth, oldScreenHeight);
+}
diff --git a/src/gui/widgets/windowcontainer.h b/src/gui/widgets/windowcontainer.h
index 2ec65d15..ce6989c6 100644
--- a/src/gui/widgets/windowcontainer.h
+++ b/src/gui/widgets/windowcontainer.h
@@ -45,6 +45,12 @@ class WindowContainer : public Container
*/
void scheduleDelete(gcn::Widget *widget);
+ /**
+ * Ensures that all visible windows are on the screen after the screen
+ * has been resized.
+ */
+ void adjustAfterResize(int oldScreenWidth, int oldScreenHeight);
+
private:
/**
* List of widgets that are scheduled to be deleted.
diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp
index e2fd9397..4eb0c60a 100644
--- a/src/openglgraphics.cpp
+++ b/src/openglgraphics.cpp
@@ -77,7 +77,17 @@ bool OpenGLGraphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel)
mHWAccel = hwaccel;
if (fs)
+ {
displayFlags |= SDL_FULLSCREEN;
+ }
+ else
+ {
+ // Resizing currently not supported on Windows, where it would require
+ // reuploading all textures.
+#if !defined(_WIN32)
+ displayFlags |= SDL_RESIZABLE;
+#endif
+ }
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
@@ -654,6 +664,7 @@ void OpenGLGraphics::_beginDraw()
void OpenGLGraphics::_endDraw()
{
+ popClipArea();
}
SDL_Surface* OpenGLGraphics::getScreenshot()