From 10e82df5434c086c2afc3627e5cdf7b88205487f Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Wed, 8 Aug 2012 14:35:27 +0100 Subject: [PATCH] Fullscreen and scale setting from sim options --- src/PowderToySDL.cpp | 127 ++++++++++++++++++++++++++---- src/interface/Engine.cpp | 4 +- src/interface/Engine.h | 7 ++ src/options/OptionsController.cpp | 13 +++ src/options/OptionsController.h | 2 + src/options/OptionsModel.cpp | 21 +++++ src/options/OptionsModel.h | 4 + src/options/OptionsView.cpp | 27 ++++++- src/options/OptionsView.h | 2 + 9 files changed, 190 insertions(+), 17 deletions(-) diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 3123a4ba5..2b606c96b 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -40,6 +40,8 @@ extern "C" IMAGE_DOS_HEADER __ImageBase; #endif SDL_Surface * sdl_scrn; +int scale = 1; +bool fullscreen = false; #ifdef OGLI void blit() @@ -51,18 +53,96 @@ void blit(pixel * vid) { if(sdl_scrn) { - pixel * dst; pixel * src = vid; int j, x = 0, y = 0, w = XRES+BARSIZE, h = YRES+MENUSIZE, pitch = XRES+BARSIZE; + pixel *dst; if (SDL_MUSTLOCK(sdl_scrn)) if (SDL_LockSurface(sdl_scrn)<0) return; dst=(pixel *)sdl_scrn->pixels+y*sdl_scrn->pitch/PIXELSIZE+x; - for (j=0; jformat,0x33,0x55,0x77)!=PIXPACK(0x335577)) { - memcpy(dst, src, w*PIXELSIZE); - dst+=sdl_scrn->pitch/PIXELSIZE; - src+=pitch; + //pixel format conversion + int i; + pixel px; + SDL_PixelFormat *fmt = sdl_scrn->format; + for (j=0; j>fmt->Rloss)<Rshift)| + ((PIXG(px)>>fmt->Gloss)<Gshift)| + ((PIXB(px)>>fmt->Bloss)<Bshift); + } + dst+=sdl_scrn->pitch/PIXELSIZE; + src+=pitch; + } + } + else + { + for (j=0; jpitch/PIXELSIZE; + src+=pitch; + } + } + if (SDL_MUSTLOCK(sdl_scrn)) + SDL_UnlockSurface(sdl_scrn); + SDL_UpdateRect(sdl_scrn,0,0,0,0); + } +} +void blit2(pixel * vid, int currentScale) +{ + if(sdl_scrn) + { + pixel * src = vid; + int j, x = 0, y = 0, w = XRES+BARSIZE, h = YRES+MENUSIZE, pitch = XRES+BARSIZE; + pixel *dst; + int i,k; + if (SDL_MUSTLOCK(sdl_scrn)) + if (SDL_LockSurface(sdl_scrn)<0) + return; + dst=(pixel *)sdl_scrn->pixels+y*sdl_scrn->pitch/PIXELSIZE+x; + if (SDL_MapRGB(sdl_scrn->format,0x33,0x55,0x77)!=PIXPACK(0x335577)) + { + //pixel format conversion + pixel px; + SDL_PixelFormat *fmt = sdl_scrn->format; + for (j=0; j>fmt->Rloss)<Rshift)| + ((PIXG(px)>>fmt->Gloss)<Gshift)| + ((PIXB(px)>>fmt->Bloss)<Bshift); + dst[i*2]=px; + dst[i*2+1]=px; + } + dst+=sdl_scrn->pitch/PIXELSIZE; + } + src+=pitch; + } + } + else + { + for (j=0; jpitch/PIXELSIZE; + } + src+=pitch; + } } if (SDL_MUSTLOCK(sdl_scrn)) SDL_UnlockSurface(sdl_scrn); @@ -71,7 +151,7 @@ void blit(pixel * vid) } #endif -SDL_Surface * SDLOpen() +int SDLOpen() { SDL_Surface * surface; #if defined(WIN) && defined(WINCONSOLE) @@ -80,7 +160,7 @@ SDL_Surface * SDLOpen() if (SDL_Init(SDL_INIT_VIDEO)<0) { fprintf(stderr, "Initializing SDL: %s\n", SDL_GetError()); - return 0; + return 1; } SDL_EnableUNICODE(1); #if defined(WIN) && defined(WINCONSOLE) @@ -112,11 +192,6 @@ SDL_Surface * SDLOpen() SDL_WM_SetCaption("The Powder Toy", "Powder Toy"); //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); atexit(SDL_Quit); -#ifndef OGLI - surface = SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_SWSURFACE); -#else - surface = SDL_SetVideoMode((XRES + BARSIZE), (YRES + MENUSIZE), 32, SDL_OPENGL | SDL_RESIZABLE); -#endif #if defined(OGLI) int status = glewInit(); @@ -126,8 +201,20 @@ SDL_Surface * SDLOpen() exit(-1); } #endif + return 0; +} -return surface; +SDL_Surface * SDLSetScreen(int newScale, bool newFullscreen) +{ + scale = newScale; + fullscreen = newFullscreen; + SDL_Surface * surface; +#ifndef OGLI + surface = SDL_SetVideoMode((XRES + BARSIZE) * newScale, (YRES + MENUSIZE) * newScale, 32, SDL_SWSURFACE | (newFullscreen?SDL_FULLSCREEN:0)); +#else + surface = SDL_SetVideoMode((XRES + BARSIZE) * newScale, (YRES + MENUSIZE) * newScale, 32, SDL_OPENGL | SDL_RESIZABLE | (newFullscreen?SDL_FULLSCREEN:0)); +#endif + return surface; } std::map readArguments(int argc, char * argv[]) @@ -200,7 +287,8 @@ int main(int argc, char * argv[]) std::map arguments = readArguments(argc, argv); - sdl_scrn = SDLOpen(); + int sdlStatus = SDLOpen(); + sdl_scrn = SDLSetScreen(1, false); #ifdef OGLI SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); //glScaled(2.0f, 2.0f, 1.0f); @@ -290,10 +378,19 @@ int main(int argc, char * argv[]) Client::Ref().Tick(); } + if(scale != engine->Scale || fullscreen != engine->Fullscreen) + { + sdl_scrn = SDLSetScreen(engine->Scale, engine->Fullscreen); + inputScale = 1.0f/float(scale); + } + #ifdef OGLI blit(); #else - blit(engine->g->vid); + if(engine->Scale==2) + blit2(engine->g->vid, engine->Scale); + else + blit(engine->g->vid); #endif currentFrame++; diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp index bad931c0c..b0052012a 100644 --- a/src/interface/Engine.cpp +++ b/src/interface/Engine.cpp @@ -22,7 +22,9 @@ Engine::Engine(): lastBuffer(NULL), prevBuffers(stack()), windowTargetPosition(0, 0), - FrameIndex(0) + FrameIndex(0), + Fullscreen(false), + Scale(1) { } diff --git a/src/interface/Engine.h b/src/interface/Engine.h index 824f968d6..cd1c341e5 100644 --- a/src/interface/Engine.h +++ b/src/interface/Engine.h @@ -37,6 +37,11 @@ namespace ui inline bool Running() { return running_; } void Exit(); + void SetFullscreen(bool fullscreen) { Fullscreen = fullscreen; } + inline bool GetFullscreen() { return Fullscreen; } + void SetScale(int scale) { Scale = scale; } + inline int GetScale() { return Scale; } + void Tick(); void Draw(); @@ -55,6 +60,8 @@ namespace ui inline Window* GetWindow() { return state_; } float FpsLimit; Graphics * g; + int Scale; + bool Fullscreen; unsigned int FrameIndex; private: diff --git a/src/options/OptionsController.cpp b/src/options/OptionsController.cpp index 7fa3309b1..93d3062b7 100644 --- a/src/options/OptionsController.cpp +++ b/src/options/OptionsController.cpp @@ -38,19 +38,32 @@ void OptionsController::SetWaterEqualisation(bool state) { model->SetWaterEqualisation(state); } + void OptionsController::SetGravityMode(int gravityMode) { model->SetGravityMode(gravityMode); } + void OptionsController::SetAirMode(int airMode) { model->SetAirMode(airMode); } + void OptionsController::SetEdgeMode(int airMode) { model->SetEdgeMode(airMode); } +void OptionsController::SetFullscreen(bool fullscreen) +{ + model->SetFullscreen(fullscreen); +} + +void OptionsController::SetScale(bool scale) +{ + model->SetScale(scale); +} + OptionsView * OptionsController::GetView() { return view; diff --git a/src/options/OptionsController.h b/src/options/OptionsController.h index 821eb683e..862a0e23c 100644 --- a/src/options/OptionsController.h +++ b/src/options/OptionsController.h @@ -29,6 +29,8 @@ public: void SetGravityMode(int gravityMode); void SetAirMode(int airMode); void SetEdgeMode(int airMode); + void SetFullscreen(bool fullscreen); + void SetScale(bool scale); void Exit(); OptionsView * GetView(); virtual ~OptionsController(); diff --git a/src/options/OptionsModel.cpp b/src/options/OptionsModel.cpp index 6fe9a790b..6e71f360e 100644 --- a/src/options/OptionsModel.cpp +++ b/src/options/OptionsModel.cpp @@ -95,6 +95,27 @@ void OptionsModel::SetGravityMode(int gravityMode) notifySettingsChanged(); } +bool OptionsModel::GetScale() +{ + return ui::Engine::Ref().GetScale()==2; +} +void OptionsModel::SetScale(bool doubleScale) +{ + ui::Engine::Ref().SetScale(doubleScale?2:1); + notifySettingsChanged(); +} + + +bool OptionsModel::GetFullscreen() +{ + return ui::Engine::Ref().GetFullscreen(); +} +void OptionsModel::SetFullscreen(bool fullscreen) +{ + ui::Engine::Ref().SetFullscreen(fullscreen); + notifySettingsChanged(); +} + void OptionsModel::notifySettingsChanged() { for(int i = 0; i < observers.size(); i++) diff --git a/src/options/OptionsModel.h b/src/options/OptionsModel.h index c7fa6a0e2..b011fef02 100644 --- a/src/options/OptionsModel.h +++ b/src/options/OptionsModel.h @@ -34,6 +34,10 @@ public: void SetEdgeMode(int edgeMode); int GetGravityMode(); void SetGravityMode(int gravityMode); + bool GetFullscreen(); + void SetFullscreen(bool fullscreen); + bool GetScale(); + void SetScale(bool scale); virtual ~OptionsModel(); }; diff --git a/src/options/OptionsView.cpp b/src/options/OptionsView.cpp index 002bc8d27..cd4e319c4 100644 --- a/src/options/OptionsView.cpp +++ b/src/options/OptionsView.cpp @@ -12,7 +12,7 @@ #include "interface/DropDown.h" OptionsView::OptionsView(): - ui::Window(ui::Point(-1, -1), ui::Point(300, 226)){ + ui::Window(ui::Point(-1, -1), ui::Point(300, 266)){ ui::Label * tempLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 14), "Simulation Options"); tempLabel->SetTextColour(style::Colour::InformationTitle); @@ -136,6 +136,29 @@ OptionsView::OptionsView(): tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; AddComponent(tempLabel); + class ScaleAction: public ui::CheckboxAction + { + OptionsView * v; + public: + ScaleAction(OptionsView * v_){ v = v_; } + virtual void ActionCallback(ui::Checkbox * sender){ v->c->SetScale(sender->GetChecked()); } + }; + + scale = new ui::Checkbox(ui::Point(8, 206), ui::Point(Size.X-6, 16), "Large screen"); + scale->SetActionCallback(new ScaleAction(this)); + AddComponent(scale); + + class FullscreenAction: public ui::CheckboxAction + { + OptionsView * v; + public: + FullscreenAction(OptionsView * v_){ v = v_; } + virtual void ActionCallback(ui::Checkbox * sender){ v->c->SetFullscreen(sender->GetChecked()); } + }; + + fullscreen = new ui::Checkbox(ui::Point(8, 226), ui::Point(Size.X-6, 16), "Fullscreen"); + fullscreen->SetActionCallback(new FullscreenAction(this)); + AddComponent(fullscreen); class CloseAction: public ui::ButtonAction { @@ -164,6 +187,8 @@ void OptionsView::NotifySettingsChanged(OptionsModel * sender) airMode->SetOption(sender->GetAirMode()); gravityMode->SetOption(sender->GetGravityMode()); edgeMode->SetOption(sender->GetEdgeMode()); + scale->SetChecked(sender->GetScale()); + fullscreen->SetChecked(sender->GetFullscreen()); } void OptionsView::AttachController(OptionsController * c_) diff --git a/src/options/OptionsView.h b/src/options/OptionsView.h index 821413d15..d0a99abcd 100644 --- a/src/options/OptionsView.h +++ b/src/options/OptionsView.h @@ -25,6 +25,8 @@ class OptionsView: public ui::Window { ui::DropDown * airMode; ui::DropDown * gravityMode; ui::DropDown * edgeMode; + ui::Checkbox * scale; + ui::Checkbox * fullscreen; public: OptionsView(); void NotifySettingsChanged(OptionsModel * sender);