Advertisement
atm959

main.c (PlayStation)

Jul 18th, 2018
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.79 KB | None | 0 0
  1. /*
  2. ===========================================================
  3.                 Sony PlayStation 1 Source Code
  4. ===========================================================
  5.                          TIM EXAMPLE
  6. -----------------------------------------------------------
  7.  
  8.     Developer / Programmer..............: PSXDEV.net
  9.     Software Ddevelopment Kit...........: PSY-Q
  10.     First Release.......................: 26/JANUARY/2013
  11.     Last Release........................: 03/JULY/2013
  12.     Version.............................: 1.2
  13.  
  14.   Copyright (C) 1994,1995 by Sony Computer Entertainment Inc.
  15.                      All Rights Reserved.
  16.  
  17.   Sony Computer Entertainment Inc. Development Department
  18.  
  19.                     http://psxdev.net/
  20.  
  21. -----------------------------------------------------------*/
  22. #include <sys/types.h>
  23. #include <libgte.h>
  24. #include <libgpu.h>
  25. #include <libetc.h>
  26. #include <libgs.h>
  27.  
  28. //Image files
  29. #include "images/grassTileset.h"
  30.  
  31. #define OT_LENGTH 1                              // ordering table length
  32. #define PACKETMAX (300)                          // the max num of objects
  33.  
  34. GsOT      WorldOrderingTable[2];                 // ordering table handlers
  35. GsOT_TAG  OrderingTable[2][1<<OT_LENGTH];        // ordering tables
  36. PACKET    GPUOutputPacket[2][PACKETMAX];         // GPU Packet work area
  37.  
  38. GsSPRITE tiles[64];
  39.  
  40. unsigned long __ramsize =   0x00200000;          // force 2 megabytes of RAM
  41. unsigned long __stacksize = 0x00004000;          // force 16 kilobytes of stack
  42.  
  43. extern unsigned char grassTileset[];
  44.  
  45. int SCREEN_WIDTH, SCREEN_HEIGHT;        
  46.  
  47. // --------
  48. // integers
  49. // --------
  50. int currentbuffer; // double buffer holder
  51. char version[5]="V1.2"; // version number
  52.  
  53. char i;
  54. int j;
  55.  
  56. // ----------
  57. // prototypes
  58. // ----------
  59. void initGraphics(); // initialise the graphics
  60. void initFont();
  61. void initGraphics();
  62. void clearVRAM();
  63. void initImages();
  64. void display();
  65. int getTimWidth(unsigned char timData[]);
  66. int getTimHeight(unsigned char timData[]);
  67. void loadImageToFB(unsigned char timData[]);
  68. GsSPRITE initSprite(unsigned char timData[], int tX, int tY, int u, int v, int w, int h);
  69. void readPad();
  70.  
  71. // ----------------
  72. // system debugging
  73. // ----------------
  74. const DEBUG = 1; // light/general debugging
  75.  
  76. int main()
  77. {
  78.     printf("\nPLATFORMER FOR PLAYSTATION [BEGIN]\n\n");
  79.  
  80.     ResetCallback(); // initialises all system callbacks
  81.    
  82.     PadInit(0);
  83.    
  84.     initGraphics(); // this will initialise the graphics
  85.     if (DEBUG) initFont(); // initialise the psy-q debugging font
  86.     initImages(); // initialise the TIM image
  87.    
  88.     while(1)
  89.     {
  90.         readPad();
  91.        
  92.         if(DEBUG){
  93.            
  94.         }
  95.        
  96.         for(j = 0; j < 64; j++){
  97.             tiles[j].x = (j % 16) * 16;
  98.             tiles[j].y = (j / 16) * 16;
  99.         }
  100.        
  101.         display();
  102.     }
  103.  
  104.     ResetGraph(3); // set the video mode back
  105.     PadStopCom(); // stop controller communications
  106.    
  107.     if (DEBUG) printf("\nPLATFORMER FOR PLAYSTATION [COMPLETE]\n");
  108.     return 0;
  109. }
  110.  
  111. void initFont()
  112. {
  113.   FntLoad(960, 256);  // load basic font pattern
  114.   if (DEBUG) printf("Font Loaded (Framebuffer x=960, y=256)\n");
  115.  
  116.   // screen X,Y | max text length X,Y | automatic background clear 0,1 | max characters (eg: 50).
  117.   SetDumpFnt(FntOpen(10, 10, 320, 240, 0, 512));
  118. }
  119.  
  120.  
  121. void initGraphics()
  122. {
  123.    // puts display mask into the status specified by the mask (0 not displayed, 1 displayed)
  124.    SetDispMask(1);
  125.    
  126.    // resets the graphic system (0 full reset, 1 cancels the current drawing and flushes the command buffer, 3 initialises the drawing engine while preserving the current display environment)
  127.    ResetGraph(0);
  128.  
  129.    // clear all VRAM contents
  130.    //clearVRAM();
  131.  
  132.    // automatic video mode control
  133.    if (*(char *)0xbfc7ff52=='E')  // SCEE string address
  134.    {
  135.      // PAL MODE
  136.      SCREEN_WIDTH = 320;
  137.      SCREEN_HEIGHT = 256;
  138.      if (DEBUG) printf("Setting the PlayStation Video Mode to (PAL %dx%d)\n",SCREEN_WIDTH,SCREEN_HEIGHT,")");
  139.      SetVideoMode(1);
  140.      if (DEBUG) printf("Video Mode is (%d)\n",GetVideoMode());
  141.    }
  142.    else
  143.    {
  144.      // NTSC MODE
  145.      SCREEN_WIDTH = 320;
  146.      SCREEN_HEIGHT = 240;
  147.      if (DEBUG) printf("Setting the PlayStation Video Mode to (NTSC %dx%d)\n",SCREEN_WIDTH,SCREEN_HEIGHT,")");
  148.      SetVideoMode(0);
  149.      if (DEBUG) printf("Video Mode is (%d)\n",GetVideoMode());
  150.    }
  151.  
  152.    // set the graphics mode resolutions
  153.    GsInitGraph(SCREEN_WIDTH, SCREEN_HEIGHT, GsNONINTER|GsOFSGPU, 1, 0);
  154.  
  155.    // set the top left coordinates of the two buffers in video memory
  156.    GsDefDispBuff(0, 0, 0, SCREEN_HEIGHT);
  157.  
  158.    // initialise the ordering tables
  159.    WorldOrderingTable[0].length = OT_LENGTH;
  160.    WorldOrderingTable[1].length = OT_LENGTH;
  161.    WorldOrderingTable[0].org = OrderingTable[0];
  162.    WorldOrderingTable[1].org = OrderingTable[1];
  163.    
  164.    GsClearOt(0,0,&WorldOrderingTable[0]);
  165.    GsClearOt(0,0,&WorldOrderingTable[1]);
  166.  
  167.    if (DEBUG) printf("Graphics Initilised!\n");
  168. }
  169.  
  170. void clearVRAM() {
  171.     RECT rectTL;
  172.    
  173.     setRECT(&rectTL, 0, 0, 1024, 512);
  174.     ClearImage2(&rectTL, 0, 0, 0);
  175.    
  176.     DrawSync(0); // ensure that the VRAM is clear before exiting
  177.    
  178.     if (DEBUG) printf("VRAM cleared!\n");
  179.     return;
  180. }
  181.  
  182.  
  183. // Initialize images
  184. void initImages() {
  185.     loadImageToFB(grassTileset);
  186.    
  187.     //Set both tX and tY to -1 for the tex page position to be based on the TIM's position in the framebuffer
  188.     for(j = 0; j < 64; j++){
  189.         tiles[j] = initSprite(grassTileset, -1, -1, 0, 0, 16, 16);
  190.     }
  191.  
  192.     // wait for all drawing to finish
  193.     DrawSync(0);
  194. }
  195.  
  196.  
  197. void display() {
  198.     // refresh the font
  199.     if (DEBUG) FntFlush(-1);
  200.    
  201.     // get the current buffer
  202.     currentbuffer=GsGetActiveBuff();
  203.    
  204.     // setup the packet workbase
  205.     GsSetWorkBase((PACKET*)GPUOutputPacket[currentbuffer]);
  206.    
  207.     // clear the ordering table
  208.     GsClearOt(0,0,&WorldOrderingTable[currentbuffer]);
  209.    
  210.     // insert sprites into the ordering table
  211.    
  212.     for(j = 0; j < 64; j++){
  213.         GsSortSprite(&tiles[i], &WorldOrderingTable[currentbuffer], j);
  214.     }
  215.    
  216.     // wait for all drawing to finish
  217.     DrawSync(0);
  218.    
  219.     // wait for v_blank interrupt
  220.     VSync(0);
  221.    
  222.     // flip double buffers
  223.     GsSwapDispBuff();
  224.    
  225.     // clear the ordering table with a background color
  226.     GsSortClear(0,0,0,&WorldOrderingTable[currentbuffer]);
  227.    
  228.     // Draw the ordering table for the currentbuffer
  229.     GsDrawOt(&WorldOrderingTable[currentbuffer]);
  230. }
  231.  
  232. int getTimWidth(unsigned char timData[]){
  233.     GsIMAGE         tempTimData;                                // holds tim graphic info
  234.    
  235.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  236.    
  237.     return tempTimData.pw;
  238. }
  239.  
  240.  
  241. int getTimHeight(unsigned char timData[]){
  242.     GsIMAGE tempTimData;                                // holds tim graphic info
  243.    
  244.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  245.    
  246.     return tempTimData.ph;
  247. }
  248.  
  249. void loadImageToFB(unsigned char timData[]){
  250.     RECT tempRect;                                    // RECT structure
  251.     GsIMAGE tempTimData;                                // holds tim graphic info
  252.    
  253.     // put data from tim file into rect        
  254.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  255.    
  256.     // load the image into the frame buffer
  257.     tempRect.x = tempTimData.px;                                    // tim start X coord
  258.     tempRect.y = tempTimData.py;                                    // tim start Y coord
  259.     tempRect.w = tempTimData.pw;                                    // data width
  260.     tempRect.h = tempTimData.ph;                                    // data height
  261.     // load the tim data into the frame buffer
  262.     LoadImage(&tempRect, tempTimData.pixel);      
  263.    
  264.     // load the CLUT into the frame buffer
  265.     tempRect.x = tempTimData.cx;                                    // x pos in frame buffer
  266.     tempRect.y = tempTimData.cy;                                    // y pos in frame buffer
  267.     tempRect.w = tempTimData.cw;                                    // width of CLUT
  268.     tempRect.h = tempTimData.ch;                                    // height of CLUT
  269.     // load data into frame buffer (DMA from DRAM to VRAM)
  270.     LoadImage(&tempRect, tempTimData.clut);                
  271. }
  272.  
  273. GsSPRITE initSprite(unsigned char timData[], int tX, int tY, int u, int v, int w, int h){
  274.     GsSPRITE tempSprite;
  275.     GsIMAGE tempTimData;
  276.    
  277.     // put data from tim file into rect        
  278.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  279.  
  280.     // initialise sprite
  281.     tempSprite.attribute=0x2000000;                           // 16 bit CLUT, all options off (0x1 = 8-bit, 0x2 = 16-bit)
  282.     tempSprite.x = 0;                                         // draw at x coord 0
  283.     tempSprite.y = 0;                                         // draw at y coord 0
  284.     tempSprite.w = w;                                       // width of sprite
  285.     tempSprite.h = h;                               // height of sprite
  286.     // texture page | texture mode (0 4-bit, 1 8-bit, 2 16-bit), semi-transparency rate, texture x, texture y in the framebuffer
  287.     if((tX == -1) && (tY == -1)){
  288.         tempSprite.tpage=GetTPage(1, 2, tempTimData.px, tempTimData.py);
  289.     } else {
  290.         tempSprite.tpage=GetTPage(1, 2, tX, tY);
  291.     }
  292.     tempSprite.r = 128;                                       // RGB Data
  293.     tempSprite.g = 128;
  294.     tempSprite.b = 128;
  295.     tempSprite.u=u;                                           // position within timfile for sprite
  296.     tempSprite.v=v;                                          
  297.     tempSprite.cx = tempTimData.cx;                              // CLUT location x coord
  298.     tempSprite.cy = tempTimData.cy;                              // CLUT location y coord
  299.     tempSprite.mx = 0;                                        // rotation x coord
  300.     tempSprite.my = 0;                                        // rotation y coord
  301.     tempSprite.scalex = ONE;                                  // scale x coord (ONE = 100%)
  302.     tempSprite.scaley = ONE;                                  // scale y coord (ONE = 100%)
  303.     tempSprite.rotate = 0;                                    // degrees to rotate  
  304.    
  305.     return tempSprite;
  306. }
  307.  
  308. void readPad(){
  309.     u_long padd;
  310.     padd = PadRead(1);
  311.    
  312.    
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement