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"
- #include "snowflake.h"
- #include "matrices.h"
- #include "tevtest.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
- };
- 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;
- static void update_screen();
- static void copy_buffers(u32 unused);
- GXTexObj grassTex;
- GXTexObj waterTex;
- GXTexObj snowflakeTex;
- GXTexObj msgBoxTex;
- GXTexObj fontTex;
- GXTexObj skyboxTex;
- expansion_t expCon;
- int nx;
- int ny;
- bool nunchukConnected = false;
- bool useGamecubeController;
- Camera cam;
- Skybox skybox;
- Terrain terrain;
- Water water;
- Matrices matrices;
- TevTest tevTest;
- #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 or press A on a Gamecube\ncontroller."
- //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."
- #define TEXTURE_MSG_2 "Yay, this message box is fixed! I\njust need to disable the compression\non this texture so transparency in\nthe corners will work."
- //---------------------------------------------------------------------------------
- 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
- }
- //---------------------------------------------------------------------------------
- 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(size_t 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);
- }
- float a = 0.0f;
- void powerBtnCallback(){
- SYS_ResetSystem(SYS_POWEROFF, 0, 0);
- }
- int main(void) {
- SYS_SetPowerCallback(powerBtnCallback);
- cam.initCamera();
- initSkybox(&skybox);
- terrain.initTerrain();
- water.initWater();
- for(int i = 0; i < 256; i++){
- initSnowflake(&snowflakes[i]);
- setSnowflakePosition(&snowflakes[i], (guVector){rand() % 64, rand() % 64, rand() % 64});
- }
- initTevTest(&tevTest);
- allIdentity(&matrices);
- GXColor backgroundColor = {0, 0, 0, 255};
- void *fifoBuffer = NULL;
- VIDEO_Init();
- WPAD_Init();
- PAD_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);
- 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, grassTxr, &grassTex);
- TPL_GetTexture(&texTPL, waterTxr, &waterTex);
- TPL_GetTexture(&texTPL, snowflakeTxr, &snowflakeTex);
- TPL_GetTexture(&texTPL, msgBoxTxr, &msgBoxTex);
- GX_InitTexObjFilterMode(&msgBoxTex, GX_NEAR, GX_NEAR);
- TPL_GetTexture(&texTPL, fontTxr, &fontTex);
- TPL_GetTexture(&texTPL, skyboxTxr, &skyboxTex);
- while (1) {
- cam.setCamAngle(a);
- cam.calcCamVectors();
- calcViewMatrix(&matrices, &cam);
- GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
- update_screen();
- 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() {
- updateSkybox(&skybox, &cam);
- water.updateWater();
- for(int i = 0; i < 256; i++){
- updateSnowflake(&snowflakes[i]);
- }
- updateTevTest(&tevTest);
- GX_InvVtxCache();
- GX_InvalidateTexAll();
- GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
- setPerspective(&matrices);
- GX_SetVtxDesc(GX_VA_POS, GX_INDEX16);
- GX_SetVtxDesc(GX_VA_TEX0, GX_INDEX8);
- GX_LoadTexObj(&skyboxTex, GX_TEXMAP0);
- renderSkybox(&skybox, &matrices);
- guMtxIdentity(matrices.modelViewMatrix);
- guMtxIdentity(matrices.rotationMatrix);
- guMtxConcat(matrices.viewMatrix, matrices.modelViewMatrix,matrices.modelViewMatrix);
- GX_LoadPosMtxImm(matrices.modelViewMatrix, GX_PNMTX0);
- GX_LoadTexObj(&grassTex, GX_TEXMAP0);
- GX_SetArray(GX_VA_TEX0, texCoords, 2*sizeof(float));
- terrain.renderTerrain();
- GX_LoadTexObj(&waterTex, GX_TEXMAP0);
- water.renderWater();
- 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(matrices.modelViewMatrix);
- guMtxIdentity(matrices.rotationMatrix);
- guMtxRotDeg(matrices.rotationMatrix, 'x', snowflakes[i].rotation.x);
- guMtxConcat(matrices.rotationMatrix,matrices.modelViewMatrix,matrices.modelViewMatrix);
- guMtxRotDeg(matrices.rotationMatrix, 'y', snowflakes[i].rotation.y);
- guMtxConcat(matrices.rotationMatrix,matrices.modelViewMatrix,matrices.modelViewMatrix);
- guMtxRotDeg(matrices.rotationMatrix, 'z', snowflakes[i].rotation.z);
- guMtxConcat(matrices.rotationMatrix,matrices.modelViewMatrix,matrices.modelViewMatrix);
- guMtxTransApply(matrices.modelViewMatrix, matrices.modelViewMatrix, snowflakes[i].position.x, snowflakes[i].position.y, snowflakes[i].position.z);
- guMtxConcat(matrices.viewMatrix,matrices.modelViewMatrix,matrices.modelViewMatrix);
- GX_LoadPosMtxImm(matrices.modelViewMatrix, GX_PNMTX0);
- renderSnowflake(&snowflakes[i]);
- }
- guMtxIdentity(matrices.modelViewMatrix);
- guMtxIdentity(matrices.rotationMatrix);
- guMtxScale(matrices.rotationMatrix, 4.0f, 4.0f, 1.0f);
- guMtxConcat(matrices.rotationMatrix,matrices.modelViewMatrix,matrices.modelViewMatrix);
- GX_LoadPosMtxImm(matrices.modelViewMatrix, GX_PNMTX0);
- GX_LoadTexObj(&grassTex, GX_TEXMAP1);
- setOrtho(&matrices);
- GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
- GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
- guMtxIdentity(matrices.modelViewMatrix);
- GX_LoadPosMtxImm(matrices.modelViewMatrix, GX_PNMTX0);
- 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);
- if((!nunchukConnected) && (!useGamecubeController)){
- messageBox(NUNCHUK_MSG);
- }
- GX_DrawDone();
- readyForCopy = GX_TRUE;
- 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