Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************/
- /******************************************************************************/
- //"sokoban_and_kit32_2025-01-31\sokoban\src\utils_misc.cpp":
- #include <include_all.hpp>
- #include <filesystem>
- namespace kit { extern size_t numAllocations; }
- static char errFmt[512];
- namespace fs = std::filesystem;
- using namespace kit;
- file_list::file_list(const char* searchPath,
- u32 signature)
- {
- if(valid) return;
- try {
- //get number of files
- files_len = 0;
- for(const auto& entry : fs::recursive_directory_iterator(searchPath))
- if(entry.is_regular_file()) ++files_len;
- //allocate space for file strings
- if(!files_len) return;
- files = (std::string**)memory::alloc2(sizeof(std::string**) * files_len);
- //add files to list
- u32 i=0;
- for(const auto& entry : fs::recursive_directory_iterator(searchPath)){
- if(entry.is_regular_file()){
- if(i >= files_len)
- throw "file count changed in the middle of querying levels directory";
- std::string pathstr = entry.path().string();
- u32 magic;
- if(File(pathstr.c_str(),"rb").read(&magic,sizeof(magic))<sizeof(magic))
- {
- snPrintf(errFmt, sizeof(errFmt),
- "failed to read file signature of level \"%s\"",
- pathstr.c_str());
- throw (const char*)errFmt;
- }
- if(signature != 0 && magic != signature) continue;
- files[i++] = new std::string(pathstr);
- ++numAllocations;
- }
- }
- //trim any unused space
- files_len = i;
- memory::realloc2(&files, sizeof(std::string**) * files_len);
- } catch(const char* errorText){
- throw errorText;
- } catch(...){ //catch(const fs::filesystem_error& _){
- throw "failed to query file list";
- }
- valid = true;
- }
- file_list::~file_list(){
- if(!valid) return;
- valid = false;
- if(files == nullptr) return;
- for(u32 i=0; i<files_len; ++i){
- NULLDELETE(files[i], std::string);
- --numAllocations;
- }
- memory::free(&files);
- files_len = 0;
- }
- void file_list::draw(s32 x, s32 y, u32 max_len,
- u32 first, u32 last)
- {
- if(last == KIT_U32_MAX) last = files_len-1;
- bool neg_pos_margin = text->neg_pos_margin;
- text->neg_pos_margin = false;
- for(u32 i=0,n=first; n<=last; ++i,++n){
- std::string* name = files[n];
- s32 end = name->length();
- for(s32 c=end-1; c>=0; --c){
- if((*name)[c] == '.'){
- end = c; break;
- }
- }
- s32 start = end-(max_len);
- s32 len = end-start;
- bool longName = start > 0;
- if(longName){
- start += 3;
- len -= 3;
- }
- start = MAX(start, 0);
- len = MAX(len, 0);
- text->draw(x, y + i*8*text->scale.y,
- "%s%s", 0, (longName) ? "..." : "",
- name->substr(start, len).c_str());
- }
- text->neg_pos_margin = neg_pos_margin;
- }
- /******************************************************************************/
- /******************************************************************************/
- //"sokoban_and_kit32_2025-01-31\sokoban\src\utils_tile.cpp":
- #include <include_all.hpp>
- using namespace kit;
- #define UV_STEP(_amount) ((1.0f/16)*(_amount))
- shape::fpoint tile_camera = {0, 0};
- s32 tile_layer = 32;
- u32 player_which = TILE_ID_PLAYER_BACK;
- Texture* tile_tex = nullptr;
- Vertex tile_verts[4];
- s32 tile_indices[6];
- void tile_init(){ //inits all but tile_tex itself
- static bool isInit = false;
- if(isInit) return;
- clrs::ABGR color = 0xFFFFFFFF;
- tile_verts[0].c = color;
- tile_verts[1].c = color;
- tile_verts[2].c = color;
- tile_verts[3].c = color;
- tile_verts[0].v = 0.0f;
- tile_verts[1].v = 0.0f;
- tile_verts[2].v = 1.0f;
- tile_verts[3].v = 1.0f;
- tile_indices[0] = 0;
- tile_indices[1] = 1;
- tile_indices[2] = 2;
- tile_indices[3] = 3;
- tile_indices[4] = 1;
- tile_indices[5] = 2;
- isInit = true;
- }
- ATTR_ALWAYS_INLINE
- static inline void _draw_tile(s32 x, s32 y, s32 z, u32 which){
- tile_verts[0].x = 16*x + tile_camera.x;
- tile_verts[0].y = 8*x + tile_camera.y;
- tile_verts[0].y -= 16*y;
- tile_verts[0].x += 16*z;
- tile_verts[0].y += 8*z;
- tile_verts[1].x = tile_verts[0].x + 32;
- tile_verts[1].y = tile_verts[0].y;
- tile_verts[2].x = tile_verts[0].x;
- tile_verts[2].y = tile_verts[0].y + 32;
- tile_verts[3].x = tile_verts[1].x;
- tile_verts[3].y = tile_verts[2].y;
- tile_verts[0].u = UV_STEP(which);
- tile_verts[1].u = tile_verts[0].u + UV_STEP(1);
- tile_verts[2].u = tile_verts[0].u;
- tile_verts[3].u = tile_verts[1].u;
- rndr->renderGeometry(tile_verts, 4, tile_tex, tile_indices, 6);
- }
- //4 to make them appear as 8x8; 8 to appear as 4x4
- #define TINY_SCALE(_normal_size) ((_normal_size)/8)
- static inline void _draw_tile_tiny(s32 x, s32 y, s32 z, u32 which){
- tile_verts[0].x = TINY_SCALE(16)*x + tile_camera.x;
- tile_verts[0].y = TINY_SCALE( 8)*x + tile_camera.y;
- tile_verts[0].y -= TINY_SCALE(16)*y;
- tile_verts[0].x += TINY_SCALE(16)*z;
- tile_verts[0].y += TINY_SCALE( 8)*z;
- tile_verts[1].x = tile_verts[0].x + TINY_SCALE(32);
- tile_verts[1].y = tile_verts[0].y;
- tile_verts[2].x = tile_verts[0].x;
- tile_verts[2].y = tile_verts[0].y + TINY_SCALE(32);
- tile_verts[3].x = tile_verts[1].x;
- tile_verts[3].y = tile_verts[2].y;
- tile_verts[0].u = UV_STEP(which);
- tile_verts[1].u = tile_verts[0].u + UV_STEP(1);
- tile_verts[2].u = tile_verts[0].u;
- tile_verts[3].u = tile_verts[1].u;
- rndr->renderGeometry(tile_verts, 4, tile_tex, tile_indices, 6);
- }
- void draw_tile(s32 x, s32 y, s32 z, u32 which, f32 alpha){
- u8 alpha_u8 = 255*alpha;
- tile_verts[0].c.a = alpha_u8;
- tile_verts[1].c.a = alpha_u8;
- tile_verts[2].c.a = alpha_u8;
- tile_verts[3].c.a = alpha_u8;
- _draw_tile(x, y, z, which);
- }
- void draw_tiles(s32 size_x, s32 size_y, s32 size_z, tile_array* tiles){
- if(tiles == nullptr) return;
- u32 i = 0;
- size_y = MIN(size_y, tile_layer);
- for(s32 y=0; y<size_y; ++y)
- for(s32 x=0; x<size_x; ++x)
- for(s32 z=0; z<size_z; ++z)
- {
- tile_array tle = tiles[i++];
- if(tle.tile){
- u8 alpha = (tle.value&0b10000000) | 0b01111111;
- tile_verts[0].c.a = alpha;
- tile_verts[1].c.a = alpha;
- tile_verts[2].c.a = alpha;
- tile_verts[3].c.a = alpha;
- _draw_tile_tiny(x, y, z, tle.tile);
- }
- if(tle.has_player){
- tile_verts[0].c.a = 255;
- tile_verts[1].c.a = 255;
- tile_verts[2].c.a = 255;
- tile_verts[3].c.a = 255;
- _draw_tile_tiny(x, y, z, player_which);
- }
- }
- }
- void draw_tiles_tiny(s32 size_x, s32 size_y, s32 size_z, tile_array* tiles){
- if(tiles == nullptr) return;
- u32 i = 0;
- size_y = MIN(size_y, tile_layer);
- for(s32 y=0; y<size_y; ++y)
- for(s32 x=0; x<size_x; ++x)
- for(s32 z=0; z<size_z; ++z)
- {
- tile_array tle = tiles[i++];
- if(tle.tile){
- u8 alpha = (tle.value&0b10000000) | 0b01111111;
- tile_verts[0].c.a = alpha;
- tile_verts[1].c.a = alpha;
- tile_verts[2].c.a = alpha;
- tile_verts[3].c.a = alpha;
- _draw_tile_tiny(x, y, z, tle.tile);
- }
- if(tle.has_player){
- tile_verts[0].c.a = 255;
- tile_verts[1].c.a = 255;
- tile_verts[2].c.a = 255;
- tile_verts[3].c.a = 255;
- _draw_tile_tiny(x, y, z, player_which);
- }
- }
- }
- /******************************************************************************/
- /******************************************************************************/
- //"sokoban_and_kit32_2025-01-31\kit32\simulator\include\include_all.hpp":
- #ifndef _INCLUDE_ALL_HPP
- #define _INCLUDE_ALL_HPP
- #include <kit/all.hpp>
- #define loghere kit_LogInfo("%s: %3i", __FILE__, __LINE__);
- /********************************* "main.cpp" *********************************/
- #define LOGI_W (256)
- #define LOGI_H (144)
- #define WIN_MUL (4)
- //(window is hidden initially, use setVisibility() to change that)
- #define WIN_TITLE "kit-32 simulator"
- #define WIN_W (LOGI_W*WIN_MUL)
- #define WIN_H (LOGI_H*WIN_MUL)
- #define WIN_FLAGS 0
- #define TEXTCOLORED_LEN 16
- extern kit::Window* wndw;
- extern kit::Renderer* rndr;
- extern kit::BFont_Texture* text;
- extern kit::BFont_Texture* textColored[TEXTCOLORED_LEN];
- kit::f64 frand (); // 0.0 -> 1.0
- kit::f64 frand2 (); //-1.0 -> 1.0
- kit::f32 frandf (); // 0.0f -> 1.0f
- kit::f32 frandf2(); //-1.0f -> 1.0f
- //only uses a single call to std::rand()
- kit::f32 frandf_b (); // 0.0f -> 1.0f
- kit::f32 frandf2_b(); //-1.0f -> 1.0f
- /****************************** "callbacks.cpp" *******************************/
- //...
- /****************************** "utils_core.cpp" ******************************/
- //...
- /****************************** "kit_32_cpu.cpp" ******************************/
- #include "kit_32_cpu.hpp"
- /******************************************************************************/
- #endif /* _INCLUDE_ALL_HPP */
- /******************************************************************************/
- /******************************************************************************/
- //"sokoban_and_kit32_2025-01-31\kit32\simulator\include\kit_32_cpu.hpp":
- #ifndef _KIT_32_CPU_HPP
- #define _KIT_32_CPU_HPP
- #include <kit/commondef.hpp>
- namespace kit {
- //must be a power of 2, minus 1
- #define ADDRESS_LIMIT (256*256*256 - 1) //for 16MiB
- #define MEMSIZE (ADDRESS_LIMIT+1)
- #define MEMSIZEh (MEMSIZE/2)
- #define ADDR(_type, _byte) \
- ( (_type *)(memory+((_byte)&ADDRESS_LIMIT)) )
- /* TWOS' COMPLEMENT COMPARISON CHEAT SHEET:
- un/signed !=: !zero
- un/signed ==: zero
- unsigned < : !carry
- unsigned > : carry && !zero
- unsigned <=: !carry || zero
- unsigned >=: carry
- signed < : negative
- signed > : !negative && !zero
- signed <=: negative || zero
- signed >=: !negative
- */
- #define CMP_ZC (!flags_zero) // !=
- #define CMP_ZS ( flags_zero) // ==
- #define CMP_NE (!flags_equal) // !=
- #define CMP_EQ ( flags_equal) // ==
- #define CMP_LT ( flags_less ) // < (sign agnostic)
- #define CMP_GT (!flags_less && !flags_equal) // > (sign agnostic)
- #define CMP_LE ( flags_less || flags_equal) // <= (sign agnostic)
- #define CMP_GE (!flags_less ) // >= (sign agnostic)
- #define CMP_LT_U (!flags_carry ) // < (unsigned)
- #define CMP_GT_U ( flags_carry && !flags_zero) // > (unsigned)
- #define CMP_LE_U (!flags_carry || flags_zero) // <= (unsigned)
- #define CMP_GE_U ( flags_carry ) // >= (unsigned)
- #define CMP_LT_S ( flags_negative ) // < (signed)
- #define CMP_GT_S (!flags_negative && !flags_zero) // > (signed)
- #define CMP_LE_S ( flags_negative || flags_zero) // <= (signed)
- #define CMP_GE_S (!flags_negative ) // >= (signed)
- enum opdata_enum {
- OPDATA_U8 = 0b000 << 5,
- OPDATA_S8 = 0b100 << 5,
- OPDATA_U16 = 0b001 << 5,
- OPDATA_S16 = 0b101 << 5,
- OPDATA_U32 = 0b010 << 5,
- OPDATA_S32 = 0b110 << 5,
- OPDATA_IMP = 0b011 << 5, //reserved for op types with implied data types
- OPDATA_F32 = 0b111 << 5,
- OPDATA_MASK = OPDATA_F32,
- };
- //operations with implied data types (if any are even applicable)
- enum optypes_implied_enum {
- OPTYPE_NOP, // 0: no operation
- OPTYPE_SRT, // 1: D = sqrtf(S)
- OPTYPE_CPU, // 2: cpuid basically, u16 implied
- OPTYPE_SYS, // 3: system call; u16 implied
- //BRANCHES HAVE IMPLIED S16 TYPE
- OPTYPE_BNC, // 4: branch to source if !negative
- OPTYPE_BNS, // 5: branch to source if negative
- OPTYPE_BZC, // 6: branch to source if !zero
- OPTYPE_BZS, // 7: branch to source if zero
- //the thing about these 4 ops (and the jump equivalents) is that
- //these are only useful if preceeded by a cmp operation.
- //the comparison between D[estination] and S[ource] refers to that cmp,
- //not the branch (otherwise the source operands would conflict!)
- OPTYPE_BLT, // 8: branch to source if D < S
- OPTYPE_BGT, // 9: branch to source if D > S
- OPTYPE_BLE, //10: branch to source if D <= S
- OPTYPE_BGE, //11: branch to source if D >= S
- OPTYPE_BNE, //12: branch to source if D != S
- OPTYPE_BEQ, //13: branch to source if D == S
- //CONDITIONAL JUMPS HAVE IMPLIED U32 TYPE
- OPTYPE_JNC, //14: jump to source if !negative
- OPTYPE_JNS, //15: jump to source if negative
- OPTYPE_JZC, //16: jump to source if !zero (AKA bne, !=)
- OPTYPE_JZS, //17: jump to source if zero (AKA beq, ==)
- OPTYPE_JLT, //18: jump to source if D < S
- OPTYPE_JGT, //19: jump to source if D > S
- OPTYPE_JLE, //20: jump to source if D <= S
- OPTYPE_JGE, //21: jump to source if D >= S
- OPTYPE_JNE, //22: jump to source if D != S
- OPTYPE_JEQ, //23: jump to source if D == S
- OPTYPE_U2B, //24: u32-to-binary-coded-decimal
- OPTYPE_B2U, //25: binary-coded-decimal-to-u32
- OPTYPE_I2F, //26: s32-to-float cast
- OPTYPE_F2I, //27: float-to-s32 cast
- OPTYPE_RTS, //28: return from subroutine
- OPTYPE_BRA, //29: branch always
- OPTYPE_RSI, //30: return from subroutine with custom increment (s16 type)
- OPTYPE_RFN, //31: return from function; rts, but pull r5 -> r1 (not r0)
- };
- //operations without implied data types
- enum optypes_normal_enum {
- //misc
- OPTYPE_BRK, // 0: (tbd)
- OPTYPE_MOV, // 1: move data
- OPTYPE_CMP, // 2: compare data
- OPTYPE_JMP, // 3: unconditional jump to source
- OPTYPE_JSR, // 4: jump to subroutine in source
- OPTYPE_SLD, // 5: load to dst from stack offset (s16 src offset, rel. to sp)
- OPTYPE_SST, // 6: store dst to stack offset (s16 src, relative to sp)
- OPTYPE_SPH, // 7: push s to stack
- OPTYPE_SPL, // 8: pull from stack into s
- //arithmetic
- OPTYPE_INC, // 9: ++D
- OPTYPE_DEC, //10: --D
- OPTYPE_ADD, //11: D += S
- OPTYPE_SUB, //12: D -= S
- OPTYPE_MUL, //13: D *= S
- OPTYPE_DIV, //14: D /= S
- OPTYPE_MOD, //15: D %= S
- OPTYPE_NEG, //16: D = -S
- //bitwise (not available for f32 types)
- OPTYPE_NOT, //17: bitwise not (~)
- OPTYPE_NND, //18: bitwise nand (& ~)
- OPTYPE_AND, //19: bitwise and (&)
- OPTYPE_NOR, //20: bitwise nor (| ~)
- OPTYPE_IOR, //21: bitwise inclusive or (|)
- OPTYPE_XOR, //22: bitwise exclusive or (^)
- OPTYPE_SHL, //23: shift left (<<)
- OPTYPE_SHR, //24: shift right (>>)
- OPTYPE_ROL, //25: rotate left (technically redundant)
- OPTYPE_ROR, //26: rotate right
- OPTYPE_EXT, //27: sign extend; 8-bit to 16-bit, or 16-bit to 32-bit (no floats!)
- OPTYPE_JFN, //28: jump to function; jsr, but push r1 -> r5 (not r0)
- //(there's space for 3 more operation types here)
- };
- #define OPCODE(_op_data, _op_type) \
- ( OPDATA_##_op_data | OPTYPE_##_op_type)
- //addressing modes
- enum opaddr_enum {
- //general-purpose registers
- OPADDR_GP0 = 0,
- OPADDR_GP1 = 1,
- OPADDR_GP2 = 2,
- OPADDR_GP3 = 3,
- OPADDR_GP4 = 4,
- OPADDR_GP5 = 5,
- //stack pointer
- OPADDR_SP = 6,
- //immediate value (source only)
- OPADDR_IMM = 7,
- //flags register (destination only)
- OPADDR_FLAGS = 7,
- };
- enum execute_result_enum {
- EXERES_SUCCESS = 0, //no error
- EXERES_BRK = 1, //brk was invoked
- EXERES_NOP = 2, //nop was invoked
- EXERES_ILLEGAL = 3, //attempted to execute an illegal opcode
- };
- /*
- |FEDCBA9876543210|FEDCBA98-76543210|
- /----------------------------------\
- |VVVVVVVVVVVVVVVV|dDDDsSSS-NTTOOOOO|
- \----------------------------------/
- V: if applicable, and T <= 1, operand value will be stored here
- instead of in the next word (32-bits)
- d: 'is dst value indirect?', or 'dereference u32 value in dst?'
- D: destination register; 0-5 = general purpose, 6 = stack ptr, 7 = flags
- s: 'is src value indirect?', or 'dereference u32 value in src?'
- S: source register; 0-5 = general purpose, 6 = stack ptr, 7 = immediate value
- (immediate value isn't actually a cpu register,
- rather it's a value within the full opcode itself)
- N: 'data is signed?'
- T: data type; 0,1,2,3 = char,short,int,float
- O: operation type
- behavior of D and S being equal is undefined unless explicitly supported
- (this can be checked with the CPU operation!)
- dereferencing values not of type u32 is undefined
- behavior of misaligned instructions (not a multiple of 4 bytes) is undefined
- if N and TT make 011, an unsigned float
- (which isn't standard, if it exists at all),
- OOOOO will be a separate line operation type category
- */
- union ins32 { //instruction; 4B
- u32 all;
- struct {
- //opcode; 1B
- union {
- u8 op;
- struct {
- u8 op_type : 5;
- u8 op_dtype : 2; //operand data type; 0,1,2,3 = char,short,int,float
- u8 op_dsign : 1; //'is data type signed?'
- };
- };
- //source and destination; 1B
- struct {
- u8 src : 3;
- u8 src_ind : 1;
- u8 dst : 3;
- u8 dst_ind : 1;
- };
- //operand value (if op_data <= 1); 2B
- union {
- u8 v_u8;
- s8 v_s8;
- u16 v_u16;
- s16 v_s16;
- };
- };
- };
- //"kit::" explicitly used in type to make reading it a bit less confusing
- union reg32 { //register
- kit::u8 u8;
- kit::s8 s8;
- kit::u16 u16;
- kit::s16 s16;
- kit::u32 u32;
- kit::s32 s32;
- kit::f32 f32;
- ins32 ins; //may or may not use this
- };
- struct cpu32 { //cpu state; 56B
- union {
- reg32 regs[8];
- struct {
- reg32 regs_gp[6];
- reg32 regs_sp;
- union {
- reg32 regs_flags;
- struct {
- bool flags_less : 1; //these two are only set by cmp
- bool flags_equal : 1; //
- bool _flags_padding0 : 2;
- bool flags_zero : 1;
- bool _flags_padding1 : 2;
- bool flags_negative : 1;
- };
- };
- };
- };
- u32 regs_pc, _;
- u64 ticksStart = 0;
- u8* memory = nullptr;
- //(memory should be padded by at least 8B beyond its
- //max address value to prevent any possible overstepping!)
- s32 execute(); //advances program counter automatically
- void cpuid(u16 type);
- void syscall(u16 type);
- };
- enum syscall_enum {
- SYSCALL_TAN, // 0: r0.f32 = tanf(r0.f32)
- SYSCALL_COSSIN, // 1: r0.f32 = cosf(r0.f32), r1.f32 = sinf(r1.f32)
- SYSCALL_COS, // 2: r0.f32 = cosf(r0.f32)
- SYSCALL_SIN, // 3: r1.f32 = sinf(r1.f32)
- SYSCALL_MEMSET, // 4: memory::set ((void*)r0.u32, r1.u8 , r2.u32)
- SYSCALL_MEMCPY, // 5: memory::copy((void*)r0.u32, (void*)r1.u32, r2.u32)
- SYSCALL_SETDRAWCOLOR, // 6: rndr->setDrawColor(r0.u32)
- SYSCALL_DRAWPOINT_S32, // 7: rndr->drawPoint(r0.s32, r1.s32)
- SYSCALL_DRAWPOINT_F32, // 8: rndr->drawPoint(r0.f32, r1.f32)
- SYSCALL_DRAWLINE_S32, // 9: rndr->drawLine(r0.s32, r1.s32, r2.s32, r3.s32)
- SYSCALL_DRAWLINE_F32, //10: rndr->drawLine(r0.f32, r1.f32, r2.f32, r3.f32)
- SYSCALL_DRAWRECT_S32, //11: rndr->drawRects(&{r0.s32, r1.s32, r2.s32, r3.s32}, 1)
- SYSCALL_DRAWRECT_F32, //12: rndr->drawRects(&{r0.f32, r1.f32, r2.f32, r3.f32}, 1)
- SYSCALL_FILLRECT_S32, //13: rndr->fillRects(&{r0.s32, r1.s32, r2.s32, r3.s32}, 1)
- SYSCALL_FILLRECT_F32, //14: rndr->fillRects(&{r0.f32, r1.f32, r2.f32, r3.f32}, 1)
- SYSCALL_DRAWPOINTS_S32, //15: rndr->drawPoints((shape::point *)r0.u32, r1.u32)
- SYSCALL_DRAWPOINTS_F32, //16: rndr->drawPoints((shape::fpoint*)r0.u32, r1.u32)
- SYSCALL_DRAWLINES_S32, //17: rndr->drawLines((shape::point *)r0.u32, r1.u32)
- SYSCALL_DRAWLINES_F32, //18: rndr->drawLines((shape::fpoint*)r0.u32, r1.u32)
- SYSCALL_DRAWRECTS_S32, //19: rndr->drawRects((shape::rect *)r0.u32, r1.u32)
- SYSCALL_DRAWRECTS_F32, //20: rndr->drawRects((shape::frect*)r0.u32, r1.u32)
- SYSCALL_FILLRECTS_S32, //21: rndr->fillRects((shape::rect *)r0.u32, r1.u32)
- SYSCALL_FILLRECTS_F32, //22: rndr->fillRects((shape::frect*)r0.u32, r1.u32)
- SYSCALL_CLEAR, //23: rndr->clear()
- SYSCALL_PRESENT, //24: rndr->present(), then wait for next frame
- SYSCALL_RAND_U8, //25: r0.u8 = (u8 )(frandf_b()*KIT_U8_MAX )
- SYSCALL_RAND_U16, //26: r0.u16 = (u16)(frandf_b()*KIT_U16_MAX)
- SYSCALL_RAND_U32, //27: r0.u32 = (u32)(frandf()*KIT_U32_MAX)
- SYSCALL_RAND_F32, //28: r0.f32 = frandf()
- //29: r0.u32 = key32 if a key event was in the queue, or 0 if queue is empty
- SYSCALL_GETKEY,
- //30: text->draw(r0.s32, r1.s32, r2.u32, r3.u32&KIT_S32_MAX)
- SYSCALL_DRAWTEXT,
- };
- union key32 { //4B
- u32 value = 0;
- struct {
- u32 vkey : 10; //MSb is the _KIT_PKEY_MASK of that virtual keycode
- u32 pkey : 9; //physical keycode
- u32 keymods : 11; //Event_Key_Mod without the 4 unused bits, or scroll lock
- u32 repeat : 1; //'is this event the result of holding a key down?'
- u32 pressed : 1; //'is key down event?' (key up otherwise)
- };
- };
- static inline key32 convert_key(Event_Key evt){
- key32 result;
- result.vkey = evt.vkey&511; //get first 9 bits of vkey
- result.vkey |= (evt.vkey&_KIT_PKEY_MASK) ? 512 : 0; //set bit 9 if(pkey_mask)
- result.pkey = evt.pkey&511; //the &511 *should* be redundant here
- #define keymod evt.sym.kmod
- result.keymods = 0;
- result.keymods |= keymod.mode ; result.keymods <<= 1;
- result.keymods |= keymod.caps ; result.keymods <<= 1;
- result.keymods |= keymod.num ; result.keymods <<= 1;
- result.keymods |= keymod.rgui ; result.keymods <<= 1;
- result.keymods |= keymod.lgui ; result.keymods <<= 1;
- result.keymods |= keymod.ralt ; result.keymods <<= 1;
- result.keymods |= keymod.lalt ; result.keymods <<= 1;
- result.keymods |= keymod.rctrl ; result.keymods <<= 1;
- result.keymods |= keymod.lctrl ; result.keymods <<= 1;
- result.keymods |= keymod.rshift; result.keymods <<= 1;
- result.keymods |= keymod.lshift;
- result.repeat = evt.repeat;
- result.pressed = evt.pressed;
- return result;
- }
- }; /* namespace kit */
- #endif /* _KIT_32_CPU_HPP */
- /******************************************************************************/
- /******************************************************************************/
- //"sokoban_and_kit32_2025-01-31\sokoban\include\include_all.hpp":
- #ifndef _INCLUDE_ALL_HPP
- #define _INCLUDE_ALL_HPP
- #include <kit/all.hpp>
- #include <kit/Xmp.hpp>
- #define loghere kit_LogInfo("%s: %3i", __FILE__, __LINE__);
- #define countof(_thing, _type) (sizeof(_thing)/sizeof(_type))
- /********************************* "main.cpp" *********************************/
- #define GAME_NAME "IsoZooid"
- #define RELEASE_VERSION 1
- #define RELEASE_VERSION_STR "1"
- //(window is hidden initially, use setVisibility() to change that)
- #define WIN_TITLE \
- GAME_NAME " r" RELEASE_VERSION_STR \
- " (F11 or ALT+Return to enter fullscreen mode)"
- #define WIN_W 1280
- #define WIN_H 720
- #define WIN_FLAGS WINFLAG_RESIZABLE
- #define FULLSCREEN_MODE 2
- //logical width and height
- #define LOGI_W (WIN_W/2) //=640
- #define LOGI_H (WIN_H/2) //=360
- #define SFXPLAY(_name, _vol, _speed) \
- sfx->play(*sfxData[_name], (_vol), (_vol), (_speed))
- #define SFX_TRACKS 64
- enum sfxData_names {
- SFXNAME_BLIP1,
- SFXNAME_CHK1,
- SFXDATA_LEN,
- };
- #define SETTINGS_VERSION 3
- #define SETTINGS_PATH "settings.bin"
- struct settings_t {
- kit::u32 version;
- kit::f32 music_vol;
- kit::f32 sfx_vol;
- kit::s32 selected; //selected level index
- bool fullscreen;
- struct {
- } binds;
- };
- extern settings_t settings;
- extern kit::AudioData* sfxData[SFXDATA_LEN];
- extern kit::AudioDevice* audio;
- extern kit::Window* wndw;
- extern kit::Renderer* rndr;
- #define TEXTCOLORED_LEN 16
- extern kit::BFont_Texture* text;
- extern kit::BFont_Texture* textColored[TEXTCOLORED_LEN];
- #define GRAY(_hexvalue) 0xFF##_hexvalue##_hexvalue##_hexvalue
- //if fade delta is 1/20th, then it will take 2/20ths of a second
- //for a new song to fully fade-in (unless index is <0 of course)
- void music_play(kit::s32 index = -1); //<0 to stop music
- void music_setFadeDelta(kit::f32 fadeTimeSeconds = 0.5f);
- kit::f64 frand (); // 0.0f -> 1.0f
- kit::f64 frand2 (); //-1.0f -> 1.0f
- kit::f32 frandf (); // 0.0f -> 1.0f
- kit::f32 frandf2(); //-1.0f -> 1.0f
- void settings_load();
- void settings_save();
- kit::s32 default_event_handler(kit::Event& evt);
- kit::u32 unit_to_ryg(kit::f32 unit); //unit should be between 0.0f and 1.0f
- kit::u32 hsv2rgb(kit::f32 H, kit::f32 S, kit::f32 V); //stolen with love
- template <typename T>
- static inline T closer_to_0(T x,T y){ return (x>=0) ? MAX(x-y,0) : MIN(x+y,0); }
- /****************************** "callbacks.cpp" *******************************/
- #define MUSIC_INDEX_musicbox1_intro 0
- #define MUSIC_INDEX_musicbox1 1
- extern kit::SoundEngine* sfx;
- extern kit::s32 music_index;
- /****************************** "utils_tile.cpp" ******************************/
- #define TILE_ID_NULL 0
- #define TILE_ID_BOXSMALL 1
- #define TILE_ID_BOXBIG 2
- #define TILE_ID_GOAL 3
- #define TILE_ID_BLOCK_R 4
- #define TILE_ID_BLOCK_G 5
- #define TILE_ID_BLOCK_B 6
- #define TILE_ID_BLOCK_Y 7 //more orange than yellow, but whatever
- //...
- #define TILE_ID_PLAYER_BACK 12
- #define TILE_ID_PLAYER_RIGHT 13
- #define TILE_ID_PLAYER_LEFT 14
- #define TILE_ID_HIGHLIGHT 15 //for level editor
- union tile_run { //compressed tile data
- kit::u16 value;
- struct { kit::u16 len:11, tile:4, opaque:1; };
- tile_run(kit::u16 _value) : value(_value) {}
- };
- union tile_array { //uncompressed tile data
- kit::u8 value;
- struct { kit::u8 tile:4, arg:2, has_player:1, opaque:1; };
- tile_array(kit::u8 _value) : value(_value) {}
- };
- extern kit::shape::fpoint tile_camera;
- extern kit::s32 tile_layer; //the maximum layer to draw to
- //index in tiles array that corresponds to player's sprite
- extern kit::u32 player_which;
- void draw_tile(kit::s32 x, kit::s32 y, kit::s32 z,
- kit::u32 which, kit::f32 alpha = 1.0f);
- void draw_tiles(kit::s32 size_x, kit::s32 size_y, kit::s32 size_z,
- tile_array* tiles);
- void draw_tiles_tiny(kit::s32 size_x, kit::s32 size_y, kit::s32 size_z,
- tile_array* tiles);
- static inline bool isInFront(kit::shape::point3d p_a, kit::shape::point3d p_b){
- return (p_a.x < p_b.x) || (p_a.y < p_b.y) || (p_a.z > p_b.z);
- }
- /****************************** "user_main.cpp" *******************************/
- #define CTX_MAIN_MENU 0
- #define CTX_LVL_SELECT 1
- #define CTX_LVL_EDITOR 2
- #define CTX_PLAYING 3
- #define CTX_REBINDING 4
- struct ctx_callbacks {
- kit::s32 (*handle_events)() = nullptr;
- void (*draw_things )() = nullptr;
- };
- extern kit::u32 ctx_which;
- extern kit::shape::point cursor_pos;
- extern kit::shape::fpoint cursor_norm;
- extern bool cursor_scroll;
- extern bool cursor_snow;
- extern kit::u64 frame_num;
- void clear_snowflakes();
- /**************************** "ctx_main_menu.cpp" *****************************/
- #define ICON_MENU_LEN 8 //used for uv coordinate conversions
- extern kit::Texture* icon_menu;
- extern kit::Texture* icon_menu_inv;
- void draw_icon_rgb(kit::f32 x, kit::f32 y, kit::u32 which,
- kit::clrs::ABGR color, bool inverted = false);
- void draw_icon_hsv(kit::f32 x, kit::f32 y, kit::u32 which,
- kit::f32 hue, bool inverted = false);
- kit::s32 ctx_main_menu_evt(); void ctx_main_menu_draw();
- static inline bool point_in_rect(kit::shape::point& pnt,
- kit::shape::rect& rct)
- {
- return (pnt.x >= rct.x) && (pnt.x < (rct.x+rct.w)) &&
- (pnt.y >= rct.y) && (pnt.y < (rct.y+rct.h));
- }
- /**************************** "ctx_lvl_select.cpp" ****************************/
- kit::s32 ctx_lvl_select_evt(); void ctx_lvl_select_draw();
- /****************************** "utils_misc.cpp" ******************************/
- #include <string>
- #define LEVEL_FILE_MAGIC 0x4C5A496B // = "kIZL" (no null terminator)
- struct file_list { //16B
- std::string** files;
- kit::u32 files_len;
- kit::u8 _padding8[3];
- bool valid = false;
- file_list(const char* searchPath = ".",
- kit::u32 signature = LEVEL_FILE_MAGIC);
- ~file_list();
- void draw(kit::s32 x, kit::s32 y, kit::u32 max_len,
- kit::u32 first = 0, kit::u32 last = KIT_U32_MAX);
- const std::string& operator[](int idx) const {
- if(!valid)
- throw "file_list::operator[](): tried to index invalid list";
- if(idx < -(int)files_len || idx >= (int)files_len)
- throw "file_list::operator[](): index out of bounds";
- return (idx>=0) ? *files[idx] : *files[files_len+idx /*(idx is negative)*/];
- }
- };
- struct level_file { //32B + (64*<# of signs>)B
- kit::u32 magic; //file signature = LEVEL_FILE_MAGIC
- kit::u16 release; //'what build number was the level made in/for?'
- kit::u16 headerSize; //must be >= 32
- kit::u32 dataSize; //not counting signs
- kit::u16 dimsX;
- kit::u16 dimsY;
- kit::u16 dimsZ;
- kit::u16 playerX;
- kit::u16 playerY;
- kit::u16 playerZ;
- //inside the file, data is just the file offset to the tile data.
- //when loaded, data will be converted to an actual valid pointer
- //nesting order is: x,z,y
- tile_run* data; //compressed tile data; run length encoded
- char signs[4][64]; //maximum of 4 signs
- };
- /**************************** "ctx_lvl_editor.cpp" ****************************/
- kit::s32 ctx_lvl_editor_evt(); void ctx_lvl_editor_draw();
- /******************************************************************************/
- #endif /* _INCLUDE_ALL_HPP */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement