Advertisement
Kitomas

VGA DOS stuff as of 2025-05-22

May 22nd, 2025
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.26 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"VGA_stuff_2025-05-22\MAIN.C":
  4. #include "C:/home/kit_all.h"
  5. #include <hardware.h>
  6. #include <kit_std.h>
  7.  
  8. #define delayMS delay
  9.  
  10.  
  11.  
  12. #ifdef _DEBUG
  13.     #define PRINT_SEED \
  14.         printf("SEED = 0x%04X", seed>>16); \
  15.         printf("%04X\n", seed&0xFFFF);
  16.  
  17. #else
  18.     #define PRINT_SEED
  19.  
  20. #endif
  21.  
  22.  
  23.  
  24. // From "SRC/MISC.C":
  25. void generatePalette(void);
  26. bool generateMaze(s8* map, s16 w, s16 h, s8 fillValue);
  27. Color24 hsv2rgb(f32 h, f32 s, f32 v);
  28.  
  29.  
  30.  
  31.  
  32.  
  33. /*
  34. void draw_pointf(f32 xf, f32 yf,
  35.                                 byte color)
  36. {
  37.     s16 x = SCREEN_W/2;
  38.     s16 y = SCREEN_H/2;
  39.  
  40.     if( xf<-1.0f | xf>1.0f |
  41.             yf<-1.0f | yf>1.0f )
  42.     {
  43.         return;
  44.     }
  45.  
  46.     x += (s16)(xf * ((xf>=0.0f) ? (SCREEN_W/2-1) : (SCREEN_W/2)));
  47.     y += (s16)(yf * ((yf>=0.0f) ? (SCREEN_H/2-1) : (SCREEN_H/2)));
  48.  
  49.     VGA[x + y*SCREEN_W] = color;
  50.  
  51. }
  52. */
  53.  
  54.  
  55.  
  56.  
  57.  
  58. static s32 x,y,i;
  59. static Color24 prev_colors[256];
  60.  
  61. #define MAPMAX_W 31
  62. #define MAPMAX_H 15
  63. static s8 map[MAPMAX_W*MAPMAX_H];
  64.  
  65. static byte tiles[128];
  66.  
  67. static KIT_fpoint3d pos;
  68.  
  69.  
  70.  
  71. int main(/*int argc, char* argv[]*/)
  72. {
  73.     u32 seed;
  74.  
  75.     byte prevVideoMode;
  76.  
  77.  
  78.  
  79.     seed = readPIT_Ch0();
  80.     PRINT_SEED
  81.  
  82.     prevVideoMode = setVideoMode(0x13);
  83.  
  84.     for(i=0; i<256; ++i)
  85.         prev_colors[i] = getPaletteColor(i);
  86.  
  87.     generatePalette();
  88.  
  89.     delayMS(seed%234); // A bit hacky
  90.  
  91.     seed <<= 16;
  92.     seed |= readPIT_Ch0();
  93.     PRINT_SEED
  94.  
  95.     KIT_srand((long int)seed);
  96.  
  97.     /*******/
  98.  
  99.     generateMaze(map, MAPMAX_W, MAPMAX_H, 1);
  100.  
  101.  
  102.  
  103.     pos.x = 1.5f;
  104.     pos.y = 1.5f;
  105.     pos.z = 0.0f;
  106.     tiles[0] = 0x40;
  107.  
  108.     #define ITMAX 8
  109.     for(i=0; i<ITMAX; ++i){
  110.         draw_raycast_scene(map, MAPMAX_W, MAPMAX_H,
  111.                                              pos, tiles,
  112.                                              0x20, 0x80,
  113.                                              50, 90.0f);
  114.  
  115.         pos.z += (1.0f/ITMAX)*M_2PIf;
  116.  
  117.         delayMS(1000);
  118.  
  119.     }
  120.  
  121.  
  122.  
  123.     /*******/
  124.  
  125.     for(i=0; i<256; ++i)
  126.         setPaletteColor(i, prev_colors[i].v);
  127.  
  128.     setVideoMode(prevVideoMode);
  129.  
  130.  
  131.  
  132.     #ifdef _DEBUG
  133.         //generateMaze(map, MAPMAX_W, MAPMAX_H, 1);
  134.  
  135.         for(i=0; i<(MAPMAX_W*MAPMAX_H); ++i){
  136.             printf("%c", (map[i])?219:'.');
  137.  
  138.             if((i%MAPMAX_W) == (MAPMAX_W-1))
  139.                 printf("\n");
  140.  
  141.         }
  142.  
  143.         PRINT_SEED
  144.     #endif
  145.  
  146.     return 0;
  147.  
  148. }
  149. /******************************************************************************/
  150. /******************************************************************************/
  151. //"VGA_stuff_2025-05-22\SRC\HRDWRSRC.C":
  152. #include <draw.h>
  153.  
  154.  
  155.  
  156. byte far * const VGA = VGA_FAR_PTR;
  157.  
  158.  
  159.  
  160.  
  161.  
  162. // Returns the previous video mode
  163. byte setVideoMode(byte new_mode)
  164. {
  165.     union REGS regs;
  166.     byte prevVideoMode;
  167.  
  168.  
  169.  
  170.     regs.h.ah = 0x0F; // Get current mode
  171.     int86(0x10, &regs, &regs);
  172.     prevVideoMode = regs.h.al;
  173.  
  174.     regs.h.ah = 0x00; // Set current mode
  175.     regs.h.al = new_mode;
  176.     int86(0x10, &regs, &regs);
  177.  
  178.     return prevVideoMode;
  179.  
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. Color24 getPaletteColor(byte which)
  187. {
  188.     Color24 rgb;
  189.  
  190.     outp(0x3C7, which); // Set read index
  191.  
  192.     rgb.v  = 0;
  193.     rgb.v |= ((u32)inp(0x3C9)) << 16; // R
  194.     rgb.v |= ((u32)inp(0x3C9)) <<  8; // G
  195.     rgb.v |= ((u32)inp(0x3C9)) <<  0; // B
  196.  
  197.     return rgb;
  198.  
  199. }
  200.  
  201.  
  202.  
  203. void setPaletteColor(byte which, u32 rgb)
  204. {
  205.     outp(0x3C6, 0xFF);
  206.     outp(0x3C8, which); // Set write index
  207.  
  208.     outp(0x3C9, (rgb>>16)&0xFF); // R
  209.     outp(0x3C9, (rgb>> 8)&0xFF); // G
  210.     outp(0x3C9, (rgb>> 0)&0xFF); // B
  211.  
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218. u16 readPIT_Ch0(void)
  219. {
  220.     u16 v;
  221.  
  222.     // Latch current count value for channel 0
  223.     outp(0x43, 0x00); // 00h = latch command for channel 0
  224.  
  225.     // Read low and high byte from channel 0's data port
  226.     v  = inp(0x40);
  227.     v |= ((u16)inp(0x40))<<8;
  228.  
  229.     return v;
  230.  
  231. }
  232. /******************************************************************************/
  233. /******************************************************************************/
  234. //"VGA_stuff_2025-05-22\SRC\KIT_STDS.C":
  235. #include "C:/home/kit_all.h"
  236.  
  237.  
  238.  
  239.  
  240.  
  241. #define BITSIZE_S31 (sizeof(s32)*8 - 1)
  242.  
  243. long int KIT_labs(long int n)
  244. {
  245.     const long int mask = n>>BITSIZE_S31;
  246.     return (n+mask) ^ mask; // Branchless :D
  247.  
  248. }
  249.  
  250.  
  251.  
  252. float KIT_fabsf(float _x)
  253. {
  254.     union {
  255.         f32 f;
  256.         u32 i;
  257.     } x;
  258.  
  259.     x.f = _x;
  260.     x.i &= 0x7FFFFFFF;
  261.  
  262.     return x.f;
  263.  
  264. }
  265.  
  266.  
  267.  
  268.  
  269.  
  270. void* KIT_memset(void* dst, int val, size_t len)
  271. {
  272.     u8  val8,  *dst8;
  273.     u32 val32, *dst32;
  274.  
  275.     size_t remainder;
  276.  
  277.     if(!len) return dst;
  278.  
  279.  
  280.  
  281.     dst8 = (u8*)dst;
  282.     val8 = (u8 )val;
  283.  
  284.     remainder = len%sizeof(u32);
  285.     len -= remainder;
  286.  
  287.     while((remainder--) > 0)
  288.         *(dst8++) = val8;
  289.  
  290.  
  291.  
  292.     dst32 = (u32*)dst8;
  293.     val32 = (u32 )val8;
  294.  
  295.     val32 |= val32<< 8;
  296.     val32 |= val32<<16;
  297.  
  298.     len /= sizeof(u32);
  299.  
  300.     while((len--) > 0)
  301.         *(dst32++) = val32;
  302.  
  303.  
  304.  
  305.     return dst;
  306.  
  307. }
  308.  
  309.  
  310.  
  311.  
  312.  
  313. // (Uses a default seed)
  314. static u32 rand_state = 0x37C0FFEE;
  315.  
  316.  
  317.  
  318. void KIT_srand(long int seed)
  319. {
  320.     // Must be nonzero, otherwise
  321.     // the xorshift doesn't work
  322.     if(seed == 0) seed = 1;
  323.  
  324.     rand_state = (u32)seed;
  325.  
  326. }
  327.  
  328.  
  329.  
  330. long int KIT_lrand(void)
  331. {
  332.     s32 LSb = rand_state&1;
  333.     rand_state >>= 1;
  334.  
  335.     // Taps: 31, 21, 1, 0
  336.     rand_state ^= 0x80200003L & (-LSb);
  337.  
  338.     return (long int)rand_state;
  339.  
  340. }
  341.  
  342.  
  343.  
  344.  
  345.  
  346. #define my_fmodf(x, y) \
  347.     ( (x) - ((long int)((x)/(y)) * (y)) )
  348.  
  349. float KIT_fmodf(float x, float y)
  350. {
  351.     return my_fmodf(x, y);
  352.  
  353. }
  354.  
  355.  
  356.  
  357. // Uses Bhaskara I's sine approx. formula
  358. // (The rest is basically just domain manipulation)
  359. float KIT_sinf(float x)
  360. {
  361.     float result;
  362.     bool negative = x<0.0f;
  363.  
  364.     x = my_fmodf(x, M_2PIf);
  365.  
  366.     negative ^= x>=M_PIf;
  367.     if(x >= M_PIf) x -= M_PIf; // x %= pi
  368.  
  369.     result  = 16.0f*x * (M_PIf-x);
  370.     result /= 5.0f*M_PIf*M_PIf - result*0.25f;
  371.  
  372.     return (negative) ? -result : result;
  373.  
  374. }
  375. /******************************************************************************/
  376. /******************************************************************************/
  377. //"VGA_stuff_2025-05-22\SRC\MISC.C":
  378. #include "C:/home/kit_all.h"
  379. #include <hardware.h>
  380. #include <kit_std.h>
  381.  
  382. #define MAZE_START_X 1
  383. #define MAZE_START_Y 1
  384.  
  385.  
  386.  
  387.  
  388.  
  389. void generatePalette(void)
  390. {
  391.     s32 i;
  392.     Color8  clr_idx;
  393.     Color24 clr_val;
  394.  
  395.     // Generate custom VGA palette
  396.     //
  397.     // (Channels have a depth of 6-bits, not 8-bits!)
  398.     for(i=0; i<256; ++i){
  399.         clr_idx.v = i;
  400.         #define conv_b(_c) ((_c)*21)
  401.         #define conv_rg(_c) ((u8)( (63.0f*((f32)(_c)/7)) + 0.5f ))
  402.         clr_val.c.b = conv_b(clr_idx.c.b);
  403.         clr_val.c.g = conv_rg(clr_idx.c.g);
  404.         clr_val.c.r = conv_rg(clr_idx.c.r);
  405.         setPaletteColor(i, clr_val.v);
  406.     }
  407.  
  408.     // Grayscale portion of palette
  409.     for(i=0; i<16; ++i){
  410.         clr_val.v  = i<<2;
  411.         clr_val.v |= clr_val.v<< 8;
  412.         clr_val.v |= clr_val.v<<16;
  413.         setPaletteColor(i<<4, clr_val.v);
  414.     }
  415.  
  416. }
  417.  
  418.  
  419.  
  420.  
  421.  
  422. // Direction vectors (up, right, down, left)
  423. const s16 d_x[] = { 0, 1, 0,-1};
  424. const s16 d_y[] = {-1, 0, 1, 0};
  425.  
  426. static void carve(s16 x, s16 y,
  427.                                     s8* map, s16 w, s16 h)
  428. {
  429.     s16 i, j, tmp, nx, ny, which_a;//, which_b, cur;
  430.  
  431.     s16 dir[] = {0, 1, 2, 3};
  432.  
  433.     // Shuffle dir
  434.     for(i=3; i>0; --i)
  435.     {
  436.         j = (s16)(KIT_rand() % (i+1));
  437.         tmp = dir[i];
  438.         dir[i] = dir[j];
  439.         dir[j] = tmp;
  440.     }
  441.  
  442.  
  443.  
  444.     // Macros to save a bit on stack space,
  445.     // since this is a recursive function
  446.     #define which_b j
  447.     #define cur tmp
  448.  
  449.     for(i=0; i<4; ++i)
  450.     {
  451.         cur = dir[i];
  452.         nx  =  x + (d_x[cur]<<1);
  453.         ny  =  y + (d_y[cur]<<1);
  454.         which_a  =  nx + ny*w;
  455.  
  456.         if( nx>0 && nx<w &&
  457.                 ny>0 && ny<h &&
  458.                 map[which_a]!=0)
  459.         {
  460.             which_b = nx-d_x[cur] + (ny-d_y[cur])*w;
  461.             map[which_a] = 0;
  462.             map[which_b] = 0;
  463.             carve(nx, ny, map, w, h);
  464.         }
  465.  
  466.     }
  467.  
  468.     #undef which_b
  469.     #undef cur
  470.  
  471. }
  472.  
  473.  
  474.  
  475. // w & h must be odd, otherwise the
  476. // south-eastern edges will also be carved!
  477. bool generateMaze(s8* map, s16 w, s16 h,
  478.                                     s8 fillValue)
  479. {
  480.     s16 stepX, stepY;
  481.     u32 x,y,i, numCells = ((u32)w)*((u32)h);
  482.  
  483.     if( map == NULL  ||
  484.             w<5 || h<5   ||
  485.             fillValue==0 )
  486.     {
  487.         return false;
  488.     }
  489.  
  490.  
  491.  
  492.     // Initialize all cells to fill value,
  493.     // or at random up to -fillValue if negative
  494.     if(fillValue > 0){
  495.         for(i=0; i<numCells; ++i)
  496.             map[i] = fillValue;
  497.  
  498.     } else {
  499.         fillValue = -fillValue;
  500.         for(i=0; i<numCells; ++i)
  501.             map[i] = (s8)(1+KIT_rand()%fillValue);
  502.  
  503.     }
  504.  
  505.  
  506.  
  507.     map[MAZE_START_X + MAZE_START_Y*w] = 0;
  508.  
  509.     carve(MAZE_START_X, MAZE_START_Y, map, w, h);
  510.  
  511.  
  512.  
  513.     // In the event where carve doesn't make a
  514.     // proper path to the exit, make one manually
  515.  
  516.     x = w-2;
  517.     y = h-2;
  518.     i = x + y*w;
  519.  
  520.     stepX = -(KIT_rand()&1);
  521.     stepY = -(!stepX);
  522.  
  523.     while(map[i] != 0 &&
  524.                 x>0 && y>0)
  525.     {
  526.         map[i] = 0;
  527.         x += stepX;
  528.         y += stepY;
  529.         i = x + y*w;
  530.     }
  531.  
  532.  
  533.  
  534.     return true;
  535.  
  536. }
  537.  
  538.  
  539.  
  540.  
  541.  
  542. #define floor(v) 0
  543.  
  544. Color24 hsv2rgb(f32 h, f32 s, f32 v)
  545. {
  546.     int i;
  547.     f32 f, p, q, t;
  548.     f32 r, g, b;
  549.     Color24 out;
  550.  
  551.  
  552.  
  553.     // Normalize values
  554.     h /= 360.0f;
  555.     s /= 100.0f;
  556.     v /= 100.0f;
  557.  
  558.     // The rest is magic or something, idk
  559.     i = floor(h*6);
  560.     f = h * 6 - i;
  561.     p = v * (1.0f -            s);
  562.     q = v * (1.0f -       f  * s);
  563.     t = v * (1.0f - (1.0f-f) * s);
  564.  
  565.     switch(i%6){
  566.         case 0:  r = v,  g = t,  b = p; break;
  567.         case 1:  r = q,  g = v,  b = p; break;
  568.         case 2:  r = p,  g = v,  b = t; break;
  569.         case 3:  r = p,  g = q,  b = v; break;
  570.         case 4:  r = t,  g = p,  b = v; break;
  571.         case 5:  r = v,  g = p,  b = q; break;
  572.     }
  573.  
  574.     // 63 instead of 255, since mode 13h palette colors use 6-bit channels
  575.     out.c.r = r*63;
  576.     out.c.g = g*63;
  577.     out.c.b = b*63;
  578.     out.c._ = 0;
  579.     return out;
  580.  
  581. }
  582. /******************************************************************************/
  583. /******************************************************************************/
  584. //"VGA_stuff_2025-05-22\SRC\DRAW\COLUMN.C":
  585. #include "C:/home/kit_all.h"
  586.  
  587.  
  588.  
  589. extern byte far * const VGA;
  590.  
  591.  
  592.  
  593.  
  594.  
  595. #define EPSILON 0.0001f
  596. #define SCREEN_Hh (SCREEN_H/2)
  597.  
  598. void draw_column(u16 x, f32 height,
  599.                                  byte color)
  600. {
  601.     // Half of the column's height, in pixels,
  602.     // multiplied by SCREEN_W
  603.     s32 pixels_half;
  604.  
  605.     // The start and end pointers, respectively
  606.     byte far* dst_a;
  607.     byte far* dst_b;
  608.  
  609.  
  610.  
  611.     if(x >= SCREEN_W) return;
  612.     if(height <= EPSILON) return;
  613.  
  614.     pixels_half  = (s32)(SCREEN_Hh*MIN(height, 1.0f));
  615.     pixels_half *= SCREEN_W;
  616.  
  617.  
  618.  
  619.     dst_a = VGA + x + SCREEN_Hh*SCREEN_W;
  620.     dst_b = dst_a - SCREEN_W; // dst_a - 1 row
  621.  
  622.     dst_a -= pixels_half;
  623.     dst_b += pixels_half;
  624.  
  625.     // (Increment dst_b, so I can use < in the for loop)
  626.     ++dst_b;
  627.  
  628.  
  629.  
  630.     for(; dst_a<dst_b; dst_a+=SCREEN_W)
  631.         *dst_a = color;
  632.  
  633. }
  634. /******************************************************************************/
  635. /******************************************************************************/
  636. //"VGA_stuff_2025-05-22\SRC\DRAW\F_SCNLNE.C":
  637. #include "C:/home/kit_all.h"
  638.  
  639.  
  640.  
  641. extern byte far * const VGA;
  642.  
  643.  
  644.  
  645.  
  646.  
  647. typedef  u32 far*  u32fp;
  648. typedef  u8  far*  u8fp;
  649.  
  650. void fill_scanlines(u16 y, u16 _len,
  651.                                         byte color)
  652. {
  653.     u32 far* dst_a;
  654.     u32 far* dst_b;
  655.  
  656.     u32 len = _len;
  657.  
  658.     u32 color32 = color;
  659.     color32 |= color32<< 8;
  660.     color32 |= color32<<16;
  661.  
  662.  
  663.  
  664.     if(y   >= SCREEN_H) return;
  665.     if(len == 0       ) return;
  666.  
  667.     if(((u32)y+len) >= SCREEN_H)
  668.         len = SCREEN_H-y;
  669.  
  670.  
  671.  
  672.     dst_a = (u32fp)(VGA + y*SCREEN_W);
  673.     dst_b = (u32fp)((u8fp)dst_a + len*SCREEN_W);
  674.  
  675.     // (++dst_a means incrementing by sizeof(u32))
  676.     for(; dst_a<dst_b; ++dst_a)
  677.         *dst_a = color32;
  678.  
  679. }
  680. /******************************************************************************/
  681. /******************************************************************************/
  682. //"VGA_stuff_2025-05-22\SRC\DRAW\LINE.C":
  683. #include "C:/home/kit_all.h"
  684. #include <kit_std.h>
  685.  
  686.  
  687.  
  688. extern byte far * const VGA;
  689.  
  690.  
  691.  
  692.  
  693.  
  694. void draw_line(          s32 x_0,       s32 y_0,
  695.                              const s32 x_1, const s32 y_1,
  696.                              const byte color)
  697. {
  698.     const s32 d_x =  KIT_labs(x_1 - x_0);
  699.     const s32 d_y = -KIT_labs(y_1 - y_0);
  700.     const s32 s_x = (x_0<x_1) ? 1 : -1;
  701.     const s32 s_y = (y_0<y_1) ? 1 : -1;
  702.     const s32 s_y_row = s_y*SCREEN_W;
  703.  
  704.     s32 err = d_x + d_y;
  705.  
  706.     s32 err2;
  707.  
  708.     byte far* dst = VGA + x_0 + y_0*SCREEN_W;
  709.  
  710.  
  711.  
  712.     while(true){
  713.         if( (x_0>=0) && (x_0<SCREEN_W) &&
  714.                 (y_0>=0) && (y_0<SCREEN_H) )
  715.         {
  716.             *dst = color;
  717.         }
  718.  
  719.         if(x_0==x_1 && y_0==y_1) break;
  720.         err2 = err<<1;
  721.  
  722.         if(err2 >= d_y){
  723.             err += d_y;
  724.             x_0 += s_x;
  725.             dst += s_x;
  726.         }
  727.  
  728.         if(err2 <= d_x){
  729.             err += d_x;
  730.             y_0 += s_y;
  731.             dst += s_y_row;
  732.         }
  733.  
  734.     }
  735.  
  736. }
  737. /******************************************************************************/
  738. /******************************************************************************/
  739. //"VGA_stuff_2025-05-22\SRC\DRAW\RCST_SCN.C":
  740. #include "C:/home/kit_all.h"
  741. #include <draw.h>
  742. #include <kit_std.h>
  743.  
  744.  
  745.  
  746.  
  747.  
  748. KIT_fpoint create_vec2(f32 angleRads,
  749.                                              f32 magnitude)
  750. {
  751.     KIT_fpoint out;
  752.     out.x = KIT_cosf(angleRads)*magnitude;
  753.     out.y = KIT_sinf(angleRads)*magnitude;
  754.  
  755.     return out;
  756.  
  757. }
  758.  
  759.  
  760.  
  761.  
  762.  
  763. #define pos_angle pos.z
  764.  
  765. void draw_raycast_scene(const s8* map, s16 map_w, s16 map_h,
  766.                                                 KIT_fpoint3d pos, // (pos.z is the angle in rads)
  767.                                                 const byte* tiles, // should be 128 in length
  768.                                                 byte colorFloor, byte colorCeiling,
  769.                                                 u16 renderDistance, f32 fov)
  770. {
  771.     KIT_fpoint dir; // Player direction vector
  772.  
  773.     // Camera plane's orientation and size
  774.     // (An FOV larger than 90 is inaccurate!)
  775.     f32        plane_angle;
  776.     f32        plane_magnitude;
  777.     KIT_fpoint plane;
  778.  
  779.     KIT_fpoint rayDir;    // Ray's direction and position
  780.     KIT_fpoint deltaDist; // Length of ray from one x/y side to next x/y side
  781.     KIT_fpoint sideDist;  // Length of ray from current pos. to next x/y side
  782.  
  783.     KIT_spoint pos_map; // The tile the player is currently in
  784.  
  785.     f32 plane_offset; // x-coordinate along the camera plane
  786.  
  787.     KIT_spoint step; // What direction to step in x or y direction (-1 or 1)
  788.  
  789.     bool sideVert; // 'Was an EW (horiz.) or NS (vert.) tile hit?'
  790.  
  791.     s8 tileHit; // abs(tileHit) is the true index (make sure to cast to s16!)
  792.  
  793.     u16 x, i; // Iteration counters, of course
  794.  
  795.     u16 tileWhich; // Map index of the current tile, not the raw tile index!
  796.  
  797.     f32 perpWallDist; // Distance projected on camera direction
  798.  
  799.     f32 columnHeight; // Pixel column height for the current ray (0.0f -> 1.0f)
  800.  
  801.     Color8 color; // the color of the current ray
  802.  
  803.  
  804.  
  805.     if(!map || !tiles || map_w==0 || map_h==0) return;
  806.  
  807.     plane_angle     = M_PI2f + pos_angle; // = pi/2 + pos.z
  808.     plane_magnitude = (f32)CLAMP(fov, 1e-100, 90) / 90;
  809.     plane           = create_vec2(plane_angle, plane_magnitude);
  810.  
  811.     rayDir.x    = rayDir.y    = 0.0f;
  812.     deltaDist.x = deltaDist.y = 0.0f;
  813.     sideDist.x  = sideDist.y  = 0.0f;
  814.  
  815.  
  816.  
  817.     // Draw the ceiling and floor respectively
  818.     fill_scanlines(         0, SCREEN_H/2, colorCeiling);
  819.     fill_scanlines(SCREEN_H/2, SCREEN_H/2, colorFloor);
  820.  
  821.     if(renderDistance == 0) return; // Lol
  822.  
  823.  
  824.  
  825.     // For each column of pixels
  826.     for(x=0; x<SCREEN_W; ++x)
  827.     {
  828.         // The tile the player's currently in
  829.         pos_map.x = (s16)pos.x;
  830.         pos_map.y = (s16)pos.y;
  831.  
  832.         // x-coordinate along the camera plane;
  833.         plane_offset = (f32)(x<<1)/SCREEN_W - 1.0f; // -1.0f -> 1.0f
  834.  
  835.         // Ray's direction and position
  836.         rayDir.x = dir.x + plane.x*plane_offset;
  837.         rayDir.y = dir.y + plane.y*plane_offset;
  838.  
  839.         // (Replace number with 1e30 if it would otherwise divide by 0!)
  840.         deltaDist.x = (rayDir.x) ? KIT_fabsf(1.0f/rayDir.x) : 1e30;
  841.         deltaDist.y = (rayDir.y) ? KIT_fabsf(1.0f/rayDir.y) : 1e30;
  842.  
  843.  
  844.  
  845.         // Calculate step and initial sideDist
  846.  
  847.         if(rayDir.x < 0.0f){
  848.             step.x = -1;
  849.             sideDist.x = deltaDist.x * (pos.x - pos_map.x);
  850.  
  851.         } else {
  852.             step.x = 1;
  853.             sideDist.x = deltaDist.x * (pos_map.x+1 - pos.x);
  854.  
  855.         }
  856.  
  857.         if(rayDir.y < 0.0f){
  858.             step.y = -1;
  859.             sideDist.y = deltaDist.y * (pos.y - pos_map.y);
  860.  
  861.         } else {
  862.             step.y = 1;
  863.             sideDist.y = deltaDist.y * (pos_map.y+1 - pos.y);
  864.  
  865.         }
  866.  
  867.  
  868.  
  869.         tileHit = 0; // (Default of 0 means no tile was hit!)
  870.  
  871.         // For each step in the current ray (perform DDA)
  872.         for(i=0; i<renderDistance; ++i)
  873.         {
  874.             // Jump to next map square, either in the x or y direction
  875.             if(sideDist.x < sideDist.y){
  876.                 sideDist.x += deltaDist.x;
  877.                 pos_map.x  += step.x;
  878.                 sideVert = false; // Closest tile is horizontal
  879.  
  880.             } else {
  881.                 sideDist.y += deltaDist.y;
  882.                 pos_map.y  += step.y;
  883.                 sideVert = true; // Closest tile is vertical
  884.  
  885.             }
  886.  
  887.             tileWhich = pos_map.x + pos_map.y*map_w;
  888.  
  889.             // Check if ray has hit a wall
  890.             if( pos_map.x>=0 && pos_map.x<map_w &&
  891.                     pos_map.y>=0 && pos_map.y<map_h &&
  892.                     map[tileWhich] != 0)
  893.             {
  894.                 tileHit = map[tileWhich];
  895.                 break;
  896.             }
  897.  
  898.         }
  899.  
  900.  
  901.  
  902.         // Calculate distance projected on camera direction,
  903.         // because raw euclidean distance would give a fisheye effect!
  904.         //
  905.         // (In other words, perWallDist is the final length of the current ray)
  906.         if(!sideVert) perpWallDist = (sideDist.x - deltaDist.x);
  907.         else                    perpWallDist = (sideDist.y - deltaDist.y);
  908.  
  909.  
  910.  
  911.         if(tileHit != 0){
  912.             // A multiplier for the height of the pixel column,
  913.             // relative to the height of the framebuffer
  914.             columnHeight = 1.0f/perpWallDist;
  915.  
  916.             // (-1 is applied to tileHit, so that tiles[0] can be accessed)
  917.             if(tileHit < 0) tileHit = -tileHit;
  918.             color.v = tiles[tileHit-1];
  919.  
  920.             // If sideVert, then modulate the
  921.             // final color to be darker than normal
  922.             if(sideVert){
  923.                 color.c.r >>= 1;
  924.                 color.c.g >>= 1;
  925.                 color.c.b >>= 1;
  926.             }
  927.  
  928.             draw_column(x, columnHeight, color.v);
  929.  
  930.         }
  931.  
  932.  
  933.     }
  934.  
  935. }
  936. /******************************************************************************/
  937. /******************************************************************************/
  938. //"VGA_stuff_2025-05-22\KIT_ALL.H":
  939. #ifndef _KIT_ALL_H
  940. #define _KIT_ALL_H
  941.  
  942. #include <dos.h>
  943.  
  944.  
  945.  
  946. // (Mode 0x13 is 320x200 at 8bpp)
  947. #define SCREEN_W 320
  948. #define SCREEN_H 200
  949.  
  950. #define MIN(a,b) (((a)<(b))?(a):(b))
  951. #define MAX(a,b) (((a)>(b))?(a):(b))
  952. #define CLAMP(n,mn,mx) MIN(MAX((n),(mn)),(mx))
  953.  
  954. // Precise version
  955. #define LERP(v0,v1,t) ((1.0f-(t))*(v0)+(t)*(v1))
  956.  
  957. // Imprecise (but potentially faster) version
  958. #define LERP2(v0,v1,t) ((v0)+(t)*((v1)-(v0)))
  959.  
  960.  
  961.  
  962. // Integer bounds
  963. #define KIT_U8_MAX 0xFF
  964. #define KIT_U16_MAX 0xFFFF
  965. #define KIT_U24_MAX 0xFFFFFF
  966. #define KIT_U32_MAX 0xFFFFFFFF
  967. //
  968. #define KIT_S8_MAX 0x7F
  969. #define KIT_S16_MAX 0x7FFF
  970. #define KIT_S24_MAX 0x7FFFFF
  971. #define KIT_S32_MAX 0x7FFFFFFF
  972.  
  973. // Most significant bits/bytes
  974. #define KIT_MSb_8 0x80
  975. #define KIT_MSb_16 0x8000
  976. #define KIT_MSb_24 0x800000
  977. #define KIT_MSb_32 0x80000000
  978. //
  979. #define KIT_MSB_8 0xFF
  980. #define KIT_MSB_16 0xFF00
  981. #define KIT_MSB_24 0xFF0000
  982. #define KIT_MSB_32 0xFF000000
  983.  
  984. // Pi constants (a little overkill for
  985. // single-precision, but whatever.)
  986. #define M_PI2f 1.57079632679489661923f
  987. #define M_PIf  3.14159265358979323846f
  988. #define M_2PIf 6.28318530717958647692f
  989.  
  990. #ifndef NULL
  991. #define NULL ((void*)0)
  992. #endif
  993.  
  994. // (Is this one even necessary?)
  995. #ifndef NULL_FAR
  996. #define NULL_FAR ((void far*)0)
  997. #endif
  998.  
  999.  
  1000.  
  1001. typedef  unsigned char   u8;
  1002. typedef  unsigned short  u16;
  1003. typedef  unsigned long   u32;
  1004.  
  1005. typedef  signed char   s8;
  1006. typedef  signed short  s16;
  1007. typedef  signed long   s32;
  1008.  
  1009. // (These should be analogous
  1010. // to their signed counterparts)
  1011. typedef  char   i8;
  1012. typedef  short  i16;
  1013. typedef  long   i32;
  1014.  
  1015. typedef  float  f32;
  1016.  
  1017. #define false 0
  1018. #define true 1
  1019. typedef  u8  bool;
  1020. typedef  u8  byte;
  1021.  
  1022. #ifndef _SIZE_T
  1023. #define _SIZE_T
  1024. typedef  u32  size_t;
  1025. #endif
  1026.  
  1027.  
  1028.  
  1029. typedef struct KIT_bpoint   {  s8 x, y;            } KIT_bpoint;
  1030. typedef struct KIT_brect    {  s8 x, y, w, h;      } KIT_brect;
  1031. typedef struct KIT_bline    {  s8 x0, y0, x1, y1;  } KIT_bline;
  1032. typedef struct KIT_bcircle  {  s8 x, y, r;         } KIT_bcircle;
  1033. typedef struct KIT_bpoint3d {  s8 x, y, z;         } KIT_bpoint3d;
  1034.  
  1035. typedef struct KIT_spoint   {  s16 x, y;            } KIT_spoint;
  1036. typedef struct KIT_srect    {  s16 x, y, w, h;      } KIT_srect;
  1037. typedef struct KIT_sline    {  s16 x0, y0, x1, y1;  } KIT_sline;
  1038. typedef struct KIT_scircle  {  s16 x, y, r;         } KIT_scircle;
  1039. typedef struct KIT_spoint3d {  s16 x, y, z;         } KIT_spoint3d;
  1040.  
  1041. typedef struct KIT_point   {  s32 x, y;            } KIT_point;
  1042. typedef struct KIT_rect    {  s32 x, y, w, h;      } KIT_rect;
  1043. typedef struct KIT_line    {  s32 x0, y0, x1, y1;  } KIT_line;
  1044. typedef struct KIT_circle  {  s32 x, y, r;         } KIT_circle;
  1045. typedef struct KIT_point3d {  s32 x, y, z;         } KIT_point3d;
  1046.  
  1047. typedef struct KIT_fpoint   {  f32 x, y;            } KIT_fpoint;
  1048. typedef struct KIT_frect    {  f32 x, y, w, h;      } KIT_frect;
  1049. typedef struct KIT_fline    {  f32 x0, y0, x1, y1;  } KIT_fline;
  1050. typedef struct KIT_fcircle  {  f32 x, y, r;         } KIT_fcircle;
  1051. typedef struct KIT_fpoint3d {  f32 x, y, z;         } KIT_fpoint3d;
  1052.  
  1053.  
  1054.  
  1055. // Turns a byte into an integer that is printf'able
  1056. // as a base 2 number with the format specifier "%08X".
  1057. // (This is assuming you're able to print longs with "%X"!)
  1058. #define bin_hex(n) \
  1059. ( \
  1060.                 (n&128) << 21 |\
  1061.                 (n& 64) << 18 |\
  1062.                 (n& 32) << 15 |\
  1063.                 (n& 16) << 12 |\
  1064.                 (n&  8) <<  9 |\
  1065.                 (n&  4) <<  6 |\
  1066.                 (n&  2) <<  3 |\
  1067.                 (n&  1) \
  1068. )
  1069.  
  1070.  
  1071.  
  1072. #define VGA_FAR_PTR ((byte far*)0xA0000000L)
  1073.  
  1074.  
  1075.  
  1076. // (Including the entirety of stdio
  1077. // just for this is unnecessarily slow,
  1078. // even on a RAM disk!)
  1079. int printf(const char* fmt, ...);
  1080.  
  1081.  
  1082.  
  1083. #endif // _KIT_ALL_H
  1084. /******************************************************************************/
  1085. /******************************************************************************/
  1086. //"VGA_stuff_2025-05-22\INCLUDE\DRAW.H":
  1087. #ifndef _DRAW_H
  1088. #define _DRAW_H
  1089.  
  1090. #include "C:/home/kit_all.h"
  1091.  
  1092.  
  1093.  
  1094. #define RGB_U8(r3,g3,b2) ((u8)((r3)<<5 | (g3)<<2 | (b2)))
  1095. #define RGB_U32(r8,g8,b8) ((u32)((u32)(r8)<<16 | (u16)(g8)<<8 | (b8)))
  1096.  
  1097. typedef union Color8 {
  1098.     u8 v;
  1099.     struct {
  1100.         u8 b : 2;
  1101.         u8 g : 3;
  1102.         u8 r : 3;
  1103.     } c;
  1104. } Color8;
  1105.  
  1106. typedef union Color24 {
  1107.     u32 v;
  1108.     struct {
  1109.         u8 b : 8;
  1110.         u8 g : 8;
  1111.         u8 r : 8;
  1112.         u8 _ : 8;
  1113.     } c;
  1114. } Color24;
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120. void draw_line(      s32 x_0,       s32 y_0,
  1121.                              const s32 x_1, const s32 y_1,
  1122.                              const byte color);
  1123.  
  1124.  
  1125.  
  1126. // height is a multiplier, from 0.0f -> 1.0f
  1127. void draw_column(u16 x, f32 height, byte color);
  1128.  
  1129.  
  1130.  
  1131. void fill_scanlines(u16 y, u16 len, byte color);
  1132.  
  1133.  
  1134.  
  1135. KIT_fpoint create_vec2(f32 angleRads, f32 magnitude); // In "RCST_SCN.C"
  1136.  
  1137. void draw_raycast_scene(const s8* map, s16 map_w, s16 map_h,
  1138.                                                 KIT_fpoint3d pos, // (pos.z is the angle in rads)
  1139.                                                 const byte* tiles, // should be 128 in length
  1140.                                                 byte colorFloor, byte colorCeiling,
  1141.                                                 u16 renderDistance, f32 fov);
  1142.  
  1143.  
  1144.  
  1145. #endif // _DRAW_H
  1146. /******************************************************************************/
  1147. /******************************************************************************/
  1148. //"VGA_stuff_2025-05-22\INCLUDE\HARDWARE.H":
  1149. #ifndef _HARDWARE_H
  1150. #define _HARDWARE_H
  1151.  
  1152. #include "draw.h"
  1153.  
  1154.  
  1155.  
  1156. extern byte far * const VGA;
  1157.  
  1158.  
  1159.  
  1160. byte setVideoMode(byte new_mode);
  1161.  
  1162.  
  1163.  
  1164. Color24 getPaletteColor(byte which);
  1165.  
  1166. void setPaletteColor(byte which, u32 rgb);
  1167.  
  1168.  
  1169.  
  1170. u16 readPIT_Ch0(void);
  1171.  
  1172.  
  1173.  
  1174. #endif // _HARDWARE_H
  1175. /******************************************************************************/
  1176. /******************************************************************************/
  1177. //"VGA_stuff_2025-05-22\INCLUDE\KIT_STD.H":
  1178. #ifndef _KIT_STD_H
  1179. #define _KIT_STD_H
  1180.  
  1181. #include "C:/home/kit_all.h"
  1182.  
  1183.  
  1184.  
  1185. long int KIT_labs(long int n);
  1186.  
  1187. float KIT_fabsf(float x);
  1188.  
  1189.  
  1190.  
  1191. // (Doesn't work on far pointers!)
  1192. void* KIT_memset(void* dst, int val, size_t len);
  1193.  
  1194.  
  1195.  
  1196. #define KIT_rand() ((int)KIT_lrand())
  1197.  
  1198. void KIT_srand(long int seed);
  1199.  
  1200. long int KIT_lrand(void);
  1201.  
  1202.  
  1203.  
  1204. float KIT_fmodf(float x, float y);
  1205.  
  1206.  
  1207.  
  1208. #define KIT_cosf(x) KIT_sinf((x)+M_PI2f)
  1209.  
  1210. #define KIT_tanf(x) ( KIT_sinf(x)/KIT_cosf(x) )
  1211.  
  1212. float KIT_sinf(float x);
  1213.  
  1214.  
  1215.  
  1216. #endif // _KIT_STD_H
Tags: C89
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement