Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ===========================================================
- Sony PlayStation 1 Source Code
- ===========================================================
- PLATFORMER
- -----------------------------------------------------------
- Developer / Programmer..............: atm959
- Software Ddevelopment Kit...........: PSY-Q
- First Release.......................: 02/AUGUST/2018
- Last Release........................: 02/AUGUST/2018
- Version.............................: 0.1
- 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>
- #include <stdio.h>
- #include <libmcrd.h>
- //Image files
- #include "images/background.h" //Background TIM
- #include "images/grassTileset.h" //Grass Tileset TIM
- #include "images/character.h" //Character TIM
- #include "levels/level1.h" //The map
- #include "levels/level2.h"
- #define false 0
- #define true 1
- #define OT_LENGTH 10 // ordering table length
- #define PACKETMAX 4096 // the max num of objects
- #define PACKETMAX2 PACKETMAX*24
- GsOT WorldOrderingTable[2]; // ordering table handlers
- GsOT_TAG OrderingTable[2][1<<OT_LENGTH]; // ordering tables
- PACKET GPUOutputPacket[2][PACKETMAX2]; // GPU Packet work area
- GsSPRITE bg[2];
- GsSPRITE tiles[32*32][2];
- int map[32*32][2];
- unsigned long __ramsize = 0x00200000; // force 2 megabytes of RAM
- unsigned long __stacksize = 0x00004000; // force 16 kilobytes of stack
- extern unsigned char background[];
- extern unsigned char grassTileset[];
- extern unsigned char character[];
- int SCREEN_WIDTH, SCREEN_HEIGHT;
- // --------
- // integers
- // --------
- int currentbuffer; // double buffer holder
- char version[5]="V0.1"; // version number
- char i;
- int j;
- int scrollX, scrollY;
- typedef struct {
- GsSPRITE sprite;
- int x, y;
- char onLadder;
- } Player;
- Player player;
- // ----------
- // prototypes
- // ----------
- void initGraphics(); // initialise the graphics
- void initFont();
- void initGraphics();
- void clearVRAM();
- void initImages();
- void display();
- void collide();
- 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();
- void saveTilemapPosition();
- // ----------------
- // system debugging
- // ----------------
- const DEBUG = 1; // light/general debugging
- /* file header structure */
- typedef struct {
- char Magic[2];
- char Type;
- char BlockEntry;
- char Title[64];
- char reserve[28];
- char Clut[32];
- char Icon[3][128];
- } _CARD;
- static _CARD HEAD;
- int main()
- {
- player.x = 0;
- player.y = 0;
- 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
- for(j = 0; j < (32*32); j++){
- map[j][0] = level1[j];
- //map[j][1] = level2[j];
- }
- while(1)
- {
- readPad();
- if(DEBUG){
- FntPrint("CHARACTER X: %d\n", player.x);
- FntPrint("CHARACTER Y: %d\n", player.y);
- FntPrint("TILEMAP X: %d\n", scrollX);
- FntPrint("TILEMAP Y: %d\n", scrollY);
- }
- bg[1].x = 256;
- for(j = 0; j < (32*32); j++){
- if(map[j] != 0){
- tiles[j][0].x = ((j % 32) * 16) + scrollX;
- tiles[j][0].y = ((j / 32) * 16) + scrollY;
- tiles[j][0].u = ((map[j][0] - 1) % 16) * 16;
- tiles[j][0].v = ((map[j][0] - 1) / 16) * 16;
- }
- }
- /*
- for(j = 0; j < 256; j++){
- if(map[j] != 0){
- tiles[j + 256].x = ((j % 16) * 16) + scrollX / 2;
- tiles[j + 256].y = ((j / 16) * 16) + scrollY / 2;
- tiles[j + 256].u = ((map[j][1] - 1) % 16) * 16;
- tiles[j + 256].v = ((map[j][1] - 1) / 16) * 16;
- }
- }
- */
- player.sprite.x = player.x;
- player.sprite.y = player.y;
- player.y++;
- collide();
- 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 basfrdic 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(background);
- loadImageToFB(grassTileset);
- loadImageToFB(character);
- //Set both tX and tY to -1 for the tex page position to be based on the TIM's position in the framebuffer
- bg[0] = initSprite(background, 320, 256, 0, 0, 256, 256);
- bg[1] = initSprite(background, 576, 256, 256, 0, 64, 256);
- for(j = 0; j < (32*32); j++){
- tiles[j][0] = initSprite(grassTileset, -1, -1, 0, 0, 16, 16);
- }
- player.sprite = initSprite(character, -1, -1, 0, 0, 16, 32);
- // 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
- GsSortSprite(&bg[0], &WorldOrderingTable[currentbuffer], 2);
- GsSortSprite(&bg[1], &WorldOrderingTable[currentbuffer], 2);
- if(DEBUG) printf("Render layer 1\n");
- for(j = 0; j < (32*32); j++){
- if(map[j][0] != 0){
- if((tiles[j][0].x > (-16)) && (tiles[j][0].x < SCREEN_WIDTH)){
- if((tiles[j][0].y > (-16)) && (tiles[j][0].y < SCREEN_HEIGHT)){
- GsSortSprite(&tiles[j][0], &WorldOrderingTable[currentbuffer], 1);
- }
- }
- }
- }
- /*
- if(DEBUG) printf("Render layer 2\n");
- for(j = 0; j < 256; j++){
- if(map[j][1] != 0){
- if((tiles[j].x > (-16)) && (tiles[j + 256].x < SCREEN_WIDTH)){
- if((tiles[j].y > (-16)) && (tiles[j + 256].y < SCREEN_HEIGHT)){
- GsSortSprite(&tiles[j + 256], &WorldOrderingTable[currentbuffer], 0);
- }
- }
- }
- }
- */
- GsSortSprite(&player.sprite, &WorldOrderingTable[currentbuffer], 0);
- // 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]);
- }
- void collide(){
- int tileX = ((player.x + 8) - scrollX) / 16;
- int tileY = ((player.y + 31) - scrollY) / 16;
- int collidedTile = map[(tileY * 32) + tileX][0];
- if(DEBUG){
- FntPrint("COLLIDED TILE ID: %d\n", collidedTile);
- FntPrint("TILE X: %d\n", tileX);
- FntPrint("TILE Y: %d\n", tileY);
- }
- if(collidedTile == 2) player.y--;
- if(collidedTile == 23){
- player.onLadder = true;
- } else {
- player.onLadder = false;
- }
- }
- 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);
- if(padd & PADLup){
- if(player.onLadder) player.y -= 2;
- }
- if(padd & PADLleft) player.x--;
- if(padd & PADLright) player.x++;
- if(padd & PADRdown) scrollY++;
- if(padd & PADRup) scrollY--;
- if(padd & PADRright) scrollX++;
- if(padd & PADRleft) scrollX--;
- //if(padd & PADLstart) saveTilemapPosition();
- }
- void saveTilemapPosition(){
- //Save the position of the tilemap
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement