Skip to content

Instantly share code, notes, and snippets.

@sgermain06
Last active December 21, 2025 23:54
Show Gist options
  • Select an option

  • Save sgermain06/18fa9c0e0ff0935f3e21922f78cda9b9 to your computer and use it in GitHub Desktop.

Select an option

Save sgermain06/18fa9c0e0ff0935f3e21922f78cda9b9 to your computer and use it in GitHub Desktop.
FIRE.C
/**
* Turbo C implementation of https://gist.github.com/specht/7c3fd49400643d923c66de88a272f5e5
*/
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include "vga.h"
void fire(int x1, int y1, int x2, int y2) {
int x, y, z;
for (y = y1; y <= y2; y++) {
for (x = x1; x <= x2; x++) {
z = getPixel(x, y + 1) * 2 +
getPixel(x + 1, y) +
getPixel(x - 1, y);
z = (int)floor(z / 4);
// Add random noise
if (z > 0) z = z + random(7) - 3;
// Clipping
if (z < 0) z = 0;
if (z > 63) z = 63;
setPixel(x, y, z);
}
}
}
void main() {
int i = 0;
int heat = 10;
char quit = 0;
char c;
setMode(0x13);
clear();
for (i = 0; i <= 15; i++) {
setPalette(i, i * 2, 0, 0);
setPalette(i + 16, (i + 16) * 2, 0, 0);
setPalette(i + 32, 63, i * 4, 0);
setPalette(i + 48, 63, 63, i * 4);
}
do {
drawLine(80, 199, 240, 199, heat);
fire(75, 100, 245, 198);
if (kbhit()) {
c = getch();
if (c == 27) quit = 1;
if (c == '-' && heat > 0) heat--;
if (c == '+' && heat < 63) heat++;
if (c >= '0' && c <= '9') heat = (c - '0') * 7;
}
} while quit == 0;
setMode(3);
}
In order for this to compile properly, you need to have all 3 files in the same directory.
In Turbo C, under Project, select "Open Project"
Type in a name like "FIRE.PRJ" and hit OK
Go back to the Project menu and select "Add Item..."
In the name box, type "*.C"
In the files box, select both FIRE.C and VGA.C and hit "Add"
Once both files are included, hit "Done"
Hit Ctrl-F9 and off you go!
#include "vga.h"
void setMode(int mode) {
asm {
mov ax, mode
int 0x10
}
}
void clear() {
asm {
push 0xA000
pop es
xor di, di
mov cx, 16000
db 0x66
xor ax, ax
db 0x66
rep stosw
}
}
void setPixel(int x, int y, char color) {
asm {
mov di, y
mov bx, di
shl di, 6
shl bx, 8
add di, bx
add di, x
mov al, color
push 0xA000
pop es
stosb
}
}
char getPixel(int x, int y) {
asm {
mov di, y
mob bx, di
shl di, 6
shl bx, 8
add di, bx
add di, x
push 0xA000
pop es
mov al, es:[di]
}
return (_AL);
}
void setPalette(char num, char r, char g, char b) {
asm {
mov dx, 0x3C8
mov al, num
out dx, al
cli
mov dx, 0x3C9
mov al, r
out dx, al
mov al, g
out dx, al
mov al, b
out dx, al
sti
}
}
void swap(int *x, int *y) {
int t = *x;
*x = *y;
*y = t;
}
void drawLine(int x1, int y1, int x2, int y2, char col) {
int dx, dy, dab, inca, incb, x, y, h1, h2;
if (x1 == x2 && y1 == y1) {
setPixel(x1, y1, col);
}
else {
if (x1 > x2) {
swap(&x1, &x2);
swap(&y1, &y2);
}
dx = x2 - x1;
dy = y2 - y1;
h1 = dx;
h2 = dy;
if (dx < -dy && dy < 0) {
y1 = -y1;
y2 = -y2;
swap(&x1, &y1);
swap(&x2, &y2);
}
if (dx >= -dy && dy < 0) {
y1 = -y1;
y2 = -y2;
}
if (dx <= dy && dy > 0) {
swap(&x1, &y1);
swap(&x2, &y2);
}
dx = x2 - x1;
dy = y2 - y1;
dab = 2 * dy - dx;
inca = 2 * (dy - dx);
incb = 2 * dy;
x = x1;
y = y1;
if (h1 < -h2 && h2 < 0) setPixel(y, -x, col);
if (h1 >= -h2 && h2 < 0) setPixel(x, -y, col);
if (h1 > h2 && h2 >= 0) setPixel(x, y, col);
if (h1 <= h2 && h2 > 0) setPixel(y, x, col);
for (x = x1 + 1; x <= x2; x++) {
if (dab < 0) {
dab += incb;
}
else {
dab += inca;
y++;
}
if (h1 < -h2 && h2 < 0) setPixel(y, -x, col);
if (h1 >= -h2 && h2 < 0) setPixel(x, -y, col);
if (h1 > h2 && h2 >= 0) setPixel(x, y, col);
if (h1 <= h2 && h2 > 0) setPixel(y, x, col);
}
}
}
#ifndef VGA_H
#define VGA_H
void setMode(int mode);
void clear();
void setPixel(int x, int y, char color);
char getPixel(int x, int y);
void setPalette(char num, char r, char g, char b);
void drawLine(int x1, int y1, int x2, int y2, char col);
#endif
@sgermain06
Copy link
Copy Markdown
Author

In order to compile the VGA library, you will need to enable the 80286 instruction set in the compiler options.

In Turbo C, go to Options -> Compiler -> Advanced code generation and select the 80286 option under Instruction Set.

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