Skip to content

Instantly share code, notes, and snippets.

@root42
Created September 26, 2018 20:09
Show Gist options
  • Save root42/72ce2cb3e935aa03e54b239cfa39d298 to your computer and use it in GitHub Desktop.
Save root42/72ce2cb3e935aa03e54b239cfa39d298 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <conio.h>
#include <dos.h>
#include <mem.h>
#define VIDEO_INT 0x10
#define SET_MODE 0x00
#define VGA_256_COLOR_MODE 0x13
#define TEXT_MODE 0x03
#define PALETTE_INDEX 0x03c8
#define PALETTE_DATA 0x03c9
#define INPUT_STATUS 0x03da
#define VRETRACE 0x08
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 200
#define NUM_COLORS 256
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned long dword;
byte far *VGA=(byte far *)0xA0000000L;
word *my_clock=(word *)0x0000046c;
char far *STR_HELLO = "1234567890";
#define SETPIX(x,y,c) *(VGA+(x)+(y)*SCREEN_WIDTH)=c
#define GETPIX(x,y) *(VGA+(x)+(y)*SCREEN_WIDTH)
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
byte ONE[] = { 0x0c,0x0e,0x0c,0x0c,0x0c,0x0c,0x3f,0x00 };
byte TWO[] = { 0x1e,0x33,0x30,0x1c,0x06,0x33,0x3f,0x00 };
byte THREE[] = { 0x1e,0x33,0x30,0x1c,0x30,0x33,0x1e,0x00 };
void print(byte *ch, word x, word y, byte col)
{
int i,j;
byte *b,bit;
for(j=0;j<8;j++) {
b=ch+j;
for(i=0;i<8;i++) {
bit=(*b >> i) & 1;
if( bit ) {
SETPIX(x+i,y+j,col);
}
}
}
}
void set_mode(byte mode)
{
union REGS regs;
regs.h.ah=SET_MODE;
regs.h.al=mode;
int86(VIDEO_INT,&regs,&regs);
}
void set_palette(byte *palette)
{
int i;
outp(PALETTE_INDEX,0);
for(i=0;i<256*3;++i) {
outp(PALETTE_DATA,palette[i]);
}
}
byte *get_fire_palette()
{
byte *pal;
int i;
pal=malloc(256*3);
for(i=0;i<256;i++) {
pal[767-(i*3+2)]=(255-i)>>2;
pal[767-(i*3+1)]=MAX((128-i),0)>>2;
pal[767-(i*3+0)]=MAX((92-i),0)>>2;
}
return pal;
}
void draw_fire(word x, word y, word w, word h,int f)
{
byte c,o1,o2;
word i,j;
for(j=y;j<y+h;j++) {
for(i=x;i<x+w;i++) {
o1=GETPIX(i-1+f,j+1);
o2=GETPIX(i+1+f,j+1);
c=(o1+o2)>>1;
c=MAX(c,2)-2;
SETPIX(i,j,c);
}
}
}
void draw_fuel(word x, word y, word w, word h, byte c)
{
int i,j;
for(i=x;i<x+w;i++) {
c=(byte)(rand()&0xff);
SETPIX(i,y+h,c);
}
}
void wait_for_retrace(void)
{
while((inp(INPUT_STATUS)&VRETRACE));
while(!(inp(INPUT_STATUS)&VRETRACE));
}
int main()
{
byte *pal;
int d,i,f,winner;
word x,y,dx;
byte col[] = {0xff,0xff,0xff};
char *contestants[] = { "ONE", "TWO", "THREE" };
randomize();
winner = rand() % 3;
set_mode(VGA_256_COLOR_MODE);
pal=get_fire_palette();
set_palette(pal);
x = 100;
y = 100;
dx = 50;
f = 0;
while(!kbhit())
{
if( f++ == 100 ) {
col[0]=winner==0?0xff:0x0;
col[1]=winner==1?0xff:0x0;
col[2]=winner==2?0xff:0x0;
}
wait_for_retrace();
for(i=0;i<3;i++) {
d=rand()&0x03;
if(d==1) {
d=1;
}else if(d==2){
d=-1;
}else{
d=0;
}
draw_fire(x+i*dx-2,y-50,12,60,d);
}
print(ONE,x,y,col[0]);
print(TWO,x+dx,y,col[1]);
print(THREE,x+2*dx,y,col[2]);
}
set_mode(TEXT_MODE);
printf("Congratulations %s! You have won!\n",contestants[winner]);
getchar();
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment