the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 136 lines 5.0 kB view raw
1#include "stdafx.h" 2#include "GuiComponent.h" 3#include "Tesselator.h" 4 5void GuiComponent::hLine(int x0, int x1, int y, int col) 6{ 7 if (x1 < x0) 8 { 9 int tmp = x0; 10 x0 = x1; 11 x1 = tmp; 12 } 13 fill(x0, y, x1 + 1, y + 1, col); 14} 15 16void GuiComponent::vLine(int x, int y0, int y1, int col) 17{ 18 if (y1 < y0) 19 { 20 int tmp = y0; 21 y0 = y1; 22 y1 = tmp; 23 } 24 fill(x, y0 + 1, x + 1, y1, col); 25} 26 27void GuiComponent::fill(int x0, int y0, int x1, int y1, int col) 28{ 29 if (x0 < x1) 30 { 31 int tmp = x0; 32 x0 = x1; 33 x1 = tmp; 34 } 35 if (y0 < y1) 36 { 37 int tmp = y0; 38 y0 = y1; 39 y1 = tmp; 40 } 41 float a = ((col >> 24) & 0xff) / 255.0f; 42 float r = ((col >> 16) & 0xff) / 255.0f; 43 float g = ((col >> 8) & 0xff) / 255.0f; 44 float b = ((col) & 0xff) / 255.0f; 45 Tesselator *t = Tesselator::getInstance(); 46 glEnable(GL_BLEND); 47 glDisable(GL_TEXTURE_2D); 48 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 49 glColor4f(r, g, b, a); 50 t->begin(); 51 t->vertex((float)(x0), (float)( y1), (float)( 0)); 52 t->vertex((float)(x1), (float)( y1), (float)( 0)); 53 t->vertex((float)(x1), (float)( y0), (float)( 0)); 54 t->vertex((float)(x0), (float)( y0), (float)( 0)); 55 t->end(); 56 glEnable(GL_TEXTURE_2D); 57 glDisable(GL_BLEND); 58} 59 60void GuiComponent::fillGradient(int x0, int y0, int x1, int y1, int col1, int col2) 61{ 62 float a1 = ((col1 >> 24) & 0xff) / 255.0f; 63 float r1 = ((col1 >> 16) & 0xff) / 255.0f; 64 float g1 = ((col1 >> 8) & 0xff) / 255.0f; 65 float b1 = ((col1) & 0xff) / 255.0f; 66 67 float a2 = ((col2 >> 24) & 0xff) / 255.0f; 68 float r2 = ((col2 >> 16) & 0xff) / 255.0f; 69 float g2 = ((col2 >> 8) & 0xff) / 255.0f; 70 float b2 = ((col2) & 0xff) / 255.0f; 71 glDisable(GL_TEXTURE_2D); 72 glEnable(GL_BLEND); 73 glDisable(GL_ALPHA_TEST); 74 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 75 glShadeModel(GL_SMOOTH); 76 77 Tesselator *t = Tesselator::getInstance(); 78 t->begin(); 79 t->color(r1, g1, b1, a1); 80 t->vertex((float)(x1), (float)( y0), blitOffset); 81 t->vertex((float)(x0), (float)( y0), blitOffset); 82 t->color(r2, g2, b2, a2); 83 t->vertex((float)(x0), (float)( y1), blitOffset); 84 t->vertex((float)(x1), (float)( y1), blitOffset); 85 t->end(); 86 87 glShadeModel(GL_FLAT); 88 glDisable(GL_BLEND); 89 glEnable(GL_ALPHA_TEST); 90 glEnable(GL_TEXTURE_2D); 91} 92 93GuiComponent::GuiComponent() 94{ 95 blitOffset = 0; 96} 97 98void GuiComponent::drawCenteredString(Font *font, const wstring& str, int x, int y, int color) 99{ 100 font->drawShadow(str, x - (font->width(str)) / 2, y, color); 101} 102 103void GuiComponent::drawString(Font *font, const wstring& str, int x, int y, int color) 104{ 105 font->drawShadow(str, x, y, color); 106} 107 108void GuiComponent::blit(int x, int y, int sx, int sy, int w, int h) 109{ 110 float us = 1 / 256.0f; 111 float vs = 1 / 256.0f; 112 Tesselator *t = Tesselator::getInstance(); 113 t->begin(); 114 115 // This is a bit of a mystery. In general this ought to be 0.5 to match the centre of texels & pixels in the DX9 version of things. However, when scaling the GUI by a factor of 1.5, I'm 116 // really not sure how exactly point sampled rasterisation works, but when shifting by 0.5 we get a discontinuity down the diagonal of quads. Setting this shift to 0.75 in all cases seems to work fine. 117 const float extraShift = 0.75f; 118 119 // 4J - subtracting extraShift (actual screen pixels, so need to compensate for physical & game width) from each x & y coordinate to compensate for centre of pixels in directx vs openGL 120 float dx = ( extraShift * (float)Minecraft::GetInstance()->width ) / (float)Minecraft::GetInstance()->width_phys; 121 // 4J - Also factor in the scaling from gui coordinate space to the screen. This varies based on user-selected gui scale, and whether we are in a viewport mode or not 122 dx /= Gui::currentGuiScaleFactor; 123 float dy = extraShift / Gui::currentGuiScaleFactor; 124 // Ensure that the x/y, width and height are actually pixel aligned at our current scale factor - in particular, for split screen mode with the default (3X) 125 // scale, we have an overall scale factor of 3 * 0.5 = 1.5, and so any odd pixels won't align 126 float fx = (floorf((float)x * Gui::currentGuiScaleFactor)) / Gui::currentGuiScaleFactor; 127 float fy = (floorf((float)y * Gui::currentGuiScaleFactor)) / Gui::currentGuiScaleFactor; 128 float fw = (floorf((float)w * Gui::currentGuiScaleFactor)) / Gui::currentGuiScaleFactor; 129 float fh = (floorf((float)h * Gui::currentGuiScaleFactor)) / Gui::currentGuiScaleFactor; 130 131 t->vertexUV(fx + 0 - dx, fy + fh - dy, (float)( blitOffset), (float)( (sx + 0) * us), (float)( (sy + h) * vs)); 132 t->vertexUV(fx + fw - dx, fy + fh - dy, (float)( blitOffset), (float)( (sx + w) * us), (float)( (sy + h) * vs)); 133 t->vertexUV(fx + fw - dx, fy + 0 - dy, (float)( blitOffset), (float)( (sx + w) * us), (float)( (sy + 0) * vs)); 134 t->vertexUV(fx + 0 - dx, fy + 0 - dy, (float)( blitOffset), (float)( (sx + 0) * us), (float)( (sy + 0) * vs)); 135 t->end(); 136}