From 93d3d5d84fb724eeca431046c19ba58e8e8191ac Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 27 Mar 2024 17:02:31 +1100 Subject: [PATCH] PS1: 3D clearing works I guess --- src/Core.h | 7 +++- src/Graphics_PS1.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++ src/Platform_PS1.c | 1 + src/Program.c | 2 +- src/Protocol.c | 10 +++++- src/Server.c | 17 ++++++--- src/Window_PS1.c | 20 ++++------- 7 files changed, 123 insertions(+), 22 deletions(-) diff --git a/src/Core.h b/src/Core.h index d7fac6699..2ac752cac 100644 --- a/src/Core.h +++ b/src/Core.h @@ -118,6 +118,7 @@ typedef cc_uint8 cc_bool; #endif +#define CC_BUILD_NETWORKING #define CC_BUILD_FREETYPE #ifndef CC_BUILD_FLATPAK #define CC_BUILD_RESOURCES @@ -329,6 +330,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_CONSOLE #undef CC_BUILD_FREETYPE #undef CC_BUILD_RESOURCES + #undef CC_BUILD_NETWORKING #elif defined PLAT_PS2 #define CC_BUILD_PS2 #define CC_BUILD_OPENAL @@ -370,12 +372,15 @@ typedef cc_uint8 cc_bool; #undef CC_BUILD_FREETYPE #elif defined PLAT_PS1 #define CC_BUILD_PS1 - #define CC_BUILD_OPENAL #define CC_BUILD_HTTPCLIENT #define CC_BUILD_COOPTHREADED #define CC_BUILD_LOWMEM #define CC_BUILD_CONSOLE + #define CC_BUILD_NOMUSIC + #define CC_BUILD_NOSOUNDS #undef CC_BUILD_FREETYPE + #undef CC_BUILD_RESOURCES + #undef CC_BUILD_NETWORKING #endif #endif diff --git a/src/Graphics_PS1.c b/src/Graphics_PS1.c index d05b9837c..2ca807b52 100644 --- a/src/Graphics_PS1.c +++ b/src/Graphics_PS1.c @@ -3,6 +3,75 @@ #include "_GraphicsBase.h" #include "Errors.h" #include "Window.h" +#include +#include +#include +#include +// Based off https://github.com/Lameguy64/PSn00bSDK/blob/master/examples/beginner/hello/main.c + + +// Length of the ordering table, i.e. the range Z coordinates can have, 0-15 in +// this case. Larger values will allow for more granularity with depth (useful +// when drawing a complex 3D scene) at the expense of RAM usage and performance. +#define OT_LENGTH 64 + +// Size of the buffer GPU commands and primitives are written to. If the program +// crashes due to too many primitives being drawn, increase this value. +#define BUFFER_LENGTH 8192 + +typedef struct { + DISPENV disp_env; + DRAWENV draw_env; + + cc_uint32 ot[OT_LENGTH]; + cc_uint8 buffer[BUFFER_LENGTH]; +} RenderBuffer; + +static RenderBuffer buffers[2]; +static cc_uint8* next_packet; +static int active_buffer; + +static void SetupContexts(int w, int h, int r, int g, int b) { + SetDefDrawEnv(&buffers[0].draw_env, 0, 0, w, h); + SetDefDispEnv(&buffers[0].disp_env, 0, 0, w, h); + SetDefDrawEnv(&buffers[1].draw_env, 0, h, w, h); + SetDefDispEnv(&buffers[1].disp_env, 0, h, w, h); + + setRGB0(&buffers[0].draw_env, r, g, b); + setRGB0(&buffers[1].draw_env, r, g, b); + buffers[0].draw_env.isbg = 1; + buffers[1].draw_env.isbg = 1; + + active_buffer = 0; + next_packet = buffers[0].buffer; + ClearOTagR(buffers[0].ot, OT_LENGTH); +} + +static void FlipBuffers(void) { + DrawSync(0); + VSync(0); + + RenderBuffer* draw_buffer = &buffers[active_buffer]; + RenderBuffer* disp_buffer = &buffers[active_buffer ^ 1]; + + PutDispEnv(&disp_buffer->disp_env); + DrawOTagEnv(&draw_buffer->ot[OT_LENGTH - 1], &draw_buffer->draw_env); + + active_buffer ^= 1; + next_packet = disp_buffer->buffer; + ClearOTagR(disp_buffer->ot, OT_LENGTH); +} + +static void* new_primitive(int z, int size) { + RenderBuffer* buffer = &buffers[active_buffer]; + uint8_t* prim = next_packet; + + addPrim(&buffer->ot[z], prim); + next_packet += size; + + assert(next_packet <= &buffer->buffer[BUFFER_LENGTH]); + return (void*)prim; +} void Gfx_RestoreState(void) { InitDefaultResources(); @@ -18,6 +87,10 @@ void Gfx_Create(void) { Gfx.Created = true; Gfx_RestoreState(); + ResetGraph(0); + + SetupContexts(Window_Main.Width, Window_Main.Height, 63, 0, 127); + SetDispMask(1); } void Gfx_Free(void) { @@ -78,6 +151,12 @@ void Gfx_ClearBuffers(GfxBuffers buffers) { } void Gfx_ClearColor(PackedCol color) { + int r = PackedCol_R(color); + int g = PackedCol_G(color); + int b = PackedCol_B(color); + + setRGB0(&buffers[0].draw_env, r, g, b); + setRGB0(&buffers[1].draw_env, r, g, b); } void Gfx_SetDepthTest(cc_bool enabled) { @@ -248,9 +327,18 @@ cc_bool Gfx_WarnIfNecessary(void) { } void Gfx_BeginFrame(void) { + // Draw the square by allocating a TILE (i.e. untextured solid color + // rectangle) primitive at Z = 1. + TILE *tile = (TILE *)new_primitive(1, sizeof(TILE)); + + setTile(tile); + setXY0 (tile, 40, 40); + setWH (tile, 64, 64); + setRGB0(tile, 255, 255, 0); } void Gfx_EndFrame(void) { + FlipBuffers(); if (gfx_minFrameMs) LimitFPS(); } diff --git a/src/Platform_PS1.c b/src/Platform_PS1.c index dfd452dca..87e9b1082 100644 --- a/src/Platform_PS1.c +++ b/src/Platform_PS1.c @@ -55,6 +55,7 @@ void DateTime_CurrentLocal(struct DateTime* t) { static volatile cc_uint32 irq_count; cc_uint64 Stopwatch_Measure(void) { + //Platform_Log1("IRQ: %i", &irq_count); return irq_count; } diff --git a/src/Program.c b/src/Program.c index fef95a37f..104c9bfda 100644 --- a/src/Program.c +++ b/src/Program.c @@ -168,4 +168,4 @@ int main(int argc, char** argv) { Process_Exit(res); return res; } -#endif \ No newline at end of file +#endif diff --git a/src/Protocol.c b/src/Protocol.c index e4d45ba39..0459a32c7 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1,9 +1,10 @@ #include "Protocol.h" +#include "Game.h" +#ifdef CC_BUILD_NETWORKING #include "String.h" #include "Deflate.h" #include "Server.h" #include "Stream.h" -#include "Game.h" #include "Entity.h" #include "Platform.h" #include "Screens.h" @@ -1872,6 +1873,13 @@ static void OnReset(void) { Protocol_Reset(); FreeMapStates(); } +#else +void CPE_SendPlayerClick(int button, cc_bool pressed, cc_uint8 targetId, struct RayTracer* t) { } + +static void OnInit(void) { } + +static void OnReset(void) { } +#endif struct IGameComponent Protocol_Component = { OnInit, /* Init */ diff --git a/src/Server.c b/src/Server.c index cd13c2cbd..3d1b8dad3 100644 --- a/src/Server.c +++ b/src/Server.c @@ -128,7 +128,7 @@ static void SPConnection_BeginConnect(void) { Random_SeedFromCurrentTime(&rnd); World_NewMap(); -#if defined CC_BUILD_NDS +#if defined CC_BUILD_NDS || defined CC_BUILD_PS1 World_SetDimensions(16, 16, 16); #elif defined CC_BUILD_LOWMEM World_SetDimensions(64, 64, 64); @@ -136,7 +136,7 @@ static void SPConnection_BeginConnect(void) { World_SetDimensions(128, 64, 128); #endif -#if defined CC_BUILD_N64 || defined CC_BUILD_NDS +#if defined CC_BUILD_N64 || defined CC_BUILD_NDS || defined CC_BUILD_PS1 Gen_Active = &FlatgrassGen; #else Gen_Active = &NotchyGen; @@ -223,10 +223,12 @@ static void SPConnection_Init(void) { *--------------------------------------------------Multiplayer connection-------------------------------------------------* *#########################################################################################################################*/ static cc_socket net_socket = -1; +static cc_result net_writeFailure; +static void OnClose(void); + +#ifdef CC_BUILD_NETWORKING static cc_uint8 net_readBuffer[4096 * 5]; static cc_uint8* net_readCurrent; - -static cc_result net_writeFailure; static double net_lastPacket; static cc_uint8 lastOpcode; @@ -234,7 +236,6 @@ static cc_bool net_connecting; static double net_connectTimeout; #define NET_TIMEOUT_SECS 15 -static void OnClose(void); static void MPConnection_FinishConnect(void) { net_connecting = false; Event_RaiseVoid(&NetEvents.Connected); @@ -475,8 +476,14 @@ static void MPConnection_Init(void) { Server.SendData = MPConnection_SendData; net_readCurrent = net_readBuffer; } +#else +static void MPConnection_Init(void) { SPConnection_Init(); } +#endif +/*########################################################################################################################* +*---------------------------------------------------Component interface---------------------------------------------------* +*#########################################################################################################################*/ static void OnNewMap(void) { int i; if (Server.IsSinglePlayer) return; diff --git a/src/Window_PS1.c b/src/Window_PS1.c index 833224fd3..df1fa58d1 100644 --- a/src/Window_PS1.c +++ b/src/Window_PS1.c @@ -33,8 +33,8 @@ void Window_Init(void) { DisplayInfo.Width = SCREEN_XRES; DisplayInfo.Height = SCREEN_YRES; DisplayInfo.Depth = 4; // 32 bit - DisplayInfo.ScaleX = 1; - DisplayInfo.ScaleY = 1; + DisplayInfo.ScaleX = 0.5f; + DisplayInfo.ScaleY = 0.5f; Window_Main.Width = DisplayInfo.Width; Window_Main.Height = DisplayInfo.Height; @@ -150,26 +150,18 @@ void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) { rect.w = SCREEN_XRES; rect.h = SCREEN_YRES; - HeapUsage usage; - GetHeapUsage(&usage); - - Platform_Log4("%i / %i / %i / %i", &usage.total, &usage.heap, &usage.stack, &usage.alloc); - - - for (int y = 0; y < bmp->height; y++) + for (int y = r.y; y < r.y + r.Height; y++) { cc_uint32* src = bmp->scan0 + y * bmp->width; cc_uint16* dst = fb + y * bmp->width; - for (int x = 0; x < bmp->width; x++) { + for (int x = r.x; x < r.x + r.Width; x++) { cc_uint8* color = (cc_uint8*)&src[x]; - dst[x] = BGRA8_to_PS1(0); + dst[x] = BGRA8_to_PS1(color); } } - Platform_Log4("%i / %i / %i / %i", &usage.total, &usage.heap, &usage.stack, &usage.alloc); - - LoadImage(&rect, bmp->scan0); + LoadImage(&rect, fb); DrawSync(0); }