From 2c9ce97a0e67fedbab821508a55f67a27a6d158e Mon Sep 17 00:00:00 2001 From: Abdulmujeeb Raji Date: Thu, 16 Jan 2025 06:37:17 +0000 Subject: [PATCH] code style example, setup instructions and bastd.c --- LICENSE.md | 54 ++++++++++---------- README.md | 145 +++++++++++++++++++++++++++++++---------------------- bastd.c | 31 ++++++++++++ main.c | 60 ++++++++++++++++++++++ 4 files changed, 204 insertions(+), 86 deletions(-) create mode 100644 bastd.c create mode 100644 main.c diff --git a/LICENSE.md b/LICENSE.md index a6239f7..18bc3e4 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,27 +1,27 @@ -Copyright 2025 Abdulmujeeb Raji - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this -list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its contributors -may be used to endorse or promote products derived from this software without -specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - +Copyright 2025 Abdulmujeeb Raji + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/README.md b/README.md index 8e00085..023e008 100644 --- a/README.md +++ b/README.md @@ -1,60 +1,87 @@ -# Bravely Arranged Standard Library -`bastd` is my own custom standard library for C, made to aid in the development -of games, 3D software, 2D development tools and more. - -It strays heavily from what is supported in the -[lackluster](https://nullprogram.com/blog/2023/02/11/) C standard library and -aims to be a sufficient and even superior replacement. - -`bastd`'s design goals include: -- Simplifying the build system -- Providing a full framework for Graphical and Console Applications -- Giving clear errors and debugging tools to the Programmer -- Making C a more "tolerable" language - -`bastd`'s features include: -- [ ] No header files. They cause more harm than good in my opinion, and I prefer a -single translation unit in my builds. You just include `bastd.c` at the top of -your main C file and you start writing code. -- [ ] Memory Allocators. The premier one is the -[Arena Allocator](https://www.rfleury.com/p/untangling-lifetimes-the-arena-allocator), -which acts as our generic allocator in place of `malloc`, providing a unfirom -interface that simplifies memory management and groups allocations together -into clearly defined lifetimes -- [ ] A Length-Based String Type + String Builder. This replaces whatever the hell -`string.h` was meant to be, by introducing a length to strings, which allows -for simple slicing of the string and modification. -- [ ] Generic data-structures. The premier one is the -[Dynamic Array](https://dylanfalconer.com/articles/dynamic-arrays-in-c), which -brings the power of C++'s `std::vector` to C -- [ ] A simple I/O system that supports both standard input/output and file -input/output. It's built on a basic buffer type that you can flush to output -data. -- [ ] A logging system built ontop of this I/O system. You can either log out to -a file or to stdout. -- [ ] A simple GUI initialization system that gets you an Direct3D 11 or OpenGL -window with immediate-mode input. -- [ ] A basic 2D renderer built ontop of this graphics initializater, that can -draw shapes and textures to the screen. The shaders used can be customized for -added effect. It features a simple camera system to seperate which parts of -the screen use which shaders and viewports. -- [ ] An audio library. Windows implementation uses WASAPI, Linux uses ALSA. - ---- -- [x] implemented -- [ ] planned ---- - -The only currently supported platform is Windows, though once the APIs are -finished, a Linux and FreeBSD port are planned. - -## Why C? -Cuz I like C. It's the closest "high-level" language to assembly, so I have -maximum control. It is *really simple*, which in some ways is to its detriment. -Almost all OS libraries, Graphics libraries, Audio libraries, etc. are written -in C. - -C is great. Don't let anyone tell you it's not. - -## LICENSE +# Bravely Arranged Standard Library +`bastd` is my own custom standard library for C, made to aid in the development +of games, 3D software, 2D development tools and more. + +It strays heavily from what is supported in the +[lackluster](https://nullprogram.com/blog/2023/02/11/) C standard library and +aims to be a sufficient and even superior replacement. + +`bastd`'s design goals include: +- Simplifying the build system +- Providing a full framework for Graphical and Console Applications +- Giving clear errors and debugging tools to the Programmer +- Making C a more "tolerable" language + +`bastd`'s features include: +- [ ] No header files. They cause more harm than good in my opinion, and I prefer a +single translation unit in my builds. You just include `bastd.c` at the top of +your main C file and you start writing code. +- [ ] Memory Allocators. The premier one is the +[Arena Allocator](https://www.rfleury.com/p/untangling-lifetimes-the-arena-allocator), +which acts as our generic allocator in place of `malloc`, providing a unfirom +interface that simplifies memory management and groups allocations together +into clearly defined lifetimes +- [ ] A Length-Based String Type + String Builder. This replaces whatever the hell +`string.h` was meant to be, by introducing a length to strings, which allows +for simple slicing of the string and modification. +- [ ] Generic data-structures. The premier one is the +[Dynamic Array](https://dylanfalconer.com/articles/dynamic-arrays-in-c), which +brings the power of C++'s `std::vector` to C +- [ ] A simple I/O system that supports both standard input/output and file +input/output. It's built on a basic buffer type that you can flush to output +data. +- [ ] A logging system built ontop of this I/O system. You can either log out to +a file or to stdout. +- [ ] A simple GUI initialization system that gets you an Direct3D 11 or OpenGL +window with immediate-mode input. +- [ ] A basic 2D renderer built ontop of this graphics initializater, that can +draw shapes and textures to the screen. The shaders used can be customized for +added effect. It features a simple camera system to seperate which parts of +the screen use which shaders and viewports. +- [ ] An audio library. Windows implementation uses WASAPI, Linux uses ALSA. + +--- +- [x] implemented +- [ ] planned +--- + +The only currently supported platform is Windows, though once the APIs are +finished, a Linux and FreeBSD port are planned. + +## Why C? +Cuz I like C. It's the closest "high-level" language to assembly, so I have +maximum control. It is *really simple*, which in some ways is to its detriment. +Almost all OS libraries, Graphics libraries, Audio libraries, etc. are written +in C. + +C is great. Don't let anyone tell you it's not. + +## Setup +Download the [latest release](https://github.com/midnadimple/bastd/releases/latest) +whenever you want to start a new project. + +All of your code is going to go in the root of this folder, and all the `bastd` +code has been kept in the `bastd` folder. FOr convenience, the `bastd.c` file, +which includes all other C files in the folder, stays in the top level +directory, so you can easily include the file. + +So, open `main.c` and have a scroll through it to see some example code. Then, +delete everything in the file and enter this basic skeleton: +```c +#include "bastd.c" + +CALLBACK Error +start(Slice_S8 args, Buffer stdin, Buffer stdout) +{ + return Error_success; +} +``` + +Now, you can compile the program with `build.bat` or `build.sh` to get an +executable. + +Note that since you make a new copy of `bastd` for every project, the library +is completely vendored and you can modify it however you'd like. + +## LICENSE `bastd` is licensed under the 3-clause BSD license. See [LICENSE.md](LICENSE.md). \ No newline at end of file diff --git a/bastd.c b/bastd.c new file mode 100644 index 0000000..138ad14 --- /dev/null +++ b/bastd.c @@ -0,0 +1,31 @@ +#ifndef BASTD_C +#define BASTD_C + +#include + +typedef int8_t I8; +typedef int16_t I16; +typedef int32_t I32; +typedef int64_t I64; +typedef uint8_t U8; +typedef uint16_t U16; +typedef uint32_t U32; +typedef uint64_t U64; + +typedef float F32; +typedef double F64; + +typedef U32 B32; +#define TRUE 1 +#define FALSE 0 + +#define FUNCTION static +#define GLOBAL_VAR static +#define LOCAL_PERSIST static + +#define STMNT(s) do{ s }while(0) + +// TODO: Add Linux Version +#define ASSERT(x, msg) STMNT(if (!x) __debugbreak();) + +#endif //BASTD_C \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..5897346 --- /dev/null +++ b/main.c @@ -0,0 +1,60 @@ +#include "bastd.c" + +// Single line +/* Multi +Line */ + +// "GLOBAL_VAR" macro expands to "static" +GLOBAL_VAR F32 evil_number = 666 // variables names use snake_case + +typedef struct RenderVertex RenderVertex; // typedefs come before structs +struct RenderVertex { // struct names use PascalCase + F32 x, y, z +} + +// Generic pattern. the output type will be "Slice_RenderVertex" +#define SLICE_TYPE RenderVertex +#include "slice.h" + +// all functions prefixed with "FUNCTION" macro, which expands to "static" +FUNCTION I8 // primitive types follow rust style +addTwo(I8 a, I8 b) // Function names use camelCase +{ + I8 res = a + b; + ASSERT(res != 666, "Ah! The Devil!"); // All macros, regardless of usage, are MACRO_CASE + return res; +} + +/* Functions called by other functions are prefixed with CALLBACK, which is not + defined as "static" , incase you decide to move it to a different + compilation unit +*/ +CALLBACK Error +start(Slice_S8 args, Buffer stdin, Buffer stdout) +{ + /* all functions that belong to a type or a module are styled + "m_TypeName_functionName". "m" is a lower-case shortened version of the + module's name (usually 1-3 letters). + */ + Buffer_appendS8(&stdout, args[0]); + Buffer_flush(&stdout); + + r_Window window = r_Window_create(1280, 720, "Bastd Example"); + I32 frame_number = 0; + + for (!window.should_close) { + frame_number += 1; + + // if there is no module, function is styled "TypeName_functionName" + Buffer_appendS8(&stdout, "Current frame: "); + Buffer_appendI32(&stdout, frame_number); + Buffer_appendChar(&stdout, '\n'); + Buffer_flush(&stdout); + + r_Window_tick(&window); + } + + /* Enum names are styled with PascalCase. Enum values are styled + "TypeName_value_name"*/ + return Error_success; +} \ No newline at end of file