- Main Page
- Modules
- Data Structures
- Files

- Search for |

# vlib.c

    00001 /*
    00002
    00003   Velocity  v1.5
    00004   Copyright 2006, 2007 Gabriel Anderson
    00005
    00006   This program is free software; you can redistribute it and/or modify
    00007     it under the terms of the GNU General Public License as published by
    00008     the Free Software Foundation; either version 2 of the License, or
    00009     (at your option) any later version.
    00010
    00011     This program is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014     GNU General Public License for more details.
    00015
    00016     You should have received a copy of the GNU General Public License
    00017     along with this program; if not, write to the Free Software
    00018     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    00019
    00020 */
    00021
    00022 //---------------------------------------------------------------------------------//
    00023 // Includes                                                                        //
    00024 //---------------------------------------------------------------------------------//
    00025
    00026 #include "vlib.h"
    00027
    00028 //---------------------------------------------------------------------------------//
    00029 // Callbacks                                                                       //
    00030 //---------------------------------------------------------------------------------//
    00031
    00032 // Exit callback
    00033 static int exit_callback ( int arg1, int arg2, void *common ) {
    00034   sceKernelExitGame();
    00035   return 0;
    00036 }
    00037
    00038 // Callback thread
    00039 static int CallbackThread ( SceSize args, void *argp ) {
    00040   int cbid;
    00041
    00042   cbid = sceKernelCreateCallback ( "Exit Callback", exit_callback, NULL );
    00043   sceKernelRegisterExitCallback ( cbid );
    00044
    00045   sceKernelSleepThreadCB();
    00046
    00047   return 0;
    00048 }
    00049
    00050 // Sets up the callback thread and returns its thread id
    00051 static int SetupCallbacks ( void ) {
    00052   int thid = 0;
    00053
    00054   thid = sceKernelCreateThread ( "update_thread", CallbackThread, 0x11, 0xFA0, 0, 0 );
    00055   if ( thid >= 0 )
    00056     sceKernelStartThread ( thid, 0, 0 );
    00057
    00058   return thid;
    00059 }
    00060
    00061
    00062 //---------------------------------------------------------------------------------//
    00063 // General                                                                         //
    00064 //---------------------------------------------------------------------------------//
    00065
    00066 u32* fbp0;
    00067 u32* fbp1;
    00068 u16* zbp;
    00069 unsigned int __attribute__ ( ( aligned ( 16 ) ) ) DList[262144];
    00070 int curFps = 0, lastFps = 0;    // for calculating the frames per second
    00071
    00072 u32 tickResolution;
    00073 u64 fpsTickNow;
    00074 u64 fpsTickLast;
    00075
    00076 // More variables for time based movement (if anyone knows how I could rid my self of these (if I use the last two, it causes issues)
    00077 // and/or knows how I could make them u32 and have them still work, please tell me ASAP!
    00078 u64 timeNow;
    00079 u64 timeLastAsk;
    00080
    00081 CONTROLLER buttonInit;
    00082 SceCtrlData ctrl;
    00083
    00084 const int BUF_WIDTH = 512;
    00085 const int SCR_WIDTH = 480;
    00086 const int SCR_HEIGHT = 272;
    00087 const int FRAMEBUFFER_SIZE = 512*272*sizeof ( u32 );
    00088 const int DEPTHBUFFER_SIZE = 512*272*sizeof ( u16 );
    00089
    00090 const int PRINT_BUFF = 512;
    00091
    00092 const int IN_RAM = 0;
    00093 const int IN_VRAM = 1;
    00094
    00095 const int OFF = 0;
    00096 const int ON = 1;
    00097
    00098 const int DRAW_2D = 0;
    00099 const int DRAW_3D = 1;
    00100
    00101 const float PI = 3.141593;
    00102 const float PI_180 = ( int ) 3.141593 / 180.0;
    00103
    00104 float deltaTime = 0;
    00105
    00106 short drawingStarted = 0;
    00107
    00108 ScePspFMatrix4 m_ortho, m_projection, m_projection_view, m_projection_model, m_ortho_view, m_ortho_model;
    00109
    00110 void start_vLib ( void ) {
    00111   pspDebugScreenInit();
    00112   SetupCallbacks();
    00113   sceKernelDcacheWritebackAll();
    00114
    00115   sceCtrlSetSamplingCycle ( 0 );
    00116   sceCtrlSetSamplingMode ( 1 );
    00117
    00118   button = &buttonInit;
    00119
    00120   sceRtcGetCurrentTick ( &fpsTickLast );
    00121   sceRtcGetCurrentTick ( &timeLastAsk );
    00122   tickResolution = sceRtcGetTickResolution();
    00123
    00124   fbp0 = ( u32* ) vrelptr ( valloc ( FRAMEBUFFER_SIZE ) );
    00125   fbp1 = ( u32* ) vrelptr ( valloc ( FRAMEBUFFER_SIZE ) );
    00126   zbp = ( u16* ) vrelptr ( valloc ( DEPTHBUFFER_SIZE ) );
    00127
    00128   sceKernelDcacheWritebackAll();
    00129
    00130   // Setup GU
    00131
    00132   sceGuInit();
    00133   sceGuStart ( GU_DIRECT, DList );
    00134
    00135   sceGuDrawBuffer ( GU_PSM_8888, fbp0, BUF_WIDTH );
    00136   sceGuDispBuffer ( SCR_WIDTH, SCR_HEIGHT, fbp1, BUF_WIDTH );
    00137   sceGuDepthBuffer ( zbp, BUF_WIDTH );
    00138
    00139   sceKernelDcacheWritebackAll();
    00140
    00141   sceGuOffset ( 2048 - ( SCR_WIDTH / 2 ), 2048 - ( SCR_HEIGHT / 2 ) );
    00142   sceGuViewport ( 2048, 2048, SCR_WIDTH, SCR_HEIGHT );
    00143   sceGuDepthRange ( 65535, 0 );
    00144
    00145   // Setup the scissor test
    00146   sceGuScissor ( 0, 0, SCR_WIDTH, SCR_HEIGHT );
    00147   sceGuEnable ( GU_SCISSOR_TEST );
    00148   // Setup the depth testing
    00149   sceGuDepthFunc ( GU_GEQUAL );
    00150   sceGuEnable ( GU_DEPTH_TEST );
    00151   // Enable clipping
    00152   sceGuDisable ( GU_CLIP_PLANES );
    00153   // Setup face culling
    00154   sceGuFrontFace ( GU_CW );
    00155   sceGuDisable ( GU_CULL_FACE );
    00156   // Setup shading (not lighting!)
    00157   sceGuShadeModel ( GU_SMOOTH );
    00158   // Setup textures
    00159   sceGuTexMode ( GU_PSM_8888, 0, 0, 1 );
    00160   sceGuTexFunc ( GU_TFX_REPLACE, GU_TCC_RGBA );
    00161   // Enable 2D textures
    00162   sceGuEnable ( GU_TEXTURE_2D );
    00163   // Do alpha blending
    00164   sceGuBlendFunc ( GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0 );
    00165   sceGuEnable ( GU_BLEND );
    00166   // Setup the alpha test
    00167   sceGuAlphaFunc ( GU_GREATER, 0, 0xff );
    00168   sceGuEnable ( GU_ALPHA_TEST );
    00169   // Setup the color/depth for clearing the screen
    00170   sceGuClearColor ( RGB ( 0, 0, 0 ) );
    00171   sceGuClearDepth ( 0 );
    00172
    00173   sceGuFinish();
    00174   sceGuSync ( 0, 0 );
    00175
    00176   sceDisplayWaitVblankStart();
    00177   sceGuDisplay ( GU_TRUE );
    00178
    00179   sceKernelDcacheWritebackAll();
    00180
    00181   vfpu_perspective_m ( &m_projection, 75.0f, 16.0f / 9.0f, .5f, 1000.0f );
    00182   vfpu_ortho_m ( &m_ortho, 0.0f, 480.0f, 272.0f, 0.0f, -1.0f, 1.0f );
    00183   vfpu_identity_m ( &m_projection_view );
    00184   vfpu_identity_m ( &m_projection_model );
    00185   vfpu_identity_m ( &m_ortho_view );
    00186   vfpu_identity_m ( &m_ortho_model );
    00187 }
    00188
    00189 void stop_vLib ( void ) {
    00190   vfree ( fbp0 );
    00191   vfree ( fbp1 );
    00192   vfree ( zbp );
    00193   free ( DList );
    00194   sceGuTerm();
    00195 }
    00196
    00197 void start_drawing ( void ) {
    00198   if ( drawingStarted )
    00199     return;
    00200   sceGuStart ( GU_DIRECT, DList );
    00201   drawingStarted = 1;
    00202 }
    00203
    00204 void end_drawing ( void ) {
    00205   if ( !drawingStarted )
    00206     return;
    00207   sceGuFinish();
    00208   sceGuSync ( 0, 0 );
    00209   drawingStarted = 0;
    00210 }
    00211
    00212 void sync_frame ( void ) {
    00213   if ( drawingStarted )
    00214     return;
    00215   asm __volatile__ ( "\nsyscall 0x2147\n" );
    00216   fbp0 = sceGuSwapBuffers();
    00217 }
    00218
    00219 void flip_screen ( void ) {
    00220   if ( drawingStarted )
    00221     return;
    00222   fbp0 = sceGuSwapBuffers();
    00223 }
    00224
    00225 void clear_screen ( void ) {
    00226   if ( !drawingStarted )
    00227     return;
    00228   sceGuClear ( GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT );
    00229 }
    00230
    00231 void set_clear_color ( u32 color ) {
    00232   sceGuClearColor ( color );
    00233 }
    00234
    00235 void wait_to_vsync ( void ) {
    00236   asm __volatile__ ( "\nsyscall 0x2147\n" );
    00237 }
    00238
    00239 void update_delta_time ( void ) {
    00240   sceRtcGetCurrentTick ( &timeNow );
    00241   deltaTime = ( float ) ( ( timeNow - timeLastAsk ) / ( ( float ) tickResolution ) );
    00242   timeLastAsk = timeNow;
    00243 }