Created
April 7, 2015 05:04
-
-
Save dyama/10efcdeddb7dc20583d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#define COL 64 | |
#define ROW 24 | |
FILE* f; | |
/* 画面を白く塗りつぶす */ | |
void clear() | |
{ | |
int i; | |
char n = 0x00; | |
fseek(f, 0, SEEK_SET); | |
for (i = 0; i<COL*ROW; i++) | |
fwrite(&n, sizeof(char), 1, f); | |
} | |
/* 画面を黒く塗りつぶす */ | |
void fill() | |
{ | |
int i; | |
char n = 0xFF; | |
fseek(f, 0, SEEK_SET); | |
for (i = 0; i<COL*ROW; i++) | |
fwrite(&n, sizeof(char), 1, f); | |
} | |
/* XY座標をバイト・ビットインデックスに変換する */ | |
void xy2i(int x, int y, int* by, int* bi) | |
{ | |
int a = y * COL + x; | |
*by = a / 8; | |
*bi = a % 8; | |
} | |
/* 点を描く */ | |
void point(int x, int y, int flag) | |
{ | |
int by, bi; | |
char b; | |
xy2i(x, y, &by, &bi); | |
if (by < 0 || by > COL*ROW / 8) { | |
return; | |
} | |
if (x < 0 || x >= COL) | |
return; | |
if (y < 0 || y >= ROW) | |
return; | |
fseek(f, by, SEEK_SET); | |
fread(&b, sizeof(char), 1, f); | |
if (flag) | |
b |= (1 << (7 - bi)); | |
else | |
b &= ~(1 << (7 - bi)); | |
fseek(f, by, SEEK_SET); | |
fwrite(&b, sizeof(char), 1, f); | |
} | |
/* 直線を描く */ | |
void line(int x1, int y1, int x2, int y2) | |
{ | |
int x, y; | |
int dx, dy; | |
dx = abs(x2 - x1); | |
dy = abs(y2 - y1); | |
if (dx > dy) { | |
if (x1 > x2) { | |
x = x1; | |
x1 = x2; | |
x2 = x; | |
y = y1; | |
y1 = y2; | |
y2 = y; | |
} | |
for (x = x1 >= 0 ? x1 : 0; x <= x2 && x < COL; x++) { | |
point(x, (y2 - y1) * (x - x1) / dx + y1, 1); | |
} | |
} | |
else { | |
if (y1 > y2) { | |
x = x1; | |
x1 = x2; | |
x2 = x; | |
y = y1; | |
y1 = y2; | |
y2 = y; | |
} | |
for (y = y1 >= 0 ? y1 : 0; y <= y2 && y < ROW; y++) { | |
point((x2 - x1) * (y - y1) / dy + x1, y, 1); | |
} | |
} | |
} | |
/* 矩形を描く */ | |
void rect(int x, int y, int w, int h) | |
{ | |
line(x, y, x + w, y); | |
line(x, y, x, y + h); | |
line(x + w, y, x + w, y + h); | |
line(x, y + h, x + w, y + h); | |
} | |
void load_bitmap(char* path) | |
{ | |
FILE* ff = fopen(path, "r"); | |
char line[COL + 2]; | |
int ln = 0; | |
while (fgets(line, COL + 2, ff) != NULL) { | |
for (int i=0; i<COL && strlen(line); i++) { | |
point(i, ln, line[i] == '*' ? 1 : 0); | |
} | |
ln++; | |
if (ln > ROW) | |
break; | |
} | |
fclose(ff); | |
} | |
int main() | |
{ | |
f = fopen("fb0.bin", "w+b"); | |
clear(); | |
// load_bitmap("image.txt"); | |
fclose(f); | |
for (int i=0; i<COL * 100; i++) { | |
f = fopen("fb0.bin", "w+b"); | |
clear(); | |
line(0, ROW/2, COL - 1, ROW/2); | |
double xang; | |
double y; | |
int px, py; | |
int yy; | |
for (int x=0; x<COL + i; x++) { | |
xang = x * 3.141592 / 16; | |
y = sin(xang); | |
y *= 8; | |
if (x > 0) { | |
line(px - i, py + ROW/2, x - i, (int)y + ROW/2); | |
} | |
if (x - i == 10) { | |
yy = (int)y + ROW / 2; | |
} | |
px = x; | |
py = (int)y; | |
} | |
rect(10, yy, 3, 3); | |
fclose(f); | |
sleep(50); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment