the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1// Minecraft.cpp : Defines the entry point for the application.
2//
3
4#include "stdafx.h"
5
6#include <assert.h>
7#include "strings.h"
8#include "4J_strings.h"
9#include "Minecraft_Controller_Actions.h"
10#include "Xbox_App.h"
11#include "GameConfig\Minecraft.spa.h"
12#include "..\MinecraftServer.h"
13#include "..\LocalPlayer.h"
14#include "..\..\Minecraft.World\ItemInstance.h"
15#include "..\..\Minecraft.World\MapItem.h"
16#include "..\..\Minecraft.World\Recipes.h"
17#include "..\..\Minecraft.World\Recipy.h"
18#include "..\..\Minecraft.World\Language.h"
19#include "..\..\Minecraft.World\StringHelpers.h"
20#include "..\..\Minecraft.World\AABB.h"
21#include "..\..\Minecraft.World\Vec3.h"
22#include "..\..\Minecraft.World\Level.h"
23
24#include "..\ClientConnection.h"
25#include "..\User.h"
26#include "..\..\Minecraft.World\Socket.h"
27#include "..\..\Minecraft.World\ThreadName.h"
28#include "..\..\Minecraft.Client\StatsCounter.h"
29#include "..\ConnectScreen.h"
30//#include "Social\SocialManager.h"
31//#include "Leaderboards\LeaderboardManager.h"
32//#include "XUI\XUI_Scene_Container.h"
33//#include "QNetManager.h"
34#include "..\..\Minecraft.Client\Tesselator.h"
35#include "Xbox_Awards_enum.h"
36#include "..\..\Minecraft.Client\Options.h"
37#include "Sentient\SentientManager.h"
38#include "..\..\Minecraft.World\IntCache.h"
39#include "..\Textures.h"
40#include "Resource.h"
41
42
43#define THEME_NAME "584111F70AAAAAAA"
44#define THEME_FILESIZE 2797568
45
46//#define THREE_MB 3145728 // minimum save size (checking for this on a selected device)
47//#define FIVE_MB 5242880 // minimum save size (checking for this on a selected device)
48//#define FIFTY_TWO_MB (1024*1024*52) // Maximum TCR space required for a save (checking for this on a selected device)
49#define FIFTY_ONE_MB (1000000*51) // Maximum TCR space required for a save is 52MB (checking for this on a selected device)
50
51#if 0
52//#define PROFILE_VERSION 3 // new version for the interim bug fix 166 TU
53#define NUM_PROFILE_VALUES 5
54#define NUM_PROFILE_SETTINGS 4
55DWORD dwProfileSettingsA[NUM_PROFILE_VALUES]=
56{
57 XPROFILE_OPTION_CONTROLLER_VIBRATION,
58 XPROFILE_GAMER_YAXIS_INVERSION,
59 XPROFILE_GAMER_CONTROL_SENSITIVITY,
60 XPROFILE_GAMER_ACTION_MOVEMENT_CONTROL,
61 XPROFILE_TITLE_SPECIFIC1,
62};
63#endif
64
65//-------------------------------------------------------------------------------------
66// Time Since fAppTime is a float, we need to keep the quadword app time
67// as a LARGE_INTEGER so that we don't lose precision after running
68// for a long time.
69//-------------------------------------------------------------------------------------
70
71
72BOOL g_bWidescreen = TRUE;
73
74
75void DefineActions(void)
76{
77#if 0
78 // The app needs to define the actions required, and the possible mappings for these
79
80 // Split into Menu actions, and in-game actions
81
82 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_A, _360_JOY_BUTTON_A);
83 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_B, _360_JOY_BUTTON_B);
84 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_X, _360_JOY_BUTTON_X);
85 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_Y, _360_JOY_BUTTON_Y);
86 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_OK, _360_JOY_BUTTON_A);
87 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_CANCEL, _360_JOY_BUTTON_B);
88 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_UP, _360_JOY_BUTTON_DPAD_UP | _360_JOY_BUTTON_LSTICK_UP);
89 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_DOWN, _360_JOY_BUTTON_DPAD_DOWN | _360_JOY_BUTTON_LSTICK_DOWN);
90 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_LEFT, _360_JOY_BUTTON_DPAD_LEFT | _360_JOY_BUTTON_LSTICK_LEFT);
91 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_RIGHT, _360_JOY_BUTTON_DPAD_RIGHT | _360_JOY_BUTTON_LSTICK_RIGHT);
92 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_PAGEUP, _360_JOY_BUTTON_LT);
93 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_PAGEDOWN, _360_JOY_BUTTON_RT);
94 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_RIGHT_SCROLL, _360_JOY_BUTTON_RB);
95 InputManager.SetGameJoypadMaps(MAP_STYLE_0,ACTION_MENU_LEFT_SCROLL, _360_JOY_BUTTON_LB);
96
97 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_JUMP, _360_JOY_BUTTON_A);
98 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_FORWARD, _360_JOY_BUTTON_LSTICK_UP);
99 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_BACKWARD, _360_JOY_BUTTON_LSTICK_DOWN);
100 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_LEFT, _360_JOY_BUTTON_LSTICK_LEFT);
101 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_RIGHT, _360_JOY_BUTTON_LSTICK_RIGHT);
102 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_LOOK_LEFT, _360_JOY_BUTTON_RSTICK_LEFT);
103 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_LOOK_RIGHT, _360_JOY_BUTTON_RSTICK_RIGHT);
104 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_LOOK_UP, _360_JOY_BUTTON_RSTICK_UP);
105 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_LOOK_DOWN, _360_JOY_BUTTON_RSTICK_DOWN);
106 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_USE, _360_JOY_BUTTON_LT);
107 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_ACTION, _360_JOY_BUTTON_RT);
108 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_RIGHT_SCROLL, _360_JOY_BUTTON_RB);
109 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_LEFT_SCROLL, _360_JOY_BUTTON_LB);
110 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_INVENTORY, _360_JOY_BUTTON_Y);
111 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_PAUSEMENU, _360_JOY_BUTTON_START);
112 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_DROP, _360_JOY_BUTTON_B);
113 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_SNEAK_TOGGLE, _360_JOY_BUTTON_RTHUMB);
114 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_CRAFTING, _360_JOY_BUTTON_X);
115 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_RENDER_THIRD_PERSON, _360_JOY_BUTTON_LTHUMB);
116 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_GAME_INFO, _360_JOY_BUTTON_BACK);
117
118 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_DPAD_LEFT, _360_JOY_BUTTON_DPAD_LEFT);
119 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_DPAD_RIGHT, _360_JOY_BUTTON_DPAD_RIGHT);
120 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_DPAD_UP, _360_JOY_BUTTON_DPAD_UP);
121 InputManager.SetGameJoypadMaps(MAP_STYLE_0,MINECRAFT_ACTION_DPAD_DOWN, _360_JOY_BUTTON_DPAD_DOWN);
122
123 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_A, _360_JOY_BUTTON_A);
124 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_B, _360_JOY_BUTTON_B);
125 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_X, _360_JOY_BUTTON_X);
126 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_Y, _360_JOY_BUTTON_Y);
127 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_OK, _360_JOY_BUTTON_A);
128 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_CANCEL, _360_JOY_BUTTON_B);
129 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_UP, _360_JOY_BUTTON_DPAD_UP | _360_JOY_BUTTON_LSTICK_UP);
130 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_DOWN, _360_JOY_BUTTON_DPAD_DOWN | _360_JOY_BUTTON_LSTICK_DOWN);
131 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_LEFT, _360_JOY_BUTTON_DPAD_LEFT | _360_JOY_BUTTON_LSTICK_LEFT);
132 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_RIGHT, _360_JOY_BUTTON_DPAD_RIGHT | _360_JOY_BUTTON_LSTICK_RIGHT);
133 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_PAGEUP, _360_JOY_BUTTON_LB);
134 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_PAGEDOWN, _360_JOY_BUTTON_RT);
135 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_RIGHT_SCROLL, _360_JOY_BUTTON_RB);
136 InputManager.SetGameJoypadMaps(MAP_STYLE_1,ACTION_MENU_LEFT_SCROLL, _360_JOY_BUTTON_LB);
137
138 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_JUMP, _360_JOY_BUTTON_RB);
139 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_FORWARD, _360_JOY_BUTTON_LSTICK_UP);
140 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_BACKWARD, _360_JOY_BUTTON_LSTICK_DOWN);
141 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_LEFT, _360_JOY_BUTTON_LSTICK_LEFT);
142 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_RIGHT, _360_JOY_BUTTON_LSTICK_RIGHT);
143 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_LOOK_LEFT, _360_JOY_BUTTON_RSTICK_LEFT);
144 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_LOOK_RIGHT, _360_JOY_BUTTON_RSTICK_RIGHT);
145 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_LOOK_UP, _360_JOY_BUTTON_RSTICK_UP);
146 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_LOOK_DOWN, _360_JOY_BUTTON_RSTICK_DOWN);
147 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_USE, _360_JOY_BUTTON_RT);
148 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_ACTION, _360_JOY_BUTTON_LT);
149 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_RIGHT_SCROLL, _360_JOY_BUTTON_DPAD_RIGHT);
150 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_LEFT_SCROLL, _360_JOY_BUTTON_DPAD_LEFT);
151 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_INVENTORY, _360_JOY_BUTTON_Y);
152 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_PAUSEMENU, _360_JOY_BUTTON_START);
153 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_DROP, _360_JOY_BUTTON_B);
154 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_SNEAK_TOGGLE, _360_JOY_BUTTON_LTHUMB);
155 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_CRAFTING, _360_JOY_BUTTON_X);
156 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_RENDER_THIRD_PERSON, _360_JOY_BUTTON_RTHUMB);
157 InputManager.SetGameJoypadMaps(MAP_STYLE_1,MINECRAFT_ACTION_GAME_INFO, _360_JOY_BUTTON_BACK);
158
159 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_A, _360_JOY_BUTTON_A);
160 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_B, _360_JOY_BUTTON_B);
161 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_X, _360_JOY_BUTTON_X);
162 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_Y, _360_JOY_BUTTON_Y);
163 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_OK, _360_JOY_BUTTON_A);
164 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_CANCEL, _360_JOY_BUTTON_B);
165 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_UP, _360_JOY_BUTTON_DPAD_UP | _360_JOY_BUTTON_LSTICK_UP);
166 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_DOWN, _360_JOY_BUTTON_DPAD_DOWN | _360_JOY_BUTTON_LSTICK_DOWN);
167 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_LEFT, _360_JOY_BUTTON_DPAD_LEFT | _360_JOY_BUTTON_LSTICK_LEFT);
168 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_RIGHT, _360_JOY_BUTTON_DPAD_RIGHT | _360_JOY_BUTTON_LSTICK_RIGHT);
169 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_PAGEUP, _360_JOY_BUTTON_DPAD_UP | _360_JOY_BUTTON_LB);
170 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_PAGEDOWN, _360_JOY_BUTTON_RT);
171 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_RIGHT_SCROLL, _360_JOY_BUTTON_RB);
172 InputManager.SetGameJoypadMaps(MAP_STYLE_2,ACTION_MENU_LEFT_SCROLL, _360_JOY_BUTTON_LB);
173
174 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_JUMP, _360_JOY_BUTTON_LT);
175 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_FORWARD, _360_JOY_BUTTON_LSTICK_UP);
176 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_BACKWARD, _360_JOY_BUTTON_LSTICK_DOWN);
177 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_LEFT, _360_JOY_BUTTON_LSTICK_LEFT);
178 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_RIGHT, _360_JOY_BUTTON_LSTICK_RIGHT);
179 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_LOOK_LEFT, _360_JOY_BUTTON_RSTICK_LEFT);
180 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_LOOK_RIGHT, _360_JOY_BUTTON_RSTICK_RIGHT);
181 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_LOOK_UP, _360_JOY_BUTTON_RSTICK_UP);
182 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_LOOK_DOWN, _360_JOY_BUTTON_RSTICK_DOWN);
183 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_USE, _360_JOY_BUTTON_RT);
184 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_ACTION, _360_JOY_BUTTON_A);
185 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_RIGHT_SCROLL, _360_JOY_BUTTON_DPAD_RIGHT);
186 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_LEFT_SCROLL, _360_JOY_BUTTON_DPAD_LEFT);
187 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_INVENTORY, _360_JOY_BUTTON_Y);
188 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_PAUSEMENU, _360_JOY_BUTTON_START);
189 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_DROP, _360_JOY_BUTTON_B);
190 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_SNEAK_TOGGLE, _360_JOY_BUTTON_LB);
191 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_CRAFTING, _360_JOY_BUTTON_X);
192 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_RENDER_THIRD_PERSON, _360_JOY_BUTTON_LTHUMB);
193 InputManager.SetGameJoypadMaps(MAP_STYLE_2,MINECRAFT_ACTION_GAME_INFO, _360_JOY_BUTTON_BACK);
194#endif
195}
196
197#if 0
198HRESULT InitD3D( IDirect3DDevice9 **ppDevice,
199 D3DPRESENT_PARAMETERS *pd3dPP )
200{
201 IDirect3D9 *pD3D;
202
203 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
204
205 // Set up the structure used to create the D3DDevice
206 // Using a permanent 1280x720 backbuffer now no matter what the actual video resolution.right Have also disabled letterboxing,
207 // which would letterbox a 1280x720 output if it detected a 4:3 video source - we're doing an anamorphic squash in this
208 // mode so don't need this functionality.
209
210 ZeroMemory( pd3dPP, sizeof(D3DPRESENT_PARAMETERS) );
211 XVIDEO_MODE VideoMode;
212 XGetVideoMode( &VideoMode );
213 g_bWidescreen = VideoMode.fIsWideScreen;
214 pd3dPP->BackBufferWidth = 1280;
215 pd3dPP->BackBufferHeight = 720;
216 pd3dPP->BackBufferFormat = D3DFMT_A8R8G8B8;
217 pd3dPP->BackBufferCount = 1;
218 pd3dPP->EnableAutoDepthStencil = TRUE;
219 pd3dPP->AutoDepthStencilFormat = D3DFMT_D24S8;
220 pd3dPP->SwapEffect = D3DSWAPEFFECT_DISCARD;
221 pd3dPP->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
222 //pd3dPP->Flags = D3DPRESENTFLAG_NO_LETTERBOX;
223 //ERR[D3D]: Can't set D3DPRESENTFLAG_NO_LETTERBOX when wide-screen is enabled
224 // in the launcher/dashboard.
225 if(g_bWidescreen)
226 pd3dPP->Flags=0;
227 else
228 pd3dPP->Flags = D3DPRESENTFLAG_NO_LETTERBOX;
229
230 // Create the device.
231 return pD3D->CreateDevice(
232 0,
233 D3DDEVTYPE_HAL,
234 NULL,
235 D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_BUFFER_2_FRAMES,
236 pd3dPP,
237 ppDevice );
238}
239#endif
240//#define MEMORY_TRACKING
241
242#ifdef MEMORY_TRACKING
243void ResetMem();
244void DumpMem();
245void MemPixStuff();
246#else
247void MemSect(int sect)
248{
249}
250#endif
251
252HINSTANCE g_hInst = NULL;
253HWND g_hWnd = NULL;
254D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL;
255D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;
256ID3D11Device* g_pd3dDevice = NULL;
257ID3D11DeviceContext* g_pImmediateContext = NULL;
258IDXGISwapChain* g_pSwapChain = NULL;
259ID3D11RenderTargetView* g_pRenderTargetView = NULL;
260
261//
262// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
263//
264// PURPOSE: Processes messages for the main window.
265//
266// WM_COMMAND - process the application menu
267// WM_PAINT - Paint the main window
268// WM_DESTROY - post a quit message and return
269//
270//
271LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
272{
273 int wmId, wmEvent;
274 PAINTSTRUCT ps;
275 HDC hdc;
276
277 switch (message)
278 {
279 case WM_COMMAND:
280 wmId = LOWORD(wParam);
281 wmEvent = HIWORD(wParam);
282 // Parse the menu selections:
283 switch (wmId)
284 {
285 case IDM_EXIT:
286 DestroyWindow(hWnd);
287 break;
288 default:
289 return DefWindowProc(hWnd, message, wParam, lParam);
290 }
291 break;
292 case WM_PAINT:
293 hdc = BeginPaint(hWnd, &ps);
294 // TODO: Add any drawing code here...
295 EndPaint(hWnd, &ps);
296 break;
297 case WM_DESTROY:
298 PostQuitMessage(0);
299 break;
300 default:
301 return DefWindowProc(hWnd, message, wParam, lParam);
302 }
303 return 0;
304}
305
306
307//
308// FUNCTION: MyRegisterClass()
309//
310// PURPOSE: Registers the window class.
311//
312ATOM MyRegisterClass(HINSTANCE hInstance)
313{
314 WNDCLASSEX wcex;
315
316 wcex.cbSize = sizeof(WNDCLASSEX);
317
318 wcex.style = CS_HREDRAW | CS_VREDRAW;
319 wcex.lpfnWndProc = WndProc;
320 wcex.cbClsExtra = 0;
321 wcex.cbWndExtra = 0;
322 wcex.hInstance = hInstance;
323 wcex.hIcon = LoadIcon(hInstance, "Minecraft");
324 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
325 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
326 wcex.lpszMenuName = "Minecraft";
327 wcex.lpszClassName = "MinecraftClass";
328 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
329
330 return RegisterClassEx(&wcex);
331}
332
333//
334// FUNCTION: InitInstance(HINSTANCE, int)
335//
336// PURPOSE: Saves instance handle and creates main window
337//
338// COMMENTS:
339//
340// In this function, we save the instance handle in a global variable and
341// create and display the main program window.
342//
343BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
344{
345 g_hInst = hInstance; // Store instance handle in our global variable
346
347 g_hWnd = CreateWindow("MinecraftClass", "Minecraft", WS_OVERLAPPEDWINDOW,
348 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
349
350 if (!g_hWnd)
351 {
352 return FALSE;
353 }
354
355 ShowWindow(g_hWnd, nCmdShow);
356 UpdateWindow(g_hWnd);
357
358 return TRUE;
359}
360
361
362//--------------------------------------------------------------------------------------
363// Create Direct3D device and swap chain
364//--------------------------------------------------------------------------------------
365HRESULT InitDevice()
366{
367 HRESULT hr = S_OK;
368
369 RECT rc;
370 GetClientRect( g_hWnd, &rc );
371 UINT width = rc.right - rc.left;
372 UINT height = rc.bottom - rc.top;
373
374 UINT createDeviceFlags = 0;
375#ifdef _DEBUG
376 createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
377#endif
378
379 D3D_DRIVER_TYPE driverTypes[] =
380 {
381 D3D_DRIVER_TYPE_HARDWARE,
382 D3D_DRIVER_TYPE_WARP,
383 D3D_DRIVER_TYPE_REFERENCE,
384 };
385 UINT numDriverTypes = ARRAYSIZE( driverTypes );
386
387 D3D_FEATURE_LEVEL featureLevels[] =
388 {
389 D3D_FEATURE_LEVEL_11_0,
390 D3D_FEATURE_LEVEL_10_1,
391 D3D_FEATURE_LEVEL_10_0,
392 };
393 UINT numFeatureLevels = ARRAYSIZE( featureLevels );
394
395 DXGI_SWAP_CHAIN_DESC sd;
396 ZeroMemory( &sd, sizeof( sd ) );
397 sd.BufferCount = 1;
398 sd.BufferDesc.Width = width;
399 sd.BufferDesc.Height = height;
400 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
401 sd.BufferDesc.RefreshRate.Numerator = 60;
402 sd.BufferDesc.RefreshRate.Denominator = 1;
403 sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
404 sd.OutputWindow = g_hWnd;
405 sd.SampleDesc.Count = 1;
406 sd.SampleDesc.Quality = 0;
407 sd.Windowed = TRUE;
408
409 for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
410 {
411 g_driverType = driverTypes[driverTypeIndex];
412 hr = D3D11CreateDeviceAndSwapChain( NULL, g_driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels,
413 D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );
414 if( SUCCEEDED( hr ) )
415 break;
416 }
417 if( FAILED( hr ) )
418 return hr;
419
420 // Create a render target view
421 ID3D11Texture2D* pBackBuffer = NULL;
422 hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
423 if( FAILED( hr ) )
424 return hr;
425
426 hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
427 pBackBuffer->Release();
428 if( FAILED( hr ) )
429 return hr;
430
431 g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );
432
433 // Setup the viewport
434 D3D11_VIEWPORT vp;
435 vp.Width = (FLOAT)width;
436 vp.Height = (FLOAT)height;
437 vp.MinDepth = 0.0f;
438 vp.MaxDepth = 1.0f;
439 vp.TopLeftX = 0;
440 vp.TopLeftY = 0;
441 g_pImmediateContext->RSSetViewports( 1, &vp );
442
443 RenderManager.Initialise(g_pd3dDevice, g_pSwapChain);
444
445 return S_OK;
446}
447
448
449//--------------------------------------------------------------------------------------
450// Render the frame
451//--------------------------------------------------------------------------------------
452void Render()
453{
454 // Just clear the backbuffer
455 float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; //red,green,blue,alpha
456
457 g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, ClearColor );
458 g_pSwapChain->Present( 0, 0 );
459}
460
461
462//--------------------------------------------------------------------------------------
463// Clean up the objects we've created
464//--------------------------------------------------------------------------------------
465void CleanupDevice()
466{
467 if( g_pImmediateContext ) g_pImmediateContext->ClearState();
468
469 if( g_pRenderTargetView ) g_pRenderTargetView->Release();
470 if( g_pSwapChain ) g_pSwapChain->Release();
471 if( g_pImmediateContext ) g_pImmediateContext->Release();
472 if( g_pd3dDevice ) g_pd3dDevice->Release();
473}
474
475int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
476 _In_opt_ HINSTANCE hPrevInstance,
477 _In_ LPTSTR lpCmdLine,
478 _In_ int nCmdShow)
479{
480 UNREFERENCED_PARAMETER(hPrevInstance);
481 UNREFERENCED_PARAMETER(lpCmdLine);
482
483 // Initialize global strings
484 MyRegisterClass(hInstance);
485
486 // Perform application initialization:
487 if (!InitInstance (hInstance, nCmdShow))
488 {
489 return FALSE;
490 }
491
492 if( FAILED( InitDevice() ) )
493 {
494 CleanupDevice();
495 return 0;
496 }
497
498#if 0
499 // Main message loop
500 MSG msg = {0};
501 while( WM_QUIT != msg.message )
502 {
503 if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
504 {
505 TranslateMessage( &msg );
506 DispatchMessage( &msg );
507 }
508 else
509 {
510 Render();
511 }
512 }
513
514 return (int) msg.wParam;
515#endif
516
517 static bool bTrialTimerDisplayed=true;
518
519#ifdef MEMORY_TRACKING
520 ResetMem();
521 MEMORYSTATUS memStat;
522 GlobalMemoryStatus(&memStat);
523 printf("RESETMEM start: Avail. phys %d\n",memStat.dwAvailPhys/(1024*1024));
524#endif
525
526#if 0
527 // Initialize D3D
528 hr = InitD3D( &pDevice, &d3dpp );
529 g_pD3DDevice = pDevice;
530 if( FAILED(hr) )
531 {
532 app.DebugPrintf
533 ( "Failed initializing D3D.\n" );
534 return -1;
535 }
536
537 // Initialize the application, assuming sharing of the d3d interface.
538 hr = app.InitShared( pDevice, &d3dpp,
539 XuiPNGTextureLoader );
540
541 if ( FAILED(hr) )
542 {
543 app.DebugPrintf
544 ( "Failed initializing application.\n" );
545
546 return -1;
547 }
548
549
550#endif
551 RenderManager.Initialise(g_pd3dDevice, g_pSwapChain);
552
553 ////////////////
554 // Initialise //
555 ////////////////
556
557#if 0
558 // 4J Stu - XACT was creating these automatically, but we need them for QNet. The setup params
559 // are just copied from a sample app and may need changed for our purposes
560 // Start XAudio2
561 hr = XAudio2Create( &g_pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR );
562 if( FAILED( hr ) )
563 {
564 app.DebugPrintf( "Initializing XAudio2 failed (err = 0x%08x)!\n", hr );
565 app.FatalLoadError();
566 }
567
568 // Create an XAudio2 mastering voice (utilized by XHV2 when voice data is mixed to main speakers)
569 hr = g_pXAudio2->CreateMasteringVoice(&g_pXAudio2MasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, 0, NULL);
570 if ( FAILED( hr ) )
571 {
572 app.DebugPrintf( "Creating XAudio2 mastering voice failed (err = 0x%08x)!\n", hr );
573 app.FatalLoadError();
574 }
575
576 app.InitTime();
577
578 // Set the number of possible joypad layouts that the user can switch between, and the number of actions
579 InputManager.Initialise(1,3,64);
580
581 // Set the default joypad action mappings for Minecraft
582 DefineActions();
583 InputManager.SetJoypadMapVal(0,0);
584 InputManager.SetKeyRepeatRate(0.3f,0.2f);
585
586
587 // Initialise the profile manager with the game Title ID, Offer ID, a profile version number, and the number of profile values and settings
588 ProfileManager.Initialise(TITLEID_MINECRAFT,
589 app.m_dwOfferID,
590 PROFILE_VERSION_BUILD_TU7,
591 NUM_PROFILE_VALUES,
592 NUM_PROFILE_SETTINGS,
593 dwProfileSettingsA,
594 app.GAME_DEFINED_PROFILE_DATA_BYTES*XUSER_MAX_COUNT,
595 &app.uiGameDefinedDataChangedBitmask
596 );
597
598 // register the awards
599 ProfileManager.RegisterAward(eAward_TakingInventory, ACHIEVEMENT_01, eAwardType_Achievement);
600 ProfileManager.RegisterAward(eAward_GettingWood, ACHIEVEMENT_02, eAwardType_Achievement);
601 ProfileManager.RegisterAward(eAward_Benchmarking, ACHIEVEMENT_03, eAwardType_Achievement);
602 ProfileManager.RegisterAward(eAward_TimeToMine, ACHIEVEMENT_04, eAwardType_Achievement);
603 ProfileManager.RegisterAward(eAward_HotTopic, ACHIEVEMENT_05, eAwardType_Achievement);
604 ProfileManager.RegisterAward(eAward_AquireHardware, ACHIEVEMENT_06, eAwardType_Achievement);
605 ProfileManager.RegisterAward(eAward_TimeToFarm, ACHIEVEMENT_07, eAwardType_Achievement);
606 ProfileManager.RegisterAward(eAward_BakeBread, ACHIEVEMENT_08, eAwardType_Achievement);
607 ProfileManager.RegisterAward(eAward_TheLie, ACHIEVEMENT_09, eAwardType_Achievement);
608 ProfileManager.RegisterAward(eAward_GettingAnUpgrade, ACHIEVEMENT_10, eAwardType_Achievement);
609 ProfileManager.RegisterAward(eAward_DeliciousFish, ACHIEVEMENT_11, eAwardType_Achievement);
610 ProfileManager.RegisterAward(eAward_OnARail, ACHIEVEMENT_12, eAwardType_Achievement);
611 ProfileManager.RegisterAward(eAward_TimeToStrike, ACHIEVEMENT_13, eAwardType_Achievement);
612 ProfileManager.RegisterAward(eAward_MonsterHunter, ACHIEVEMENT_14, eAwardType_Achievement);
613 ProfileManager.RegisterAward(eAward_CowTipper, ACHIEVEMENT_15, eAwardType_Achievement);
614 ProfileManager.RegisterAward(eAward_WhenPigsFly, ACHIEVEMENT_16, eAwardType_Achievement);
615 ProfileManager.RegisterAward(eAward_LeaderOfThePack, ACHIEVEMENT_17, eAwardType_Achievement);
616 ProfileManager.RegisterAward(eAward_MOARTools, ACHIEVEMENT_18, eAwardType_Achievement);
617 ProfileManager.RegisterAward(eAward_DispenseWithThis, ACHIEVEMENT_19, eAwardType_Achievement);
618 ProfileManager.RegisterAward(eAward_InToTheNether, ACHIEVEMENT_20, eAwardType_Achievement);
619
620 ProfileManager.RegisterAward(eAward_mine100Blocks, GAMER_PICTURE_GAMERPIC1, eAwardType_GamerPic,false,app.GetStringTable(),IDS_AWARD_TITLE,IDS_AWARD_GAMERPIC1,IDS_CONFIRM_OK);
621 ProfileManager.RegisterAward(eAward_kill10Creepers, GAMER_PICTURE_GAMERPIC2, eAwardType_GamerPic,false,app.GetStringTable(),IDS_AWARD_TITLE,IDS_AWARD_GAMERPIC2,IDS_CONFIRM_OK);
622
623 ProfileManager.RegisterAward(eAward_eatPorkChop, AVATARASSETAWARD_PORKCHOP_TSHIRT, eAwardType_AvatarItem,false,app.GetStringTable(),IDS_AWARD_TITLE,IDS_AWARD_AVATAR1,IDS_CONFIRM_OK);
624 ProfileManager.RegisterAward(eAward_play100Days, AVATARASSETAWARD_WATCH, eAwardType_AvatarItem,false,app.GetStringTable(),IDS_AWARD_TITLE,IDS_AWARD_AVATAR2,IDS_CONFIRM_OK);
625 ProfileManager.RegisterAward(eAward_arrowKillCreeper, AVATARASSETAWARD_CAP, eAwardType_AvatarItem,false,app.GetStringTable(),IDS_AWARD_TITLE,IDS_AWARD_AVATAR3,IDS_CONFIRM_OK);
626
627 ProfileManager.RegisterAward(eAward_socialPost, 0, eAwardType_Theme,false,app.GetStringTable(),IDS_AWARD_TITLE,IDS_AWARD_THEME,IDS_CONFIRM_OK,THEME_NAME,THEME_FILESIZE);
628
629 // Rich Presence init - number of presences, number of contexts
630 ProfileManager.RichPresenceInit(4,1);
631 ProfileManager.RegisterRichPresenceContext(CONTEXT_GAME_STATE);
632
633 app.LoadXuiResources();
634
635 // initialise the storage manager with a default save display name, a Minimum save size, and a callback for displaying the saving message
636 StorageManager.Init(app.GetString(IDS_DEFAULT_SAVENAME),"savegame.dat",FIFTY_ONE_MB,&CXboxMinecraftApp::DisplaySavingMessage,(LPVOID)&app);
637 // Set up the global title storage path
638 StorageManager.StoreTMSPathName();
639
640 // set a function to be called when there's a sign in change, so we can exit a level if the primary player signs out
641 ProfileManager.SetSignInChangeCallback(&CXboxMinecraftApp::SignInChangeCallback,(LPVOID)&app);
642
643 // set a function to be called when the ethernet is disconnected, so we can back out if required
644 ProfileManager.SetNotificationsCallback(&CXboxMinecraftApp::NotificationsCallback,(LPVOID)&app);
645
646 // Set a callback for the default player options to be set - when there is no profile data for the player
647 ProfileManager.SetDefaultOptionsCallback(&CXboxMinecraftApp::DefaultOptionsCallback,(LPVOID)&app);
648 // Set a callback to deal with old profile versions needing updated to new versions
649 ProfileManager.SetOldProfileVersionCallback(&CXboxMinecraftApp::OldProfileVersionCallback,(LPVOID)&app);
650
651 // Set a callback for when there is a read error on profile data
652 ProfileManager.SetProfileReadErrorCallback(&CXboxMinecraftApp::ProfileReadErrorCallback,(LPVOID)&app);
653
654
655 // QNet needs to be setup after profile manager, as we do not want its Notify listener to handle
656 // XN_SYS_SIGNINCHANGED notifications. This does mean that we need to have a callback in the
657 // ProfileManager for XN_LIVE_INVITE_ACCEPTED for QNet.
658 g_qNetManager.Initialise();
659
660 app.InitGameSettings();
661
662 // debug switch to trial version
663 ProfileManager.SetDebugFullOverride(true);
664
665 //ProfileManager.AddDLC(2);
666 StorageManager.SetDLCPackageRoot("DLCDrive");
667 StorageManager.RegisterMarketplaceCountsCallback(&CXboxMinecraftApp::MarketplaceCountsCallback,(LPVOID)&app);
668 // Kinect !
669
670 if(XNuiGetHardwareStatus()!=0)
671 {
672 // If the Kinect Sensor is not physically connected, this function returns 0.
673 NuiInitialize(NUI_INITIALIZE_FLAG_USES_HIGH_QUALITY_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH |
674 NUI_INITIALIZE_FLAG_EXTRAPOLATE_FLOOR_PLANE | NUI_INITIALIZE_FLAG_USES_FITNESS | NUI_INITIALIZE_FLAG_NUI_GUIDE_DISABLED | NUI_INITIALIZE_FLAG_SUPPRESS_AUTOMATIC_UI,NUI_INITIALIZE_DEFAULT_HARDWARE_THREAD );
675 }
676
677 // Sentient !
678 hr = SentientManager.Init();
679
680
681#endif
682 // Initialise TLS for tesselator, for this main thread
683 Tesselator::CreateNewThreadStorage(1024*1024);
684 // Initialise TLS for AABB and Vec3 pools, for this main thread
685 AABB::CreateNewThreadStorage();
686 Vec3::CreateNewThreadStorage();
687 IntCache::CreateNewThreadStorage();
688 Level::enableLightingCache();
689
690
691 Minecraft::main();
692 Minecraft *pMinecraft=Minecraft::GetInstance();
693#if 0
694 //bool bDisplayPauseMenu=false;
695
696 // set the default gamma level
697 float fVal=50.0f*327.68f;
698 RenderManager.UpdateGamma((unsigned short)fVal);
699
700 // load any skins
701 //app.AddSkinsToMemoryTextureFiles();
702
703 // set the achievement text for a trial achievement, now we have the string table loaded
704 ProfileManager.SetTrialTextStringTable(app.GetStringTable(),IDS_CONFIRM_OK, IDS_CONFIRM_CANCEL);
705 ProfileManager.SetTrialAwardText(eAwardType_Achievement,IDS_UNLOCK_TITLE,IDS_UNLOCK_ACHIEVEMENT_TEXT);
706 ProfileManager.SetTrialAwardText(eAwardType_GamerPic,IDS_UNLOCK_TITLE,IDS_UNLOCK_GAMERPIC_TEXT);
707 ProfileManager.SetTrialAwardText(eAwardType_AvatarItem,IDS_UNLOCK_TITLE,IDS_UNLOCK_AVATAR_TEXT);
708 ProfileManager.SetTrialAwardText(eAwardType_Theme,IDS_UNLOCK_TITLE,IDS_UNLOCK_THEME_TEXT);
709 ProfileManager.SetUpsellCallback(&app.UpsellReturnedCallback,&app);
710
711 // Set up a debug character press sequence
712#ifndef _FINAL_BUILD
713 app.SetDebugSequence("LRLRYYY");
714#endif
715
716 // Initialise the social networking manager.
717 CSocialManager::Instance()->Initialise();
718
719 // Update the base scene quick selects now that the minecraft class exists
720 //CXuiSceneBase::UpdateScreenSettings(0);
721
722 app.InitialiseTips();
723
724 DWORD initData=0;
725
726
727
728#ifndef _FINAL_BUILD
729#ifndef _DEBUG
730 #pragma message(__LOC__"Need to define the _FINAL_BUILD before submission")
731#endif
732#endif
733
734 pMinecraft->soundEngine->init(NULL);
735 // Set the default sound levels
736 pMinecraft->options->set(Options::Option::MUSIC,1.0f);
737 pMinecraft->options->set(Options::Option::SOUND,1.0f);
738
739 app.NavigateToScene(XUSER_INDEX_ANY,CXboxMinecraftApp::e_xuiScene_Intro,&initData);
740#endif
741
742 //Sleep(10000);
743#if 0
744 // Intro loop ?
745 while(app.IntroRunning())
746 {
747 ProfileManager.Tick();
748 // Tick XUI
749 app.RunFrame();
750
751 // 4J : WESTY : Added to ensure we always have clear background for intro.
752 RenderManager.SetClearColour(D3DCOLOR_RGBA(0,0,0,255));
753 RenderManager.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
754
755 // Render XUI
756 hr = app.Render();
757
758 // Present the frame.
759 RenderManager.Present();
760
761 // Update XUI Timers
762 hr = XuiTimersRun();
763 }
764#endif
765
766 while( TRUE )
767 {
768#if 0
769 if(pMinecraft->soundEngine->isStreamingWavebankReady() &&
770 !pMinecraft->soundEngine->isPlayingStreamingGameMusic() &&
771 !pMinecraft->soundEngine->isPlayingStreamingCDMusic() )
772 {
773 // play some music in the menus
774 pMinecraft->soundEngine->playStreaming(L"", 0, 0, 0, 0, 0, false);
775 }
776#endif
777 app.UpdateTime();
778 PIXBeginNamedEvent(0,"Input manager tick");
779// InputManager.Tick();
780 PIXEndNamedEvent();
781 PIXBeginNamedEvent(0,"Profile manager tick");
782// ProfileManager.Tick();
783 PIXEndNamedEvent();
784 PIXBeginNamedEvent(0,"Storage manager tick");
785// StorageManager.Tick();
786 PIXEndNamedEvent();
787 PIXBeginNamedEvent(0,"Render manager tick");
788 RenderManager.Tick();
789 PIXEndNamedEvent();
790
791 // Tick the social networking manager.
792 PIXBeginNamedEvent(0,"Social network manager tick");
793// CSocialManager::Instance()->Tick();
794 PIXEndNamedEvent();
795
796 // Tick sentient.
797 PIXBeginNamedEvent(0,"Sentient tick");
798 MemSect(37);
799// SentientManager.Tick();
800 MemSect(0);
801 PIXEndNamedEvent();
802
803 PIXBeginNamedEvent(0,"Qnet do work #1");
804// g_qNetManager.DoWork();
805 PIXEndNamedEvent();
806
807// LeaderboardManager::Instance()->Tick();
808 // Render game graphics.
809 if(app.GetGameStarted())
810 {
811 pMinecraft->run_middle();
812 app.SetAppPaused( g_qNetManager.IsLocalGame() && g_qNetManager.GetPlayerCount() == 1 && app.IsPauseMenuDisplayed(ProfileManager.GetPrimaryPad()) );
813 }
814 else
815 {
816 MemSect(28);
817 pMinecraft->soundEngine->update(NULL, 0.0f);
818 MemSect(0);
819 pMinecraft->soundEngine->playMusicTick();
820 pMinecraft->textures->tick(true,false);
821 IntCache::Reset();
822 app.SetGameStarted(true);
823 }
824
825 app.ToggleDimensionIfRequested();
826
827
828#ifdef MEMORY_TRACKING
829 static bool bResetMemTrack = false;
830 static bool bDumpMemTrack = false;
831
832 MemPixStuff();
833
834 if( bResetMemTrack )
835 {
836 ResetMem();
837 MEMORYSTATUS memStat;
838 GlobalMemoryStatus(&memStat);
839 printf("RESETMEM: Avail. phys %d\n",memStat.dwAvailPhys/(1024*1024));
840 bResetMemTrack = false;
841 }
842
843 if( bDumpMemTrack )
844 {
845 DumpMem();
846 bDumpMemTrack = false;
847 MEMORYSTATUS memStat;
848 GlobalMemoryStatus(&memStat);
849 printf("DUMPMEM: Avail. phys %d\n",memStat.dwAvailPhys/(1024*1024));
850 printf("Renderer used: %d\n",RenderManager.CBuffSize(-1));
851 }
852#endif
853#if 0
854 static bool bDumpTextureUsage = false;
855 if( bDumpTextureUsage )
856 {
857 RenderManager.TextureGetStats();
858 bDumpTextureUsage = false;
859 }
860#endif
861
862#if 0
863 app.HandleButtonPresses();
864
865 // store the minecraft renderstates, and re-set them after the xui render
866 GetRenderAndSamplerStates(pDevice,RenderStateA,SamplerStateA);
867
868 // Tick XUI
869 PIXBeginNamedEvent(0,"Xui running");
870 app.RunFrame();
871 PIXEndNamedEvent();
872
873 // Render XUI
874
875 PIXBeginNamedEvent(0,"XUI render");
876 MemSect(7);
877 hr = app.Render();
878 MemSect(0);
879 GetRenderAndSamplerStates(pDevice,RenderStateA2,SamplerStateA2);
880 PIXEndNamedEvent();
881
882 for(int i=0;i<8;i++)
883 {
884 if(RenderStateA2[i]!=RenderStateA[i])
885 {
886 //printf("Reseting RenderStateA[%d] after a XUI render\n",i);
887 pDevice->SetRenderState(RenderStateModes[i],RenderStateA[i]);
888 }
889 }
890 for(int i=0;i<5;i++)
891 {
892 if(SamplerStateA2[i]!=SamplerStateA[i])
893 {
894 //printf("Reseting SamplerStateA[%d] after a XUI render\n",i);
895 pDevice->SetSamplerState(0,SamplerStateModes[i],SamplerStateA[i]);
896 }
897 }
898
899 RenderManager.Set_matrixDirty();
900#endif
901 // Present the frame.
902 PIXBeginNamedEvent(0,"Frame present");
903 RenderManager.Present();
904 PIXEndNamedEvent();
905#if 0
906 app.CheckMenuDisplayed();
907 PIXBeginNamedEvent(0,"Profile load check");
908 // has the game defined profile data been changed (by a profile load)
909 if(app.uiGameDefinedDataChangedBitmask!=0)
910 {
911 void *pData;
912 for(int i=0;i<XUSER_MAX_COUNT;i++)
913 {
914 if(app.uiGameDefinedDataChangedBitmask&(1<<i))
915 {
916 // It has - game needs to update its values with the data from the profile
917 pData=ProfileManager.GetGameDefinedProfileData(i);
918 // reset the changed flag
919 app.ClearGameSettingsChangedFlag(i);
920 app.DebugPrintf("*** - APPLYING GAME SETTINGS CHANGE for pad %d\n",i);
921 app.ApplyGameSettingsChanged(i);
922
923#ifdef _DEBUG_MENUS_ENABLED
924 if(app.DebugSettingsOn())
925 {
926 app.ActionDebugMask(i);
927 }
928 else
929 {
930 // force debug mask off
931 app.ActionDebugMask(i,true);
932 }
933#endif
934 // clear the stats first - there could have beena signout and sign back in in the menus
935 // need to clear the player stats - can't assume it'll be done in setlevel - we may not be in the game
936 pMinecraft->stats[ i ]->clear();
937 pMinecraft->stats[i]->parse(pData);
938 }
939 }
940
941 // Check to see if we can post to social networks.
942 CSocialManager::Instance()->RefreshPostingCapability();
943
944 // clear the flag
945 app.uiGameDefinedDataChangedBitmask=0;
946
947 // Check if any profile write are needed
948 app.CheckGameSettingsChanged();
949
950 }
951 PIXEndNamedEvent();
952 app.TickDLCOffersRetrieved();
953 app.TickTMSImagesRetrieved();
954
955 PIXBeginNamedEvent(0,"Qnet do work #2");
956 g_qNetManager.DoWork();
957 PIXEndNamedEvent();
958
959 PIXBeginNamedEvent(0,"Misc extra xui");
960 // Update XUI Timers
961 hr = XuiTimersRun();
962
963 // Any threading type things to deal with from the xui side?
964 app.HandleXuiActions();
965 PIXEndNamedEvent();
966
967 // 4J-PB - Update the trial timer display if we are in the trial version
968 if(!ProfileManager.IsFullVersion())
969 {
970 // display the trial timer
971 if(app.GetGameStarted())
972 {
973 // 4J-PB - if the game is paused, add the elapsed time to the trial timer count so it doesn't tick down
974 if(app.IsAppPaused())
975 {
976 app.UpdateTrialPausedTimer();
977 }
978 CXuiSceneBase::UpdateTrialTimer(ProfileManager.GetPrimaryPad());
979 }
980 }
981 else
982 {
983 // need to turn off the trial timer if it was on , and we've unlocked the full version
984 if(bTrialTimerDisplayed)
985 {
986 CXuiSceneBase::ShowTrialTimer(FALSE);
987 bTrialTimerDisplayed=false;
988 }
989 }
990#endif
991
992 // Fix for #7318 - Title crashes after short soak in the leaderboards menu
993 // A memory leak was caused because the icon renderer kept creating new Vec3's because the pool wasn't reset
994 Vec3::resetPool();
995 }
996
997 // Free resources, unregister custom classes, and exit.
998// app.Uninit();
999 g_pd3dDevice->Release();
1000}
1001
1002
1003#ifdef MEMORY_TRACKING
1004
1005int totalAllocGen = 0;
1006unordered_map<int,int> allocCounts;
1007bool trackEnable = false;
1008bool trackStarted = false;
1009volatile size_t sizeCheckMin = 1160;
1010volatile size_t sizeCheckMax = 1160;
1011volatile int sectCheck = 48;
1012CRITICAL_SECTION memCS;
1013DWORD tlsIdx;
1014
1015LPVOID XMemAlloc(SIZE_T dwSize, DWORD dwAllocAttributes)
1016{
1017 if( !trackStarted )
1018 {
1019 void *p = XMemAllocDefault(dwSize,dwAllocAttributes);
1020 size_t realSize = XMemSizeDefault(p, dwAllocAttributes);
1021 totalAllocGen += realSize;
1022 return p;
1023 }
1024
1025 EnterCriticalSection(&memCS);
1026
1027 void *p=XMemAllocDefault(dwSize + 16,dwAllocAttributes);
1028 size_t realSize = XMemSizeDefault(p,dwAllocAttributes) - 16;
1029
1030 if( trackEnable )
1031 {
1032#if 1
1033 int sect = ((int) TlsGetValue(tlsIdx)) & 0x3f;
1034 *(((unsigned char *)p)+realSize) = sect;
1035
1036 if( ( realSize >= sizeCheckMin ) && ( realSize <= sizeCheckMax ) && ( ( sect == sectCheck ) || ( sectCheck == -1 ) ) )
1037 {
1038 app.DebugPrintf("Found one\n");
1039 }
1040#endif
1041
1042 if( p )
1043 {
1044 totalAllocGen += realSize;
1045 trackEnable = false;
1046 int key = ( sect << 26 ) | realSize;
1047 int oldCount = allocCounts[key];
1048 allocCounts[key] = oldCount + 1;
1049
1050 trackEnable = true;
1051 }
1052 }
1053
1054 LeaveCriticalSection(&memCS);
1055
1056 return p;
1057}
1058
1059void* operator new (size_t size)
1060{
1061 return (unsigned char *)XMemAlloc(size,MAKE_XALLOC_ATTRIBUTES(0,FALSE,TRUE,FALSE,0,XALLOC_PHYSICAL_ALIGNMENT_DEFAULT,XALLOC_MEMPROTECT_READWRITE,FALSE,XALLOC_MEMTYPE_HEAP));
1062}
1063
1064void operator delete (void *p)
1065{
1066 XMemFree(p,MAKE_XALLOC_ATTRIBUTES(0,FALSE,TRUE,FALSE,0,XALLOC_PHYSICAL_ALIGNMENT_DEFAULT,XALLOC_MEMPROTECT_READWRITE,FALSE,XALLOC_MEMTYPE_HEAP));
1067}
1068
1069void WINAPI XMemFree(PVOID pAddress, DWORD dwAllocAttributes)
1070{
1071 bool special = false;
1072 if( dwAllocAttributes == 0 )
1073 {
1074 dwAllocAttributes = MAKE_XALLOC_ATTRIBUTES(0,FALSE,TRUE,FALSE,0,XALLOC_PHYSICAL_ALIGNMENT_DEFAULT,XALLOC_MEMPROTECT_READWRITE,FALSE,XALLOC_MEMTYPE_HEAP);
1075 special = true;
1076 }
1077 if(!trackStarted )
1078 {
1079 size_t realSize = XMemSizeDefault(pAddress, dwAllocAttributes);
1080 XMemFreeDefault(pAddress, dwAllocAttributes);
1081 totalAllocGen -= realSize;
1082 return;
1083 }
1084 EnterCriticalSection(&memCS);
1085 if( pAddress )
1086 {
1087 size_t realSize = XMemSizeDefault(pAddress, dwAllocAttributes) - 16;
1088
1089 if(trackEnable)
1090 {
1091 int sect = *(((unsigned char *)pAddress)+realSize);
1092 totalAllocGen -= realSize;
1093 trackEnable = false;
1094 int key = ( sect << 26 ) | realSize;
1095 int oldCount = allocCounts[key];
1096 allocCounts[key] = oldCount - 1;
1097 trackEnable = true;
1098
1099 }
1100 XMemFreeDefault(pAddress, dwAllocAttributes);
1101 }
1102 LeaveCriticalSection(&memCS);
1103}
1104
1105SIZE_T WINAPI XMemSize(
1106 PVOID pAddress,
1107 DWORD dwAllocAttributes
1108)
1109{
1110 if( trackStarted )
1111 {
1112 return XMemSizeDefault(pAddress, dwAllocAttributes) - 16;
1113 }
1114 else
1115 {
1116 return XMemSizeDefault(pAddress, dwAllocAttributes);
1117 }
1118}
1119
1120
1121void DumpMem()
1122{
1123 int totalLeak = 0;
1124 for(AUTO_VAR(it, allocCounts.begin()); it != allocCounts.end(); it++ )
1125 {
1126 if(it->second > 0 )
1127 {
1128 app.DebugPrintf("%d %d %d %d\n",( it->first >> 26 ) & 0x3f,it->first & 0x03ffffff, it->second, (it->first & 0x03ffffff) * it->second);
1129 totalLeak += ( it->first & 0x03ffffff ) * it->second;
1130 }
1131 }
1132 app.DebugPrintf("Total %d\n",totalLeak);
1133}
1134
1135void ResetMem()
1136{
1137 if( !trackStarted )
1138 {
1139 trackEnable = true;
1140 trackStarted = true;
1141 totalAllocGen = 0;
1142 InitializeCriticalSection(&memCS);
1143 tlsIdx = TlsAlloc();
1144 }
1145 EnterCriticalSection(&memCS);
1146 trackEnable = false;
1147 allocCounts.clear();
1148 trackEnable = true;
1149 LeaveCriticalSection(&memCS);
1150}
1151
1152void MemSect(int section)
1153{
1154 unsigned int value = (unsigned int)TlsGetValue(tlsIdx);
1155 if( section == 0 ) // pop
1156 {
1157 value = (value >> 6) & 0x03ffffff;
1158 }
1159 else
1160 {
1161 value = (value << 6) | section;
1162 }
1163 TlsSetValue(tlsIdx, (LPVOID)value);
1164}
1165
1166void MemPixStuff()
1167{
1168 const int MAX_SECT = 46;
1169
1170 int totals[MAX_SECT] = {0};
1171
1172 for(AUTO_VAR(it, allocCounts.begin()); it != allocCounts.end(); it++ )
1173 {
1174 if(it->second > 0 )
1175 {
1176 int sect = ( it->first >> 26 ) & 0x3f;
1177 int bytes = it->first & 0x03ffffff;
1178 totals[sect] += bytes * it->second;
1179 }
1180 }
1181
1182 unsigned int allSectsTotal = 0;
1183 for( int i = 0; i < MAX_SECT; i++ )
1184 {
1185 allSectsTotal += totals[i];
1186 PIXAddNamedCounter(((float)totals[i])/1024.0f,"MemSect%d",i);
1187 }
1188
1189 PIXAddNamedCounter(((float)allSectsTotal)/(4096.0f),"MemSect total pages");
1190}
1191
1192#endif