diff options
| author | spicyjpeg <thatspicyjpeg@gmail.com> | 2023-05-11 19:11:35 +0200 |
|---|---|---|
| committer | spicyjpeg <thatspicyjpeg@gmail.com> | 2023-05-11 19:11:35 +0200 |
| commit | 3b696fc431a9c3f2aa7ea4f27aec20ce5dd67859 (patch) | |
| tree | 3cf2dc24f38ad1b532f0ec913c3604d334e79872 | |
| parent | ca6b54f3c15a7b00a5ede64ba452f2955a421a1e (diff) | |
Add C++ standard library headers, update beginner/cppdemo
| -rw-r--r-- | examples/beginner/cppdemo/main.cpp | 274 | ||||
| -rw-r--r-- | libpsn00b/include/cassert | 8 | ||||
| -rw-r--r-- | libpsn00b/include/cctype | 22 | ||||
| -rw-r--r-- | libpsn00b/include/cstdint | 34 | ||||
| -rw-r--r-- | libpsn00b/include/cstdio | 32 | ||||
| -rw-r--r-- | libpsn00b/include/cstdlib | 59 | ||||
| -rw-r--r-- | libpsn00b/include/cstring | 38 |
7 files changed, 327 insertions, 140 deletions
diff --git a/examples/beginner/cppdemo/main.cpp b/examples/beginner/cppdemo/main.cpp index fd2e3a8..f55f952 100644 --- a/examples/beginner/cppdemo/main.cpp +++ b/examples/beginner/cppdemo/main.cpp @@ -1,157 +1,151 @@ -/* Work in progress example, need to add comments. +/* Work in progress example, need to add comments. * * Basically a quick little example that showcases C++ classes are * functioning in PSn00bSDK. - Lameguy64 * - * First written in December 18, 2020. + * First written in December 18, 2020. * * Changelog: * - * May 10, 2021 - Variable types updated for psxgpu.h changes. + * May 11, 2023 - Updated the example to use C++ standard library headers, + * renamed the class and cleaned up some methods. + * May 10, 2021 - Variable types updated for psxgpu.h changes. * */ - -#include <sys/types.h> -#include <stdio.h> -#include <stdlib.h> + +#include <cstddef> +#include <cstdint> +#include <cstdio> +#include <cstdlib> #include <psxgte.h> #include <psxgpu.h> -class GraphClass -{ - u_long *_ot[2]; - u_char *_pri[2]; - u_char *_nextpri; +/* Example class */ + +class RenderContext { +private: + std::uint32_t *_ot[2]; + std::uint8_t *_pri[2]; + std::uint8_t *_nextpri; + + int _ot_count, _db; - int _ot_count; - int _db; - - DISPENV _disp[2]; + DISPENV _disp[2]; DRAWENV _draw[2]; - + public: + RenderContext(std::size_t ot_len = 8, std::size_t pri_len = 8192); + ~RenderContext(void); + void setupBuffers(int w, int h, int r, int g, int b); + void flip(void); + + template<typename T> inline T *addPrimitive(void) { + auto pri = reinterpret_cast<T *>(_nextpri); + addPrim(_ot[_db], pri); + + _nextpri += sizeof(T); + return pri; + } +}; + +RenderContext::RenderContext(std::size_t ot_len, std::size_t pri_len) { + _ot[0] = new std::uint32_t[ot_len]; + _ot[1] = new std::uint32_t[ot_len]; + + _db = 0; + _ot_count = ot_len; + ClearOTagR(_ot[0], _ot_count); + ClearOTagR(_ot[1], _ot_count); + + _pri[0] = new std::uint8_t[pri_len]; + _pri[1] = new std::uint8_t[pri_len]; + + _nextpri = _pri[0]; + + std::printf("RenderContext::RenderContext: Buffers allocated.\n"); +} + +RenderContext::~RenderContext(void) { + delete[] _ot[0]; + delete[] _ot[1]; + + delete[] _pri[0]; + delete[] _pri[1]; + + std::printf("RenderContext::RenderContext: Buffers freed.\n"); +} + +void RenderContext::setupBuffers(int w, int h, int r, int g, int b) { + SetDefDispEnv(&_disp[0], 0, h, w, h); + SetDefDispEnv(&_disp[1], 0, 0, w, h); + SetDefDrawEnv(&_draw[0], 0, 0, w, h); + SetDefDrawEnv(&_draw[1], 0, h, w, h); + + setRGB0(&_draw[0], r, g, b); + _draw[0].isbg = 1; + _draw[0].dtd = 1; - GraphClass( int ot_len = 8, int pri_len = 8192 ) - { - _ot[0] = (u_long*)malloc( sizeof(u_long)*ot_len ); - _ot[1] = (u_long*)malloc( sizeof(u_long)*ot_len ); - - _db = 0; - _ot_count = ot_len; - ClearOTagR( _ot[0], _ot_count ); - ClearOTagR( _ot[1], _ot_count ); - - _pri[0] = (u_char*)malloc( pri_len ); - _pri[1] = (u_char*)malloc( pri_len ); - - _nextpri = _pri[0]; - - printf( "GraphClass::GraphClass: Buffers allocated.\n" ); - - } /* GraphClass */ - - virtual ~GraphClass() - { - /* free the OTs and primitive buffers */ - free( _ot[0] ); - free( _ot[1] ); - - free( _pri[0] ); - free( _pri[1] ); - - printf( "GraphClass::GraphClass: Buffers freed.\n" ); - - } /* ~GraphClass */ - - void SetRes( int w, int h ) - { - SetDefDispEnv( &_disp[0], 0, h, w, h ); - SetDefDispEnv( &_disp[1], 0, 0, w, h ); - - SetDefDrawEnv( &_draw[0], 0, 0, w, h ); - SetDefDrawEnv( &_draw[1], 0, h, w, h ); - - setRGB0( &_draw[0], 63, 0, 127 ); - _draw[0].isbg = 1; - _draw[0].dtd = 1; - setRGB0( &_draw[1], 63, 0, 127 ); - _draw[1].isbg = 1; - _draw[1].dtd = 1; - - PutDispEnv( &_disp[0] ); - PutDrawEnv( &_draw[0] ); - - } /* SetRes */ - - void IncPri( int bytes ) - { - _nextpri += bytes; - - } /* IncPri */ - - void SetPri( u_char *ptr ) - { - _nextpri = ptr; - - } /* SetPri */ - - u_char *GetNextPri( void ) - { - return( _nextpri ); - - } /* GetNextPri */ - - u_long *GetOt( void ) - { - return( _ot[_db] ); - - } /* GetOt */ - - void Display( void ) - { - VSync( 0 ); - DrawSync( 0 ); - SetDispMask( 1 ); - - _db = !_db; - - PutDispEnv( &_disp[_db] ); - PutDrawEnv( &_draw[_db] ); - - DrawOTag( _ot[!_db]+(_ot_count-1) ); - - ClearOTagR( _ot[_db], _ot_count ); - _nextpri = _pri[_db]; - - } /* Display */ - -}; /* GraphClass */ - -GraphClass *otable; - -int main( int argc, const char *argv[] ) -{ - TILE *tile; - - ResetGraph( 0 ); - - otable = new GraphClass(); - - otable->SetRes( 320, 240 ); - - while( 1 ) - { - tile = (TILE*)otable->GetNextPri(); - setTile( tile ); - setXY0( tile, 32, 32 ); - setWH( tile, 128, 128 ); - setRGB0( tile, 255, 255, 0 ); - addPrim( otable->GetOt(), tile ); - otable->IncPri( sizeof(TILE) ); - - otable->Display(); + setRGB0(&_draw[1], r, g, b); + _draw[1].isbg = 1; + _draw[1].dtd = 1; + + PutDispEnv(&_disp[0]); + PutDrawEnv(&_draw[0]); +} + +void RenderContext::flip(void) { + DrawSync(0); + VSync(0); + + _db ^= 1; + + PutDispEnv(&_disp[_db]); + PutDrawEnv(&_draw[_db]); + + DrawOTag(_ot[_db ^ 1] + _ot_count - 1); + ClearOTagR(_ot[_db], _ot_count); + + _nextpri = _pri[_db]; +} + +/* Main */ + +static constexpr int SCREEN_XRES = 320; +static constexpr int SCREEN_YRES = 240; + +static constexpr int BGCOLOR_R = 63; +static constexpr int BGCOLOR_G = 0; +static constexpr int BGCOLOR_B = 127; + +int main(int argc, const char **argv) { + ResetGraph(0); + SetDispMask(1); + + RenderContext ctx; + ctx.setupBuffers(SCREEN_XRES, SCREEN_YRES, BGCOLOR_R, BGCOLOR_G, BGCOLOR_B); + + int x = 0, y = 0; + int dx = 1, dy = 1; + + for (;;) { + // Update the position and velocity of the bouncing square. + if (x < 0 || x > (SCREEN_XRES - 64)) + dx = -dx; + if (y < 0 || y > (SCREEN_YRES - 64)) + dy = -dy; + + x += dx; + y += dy; + + // Draw the square. + auto tile = ctx.addPrimitive<TILE>(); + setTile(tile); + setXY0 (tile, x, y); + setWH (tile, 64, 64); + setRGB0(tile, 255, 255, 0); + + ctx.flip(); } - - return( 0 ); - -} /* main */
\ No newline at end of file + + return 0; +} diff --git a/libpsn00b/include/cassert b/libpsn00b/include/cassert new file mode 100644 index 0000000..0923486 --- /dev/null +++ b/libpsn00b/include/cassert @@ -0,0 +1,8 @@ +/* + * PSn00bSDK assert macro and internal logging + * (C) 2022-2023 spicyjpeg - MPL licensed + */ + +#pragma once + +#include <assert.h> diff --git a/libpsn00b/include/cctype b/libpsn00b/include/cctype new file mode 100644 index 0000000..b73ad34 --- /dev/null +++ b/libpsn00b/include/cctype @@ -0,0 +1,22 @@ +/* + * PSn00bSDK standard library + * (C) 2019-2023 PSXSDK authors, Lameguy64, spicyjpeg - MPL licensed + */ + +#pragma once + +namespace std { +extern "C" { + +int isprint(int ch); +int isgraph(int ch); +int isspace(int ch); +int isblank(int ch); +int isalpha(int ch); +int isdigit(int ch); + +int tolower(int ch); +int toupper(int ch); + +} +} diff --git a/libpsn00b/include/cstdint b/libpsn00b/include/cstdint new file mode 100644 index 0000000..3b1bc4a --- /dev/null +++ b/libpsn00b/include/cstdint @@ -0,0 +1,34 @@ +/* + * PSn00bSDK standard library + * (C) 2019-2023 Lameguy64, spicyjpeg - MPL licensed + * + * This is a replacement for the <cstdint> header included with GCC, which seems + * to be broken (at least in GCC 12.2.0) as it requires some macros to be set. + */ + +#pragma once + +#include <stdint.h> + +namespace std { + +#define _DEF_TYPE(bits, prefix) \ + using ::prefix##bits##_t; \ + using ::prefix##_fast##bits##_t; \ + using ::prefix##_least##bits##_t; + +_DEF_TYPE( 8, int) +_DEF_TYPE( 8, uint) +_DEF_TYPE(16, int) +_DEF_TYPE(16, uint) +_DEF_TYPE(32, int) +_DEF_TYPE(32, uint) + +#undef _DEF_TYPE + +using ::intmax_t; +using ::uintmax_t; +using ::intptr_t; +using ::uintptr_t; + +} diff --git a/libpsn00b/include/cstdio b/libpsn00b/include/cstdio new file mode 100644 index 0000000..800d1a2 --- /dev/null +++ b/libpsn00b/include/cstdio @@ -0,0 +1,32 @@ +/* + * PSn00bSDK standard library + * (C) 2019-2023 Lameguy64, spicyjpeg - MPL licensed + */ + +#pragma once + +#include <cstdarg> + +namespace std { +extern "C" { + +/* String I/O API (provided by BIOS) */ + +int printf(const char *fmt, ...); +char *gets(char *str); +void puts(const char *str); +int getchar(void); +void putchar(int ch); + +/* String formatting API (built-in) */ + +int vsnprintf(char *string, unsigned int size, const char *fmt, va_list ap); +int vsprintf(char *string, const char *fmt, va_list ap); +int sprintf(char *string, const char *fmt, ...); +int snprintf(char *string, unsigned int size, const char *fmt, ...); + +int vsscanf(const char *str, const char *format, va_list ap); +int sscanf(const char *str, const char *fmt, ...); + +} +} diff --git a/libpsn00b/include/cstdlib b/libpsn00b/include/cstdlib new file mode 100644 index 0000000..4fa859d --- /dev/null +++ b/libpsn00b/include/cstdlib @@ -0,0 +1,59 @@ +/* + * PSn00bSDK standard library + * (C) 2019-2023 PSXSDK authors, Lameguy64, spicyjpeg - MPL licensed + */ + +#pragma once + +#include <cstddef> + +namespace std { + +/* Definitions */ + +static constexpr int RAND_MAX = 0x7fff; + +/* Structure definitions */ + +struct HeapUsage { + size_t total; // Total size of heap + stack + size_t heap; // Amount of memory currently reserved for heap + size_t stack; // Amount of memory currently reserved for stack + size_t alloc; // Amount of memory currently allocated + size_t alloc_max; // Maximum amount of memory ever allocated +}; + +/* API */ + +extern "C" { + +extern int __argc; +extern const char **__argv; + +void abort(void); + +int rand(void); +void srand(int seed); + +int abs(int j); +long labs(long i); + +long strtol(const char *str, char **str_end, int base); +long long strtoll(const char *str, char **str_end, int base); +//float strtof(const char *str, char **str_end); +//double strtod(const char *str, char **str_end); +//long double strtold(const char *str, char **str_end); + +void InitHeap(void *addr, size_t size); +void *sbrk(ptrdiff_t incr); + +void TrackHeapUsage(ptrdiff_t alloc_incr); +void GetHeapUsage(HeapUsage *usage); + +void *malloc(size_t size); +void *calloc(size_t num, size_t size); +void *realloc(void *ptr, size_t size); +void free(void *ptr); + +} +} diff --git a/libpsn00b/include/cstring b/libpsn00b/include/cstring new file mode 100644 index 0000000..1ce7246 --- /dev/null +++ b/libpsn00b/include/cstring @@ -0,0 +1,38 @@ +/* + * PSn00bSDK standard library + * (C) 2019-2023 PSXSDK authors, Lameguy64, spicyjpeg - MPL licensed + */ + +#pragma once + +#include <cstddef> + +namespace std { +extern "C" { + +void *memset(void *dest, int ch, size_t count); +void *memcpy(void *dest, const void *src, size_t count); +void *memccpy(void *dest, const void *src, int ch, size_t count); +void *memmove(void *dest, const void *src, size_t count); +int memcmp(const void *lhs, const void *rhs, size_t count); +void *memchr(const void *ptr, int ch, size_t count); + +char *strcpy(char *dest, const char *src); +char *strncpy(char *dest, const char *src, size_t count); +int strcmp(const char *lhs, const char *rhs); +int strncmp(const char *lhs, const char *rhs, size_t count); +char *strchr(const char *str, int ch); +char *strrchr(const char *str, int ch); +char *strpbrk(const char *str, const char *breakset); +char *strstr(const char *str, const char *substr); + +size_t strlen(const char *str); +char *strcat(char *dest, const char *src); +char *strncat(char *dest, const char *src, size_t count); +char *strdup(const char *str); +char *strndup(const char *str, size_t count); + +char *strtok(char *str, const char *delim); + +} +} |
