Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2. ===========================================================
  3.                 Sony PlayStation 1 Source Code
  4. ===========================================================
  5.                          FONT EXAMPLE
  6. Displays text on the screen using the built in GPU routines
  7. -----------------------------------------------------------
  8.  
  9.     Developer / Programmer..............: SCEI & PSXDEV.net
  10.     Software Ddevelopment Kit...........: PSY-Q
  11.     Last Updated........................: 04/MAY/2017
  12.  
  13.     Original code by SCEI | Edited by PSXDEV.net
  14.    
  15.     NOTE: This example uses double buffering.
  16.  
  17.   Copyright (C) 1994,1995 by Sony Computer Entertainment Inc.
  18.    Sony Computer Entertainment Inc. Development Department.
  19.                    All Rights Reserved.
  20.                    
  21.                     http://psxdev.net/
  22.  
  23. -----------------------------------------------------------*/
  24.  
  25. #include <stdlib.h>
  26. #include <libgte.h>
  27. #include <libgpu.h>
  28. #include <libgs.h>
  29.  
  30. #define OT_LENGTH 1 // the ordertable length
  31. #define PACKETMAX 50 // the maximum number of objects on the screen (please note that i added more than on the hello word example so it can display all the triangles correctly)
  32. #define SCREEN_WIDTH  320 // screen width-
  33. #define SCREEN_HEIGHT 240 // screen height (240 NTSC, 256 PAL)
  34.  
  35. GsOT myOT[2]; // ordering table header
  36. GsOT_TAG myOT_TAG[2][1<<OT_LENGTH]; // ordering table unit
  37. PACKET GPUPacketArea[2][PACKETMAX]; // GPU packet data
  38.  
  39. u_long _ramsize   = 0x00200000; // force 2 megabytes of RAM
  40. u_long _stacksize = 0x00004000; // force 16 kilobytes of stack
  41.  
  42. // --------
  43. // INTEGERS
  44. // --------
  45. short CurrentBuffer = 0; // holds the current buffer number
  46.  
  47. long Counter = 0;
  48. short RGBColorFlag[3] = {0, 0, 0};
  49. short RGBColor[3] = {0, 0, 0};
  50.  
  51. // ----------
  52. // PROTOTYPES
  53. // ----------
  54. void graphics(); // inits the GPU
  55. void display(); // updates the GPU (IE: VRAM/SGRAM/framebuffer)
  56.  
  57. const DEBUG = 1; // debugging (1=on, 0=off)
  58.  
  59. // ---------
  60. // FUNCTIONS
  61. // ---------
  62. POLY_F3 pf3;
  63.  
  64. void DrawSolidTri(short x1, short y1, short x2, short y2, short x3, short y3, int r, int g, int b, int ot)
  65. {
  66.     // draw a solid triangle and fill it with rgb colour
  67.     // EG: DrawSolidTri(200,0, 170,30, 230,30, 255,0,255, 0);
  68.     // check the Psy-Q documentation for the coordinates!
  69.  
  70.     SetPolyF3(&pf3);                            // initialise the primitive
  71.     setRGB0(&pf3, r, g, b);                     // set the fill colour (RGB)
  72.     setXY3(&pf3, x1,y1, x2,y2, x3,y3);          // draw 3 lines (3 sets of x/y co-ords)
  73.     DrawPrim(&pf3);                             // copy shape to frame buffer
  74.     GsSortPoly(&pf3, &myOT[CurrentBuffer], ot); // put the polygon into the order table
  75. }
  76.  
  77.  
  78. void DrawTransTri(short x1, short y1, short x2, short y2, short x3, short y3, int r, int g, int b, int ot)
  79. {
  80.     // draw a transparent triangle
  81.     // the transparency 'colour' is calculated from the fgnd & bgns pixels.
  82.     // EG: DrawTransTri(50,0, 70,60, 130,60, 255,0,255, 0);
  83.  
  84.     SetPolyF3(&pf3);                            // initialize primitive
  85.     setRGB0(&pf3, r, g, b);                     // set the foreground fill colour
  86.     setSemiTrans(&pf3, 1);                      // semi-transparency; 0=off; 1=on
  87.     setXY3(&pf3, x1,y1, x2,y2, x3,y3);          // draw 4 lines (4 sets of x/y co-ords)
  88.     DrawPrim(&pf3);                         // copy shape to frame buffer
  89.     GsSortPoly(&pf3, &myOT[CurrentBuffer], ot); // put the polygon into the order table
  90. }
  91.  
  92. // ---------
  93. // FUNCYIONS
  94. // ---------
  95. int ColorAnimation(){
  96.     if (RGBColorFlag[0] == 0){
  97.         RGBColor[0] += 2;
  98.         if (RGBColor[0] >= 252){ // When the Red color reaches high enough, this changes to 1, and the color goes backwards
  99.             RGBColorFlag[0] = 1;
  100.             }
  101.         }
  102.     else {  RGBColor[0] -= 2;
  103.             if (RGBColor[0] <= 3){
  104.             RGBColorFlag[0] = 0;
  105.             }
  106.         }
  107.     if (RGBColorFlag[1] == 0){
  108.         RGBColor[1] += 1;
  109.         if (RGBColor[1] >= 252){ // When the Green color reaches high enough, this changes to 1, and the color goes backwards
  110.             RGBColorFlag[1] = 1;
  111.             }
  112.         }
  113.     else {  RGBColor[1] -= 1;
  114.             if (RGBColor[1] <= 3){
  115.             RGBColorFlag[1] = 0;
  116.             }
  117.         }
  118.     if (RGBColorFlag[2] == 0){
  119.         RGBColor[2] += 3;
  120.         if (RGBColor[2] >= 252){ // When the Blue color reaches high enough, this changes to 1, and the color goes backwards
  121.             RGBColorFlag[2] = 1;
  122.             }
  123.         }
  124.     else {  RGBColor[2] -= 3;
  125.             if (RGBColor[2] <= 3){
  126.             RGBColorFlag[2] = 0;
  127.             }
  128.         }
  129. }
  130.  
  131. // ----
  132. // MAIN
  133. // ----
  134. int main()
  135. {
  136.     graphics(); // setup the graphics (seen below)
  137.     FntLoad(960, 256); // load the font from the BIOS into the framebuffer
  138.     SetDumpFnt(FntOpen(5, 20, 320, 240, 0, 512)); // screen X,Y | max text length X,Y | autmatic background clear 0,1 | max characters
  139.    
  140.     if (DEBUG) // should debug be true (equal 1)...
  141.     {
  142.         // print to the TTY stream (only visible if you're using one)
  143.         printf("\n\nHello World\n");
  144.         printf("\nhttp://psxdev.net/");
  145.     }
  146.  
  147.     while (1) // draw and display forever
  148.     {
  149.         DrawSolidTri(40, 60, 160, 180, 40, 180, 0, RGBColor[1], 0, 0); //Green Solid Triangle at the lower left
  150.         DrawSolidTri(40, 60, 160, 60, 160, 180, RGBColor[0], RGBColor[1], RGBColor[2], 1); //Multi Colored Solid Triangle at the upper left
  151.         DrawTransTri(160, 60, 280, 180, 160, 180, 0, RGBColor[1], 0, 2); //Green Transparent Triangle at the lower right
  152.         DrawTransTri(160, 60, 280, 60, 280, 180, RGBColor[0], RGBColor[1], RGBColor[2], 3); //Multi Colores Transparent Triangle at the upper right
  153.         ColorAnimation(); // Actively changes the RGBColor array values
  154.         FntPrint("             HELLO WORLD\n\n          HTTP://PSXDEV.NET/");
  155.         FntPrint("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // Making spaces so put the next texts in place
  156.         FntPrint("\n Counter is = %d\r", Counter);
  157.         FntPrint("\n RGBColorFlag[0] is = %d\r", RGBColorFlag[0]); // Displays the value of the Red flag
  158.         FntPrint("\n RGBColorFlag[1] is = %d\r", RGBColorFlag[1]); // Displays the value of the Green flag
  159.         FntPrint("\n RGBColorFlag[2] is = %d\r", RGBColorFlag[2]); // Displays the value of the Blue flag
  160.         FntPrint("\n RGBColor[0] is = %d\r", RGBColor[0]); // This display the current Red color value
  161.         FntPrint("\n RGBColor[1] is = %d\r", RGBColor[1]); // This display the current Green color value
  162.         FntPrint("\n RGBColor[2] is = %d\r", RGBColor[2]); // This display the current Blue color value
  163.         display();
  164.         Counter++;
  165.     }
  166.  
  167.     return 0; // this will never be reached because we're in a while loop above
  168. }
  169.  
  170. void graphics()
  171. {
  172.     SetVideoMode(1); // PAL mode
  173.     //SetVideoMode(0); // NTSC mode
  174.    
  175.     GsInitGraph(SCREEN_WIDTH, SCREEN_HEIGHT, GsINTER|GsOFSGPU, 1, 0); // set the graphics mode resolutions (GsNONINTER for NTSC, and GsINTER for PAL)
  176.     GsDefDispBuff(0, 0, 0, SCREEN_HEIGHT); // tell the GPU to draw from the top left coordinates of the framebuffer
  177.    
  178.     // init the ordertables
  179.     myOT[0].length = OT_LENGTH;
  180.     myOT[1].length = OT_LENGTH;
  181.     myOT[0].org = myOT_TAG[0];
  182.     myOT[1].org = myOT_TAG[1];
  183.    
  184.     // clear the ordertables
  185.     GsClearOt(0,0,&myOT[0]);
  186.     GsClearOt(0,0,&myOT[1]);
  187. }
  188.  
  189.  
  190. void display()
  191. {
  192.     // refresh the font
  193.     FntFlush(-1);
  194.    
  195.     // get the current buffer
  196.     CurrentBuffer = GsGetActiveBuff();
  197.    
  198.     // setup the packet workbase
  199.     GsSetWorkBase((PACKET*)GPUPacketArea[CurrentBuffer]);
  200.    
  201.     // clear the ordering table
  202.     GsClearOt(0,0, &myOT[CurrentBuffer]);
  203.    
  204.     // wait for all drawing to finish
  205.     DrawSync(0);
  206.    
  207.     // wait for v_blank interrupt
  208.     VSync(0);
  209.    
  210.     // flip the double buffers
  211.     GsSwapDispBuff();
  212.    
  213.     // clear the ordering table with a background color (R,G,B)
  214.     GsSortClear(50,50,50, &myOT[CurrentBuffer]);
  215.    
  216.     // draw the ordering table
  217.     GsDrawOt(&myOT[CurrentBuffer]);
  218. }