Advertisement
atm959

Wow! Lol

Aug 2nd, 2018
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.22 KB | None | 0 0
  1. /*
  2. ===========================================================
  3.                 Sony PlayStation 1 Source Code
  4. ===========================================================
  5.                         PLATFORMER
  6. -----------------------------------------------------------
  7.  
  8.     Developer / Programmer..............: atm959
  9.     Software Ddevelopment Kit...........: PSY-Q
  10.     First Release.......................: 02/AUGUST/2018
  11.     Last Release........................: 02/AUGUST/2018
  12.     Version.............................: 0.1
  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. #include <stdio.h>
  28. #include <libmcrd.h>
  29.  
  30. //Image files
  31. #include "images/background.h"   //Background TIM
  32. #include "images/grassTileset.h" //Grass Tileset TIM
  33. #include "images/character.h"    //Character TIM
  34.  
  35. #include "levels/level1.h"      //The map
  36. #include "levels/level2.h"
  37.  
  38. #define false 0
  39. #define true 1
  40.  
  41. #define OT_LENGTH 10                              // ordering table length
  42. #define PACKETMAX 4096                          // the max num of objects
  43. #define PACKETMAX2 PACKETMAX*24
  44.  
  45. GsOT      WorldOrderingTable[2];                 // ordering table handlers
  46. GsOT_TAG  OrderingTable[2][1<<OT_LENGTH];        // ordering tables
  47. PACKET    GPUOutputPacket[2][PACKETMAX2];         // GPU Packet work area
  48.  
  49. GsSPRITE bg[2];
  50. GsSPRITE tiles[32*32][2];
  51.  
  52. int map[32*32][2];
  53.  
  54. unsigned long __ramsize =   0x00200000;          // force 2 megabytes of RAM
  55. unsigned long __stacksize = 0x00004000;          // force 16 kilobytes of stack
  56.  
  57. extern unsigned char background[];
  58. extern unsigned char grassTileset[];
  59. extern unsigned char character[];
  60.  
  61. int SCREEN_WIDTH, SCREEN_HEIGHT;        
  62.  
  63. // --------
  64. // integers
  65. // --------
  66. int currentbuffer; // double buffer holder
  67. char version[5]="V0.1"; // version number
  68.  
  69. char i;
  70. int j;
  71.  
  72. int scrollX, scrollY;
  73.  
  74. typedef struct {
  75.     GsSPRITE sprite;
  76.     int x, y;
  77.     char onLadder;
  78. } Player;
  79.  
  80. Player player;
  81.  
  82. // ----------
  83. // prototypes
  84. // ----------
  85. void initGraphics(); // initialise the graphics
  86. void initFont();
  87. void initGraphics();
  88. void clearVRAM();
  89. void initImages();
  90. void display();
  91. void collide();
  92. int getTimWidth(unsigned char timData[]);
  93. int getTimHeight(unsigned char timData[]);
  94. void loadImageToFB(unsigned char timData[]);
  95. GsSPRITE initSprite(unsigned char timData[], int tX, int tY, int u, int v, int w, int h);
  96. void readPad();
  97. void saveTilemapPosition();
  98.  
  99. // ----------------
  100. // system debugging
  101. // ----------------
  102. const DEBUG = 1; // light/general debugging
  103.  
  104. /* file header structure */
  105. typedef struct {
  106.     char    Magic[2];
  107.     char    Type;
  108.     char    BlockEntry;
  109.     char    Title[64];
  110.     char    reserve[28];
  111.     char    Clut[32];
  112.     char    Icon[3][128];
  113. } _CARD;
  114.  
  115. static _CARD HEAD;
  116.  
  117. int main()
  118. {
  119.     player.x = 0;
  120.     player.y = 0;
  121.  
  122.     printf("\nPLATFORMER FOR PLAYSTATION [BEGIN]\n\n");
  123.  
  124.     ResetCallback(); // initialises all system callbacks
  125.    
  126.     PadInit(0);
  127.    
  128.     initGraphics(); // this will initialise the graphics
  129.     if (DEBUG) initFont(); // initialise the psy-q debugging font
  130.     initImages(); // initialise the TIM image
  131.    
  132.     for(j = 0; j < (32*32); j++){
  133.         map[j][0] = level1[j];
  134.         //map[j][1] = level2[j];
  135.     }
  136.    
  137.     while(1)
  138.     {
  139.         readPad();
  140.        
  141.         if(DEBUG){
  142.             FntPrint("CHARACTER X: %d\n", player.x);
  143.             FntPrint("CHARACTER Y: %d\n", player.y);
  144.             FntPrint("TILEMAP X: %d\n", scrollX);
  145.             FntPrint("TILEMAP Y: %d\n", scrollY);
  146.         }
  147.        
  148.         bg[1].x = 256;
  149.        
  150.         for(j = 0; j < (32*32); j++){
  151.             if(map[j] != 0){
  152.                 tiles[j][0].x = ((j % 32) * 16) + scrollX;
  153.                 tiles[j][0].y = ((j / 32) * 16) + scrollY;
  154.                 tiles[j][0].u = ((map[j][0] - 1) % 16) * 16;
  155.                 tiles[j][0].v = ((map[j][0] - 1) / 16) * 16;
  156.             }
  157.         }
  158.         /*
  159.         for(j = 0; j < 256; j++){
  160.             if(map[j] != 0){
  161.                 tiles[j + 256].x = ((j % 16) * 16) + scrollX / 2;
  162.                 tiles[j + 256].y = ((j / 16) * 16) + scrollY / 2;
  163.                 tiles[j + 256].u = ((map[j][1] - 1) % 16) * 16;
  164.                 tiles[j + 256].v = ((map[j][1] - 1) / 16) * 16;
  165.             }
  166.         }
  167.         */
  168.        
  169.         player.sprite.x = player.x;
  170.         player.sprite.y = player.y;
  171.        
  172.         player.y++;
  173.        
  174.         collide();
  175.        
  176.         display();
  177.     }
  178.  
  179.     ResetGraph(3); // set the video mode back
  180.     PadStopCom(); // stop controller communications
  181.    
  182.     if (DEBUG) printf("\nPLATFORMER FOR PLAYSTATION [COMPLETE]\n");
  183.     return 0;
  184. }
  185.  
  186. void initFont()
  187. {
  188.   FntLoad(960, 256);  // load basfrdic font pattern
  189.   if (DEBUG) printf("Font Loaded (Framebuffer x=960, y=256)\n");
  190.  
  191.   // screen X,Y | max text length X,Y | automatic background clear 0,1 | max characters (eg: 50).
  192.   SetDumpFnt(FntOpen(10, 10, 320, 240, 0, 512));
  193. }
  194.  
  195.  
  196. void initGraphics()
  197. {
  198.    // puts display mask into the status specified by the mask (0 not displayed, 1 displayed)
  199.    SetDispMask(1);
  200.    
  201.    // 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)
  202.    ResetGraph(0);
  203.  
  204.    // clear all VRAM contents
  205.    //clearVRAM();
  206.  
  207.    // automatic video mode control
  208.    if (*(char *)0xbfc7ff52=='E')  // SCEE string address
  209.    {
  210.      // PAL MODE
  211.      SCREEN_WIDTH = 320;
  212.      SCREEN_HEIGHT = 256;
  213.      if (DEBUG) printf("Setting the PlayStation Video Mode to (PAL %dx%d)\n",SCREEN_WIDTH,SCREEN_HEIGHT,")");
  214.      SetVideoMode(1);
  215.      if (DEBUG) printf("Video Mode is (%d)\n",GetVideoMode());
  216.    }
  217.    else
  218.    {
  219.      // NTSC MODE
  220.      SCREEN_WIDTH = 320;
  221.      SCREEN_HEIGHT = 240;
  222.      if (DEBUG) printf("Setting the PlayStation Video Mode to (NTSC %dx%d)\n",SCREEN_WIDTH,SCREEN_HEIGHT,")");
  223.      SetVideoMode(0);
  224.      if (DEBUG) printf("Video Mode is (%d)\n",GetVideoMode());
  225.    }
  226.  
  227.    // set the graphics mode resolutions
  228.    GsInitGraph(SCREEN_WIDTH, SCREEN_HEIGHT, GsNONINTER|GsOFSGPU, 1, 0);
  229.  
  230.    // set the top left coordinates of the two buffers in video memory
  231.    GsDefDispBuff(0, 0, 0, SCREEN_HEIGHT);
  232.  
  233.    // initialise the ordering tables
  234.    WorldOrderingTable[0].length = OT_LENGTH;
  235.    WorldOrderingTable[1].length = OT_LENGTH;
  236.    WorldOrderingTable[0].org = OrderingTable[0];
  237.    WorldOrderingTable[1].org = OrderingTable[1];
  238.    
  239.    GsClearOt(0,0,&WorldOrderingTable[0]);
  240.    GsClearOt(0,0,&WorldOrderingTable[1]);
  241.  
  242.    if (DEBUG) printf("Graphics Initilised!\n");
  243. }
  244.  
  245. void clearVRAM() {
  246.     RECT rectTL;
  247.    
  248.     setRECT(&rectTL, 0, 0, 1024, 512);
  249.     ClearImage2(&rectTL, 0, 0, 0);
  250.    
  251.     DrawSync(0); // ensure that the VRAM is clear before exiting
  252.    
  253.     if (DEBUG) printf("VRAM cleared!\n");
  254.     return;
  255. }
  256.  
  257.  
  258. // Initialize images
  259. void initImages() {
  260.     loadImageToFB(background);
  261.     loadImageToFB(grassTileset);
  262.     loadImageToFB(character);
  263.    
  264.     //Set both tX and tY to -1 for the tex page position to be based on the TIM's position in the framebuffer
  265.     bg[0] = initSprite(background, 320, 256, 0, 0, 256, 256);
  266.     bg[1] = initSprite(background, 576, 256, 256, 0, 64, 256);
  267.    
  268.     for(j = 0; j < (32*32); j++){
  269.         tiles[j][0] = initSprite(grassTileset, -1, -1, 0, 0, 16, 16);
  270.     }
  271.    
  272.     player.sprite = initSprite(character, -1, -1, 0, 0, 16, 32);
  273.  
  274.     // wait for all drawing to finish
  275.     DrawSync(0);
  276. }
  277.  
  278.  
  279. void display() {
  280.     // refresh the font
  281.     if (DEBUG) FntFlush(-1);
  282.    
  283.     // get the current buffer
  284.     currentbuffer=GsGetActiveBuff();
  285.    
  286.     // setup the packet workbase
  287.     GsSetWorkBase((PACKET*)GPUOutputPacket[currentbuffer]);
  288.    
  289.     // clear the ordering table
  290.     GsClearOt(0,0,&WorldOrderingTable[currentbuffer]);
  291.    
  292.     // insert sprites into the ordering table
  293.    
  294.     GsSortSprite(&bg[0], &WorldOrderingTable[currentbuffer], 2);
  295.     GsSortSprite(&bg[1], &WorldOrderingTable[currentbuffer], 2);
  296.    
  297.     if(DEBUG) printf("Render layer 1\n");
  298.     for(j = 0; j < (32*32); j++){
  299.         if(map[j][0] != 0){
  300.             if((tiles[j][0].x > (-16)) && (tiles[j][0].x < SCREEN_WIDTH)){
  301.                 if((tiles[j][0].y > (-16)) && (tiles[j][0].y < SCREEN_HEIGHT)){
  302.                     GsSortSprite(&tiles[j][0], &WorldOrderingTable[currentbuffer], 1);
  303.                 }
  304.             }
  305.         }
  306.     }
  307.     /*
  308.     if(DEBUG) printf("Render layer 2\n");
  309.     for(j = 0; j < 256; j++){
  310.         if(map[j][1] != 0){
  311.             if((tiles[j].x > (-16)) && (tiles[j + 256].x < SCREEN_WIDTH)){
  312.                 if((tiles[j].y > (-16)) && (tiles[j + 256].y < SCREEN_HEIGHT)){
  313.                     GsSortSprite(&tiles[j + 256], &WorldOrderingTable[currentbuffer], 0);
  314.                 }
  315.             }
  316.         }
  317.     }
  318.     */
  319.    
  320.     GsSortSprite(&player.sprite, &WorldOrderingTable[currentbuffer], 0);
  321.    
  322.     // wait for all drawing to finish
  323.     DrawSync(0);
  324.    
  325.     // wait for v_blank interrupt
  326.     VSync(0);
  327.    
  328.     // flip double buffers
  329.     GsSwapDispBuff();
  330.    
  331.     // clear the ordering table with a background color
  332.     GsSortClear(0,0,0,&WorldOrderingTable[currentbuffer]);
  333.    
  334.     // Draw the ordering table for the currentbuffer
  335.     GsDrawOt(&WorldOrderingTable[currentbuffer]);
  336. }
  337.  
  338. void collide(){
  339.     int tileX = ((player.x + 8) - scrollX) / 16;
  340.     int tileY = ((player.y + 31) - scrollY) / 16;
  341.     int collidedTile = map[(tileY * 32) + tileX][0];
  342.  
  343.     if(DEBUG){
  344.         FntPrint("COLLIDED TILE ID: %d\n", collidedTile);
  345.         FntPrint("TILE X: %d\n", tileX);
  346.         FntPrint("TILE Y: %d\n", tileY);
  347.     }
  348.    
  349.     if(collidedTile == 2) player.y--;
  350.     if(collidedTile == 23){
  351.         player.onLadder = true;
  352.     } else {
  353.         player.onLadder = false;
  354.     }
  355. }
  356.  
  357. int getTimWidth(unsigned char timData[]){
  358.     GsIMAGE         tempTimData;                                // holds tim graphic info
  359.    
  360.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  361.    
  362.     return tempTimData.pw;
  363. }
  364.  
  365.  
  366. int getTimHeight(unsigned char timData[]){
  367.     GsIMAGE tempTimData;                                // holds tim graphic info
  368.    
  369.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  370.    
  371.     return tempTimData.ph;
  372. }
  373.  
  374. void loadImageToFB(unsigned char timData[]){
  375.     RECT tempRect;                                    // RECT structure
  376.     GsIMAGE tempTimData;                                // holds tim graphic info
  377.    
  378.     // put data from tim file into rect        
  379.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  380.    
  381.     // load the image into the frame buffer
  382.     tempRect.x = tempTimData.px;                                    // tim start X coord
  383.     tempRect.y = tempTimData.py;                                    // tim start Y coord
  384.     tempRect.w = tempTimData.pw;                                    // data width
  385.     tempRect.h = tempTimData.ph;                                    // data height
  386.     // load the tim data into the frame buffer
  387.     LoadImage(&tempRect, tempTimData.pixel);      
  388.    
  389.     // load the CLUT into the frame buffer
  390.     tempRect.x = tempTimData.cx;                                    // x pos in frame buffer
  391.     tempRect.y = tempTimData.cy;                                    // y pos in frame buffer
  392.     tempRect.w = tempTimData.cw;                                    // width of CLUT
  393.     tempRect.h = tempTimData.ch;                                    // height of CLUT
  394.     // load data into frame buffer (DMA from DRAM to VRAM)
  395.     LoadImage(&tempRect, tempTimData.clut);                
  396. }
  397.  
  398. GsSPRITE initSprite(unsigned char timData[], int tX, int tY, int u, int v, int w, int h){
  399.     GsSPRITE tempSprite;
  400.     GsIMAGE tempTimData;
  401.    
  402.     // put data from tim file into rect        
  403.     GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
  404.  
  405.     // initialise sprite
  406.     tempSprite.attribute=0x2000000;                           // 16 bit CLUT, all options off (0x1 = 8-bit, 0x2 = 16-bit)
  407.     tempSprite.x = 0;                                         // draw at x coord 0
  408.     tempSprite.y = 0;                                         // draw at y coord 0
  409.     tempSprite.w = w;                                       // width of sprite
  410.     tempSprite.h = h;                               // height of sprite
  411.     // texture page | texture mode (0 4-bit, 1 8-bit, 2 16-bit), semi-transparency rate, texture x, texture y in the framebuffer
  412.     if((tX == -1) && (tY == -1)){
  413.         tempSprite.tpage=GetTPage(1, 2, tempTimData.px, tempTimData.py);
  414.     } else {
  415.         tempSprite.tpage=GetTPage(1, 2, tX, tY);
  416.     }
  417.     tempSprite.r = 128;                                       // RGB Data
  418.     tempSprite.g = 128;
  419.     tempSprite.b = 128;
  420.     tempSprite.u=u;                                           // position within timfile for sprite
  421.     tempSprite.v=v;                                          
  422.     tempSprite.cx = tempTimData.cx;                              // CLUT location x coord
  423.     tempSprite.cy = tempTimData.cy;                              // CLUT location y coord
  424.     tempSprite.mx = 0;                                        // rotation x coord
  425.     tempSprite.my = 0;                                        // rotation y coord
  426.     tempSprite.scalex = ONE;                                  // scale x coord (ONE = 100%)
  427.     tempSprite.scaley = ONE;                                  // scale y coord (ONE = 100%)
  428.     tempSprite.rotate = 0;                                    // degrees to rotate  
  429.    
  430.     return tempSprite;
  431. }
  432.  
  433. void readPad(){
  434.     u_long padd;
  435.     padd = PadRead(1);
  436.    
  437.     if(padd & PADLup){
  438.         if(player.onLadder) player.y -= 2;
  439.     }
  440.     if(padd & PADLleft) player.x--;
  441.     if(padd & PADLright) player.x++;
  442.     if(padd & PADRdown) scrollY++;
  443.     if(padd & PADRup) scrollY--;
  444.     if(padd & PADRright) scrollX++;
  445.     if(padd & PADRleft) scrollX--;
  446.     //if(padd & PADLstart) saveTilemapPosition();
  447. }
  448.  
  449. void saveTilemapPosition(){
  450.     //Save the position of the tilemap
  451.    
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement