summaryrefslogtreecommitdiff
path: root/user/apple.c
blob: 578a075159dbfc335a0f9825a1e31ab0cec91125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <incbin.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

INCBIN(APPLE, "data/apple.bin");

#define APPLE_WIDTH 480
#define APPLE_HEIGHT 360
#define APPLE_FPS 30
#define APPLE_FRAMES 6572
#define VSYNC_FPS 60

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

// returned from drm
static uint32_t *fb;
static int width, height, bpp, scale;
size_t fb_size;

// decompress state
static size_t d_offset = 0, d_length = 0;
static unsigned char d_value;

// frame counter
static size_t last_frame = SIZE_MAX, frame = 0;

// frame buffer
static unsigned char frame_buf[APPLE_WIDTH * APPLE_HEIGHT];

// backbuffer
static uint32_t *back_fb;

// start tick
static size_t tick_start;

static unsigned char read_byte(void)
{
	while (d_length < 1) {
		unsigned char len;

		d_length = 0;
		do {
			len = gAPPLEData[d_offset++];
			d_length += len;
		} while (len == 255);
		d_value = gAPPLEData[d_offset++];
	}

	d_length--;
	return d_value;
}

static void read_frame(void)
{
	for (size_t i = 0; i < sizeof(frame_buf); i++)
		frame_buf[i] = read_byte();
}

static void swap_buffers(void)
{
	// terrible :3
	uint32_t count = width * height / 2;
	__asm__ volatile("rep movsq" ::"S"(back_fb), "D"(fb), "c"(count)
					 : "memory");
}

static void draw_frame(void)
{
	for (int y = 0; y < APPLE_HEIGHT * scale; y++) {
		for (int x = 0; x < APPLE_WIDTH * scale; x++) {
			unsigned char color =
				frame_buf[(x / scale) + (y / scale) * APPLE_WIDTH];
			back_fb[x + y * width] = (color << 16) | (color << 8) |
									 (color << 0);
		}
	}
}

int main(void)
{
	printf("all your apple belong to bad\n");

	if (drm((void **)&fb, &width, &height, &bpp)) {
		fprintf(stderr, "failure!\n");
		return 1;
	}

	fb_size = (width * height * bpp) / 8;
	back_fb = malloc(fb_size);
	if (back_fb == NULL) {
		fprintf(stderr, "failure but 2\n");
		return 1;
	}

	tick_start = ticks();
	scale = MIN(width / APPLE_WIDTH, height / APPLE_HEIGHT);

	while (1) {
		if (last_frame != frame) {
			read_frame();
			draw_frame();
			swap_buffers();
		}

		last_frame = frame;
		frame = ((ticks() - tick_start) / (1000 / APPLE_FPS));
		if (frame >= APPLE_FRAMES)
			break;
	}

	return 0;
}