Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "includes.h"
- #include "textures_tpl.h"
- #include "textures.h"
- #include "skybox.h"
- #include "terrain.h"
- #include "water.h"
- GXRModeObj *screenMode;
- static void *frameBuffer;
- static vu8 readyForCopy;
- #define FIFO_SIZE (256*1024)
- float texCoords[] ATTRIBUTE_ALIGN(32) = {
- 0.0f, 1.0f,
- 1.0f, 0.0f,
- 0.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f,
- 1.0f, 0.0f
- };
- float quadVertices[] ATTRIBUTE_ALIGN(32) = {
- -0.5f, 0.5f, 0.0f, // top
- -0.5f, -0.5f, 0.0f, // bottom left
- 0.5f, -0.5f, 0.0f, // bottom right
- -0.5f, 0.5f, 0.0f, // top
- 0.5f, -0.5f, 0.0f, // bottom right
- 0.5f, 0.5f, 0.0f
- };
- float quadTexCoords[] ATTRIBUTE_ALIGN(32) = {
- 0.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 1.0f,
- 1.0f, 0.0f
- };
- typedef struct {
- guVector position;
- guVector velocity;
- guVector rotation;
- } Snowflake;
- Snowflake snowflakes[256];
- typedef struct {
- guVector pos;
- } PreCamPos;
- PreCamPos camLocations[5] = {
- {{0.0f, 0.0f, 0.0f}},
- {{10.0f, 50.0f, 45.0f}},
- {{5.0f, 55.0f, 0.0f}},
- {{32767.0f, 32767.0f, 32767.0f}},
- {{-32767.0f, -32767.0f, -32767.0f}}
- };
- int preCamIndex = 0;
- void update_screen(Mtx44 viewMatrix);
- static void copy_buffers(u32 unused);
- int seed;
- GXTexObj grassTex;
- GXTexObj waterTex;
- GXTexObj snowflakeTex;
- GXTexObj msgBoxTex;
- GXTexObj fontTex;
- GXTexObj skyboxTex;
- expansion_t expCon;
- int nx;
- int ny;
- bool nunchukConnected = false;
- Camera cam;
- Skybox skybox;
- Terrain terrain;
- Water water;
- #define TEXTURE_MSG "I'm not sure how to fix these\ntexture issues with this message\nbox..."
- #define NUNCHUK_MSG "A Nunchuk is required for the use\nof this software. Please connect a\nNunchuk to Player 1's expansion\nport."
- //36 chars per line
- #define TEST_MSG "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij\nklmnopqrstuvwxyz[/]{|}~!\"#$%&'()*+,\n-.\\0123456789:;<=>?@~_`\n\nTEST MESSAGE\n\nMade by atm959.\n\nCool, huh?\n\nMore room for text.\nMore room?\nFinally, the last line."
- //---------------------------------------------------------------------------------
- void drawImage( int x, int y, int width, int height, float u, float v, float u2, float v2) {
- //---------------------------------------------------------------------------------
- GX_Begin(GX_QUADS, GX_VTXFMT1, 4); // Draw A Quad
- GX_Position2f32(x, y); // Top Left
- GX_TexCoord2f32(u,v);
- GX_Position2f32(x+width-1, y); // Top Right
- GX_TexCoord2f32(u2,v);
- GX_Position2f32(x+width-1,y+height-1); // Bottom Right
- GX_TexCoord2f32(u2,v2);
- GX_Position2f32(x,y+height-1); // Bottom Left
- GX_TexCoord2f32(u,v2);
- GX_End(); // Done Drawing The Quad
- }
- Mtx44 projection;
- Mtx44 perspective;
- //---------------------------------------------------------------------------------
- void drawAtlasImage( int x, int y, int width, int height, int image) {
- //---------------------------------------------------------------------------------
- float u = (float)((image % 16) * 16) / 256;
- float v = (float)((image / 16) * 16) / 256;
- float u2 = (float)(((image % 16) * 16) + 16) / 256;
- float v2 = (float)(((image / 16) * 16) + 16) / 256;
- GX_Begin(GX_QUADS, GX_VTXFMT1, 4); // Draw A Quad
- GX_Position2f32(x, y); // Top Left
- GX_TexCoord2f32(u,v);
- GX_Position2f32(x+width, y); // Top Right
- GX_TexCoord2f32(u2,v);
- GX_Position2f32(x+width,y+height); // Bottom Right
- GX_TexCoord2f32(u2,v2);
- GX_Position2f32(x,y+height); // Bottom Left
- GX_TexCoord2f32(u,v2);
- GX_End(); // Done Drawing The Quad
- }
- void renderStringTemporary(char* text, int x, int y){
- GX_LoadTexObj(&fontTex, GX_TEXMAP0);
- int j = 0;
- for(int i = 0; i < strlen(text); i++){
- if(text[i] == '\n'){
- y += 16;
- j = -1;
- }
- drawAtlasImage(x + (j * 13), y, 16, 16, text[i] - 32);
- j++;
- }
- }
- void messageBox(char* text){
- GX_LoadTexObj(&msgBoxTex, GX_TEXMAP0);
- drawImage(0 + 64, 0 + 112, 33, 33, 0.0f, 0.0f, (float)1/5, (float)1/2);
- drawImage(480 + 64, 0 + 112, 33, 33, (float)1/5, 0.0f, (float)2/5, (float)1/2);
- drawImage(0 + 64, 224 + 112, 33, 33, 0.0f, (float)1/2, (float)1/5, 1.0f);
- drawImage(480 + 64, 224 + 112, 33, 33, (float)1/5, (float)1/2, (float)2/5, 1.0f);
- for(int i = 0; i < 14; i++){
- drawImage((32 + (i * 32)) + 64, 0 + 112, 33, 33, (float)3/5, 0.0f, (float)4/5, (float)1/2);
- drawImage((32 + (i * 32)) + 64, 224 + 112, 33, 33, (float)2/5, (float)1/2, (float)3/5, 1.0f);
- }
- for(int i = 0; i < 6; i++){
- drawImage(0 + 64, (32 + (i * 32)) + 112, 33, 33, (float)2/5, 0.0f, (float)3/5, (float)1/2);
- drawImage(480 + 64, (32 + (i * 32)) + 112, 33, 33, (float)3/5, (float)1/2, (float)4/5, 1.0f);
- }
- drawImage(32 + 64, 32 + 112, 449, 193, (float)4/5, 0.0f, 1.0f, (float)1/2);
- renderStringTemporary(text, 64 + 32, 112 + 32);
- }
- void calcCamAngle(float angle){
- cam.look.x = cam.pos.x + (1 * sin(angle));
- cam.look.z = cam.pos.z + (1 * cos(angle));
- cam.look.y = cam.pos.y;
- }
- float a = 0.0f;
- void powerBtnCallback(){
- SYS_ResetSystem(SYS_POWEROFF, 0, 0);
- }
- int main(void);
- void resetBtnCallback(){
- SYS_ResetSystem(SYS_HOTRESET, 0, 0);
- }
- int main(void) {
- SYS_SetPowerCallback(powerBtnCallback);
- SYS_SetResetCallback(resetBtnCallback);
- WPAD_SetPowerButtonCallback(powerBtnCallback);
- initSkybox(&skybox);
- initTerrain(&terrain);
- initWater(&water);
- for(int i = 0; i < 256; i++){
- snowflakes[i] = (Snowflake){{rand() % 64, rand() % 64, rand() % 64}, {0.0f, 0.0f, 0.0f}, {0.0f, rand() % 360, 0.0f}};
- }
- Mtx44 view;
- GXColor backgroundColor = {0, 0, 0, 255};
- void *fifoBuffer = NULL;
- VIDEO_Init();
- WPAD_Init();
- screenMode = VIDEO_GetPreferredMode(NULL);
- frameBuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode));
- VIDEO_Configure(screenMode);
- VIDEO_SetNextFramebuffer(frameBuffer);
- VIDEO_SetPostRetraceCallback(copy_buffers);
- VIDEO_SetBlack(FALSE);
- VIDEO_Flush();
- fifoBuffer = MEM_K0_TO_K1(memalign(32,FIFO_SIZE));
- memset(fifoBuffer, 0, FIFO_SIZE);
- GX_Init(fifoBuffer, FIFO_SIZE);
- GX_SetCopyClear(backgroundColor, 0x00ffffff);
- GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
- GX_SetDispCopyYScale((f32)screenMode->xfbHeight/(f32)screenMode->efbHeight);
- GX_SetScissor(0,0,screenMode->fbWidth,screenMode->efbHeight);
- GX_SetDispCopySrc(0,0,screenMode->fbWidth,screenMode->efbHeight);
- GX_SetDispCopyDst(screenMode->fbWidth,screenMode->xfbHeight);
- GX_SetCopyFilter(screenMode->aa,screenMode->sample_pattern,
- GX_TRUE,screenMode->vfilter);
- GX_SetFieldMode(screenMode->field_rendering,
- ((screenMode->viHeight==2*screenMode->xfbHeight)?GX_ENABLE:GX_DISABLE));
- GX_SetCullMode(GX_CULL_NONE);
- GX_CopyDisp(frameBuffer,GX_TRUE);
- GX_SetDispCopyGamma(GX_GM_1_0);
- guPerspective(projection, 60, 1.33F, 10.0F, 300.0F);
- GX_LoadProjectionMtx(projection, GX_PERSPECTIVE);
- guOrtho(perspective,0,479,0,639,0,300);
- GX_ClearVtxDesc();
- GX_SetVtxDesc(GX_VA_POS, GX_INDEX16);
- //GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX16);
- GX_SetVtxDesc(GX_VA_TEX0, GX_INDEX8);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
- //GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
- GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
- GX_SetVtxAttrFmt(GX_VTXFMT1, GX_VA_POS, GX_POS_XY, GX_F32, 0);
- GX_SetVtxAttrFmt(GX_VTXFMT1, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
- GX_SetNumChans(1);
- GX_SetNumTexGens(1);
- GX_SetNumChans(1);
- GX_SetNumTexGens(1);
- GX_SetTevOp(GX_TEVSTAGE0, GX_REPLACE);
- GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
- GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
- TPLFile texTPL;
- TPL_OpenTPLFromMemory(&texTPL, (void *)textures_tpl, textures_tpl_size);
- TPL_GetTexture(&texTPL, grass, &grassTex);
- TPL_GetTexture(&texTPL, waterTxr, &waterTex);
- TPL_GetTexture(&texTPL, snowflake, &snowflakeTex);
- TPL_GetTexture(&texTPL, msgBox, &msgBoxTex);
- TPL_GetTexture(&texTPL, font, &fontTex);
- TPL_GetTexture(&texTPL, skyboxTxr, &skyboxTex);
- while (1) {
- calcCamAngle(a);
- guLookAt(view, &cam.pos, &cam.up, &cam.look);
- GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
- GX_InvVtxCache();
- GX_InvalidateTexAll();
- update_screen(view);
- WPAD_ScanPads();
- if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) exit(0);
- if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_LEFT) a -= 0.01f;
- if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_RIGHT) a += 0.01f;
- if(WPAD_ButtonsDown(0) & WPAD_BUTTON_MINUS){
- if(preCamIndex > 0) preCamIndex--;
- cam.pos.x = camLocations[preCamIndex].pos.x;
- cam.pos.y = camLocations[preCamIndex].pos.y;
- cam.pos.z = camLocations[preCamIndex].pos.z;
- }
- if(WPAD_ButtonsDown(0) & WPAD_BUTTON_PLUS){
- if(preCamIndex < 4) preCamIndex++;
- cam.pos.x = camLocations[preCamIndex].pos.x;
- cam.pos.y = camLocations[preCamIndex].pos.y;
- cam.pos.z = camLocations[preCamIndex].pos.z;
- }
- WPAD_Expansion(WPAD_CHAN_0, &expCon);
- if(expCon.type == WPAD_EXP_NUNCHUK){
- nunchukConnected = true;
- nx = (expCon.nunchuk.js.pos.x - expCon.nunchuk.js.center.x);
- if((nx > -2) && (nx < 2)) nx = 0;
- ny = (expCon.nunchuk.js.pos.y - expCon.nunchuk.js.center.y);
- if((ny > -2) && (ny < 2)) ny = 0;
- cam.pos.x += (ny * 0.01f) * (cam.look.x - cam.pos.x);
- cam.pos.z += (ny * 0.01f) * (cam.look.z - cam.pos.z);
- cam.pos.x += -(nx * 0.01f) * cam.side.x;
- cam.pos.z += -(nx * 0.01f) * cam.side.z;
- if(expCon.nunchuk.btns_held & NUNCHUK_BUTTON_C) cam.pos.y += 0.1f;
- if(expCon.nunchuk.btns_held & NUNCHUK_BUTTON_Z) cam.pos.y -= 0.1f;
- } else if(expCon.type == WPAD_EXP_NONE){
- nunchukConnected = false;
- }
- cam.side.x = (cam.look.z - cam.pos.z);
- cam.side.z = -(cam.look.x - cam.pos.x);
- cam.up.y = 1.0f;
- }
- return 0;
- }
- void update_screen(Mtx44 viewMatrix) {
- updateSkybox(&skybox, cam);
- updateWater(&water);
- GX_InvVtxCache();
- GX_InvalidateTexAll();
- GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
- GX_LoadProjectionMtx(projection, GX_PERSPECTIVE);
- Mtx44 rot;
- Mtx44 modelView;
- GX_SetVtxDesc(GX_VA_POS, GX_INDEX16);
- GX_SetVtxDesc(GX_VA_TEX0, GX_INDEX8);
- GX_LoadTexObj(&skyboxTex, GX_TEXMAP0);
- guMtxIdentity(modelView);
- guMtxIdentity(rot);
- guMtxScale(rot, 100.0f, 100.0f, 100.0f);
- guMtxConcat(rot,modelView,modelView);
- guMtxTransApply(modelView, modelView, cam.pos.x, cam.pos.y, cam.pos.z);
- guMtxConcat(viewMatrix,modelView,modelView);
- GX_LoadPosMtxImm(modelView, GX_PNMTX0);
- renderSkybox(&skybox);
- guMtxIdentity(modelView);
- guMtxIdentity(rot);
- guMtxConcat(viewMatrix,modelView,modelView);
- GX_LoadPosMtxImm(modelView, GX_PNMTX0);
- GX_LoadTexObj(&grassTex, GX_TEXMAP0);
- GX_SetArray(GX_VA_TEX0, texCoords, 2*sizeof(float));
- renderTerrain(&terrain);
- GX_LoadTexObj(&waterTex, GX_TEXMAP0);
- renderWater(&water);
- GX_SetArray(GX_VA_POS, quadVertices, 3 * sizeof(float));
- GX_SetArray(GX_VA_TEX0, quadTexCoords, 2*sizeof(float));
- GX_LoadTexObj(&snowflakeTex, GX_TEXMAP0);
- for(int i = 0; i < 256; i++){
- guMtxIdentity(modelView);
- guMtxIdentity(rot);
- guMtxRotDeg(rot, 'x', snowflakes[i].rotation.x);
- guMtxConcat(rot,modelView,modelView);
- guMtxRotDeg(rot, 'y', snowflakes[i].rotation.y);
- guMtxConcat(rot,modelView,modelView);
- guMtxRotDeg(rot, 'z', snowflakes[i].rotation.z);
- guMtxConcat(rot,modelView,modelView);
- guMtxTransApply(modelView, modelView, snowflakes[i].position.x, snowflakes[i].position.y, snowflakes[i].position.z);
- guMtxConcat(viewMatrix,modelView,modelView);
- GX_LoadPosMtxImm(modelView, GX_PNMTX0);
- GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 6);
- for(int i = 0; i < 6; i++){
- GX_Position1x16(i);
- GX_TexCoord1x8(i);
- }
- GX_End();
- }
- GX_LoadProjectionMtx(perspective, GX_ORTHOGRAPHIC);
- GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
- GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
- guMtxIdentity(modelView);
- GX_LoadPosMtxImm(modelView, GX_PNMTX0);
- nunchukConnected = true;
- if(!nunchukConnected){
- messageBox(NUNCHUK_MSG);
- }
- char tempText[256];
- sprintf(tempText, "Camera Position: (%g, %g, %g)", cam.pos.x, cam.pos.y, cam.pos.z);
- renderStringTemporary(tempText, 20, 20);
- sprintf(tempText, "Analog Position: (%d, %d)", nx, ny);
- renderStringTemporary(tempText, 20, 40);
- sprintf(tempText, "Camera Look Position: (%g, %g, %g)", cam.look.x, cam.look.y, cam.look.z);
- renderStringTemporary(tempText, 20, 60);
- sprintf(tempText, "Camera Facing Vector: (%g, %g)", cam.look.x - cam.pos.x, cam.look.z - cam.pos.z);
- renderStringTemporary(tempText, 20, 80);
- sprintf(tempText, "Camera Sideways Vector: (%g, %g)", cam.side.x, cam.side.z);
- renderStringTemporary(tempText, 20, 100);
- sprintf(tempText, "Camera Up Vector: (%g, %g, %g)", cam.up.x, cam.up.y, cam.up.z);
- renderStringTemporary(tempText, 20, 120);
- GX_DrawDone();
- readyForCopy = GX_TRUE;
- for(int i = 0; i < 256; i++){
- snowflakes[i].position.y -= 0.1f;
- snowflakes[i].rotation.x += 1.0f;
- snowflakes[i].rotation.y += 1.0f;
- snowflakes[i].rotation.z += 1.0f;
- if(snowflakes[i].position.y <= -2.0f){
- snowflakes[i].position.y = 64.0f;
- }
- }
- VIDEO_WaitVSync();
- return;
- }
- static void copy_buffers(u32 count __attribute__ ((unused)))
- {
- if (readyForCopy==GX_TRUE) {
- GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
- GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
- GX_SetAlphaUpdate(GX_TRUE);
- GX_SetColorUpdate(GX_TRUE);
- GX_CopyDisp(frameBuffer,GX_TRUE);
- GX_Flush();
- readyForCopy = GX_FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement