Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ===========================================================
- Sony PlayStation 1 Source Code
- ===========================================================
- TIM EXAMPLE
- -----------------------------------------------------------
- Developer / Programmer..............: PSXDEV.net
- Software Ddevelopment Kit...........: PSY-Q
- First Release.......................: 26/JANUARY/2013
- Last Release........................: 03/JULY/2013
- Version.............................: 1.2
- Copyright (C) 1994,1995 by Sony Computer Entertainment Inc.
- All Rights Reserved.
- Sony Computer Entertainment Inc. Development Department
- http://psxdev.net/
- -----------------------------------------------------------*/
- #include <sys/types.h>
- #include <libgte.h>
- #include <libgpu.h>
- #include <libetc.h>
- #include <libgs.h>
- //Image files
- #include "images/grassTileset.h"
- #define OT_LENGTH 1 // ordering table length
- #define PACKETMAX (300) // the max num of objects
- GsOT WorldOrderingTable[2]; // ordering table handlers
- GsOT_TAG OrderingTable[2][1<<OT_LENGTH]; // ordering tables
- PACKET GPUOutputPacket[2][PACKETMAX]; // GPU Packet work area
- GsSPRITE tiles[64];
- unsigned long __ramsize = 0x00200000; // force 2 megabytes of RAM
- unsigned long __stacksize = 0x00004000; // force 16 kilobytes of stack
- extern unsigned char grassTileset[];
- int SCREEN_WIDTH, SCREEN_HEIGHT;
- // --------
- // integers
- // --------
- int currentbuffer; // double buffer holder
- char version[5]="V1.2"; // version number
- char i;
- int j;
- // ----------
- // prototypes
- // ----------
- void initGraphics(); // initialise the graphics
- void initFont();
- void initGraphics();
- void clearVRAM();
- void initImages();
- void display();
- int getTimWidth(unsigned char timData[]);
- int getTimHeight(unsigned char timData[]);
- void loadImageToFB(unsigned char timData[]);
- GsSPRITE initSprite(unsigned char timData[], int tX, int tY, int u, int v, int w, int h);
- void readPad();
- // ----------------
- // system debugging
- // ----------------
- const DEBUG = 1; // light/general debugging
- int main()
- {
- printf("\nPLATFORMER FOR PLAYSTATION [BEGIN]\n\n");
- ResetCallback(); // initialises all system callbacks
- PadInit(0);
- initGraphics(); // this will initialise the graphics
- if (DEBUG) initFont(); // initialise the psy-q debugging font
- initImages(); // initialise the TIM image
- while(1)
- {
- readPad();
- if(DEBUG){
- }
- for(j = 0; j < 64; j++){
- tiles[j].x = (j % 16) * 16;
- tiles[j].y = (j / 16) * 16;
- }
- display();
- }
- ResetGraph(3); // set the video mode back
- PadStopCom(); // stop controller communications
- if (DEBUG) printf("\nPLATFORMER FOR PLAYSTATION [COMPLETE]\n");
- return 0;
- }
- void initFont()
- {
- FntLoad(960, 256); // load basic font pattern
- if (DEBUG) printf("Font Loaded (Framebuffer x=960, y=256)\n");
- // screen X,Y | max text length X,Y | automatic background clear 0,1 | max characters (eg: 50).
- SetDumpFnt(FntOpen(10, 10, 320, 240, 0, 512));
- }
- void initGraphics()
- {
- // puts display mask into the status specified by the mask (0 not displayed, 1 displayed)
- SetDispMask(1);
- // 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)
- ResetGraph(0);
- // clear all VRAM contents
- //clearVRAM();
- // automatic video mode control
- if (*(char *)0xbfc7ff52=='E') // SCEE string address
- {
- // PAL MODE
- SCREEN_WIDTH = 320;
- SCREEN_HEIGHT = 256;
- if (DEBUG) printf("Setting the PlayStation Video Mode to (PAL %dx%d)\n",SCREEN_WIDTH,SCREEN_HEIGHT,")");
- SetVideoMode(1);
- if (DEBUG) printf("Video Mode is (%d)\n",GetVideoMode());
- }
- else
- {
- // NTSC MODE
- SCREEN_WIDTH = 320;
- SCREEN_HEIGHT = 240;
- if (DEBUG) printf("Setting the PlayStation Video Mode to (NTSC %dx%d)\n",SCREEN_WIDTH,SCREEN_HEIGHT,")");
- SetVideoMode(0);
- if (DEBUG) printf("Video Mode is (%d)\n",GetVideoMode());
- }
- // set the graphics mode resolutions
- GsInitGraph(SCREEN_WIDTH, SCREEN_HEIGHT, GsNONINTER|GsOFSGPU, 1, 0);
- // set the top left coordinates of the two buffers in video memory
- GsDefDispBuff(0, 0, 0, SCREEN_HEIGHT);
- // initialise the ordering tables
- WorldOrderingTable[0].length = OT_LENGTH;
- WorldOrderingTable[1].length = OT_LENGTH;
- WorldOrderingTable[0].org = OrderingTable[0];
- WorldOrderingTable[1].org = OrderingTable[1];
- GsClearOt(0,0,&WorldOrderingTable[0]);
- GsClearOt(0,0,&WorldOrderingTable[1]);
- if (DEBUG) printf("Graphics Initilised!\n");
- }
- void clearVRAM() {
- RECT rectTL;
- setRECT(&rectTL, 0, 0, 1024, 512);
- ClearImage2(&rectTL, 0, 0, 0);
- DrawSync(0); // ensure that the VRAM is clear before exiting
- if (DEBUG) printf("VRAM cleared!\n");
- return;
- }
- // Initialize images
- void initImages() {
- loadImageToFB(grassTileset);
- //Set both tX and tY to -1 for the tex page position to be based on the TIM's position in the framebuffer
- for(j = 0; j < 64; j++){
- tiles[j] = initSprite(grassTileset, -1, -1, 0, 0, 16, 16);
- }
- // wait for all drawing to finish
- DrawSync(0);
- }
- void display() {
- // refresh the font
- if (DEBUG) FntFlush(-1);
- // get the current buffer
- currentbuffer=GsGetActiveBuff();
- // setup the packet workbase
- GsSetWorkBase((PACKET*)GPUOutputPacket[currentbuffer]);
- // clear the ordering table
- GsClearOt(0,0,&WorldOrderingTable[currentbuffer]);
- // insert sprites into the ordering table
- for(j = 0; j < 64; j++){
- GsSortSprite(&tiles[i], &WorldOrderingTable[currentbuffer], j);
- }
- // wait for all drawing to finish
- DrawSync(0);
- // wait for v_blank interrupt
- VSync(0);
- // flip double buffers
- GsSwapDispBuff();
- // clear the ordering table with a background color
- GsSortClear(0,0,0,&WorldOrderingTable[currentbuffer]);
- // Draw the ordering table for the currentbuffer
- GsDrawOt(&WorldOrderingTable[currentbuffer]);
- }
- int getTimWidth(unsigned char timData[]){
- GsIMAGE tempTimData; // holds tim graphic info
- GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
- return tempTimData.pw;
- }
- int getTimHeight(unsigned char timData[]){
- GsIMAGE tempTimData; // holds tim graphic info
- GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
- return tempTimData.ph;
- }
- void loadImageToFB(unsigned char timData[]){
- RECT tempRect; // RECT structure
- GsIMAGE tempTimData; // holds tim graphic info
- // put data from tim file into rect
- GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
- // load the image into the frame buffer
- tempRect.x = tempTimData.px; // tim start X coord
- tempRect.y = tempTimData.py; // tim start Y coord
- tempRect.w = tempTimData.pw; // data width
- tempRect.h = tempTimData.ph; // data height
- // load the tim data into the frame buffer
- LoadImage(&tempRect, tempTimData.pixel);
- // load the CLUT into the frame buffer
- tempRect.x = tempTimData.cx; // x pos in frame buffer
- tempRect.y = tempTimData.cy; // y pos in frame buffer
- tempRect.w = tempTimData.cw; // width of CLUT
- tempRect.h = tempTimData.ch; // height of CLUT
- // load data into frame buffer (DMA from DRAM to VRAM)
- LoadImage(&tempRect, tempTimData.clut);
- }
- GsSPRITE initSprite(unsigned char timData[], int tX, int tY, int u, int v, int w, int h){
- GsSPRITE tempSprite;
- GsIMAGE tempTimData;
- // put data from tim file into rect
- GsGetTimInfo ((u_long *)(timData+4),&tempTimData);
- // initialise sprite
- tempSprite.attribute=0x2000000; // 16 bit CLUT, all options off (0x1 = 8-bit, 0x2 = 16-bit)
- tempSprite.x = 0; // draw at x coord 0
- tempSprite.y = 0; // draw at y coord 0
- tempSprite.w = w; // width of sprite
- tempSprite.h = h; // height of sprite
- // texture page | texture mode (0 4-bit, 1 8-bit, 2 16-bit), semi-transparency rate, texture x, texture y in the framebuffer
- if((tX == -1) && (tY == -1)){
- tempSprite.tpage=GetTPage(1, 2, tempTimData.px, tempTimData.py);
- } else {
- tempSprite.tpage=GetTPage(1, 2, tX, tY);
- }
- tempSprite.r = 128; // RGB Data
- tempSprite.g = 128;
- tempSprite.b = 128;
- tempSprite.u=u; // position within timfile for sprite
- tempSprite.v=v;
- tempSprite.cx = tempTimData.cx; // CLUT location x coord
- tempSprite.cy = tempTimData.cy; // CLUT location y coord
- tempSprite.mx = 0; // rotation x coord
- tempSprite.my = 0; // rotation y coord
- tempSprite.scalex = ONE; // scale x coord (ONE = 100%)
- tempSprite.scaley = ONE; // scale y coord (ONE = 100%)
- tempSprite.rotate = 0; // degrees to rotate
- return tempSprite;
- }
- void readPad(){
- u_long padd;
- padd = PadRead(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement