Advertisement
atm959

main.cpp

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