Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- #include <math.h>
- #include <gccore.h>
- #include <wiiuse/wpad.h>
- #include "FreeTypeGX.h"
- #include "rursus_compact_mono_ttf.h"
- const float vertices[8 * 3] ATTRIBUTE_ALIGN(32) = {
- -1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- -1.0f, -1.0f, 1.0f,
- 1.0f, -1.0f, 1.0f,
- -1.0f, 1.0f, -1.0f,
- 1.0f, 1.0f, -1.0f,
- -1.0f, -1.0f, -1.0f,
- 1.0f, -1.0f, -1.0f,
- };
- const char indices[6 * 4] ATTRIBUTE_ALIGN(32) = {
- 0, 1, 2, 3, //Front
- 5, 4, 7, 6, //Back
- 1, 5, 3, 7, //Right
- 4, 0, 6, 2, //Left
- 4, 5, 0, 1, //Top
- 2, 3, 6, 7 //Bottom
- };
- const unsigned char colors[4 * 4] ATTRIBUTE_ALIGN(32) = {
- 255, 0, 0, 100,
- 0, 255, 0, 20,
- 0, 0, 255, 255,
- 255, 255, 0, 60
- };
- #define DEFAULT_FIFO_SIZE (256*1024)
- static void *frameBuffer[2] = { NULL, NULL};
- GXRModeObj *rmode;
- Mtx44 orthoMatrix, perspMatrix, viewMatrix;
- int run = 1;
- void draw_triangle();
- void set_2d();
- void set_3d();
- int main( int argc, char **argv ){
- f32 yscale;
- u32 xfbHeight;
- Mtx model, modelview;
- u32 fb = 0; // initial framebuffer index
- GXColor background = {0, 0, 0, 0xff};
- // init the vi.
- VIDEO_Init();
- WPAD_Init();
- rmode = VIDEO_GetPreferredMode(NULL);
- // allocate 2 framebuffers for double buffering
- frameBuffer[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
- frameBuffer[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
- VIDEO_Configure(rmode);
- VIDEO_SetNextFramebuffer(frameBuffer[fb]);
- VIDEO_SetBlack(FALSE);
- VIDEO_Flush();
- VIDEO_WaitVSync();
- if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
- // setup the fifo and then init the flipper
- void *gp_fifo = NULL;
- gp_fifo = memalign(32,DEFAULT_FIFO_SIZE);
- memset(gp_fifo,0,DEFAULT_FIFO_SIZE);
- GX_Init(gp_fifo,DEFAULT_FIFO_SIZE);
- // clears the bg to color and clears the z buffer
- GX_SetCopyClear(background, 0x00ffffff);
- // other gx setup
- GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1);
- yscale = GX_GetYScaleFactor(rmode->efbHeight,rmode->xfbHeight);
- xfbHeight = GX_SetDispCopyYScale(yscale);
- GX_SetScissor(0,0,rmode->fbWidth,rmode->efbHeight);
- GX_SetDispCopySrc(0,0,rmode->fbWidth,rmode->efbHeight);
- GX_SetDispCopyDst(rmode->fbWidth,xfbHeight);
- GX_SetCopyFilter(rmode->aa,rmode->sample_pattern,GX_TRUE,rmode->vfilter);
- if (rmode->viHeight == (2 * rmode->xfbHeight))
- GX_SetFieldMode(rmode->field_rendering, GX_ENABLE);
- else
- GX_SetFieldMode(rmode->field_rendering, GX_DISABLE);
- if (rmode->aa)
- GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
- else
- GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
- GX_SetCullMode(GX_CULL_NONE);
- GX_CopyDisp(frameBuffer[fb],GX_TRUE);
- GX_SetDispCopyGamma(GX_GM_1_0);
- GX_InvVtxCache();
- GX_ClearVtxDesc();
- GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
- GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
- GX_SetNumChans(1);
- GX_SetNumTexGens(1);
- GX_SetTevOp(GX_TEVSTAGE0, GX_BLEND);
- GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
- GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
- GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
- GX_SetAlphaUpdate(GX_TRUE);
- GX_SetAlphaCompare(GX_GREATER, 0, GX_AOP_AND, GX_ALWAYS, 0);
- GX_SetColorUpdate(GX_ENABLE);
- GX_PixModeSync();
- GX_SetZCompLoc(GX_FALSE);
- GX_InvalidateTexAll();
- guVector cam = {0.0F, 0.0F, 0.0F},
- up = {0.0F, 1.0F, 0.0F},
- look = {0.0F, 0.0F, -1.0F};
- guLookAt(viewMatrix, &cam, &up, &look);
- f32 w = rmode->viWidth;
- f32 h = rmode->viHeight;
- guOrtho(orthoMatrix, 0, rmode->efbHeight, 0, rmode->fbWidth, 0, 1000.0f);
- guPerspective(perspMatrix, 90, (f32)w/h, 0.1F, 300.0F);
- FreeTypeGX *font = new FreeTypeGX(GX_TF_RGBA8, GX_POS_XY);
- font->loadFont(rursus_compact_mono_ttf, rursus_compact_mono_ttf_size, 16, false);
- float a = 0.0f;
- while(run) {
- WPAD_ScanPads();
- GX_SetViewport(0, 0, rmode->fbWidth, rmode->efbHeight, 0, 1);
- a += 0.025f;
- set_2d();
- Mtx tmp;
- guMtxIdentity(tmp);
- GX_LoadPosMtxImm(tmp, GX_PNMTX1);
- GX_SetCurrentMtx(GX_PNMTX1);
- font->setVertexFormat(GX_VTXFMT0);
- font->drawText(10, 25, _TEXT("FreeTypeGX 2d + 3d by xerpi :P"));
- uint32_t textStyle = FTGX_JUSTIFY_CENTER;
- font->drawText(320, 50, _TEXT("THE QUICK BROWN"), (GXColor){0xff, 0x00, 0x00, 0xff}, textStyle | FTGX_ALIGN_TOP);
- font->drawText(320, 125, _TEXT("FOX JUMPS OVER"), (GXColor){0x00, 0xff, 0x00, 0xff}, textStyle | FTGX_ALIGN_MIDDLE);
- font->drawText(320, 200, _TEXT("THE LAZY DOG"), (GXColor){0x00, 0x00, 0xff, 0xff}, textStyle | FTGX_ALIGN_BOTTOM);
- font->drawText(320, 275, _TEXT("the quick brown"), (GXColor){0xff, 0xff, 0x00, 0xff}, textStyle);
- font->drawText(320, 350, _TEXT("fox jumps over"), (GXColor){0xff, 0x00, 0xff, 0xff}, textStyle);
- font->drawText(320, 425, _TEXT("the lazy dog"), (GXColor){0x00, 0xff, 0xff, 0xff}, textStyle);
- set_3d();
- guMtxIdentity(model);
- //Rotate X
- guMtxIdentity(tmp);
- guMtxRotRad(tmp, 'x', a);
- guMtxConcat(tmp, model, model);
- //Rotate Y
- guMtxIdentity(tmp);
- guMtxRotRad(tmp, 'y', a);
- guMtxConcat(tmp, model, model);
- guMtxTransApply(model, model, -2.0f, 0.0f,-4.0f);
- guMtxConcat(viewMatrix, model, modelview);
- // load the modelview matrix into matrix memory
- GX_LoadPosMtxImm(modelview, GX_PNMTX0);
- GX_SetCurrentMtx(GX_PNMTX0);
- draw_triangle();
- guMtxTransApply(model, model, 3.0f,0.0f,0.0f);
- guMtxConcat(viewMatrix, model, modelview);
- // load the modelview matrix into matrix memory
- GX_LoadPosMtxImm(modelview, GX_PNMTX0);
- GX_ClearVtxDesc();
- GX_SetVtxDesc(GX_VA_POS, GX_INDEX8);
- GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX8);
- GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
- GX_SetArray(GX_VA_POS, (void*)vertices, 3 * sizeof(float));
- GX_SetArray(GX_VA_CLR0, (void*)colors, 4 * sizeof(u8));
- int i, j;
- for (i = 0; i < 6; ++i) {
- GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
- for (j = 0; j < 4; ++j) {
- GX_Position1x8(indices[i*4 + j]);
- GX_Color1x8(j);
- }
- GX_End();
- }
- // do this stuff after drawing
- GX_DrawDone();
- fb ^= 1; // flip framebuffer
- GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
- GX_SetColorUpdate(GX_TRUE);
- GX_CopyDisp(frameBuffer[fb],GX_TRUE);
- VIDEO_SetNextFramebuffer(frameBuffer[fb]);
- VIDEO_Flush();
- VIDEO_WaitVSync();
- if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) run = 0;
- }
- delete font;
- return 0;
- }
- void set_2d()
- {
- GX_LoadProjectionMtx(orthoMatrix, GX_ORTHOGRAPHIC);
- GX_ClearVtxDesc();
- GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
- GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
- }
- void set_3d()
- {
- GX_LoadProjectionMtx(perspMatrix, GX_PERSPECTIVE);
- GX_ClearVtxDesc();
- GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
- GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
- GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
- GX_SetTevOp(GX_TEVSTAGE0, GX_BLEND);
- }
- void draw_triangle()
- {
- GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
- GX_SetVtxDesc(GX_VA_TEX0, GX_NONE);
- GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
- GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3);
- GX_Position3f32( 0.0f, 1.0f, 0.0f); // Top
- GX_Color3f32(1.0f, 0.0f, 0.0f);
- GX_Position3f32(-1.0f,-1.0f, 0.0f); // Bottom Left
- GX_Color3f32(0.0f, 1.0f, 0.0f);
- GX_Position3f32( 1.0f,-1.0f, 0.0f); // Bottom Right
- GX_Color3f32(0.0f, 0.0f, 1.0f);
- GX_End();
- }
Add Comment
Please, Sign In to add comment