Skip to content

Instantly share code, notes, and snippets.

@lpereira
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpereira/40f96983113faa3fade4 to your computer and use it in GitHub Desktop.
Save lpereira/40f96983113faa3fade4 to your computer and use it in GitHub Desktop.
/*
* ----------------------------------------------------------------------------
* "THE MATE-WARE LICENSE" (Revision 42): <leandro@tia.mat.br> wrote this
* file. As long as you retain this notice you can do whatever you want
* with this stuff. If we meet some day, and you think this stuff is worth
* it, you can buy me a Club-Mate in return. Leandro Pereira
* ----------------------------------------------------------------------------
*/
#include <arpa/inet.h>
#include <stdbool.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define WIDTH 160
#define HEIGHT 96
static unsigned char fire[WIDTH * HEIGHT];
static struct {
unsigned char r, g, b;
} palette[256];
#define INIT_PALETTE(index, R, G, B) do { \
palette[index].r = (R); \
palette[index].g = (G); \
palette[index].b = (B); \
} while (0)
static void palette_init()
{
int i;
for (i = 0; i < 0x20; i++) {
unsigned char double_i = i << 1;
unsigned char quad_i = i << 2;
unsigned char octo_i = i << 3;
INIT_PALETTE(i, 0, 0, double_i);
INIT_PALETTE(i + 32, octo_i, 0, 64 - double_i);
INIT_PALETTE(i + 64, 255, octo_i, 0);
INIT_PALETTE(i + 96, 0xff, 0xff, quad_i);
INIT_PALETTE(i + 128, 0xff, 0xff, 64 + quad_i);
INIT_PALETTE(i + 160, 0xff, 0xff, 128 + quad_i);
INIT_PALETTE(i + 192, 0xff, 0xff, 192 + i);
INIT_PALETTE(i + 224, 0xff, 0xff, 224 + i);
}
}
static void fire_update()
{
int i;
int j = WIDTH * (HEIGHT - 1);
int k;
for (i = 0; i < WIDTH - 1; i++)
fire[j + i] = 0xff * (1 + (rand() & 0xf) > 10);
for (k = 0; k < HEIGHT - 1; k++) {
fire[j - WIDTH] = (fire[j] + fire[j + 1] + fire[j - WIDTH]) / 3;
for (i = 1; i < WIDTH - 1; i++) {
short temp =
(fire[j + i] + fire[j + i + 1] + fire[j + i - 1] +
fire[j - WIDTH + i]) >> 2;
fire[j - WIDTH + i] = temp - (temp > 1);
}
j -= WIDTH;
}
}
int socket_init(void)
{
int fd;
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0)
return -errno;
struct sockaddr_in nope = {
.sin_family = AF_INET,
.sin_port = htons(6073)
};
if (inet_pton(AF_INET, "151.217.8.152", &nope.sin_addr) < 0)
return -errno;
if (connect(fd, (struct sockaddr *)&nope, sizeof(nope)) < 0)
return -errno;
return fd;
}
int main(int argc, char *argv[])
{
unsigned char firebuf[WIDTH * HEIGHT];
unsigned char imgbuf[46081] = {0};
int imgbuf_idx;
int fd;
fd = socket_init();
if (fd < 0) {
printf("Error while connecting: %s", strerror(-fd));
return 1;
}
palette_init();
while (true) {
int i, j;
fire_update(firebuf);
for (imgbuf_idx = sizeof(imgbuf), i = HEIGHT - 1; i >= 0; i--) {
for (j = WIDTH - 1; j >= 0; j--) {
int pixel = fire[i * WIDTH + j];
imgbuf[imgbuf_idx--] = palette[pixel].r;
imgbuf[imgbuf_idx--] = palette[pixel].g;
imgbuf[imgbuf_idx--] = palette[pixel].b;
}
}
write(fd, imgbuf, sizeof(imgbuf));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment