Skyscraper 2.0
wx.cpp
Go to the documentation of this file.
1/*
2 Skyscraper 2.1 - wxWidgets Frontend
3 Copyright (C)2003-2025 Ryan Thoryk
4 https://www.skyscrapersim.net
5 https://sourceforge.net/projects/skyscraper/
6 Contact - ryan@skyscrapersim.net
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21*/
22
23#ifdef USING_WX
24#include "wx/wx.h"
25#include "wx/dir.h"
26#include "wx/cmdline.h"
27#include "wx/filename.h"
28#include "wx/filefn.h"
29#include "wx/stdpaths.h"
30#include "globals.h"
31#include "vm.h"
32#include "hal.h"
33#include "skyscraper.h"
34#include "mainscreen.h"
35
36#if defined(__WXGTK__)
37 // NOTE: Find the GTK install config with `pkg-config --cflags gtk+-2.0`
38 #include "gtk/gtk.h"
39 #include "gdk/gdk.h"
40 #include "gdk/gdkx.h"
41 #include "GL/glx.h"
42#endif
43
44namespace Skyscraper {
45
46void Skyscraper::AllowResize(bool value)
47{
48 //changes the window style to either allow or disallow resizing
49
50#ifdef USING_WX
51 if (value)
52 window->SetWindowStyleFlag(wxDEFAULT_FRAME_STYLE | wxMAXIMIZE);
53 else
54 window->SetWindowStyleFlag(wxDEFAULT_FRAME_STYLE & ~wxMAXIMIZE);
55 window->Refresh();
56#endif
57}
58
59Ogre::RenderWindow* Skyscraper::CreateRenderWindow(const Ogre::NameValuePairList* miscParams, const std::string& windowName)
60{
61#ifdef USING_WX
62 std::string name = windowName;
63
64 int width, height;
65 window->GetClientSize(&width, &height);
66
67 Ogre::NameValuePairList params;
68 if (miscParams)
69 params = *miscParams;
70
71 bool vsync = vm->GetHAL()->GetConfigBool(vm->GetHAL()->configfile, "Skyscraper.Frontend.Vsync", true);
72 if (vsync == true)
73 params["vsync"] = "true";
74 else
75 params["vsync"] = "false";
76 params["vsyncInterval"] = "1";
77 params["externalWindowHandle"] = getOgreHandle();
78
79#if defined(__WXMAC__)
80 params["macAPI"] = "cocoa";
81 params["macAPICocoaUseNSView"] = "true";
82#endif
83
84 return vm->GetHAL()->CreateRenderWindow(name, width, height, params);
85#else
86 return 0;
87#endif
88}
89
91{
92 //switch current working directory to executable's path, if needed
93 wxString exefile = wxStandardPaths::Get().GetExecutablePath(); //get full path and filename
94 wxString app_path = wxPathOnly(exefile); //strip off filename
95#if defined(__WXMAC__)
96 vm->data_path = settingsPath() + "/Skyscraper/"; //Application Support folder
97
98 if (!wxDirExists(vm->data_path))
99 wxMkdir(vm->data_path);
100
101 if (!wxDirExists(vm->data_path + wxT("buildings")))
102 wxMkdir(vm->data_path + wxT("buildings"));
103 if (!wxDirExists(vm->data_path + wxT("data")))
104 wxMkdir(vm->data_path + wxT("data"));
105 if (!wxDirExists(vm->data_path + wxT("screenshots")))
106 wxMkdir(vm->data_path + wxT("screenshots"));
107
108 wxSetWorkingDirectory(app_path + wxT("/../Resources")); //set working directory to resources folder on Mac
109
110 if (!wxFileExists(vm->data_path + wxT("skyscraper.ini")))
111 wxCopyFile("skyscraper.ini", vm->data_path + wxT("skyscraper.ini"));
112 if (!wxFileExists(vm->data_path + wxT("keyboard.ini")))
113 wxCopyFile("keyboard.ini", vm->data_path + wxT("keyboard.ini"));
114 if (!wxFileExists(vm->data_path + wxT("joystick.ini")))
115 wxCopyFile("joystick.ini", vm->data_path + wxT("joystick.ini"));
116
117#elif defined (__WXGTK__)
118 wxSetWorkingDirectory(app_path + wxT("/../")); //set working directory parent directory
119#else
120 wxSetWorkingDirectory(app_path); //set working directory
121#endif
122
123 //define command line options
124 static const wxCmdLineEntryDesc cmdLineDesc[] =
125 {
126 { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
127 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
128
129 { wxCMD_LINE_SWITCH, "c", "no-console", "hide the console",
130 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
131
132 { wxCMD_LINE_SWITCH, "f", "fullscreen", "start up in full-screen mode",
133 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
134
135 { wxCMD_LINE_SWITCH, "k", "check-script", "quickly check building script, and exit after",
136 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
137
138 { wxCMD_LINE_SWITCH, "m", "no-menu", "hide the main menu",
139 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
140
141 { wxCMD_LINE_SWITCH, "M", "no-music", "disable the intro music",
142 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
143
144 { wxCMD_LINE_SWITCH, "p", "no-panel", "hide the control panel",
145 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
146
147 { wxCMD_LINE_SWITCH, "v", "verbose", "enable verbose mode",
148 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
149
150 { wxCMD_LINE_SWITCH, "V", "version", "show version",
151 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
152
153 { wxCMD_LINE_PARAM, NULL, NULL, "building filename",
154 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
155
156 { wxCMD_LINE_NONE }
157 };
158
159 //set up command line parser
160 parser = new wxCmdLineParser(cmdLineDesc, argc, argv);
161
162 //process command line options
163 switch (parser->Parse())
164 {
165 case -1:
166 return false; //help was given, exit
167 case 0:
168 break; //everything is good, continue
169 default:
170 return false; //exit if parameters are incorrect
171 }
172
173 //only run idle events on specified windows, to reduce overhead
174 wxIdleEvent::SetMode(wxIDLE_PROCESS_SPECIFIED);
175 return true;
176}
177
178const std::string Skyscraper::getOgreHandle() const
179{
180#ifdef USING_WX
181#if defined(__WXMSW__)
182 // Handle for Windows systems
183 return Ogre::StringConverter::toString((size_t)((HWND)window->panel->GetHandle()));
184#elif defined(__WXGTK__)
185 // Handle for GTK-based systems
186
187 // wxWidgets uses several internal GtkWidgets, the GetHandle method
188 // returns a different one than this, but wxWidgets's GLCanvas uses this
189 // one to interact with GLX, so we do the same.
190 // NOTE: this method relies on implementation details in wxGTK and could
191 // change without any notification from the developers.
192 GtkWidget* privHandle = window->m_wxwindow;
193
194 // prevents flickering
195 //gtk_widget_set_double_buffered(privHandle, false);
196
197 gtk_widget_realize(privHandle);
198
199 // grab the window object
200 GdkWindow* gdkWin = gtk_widget_get_window((GtkWidget*)window->GetHandle());
201 Display* display = GDK_WINDOW_XDISPLAY(gdkWin);
202 Window wid = GDK_WINDOW_XID(gdkWin);
203
204 // screen (returns "display.screen")
205 std::string screenStr = DisplayString(display);
206 screenStr = screenStr.substr(screenStr.find(".") + 1, screenStr.size());
207
208 std::stringstream handleStream;
209 handleStream << (unsigned long)display << ':' << screenStr << ':' << wid;
210
211 // retrieve XVisualInfo
212 // NOTE: '-lGL' linker flag must be specified.
213 int attrlist[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 16, GLX_STENCIL_SIZE, 8, None };
214 XVisualInfo* vi = glXChooseVisual(display, DefaultScreen(display), attrlist);
215 handleStream << ':' << (unsigned long)vi;
216
217 return std::string(handleStream.str());
218
219#elif defined(__WXMAC__)
220 return Ogre::StringConverter::toString((size_t)window->MacGetTopLevelWindowRef());
221#else
222 #error Not supported on this platform!
223#endif
224#else
225 return "";
226#endif
227}
228
229}
230
231#endif
Ogre::RenderWindow * CreateRenderWindow(const Ogre::NameValuePairList *miscParams=0, const std::string &windowName="")
const std::string getOgreHandle() const
void AllowResize(bool value)
std::string settingsPath()