aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJohn Wilbert M. Villamor <lameguy64@gmail.com>2020-11-29 20:18:38 +0800
committerJohn Wilbert M. Villamor <lameguy64@gmail.com>2020-11-29 20:18:38 +0800
commit3d38a12ca171490e22c7b56cd19c008f2dadfb83 (patch)
tree543653494423391c02fa21f5688baa90c67fdabe /examples
parent60deeff6e078271a17cf77c6204edbbde846f0bd (diff)
downloadpsn00bsdk-3d38a12ca171490e22c7b56cd19c008f2dadfb83.tar.gz
Defined MDEC registers, corrected SquareRoot12() lookup table typo, updated memmove(), removed redundant toolchain definitions, added HDTV example
Diffstat (limited to 'examples')
-rw-r--r--examples/beginner/hello/makefile6
-rw-r--r--examples/cdrom/cdbrowse/makefile5
-rw-r--r--examples/cdrom/cdxa/makefile5
-rw-r--r--examples/demos/n00bdemo/makefile5
-rw-r--r--examples/examples-setup.mk1
-rw-r--r--examples/graphics/balls/makefile6
-rw-r--r--examples/graphics/billboard/makefile5
-rw-r--r--examples/graphics/fpscam/makefile5
-rw-r--r--examples/graphics/gte/makefile5
-rw-r--r--examples/graphics/hdtv/clip.c108
-rw-r--r--examples/graphics/hdtv/clip.h27
-rw-r--r--examples/graphics/hdtv/lookat.c40
-rw-r--r--examples/graphics/hdtv/lookat.h19
-rw-r--r--examples/graphics/hdtv/main.c730
-rw-r--r--examples/graphics/hdtv/makefile35
-rw-r--r--examples/graphics/render2tex/makefile5
-rw-r--r--examples/graphics/rgb24/makefile5
-rw-r--r--examples/system/childexec/makefile3
-rw-r--r--examples/system/console/makefile6
-rw-r--r--examples/system/timer/makefile5
-rw-r--r--examples/system/tty/makefile6
21 files changed, 960 insertions, 72 deletions
diff --git a/examples/beginner/hello/makefile b/examples/beginner/hello/makefile
index 27ca670..f80d2aa 100644
--- a/examples/beginner/hello/makefile
+++ b/examples/beginner/hello/makefile
@@ -34,12 +34,6 @@ AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections \
-T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-# Toolchain programs
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/cdrom/cdbrowse/makefile b/examples/cdrom/cdbrowse/makefile
index adbc9cb..61e54ab 100644
--- a/examples/cdrom/cdbrowse/makefile
+++ b/examples/cdrom/cdbrowse/makefile
@@ -24,11 +24,6 @@ AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections \
-T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/cdrom/cdxa/makefile b/examples/cdrom/cdxa/makefile
index 3e46301..8d6d215 100644
--- a/examples/cdrom/cdxa/makefile
+++ b/examples/cdrom/cdxa/makefile
@@ -24,11 +24,6 @@ AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections \
-T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/demos/n00bdemo/makefile b/examples/demos/n00bdemo/makefile
index f90fb11..27d0f1e 100644
--- a/examples/demos/n00bdemo/makefile
+++ b/examples/demos/n00bdemo/makefile
@@ -16,11 +16,6 @@ CPPFLAGS = $(CFLAGS) -fno-exceptions
AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: resources $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/examples-setup.mk b/examples/examples-setup.mk
index a5cfc20..0692d89 100644
--- a/examples/examples-setup.mk
+++ b/examples/examples-setup.mk
@@ -60,4 +60,5 @@ CC = $(GCC_BIN)$(PREFIX)gcc
CXX = $(GCC_BIN)$(PREFIX)g++
AS = $(GCC_BIN)$(PREFIX)as
AR = $(GCC_BIN)$(PREFIX)ar
+LD = $(GCC_BIN)$(PREFIX)ld
RANLIB = $(GCC_BIN)$(PREFIX)ranlib \ No newline at end of file
diff --git a/examples/graphics/balls/makefile b/examples/graphics/balls/makefile
index c5ae67b..f697fab 100644
--- a/examples/graphics/balls/makefile
+++ b/examples/graphics/balls/makefile
@@ -34,12 +34,6 @@ AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections \
-T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-# Toolchain programs
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/graphics/billboard/makefile b/examples/graphics/billboard/makefile
index 6537d49..5131d88 100644
--- a/examples/graphics/billboard/makefile
+++ b/examples/graphics/billboard/makefile
@@ -18,11 +18,6 @@ CPPFLAGS = $(CFLAGS) -fno-exceptions
AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/graphics/fpscam/makefile b/examples/graphics/fpscam/makefile
index fe62ea8..dd2b13e 100644
--- a/examples/graphics/fpscam/makefile
+++ b/examples/graphics/fpscam/makefile
@@ -18,11 +18,6 @@ CPPFLAGS = $(CFLAGS) -fno-exceptions
AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/graphics/gte/makefile b/examples/graphics/gte/makefile
index 2cd5ed5..bfcaff1 100644
--- a/examples/graphics/gte/makefile
+++ b/examples/graphics/gte/makefile
@@ -18,11 +18,6 @@ CPPFLAGS = $(CFLAGS) -fno-exceptions
AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/graphics/hdtv/clip.c b/examples/graphics/hdtv/clip.c
new file mode 100644
index 0000000..7f2b780
--- /dev/null
+++ b/examples/graphics/hdtv/clip.c
@@ -0,0 +1,108 @@
+/* Polygon clip detection code
+ *
+ * The polygon clipping logic is based on the Cohen-Sutherland algorithm, but
+ * only the off-screen detection logic is used to determine which polygon edges
+ * are off-screen.
+ *
+ * In tri_clip, the following edges are checked as follows:
+ *
+ * |\
+ * | \
+ * | \
+ * | \
+ * |--------
+ *
+ * In quad_clip, the following edges are checked as follows:
+ *
+ * |---------|
+ * | \ / |
+ * | \ / |
+ * | / \ |
+ * | / \ |
+ * |---------|
+ *
+ * The inner portion of the quad is checked, otherwise the quad will be
+ * culled out if the camera faces right into it, where all four edges
+ * are off-screen at once.
+ *
+ */
+
+#include "clip.h"
+
+#define CLIP_LEFT 1
+#define CLIP_RIGHT 2
+#define CLIP_TOP 4
+#define CLIP_BOTTOM 8
+
+int test_clip(RECT *clip, short x, short y) {
+
+ // Tests which corners of the screen a point lies outside of
+
+ int result = 0;
+
+ if ( x < clip->x ) {
+ result |= CLIP_LEFT;
+ }
+
+ if ( x >= (clip->x+(clip->w-1)) ) {
+ result |= CLIP_RIGHT;
+ }
+
+ if ( y < clip->y ) {
+ result |= CLIP_TOP;
+ }
+
+ if ( y >= (clip->y+(clip->h-1)) ) {
+ result |= CLIP_BOTTOM;
+ }
+
+ return result;
+
+}
+
+int tri_clip(RECT *clip, DVECTOR *v0, DVECTOR *v1, DVECTOR *v2) {
+
+ // Returns non-zero if a triangle is outside the screen boundaries
+
+ short c[3];
+
+ c[0] = test_clip(clip, v0->vx, v0->vy);
+ c[1] = test_clip(clip, v1->vx, v1->vy);
+ c[2] = test_clip(clip, v2->vx, v2->vy);
+
+ if ( ( c[0] & c[1] ) == 0 )
+ return 0;
+ if ( ( c[1] & c[2] ) == 0 )
+ return 0;
+ if ( ( c[2] & c[0] ) == 0 )
+ return 0;
+
+ return 1;
+}
+
+int quad_clip(RECT *clip, DVECTOR *v0, DVECTOR *v1, DVECTOR *v2, DVECTOR *v3) {
+
+ // Returns non-zero if a quad is outside the screen boundaries
+
+ short c[4];
+
+ c[0] = test_clip(clip, v0->vx, v0->vy);
+ c[1] = test_clip(clip, v1->vx, v1->vy);
+ c[2] = test_clip(clip, v2->vx, v2->vy);
+ c[3] = test_clip(clip, v3->vx, v3->vy);
+
+ if ( ( c[0] & c[1] ) == 0 )
+ return 0;
+ if ( ( c[1] & c[2] ) == 0 )
+ return 0;
+ if ( ( c[2] & c[3] ) == 0 )
+ return 0;
+ if ( ( c[3] & c[0] ) == 0 )
+ return 0;
+ if ( ( c[0] & c[2] ) == 0 )
+ return 0;
+ if ( ( c[1] & c[3] ) == 0 )
+ return 0;
+
+ return 1;
+} \ No newline at end of file
diff --git a/examples/graphics/hdtv/clip.h b/examples/graphics/hdtv/clip.h
new file mode 100644
index 0000000..3b428bb
--- /dev/null
+++ b/examples/graphics/hdtv/clip.h
@@ -0,0 +1,27 @@
+#ifndef _CLIP_H
+#define _CLIP_H
+
+#include <psxgte.h>
+#include <psxgpu.h>
+
+/* tri_clip
+ *
+ * Returns non-zero if a triangle (v0, v1, v2) is outside 'clip'.
+ *
+ * clip - Clipping area
+ * v0,v1,v2 - Triangle coordinates
+ *
+ */
+int tri_clip(RECT *clip, DVECTOR *v0, DVECTOR *v1, DVECTOR *v2);
+
+/* quad_clip
+ *
+ * Returns non-zero if a quad (v0, v1, v2, v3) is outside 'clip'.
+ *
+ * clip - Clipping area
+ * v0,v1,v2,v3 - Quad coordinates
+ *
+ */
+int quad_clip(RECT *clip, DVECTOR *v0, DVECTOR *v1, DVECTOR *v2, DVECTOR *v3);
+
+#endif // _CLIP_H \ No newline at end of file
diff --git a/examples/graphics/hdtv/lookat.c b/examples/graphics/hdtv/lookat.c
new file mode 100644
index 0000000..d7c9ce4
--- /dev/null
+++ b/examples/graphics/hdtv/lookat.c
@@ -0,0 +1,40 @@
+// LookAt matrix code (may be implemented into libpsxgte soon)
+
+#include "lookat.h"
+
+void crossProduct(SVECTOR *v0, SVECTOR *v1, VECTOR *out) {
+
+ out->vx = ((v0->vy*v1->vz)-(v0->vz*v1->vy))>>12;
+ out->vy = ((v0->vz*v1->vx)-(v0->vx*v1->vz))>>12;
+ out->vz = ((v0->vx*v1->vy)-(v0->vy*v1->vx))>>12;
+
+}
+
+void LookAt(VECTOR *eye, VECTOR *at, SVECTOR *up, MATRIX *mtx) {
+
+ VECTOR taxis;
+ SVECTOR zaxis;
+ SVECTOR xaxis;
+ SVECTOR yaxis;
+ VECTOR pos;
+ VECTOR vec;
+
+ setVector(&taxis, at->vx-eye->vx, at->vy-eye->vy, at->vz-eye->vz);
+ VectorNormalS(&taxis, &zaxis);
+ crossProduct(&zaxis, up, &taxis);
+ VectorNormalS(&taxis, &xaxis);
+ crossProduct(&zaxis, &xaxis, &taxis);
+ VectorNormalS(&taxis, &yaxis);
+
+ mtx->m[0][0] = xaxis.vx; mtx->m[1][0] = yaxis.vx; mtx->m[2][0] = zaxis.vx;
+ mtx->m[0][1] = xaxis.vy; mtx->m[1][1] = yaxis.vy; mtx->m[2][1] = zaxis.vy;
+ mtx->m[0][2] = xaxis.vz; mtx->m[1][2] = yaxis.vz; mtx->m[2][2] = zaxis.vz;
+
+ pos.vx = -eye->vx;;
+ pos.vy = -eye->vy;;
+ pos.vz = -eye->vz;;
+
+ ApplyMatrixLV(mtx, &pos, &vec);
+ TransMatrix(mtx, &vec);
+
+} \ No newline at end of file
diff --git a/examples/graphics/hdtv/lookat.h b/examples/graphics/hdtv/lookat.h
new file mode 100644
index 0000000..c57e50a
--- /dev/null
+++ b/examples/graphics/hdtv/lookat.h
@@ -0,0 +1,19 @@
+#ifndef _LOOKAT_H
+#define _LOOKAT_H
+
+#include <psxgte.h>
+#include <psxgpu.h>
+
+/* LookAt
+ *
+ * Generates a matrix that looks from 'eye' to 'at'.
+ *
+ * eye - Position of viewpoint
+ * at - Position to 'look at' from viewpoint
+ * up - Vector that defines the 'up' direction
+ * mtx - Matrix output
+ *
+ */
+void LookAt(VECTOR *eye, VECTOR *at, SVECTOR *up, MATRIX *mtx);
+
+#endif // _LOOKAT_H \ No newline at end of file
diff --git a/examples/graphics/hdtv/main.c b/examples/graphics/hdtv/main.c
new file mode 100644
index 0000000..82911d5
--- /dev/null
+++ b/examples/graphics/hdtv/main.c
@@ -0,0 +1,730 @@
+/*
+ * LibPSn00b Example Programs
+ *
+ * Full-resolution, Anamorphic Widescreen 3D Example
+ * 2020 Meido-Tek Productions / PSn00bSDK Project
+ *
+ * This example is a modification of the fpscam example demonstrating
+ * a method for taking advantage of widescreen HDTVs by means of a
+ * custom 704x480 video mode (as opposed to 640x480) and applying
+ * anamorphic widescreen aspect correction to a 3D perspective.
+ *
+ * The extended horizontal resolution guarantees that the picture will
+ * fill the entire screen of a widescreen television as the more
+ * conventional 640x480 mode would often show a black border on the sides
+ * of the picture. However, this extended video mode may cause odd effects
+ * on some analog television displays, so its recommended to implement this
+ * extended video mode as an option in your software title. Pixels are still
+ * not aspect correct however, so widescreen has to be achieved through
+ * anamorphic means, and is not exactly practical for most 2D graphics.
+ *
+ * If you wish to use a lower resolution 240/256 line mode but want to make
+ * it presentable on a widescreen television, use 384x240 or 384x256 as
+ * such a resolution would not only fill the entire screen of a widescreen
+ * but it also has close to aspect correct pixels, which is well suited for
+ * 2D games. The anamorphic aspect correction demonstrated in this example
+ * should also work for 320x240/256 modes for 3D games, but prefer to use
+ * 320x240 resolution.
+ *
+ * When using 240/256 line modes, you may want to include an option in your
+ * software title to enable interlace even though your software title does not
+ * run in high resolution mode. This helps with compatibility on HDTVs
+ * immensely and in some cases, improves the image quality on such
+ * televisions.
+ *
+ * Controls:
+ * Up - Look up
+ * Down - Look down
+ * Left - Look left
+ * Right - Look right
+ * Triangle - Move forward
+ * Cross - Move backward
+ * Square - Strafe left
+ * Circle - Strafe right
+ * R1 - Slide up
+ * R2 - Slide down
+ * L1 - Look at cube
+ * Select - Exit program (only works with CD loaders)
+ *
+ *
+ * Example by Lameguy64
+ *
+ * Changelog:
+ *
+ * November 27, 2020 - Initial version.
+ *
+ */
+
+#include <stdio.h>
+#include <psxgpu.h>
+#include <psxgte.h>
+#include <psxpad.h>
+#include <psxapi.h>
+#include <psxetc.h>
+#include <inline_c.h>
+
+#include "clip.h"
+#include "lookat.h"
+
+// OT and Packet Buffer sizes
+#define OT_LEN 1024
+#define PACKET_LEN 8192
+
+// Screen resolution
+#define SCREEN_XRES 704
+#define SCREEN_YRES 480
+
+// Screen center position
+#define CENTERX SCREEN_XRES>>1
+#define CENTERY SCREEN_YRES>>1
+
+
+// Double buffer structure
+typedef struct {
+ DISPENV disp; // Display environment
+ DRAWENV draw; // Drawing environment
+ int ot[OT_LEN]; // Ordering table
+ char p[PACKET_LEN]; // Packet buffer
+} DB;
+
+// Double buffer variables
+DB db[2];
+int db_active = 0;
+char *db_nextpri;
+RECT screen_clip;
+
+// Pad data buffer
+char pad_buff[2][34];
+
+
+// For easier handling of vertex indexes
+typedef struct {
+ short v0,v1,v2,v3;
+} INDEX;
+
+// Cube vertices
+SVECTOR cube_verts[] = {
+ { -100, -100, -100, 0 },
+ { 100, -100, -100, 0 },
+ { -100, 100, -100, 0 },
+ { 100, 100, -100, 0 },
+ { 100, -100, 100, 0 },
+ { -100, -100, 100, 0 },
+ { 100, 100, 100, 0 },
+ { -100, 100, 100, 0 }
+};
+
+// Cube face normals
+SVECTOR cube_norms[] = {
+ { 0, 0, -ONE, 0 },
+ { 0, 0, ONE, 0 },
+ { 0, -ONE, 0, 0 },
+ { 0, ONE, 0, 0 },
+ { -ONE, 0, 0, 0 },
+ { ONE, 0, 0, 0 }
+};
+
+// Cube vertex indices
+INDEX cube_indices[] = {
+ { 0, 1, 2, 3 },
+ { 4, 5, 6, 7 },
+ { 5, 4, 0, 1 },
+ { 6, 7, 3, 2 },
+ { 0, 2, 5, 7 },
+ { 3, 1, 6, 4 }
+};
+
+// Number of faces of cube
+#define CUBE_FACES 6
+
+
+// Light color matrix
+// Each column represents the color matrix of each light source and is
+// used as material color when using gte_ncs() or multiplied by a
+// source color when using gte_nccs(). 4096 is 1.0 in this matrix
+// A column of zeroes effectively disables the light source.
+MATRIX color_mtx = {
+ ONE, 0, 0, // Red
+ 0, 0, 0, // Green
+ ONE, 0, 0 // Blue
+};
+
+// Light matrix
+// Each row represents a vector direction of each light source.
+// An entire row of zeroes effectively disables the light source.
+MATRIX light_mtx = {
+ /* X, Y, Z */
+ -2048 , -2048 , -2048,
+ 0 , 0 , 0,
+ 0 , 0 , 0
+};
+
+
+// Function declarations
+void init();
+void display();
+void sort_cube(MATRIX *mtx, VECTOR *pos, SVECTOR *rot);
+
+
+// Main function
+int main() {
+
+ int i,p,xy_temp;
+ int px,py;
+
+ SVECTOR rot; // Rotation vector for cube
+ VECTOR pos; // Position vector for cube
+
+ SVECTOR verts[17][17]; // Vertex array for floor
+
+ VECTOR cam_pos; // Camera position (in fixed point integers)
+ VECTOR cam_rot; // Camera view angle (in fixed point integers)
+ int cam_mode; // Camera mode (between first-person and look-at)
+
+ VECTOR cam_scale;
+
+ VECTOR tpos; // Translation value for matrix calculations
+ SVECTOR trot; // Rotation value for matrix calculations
+ MATRIX mtx,lmtx; // Rotation matrices for geometry and lighting
+
+ PADTYPE *pad; // Pad structure pointer for parsing controller
+
+ POLY_F4 *pol4; // Flat shaded quad primitive pointer
+
+
+ // Init graphics and GTE
+ init();
+
+
+ // Set coordinates to the vertex array for the floor
+ for( py=0; py<17; py++ ) {
+ for( px=0; px<17; px++ ) {
+
+ setVector( &verts[py][px],
+ (100*(px-8))-50,
+ 0,
+ (100*(py-8))-50 );
+
+ }
+ }
+
+
+ // Camera default coordinates
+ setVector( &cam_pos, 0, ONE*-200, 0 );
+ setVector( &cam_rot, 0, 0, 0 );
+
+ setVector( &cam_scale, 3008, ONE, ONE );
+
+ // Main loop
+ while( 1 ) {
+
+ // Set pad buffer data to pad pointer
+ pad = (PADTYPE*)&pad_buff[0][0];
+
+ // Parse controller input
+ cam_mode = 0;
+
+ // Divide out fractions of camera rotation
+ trot.vx = cam_rot.vx >> 12;
+ trot.vy = cam_rot.vy >> 12;
+ trot.vz = cam_rot.vz >> 12;
+
+ if( pad->stat == 0 ) {
+
+ // For digital pad, dual-analog and dual-shock
+ if( ( pad->type == 0x4 ) || ( pad->type == 0x5 ) || ( pad->type == 0x7 ) ) {
+
+ // The button status bits are inverted,
+ // so 0 means pressed in this case
+
+ // Look controls
+ if( !(pad->btn&PAD_UP) ) {
+
+ // Look up
+ cam_rot.vx -= ONE*8;
+
+ } else if( !(pad->btn&PAD_DOWN) ) {
+
+ // Look down
+ cam_rot.vx += ONE*8;
+
+ }
+
+ if( !(pad->btn&PAD_LEFT) ) {
+
+ // Look left
+ cam_rot.vy += ONE*8;
+
+ } else if( !(pad->btn&PAD_RIGHT) ) {
+
+ // Look right
+ cam_rot.vy -= ONE*8;
+
+ }
+
+ // Movement controls
+ if( !(pad->btn&PAD_TRIANGLE) ) {
+
+ // Move forward
+ cam_pos.vx -= (( isin( trot.vy )*icos( trot.vx ) )>>12)<<2;
+ cam_pos.vy += isin( trot.vx )<<2;
+ cam_pos.vz += (( icos( trot.vy )*icos( trot.vx ) )>>12)<<2;
+
+ } else if( !(pad->btn&PAD_CROSS) ) {
+
+ // Move backward
+ cam_pos.vx += (( isin( trot.vy )*icos( trot.vx ) )>>12)<<2;
+ cam_pos.vy -= isin( trot.vx )<<2;
+ cam_pos.vz -= (( icos( trot.vy )*icos( trot.vx ) )>>12)<<2;
+
+ }
+
+ if( !(pad->btn&PAD_SQUARE ) ) {
+
+ // Slide left
+ cam_pos.vx -= icos( trot.vy )<<2;
+ cam_pos.vz -= isin( trot.vy )<<2;
+
+ } else if( !(pad->btn&PAD_CIRCLE ) ) {
+
+ // Slide right
+ cam_pos.vx += icos( trot.vy )<<2;
+ cam_pos.vz += isin( trot.vy )<<2;
+
+ }
+
+ if( !(pad->btn&PAD_R1) ) {
+
+ // Slide up
+ cam_pos.vx -= (( isin( trot.vy )*isin( trot.vx ) )>>12)<<2;
+ cam_pos.vy -= icos( trot.vx )<<2;
+ cam_pos.vz += (( icos( trot.vy )*isin( trot.vx ) )>>12)<<2;
+
+ }
+
+ if( !(pad->btn&PAD_R2) ) {
+
+ // Slide down
+ cam_pos.vx += (( isin( trot.vy )*isin( trot.vx ) )>>12)<<2;
+ cam_pos.vy += icos( trot.vx )<<2;
+ cam_pos.vz -= (( icos( trot.vy )*isin( trot.vx ) )>>12)<<2;
+
+ }
+
+ // Look at cube
+ if( !(pad->btn&PAD_L1) ) {
+
+ cam_mode = 1;
+
+ }
+
+ if( !(pad->btn&PAD_SELECT) ) {
+ _boot();
+ }
+
+ }
+
+ // For dual-analog and dual-shock (analog input)
+ if( ( pad->type == 0x5 ) || ( pad->type == 0x7 ) ) {
+
+ // Moving forwards and backwards
+ if( ( (pad->ls_y-128) < -16 ) || ( (pad->ls_y-128) > 16 ) ) {
+
+ cam_pos.vx += ((( isin( trot.vy )*icos( trot.vx ) )>>12)*(pad->ls_y-128))>>5;
+ cam_pos.vy -= (isin( trot.vx )*(pad->ls_y-128))>>5;
+ cam_pos.vz -= ((( icos( trot.vy )*icos( trot.vx ) )>>12)*(pad->ls_y-128))>>5;
+
+ }
+
+ // Strafing left and right
+ if( ( (pad->ls_x-128) < -16 ) || ( (pad->ls_x-128) > 16 ) ) {
+ cam_pos.vx += (icos( trot.vy )*(pad->ls_x-128))>>5;
+ cam_pos.vz += (isin( trot.vy )*(pad->ls_x-128))>>5;
+ }
+
+ // Look up and down
+ if( ( (pad->rs_y-128) < -16 ) || ( (pad->rs_y-128) > 16 ) ) {
+ cam_rot.vx += (pad->rs_y-128)<<9;
+ }
+
+ // Look left and right
+ if( ( (pad->rs_x-128) < -16 ) || ( (pad->rs_x-128) > 16 ) ) {
+ cam_rot.vy -= (pad->rs_x-128)<<9;
+ }
+
+ }
+
+ }
+
+ // Print out some info
+ FntPrint(-1, "BUTTONS=%04x\n", pad->btn);
+ FntPrint(-1, "X=%d Y=%d Z=%d\n",
+ cam_pos.vx>>12,
+ cam_pos.vy>>12,
+ cam_pos.vz>>12);
+ FntPrint(-1, "RX=%d RY=%d\n",
+ cam_rot.vx>>12,
+ cam_rot.vy>>12);
+
+ if( cam_mode == 0 ) { // First-person camera mode
+
+ // Set rotation to the matrix
+ RotMatrix( &trot, &mtx );
+
+ // Divide out the fractions of camera coordinates and invert
+ // the sign, so camera coordinates will line up to world
+ // (or geometry) coordinates
+ tpos.vx = -cam_pos.vx >> 12;
+ tpos.vy = -cam_pos.vy >> 12;
+ tpos.vz = -cam_pos.vz >> 12;
+
+ // Squish perspective horizontally for 16:9 aspect correction
+ ScaleMatrixL(&mtx, &cam_scale);
+
+ // Apply rotation of matrix to translation value to achieve a
+ // first person perspective
+ ApplyMatrixLV( &mtx, &tpos, &tpos );
+
+ // Set translation to matrix
+ TransMatrix( &mtx, &tpos );
+
+ } else { // Tracking mode
+
+ // Vector that defines the 'up' direction of the camera
+ SVECTOR up = { 0, -ONE, 0 };
+
+ // Divide out fractions of camera coordinates
+ tpos.vx = cam_pos.vx >> 12;
+ tpos.vy = cam_pos.vy >> 12;
+ tpos.vz = cam_pos.vz >> 12;
+
+ // Look at the cube
+ LookAt(&tpos, &pos, &up, &mtx);
+
+ // Squish perspective horizontally for 16:9 aspect correction
+ ScaleMatrixL(&mtx, &cam_scale);
+
+ // Clear translation coordinates of matrix, they aren't scaled
+ // by ScaleMatrixL(), so the result will look wrong after scaling
+ mtx.t[0] = 0; mtx.t[1] = 0; mtx.t[2] = 0;
+
+ tpos.vx = -cam_pos.vx >> 12;
+ tpos.vy = -cam_pos.vy >> 12;
+ tpos.vz = -cam_pos.vz >> 12;
+
+ ApplyMatrixLV( &mtx, &tpos, &tpos );
+
+ // Set translation to matrix
+ TransMatrix( &mtx, &tpos );
+
+ }
+
+ // Set rotation and translation matrix
+ gte_SetRotMatrix( &mtx );
+ gte_SetTransMatrix( &mtx );
+
+ // Draw the floor
+ pol4 = (POLY_F4*)db_nextpri;
+
+ for( py=0; py<16; py++ ) {
+ for( px=0; px<16; px++ ) {
+
+ // Load first three vertices to GTE
+ gte_ldv3(
+ &verts[py][px],
+ &verts[py][px+1],
+ &verts[py+1][px] );
+
+ gte_rtpt();
+
+ gte_avsz3();
+ gte_stotz( &p );
+
+ if( ( (p>>2) >= OT_LEN ) || ( (p>>2) <= 0 ) )
+ continue;
+
+ setPolyF4( pol4 );
+
+ // Set the projected vertices to the primitive
+ gte_stsxy0( &pol4->x0 );
+ gte_stsxy1( &pol4->x1 );
+ gte_stsxy2( &pol4->x2 );
+
+ // Compute the last vertex and set the result
+ gte_ldv0( &verts[py+1][px+1] );
+ gte_rtps();
+ gte_stsxy( &pol4->x3 );
+
+ // Test if quad is off-screen, discard if so
+ // Clipping is important as it not only prevents primitive
+ // overflows (tends to happen on textured polys) but also
+ // saves packet buffer space and speeds up rendering.
+ if( quad_clip( &screen_clip,
+ (DVECTOR*)&pol4->x0, (DVECTOR*)&pol4->x1,
+ (DVECTOR*)&pol4->x2, (DVECTOR*)&pol4->x3 ) )
+ continue;
+
+ gte_avsz4();
+ gte_stotz( &p );
+
+ if((px+py)&0x1) {
+ setRGB0( pol4, 128, 128, 128 );
+ } else {
+ setRGB0( pol4, 255, 255, 255 );
+ }
+
+ addPrim( db[db_active].ot+(p>>2), pol4 );
+ pol4++;
+
+ }
+ }
+
+ // Update nextpri variable (very important)
+ db_nextpri = (char*)pol4;
+
+
+ // Position the cube going around the floor bouncily
+ setVector( &pos,
+ isin( rot.vy )>>4,
+ -300+(isin( rot.vy<<2 )>>5),
+ icos( rot.vy )>>3 );
+
+ // Sort cube
+ sort_cube( &mtx, &pos, &rot );
+
+ // Make the cube SPEEN
+ rot.vx += 8;
+ rot.vy += 8;
+
+
+ // Flush text to drawing area
+ FntFlush(-1);
+
+ // Swap buffers and draw the primitives
+ display();
+
+ }
+
+ return 0;
+
+}
+
+void sort_cube(MATRIX *mtx, VECTOR *pos, SVECTOR *rot) {
+
+ int i,p;
+ POLY_F4 *pol4;
+
+ // Object and light matrix for object
+ MATRIX omtx,lmtx;
+
+ // Set object rotation and position
+ RotMatrix( rot, &omtx );
+ TransMatrix( &omtx, pos );
+
+ // Multiply light matrix to object matrix
+ MulMatrix0( &light_mtx, &omtx, &lmtx );
+
+ // Set result to GTE light matrix
+ gte_SetLightMatrix( &lmtx );
+
+ // Composite coordinate matrix transform, so object will be rotated and
+ // positioned relative to camera matrix (mtx), so it'll appear as
+ // world-space relative.
+ CompMatrixLV( mtx, &omtx, &omtx );
+
+ // Save matrix
+ PushMatrix();
+
+ // Set matrices
+ gte_SetRotMatrix( &omtx );
+ gte_SetTransMatrix( &omtx );
+
+ // Sort the cube
+ pol4 = (POLY_F4*)db_nextpri;
+
+ for( i=0; i<CUBE_FACES; i++ ) {
+
+ // Load the first 3 vertices of a quad to the GTE
+ gte_ldv3(
+ &cube_verts[cube_indices[i].v0],
+ &cube_verts[cube_indices[i].v1],
+ &cube_verts[cube_indices[i].v2] );
+
+ // Rotation, Translation and Perspective Triple
+ gte_rtpt();
+
+ // Compute normal clip for backface culling
+ gte_nclip();
+
+ // Get result
+ gte_stopz( &p );
+
+ // Skip this face if backfaced
+ if( p < 0 )
+ continue;
+
+ // Calculate average Z for depth sorting
+ gte_avsz3();
+ gte_stotz( &p );
+
+ // Skip if clipping off
+ // (the shift right operator is to scale the depth precision)
+ if( ((p>>2) <= 0) || ((p>>2) >= OT_LEN) )
+ continue;
+
+ // Initialize a quad primitive
+ setPolyF4( pol4 );
+
+ // Set the projected vertices to the primitive
+ gte_stsxy0( &pol4->x0 );
+ gte_stsxy1( &pol4->x1 );
+ gte_stsxy2( &pol4->x2 );
+
+ // Compute the last vertex and set the result
+ gte_ldv0( &cube_verts[cube_indices[i].v3] );
+ gte_rtps();
+ gte_stsxy( &pol4->x3 );
+
+ // Test if quad is off-screen, discard if so
+ if( quad_clip( &screen_clip,
+ (DVECTOR*)&pol4->x0, (DVECTOR*)&pol4->x1,
+ (DVECTOR*)&pol4->x2, (DVECTOR*)&pol4->x3 ) )
+ continue;
+
+ // Load primitive color even though gte_ncs() doesn't use it.
+ // This is so the GTE will output a color result with the
+ // correct primitive code.
+ gte_ldrgb( &pol4->r0 );
+
+ // Load the face normal
+ gte_ldv0( &cube_norms[i] );
+
+ // Normal Color Single
+ gte_ncs();
+
+ // Store result to the primitive
+ gte_strgb( &pol4->r0 );
+
+ gte_avsz4();
+ gte_stotz( &p );
+
+ // Sort primitive to the ordering table
+ addPrim( db[db_active].ot+(p>>2), pol4 );
+
+ // Advance to make another primitive
+ pol4++;
+
+ }
+
+ // Update nextpri
+ db_nextpri = (char*)pol4;
+
+ // Restore matrix
+ PopMatrix();
+
+}
+
+void init() {
+
+ // Reset the GPU, also installs a VSync event handler
+ ResetGraph( 0 );
+
+ // Set display and draw environment areas
+ // (display and draw areas must be separate, otherwise hello flicker)
+ SetDefDispEnv( &db[0].disp, 0, 0, 640, SCREEN_YRES );
+ SetDefDrawEnv( &db[0].draw, 0, 0, SCREEN_XRES, SCREEN_YRES );
+
+ db[0].disp.isinter = 1; // enable interlace, mandatory for hi-res modes
+
+ db[0].disp.screen.x = -37; // shift left to center the widened picture
+
+ db[0].disp.screen.w = 704; // increase the picture width to 704 pixels
+ // (this does not strech the picture, it just
+ // tells the GPU to output more pixels)
+
+ // Enable draw area clear and dither processing
+ setRGB0( &db[0].draw, 63, 0, 127 );
+ db[0].draw.isbg = 1;
+ db[0].draw.dtd = 1;
+
+
+ // Define the second set of display/draw environments
+ SetDefDispEnv( &db[1].disp, 0, 0, 640, SCREEN_YRES );
+ SetDefDrawEnv( &db[1].draw, 0, 0, SCREEN_XRES, SCREEN_YRES );
+
+ db[1].disp.isinter = 1; // enable interlace
+ db[1].disp.screen = db[0].disp.screen; // copy the custom screen coords
+
+ setRGB0( &db[1].draw, 63, 0, 127 );
+ db[1].draw.isbg = 1;
+ db[1].draw.dtd = 1;
+
+ // Apply the drawing environment of the first double buffer
+ PutDrawEnv( &db[0].draw );
+
+ // Clear both ordering tables to make sure they are clean at the start
+ ClearOTagR( db[0].ot, OT_LEN );
+ ClearOTagR( db[1].ot, OT_LEN );
+
+ // Set primitive pointer address
+ db_nextpri = db[0].p;
+
+ // Set clip region
+ setRECT( &screen_clip, 0, 0, SCREEN_XRES, SCREEN_YRES );
+
+
+ // Initialize the GTE
+ InitGeom();
+
+ // Set GTE offset (recommended method of centering)
+ gte_SetGeomOffset( CENTERX, CENTERY );
+
+ // Set screen depth (basically FOV control, W/2 works best)
+ gte_SetGeomScreen( CENTERX );
+
+ // Set light ambient color and light color matrix
+ gte_SetBackColor( 63, 63, 63 );
+ gte_SetColorMatrix( &color_mtx );
+
+
+ // Init BIOS pad driver and set pad buffers (buffers are updated
+ // automatically on every V-Blank)
+ InitPAD(&pad_buff[0][0], 34, &pad_buff[1][0], 34);
+
+ // Start pad
+ StartPAD();
+
+ // Don't make pad driver acknowledge V-Blank IRQ (to avoid VSync timeout)
+ ChangeClearPAD(0);
+
+ // Load font and open a text stream
+ FntLoad(960, 0);
+ FntOpen(0, 8, 320, 216, 0, 100);
+
+}
+
+void display() {
+
+ // Wait for GPU to finish drawing and vertical retrace
+ DrawSync( 0 );
+ VSync( 0 );
+
+ // Swap buffers
+ db_active ^= 1;
+ db_nextpri = db[db_active].p;
+
+ // Clear the OT of the next frame
+ ClearOTagR( db[db_active].ot, OT_LEN );
+
+ // Apply display/drawing environments
+ PutDrawEnv( &db[db_active].draw );
+ PutDispEnv( &db[db_active].disp );
+
+ // Enable display
+ SetDispMask( 1 );
+
+ // Start drawing the OT of the last buffer
+ DrawOTag( db[1-db_active].ot+(OT_LEN-1) );
+
+}
+
diff --git a/examples/graphics/hdtv/makefile b/examples/graphics/hdtv/makefile
new file mode 100644
index 0000000..d9321bf
--- /dev/null
+++ b/examples/graphics/hdtv/makefile
@@ -0,0 +1,35 @@
+include ../../examples-setup.mk
+
+TARGET = hdtv.elf
+
+CFILES = $(notdir $(wildcard *.c))
+CPPFILES = $(notdir $(wildcard *.cpp))
+AFILES = $(notdir $(wildcard *.s))
+
+OFILES = $(addprefix build/,$(CFILES:.c=.o) $(CPPFILES:.cpp=.o) $(AFILES:.s=.o))
+
+INCLUDE +=
+LIBDIRS +=
+
+LIBS = -lpsxetc -lpsxgpu -lpsxgte -lpsxspu -lpsxetc -lpsxapi -lc
+LIBS = -lpsxgpu -lpsxgte -lpsxspu -lpsxetc -lpsxapi -lc
+
+CFLAGS = -g -O2 -fno-builtin -fdata-sections -ffunction-sections
+CPPFLAGS = $(CFLAGS) -fno-exceptions
+AFLAGS = -g -msoft-float
+LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
+
+all: $(OFILES)
+ $(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
+ elf2x -q $(TARGET)
+
+build/%.o: %.c
+ @mkdir -p $(dir $@)
+ $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
+
+build/%.o: %.s
+ @mkdir -p $(dir $@)
+ $(CC) $(AFLAGS) $(INCLUDE) -c $< -o $@
+
+clean:
+ rm -rf build $(TARGET) $(TARGET:.elf=.exe)
diff --git a/examples/graphics/render2tex/makefile b/examples/graphics/render2tex/makefile
index a6c2e91..c69d37f 100644
--- a/examples/graphics/render2tex/makefile
+++ b/examples/graphics/render2tex/makefile
@@ -18,11 +18,6 @@ CPPFLAGS = $(CFLAGS) -fno-exceptions
AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/graphics/rgb24/makefile b/examples/graphics/rgb24/makefile
index ca99600..5c08f87 100644
--- a/examples/graphics/rgb24/makefile
+++ b/examples/graphics/rgb24/makefile
@@ -18,11 +18,6 @@ CPPFLAGS = $(CFLAGS) -fno-exceptions
AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/system/childexec/makefile b/examples/system/childexec/makefile
index 4475832..b1952fc 100644
--- a/examples/system/childexec/makefile
+++ b/examples/system/childexec/makefile
@@ -13,9 +13,6 @@ LDFLAGS = -g -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32
LDFLAGS_P = $(LDFLAGS) -Ttext=0x80010000
LDFLAGS_C = $(LDFLAGS) -Ttext=0x80030000
-CC = $(PREFIX)gcc
-LD = $(PREFIX)ld
-
all: child parent
child: build/child.o
diff --git a/examples/system/console/makefile b/examples/system/console/makefile
index d7e9374..4e0456e 100644
--- a/examples/system/console/makefile
+++ b/examples/system/console/makefile
@@ -34,12 +34,6 @@ AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections \
-T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-# Toolchain programs
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/system/timer/makefile b/examples/system/timer/makefile
index 3fe1562..4b4c1a6 100644
--- a/examples/system/timer/makefile
+++ b/examples/system/timer/makefile
@@ -18,11 +18,6 @@ CPPFLAGS = $(CFLAGS) -fno-exceptions
AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections -T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)
diff --git a/examples/system/tty/makefile b/examples/system/tty/makefile
index d0a8caa..5514916 100644
--- a/examples/system/tty/makefile
+++ b/examples/system/tty/makefile
@@ -34,12 +34,6 @@ AFLAGS = -g -msoft-float
LDFLAGS = -g -Ttext=0x80010000 -gc-sections \
-T $(GCC_BASE)/mipsel-unknown-elf/lib/ldscripts/elf32elmip.x
-# Toolchain programs
-CC = $(PREFIX)gcc
-CXX = $(PREFIX)g++
-AS = $(PREFIX)as
-LD = $(PREFIX)ld
-
all: $(OFILES)
$(LD) $(LDFLAGS) $(LIBDIRS) $(OFILES) $(LIBS) -o $(TARGET)
elf2x -q $(TARGET)