PageRenderTime 285ms CodeModel.GetById 131ms app.highlight 18ms RepoModel.GetById 133ms app.codeStats 0ms

/source/main.c

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