/source/main.c

http://github.com/Vect/fail0verflow2 · C · 274 lines · 164 code · 96 blank · 14 comment · 19 complexity · 762449b222491c8d0bba801667bdfae1 MD5 · raw file

  1. /*
  2. TINY3D sample / (c) 2010 Hermes <www.elotrolado.net>
  3. */
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <unistd.h>
  9. #include <math.h>
  10. #include <io/pad.h>
  11. #include <psl1ght/lv2.h>
  12. #include <psl1ght/lv2/spu.h>
  13. #include <lv2/spu.h>
  14. #include <sysmodule/sysmodule.h>
  15. #include <pngdec/loadpng.h>
  16. #include <tiny3d.h>
  17. #include "spu_soundmodule.bin.h" // load SPU Module
  18. #include "spu_soundlib.h"
  19. #include "audioplayer.h"
  20. #include "geohotlightsitup_mp3.bin.h"
  21. typedef struct
  22. {
  23. int w, h;
  24. int stride;
  25. u32 rsx_offset;
  26. u8 * bitmap;
  27. int color_format;
  28. } Surface;
  29. Surface surface1;
  30. extern unsigned *video_text;
  31. // draw one background color in virtual 2D coordinates
  32. void DrawBackground2D(u32 rgba)
  33. {
  34. tiny3d_SetPolygon(TINY3D_QUADS);
  35. tiny3d_VertexPos(0 , 0 , 65535);
  36. tiny3d_VertexColor(rgba);
  37. tiny3d_VertexPos(847, 0 , 65535);
  38. tiny3d_VertexPos(847, 511, 65535);
  39. tiny3d_VertexPos(0 , 511, 65535);
  40. tiny3d_End();
  41. }
  42. #define SURFACE_RGBA(r, g, b, a) (((a)<<24) | ((r)<<16) | ((g)<<8) | (b))
  43. u8 * CreateSurface(Surface *surface, u8 *texture, int w, int h, text_format color_format)
  44. {
  45. texture = (u8 *) ((((long) texture) + 15) & ~15);
  46. surface->rsx_offset = tiny3d_TextureOffset(texture);
  47. surface->bitmap = texture;
  48. surface->color_format = color_format;
  49. surface->w = w;
  50. surface->h = h;
  51. switch(color_format) {
  52. case TINY3D_TEX_FORMAT_L8: // this format is unsupported
  53. case TINY3D_TEX_FORMAT_A8R8G8B8:
  54. surface->color_format = TINY3D_TEX_FORMAT_A8R8G8B8; // force color
  55. surface->stride = w * 4;
  56. texture += surface->stride * surface->h;
  57. texture = (u8 *) ((((long) texture) + 15) & ~15);
  58. break;
  59. default:
  60. surface->color_format = TINY3D_TEX_FORMAT_A1R5G5B5; // force color
  61. surface->stride = w * 2;
  62. texture += surface->stride * surface->h;
  63. texture = (u8 *) ((((long) texture) + 15) & ~15);
  64. break;
  65. }
  66. return texture;
  67. }
  68. void DrawSurface(Surface *surface, float x, float y, float z, float w, float h, int smooth)
  69. {
  70. // Load surface texture
  71. tiny3d_SetTextureWrap(0, surface->rsx_offset, surface->w, surface->h, surface->stride, surface->color_format, TEXTWRAP_CLAMP, TEXTWRAP_CLAMP, smooth);
  72. tiny3d_SetPolygon(TINY3D_QUADS);
  73. tiny3d_VertexPos(x , y , z);
  74. tiny3d_VertexColor(0xffffffff);
  75. tiny3d_VertexTexture(0.0f, 0.0f);
  76. tiny3d_VertexPos(x + w, y , z);
  77. tiny3d_VertexTexture(0.999f, 0.0f);
  78. tiny3d_VertexPos(x + w, y + h, z);
  79. tiny3d_VertexTexture(0.999f, 0.999f);
  80. tiny3d_VertexPos(x , y + h, z);
  81. tiny3d_VertexTexture(0.0f, 0.999f);
  82. tiny3d_End();
  83. }
  84. void drawScene()
  85. {
  86. tiny3d_Clear(0xff000000, TINY3D_CLEAR_ALL);
  87. tiny3d_Project2D(); // change to 2D context (remember you it works with 848 x 512 as virtual coordinates)
  88. // fix Perspective Projection Matrix
  89. DrawBackground2D(0x00102fff) ; // light blue
  90. DrawSurface(&surface1, (Video_aspect != 1) ? (848-682)/2 : 0, 0.0f, 1.0f, (Video_aspect != 1) ? 682 : 848, 512.0f, 1);
  91. /* DRAWING FINISH HERE */
  92. tiny3d_Flip();
  93. }
  94. void LoadTexture()
  95. {
  96. u32 * texture_mem = tiny3d_AllocTexture(64*1024*1024); // alloc 64MB of space for textures (this pointer can be global)
  97. u32 * texture_pointer; // use to asign texture space without changes texture_mem
  98. if(!texture_mem) return; // fail!
  99. texture_pointer = texture_mem;
  100. // copy texture datas from PNG to the RSX memory allocated for textures
  101. // creates one surface of 320 x 200 and 32 bits
  102. texture_pointer = (u32 *) CreateSurface(&surface1, (u8 *) texture_pointer, 256, 192, TINY3D_TEX_FORMAT_A8R8G8B8);
  103. // clear surface 1
  104. memset (surface1.bitmap, 0, surface1.stride * surface1.h);
  105. }
  106. int inited = 0;
  107. #define INITED_SPU 2
  108. #define INITED_SOUNDLIB 4
  109. // SPU
  110. u32 spu = 0;
  111. sysSpuImage spu_image;
  112. #define SPU_SIZE(x) (((x)+127) & ~127)
  113. void exiting()
  114. {
  115. StopAudio();
  116. if(inited & INITED_SOUNDLIB)
  117. SND_End();
  118. if(inited & INITED_SPU) {
  119. sleep(1);
  120. lv2SpuRawDestroy(spu);
  121. sysSpuImageClose(&spu_image);
  122. }
  123. inited=0;
  124. }
  125. extern int pintor();
  126. extern unsigned hiscore;
  127. char hiscore_path[MAXPATHLEN];
  128. s32 main(s32 argc, const char* argv[])
  129. {
  130. u32 entry = 0;
  131. u32 segmentcount = 0;
  132. sysSpuSegment * segments;
  133. tiny3d_Init(1024*1024);
  134. ioPadInit(7);
  135. if(argc>0 && argv) {
  136. int n;
  137. strcpy(hiscore_path, argv[0]);
  138. n= 0;
  139. while(hiscore_path[n] != 0) n++;
  140. while(hiscore_path[n] != '/' && n > 1) n--;
  141. if(hiscore_path[n] == '/') {
  142. sprintf(&hiscore_path[n], "%s", "/hiscore.bin");
  143. } else strcpy(hiscore_path, "/dev_hdd0/temp/hiscore.bin");
  144. }
  145. // SysLoadModule(SYSMODULE_PNGDEC);
  146. atexit(exiting); // Tiny3D register the event 3 and do exit() call when you exit to the menu
  147. // Load texture
  148. LoadTexture();
  149. lv2SpuInitialize(6, 5);
  150. lv2SpuRawCreate(&spu, NULL);
  151. sysSpuElfGetInformation(spu_soundmodule_bin, &entry, &segmentcount);
  152. size_t segmentsize = sizeof(sysSpuSegment) * segmentcount;
  153. segments = (sysSpuSegment*)memalign(128, SPU_SIZE(segmentsize)); // must be aligned to 128 or it break malloc() allocations
  154. memset(segments, 0, segmentsize);
  155. sysSpuElfGetSegments(spu_soundmodule_bin, segments, segmentcount);
  156. sysSpuImageImport(&spu_image, spu_soundmodule_bin, 0);
  157. sysSpuRawImageLoad(spu, &spu_image);
  158. inited |= INITED_SPU;
  159. if(SND_Init(spu)==0) inited |= INITED_SOUNDLIB;
  160. FILE *fp = fopen(hiscore_path, "r");
  161. if(fp) {
  162. fread(&hiscore, 1, 4, fp);
  163. fclose(fp);
  164. }
  165. SND_Pause(0);
  166. fp= (FILE *) mem_open( (char *) geohotlightsitup_mp3_bin, sizeof(geohotlightsitup_mp3_bin));
  167. PlayAudiofd(fp, 0, AUDIO_INFINITE_TIME);
  168. SetVolumeAudio(255);
  169. video_text = (unsigned *) surface1.bitmap;
  170. pintor();
  171. fp = fopen(hiscore_path, "w");
  172. if(fp) {
  173. fwrite(&hiscore, 1, 4, fp);
  174. fclose(fp);
  175. }
  176. return 0;
  177. }