Advertisement
atm959

main.c

Jan 7th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.37 KB | None | 0 0
  1. #include "includes.h"
  2.  
  3. #include "textures_tpl.h"
  4. #include "textures.h"
  5.  
  6. #include "skybox.h"
  7. #include "terrain.h"
  8. #include "water.h"
  9.  
  10. GXRModeObj  *screenMode;
  11. static void *frameBuffer;
  12. static vu8  readyForCopy;
  13. #define FIFO_SIZE (256*1024)
  14.  
  15. float texCoords[] ATTRIBUTE_ALIGN(32) = {
  16.     0.0f, 1.0f,
  17.     1.0f, 0.0f,
  18.     0.0f, 0.0f,
  19.     0.0f, 1.0f,
  20.     1.0f, 1.0f,
  21.     1.0f, 0.0f
  22. };
  23.  
  24. float quadVertices[] ATTRIBUTE_ALIGN(32) = {
  25.     -0.5f,  0.5f, 0.0f, // top
  26.     -0.5f, -0.5f, 0.0f, // bottom left
  27.     0.5f, -0.5f, 0.0f,  // bottom right
  28.     -0.5f,  0.5f, 0.0f, // top
  29.     0.5f, -0.5f, 0.0f,  // bottom right
  30.     0.5f,  0.5f, 0.0f
  31. };
  32.  
  33. float quadTexCoords[] ATTRIBUTE_ALIGN(32) = {
  34.     0.0f, 0.0f,
  35.     0.0f, 1.0f,
  36.     1.0f, 1.0f,
  37.     0.0f, 0.0f,
  38.     1.0f, 1.0f,
  39.     1.0f, 0.0f
  40. };
  41.  
  42. typedef struct {
  43.     guVector position;
  44.     guVector velocity;
  45.     guVector rotation;
  46. } Snowflake;
  47.  
  48. Snowflake snowflakes[256];
  49.  
  50. typedef struct {
  51.     guVector pos;
  52. } PreCamPos;
  53.  
  54. PreCamPos camLocations[5] = {
  55.     {{0.0f, 0.0f, 0.0f}},
  56.     {{10.0f, 50.0f, 45.0f}},
  57.     {{5.0f, 55.0f, 0.0f}},
  58.     {{32767.0f, 32767.0f, 32767.0f}},
  59.     {{-32767.0f, -32767.0f, -32767.0f}}
  60. };
  61. int preCamIndex = 0;
  62.  
  63. void update_screen(Mtx44 viewMatrix);
  64. static void copy_buffers(u32 unused);
  65.  
  66. int seed;
  67.  
  68. GXTexObj grassTex;
  69. GXTexObj waterTex;
  70. GXTexObj snowflakeTex;
  71.  
  72. GXTexObj msgBoxTex;
  73.  
  74. GXTexObj fontTex;
  75.  
  76. GXTexObj skyboxTex;
  77.  
  78. expansion_t expCon;
  79. int nx;
  80. int ny;
  81.  
  82. bool nunchukConnected = false;
  83.  
  84. Camera cam;
  85. Skybox skybox;
  86. Terrain terrain;
  87. Water water;
  88.  
  89. #define TEXTURE_MSG "I'm not sure how to fix these\ntexture issues with this message\nbox..."
  90. #define NUNCHUK_MSG "A Nunchuk is required for the use\nof this software. Please connect a\nNunchuk to Player 1's expansion\nport."
  91. //36 chars per line
  92. #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."
  93.  
  94. //---------------------------------------------------------------------------------
  95. void drawImage( int x, int y, int width, int height, float u, float v, float u2, float v2) {
  96. //---------------------------------------------------------------------------------
  97.     GX_Begin(GX_QUADS, GX_VTXFMT1, 4);          // Draw A Quad
  98.         GX_Position2f32(x, y);                  // Top Left
  99.         GX_TexCoord2f32(u,v);
  100.  
  101.         GX_Position2f32(x+width-1, y);          // Top Right
  102.         GX_TexCoord2f32(u2,v);
  103.  
  104.         GX_Position2f32(x+width-1,y+height-1);  // Bottom Right
  105.         GX_TexCoord2f32(u2,v2);
  106.  
  107.         GX_Position2f32(x,y+height-1);          // Bottom Left
  108.         GX_TexCoord2f32(u,v2);
  109.     GX_End();                                   // Done Drawing The Quad
  110.  
  111. }
  112.  
  113. Mtx44   projection;
  114. Mtx44 perspective;
  115.  
  116. //---------------------------------------------------------------------------------
  117. void drawAtlasImage( int x, int y, int width, int height, int image) {
  118. //---------------------------------------------------------------------------------
  119.  
  120.     float u = (float)((image % 16) * 16) / 256;
  121.     float v = (float)((image / 16) * 16) / 256;
  122.     float u2 = (float)(((image % 16) * 16) + 16) / 256;
  123.     float v2 = (float)(((image / 16) * 16) + 16) / 256;
  124.  
  125.     GX_Begin(GX_QUADS, GX_VTXFMT1, 4);          // Draw A Quad
  126.         GX_Position2f32(x, y);                  // Top Left
  127.         GX_TexCoord2f32(u,v);
  128.         GX_Position2f32(x+width, y);            // Top Right
  129.         GX_TexCoord2f32(u2,v);
  130.         GX_Position2f32(x+width,y+height);  // Bottom Right
  131.         GX_TexCoord2f32(u2,v2);
  132.         GX_Position2f32(x,y+height);            // Bottom Left
  133.         GX_TexCoord2f32(u,v2);
  134.     GX_End();                                   // Done Drawing The Quad
  135. }
  136.  
  137. void renderStringTemporary(char* text, int x, int y){
  138.     GX_LoadTexObj(&fontTex, GX_TEXMAP0);
  139.     int j = 0;
  140.     for(int i = 0; i < strlen(text); i++){
  141.         if(text[i] == '\n'){
  142.             y += 16;
  143.             j = -1;
  144.         }
  145.         drawAtlasImage(x + (j * 13), y, 16, 16, text[i] - 32);
  146.         j++;
  147.     }
  148. }
  149.  
  150. void messageBox(char* text){
  151.     GX_LoadTexObj(&msgBoxTex, GX_TEXMAP0);
  152.     drawImage(0 + 64, 0 + 112, 33, 33, 0.0f, 0.0f, (float)1/5, (float)1/2);
  153.     drawImage(480 + 64, 0 + 112, 33, 33, (float)1/5, 0.0f, (float)2/5, (float)1/2);
  154.     drawImage(0 + 64, 224 + 112, 33, 33, 0.0f, (float)1/2, (float)1/5, 1.0f);
  155.     drawImage(480 + 64, 224 + 112, 33, 33, (float)1/5, (float)1/2, (float)2/5, 1.0f);
  156.  
  157.     for(int i = 0; i < 14; i++){
  158.         drawImage((32 + (i * 32)) + 64, 0 + 112, 33, 33, (float)3/5, 0.0f, (float)4/5, (float)1/2);
  159.         drawImage((32 + (i * 32)) + 64, 224 + 112, 33, 33, (float)2/5, (float)1/2, (float)3/5, 1.0f);
  160.     }
  161.     for(int i = 0; i < 6; i++){
  162.         drawImage(0 + 64, (32 + (i * 32)) + 112, 33, 33, (float)2/5, 0.0f, (float)3/5, (float)1/2);
  163.         drawImage(480 + 64, (32 + (i * 32)) + 112, 33, 33, (float)3/5, (float)1/2, (float)4/5, 1.0f);
  164.     }
  165.  
  166.     drawImage(32 + 64, 32 + 112, 449, 193, (float)4/5, 0.0f, 1.0f, (float)1/2);
  167.    
  168.     renderStringTemporary(text, 64 + 32, 112 + 32);
  169. }
  170.  
  171. void calcCamAngle(float angle){
  172.     cam.look.x = cam.pos.x + (1 * sin(angle));
  173.     cam.look.z = cam.pos.z + (1 * cos(angle));
  174.     cam.look.y = cam.pos.y;
  175. }
  176.  
  177. float a = 0.0f;
  178.  
  179. void powerBtnCallback(){
  180.     SYS_ResetSystem(SYS_POWEROFF, 0, 0);
  181. }
  182.  
  183. int main(void);
  184.  
  185. void resetBtnCallback(){
  186.     SYS_ResetSystem(SYS_HOTRESET, 0, 0);
  187. }
  188.  
  189. int main(void) {
  190.     SYS_SetPowerCallback(powerBtnCallback);
  191.     SYS_SetResetCallback(resetBtnCallback);
  192.     WPAD_SetPowerButtonCallback(powerBtnCallback);
  193.    
  194.     initSkybox(&skybox);
  195.     initTerrain(&terrain);
  196.     initWater(&water);
  197.    
  198.     for(int i = 0; i < 256; i++){
  199.         snowflakes[i] = (Snowflake){{rand() % 64, rand() % 64, rand() % 64}, {0.0f, 0.0f, 0.0f}, {0.0f, rand() % 360, 0.0f}};
  200.     }
  201.  
  202.     Mtx44   view;
  203.     GXColor backgroundColor = {0, 0, 0, 255};
  204.     void *fifoBuffer = NULL;
  205.  
  206.     VIDEO_Init();
  207.     WPAD_Init();
  208.  
  209.     screenMode = VIDEO_GetPreferredMode(NULL);
  210.  
  211.     frameBuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode));
  212.  
  213.     VIDEO_Configure(screenMode);
  214.     VIDEO_SetNextFramebuffer(frameBuffer);
  215.     VIDEO_SetPostRetraceCallback(copy_buffers);
  216.     VIDEO_SetBlack(FALSE);
  217.     VIDEO_Flush();
  218.  
  219.     fifoBuffer = MEM_K0_TO_K1(memalign(32,FIFO_SIZE));
  220.     memset(fifoBuffer,  0, FIFO_SIZE);
  221.  
  222.     GX_Init(fifoBuffer, FIFO_SIZE);
  223.     GX_SetCopyClear(backgroundColor, 0x00ffffff);
  224.     GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
  225.     GX_SetDispCopyYScale((f32)screenMode->xfbHeight/(f32)screenMode->efbHeight);
  226.     GX_SetScissor(0,0,screenMode->fbWidth,screenMode->efbHeight);
  227.     GX_SetDispCopySrc(0,0,screenMode->fbWidth,screenMode->efbHeight);
  228.     GX_SetDispCopyDst(screenMode->fbWidth,screenMode->xfbHeight);
  229.     GX_SetCopyFilter(screenMode->aa,screenMode->sample_pattern,
  230.                      GX_TRUE,screenMode->vfilter);
  231.     GX_SetFieldMode(screenMode->field_rendering,
  232.                     ((screenMode->viHeight==2*screenMode->xfbHeight)?GX_ENABLE:GX_DISABLE));
  233.  
  234.     GX_SetCullMode(GX_CULL_NONE);
  235.     GX_CopyDisp(frameBuffer,GX_TRUE);
  236.     GX_SetDispCopyGamma(GX_GM_1_0);
  237.  
  238.     guPerspective(projection, 60, 1.33F, 10.0F, 300.0F);
  239.     GX_LoadProjectionMtx(projection, GX_PERSPECTIVE);
  240.    
  241.     guOrtho(perspective,0,479,0,639,0,300);
  242.  
  243.     GX_ClearVtxDesc();
  244.     GX_SetVtxDesc(GX_VA_POS, GX_INDEX16);
  245.     //GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX16);
  246.     GX_SetVtxDesc(GX_VA_TEX0, GX_INDEX8);
  247.    
  248.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
  249.     //GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8,   0);
  250.     GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
  251.    
  252.     GX_SetVtxAttrFmt(GX_VTXFMT1, GX_VA_POS, GX_POS_XY, GX_F32, 0);
  253.     GX_SetVtxAttrFmt(GX_VTXFMT1, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
  254.    
  255.     GX_SetNumChans(1);
  256.     GX_SetNumTexGens(1);
  257.    
  258.     GX_SetNumChans(1);
  259.     GX_SetNumTexGens(1);
  260.     GX_SetTevOp(GX_TEVSTAGE0, GX_REPLACE);
  261.     GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
  262.     GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
  263.    
  264.     TPLFile texTPL;
  265.     TPL_OpenTPLFromMemory(&texTPL, (void *)textures_tpl, textures_tpl_size);
  266.     TPL_GetTexture(&texTPL, grass, &grassTex);
  267.     TPL_GetTexture(&texTPL, waterTxr, &waterTex);
  268.     TPL_GetTexture(&texTPL, snowflake, &snowflakeTex);
  269.    
  270.     TPL_GetTexture(&texTPL, msgBox, &msgBoxTex);
  271.    
  272.     TPL_GetTexture(&texTPL, font, &fontTex);
  273.    
  274.     TPL_GetTexture(&texTPL, skyboxTxr, &skyboxTex);
  275.  
  276.     while (1) {
  277.         calcCamAngle(a);
  278.         guLookAt(view, &cam.pos, &cam.up, &cam.look);
  279.         GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
  280.         GX_InvVtxCache();
  281.         GX_InvalidateTexAll();
  282.         update_screen(view);
  283.  
  284.         WPAD_ScanPads();
  285.         if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) exit(0);
  286.         if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_LEFT) a -= 0.01f;
  287.         if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_RIGHT) a += 0.01f;
  288.        
  289.         if(WPAD_ButtonsDown(0) & WPAD_BUTTON_MINUS){
  290.             if(preCamIndex > 0) preCamIndex--;
  291.             cam.pos.x = camLocations[preCamIndex].pos.x;
  292.             cam.pos.y = camLocations[preCamIndex].pos.y;
  293.             cam.pos.z = camLocations[preCamIndex].pos.z;
  294.         }
  295.        
  296.         if(WPAD_ButtonsDown(0) & WPAD_BUTTON_PLUS){
  297.             if(preCamIndex < 4) preCamIndex++;
  298.             cam.pos.x = camLocations[preCamIndex].pos.x;
  299.             cam.pos.y = camLocations[preCamIndex].pos.y;
  300.             cam.pos.z = camLocations[preCamIndex].pos.z;
  301.         }
  302.        
  303.         WPAD_Expansion(WPAD_CHAN_0, &expCon);
  304.         if(expCon.type == WPAD_EXP_NUNCHUK){
  305.             nunchukConnected = true;
  306.             nx = (expCon.nunchuk.js.pos.x - expCon.nunchuk.js.center.x);
  307.             if((nx > -2) && (nx < 2)) nx = 0;
  308.             ny = (expCon.nunchuk.js.pos.y - expCon.nunchuk.js.center.y);
  309.             if((ny > -2) && (ny < 2)) ny = 0;
  310.             cam.pos.x += (ny * 0.01f) * (cam.look.x - cam.pos.x);
  311.             cam.pos.z += (ny * 0.01f) * (cam.look.z - cam.pos.z);
  312.             cam.pos.x += -(nx * 0.01f) * cam.side.x;
  313.             cam.pos.z += -(nx * 0.01f) * cam.side.z;
  314.             if(expCon.nunchuk.btns_held & NUNCHUK_BUTTON_C) cam.pos.y += 0.1f;
  315.             if(expCon.nunchuk.btns_held & NUNCHUK_BUTTON_Z) cam.pos.y -= 0.1f;
  316.         } else if(expCon.type == WPAD_EXP_NONE){
  317.             nunchukConnected = false;
  318.         }
  319.        
  320.         cam.side.x = (cam.look.z - cam.pos.z);
  321.         cam.side.z = -(cam.look.x - cam.pos.x);
  322.        
  323.         cam.up.y = 1.0f;
  324.     }
  325.     return 0;
  326. }
  327.  
  328. void update_screen(Mtx44 viewMatrix) {
  329.     updateSkybox(&skybox, cam);
  330.     updateWater(&water);
  331.  
  332.     GX_InvVtxCache();
  333.     GX_InvalidateTexAll();
  334.    
  335.     GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
  336.     GX_LoadProjectionMtx(projection, GX_PERSPECTIVE);
  337.  
  338.     Mtx44 rot;
  339.     Mtx44 modelView;
  340.    
  341.     GX_SetVtxDesc(GX_VA_POS, GX_INDEX16);
  342.     GX_SetVtxDesc(GX_VA_TEX0, GX_INDEX8);
  343.    
  344.     GX_LoadTexObj(&skyboxTex, GX_TEXMAP0);
  345.    
  346.     guMtxIdentity(modelView);
  347.     guMtxIdentity(rot);
  348.     guMtxScale(rot, 100.0f, 100.0f, 100.0f);
  349.     guMtxConcat(rot,modelView,modelView);
  350.     guMtxTransApply(modelView, modelView, cam.pos.x, cam.pos.y, cam.pos.z);
  351.     guMtxConcat(viewMatrix,modelView,modelView);
  352.     GX_LoadPosMtxImm(modelView, GX_PNMTX0);
  353.     renderSkybox(&skybox);
  354.    
  355.    
  356.     guMtxIdentity(modelView);
  357.     guMtxIdentity(rot);
  358.     guMtxConcat(viewMatrix,modelView,modelView);
  359.     GX_LoadPosMtxImm(modelView, GX_PNMTX0);
  360.     GX_LoadTexObj(&grassTex, GX_TEXMAP0);
  361.     GX_SetArray(GX_VA_TEX0, texCoords, 2*sizeof(float));
  362.     renderTerrain(&terrain);
  363.    
  364.     GX_LoadTexObj(&waterTex, GX_TEXMAP0);
  365.     renderWater(&water);
  366.    
  367.     GX_SetArray(GX_VA_POS, quadVertices, 3 * sizeof(float));
  368.     GX_SetArray(GX_VA_TEX0, quadTexCoords, 2*sizeof(float));
  369.     GX_LoadTexObj(&snowflakeTex, GX_TEXMAP0);
  370.    
  371.     for(int i = 0; i < 256; i++){
  372.         guMtxIdentity(modelView);
  373.         guMtxIdentity(rot);
  374.         guMtxRotDeg(rot, 'x', snowflakes[i].rotation.x);
  375.         guMtxConcat(rot,modelView,modelView);
  376.         guMtxRotDeg(rot, 'y', snowflakes[i].rotation.y);
  377.         guMtxConcat(rot,modelView,modelView);
  378.         guMtxRotDeg(rot, 'z', snowflakes[i].rotation.z);
  379.         guMtxConcat(rot,modelView,modelView);
  380.         guMtxTransApply(modelView, modelView, snowflakes[i].position.x, snowflakes[i].position.y, snowflakes[i].position.z);
  381.         guMtxConcat(viewMatrix,modelView,modelView);
  382.  
  383.         GX_LoadPosMtxImm(modelView, GX_PNMTX0);
  384.         GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 6);
  385.         for(int i = 0; i < 6; i++){
  386.             GX_Position1x16(i);
  387.             GX_TexCoord1x8(i);
  388.         }
  389.         GX_End();
  390.     }
  391.    
  392.     GX_LoadProjectionMtx(perspective, GX_ORTHOGRAPHIC);
  393.    
  394.     GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
  395.     GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);
  396.    
  397.     guMtxIdentity(modelView);
  398.  
  399.     GX_LoadPosMtxImm(modelView, GX_PNMTX0);
  400.    
  401.     nunchukConnected = true;
  402.     if(!nunchukConnected){
  403.         messageBox(NUNCHUK_MSG);
  404.     }
  405.    
  406.     char tempText[256];
  407.     sprintf(tempText, "Camera Position: (%g, %g, %g)", cam.pos.x, cam.pos.y, cam.pos.z);
  408.     renderStringTemporary(tempText, 20, 20);
  409.     sprintf(tempText, "Analog Position: (%d, %d)", nx, ny);
  410.     renderStringTemporary(tempText, 20, 40);
  411.     sprintf(tempText, "Camera Look Position: (%g, %g, %g)", cam.look.x, cam.look.y, cam.look.z);
  412.     renderStringTemporary(tempText, 20, 60);
  413.     sprintf(tempText, "Camera Facing Vector: (%g, %g)", cam.look.x - cam.pos.x, cam.look.z - cam.pos.z);
  414.     renderStringTemporary(tempText, 20, 80);
  415.     sprintf(tempText, "Camera Sideways Vector: (%g, %g)", cam.side.x, cam.side.z);
  416.     renderStringTemporary(tempText, 20, 100);
  417.     sprintf(tempText, "Camera Up Vector: (%g, %g, %g)", cam.up.x, cam.up.y, cam.up.z);
  418.     renderStringTemporary(tempText, 20, 120);
  419.  
  420.     GX_DrawDone();
  421.     readyForCopy = GX_TRUE;
  422.    
  423.     for(int i = 0; i < 256; i++){
  424.         snowflakes[i].position.y -= 0.1f;
  425.         snowflakes[i].rotation.x += 1.0f;
  426.         snowflakes[i].rotation.y += 1.0f;
  427.         snowflakes[i].rotation.z += 1.0f;
  428.         if(snowflakes[i].position.y <= -2.0f){
  429.             snowflakes[i].position.y = 64.0f;
  430.         }
  431.     }
  432.  
  433.     VIDEO_WaitVSync();
  434.     return;
  435. }
  436.  
  437. static void copy_buffers(u32 count __attribute__ ((unused)))
  438. {
  439.     if (readyForCopy==GX_TRUE) {
  440.         GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
  441.         GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
  442.         GX_SetAlphaUpdate(GX_TRUE);
  443.         GX_SetColorUpdate(GX_TRUE);
  444.         GX_CopyDisp(frameBuffer,GX_TRUE);
  445.         GX_Flush();
  446.         readyForCopy = GX_FALSE;
  447.     }
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement