Created
October 20, 2014 21:23
-
-
Save kayru/d1716e1d6d97ef676048 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*--------------------------------------------------------------------------------- | |
nehe lesson 2 port to GX by WinterMute | |
---------------------------------------------------------------------------------*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <malloc.h> | |
#include <math.h> | |
#include <gccore.h> | |
#define DEFAULT_FIFO_SIZE (256*1024) | |
static void *frameBuffer[2] = { NULL, NULL}; | |
GXRModeObj *rmode; | |
//--------------------------------------------------------------------------------- | |
int main( int argc, char **argv ){ | |
//--------------------------------------------------------------------------------- | |
f32 yscale; | |
u32 xfbHeight; | |
Mtx44 proj; | |
u32 fb = 0; // initial framebuffer index | |
u32 i; | |
GXColor background = {0x11, 0x22, 0x33, 0xff}; | |
// init the vi. | |
VIDEO_Init(); | |
rmode = VIDEO_GetPreferredMode(NULL); | |
PAD_Init(); | |
// 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); | |
GX_SetFieldMode(rmode->field_rendering,((rmode->viHeight==2*rmode->xfbHeight)?GX_ENABLE:GX_DISABLE)); | |
GX_SetCullMode(GX_CULL_NONE); | |
GX_CopyDisp(frameBuffer[fb],GX_TRUE); | |
GX_SetDispCopyGamma(GX_GM_1_0); | |
// setup the vertex descriptor | |
// tells the flipper to expect direct data | |
GX_ClearVtxDesc(); | |
GX_SetVtxDesc(GX_VA_POS, GX_DIRECT); | |
// setup the vertex attribute table | |
// describes the data | |
// args: vat location 0-7, type of data, data format, size, scale | |
// so for ex. in the first call we are sending position data with | |
// 3 values X,Y,Z of size F32. scale sets the number of fractional | |
// bits for non float data. | |
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0); | |
GX_SetNumChans(1); | |
GX_SetNumTexGens(0); | |
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0); | |
GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR); | |
// setup our projection matrix | |
// this creates a perspective matrix with a view angle of 90, | |
// and aspect ratio based on the display resolution | |
f32 w = rmode->viWidth; | |
f32 h = rmode->viHeight; | |
(void)w; (void)h; | |
//guPerspective(proj, 45, (f32)w/h, 0.1F, 300.0F); | |
guMtxIdentity(proj); | |
//GX_LoadProjectionMtx(proj, GX_PERSPECTIVE); | |
GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC); | |
while(1) { | |
PAD_ScanPads(); | |
if ( PAD_ButtonsDown(0) & PAD_BUTTON_START) { | |
exit(0); | |
} | |
// do this before drawing | |
GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1); | |
Mtx model; | |
guMtxTrans(model, 0.0f, 0.0f, -2.0f); | |
guMtxScaleApply(model, model, 0.125f, 0.125f, 1.0f); | |
guMtxTransApply(model, model, -0.5, 0.0f, 0.0f); | |
for (i=0; i<5; ++i) | |
{ | |
GX_LoadPosMtxImm(model, GX_PNMTX0); | |
GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3); | |
GX_Position3f32( 0.0f, 1.0f, 0.0f); | |
GX_Position3f32(-1.0f, -1.0f, 0.0f); | |
GX_Position3f32( 1.0f, -1.0f, 0.0f); | |
GX_End(); | |
guMtxTransApply(model, model, 0.25f, 0.0f, 1.0f); | |
} | |
guMtxIdentity(model); | |
GX_LoadPosMtxImm(model, GX_PNMTX0); | |
GX_Begin(GX_QUADS, GX_VTXFMT0, 4); | |
GX_Position3f32(-1.0f, 1.0f, -2.0f); | |
GX_Position3f32( 1.0f, 1.0f, 2.0f); | |
GX_Position3f32( 1.0f, 0.5f, 2.0f); | |
GX_Position3f32(-1.0f, 0.5f, -2.0f); | |
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(); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment