[Gcblue-commits] gcb_wx/src/graphics tcDisplayModes.cpp,NONE,1.1 tcDisplaySettingsView.cpp,NONE,1.1
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2004-06-14 21:53:30
|
Update of /cvsroot/gcblue/gcb_wx/src/graphics In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3244/src/graphics Modified Files: tcGraphicsEngine.cpp tcWindow.cpp Added Files: tcDisplayModes.cpp tcDisplaySettingsView.cpp Log Message: Index: tcGraphicsEngine.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/graphics/tcGraphicsEngine.cpp,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** tcGraphicsEngine.cpp 11 Apr 2004 22:25:33 -0000 1.15 --- tcGraphicsEngine.cpp 14 Jun 2004 21:52:47 -0000 1.16 *************** *** 38,41 **** --- 38,42 ---- #include <iostream> #include "wx/dcscreen.h" + #include "tcDisplayModes.h" #if _MSC_VER > 1000 *************** *** 851,855 **** wxMessageBox(text.c_str(),"Warning",wxICON_WARNING); } ! } --- 852,856 ---- wxMessageBox(text.c_str(),"Warning",wxICON_WARNING); } ! tcDisplayModes::Get()->LogModeInfo(); } Index: tcWindow.cpp =================================================================== RCS file: /cvsroot/gcblue/gcb_wx/src/graphics/tcWindow.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** tcWindow.cpp 11 Apr 2004 22:22:11 -0000 1.2 --- tcWindow.cpp 14 Jun 2004 21:52:48 -0000 1.3 *************** *** 41,47 **** BEGIN_EVENT_TABLE(tcWindow, wxWindow) ! //EVT_PAINT(tcWindow::OnPaint) ! //EVT_NC_PAINT(tcWindow::OnPaint) ! //EVT_ERASE_BACKGROUND(tcWindow::OnEraseBackground) EVT_MOVE(tcWindow::OnMove) EVT_LEFT_DOWN(tcWindow::OnLButtonDown) --- 41,47 ---- BEGIN_EVENT_TABLE(tcWindow, wxWindow) ! EVT_PAINT(tcWindow::OnPaint) ! EVT_NC_PAINT(tcWindow::OnPaint) ! EVT_ERASE_BACKGROUND(tcWindow::OnEraseBackground) EVT_MOVE(tcWindow::OnMove) EVT_LEFT_DOWN(tcWindow::OnLButtonDown) *************** *** 94,99 **** void tcWindow::OnEraseBackground(wxEraseEvent& event) { ! SetBackgroundColour(*wxBLACK); ! fprintf(stdout, "Erase [%s]\n",GetName()); } --- 94,100 ---- void tcWindow::OnEraseBackground(wxEraseEvent& event) { ! //SetBackgroundColour(*wxBLACK); ! //fprintf(stdout, "Erase [%s]\n",GetName()); ! return; } --- NEW FILE: tcDisplayModes.cpp --- /** @file tcDisplayModes.cpp ** ** Copyright (C) 2004 Dewitt Colclough (de...@tw...) ** All rights reserved. ** ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** ** GCB is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifdef WIN32 #include "wx/msw/private.h" // for MS Windows specific definitions #else #error "tcDisplayModes is Windows only at the moment" #endif #include "tcDisplayModes.h" #include "tcOptions.h" /** * Accessor for singleton instance */ tcDisplayModes* tcDisplayModes::Get() { static tcDisplayModes instance; return &instance; } void tcDisplayModes::ChangeMode(unsigned width, unsigned height, unsigned bits) { DEVMODE winModeInfo; winModeInfo.dmSize = sizeof(DEVMODE); winModeInfo.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; winModeInfo.dmBitsPerPel = bits; winModeInfo.dmPelsHeight = height; winModeInfo.dmPelsWidth = width; if (!IsModeValid(width, height, bits)) { fprintf(stderr, "tcDisplayModes::ChangeMode - Invalid mode: " "W:%d, H:%d, Bits:%d\n", width, height, bits); return; } long result = ChangeDisplaySettings(&winModeInfo, 0); // change dynamically if (result == DISP_CHANGE_SUCCESSFUL) { currentMode.bits = bits; currentMode.width = width; currentMode.height = height; } else if (result == DISP_CHANGE_BADMODE) { fprintf(stderr, "tcDisplayModes::ChangeMode - Mode not supported: " "W:%d, H:%d, Bits:%d\n", width, height, bits); } else { fprintf(stderr, "tcDisplayModes::ChangeMode - Mode change failed (%d)", result); } } /** * Changes display mode info in options xml file. Mode will take effect * after restart of game (if mode is valid). */ void tcDisplayModes::ChangeOptionsMode(unsigned width, unsigned height, unsigned bits) { if (!IsModeValid(width, height, bits)) { fprintf(stderr, "tcDisplayModes::ChangeOptionsMode - Invalid mode: " "W:%d, H:%d, Bits:%d\n", width, height, bits); return; } wxString modeString = wxString::Format("%d %d %d", width, height, bits); tcOptions::Get()->SetOptionString("DisplaySettings", modeString.GetData()); } /** * @return number of display modes (width, height, bits) */ unsigned int tcDisplayModes::GetModeCount() const { return (unsigned int)modeInfo.size(); } const tcDisplayModes::Info& tcDisplayModes::GetCurrentModeInfo() const { return currentMode; } const tcDisplayModes::Info& tcDisplayModes::GetModeInfo(unsigned int n) const { if (n >= modeInfo.size()) { fprintf(stderr, "tcDisplayModes::GetModeInfo - index out of bounds\n"); wxASSERT(0); } return modeInfo[n]; } bool tcDisplayModes::IsCurrentMode(unsigned n) const { if (n >= GetModeCount()) return false; tcDisplayModes::Info mode_n = modeInfo[n]; return ((mode_n.bits == currentMode.bits)&& (mode_n.height == currentMode.height)&& (mode_n.width == currentMode.width)); } /** * @return true if mode params match at least one of the modes in modeInfo */ bool tcDisplayModes::IsModeValid(unsigned width, unsigned height, unsigned bits) { unsigned int modeCount = GetModeCount(); for (unsigned int n=0; n<modeCount; n++) { tcDisplayModes::Info& info = modeInfo[n]; if ((info.bits == bits)&& (info.width == width)&& (info.height == height)) { return true; } } return false; } void tcDisplayModes::LoadModeFromOptions() { const char* modeString = tcOptions::Get()->GetOptionString("DisplaySettings"); int width; int height; int bits; if (sscanf(modeString, "%d %d %d", &width, &height, &bits) < 3) { return; } ChangeMode(width, height, bits); } /** * Writes modeInfo to stdout. PopulateModeInfo should be called first */ void tcDisplayModes::LogModeInfo() { unsigned int modeCount = GetModeCount(); for (unsigned int n=0; n<modeCount; n++) { tcDisplayModes::Info& info = modeInfo[n]; fprintf(stdout, "Display mode %d: W: %d, H: %d, Bits: %d\n", n, info.width, info.height, info.bits); } } /** * Initializes modeInfo vector with info on display modes. Ignores * modes with less than 16 bits per pixel and modes with width < 800. * WINDOWS ONLY for now. Next version of wxWidgets should have a * cross-platform version of these functions that we can use instead. */ void tcDisplayModes::PopulateModeInfo() { modeInfo.clear(); DEVMODE winModeInfo; winModeInfo.dmSize = sizeof(DEVMODE); unsigned lastBits = 0; unsigned lastWidth = 0; unsigned lastHeight = 0; int i = 0; while ( EnumDisplaySettings(0, i++, &winModeInfo) ) { tcDisplayModes::Info info; info.bits = winModeInfo.dmBitsPerPel; info.height = winModeInfo.dmPelsHeight; info.width = winModeInfo.dmPelsWidth; bool matchesLast = ((info.bits == lastBits)&& (info.width == lastWidth)&& (info.height == lastHeight)); if ((info.bits >= 16)&&(info.width >= 800)&&(!matchesLast)) { modeInfo.push_back(info); } lastBits = info.bits; lastWidth = info.width; lastHeight = info.height; } // ENUM_CURRENT_SETTINGS is defined as ((DWORD)-1) in windows.h EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &winModeInfo); currentMode.bits = winModeInfo.dmBitsPerPel; currentMode.height = winModeInfo.dmPelsHeight; currentMode.width = winModeInfo.dmPelsWidth; } tcDisplayModes::tcDisplayModes() { PopulateModeInfo(); startMode = currentMode; } tcDisplayModes::~tcDisplayModes() { ChangeMode(startMode.width, startMode.height, startMode.bits); } --- NEW FILE: tcDisplaySettingsView.cpp --- /* ** tcDisplaySettingsView.cpp ** ** Copyright (C) 2004 Dewitt Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "wx/wx.h" #ifdef WIN32 #include "wx/msw/private.h" // for MS Windows specific definitions #endif #include "wx/string.h" #include "tcDisplaySettingsView.h" #include "tcDisplayModes.h" #include "wxcommands.h" using namespace Gdiplus; /*******************************************************************************/ bool tcDisplaySettingsView::Init() { FontFamily ff(L"Arial"); mpFont = new Font(&ff,12,FontStyleBold,UnitPixel); if (mpFont == NULL) { fprintf(stderr, "tcDisplaySettingsView - mpFont creation failed\n"); return false; } mpBrush = new SolidBrush(Color(0xFEFFFFFF)); // color is ARGB if (mpBrush == NULL) { fprintf(stderr, "tcDisplaySettingsView - mpBrush creation failed\n"); return false; } mpPen = new Pen(Color(255,200,200,200),2); if (mpPen == NULL) { fprintf(stderr, "tcDisplaySettingsView - mpPen creation failed\n"); return false; } return true; } int tcDisplaySettingsView::Draw() { static nTestCount = 0; static WCHAR szwchar[256]; // wide char array buffer for string conversion Graphics *graphics; //Erase(); if (!GetGraphics(graphics)) { return false; } if (!modeListUpdated) { UpdateModeList(graphics); modeListUpdated = true; } DrawBackground(graphics); mpBrush->SetColor(0xFEFFFFFF); tcWindow::DrawText(graphics, mpFont, mpBrush, "To change display settings, select new display settings and restart game.", 25.0f, 80.0f); unsigned nModes = tcDisplayModes::Get()->GetModeCount(); RectF rstring; for(unsigned int n=0; n<nModes; n++) { modeDrawingInfo& drawingInfo = modeList[n]; wxString& modeText = drawingInfo.modeText; float xpos = drawingInfo.x; float ypos = drawingInfo.y; float height = drawingInfo.height; float textWidth = drawingInfo.width; if (n == mouseOverIdx) { mpBrush->SetColor(0xFE243280); } else if (tcDisplayModes::Get()->IsCurrentMode(n)) { mpBrush->SetColor(0xFE328032); } else { mpBrush->SetColor(0xFE122012); } graphics->FillRectangle(mpBrush, xpos, ypos, boxWidth, height); mpBrush->SetColor(0xFEFFFFFF); if (n == selectedModeIdx) { mpPen->SetColor(0xFEFFFFFF); graphics->DrawRectangle(mpPen, xpos, ypos, boxWidth, height); } float xoffset = 0.5f * (boxWidth - textWidth); tcWindow::DrawTextInRect(graphics, mpFont, mpBrush, modeText.GetData(), RectF(xpos + xoffset, ypos + 1.0f, boxWidth, height)); } DrawChildren(graphics); ReleaseGraphics(graphics); DrawBorder(); return true; } /** * @return index of mode containing screen point or -1 if none */ int tcDisplaySettingsView::GetBoxContainingPoint(const wxPoint& point) { std::map<unsigned int, modeDrawingInfo>::iterator iter = modeList.begin(); for(; iter != modeList.end(); ++iter) { RectF r(iter->second.x, iter->second.y, iter->second.width, iter->second.height); if (r.Contains(PointF(point.x, point.y))) { return (int)iter->first; } } return -1; } void tcDisplaySettingsView::OnLButtonDown(wxMouseEvent& event) { wxPoint point = event.GetPosition(); int downIdx = GetBoxContainingPoint(point); if ((downIdx >= 0) && ((unsigned)downIdx < tcDisplayModes::Get()->GetModeCount())) { const tcDisplayModes::Info& info = tcDisplayModes::Get()->GetModeInfo((unsigned)downIdx); tcDisplayModes::Get()->ChangeOptionsMode(info.width, info.height, info.bits); selectedModeIdx = downIdx; } } void tcDisplaySettingsView::OnMouseMove(wxMouseEvent& event) { wxPoint point = event.GetPosition(); mouseOverIdx = GetBoxContainingPoint(point); } /** * Updates modeList with bounding rectangle and mode text info. * Used for drawing and associating mouse location with a mode index. * Should be called at least once before drawing. */ void tcDisplaySettingsView::UpdateModeList(Gdiplus::Graphics *graphics) { static WCHAR szwchar[256]; // wide char array buffer for string conversion float xpos = 25.0f; const float ystart = 150.0f; float ypos = ystart; unsigned nModes = tcDisplayModes::Get()->GetModeCount(); RectF rstring; for(unsigned n=0; n<nModes; n++) { modeDrawingInfo drawingInfo; const tcDisplayModes::Info& info = tcDisplayModes::Get()->GetModeInfo(n); drawingInfo.modeText = wxString::Format("%d x %d (%d)", info.width, info.height, info.bits); MultiByteToWideChar(CP_ACP, 0, drawingInfo.modeText, -1, szwchar, 255); graphics->MeasureString(szwchar, -1, mpFont, PointF(0,0), &rstring); drawingInfo.x = xpos; drawingInfo.y = ypos - 1.0f; drawingInfo.width = rstring.Width; drawingInfo.height = rstring.Height + 2.0f; modeList[n] = drawingInfo; ypos += rstring.Height + 5.0f; if (ypos > float(mnHeight) - 20.0f) { ypos = ystart; xpos += boxWidth + 50.0f; } } } ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// /** * @param surfaceHost tcWindow to share surface of */ tcDisplaySettingsView::tcDisplaySettingsView(wxWindow *parent, tcWindow *surfaceHost, const wxPoint& pos, const wxSize& size, const wxString& name) : tcStandardWindow(parent, pos, size, "xml/displaysettings_gui.xml", name, surfaceHost), mpPen(0), mpFont(0), mpBrush(0), selectedModeIdx(-1), mouseOverIdx(-1), boxWidth(140.0f), modeListUpdated(false) { wxASSERT(surfaceHost); Init(); LoadBackgroundImage("background.jpg"); if (config) { AddXMLControls(); } else { fprintf(stderr, "tcDisplaySettingsView::tcDisplaySettingsView - " "NULL xml config node\n"); return; } } tcDisplaySettingsView::~tcDisplaySettingsView() { if (mpFont != NULL) {delete mpFont;} if (mpBrush != NULL) {delete mpBrush;} if (mpPen != NULL) {delete mpPen;} } |