the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2
3#include "..\..\..\Minecraft.World\net.minecraft.world.item.h"
4#include "..\..\..\Minecraft.World\net.minecraft.world.level.tile.entity.h"
5#include "..\..\..\Minecraft.World\net.minecraft.world.inventory.h"
6
7#include "..\..\Minecraft.h"
8#include "..\..\ScreenSizeCalculator.h"
9#include "..\..\TileEntityRenderDispatcher.h"
10#include "..\..\EnchantTableRenderer.h"
11#include "..\..\Lighting.h"
12#include "..\..\LocalPlayer.h"
13
14#include "XUI_Scene_Enchant.h"
15
16#include "XUI_Ctrl_EnchantmentBook.h"
17#include "..\..\BookModel.h"
18#include "..\..\Options.h"
19
20//-----------------------------------------------------------------------------
21// CXuiCtrlEnchantmentBook class
22//-----------------------------------------------------------------------------
23
24//-----------------------------------------------------------------------------
25CXuiCtrlEnchantmentBook::CXuiCtrlEnchantmentBook() :
26 m_bDirty(FALSE),
27 m_fScale(1.0f),
28 m_fAlpha(1.0f)
29{
30 Minecraft *pMinecraft=Minecraft::GetInstance();
31
32 ScreenSizeCalculator ssc(pMinecraft->options, pMinecraft->width_phys, pMinecraft->height_phys);
33 m_fScreenWidth=(float)pMinecraft->width_phys;
34 m_fRawWidth=(float)ssc.rawWidth;
35 m_fScreenHeight=(float)pMinecraft->height_phys;
36 m_fRawHeight=(float)ssc.rawHeight;
37
38 model = NULL;
39
40 time = 0;
41 flip = oFlip = flipT = flipA = 0.0f;
42 open = oOpen = 0.0f;
43}
44
45CXuiCtrlEnchantmentBook::~CXuiCtrlEnchantmentBook()
46{
47 //if(model != NULL) delete model;
48}
49
50//-----------------------------------------------------------------------------
51HRESULT CXuiCtrlEnchantmentBook::OnInit(XUIMessageInit* pInitData, BOOL& rfHandled)
52{
53 HRESULT hr=S_OK;
54
55 HXUIOBJ parent = m_hObj;
56 HXUICLASS hcInventoryClass = XuiFindClass( L"CXuiSceneEnchant" );
57 HXUICLASS currentClass;
58
59 do
60 {
61 XuiElementGetParent(parent,&parent);
62 currentClass = XuiGetObjectClass( parent );
63 } while (parent != NULL && !XuiClassDerivesFrom( currentClass, hcInventoryClass ) );
64
65 assert( parent != NULL );
66
67 VOID *pObj;
68 XuiObjectFromHandle( parent, &pObj );
69 m_containerScene = (CXuiSceneEnchant *)pObj;
70
71 last = nullptr;
72
73 m_iPad = m_containerScene->getPad();
74
75 return hr;
76}
77
78HRESULT CXuiCtrlEnchantmentBook::OnRender(XUIMessageRender *pRenderData, BOOL &bHandled )
79{
80#ifdef _XBOX
81 HXUIDC hDC = pRenderData->hDC;
82
83 // build and render with the game call
84
85 RenderManager.Set_matrixDirty();
86
87 Minecraft *pMinecraft=Minecraft::GetInstance();
88
89 float alpha = 1.0f;
90 //GetOpacity( &alpha );
91
92
93 D3DXMATRIX matrix;
94 GetFullXForm(&matrix);
95
96 float bwidth,bheight;
97 GetBounds(&bwidth,&bheight);
98
99 glColor4f(1, 1, 1, alpha);
100
101 // Annoyingly, XUI renders everything to a z of 0 so if we want to render anything that needs the z-buffer on top of it, then we need to clear it.
102 // Clear just the region required for this control.
103 D3DRECT clearRect;
104 clearRect.x1 = (int)(matrix._41) - 2;
105 clearRect.y1 = (int)(matrix._42) - 2;
106 clearRect.x2 = (int)(matrix._41 + ( bwidth * matrix._11 )) + 2;
107 clearRect.y2 = (int)(matrix._42 + ( bheight * matrix._22 )) + 2;
108
109 RenderManager.Clear(GL_DEPTH_BUFFER_BIT, &clearRect);
110
111 glClear(GL_DEPTH_BUFFER_BIT);
112 glMatrixMode(GL_PROJECTION);
113 glLoadIdentity();
114 glOrtho(0, m_fRawWidth, m_fRawHeight, 0, 1000, 3000);
115 //gluPerspective(90, (float) (320 / 240.0f), 0.5f, 3000);
116 glMatrixMode(GL_MODELVIEW);
117 glLoadIdentity();
118 glTranslatef(0, 0, -2000);
119
120 float xo = ( (matrix._41 + ( (bwidth*matrix._11)/2) ) / m_fScreenWidth ) * m_fRawWidth;
121 float yo = ( (matrix._42 + ((bheight*matrix._22)/2) ) / m_fScreenHeight ) * m_fRawHeight;
122
123 glEnable(GL_RESCALE_NORMAL);
124 glEnable(GL_COLOR_MATERIAL);
125
126 glPushMatrix();
127 glTranslatef(xo, yo, 50.0f);
128 float ss;
129
130 // Base scale on height of this control
131 // Potentially we might want separate x & y scales here
132 ss = ( ( (bheight*matrix._22) / m_fScreenHeight ) * m_fRawHeight ) * 1.5f;
133
134 glScalef(-ss, ss, ss);
135 //glRotatef(180, 0, 0, 1);
136
137 glRotatef(45 + 90, 0, 1, 0);
138 Lighting::turnOn();
139 glRotatef(-45 - 90, 0, 1, 0);
140
141 //float sss = 4;
142
143 //glTranslatef(0, 3.3f, -16);
144 //glScalef(sss, sss, sss);
145
146 int tex = pMinecraft->textures->loadTexture(TN_ITEM_BOOK); // 4J was L"/1_2_2/item/book.png"
147 pMinecraft->textures->bind(tex);
148
149 glRotatef(20, 1, 0, 0);
150
151 float a = 1;
152 float o = oOpen + (open - oOpen) * a;
153 glTranslatef((1 - o) * 0.2f, (1 - o) * 0.1f, (1 - o) * 0.25f);
154 glRotatef(-(1 - o) * 90 - 90, 0, 1, 0);
155 glRotatef(180, 1, 0, 0);
156
157 float ff1 = oFlip + (flip - oFlip) * a + 0.25f;
158 float ff2 = oFlip + (flip - oFlip) * a + 0.75f;
159 ff1 = (ff1 - floor(ff1)) * 1.6f - 0.3f;
160 ff2 = (ff2 - floor(ff2)) * 1.6f - 0.3f;
161
162 if (ff1 < 0) ff1 = 0;
163 if (ff2 < 0) ff2 = 0;
164 if (ff1 > 1) ff1 = 1;
165 if (ff2 > 1) ff2 = 1;
166
167 glEnable(GL_CULL_FACE);
168
169 if(model == NULL)
170 {
171 // Share the model the the EnchantTableRenderer
172
173 EnchantTableRenderer *etr = (EnchantTableRenderer*)TileEntityRenderDispatcher::instance->getRenderer(eTYPE_ENCHANTMENTTABLEENTITY);
174 if(etr != NULL)
175 {
176 model = etr->bookModel;
177 }
178 else
179 {
180 model = new BookModel();
181 }
182 }
183
184 model->render(NULL, 0, ff1, ff2, o, 0, 1 / 16.0f,true);
185 glDisable(GL_CULL_FACE);
186
187 glPopMatrix();
188 Lighting::turnOff();
189 glDisable(GL_RESCALE_NORMAL);
190
191 XuiRenderRestoreState(hDC);
192
193 tickBook();
194
195 bHandled = TRUE;
196
197#endif
198 return S_OK;
199}
200
201//HRESULT CXuiCtrlEnchantmentBook::OnRender(XUIMessageRender *pRenderData, BOOL &bHandled )
202//{
203// HXUIDC hDC = pRenderData->hDC;
204//
205// RenderManager.Set_matrixDirty();
206//
207// Minecraft *minecraft = Minecraft::GetInstance();
208//
209// // 4J JEV: Inputs in the java, dunno what they are.
210// float a = 1;
211//
212// D3DXMATRIX matrix;
213// CXuiControl xuiControl(m_hObj);
214// xuiControl.GetFullXForm(&matrix);
215// float bwidth,bheight;
216// xuiControl.GetBounds(&bwidth,&bheight);
217//
218// D3DXVECTOR3 vec, vecP, vecPP;
219// xuiControl.GetPosition(&vec);
220//
221// CXuiElement parent, scene;
222// xuiControl.GetParent(&parent);
223// parent.GetPosition(&vecP);
224// parent.GetParent(&scene);
225// scene.GetPosition(&vecPP);
226//
227// float xo = ( (matrix._41 + ( (bwidth*matrix._11)/2) ) / m_fScreenWidth ) * m_fRawWidth;
228// float yo = ( (matrix._42 + (bheight*matrix._22) ) / m_fScreenHeight ) * m_fRawHeight;
229//
230// glColor4f(1, 1, 1, 1);
231//
232// glPushMatrix();
233// glMatrixMode(GL_PROJECTION);
234// glPushMatrix();
235// glLoadIdentity();
236//
237// ScreenSizeCalculator ssc = ScreenSizeCalculator(minecraft->options, minecraft->width, minecraft->height);
238//
239// //glViewport((int) (ssc.getWidth() - 320) / 2 * ssc.scale, (int) (ssc.getHeight() - 240) / 2 * ssc.scale, (int) (320 * ssc.scale), (int) (240 * ssc.scale));
240//
241// // 4J JEV: Trying to position it within the XUI element
242// float xPos, yPos;
243// xPos = (vec.x + vecP.x + vecPP.x) / (minecraft->width/2); xPos = xPos - 1; xPos = 2*xPos/3;
244// yPos = (vec.y + vecP.y + vecPP.y) / (minecraft->height/2); yPos = 1 - yPos; yPos = 2*yPos/3;
245// glTranslatef(xPos, yPos, 0);
246//
247// gluPerspective(90, (float) (320 / 240.0f), 9, 80);
248//
249// float sss = 1;
250// glMatrixMode(GL_MODELVIEW);
251// glLoadIdentity();
252// Lighting::turnOn();
253//
254// glTranslatef(0, 3.3f, -16);
255// glScalef(sss, sss, sss);
256//
257// float ss = 5;
258//
259// glScalef(ss, ss, ss);
260// glRotatef(180, 0, 0, 1);
261//
262// int tex = minecraft->textures->loadTexture(TN_ITEM_BOOK); // 4J was L"/1_2_2/item/book.png"
263// minecraft->textures->bind(tex);
264//
265// glRotatef(20, 1, 0, 0);
266//
267// float o = oOpen + (open - oOpen) * a;
268// glTranslatef((1 - o) * 0.2f, (1 - o) * 0.1f, (1 - o) * 0.25f);
269// glRotatef(-(1 - o) * 90 - 90, 0, 1, 0);
270// glRotatef(180, 1, 0, 0);
271//
272// float ff1 = oFlip + (flip - oFlip) * a + 0.25f;
273// float ff2 = oFlip + (flip - oFlip) * a + 0.75f;
274// ff1 = (ff1 - floor(ff1)) * 1.6f - 0.3f;
275// ff2 = (ff2 - floor(ff2)) * 1.6f - 0.3f;
276//
277// if (ff1 < 0) ff1 = 0;
278// if (ff2 < 0) ff2 = 0;
279// if (ff1 > 1) ff1 = 1;
280// if (ff2 > 1) ff2 = 1;
281//
282// glEnable(GL_RESCALE_NORMAL);
283//
284// model.render(NULL, 0, ff1, ff2, o, 0, 1 / 16.0f,true);
285//
286// glDisable(GL_RESCALE_NORMAL);
287// Lighting::turnOff();
288// glMatrixMode(GL_PROJECTION);
289// //glViewport(0, 0, minecraft->width, minecraft->height);
290// glPopMatrix();
291// glMatrixMode(GL_MODELVIEW);
292// glPopMatrix();
293//
294// Lighting::turnOff();
295// glColor4f(1, 1, 1, 1);
296//
297// XuiRenderRestoreState(hDC);
298//
299// tickBook();
300//
301// bHandled = TRUE;
302//
303// return S_OK;
304//}
305
306void CXuiCtrlEnchantmentBook::tickBook()
307{
308 EnchantmentMenu *menu = m_containerScene->getMenu();
309 shared_ptr<ItemInstance> current = menu->getSlot(0)->getItem();
310 if (!ItemInstance::matches(current, last))
311 {
312 last = current;
313
314 do
315 {
316 flipT += random.nextInt(4) - random.nextInt(4);
317 } while (flip <= flipT + 1 && flip >= flipT - 1);
318 }
319
320 time++;
321 oFlip = flip;
322 oOpen = open;
323
324 bool shouldBeOpen = false;
325 for (int i = 0; i < 3; i++)
326 {
327 if (menu->costs[i] != 0)
328 {
329 shouldBeOpen = true;
330 }
331 }
332
333 if (shouldBeOpen) open += 0.2f;
334 else open -= 0.2f;
335 if (open < 0) open = 0;
336 if (open > 1) open = 1;
337
338
339 float diff = (flipT - flip) * 0.4f;
340 float max = 0.2f;
341 if (diff < -max) diff = -max;
342 if (diff > +max) diff = +max;
343 flipA += (diff - flipA) * 0.9f;
344
345 flip = flip + flipA;
346}