1 /*
2 * This source file is part of libRocket, the HTML/CSS Interface Middleware
3 *
4 * For the latest information, see http://www.librocket.com
5 *
6 * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 #define _WIN32_WINNT 0x0500
29 #include "PythonInterface.h"
30
31 #include <Rocket/Core.h>
32 #include <Rocket/Controls.h>
33 #include <Rocket/Debugger.h>
34
35 #include <Input.h>
36 #include <Shell.h>
37 #include "DecoratorInstancerDefender.h"
38 #include "DecoratorInstancerStarfield.h"
39 #include "ElementGame.h"
40 #include "HighScores.h"
41
42 Rocket::Core::Context* context = NULL;
43
44 void DoAllocConsole();
45
46 ShellRenderInterfaceExtensions *shell_renderer;
47
GameLoop()48 void GameLoop()
49 {
50 context->Update();
51
52 shell_renderer->PrepareRenderBuffer();
53 context->Render();
54 shell_renderer->PresentRenderBuffer();
55 }
56
57 #if defined ROCKET_PLATFORM_WIN32
58 #include <windows.h>
WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER (instance_handle),HINSTANCE ROCKET_UNUSED_PARAMETER (previous_instance_handle),char * ROCKET_UNUSED_PARAMETER (command_line),int ROCKET_UNUSED_PARAMETER (command_show))59 int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTANCE ROCKET_UNUSED_PARAMETER(previous_instance_handle), char* ROCKET_UNUSED_PARAMETER(command_line), int ROCKET_UNUSED_PARAMETER(command_show))
60 #else
61 int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv))
62 #endif
63 {
64 #ifdef ROCKET_PLATFORM_WIN32
65 ROCKET_UNUSED(instance_handle);
66 ROCKET_UNUSED(previous_instance_handle);
67 ROCKET_UNUSED(command_line);
68 ROCKET_UNUSED(command_show);
69 #else
70 ROCKET_UNUSED(argc);
71 ROCKET_UNUSED(argv);
72 #endif
73
74 // @TODO Make these lookup at runtime rather than using hard coded paths
75 #ifdef ROCKET_PLATFORM_LINUX
76 #define APP_PATH "../Samples/pyinvaders/"
77 #define ROCKET_PATH ""
78 #else
79 #define APP_PATH "../../Samples/pyinvaders/"
80 #define ROCKET_PATH "."
81 #endif
82
83 #ifdef ROCKET_PLATFORM_WIN32
84 DoAllocConsole();
85 #endif
86
87 int window_width = 1024;
88 int window_height = 768;
89
90 ShellRenderInterfaceOpenGL opengl_renderer;
91 shell_renderer = &opengl_renderer;
92
93 // @TODO switch to using variable for window sizes
94 // Generic OS initialisation, creates a window and attaches OpenGL.
95 if (!Shell::Initialise(APP_PATH) ||
96 !Shell::OpenWindow("Rocket Invaders from Mars (Python Powered)", shell_renderer, window_width, window_height, false))
97 {
98 Shell::Shutdown();
99 return -1;
100 }
101
102 // Rocket initialisation.
103 Rocket::Core::SetRenderInterface(&opengl_renderer);
104 opengl_renderer.SetViewport(window_width, window_height);
105
106 ShellSystemInterface system_interface;
107 Rocket::Core::SetSystemInterface(&system_interface);
108
109 Rocket::Core::Initialise();
110 // Initialise the Rocket Controls library.
111 Rocket::Controls::Initialise();
112
113 // Initialise the Python interface.
114 PythonInterface::Initialise((Shell::GetExecutablePath() + (APP_PATH "python") + PATH_SEPARATOR + Shell::GetExecutablePath() + ROCKET_PATH).CString());
115
116 // Create the main Rocket context and set it on the shell's input layer.
117 context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(window_width, window_height));
118 if (context == NULL)
119 {
120 Rocket::Core::Shutdown();
121 Shell::Shutdown();
122 return -1;
123 }
124
125 Rocket::Debugger::Initialise(context);
126 Input::SetContext(context);
127 shell_renderer->SetContext(context);
128
129 // Load the font faces required for Invaders.
130 Shell::LoadFonts("../assets/");
131
132 // Register Invader's custom decorator instancers.
133 Rocket::Core::DecoratorInstancer* decorator_instancer = new DecoratorInstancerStarfield();
134 Rocket::Core::Factory::RegisterDecoratorInstancer("starfield", decorator_instancer);
135 decorator_instancer->RemoveReference();
136
137 decorator_instancer = new DecoratorInstancerDefender();
138 Rocket::Core::Factory::RegisterDecoratorInstancer("defender", decorator_instancer);
139 decorator_instancer->RemoveReference();
140
141 // Construct the game singletons.
142 HighScores::Initialise();
143
144 // Fire off the startup script.
145 PythonInterface::Import("autoexec");
146
147 Shell::EventLoop(GameLoop);
148
149 // Shutdown the Rocket contexts.
150 context->RemoveReference();
151
152 // Shutdown Python before we shut down Rocket.
153 PythonInterface::Shutdown();
154
155 // Shut down the game singletons.
156 HighScores::Shutdown();
157
158 // Shutdown Rocket.
159 Rocket::Core::Shutdown();
160
161 Shell::CloseWindow();
162 Shell::Shutdown();
163
164 return 0;
165 }
166
167 #ifdef ROCKET_PLATFORM_WIN32
168
169 #include <windows.h>
170 #include <fcntl.h>
171 #include <io.h>
172 #include <process.h>
173
DoAllocConsole()174 void DoAllocConsole()
175 {
176 static const WORD MAX_CONSOLE_LINES = 500;
177 int hConHandle;
178 long lStdHandle;
179 CONSOLE_SCREEN_BUFFER_INFO coninfo;
180 FILE *fp;
181
182 // allocate a console for this app
183 AllocConsole();
184
185 // set the screen buffer to be big enough to let us scroll text
186 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
187 coninfo.dwSize.Y = MAX_CONSOLE_LINES;
188 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
189
190 // redirect unbuffered STDOUT to the console
191 lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
192 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
193 fp = _fdopen( hConHandle, "w" );
194
195 *stdout = *fp;
196 setvbuf( stdout, NULL, _IONBF, 0 );
197
198 // redirect unbuffered STDIN to the console
199 lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
200 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
201 fp = _fdopen( hConHandle, "r" );
202
203 *stdin = *fp;
204 setvbuf( stdin, NULL, _IONBF, 0 );
205
206 // redirect unbuffered STDERR to the console
207 lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
208 hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
209 fp = _fdopen( hConHandle, "w" );
210 *stderr = *fp;
211
212 setvbuf( stderr, NULL, _IONBF, 0 );
213 ShowWindow(GetConsoleWindow(), SW_SHOW);
214 }
215 #endif
216