summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjörn Steinbrink <B.Steinbrink@gmx.de>2005-08-02 21:53:36 +0000
committerBjörn Steinbrink <B.Steinbrink@gmx.de>2005-08-02 21:53:36 +0000
commit88b41d7c9ffe40248218a7af58d92910c6737679 (patch)
tree3535eef1d6585a0c522255323d865d568bb58671
parent56797f661324ca160d1f7b3b678be9b1eaf8b534 (diff)
downloadmana-client-88b41d7c9ffe40248218a7af58d92910c6737679.tar.gz
mana-client-88b41d7c9ffe40248218a7af58d92910c6737679.tar.bz2
mana-client-88b41d7c9ffe40248218a7af58d92910c6737679.tar.xz
mana-client-88b41d7c9ffe40248218a7af58d92910c6737679.zip
Moved graphics setup code into the graphics class.
-rw-r--r--ChangeLog2
-rw-r--r--src/graphics.cpp129
-rw-r--r--src/graphics.h23
-rw-r--r--src/gui/setup.cpp45
-rw-r--r--src/main.cpp78
-rw-r--r--src/main.h1
6 files changed, 147 insertions, 131 deletions
diff --git a/ChangeLog b/ChangeLog
index c2a19414..f45feaa8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,8 @@
* src/engine.cpp, src/game.cpp: Made autoTarget checks being handled at a
single location.
* src/being.cpp, src/being.h, src/engine.h: Small header cleanups.
+ * src/graphics.cpp, src/graphics.h, src/main.cpp, src/main.h,
+ src/gui/setup.cpp: Moved graphics setup code into the graphics class.
2005-08-02 Marcel W. Wysocki <maci@satgnu.org>
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 93448c7c..0ee2371d 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -29,50 +29,130 @@
extern volatile int framesToDraw;
-Graphics::Graphics(SDL_Surface *screen):
- mScreen(screen)
+Graphics::Graphics():
+ mScreen(0)
{
+}
+
+Graphics::~Graphics()
+{
+ _endDraw();
+}
+
+bool Graphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel)
+{
+ int displayFlags = SDL_ANYFORMAT;
+
+ mFullscreen = fs;
+ mHWAccel = hwaccel;
+
+ if (fs) {
+ displayFlags |= SDL_FULLSCREEN;
+ }
+
+#ifdef USE_OPENGL
if (useOpenGL) {
+ displayFlags |= SDL_OPENGL;
+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+ } else
+#endif
+ {
+ if (hwaccel) {
+ displayFlags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
+ } else {
+ displayFlags |= SDL_SWSURFACE;
+ }
+ }
+
+ mScreen = SDL_SetVideoMode(w, h, bpp, displayFlags);
+
+ if (!mScreen) {
+ return false;
+ }
+
+ char videoDriverName[64];
+
+ if (SDL_VideoDriverName(videoDriverName, 64)) {
+ logger->log("Using video driver: %s", videoDriverName);
+ }
+ else {
+ logger->log("Using video driver: unkown");
+ }
+
+ const SDL_VideoInfo *vi = SDL_GetVideoInfo();
+
+ logger->log("Possible to create hardware surfaces: %s",
+ ((vi->hw_available) ? "yes" : "no"));
+ logger->log("Window manager available: %s",
+ ((vi->wm_available) ? "yes" : "no"));
+ logger->log("Accelerated hardware to hardware blits: %s",
+ ((vi->blit_hw) ? "yes" : "no"));
+ logger->log("Accelerated hardware to hardware colorkey blits: %s",
+ ((vi->blit_hw_CC) ? "yes" : "no"));
+ logger->log("Accelerated hardware to hardware alpha blits: %s",
+ ((vi->blit_hw_A) ? "yes" : "no"));
+ logger->log("Accelerated software to hardware blits: %s",
+ ((vi->blit_sw) ? "yes" : "no"));
+ logger->log("Accelerated software to hardware colorkey blits: %s",
+ ((vi->blit_sw_CC) ? "yes" : "no"));
+ logger->log("Accelerated software to hardware alpha blits: %s",
+ ((vi->blit_sw_A) ? "yes" : "no"));
+ logger->log("Accelerated color fills: %s",
+ ((vi->blit_fill) ? "yes" : "no"));
+ logger->log("Available video memory: %d", vi->video_mem);
+
#ifdef USE_OPENGL
+ if (useOpenGL) {
// Setup OpenGL
- glViewport(0, 0, 800, 600);
+ glViewport(0, 0, w, h);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
int gotDoubleBuffer;
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &gotDoubleBuffer);
logger->log("Using OpenGL %s double buffering.",
(gotDoubleBuffer ? "with" : "without"));
- setTargetPlane(800, 600);
+ setTargetPlane(w, h);
+ } else
#endif
- }
- else {
+ {
setTarget(mScreen);
}
- // Initialize for drawing
- if (!useOpenGL) {
- gcn::SDLGraphics::_beginDraw();
+ return true;
+}
+
+bool Graphics::setFullscreen(bool fs)
+{
+ if (mFullscreen == fs) {
+ return true;
}
+
+ return setVideoMode(mScreen->w, mScreen->h,
+ mScreen->format->BitsPerPixel, fs, mHWAccel);
+}
+
+void Graphics::_beginDraw()
+{
#ifdef USE_OPENGL
- else {
+ if (useOpenGL) {
gcn::OpenGLGraphics::_beginDraw();
- }
+ } else
#endif
- //_beginDraw();
+ {
+ gcn::SDLGraphics::_beginDraw();
+ }
}
-Graphics::~Graphics()
+void Graphics::_endDraw()
{
- // Deinitialize for drawing
- if (!useOpenGL) {
- gcn::SDLGraphics::_endDraw();
- }
#ifdef USE_OPENGL
- else {
+ if (useOpenGL) {
gcn::OpenGLGraphics::_endDraw();
- }
+ } else
#endif
- //_endDraw();
+ {
+ gcn::SDLGraphics::_endDraw();
+ }
}
void Graphics::setFont(gcn::ImageFont *font)
@@ -88,9 +168,7 @@ void Graphics::setFont(gcn::ImageFont *font)
}
void Graphics::drawText(const std::string &text,
- int x,
- int y,
- unsigned int alignment)
+ int x, int y, unsigned int alignment)
{
if (!useOpenGL) {
gcn::SDLGraphics::drawText(text, x, y, alignment);
@@ -223,8 +301,3 @@ void Graphics::updateScreen()
SDL_Delay(10);
}
}
-
-void Graphics::setScreen(SDL_Surface *screen)
-{
- mScreen = screen;
-}
diff --git a/src/graphics.h b/src/graphics.h
index 461bb45c..1d616611 100644
--- a/src/graphics.h
+++ b/src/graphics.h
@@ -67,13 +67,26 @@ public gcn::SDLGraphics {
/**
* Constructor.
*/
- Graphics(SDL_Surface *screen);
+ Graphics();
/**
* Destructor.
*/
~Graphics();
+ /**
+ * Try to create a window with the given settings.
+ */
+ bool setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel);
+
+ /**
+ * Set fullscreen mode.
+ */
+ bool setFullscreen(bool fs);
+
+ void _beginDraw();
+ void _endDraw();
+
void drawImage(Image *image, int x, int y);
void drawImagePattern(Image *image, int x, int y, int w, int h);
@@ -113,13 +126,6 @@ public gcn::SDLGraphics {
*/
int getHeight();
- /**
- * Sets a new screen pointer. This is necessary after switching screen
- * modes, which probably should happen by this class instead of in the
- * setup window.
- */
- void setScreen(SDL_Surface *screen);
-
void setFont(gcn::ImageFont *font);
void drawText(const std::string &text,
@@ -131,6 +137,7 @@ public gcn::SDLGraphics {
private:
SDL_Surface *mScreen;
+ bool mFullscreen, mHWAccel;
};
#endif
diff --git a/src/gui/setup.cpp b/src/gui/setup.cpp
index cda9ae31..91f64ecc 100644
--- a/src/gui/setup.cpp
+++ b/src/gui/setup.cpp
@@ -214,39 +214,20 @@ void Setup::action(const std::string &eventId)
else if (eventId == "apply")
{
setVisible(false);
- bool changed = false;
-
- if (fsCheckBox->isMarked() && config.getValue("screen", 0) == 0) {
- // Fullscreen
- config.setValue("screen", 1);
- displayFlags |= SDL_FULLSCREEN;
- changed = true;
- }
- else if(!fsCheckBox->isMarked() && config.getValue("screen", 0) == 1) {
- // Windowed
- config.setValue("screen", 0);
- displayFlags &= ~SDL_FULLSCREEN;
- changed = true;
- }
-
- if(changed) {
- displayFlags |= SDL_DOUBLEBUF;
- if (useOpenGL) {
- //displayFlags |= SDL_OPENGL;
- //SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+ bool fullscreen = fsCheckBox->isMarked();
+
+ if (fullscreen != (config.getValue("screen", 0) == 1)) {
+ if (!guiGraphics->setFullscreen(fullscreen)) {
+ fullscreen = !fullscreen;
+ if (!guiGraphics->setFullscreen(fullscreen)) {
+ std::cerr << "Failed to switch to " <<
+ (fullscreen ? "windowed" : "fullscreen") <<
+ "mode and restoration of old mode also failed!" <<
+ std::endl;
+ exit(1);
+ }
}
-
- SDL_Surface *screen =
- SDL_SetVideoMode(screenW, screenH, bitDepth, displayFlags);
-
- if (screen == NULL) {
- std::cerr << "Couldn't set " << screenW << "x" <<
- screenH << "x" << bitDepth << " video mode: " <<
- SDL_GetError() << std::endl;
- exit(1);
- }
-
- guiGraphics->setScreen(screen);
+ config.setValue("screen", fullscreen ? 1 : 0);
}
// Sound settings
diff --git a/src/main.cpp b/src/main.cpp
index 580a838a..c2ea4886 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -75,7 +75,6 @@ short map_port;
char map_name[16];
unsigned char state;
unsigned char screen_mode;
-int displayFlags, screenW, screenH, bitDepth;
bool useOpenGL = false;
volatile int framesToDraw = 0;
@@ -231,82 +230,37 @@ void init_engine()
}
SDL_WM_SetCaption("The Mana World", NULL);
+ SDL_WM_SetIcon(IMG_Load(TMW_DATADIR "data/icons/tmw-icon.png"), NULL);
#ifdef USE_OPENGL
useOpenGL = (config.getValue("opengl", 0) == 1);
#endif
- displayFlags = SDL_ANYFORMAT;
-
- if ((int)config.getValue("screen", 0) == 1) {
- displayFlags |= SDL_FULLSCREEN;
- }
-
- if (useOpenGL) {
- displayFlags |= SDL_OPENGL;
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- }
- else {
- if ((int)config.getValue("hwaccel", 0)) {
- logger->log("Attempting to use hardware acceleration.");
- displayFlags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
- }
- else {
- displayFlags |= SDL_SWSURFACE;
- }
- }
+ int width, height, bpp;
+ bool fullscreen, hwaccel;
- screenW = (int)config.getValue("screenwidth", 800);
- screenH = (int)config.getValue("screenheight", 600);
+ width = (int)config.getValue("screenwidth", 800);
+ height = (int)config.getValue("screenheight", 600);
+ bpp = 0;
+ fullscreen = ((int)config.getValue("screen", 0) == 1);
+ hwaccel = ((int)config.getValue("hwaccel", 0) == 1);
- SDL_WM_SetIcon(IMG_Load(TMW_DATADIR "data/icons/tmw-icon.png"), NULL);
+ // Create the graphics context
+ graphics = new Graphics();
- SDL_Surface *screen = SDL_SetVideoMode(screenW, screenH, 0, displayFlags);
- if (screen == NULL) {
- std::cerr << "Couldn't set " << screenW << "x" << screenH << "x" <<
- bitDepth << " video mode: " << SDL_GetError() << std::endl;
+ // Try to set the desired video mode
+ if (!graphics->setVideoMode(width, height, bpp, fullscreen, hwaccel)) {
+ std::cerr << "Couldn't set " << width << "x" << height << "x" <<
+ bpp << " video mode: " << SDL_GetError() << std::endl;
exit(1);
}
- char videoDriverName[64];
-
- if (SDL_VideoDriverName(videoDriverName, 64)) {
- logger->log("Using video driver: %s", videoDriverName);
- }
- else {
- logger->log("Using video driver: unkown");
- }
-
- const SDL_VideoInfo *vi = SDL_GetVideoInfo();
-
- logger->log("Possible to create hardware surfaces: %s",
- ((vi->hw_available) ? "yes" : "no"));
- logger->log("Window manager available: %s",
- ((vi->wm_available) ? "yes" : "no"));
- logger->log("Accelerated hardware to hardware blits: %s",
- ((vi->blit_hw) ? "yes" : "no"));
- logger->log("Accelerated hardware to hardware colorkey blits: %s",
- ((vi->blit_hw_CC) ? "yes" : "no"));
- logger->log("Accelerated hardware to hardware alpha blits: %s",
- ((vi->blit_hw_A) ? "yes" : "no"));
- logger->log("Accelerated software to hardware blits: %s",
- ((vi->blit_sw) ? "yes" : "no"));
- logger->log("Accelerated software to hardware colorkey blits: %s",
- ((vi->blit_sw_CC) ? "yes" : "no"));
- logger->log("Accelerated software to hardware alpha blits: %s",
- ((vi->blit_sw_A) ? "yes" : "no"));
- logger->log("Accelerated color fills: %s",
- ((vi->blit_fill) ? "yes" : "no"));
- logger->log("Available video memory: %d", vi->video_mem);
-
- //vfmt Pixel format of the video device
+ // Initialize for drawing
+ graphics->_beginDraw();
// Initialize item manager
itemDb = new ItemManager();
- // Create the graphics context
- graphics = new Graphics(screen);
-
login_wallpaper = resman->getImage(
"graphics/images/login_wallpaper.png");
Image *playerImg = resman->getImage(
diff --git a/src/main.h b/src/main.h
index ce248c7b..10475f76 100644
--- a/src/main.h
+++ b/src/main.h
@@ -69,7 +69,6 @@ extern int account_ID, session_ID1, session_ID2;
extern char sex, n_server, n_character;
extern unsigned char state;
extern Sound sound;
-extern int screenW, screenH, bitDepth, displayFlags;
extern bool useOpenGL;
#endif