BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
pico8.h
Go to the documentation of this file.
1
21#pragma once
22#include <vector>
23#include <span>
24#include <b8/type.h>
25#include <b8/assert.h>
26#include <b8/ppu.h>
27#include <b8/misc.h>
28#include <trace.h>
29#include <handle.h>
30#include <submath.h>
31#include <stdarg.h>
32#include <memory>
33#include <optional>
34
35/*
36 TODO: Comparison table with original PICO-8
37 TODO: Standardize naming with tget / tset
38 TODO: Implement sset(x, y, [col]) -- Sets the color of a pixel on the sprite sheet
39*/
40namespace pico8 {
41
42#define MUST(cond, errcode) \
43 do { \
44 if (!(cond)) { \
45 seterr(errcode); \
46 return; \
47 } \
48 } while(0)
49
50#define MUST_RETURN(cond, errcode, retval) \
51 do { \
52 if (!(cond)) { \
53 seterr(errcode); \
54 return (retval); \
55 } \
56 } while(0)
57
58
59
91
92 enum BgPal {
93 BG_PAL_0,
94 BG_PAL_1,
95 BG_PAL_2,
96 BG_PAL_3,
97 /* --- */
98 BG_PAL_CURRENT
99 };
100
111 enum Error {
115 NOT_INITIALIZED,
116 EMPTY_SPAN
117 };
118 void seterr( Error error );
119
120 enum MouseBtn {
121 LEFT = 1<<0,
122 RIGHT = 1<<1, // Right button (currently unused, reserved for future use)
123 };
124
125 enum Button {
132 BUTTON_MOUSE_LEFT = 6,
133 /* --- */
134 BUTTON_ANY = 0x10,
135 /* --- */
136 BUTTON_MAX
137 // Note: Unlike PICO-8, BEEP-8 is primarily designed for smartphone use.
138 // These buttons are only functional on PC platforms and referencing them is discouraged
139 // for applications intended to run on mobile devices.
140 };
141
160 void cls(Color color = BLACK);
161
185 Color color(Color color);
186
204 void rectfill(fx8 x0, fx8 y0, fx8 x1, fx8 y1, Color color = CURRENT);
205
222 void pset(fx8 x0, fx8 y0, Color color = CURRENT);
223
244 void rect(fx8 x0, fx8 y0, fx8 x1, fx8 y1, Color color = CURRENT);
245
264 void line(fx8 x0,fx8 y0,fx8 x1,fx8 y1, Color color = CURRENT );
265
283 void line(const Line& ln, Color color = CURRENT);
284
300 void poly(const Poly& pol, Color color = CURRENT);
301
322 void poly(fx8 x0, fx8 y0, fx8 x1, fx8 y1, fx8 x2, fx8 y2, Color color = CURRENT);
323
357 void spr(int n, fx8 x = fx8(0), fx8 y = fx8(0), u8 w = 1, u8 h = 1, bool flip_x = false, bool flip_y = false, u8 selpal = 0);
358
398 void sprb(u8 bank, int n, fx8 x = fx8(0), fx8 y = fx8(0), u8 w = 1, u8 h = 1, bool flip_x = false, bool flip_y = false, u8 selpal = 0);
399
468 void lsp(u8 bank,const uint8_t* srcimg);
469
507 void setpal(int palsel, const std::array<unsigned char, 16>& pidx);
508
541 void pal( Color c0 , Color c1 , u8 palsel=0 );
542
556 const Vec& camera(fx8 camera_x = fx8(0), fx8 camera_y = fx8(0));
557
573 int setz(int otz);
574
583 int getz();
584
600 int maxz();
601
621 void circ(fx8 x, fx8 y, fx8 r = fx8(4), Color col = CURRENT);
622
641 void circfill(fx8 x, fx8 y, fx8 r = fx8(4), Color col = CURRENT);
642
659 const Rect& clip(fx8 x, fx8 y, fx8 w, fx8 h);
660
675 const Rect& clip(const Rect& rc);
676
689 const Rect& clip();
690
703 struct SprCursor {
704 int x;
705 int y;
707 int z;
708
714 inline void Reset() {
715 x = y = z = 0;
716 color = CURRENT;
717 }
718
719 SprCursor(){ Reset(); }
720 };
721
745 const SprCursor& scursor(int x = 0, int y = 0, Color color = CURRENT, int z = 0);
746
785 void sprint(const std::string_view format, ...);
786
809 void sprint(int x, int y, Color color, std::string_view format, ...);
810
820 struct BgCursor {
821 int x;
822 int y;
823 BgPal pal;
824
830 inline void Reset() {
831 x = y = 0;
832 pal = BG_PAL_CURRENT;
833 }
834 BgCursor(){
835 Reset();
836 }
837 };
838
855 const BgCursor& cursor(int x = 0, int y = 0, BgPal pal = BG_PAL_CURRENT);
856
888 void print(std::string_view format, ...);
889
906 void dprint(std::string_view format, ...);
907
920 void dprintenable(bool enable);
921
942 void print(int x, int y, BgPal pal, std::string_view format, ...);
943
972 void fset(u8 sprite_index, u8 flag_index = 0xff, u8 value = 0, u8 sprite_pattern_bank = 0);
973
1002 u8 fget(u8 sprite_index, u8 flag_index = 0xff, u8 sprite_pattern_bank = 0);
1003
1004
1005 enum BgIndex{ BG_0, BG_1, BG_2, BG_3 , BG_MAX };
1006 enum BgTiles{
1007 TILES_8 = 1<<3,
1008 TILES_16 = 1<<4,
1009 TILES_32 = 1<<5,
1010 TILES_64 = 1<<6,
1011 TILES_128 = 1<<7,
1012 TILES_256 = 1<<8,
1013 TILES_512 = 1<<9,
1014 TILES_1024= 1<<10,
1015 TILES_2048= 1<<11,
1016 };
1017 using BgTilesPtr = std::shared_ptr<std::vector<b8PpuBgTile>>;
1018
1044 void mapsetup(BgTiles wtile, BgTiles htile, std::optional<BgTilesPtr> tiles = std::nullopt , u8 uwrap = B8_PPU_BG_WRAP_CLAMP, u8 vwrap = B8_PPU_BG_WRAP_CLAMP , BgIndex index = BG_0 );
1045
1066 void map(s16 upix,s16 vpix, BgIndex index = BG_0 );
1067
1073 inline void mapdraw(s16 upix,s16 vpix, BgIndex index = BG_0 ){
1074 pico8::map(upix,vpix,index);
1075 }
1076
1094 b8PpuBgTile mgett(u32 x, u32 y, BgIndex index = BG_0);
1095
1115 u16 mget(u32 x, u32 y, BgIndex index = BG_0);
1116
1148 void mset(u32 x, u32 y, u8 v, u8 bank = 0, BgIndex index = BG_0, uint8_t pal = 0);
1149
1176 void msett(u32 x,u32 y,b8PpuBgTile tile,BgIndex index = BG_0 );
1177
1189 void mcls(b8PpuBgTile tile = b8PpuBgTile{0, 0, 0, 0, 0}, BgIndex index = BG_0);
1190
1225 u32 btn(Button button=BUTTON_ANY, u8 player = 0);
1226
1250 bool btnp(Button button, u8 player=0);
1251
1272 u32 btnr(Button button, u8 player=0);
1273
1307 s32 stat(int index);
1308
1320 fx8 mousex();
1321
1333 fx8 mousey();
1334
1354 u32 mousestatus();
1355
1369 fx8 cos(fx8 rad);
1370
1384 fx8 sin(fx8 rad);
1385
1410 fx8 atan2(fx8 y, fx8 x);
1411
1425 fx8 abs(fx8 x);
1426
1440 fx8 flr(fx8 x);
1441
1454 fx8 cel(fx8 x);
1455
1469 fx8 max(fx8 x, fx8 y);
1470
1484 fx8 min(fx8 x, fx8 y);
1485
1506 fx8 mid( fx8 first, fx8 second, fx8 third );
1507
1516 inline fx8 clamp( fx8 first, fx8 second, fx8 third ){
1517 return pico8::mid(first,second,third);
1518 }
1519
1530 fx8 sgn(fx8 x);
1531
1547 fx8 sqrt(fx8 x);
1548
1568 fx8 rnd(fx8 x = fx8(1.0));
1569
1578 u32 rndu();
1579
1596 fx8 rndi(fx8 x);
1597
1614 fx8 rndf(fx8 x0, fx8 x1);
1615
1671 template <typename T>
1672 const T& rndt(std::span<const T> sp) {
1673 static const T dummy_value{};
1674 if (sp.empty()) return dummy_value;
1675 std::size_t index = qmod( std::rand() , sp.size() );
1676 return sp[index];
1677 }
1678
1696 template <typename T, std::size_t N>
1697 const T& rndt(const T (&arr)[N]) {
1698 return rndt(std::span<const T>(arr));
1699 }
1700
1714 void srand(u32 seed );
1715
1724 fx8 resw();
1725
1734 fx8 resh();
1735
1754 Color sget(u8 x, u8 y , u8 bank = 0 );
1755
1756 class ImplPico8;
1757
1790 class Pico8 {
1791 friend class ImplPico8;
1792 ImplPico8* _impl;
1793 virtual void _init(){}
1794 virtual void _update(){}
1795 virtual void _draw(){}
1796
1797 public:
1801 void run();
1802 };
1803}
Definition submath.h:125
Base class for creating PICO-8 compatible applications in C/C++.
Definition pico8.h:1790
void run()
Runs the application by calling the overridden _init(), _update(), and _draw().
Definition pico8.cpp:334
Module for managing pointers using handles.
Error
Defines error codes for drawing operations.
Definition pico8.h:111
@ NOT_DURING_DRAWING
Attempt to perform drawing outside of a valid drawing context.
Definition pico8.h:113
@ NO_ERROR
No error has occurred.
Definition pico8.h:112
@ INVALID_PARAM
An invalid parameter was passed to a function.
Definition pico8.h:114
s32 stat(int index)
Definition pico8.cpp:1098
fx8 mousey()
Retrieves the current Y position of the mouse or touch input.
Definition pico8.cpp:1117
Button
Definition pico8.h:125
@ BUTTON_X
X button ('x' key on keyboard)
Definition pico8.h:131
@ BUTTON_DOWN
Down button.
Definition pico8.h:129
@ BUTTON_UP
Up button.
Definition pico8.h:128
@ BUTTON_O
O button ('z' key on keyboard)
Definition pico8.h:130
@ BUTTON_LEFT
Left button.
Definition pico8.h:126
@ BUTTON_RIGHT
Right button.
Definition pico8.h:127
fx8 clamp(fx8 first, fx8 second, fx8 third)
Alias for mid to provide clamping functionality.
Definition pico8.h:1516
bool btnp(Button button, u8 player=0)
Definition pico8.cpp:1078
const BgCursor & cursor(int x=0, int y=0, BgPal pal=BG_PAL_CURRENT)
Sets the cursor position and palette for background-based text rendering.
Definition pico8.cpp:887
void dprint(std::string_view format,...)
Prints formatted debug output to the screen in the foremost layer.
Definition pico8.cpp:900
u32 btnr(Button button, u8 player=0)
Definition pico8.cpp:1091
const T & rndt(std::span< const T > sp)
Generates a random index and returns a reference to a randomly selected element in the given span.
Definition pico8.h:1672
fx8 flr(fx8 x)
Returns the largest integer less than or equal to the specified fixed-point number.
Definition pico8.cpp:1143
u32 mousestatus()
Retrieves the current status of the mouse buttons.
Definition pico8.cpp:1123
fx8 min(fx8 x, fx8 y)
Returns the lesser of two fixed-point numbers.
Definition pico8.cpp:1155
void dprintenable(bool enable)
Enables or disables debug text output generated by dprint().
Definition pico8.cpp:896
void sprint(const std::string_view format,...)
Prints formatted text using sprites for rendering.
Definition pico8.cpp:862
fx8 mousex()
Retrieves the current X position of the mouse or touch input.
Definition pico8.cpp:1111
u8 fget(u8 sprite_index, u8 flag_index=0xff, u8 sprite_pattern_bank=0)
Retrieves attribute flags for a sprite or background (BG) pattern.
Definition pico8.cpp:950
void fset(u8 sprite_index, u8 flag_index=0xff, u8 value=0, u8 sprite_pattern_bank=0)
Sets attribute flags for a sprite or background (BG) pattern.
Definition pico8.cpp:934
u32 btn(Button button=BUTTON_ANY, u8 player=0)
Definition pico8.cpp:1035
void map(s16 upix, s16 vpix, BgIndex index=BG_0)
Draws the configured background layer at the specified pixel offset.
Definition pico8.cpp:979
void mapdraw(s16 upix, s16 vpix, BgIndex index=BG_0)
Alias for map() function to draw the configured background layer at a specified pixel offset.
Definition pico8.h:1073
void mapsetup(BgTiles wtile, BgTiles htile, std::optional< BgTilesPtr > tiles=std::nullopt, u8 uwrap=B8_PPU_BG_WRAP_CLAMP, u8 vwrap=B8_PPU_BG_WRAP_CLAMP, BgIndex index=BG_0)
Configures a background layer for the PPU.
Definition pico8.cpp:961
void print(std::string_view format,...)
Prints formatted text on the background layer.
Definition pico8.cpp:918
fx8 mid(fx8 first, fx8 second, fx8 third)
Returns the middle (median) value of three fixed-point numbers.
Definition pico8.cpp:1159
fx8 max(fx8 x, fx8 y)
Returns the greater of two fixed-point numbers.
Definition pico8.cpp:1151
Color
Represents the color palette for drawing operations.
Definition pico8.h:72
@ BLUE
Color index 12: Blue.
Definition pico8.h:85
@ YELLOW
Color index 10: Yellow.
Definition pico8.h:83
@ LIGHT_PEACH
Color index 15: Light Peach.
Definition pico8.h:88
@ LIGHT_GREY
Color index 6: Light Grey.
Definition pico8.h:79
@ ORANGE
Color index 9: Orange.
Definition pico8.h:82
@ DARK_GREY
Color index 5: Dark Grey.
Definition pico8.h:78
@ DARK_PURPLE
Color index 2: Dark Purple.
Definition pico8.h:75
@ CURRENT
Special value representing the current color.
Definition pico8.h:89
@ DARK_BLUE
Color index 1: Dark Blue.
Definition pico8.h:74
@ LAVENDER
Color index 13: Lavender.
Definition pico8.h:86
@ GREEN
Color index 11: Green.
Definition pico8.h:84
@ BLACK
Color index 0: Black.
Definition pico8.h:73
@ WHITE
Color index 7: White.
Definition pico8.h:80
@ BROWN
Color index 4: Brown.
Definition pico8.h:77
@ RED
Color index 8: Red.
Definition pico8.h:81
@ PINK
Color index 14: Pink.
Definition pico8.h:87
@ DARK_GREEN
Color index 3: Dark Green.
Definition pico8.h:76
const SprCursor & scursor(int x=0, int y=0, Color color=CURRENT, int z=0)
Sets the cursor position, color, and depth (Z-value) for printing text using sprites.
Definition pico8.cpp:853
fx8 cel(fx8 x)
Returns the smallest integer greater than or equal to the specified fixed-point number.
Definition pico8.cpp:1147
Represents a line segment in 2D space defined by two points.
Definition submath.h:327
Represents a triangle in 2D space using three points.
Definition submath.h:355
A structure for representing rectangles with fixed-point coordinates.
Definition submath.h:372
Represents the cursor state for background-based text rendering.
Definition pico8.h:820
int y
The y-coordinate in 8x8 TILE units.
Definition pico8.h:822
BgPal pal
The palette index for text rendering (default: BG_PAL_CURRENT).
Definition pico8.h:823
int x
The x-coordinate in 8x8 TILE units.
Definition pico8.h:821
void Reset()
Resets the cursor state to its default values.
Definition pico8.h:830
Represents the cursor state for sprite-based text rendering.
Definition pico8.h:703
int y
The y-coordinate in pixels (row position).
Definition pico8.h:705
Color color
The drawing color for text (default: CURRENT).
Definition pico8.h:706
int z
The depth (Z-value) for rendering order (default: 0).
Definition pico8.h:707
int x
The x-coordinate in pixels (column position).
Definition pico8.h:704
void Reset()
Resets the cursor state to its default values.
Definition pico8.h:714
Provides mathematical functions and utilities, primarily focused on fixed-point arithmetic.
fpm::fixed< std::int32_t, std::int64_t, 8 > fx8
Alias for fixed-point type with 8 fractional bits.
Definition submath.h:56
uint32_t qmod(uint32_t x, uint32_t N)
Computes the remainder of x divided by N using an optimized method.
Definition submath.cpp:1255
Debug tracing macros for logging and monitoring program execution.