xerpi

Wii FreetypeGX, 2d + 3d

Nov 24th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <math.h>
  6. #include <gccore.h>
  7. #include <wiiuse/wpad.h>
  8.  
  9. #include "FreeTypeGX.h"
  10. #include "rursus_compact_mono_ttf.h"
  11.  
  12. const float vertices[8 * 3] ATTRIBUTE_ALIGN(32) = {
  13.     -1.0f,  1.0f,  1.0f,
  14.      1.0f,  1.0f,  1.0f,
  15.     -1.0f, -1.0f,  1.0f,
  16.      1.0f, -1.0f,  1.0f,
  17.     -1.0f,  1.0f, -1.0f,
  18.      1.0f,  1.0f, -1.0f,
  19.     -1.0f, -1.0f, -1.0f,
  20.      1.0f, -1.0f, -1.0f,
  21. };
  22.  
  23. const char indices[6 * 4] ATTRIBUTE_ALIGN(32) = {
  24.     0, 1, 2, 3,  //Front
  25.     5, 4, 7, 6,  //Back
  26.     1, 5, 3, 7,  //Right
  27.     4, 0, 6, 2,  //Left
  28.     4, 5, 0, 1,  //Top
  29.     2, 3, 6, 7   //Bottom
  30. };
  31.  
  32. const unsigned char colors[4 * 4] ATTRIBUTE_ALIGN(32) = {
  33.     255, 0,   0,   100,
  34.     0,   255, 0,   20,
  35.     0,   0,   255, 255,
  36.     255, 255, 0,   60
  37. };
  38.  
  39. #define DEFAULT_FIFO_SIZE   (256*1024)
  40.  
  41. static void *frameBuffer[2] = { NULL, NULL};
  42. GXRModeObj *rmode;
  43. Mtx44 orthoMatrix, perspMatrix, viewMatrix;
  44. int run = 1;
  45. void draw_triangle();
  46. void set_2d();
  47. void set_3d();
  48.  
  49. int main( int argc, char **argv ){
  50.     f32 yscale;
  51.  
  52.     u32 xfbHeight;
  53.  
  54.     Mtx model, modelview;
  55.  
  56.     u32 fb = 0;     // initial framebuffer index
  57.     GXColor background = {0, 0, 0, 0xff};
  58.  
  59.  
  60.     // init the vi.
  61.     VIDEO_Init();
  62.     WPAD_Init();
  63.  
  64.     rmode = VIDEO_GetPreferredMode(NULL);
  65.    
  66.     // allocate 2 framebuffers for double buffering
  67.     frameBuffer[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  68.     frameBuffer[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  69.  
  70.     VIDEO_Configure(rmode);
  71.     VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  72.     VIDEO_SetBlack(FALSE);
  73.     VIDEO_Flush();
  74.     VIDEO_WaitVSync();
  75.     if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  76.  
  77.     // setup the fifo and then init the flipper
  78.     void *gp_fifo = NULL;
  79.     gp_fifo = memalign(32,DEFAULT_FIFO_SIZE);
  80.     memset(gp_fifo,0,DEFAULT_FIFO_SIZE);
  81.  
  82.     GX_Init(gp_fifo,DEFAULT_FIFO_SIZE);
  83.  
  84.     // clears the bg to color and clears the z buffer
  85.     GX_SetCopyClear(background, 0x00ffffff);
  86.  
  87.     // other gx setup
  88.     GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1);
  89.     yscale = GX_GetYScaleFactor(rmode->efbHeight,rmode->xfbHeight);
  90.     xfbHeight = GX_SetDispCopyYScale(yscale);
  91.     GX_SetScissor(0,0,rmode->fbWidth,rmode->efbHeight);
  92.     GX_SetDispCopySrc(0,0,rmode->fbWidth,rmode->efbHeight);
  93.     GX_SetDispCopyDst(rmode->fbWidth,xfbHeight);
  94.     GX_SetCopyFilter(rmode->aa,rmode->sample_pattern,GX_TRUE,rmode->vfilter);
  95.     if (rmode->viHeight == (2 * rmode->xfbHeight))
  96.         GX_SetFieldMode(rmode->field_rendering, GX_ENABLE);
  97.     else
  98.         GX_SetFieldMode(rmode->field_rendering, GX_DISABLE);
  99.     if (rmode->aa)
  100.         GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
  101.     else
  102.         GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
  103.  
  104.     GX_SetCullMode(GX_CULL_NONE);
  105.     GX_CopyDisp(frameBuffer[fb],GX_TRUE);
  106.     GX_SetDispCopyGamma(GX_GM_1_0);
  107.  
  108.     GX_InvVtxCache();
  109.     GX_ClearVtxDesc();
  110.     GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  111.     GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
  112.  
  113.  
  114.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
  115.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
  116.  
  117.     GX_SetNumChans(1);
  118.     GX_SetNumTexGens(1);
  119.     GX_SetTevOp(GX_TEVSTAGE0, GX_BLEND);
  120.     GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
  121.     GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
  122.  
  123.     GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
  124.     GX_SetAlphaUpdate(GX_TRUE);
  125.     GX_SetAlphaCompare(GX_GREATER, 0, GX_AOP_AND, GX_ALWAYS, 0);
  126.     GX_SetColorUpdate(GX_ENABLE);
  127.  
  128.     GX_PixModeSync();
  129.     GX_SetZCompLoc(GX_FALSE);
  130.  
  131.     GX_InvalidateTexAll();
  132.  
  133.     guVector cam = {0.0F, 0.0F, 0.0F},
  134.             up = {0.0F, 1.0F, 0.0F},
  135.             look = {0.0F, 0.0F, -1.0F};
  136.  
  137.     guLookAt(viewMatrix, &cam, &up, &look);
  138.     f32 w = rmode->viWidth;
  139.     f32 h = rmode->viHeight;   
  140.    
  141.     guOrtho(orthoMatrix, 0, rmode->efbHeight, 0, rmode->fbWidth, 0, 1000.0f);
  142.     guPerspective(perspMatrix, 90, (f32)w/h, 0.1F, 300.0F);
  143.    
  144.     FreeTypeGX *font = new FreeTypeGX(GX_TF_RGBA8, GX_POS_XY);
  145.     font->loadFont(rursus_compact_mono_ttf, rursus_compact_mono_ttf_size, 16, false);
  146.  
  147.     float a = 0.0f;
  148.     while(run) {
  149.         WPAD_ScanPads();
  150.         GX_SetViewport(0, 0, rmode->fbWidth, rmode->efbHeight, 0, 1);
  151.        
  152.         a += 0.025f;
  153.         set_2d();
  154.         Mtx tmp;
  155.         guMtxIdentity(tmp);
  156.        
  157.         GX_LoadPosMtxImm(tmp, GX_PNMTX1);
  158.         GX_SetCurrentMtx(GX_PNMTX1);   
  159.        
  160.         font->setVertexFormat(GX_VTXFMT0);
  161.         font->drawText(10, 25, _TEXT("FreeTypeGX 2d + 3d by xerpi :P"));
  162.         uint32_t textStyle = FTGX_JUSTIFY_CENTER;
  163.  
  164.         font->drawText(320, 50,     _TEXT("THE QUICK BROWN"),   (GXColor){0xff, 0x00, 0x00, 0xff},  textStyle | FTGX_ALIGN_TOP);
  165.         font->drawText(320, 125,    _TEXT("FOX JUMPS OVER"),    (GXColor){0x00, 0xff, 0x00, 0xff},  textStyle | FTGX_ALIGN_MIDDLE);
  166.         font->drawText(320, 200,    _TEXT("THE LAZY DOG"),      (GXColor){0x00, 0x00, 0xff, 0xff},  textStyle | FTGX_ALIGN_BOTTOM);
  167.  
  168.         font->drawText(320, 275,    _TEXT("the quick brown"),   (GXColor){0xff, 0xff, 0x00, 0xff},  textStyle);
  169.         font->drawText(320, 350,    _TEXT("fox jumps over"),    (GXColor){0xff, 0x00, 0xff, 0xff},  textStyle);
  170.         font->drawText(320, 425,    _TEXT("the lazy dog"),      (GXColor){0x00, 0xff, 0xff, 0xff},  textStyle);
  171.  
  172.         set_3d();
  173.        
  174.         guMtxIdentity(model);
  175.         //Rotate X
  176.             guMtxIdentity(tmp);
  177.             guMtxRotRad(tmp, 'x', a);
  178.             guMtxConcat(tmp, model, model);
  179.         //Rotate Y
  180.             guMtxIdentity(tmp);
  181.             guMtxRotRad(tmp, 'y', a);
  182.             guMtxConcat(tmp, model, model);
  183.         guMtxTransApply(model, model, -2.0f, 0.0f,-4.0f);
  184.         guMtxConcat(viewMatrix, model, modelview);
  185.         // load the modelview matrix into matrix memory
  186.         GX_LoadPosMtxImm(modelview, GX_PNMTX0);
  187.         GX_SetCurrentMtx(GX_PNMTX0);   
  188.  
  189.         draw_triangle();
  190.  
  191.         guMtxTransApply(model, model, 3.0f,0.0f,0.0f);
  192.         guMtxConcat(viewMatrix, model, modelview);
  193.         // load the modelview matrix into matrix memory
  194.         GX_LoadPosMtxImm(modelview, GX_PNMTX0);
  195.  
  196.         GX_ClearVtxDesc();
  197.         GX_SetVtxDesc(GX_VA_POS, GX_INDEX8);
  198.         GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX8);
  199.         GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
  200.         GX_SetArray(GX_VA_POS,  (void*)vertices, 3 * sizeof(float));
  201.         GX_SetArray(GX_VA_CLR0, (void*)colors,   4 * sizeof(u8));
  202.        
  203.         int i, j;
  204.         for (i = 0; i < 6; ++i) {
  205.             GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
  206.             for (j = 0; j < 4; ++j) {
  207.                     GX_Position1x8(indices[i*4 + j]);
  208.                     GX_Color1x8(j);
  209.             }  
  210.             GX_End();      
  211.         }
  212.         // do this stuff after drawing
  213.         GX_DrawDone();
  214.        
  215.         fb ^= 1;        // flip framebuffer
  216.         GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
  217.         GX_SetColorUpdate(GX_TRUE);
  218.         GX_CopyDisp(frameBuffer[fb],GX_TRUE);
  219.  
  220.         VIDEO_SetNextFramebuffer(frameBuffer[fb]);
  221.  
  222.         VIDEO_Flush();
  223.  
  224.         VIDEO_WaitVSync();
  225.         if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) run = 0;
  226.     }
  227.     delete font;
  228.     return 0;
  229. }
  230.  
  231.  
  232. void set_2d()
  233. {
  234.     GX_LoadProjectionMtx(orthoMatrix, GX_ORTHOGRAPHIC);
  235.     GX_ClearVtxDesc();
  236.  
  237.     GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  238.     GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
  239.  
  240. }
  241.  
  242. void set_3d()
  243. {
  244.     GX_LoadProjectionMtx(perspMatrix, GX_PERSPECTIVE);
  245.     GX_ClearVtxDesc();
  246.     GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  247.     GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
  248.     GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
  249.  
  250.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
  251.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
  252.    
  253.     GX_SetTevOp(GX_TEVSTAGE0, GX_BLEND);
  254.    
  255. }
  256.  
  257. void draw_triangle()
  258. {
  259.     GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  260.     GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
  261.     GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
  262.     GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3);
  263.         GX_Position3f32( 0.0f, 1.0f, 0.0f);     // Top
  264.         GX_Color3f32(1.0f, 0.0f, 0.0f);
  265.         GX_Position3f32(-1.0f,-1.0f, 0.0f); // Bottom Left
  266.         GX_Color3f32(0.0f, 1.0f, 0.0f);
  267.         GX_Position3f32( 1.0f,-1.0f, 0.0f); // Bottom Right
  268.         GX_Color3f32(0.0f, 0.0f, 1.0f);
  269.     GX_End();
  270. }
Add Comment
Please, Sign In to add comment