Add some more math functions and two graphical demos

This commit is contained in:
K. Lange 2018-04-17 16:48:47 +09:00 committed by Kevin Lange
parent 6fb392b110
commit d20a6cc76b
5 changed files with 903 additions and 3 deletions

View File

@ -1,4 +1,4 @@
APPS=init hello sh ls terminal uname compositor drawlines background session kdebug cat yutani-test sysinfo hostname yutani-query env mount date echo nyancat kill ps pstree bim terminal-vga cursor-off font-server migrate free uptime http-get
APPS=init hello sh ls terminal uname compositor drawlines background session kdebug cat yutani-test sysinfo hostname yutani-query env mount date echo nyancat kill ps pstree bim terminal-vga cursor-off font-server migrate free uptime http-get plasma julia
ifeq ($(TOOLCHAIN),)
ifeq ($(shell util/check.sh),y)
@ -188,6 +188,12 @@ base/bin/background: apps/background.c base/lib/libc.so base/lib/libtoaru_graphi
base/bin/drawlines: apps/drawlines.c base/lib/libc.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_pthread.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_pthread -ltoaru_hashmap -ltoaru_list $(LIBS)
base/bin/plasma: apps/plasma.c base/lib/libc.so base/lib/libtoaru_decorations.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_pthread.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_decorations -ltoaru_dlfcn -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_pthread -ltoaru_hashmap -ltoaru_list $(LIBS)
base/bin/julia: apps/julia.c base/lib/libc.so base/lib/libtoaru_decorations.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_pthread.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_decorations -ltoaru_dlfcn -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_pthread -ltoaru_hashmap -ltoaru_list $(LIBS)
base/bin/yutani-query: apps/yutani-query.c base/lib/libc.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_pthread.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_pthread -ltoaru_hashmap -ltoaru_list $(LIBS)

314
apps/julia.c Normal file
View File

@ -0,0 +1,314 @@
/* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2014 Kevin Lange
*/
/*
* Julia Fractal Generator
*
* This is the updated windowed version of the
* julia fractal generator demo.
*/
#include <stdio.h>
#include <stdint.h>
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#define DIRECT_OFFSET(x,y) ((x) + (y) * window->width)
/*
* Macros make verything easier.
*/
#define SPRITE(sprite,x,y) sprite->bitmap[sprite->width * (y) + (x)]
#define GFX_(xpt, ypt) ((uint32_t *)window->buffer)[DIRECT_OFFSET(xpt+decor_left_width,ypt+decor_top_height)]
/* Pointer to graphics memory */
static yutani_t * yctx;
static yutani_window_t * window = NULL;
static gfx_context_t * ctx = NULL;
/* Julia fractals elements */
float conx = -0.74; /* real part of c */
float cony = 0.1; /* imag part of c */
float Maxx = 2; /* X bounds */
float Minx = -2;
float Maxy = 1; /* Y bounds */
float Miny = -1;
float initer = 1000; /* Iteration levels */
float pixcorx; /* Internal values */
float pixcory;
int newcolor; /* Color we're placing */
int lastcolor; /* Last color we placed */
int no_repeat = 0; /* Repeat colors? */
/*
* Color table
* These are orange/red shades from the Ubuntu platte.
*/
int colors[] = {
0xFFeec73e,
0xFFf0a513,
0xFFfb8b00,
0xFFf44800,
0xFFffff99,
0xFFffff00,
0xFFfdca01,
0xFF986601,
0xFFf44800,
0xFFfd3301,
0xFFd40000,
0xFF980101,
};
int left = 40;
int top = 40;
int width = 300;
int height = 300;
void julia(int xpt, int ypt) {
long double x = xpt * pixcorx + Minx;
long double y = Maxy - ypt * pixcory;
long double xnew = 0;
long double ynew = 0;
int k = 0;
for (k = 0; k <= initer; k++) {
xnew = x * x - y * y + conx;
ynew = 2 * x * y + cony;
x = xnew;
y = ynew;
if ((x * x + y * y) > 4)
break;
}
int color;
if (no_repeat) {
color = 12 * k / initer;
} else {
color = k;
if (color > 11) {
color = color % 12;
}
}
if (k >= initer) {
GFX_(xpt,ypt) = rgb(0,0,0);
} else {
GFX_(xpt,ypt) = colors[color];
}
newcolor = color;
}
void usage(char * argv[]) {
printf(
"Julia fractal generator.\n"
"\n"
"usage: %s [-n] [-i \033[3miniter\033[0m] [-x \033[3mminx\033[0m] \n"
" [-X \033[3mmaxx\033[0m] [-c \033[3mconx\033[0m] [-C \033[3mcony\033[0m]\n"
" [-W \033[3mwidth\033[0m] [-H \033[3mheight\033[0m] [-h]\n"
"\n"
" -n --no-repeat \033[3mDo not repeat colors\033[0m\n"
" -i --initer \033[3mInitializer value\033[0m\n"
" -x --minx \033[3mMinimum X value\033[0m\n"
" -X --maxx \033[3mMaximum X value\033[0m\n"
" -c --conx \033[3mcon x\033[0m\n"
" -C --cony \033[3mcon y\033[0m\n"
" -W --width \033[3mWindow width\033[0m\n"
" -H --height \033[3mWindow height\033[0m\n"
" -h --help \033[3mShow this help message.\033[0m\n",
argv[0]);
}
static void decors() {
render_decorations(window, ctx, "Julia Fractals");
}
void redraw() {
#if 0
printf("initer: %f\n", initer);
printf("X: %f %f\n", Minx, Maxx);
#endif
float _x = Maxx - Minx;
float _y = _x / width * height;
Miny = 0 - _y / 2;
Maxy = _y / 2;
#if 0
printf("Y: %f %f\n", Miny, Maxy);
printf("conx: %f cony: %f\n", conx, cony);
#endif
decors();
newcolor = 0;
lastcolor = 0;
pixcorx = (Maxx - Minx) / width;
pixcory = (Maxy - Miny) / height;
int j = 0;
do {
int i = 1;
do {
julia(i,j);
if (lastcolor != newcolor) julia(i-1,j);
else if (i > 0) GFX_(i-1,j) = colors[lastcolor];
newcolor = lastcolor;
i+= 2;
} while ( i < width );
++j;
} while ( j < height );
}
void resize_finish(int w, int h) {
yutani_window_resize_accept(yctx, window, w, h);
reinit_graphics_yutani(ctx, window);
width = w - decor_left_width - decor_right_width;
height = h - decor_top_height - decor_bottom_height;
redraw();
yutani_window_resize_done(yctx, window);
yutani_flip(yctx, window);
}
int main(int argc, char * argv[]) {
#if 0
static struct option long_opts[] = {
{"no-repeat", no_argument, 0, 'n'},
{"initer", required_argument, 0, 'i'},
{"minx", required_argument, 0, 'x'},
{"maxx", required_argument, 0, 'X'},
{"conx", required_argument, 0, 'c'},
{"cony", required_argument, 0, 'C'},
{"width", required_argument, 0, 'W'},
{"height", required_argument, 0, 'H'},
{"help", no_argument, 0, 'h'},
{0,0,0,0}
};
if (argc > 1) {
/* Read some arguments */
int index, c;
while ((c = getopt_long(argc, argv, "ni:x:X:c:C:W:H:h", long_opts, &index)) != -1) {
if (!c) {
if (long_opts[index].flag == 0) {
c = long_opts[index].val;
}
}
switch (c) {
case 'n':
no_repeat = 1;
break;
case 'i':
initer = atof(optarg);
break;
case 'x':
Minx = atof(optarg);
break;
case 'X':
Maxx = atof(optarg);
break;
case 'c':
conx = atof(optarg);
break;
case 'C':
cony = atof(optarg);
break;
case 'W':
width = atoi(optarg);
break;
case 'H':
height = atoi(optarg);
break;
case 'h':
usage(argv);
exit(0);
break;
default:
break;
}
}
}
#endif
yctx = yutani_init();
init_decorations();
window = yutani_window_create(yctx, width + decor_width(), height + decor_height());
yutani_window_move(yctx, window, left, top);
yutani_window_advertise_icon(yctx, window, "Julia Fractals", "julia");
ctx = init_graphics_yutani(window);
redraw();
yutani_flip(yctx, window);
int playing = 1;
while (playing) {
yutani_msg_t * m = yutani_poll(yctx);
if (m) {
switch (m->type) {
case YUTANI_MSG_KEY_EVENT:
{
struct yutani_msg_key_event * ke = (void*)m->data;
if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
playing = 0;
}
}
break;
case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
{
struct yutani_msg_window_focus_change * wf = (void*)m->data;
yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
if (win) {
win->focused = wf->focused;
decors();
yutani_flip(yctx, window);
}
}
break;
case YUTANI_MSG_RESIZE_OFFER:
{
struct yutani_msg_window_resize * wr = (void*)m->data;
resize_finish(wr->width, wr->height);
}
break;
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
{
int result = decor_handle_event(yctx, m);
switch (result) {
case DECOR_CLOSE:
playing = 0;
break;
default:
/* Other actions */
break;
}
}
break;
case YUTANI_MSG_SESSION_END:
playing = 0;
break;
default:
break;
}
}
free(m);
}
yutani_close(yctx, window);
return 0;
}

176
apps/plasma.c Normal file
View File

@ -0,0 +1,176 @@
/* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2014 Kevin Lange
*/
/*
* test-gfx
*
* Windowed graphical test application.
*/
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <syscall.h>
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/pthread.h>
#include <toaru/spinlock.h>
#define dist(a,b,c,d) sqrt((double)(((a) - (c)) * ((a) - (c)) + ((b) - (d)) * ((b) - (d))))
static yutani_t * yctx;
static yutani_window_t * wina;
static int should_exit = 0;
uint16_t win_width;
uint16_t win_height;
uint16_t off_x;
uint16_t off_y;
static int volatile draw_lock = 0;
gfx_context_t * ctx;
void redraw_borders() {
render_decorations(wina, ctx, "Plasma");
}
uint32_t hsv_to_rgb(int h, float s, float v) {
float c = v * s;
float hp = (float)h / 42.6666666f;
float x = c * (1.0 - fabs(fmod(hp, 2) - 1.0));
float m = v - c;
float rp, gp, bp;
if (hp < 1.0) { rp = c; gp = x; bp = 0; }
else if (hp < 2.0) { rp = x; gp = c; bp = 0; }
else if (hp < 3.0) { rp = 0; gp = c; bp = x; }
else if (hp < 4.0) { rp = 0; gp = x; bp = c; }
else if (hp < 5.0) { rp = x; gp = 0; bp = c; }
else if (hp < 6.0) { rp = c; gp = 0; bp = x; }
else { rp = 0; gp = 0; bp = 0; }
return rgb((rp + m) * 255, (gp + m) * 255, (bp + m) * 255);
}
void * draw_thread(void * garbage) {
(void)garbage;
double time = 0;
/* Generate a palette */
uint32_t palette[256];
for (int x = 0; x < 256; ++x) {
palette[x] = hsv_to_rgb(x,1.0,1.0);
}
while (!should_exit) {
time += 1.0;
int w = win_width;
int h = win_height;
spin_lock(&draw_lock);
for (int x = 0; x < win_width; ++x) {
for (int y = 0; y < win_height; ++y) {
double value = sin(dist(x + time, y, 128.0, 128.0) / 8.0)
+ sin(dist(x, y, 64.0, 64.0) / 8.0)
+ sin(dist(x, y + time / 7, 192.0, 64) / 7.0)
+ sin(dist(x, y, 192.0, 100.0) / 8.0);
GFX(ctx, x + off_x, y + off_y) = palette[(int)((value + 4) * 32)];
}
}
redraw_borders();
flip(ctx);
yutani_flip(yctx, wina);
spin_unlock(&draw_lock);
syscall_yield();
}
}
void resize_finish(int w, int h) {
yutani_window_resize_accept(yctx, wina, w, h);
reinit_graphics_yutani(ctx, wina);
win_width = w - decor_width();
win_height = h - decor_height();
yutani_window_resize_done(yctx, wina);
}
int main (int argc, char ** argv) {
yctx = yutani_init();
win_width = 100;
win_height = 100;
init_decorations();
off_x = decor_left_width;
off_y = decor_top_height;
/* Do something with a window */
wina = yutani_window_create(yctx, win_width + decor_width(), win_height + decor_height());
yutani_window_move(yctx, wina, 300, 300);
ctx = init_graphics_yutani_double_buffer(wina);
draw_fill(ctx, rgb(0,0,0));
redraw_borders();
flip(ctx);
yutani_flip(yctx, wina);
yutani_window_advertise_icon(yctx, wina, "Plasma", "plasma");
pthread_t thread;
pthread_create(&thread, NULL, draw_thread, NULL);
while (!should_exit) {
yutani_msg_t * m = yutani_poll(yctx);
if (m) {
switch (m->type) {
case YUTANI_MSG_KEY_EVENT:
{
struct yutani_msg_key_event * ke = (void*)m->data;
if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
should_exit = 1;
}
}
break;
case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
{
struct yutani_msg_window_focus_change * wf = (void*)m->data;
yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
if (win) {
win->focused = wf->focused;
}
}
break;
case YUTANI_MSG_SESSION_END:
should_exit = 1;
break;
case YUTANI_MSG_RESIZE_OFFER:
{
struct yutani_msg_window_resize * wr = (void*)m->data;
spin_lock(&draw_lock);
resize_finish(wr->width, wr->height);
spin_unlock(&draw_lock);
}
break;
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
if (decor_handle_event(yctx, m) == DECOR_CLOSE) {
should_exit = 1;
}
break;
default:
break;
}
free(m);
}
}
yutani_close(yctx, wina);
return 0;
}

View File

@ -3,5 +3,9 @@
extern double floor(double x);
extern int abs(int j);
extern double pow(double x, double y);
double exp(double x);
extern double exp(double x);
extern double fmod(double x, double y);
extern double sqrt(double x);
extern double fabs(double x);
extern double sin(double x);
extern double cos(double x);

View File

@ -29,3 +29,403 @@ int abs(int j) {
double pow(double x, double y) {
return __builtin_pow(x,y);
}
double fabs(double x) {
return __builtin_fabs(x);
}
double fmod(double x, double y) {
if (x >= 0.0) {
while (x > y) {
x -= y;
}
return x;
} else {
return 0.0;
}
}
double sqrt(double x) {
return __builtin_sqrt(x);
}
static double bad_sine_table[] = {
0,
0.01745240644,
0.03489949671,
0.05233595625,
0.06975647375,
0.08715574276,
0.1045284633,
0.1218693434,
0.139173101,
0.1564344651,
0.1736481777,
0.1908089954,
0.2079116908,
0.2249510544,
0.2419218956,
0.2588190451,
0.2756373559,
0.2923717048,
0.3090169944,
0.3255681545,
0.3420201434,
0.3583679496,
0.3746065935,
0.3907311285,
0.4067366431,
0.4226182618,
0.4383711468,
0.4539904998,
0.4694715628,
0.4848096203,
0.5000000001,
0.515038075,
0.5299192643,
0.5446390351,
0.5591929035,
0.5735764364,
0.5877852524,
0.6018150232,
0.6156614754,
0.6293203911,
0.6427876098,
0.6560590291,
0.6691306064,
0.6819983601,
0.6946583705,
0.7071067813,
0.7193398004,
0.7313537017,
0.7431448256,
0.7547095803,
0.7660444432,
0.7771459615,
0.7880107537,
0.7986355101,
0.8090169944,
0.8191520444,
0.8290375726,
0.838670568,
0.8480480962,
0.8571673008,
0.8660254039,
0.8746197072,
0.8829475929,
0.8910065243,
0.8987940464,
0.9063077871,
0.9135454577,
0.9205048535,
0.9271838546,
0.9335804266,
0.9396926208,
0.9455185757,
0.9510565163,
0.956304756,
0.961261696,
0.9659258263,
0.9702957263,
0.9743700648,
0.9781476008,
0.9816271835,
0.984807753,
0.9876883406,
0.9902680688,
0.9925461517,
0.9945218954,
0.9961946981,
0.9975640503,
0.9986295348,
0.999390827,
0.9998476952,
1,
0.9998476952,
0.999390827,
0.9986295347,
0.9975640502,
0.9961946981,
0.9945218953,
0.9925461516,
0.9902680687,
0.9876883406,
0.984807753,
0.9816271834,
0.9781476007,
0.9743700647,
0.9702957262,
0.9659258262,
0.9612616959,
0.9563047559,
0.9510565162,
0.9455185755,
0.9396926207,
0.9335804264,
0.9271838545,
0.9205048534,
0.9135454575,
0.9063077869,
0.8987940462,
0.8910065241,
0.8829475927,
0.874619707,
0.8660254036,
0.8571673006,
0.848048096,
0.8386705678,
0.8290375724,
0.8191520441,
0.8090169942,
0.7986355099,
0.7880107534,
0.7771459613,
0.7660444429,
0.75470958,
0.7431448253,
0.7313537014,
0.7193398001,
0.707106781,
0.6946583702,
0.6819983598,
0.6691306061,
0.6560590288,
0.6427876094,
0.6293203908,
0.6156614751,
0.6018150229,
0.587785252,
0.5735764361,
0.5591929032,
0.5446390347,
0.5299192639,
0.5150380746,
0.4999999997,
0.4848096199,
0.4694715625,
0.4539904994,
0.4383711465,
0.4226182614,
0.4067366428,
0.3907311282,
0.3746065931,
0.3583679492,
0.342020143,
0.3255681541,
0.309016994,
0.2923717044,
0.2756373555,
0.2588190447,
0.2419218952,
0.224951054,
0.2079116904,
0.190808995,
0.1736481773,
0.1564344647,
0.1391731006,
0.121869343,
0.1045284629,
0.08715574235,
0.06975647334,
0.05233595584,
0.0348994963,
0.01745240603,
-0.000000000410206857,
-0.01745240685,
-0.03489949712,
-0.05233595666,
-0.06975647416,
-0.08715574317,
-0.1045284637,
-0.1218693438,
-0.1391731014,
-0.1564344655,
-0.1736481781,
-0.1908089958,
-0.2079116912,
-0.2249510548,
-0.241921896,
-0.2588190455,
-0.2756373562,
-0.2923717052,
-0.3090169948,
-0.3255681549,
-0.3420201438,
-0.35836795,
-0.3746065938,
-0.3907311289,
-0.4067366435,
-0.4226182622,
-0.4383711472,
-0.4539905002,
-0.4694715632,
-0.4848096207,
-0.5000000004,
-0.5150380753,
-0.5299192646,
-0.5446390354,
-0.5591929039,
-0.5735764368,
-0.5877852527,
-0.6018150235,
-0.6156614757,
-0.6293203914,
-0.6427876101,
-0.6560590294,
-0.6691306067,
-0.6819983604,
-0.6946583708,
-0.7071067815,
-0.7193398007,
-0.731353702,
-0.7431448258,
-0.7547095806,
-0.7660444435,
-0.7771459618,
-0.7880107539,
-0.7986355104,
-0.8090169947,
-0.8191520446,
-0.8290375729,
-0.8386705682,
-0.8480480964,
-0.857167301,
-0.8660254041,
-0.8746197074,
-0.8829475931,
-0.8910065244,
-0.8987940465,
-0.9063077873,
-0.9135454579,
-0.9205048537,
-0.9271838548,
-0.9335804267,
-0.939692621,
-0.9455185758,
-0.9510565165,
-0.9563047561,
-0.9612616961,
-0.9659258264,
-0.9702957264,
-0.9743700649,
-0.9781476009,
-0.9816271836,
-0.9848077531,
-0.9876883407,
-0.9902680688,
-0.9925461517,
-0.9945218954,
-0.9961946981,
-0.9975640503,
-0.9986295348,
-0.999390827,
-0.9998476952,
-1,
-0.9998476951,
-0.999390827,
-0.9986295347,
-0.9975640502,
-0.996194698,
-0.9945218953,
-0.9925461516,
-0.9902680687,
-0.9876883405,
-0.9848077529,
-0.9816271833,
-0.9781476006,
-0.9743700646,
-0.9702957261,
-0.9659258261,
-0.9612616958,
-0.9563047558,
-0.9510565161,
-0.9455185754,
-0.9396926206,
-0.9335804263,
-0.9271838543,
-0.9205048532,
-0.9135454574,
-0.9063077868,
-0.898794046,
-0.8910065239,
-0.8829475925,
-0.8746197068,
-0.8660254034,
-0.8571673003,
-0.8480480958,
-0.8386705676,
-0.8290375722,
-0.8191520439,
-0.809016994,
-0.7986355096,
-0.7880107532,
-0.777145961,
-0.7660444427,
-0.7547095798,
-0.743144825,
-0.7313537011,
-0.7193397998,
-0.7071067807,
-0.6946583699,
-0.6819983595,
-0.6691306058,
-0.6560590284,
-0.6427876091,
-0.6293203905,
-0.6156614747,
-0.6018150226,
-0.5877852517,
-0.5735764357,
-0.5591929029,
-0.5446390344,
-0.5299192636,
-0.5150380743,
-0.4999999993,
-0.4848096196,
-0.4694715621,
-0.4539904991,
-0.4383711461,
-0.422618261,
-0.4067366424,
-0.3907311278,
-0.3746065927,
-0.3583679488,
-0.3420201426,
-0.3255681537,
-0.3090169936,
-0.292371704,
-0.2756373551,
-0.2588190443,
-0.2419218948,
-0.2249510536,
-0.20791169,
-0.1908089946,
-0.1736481769,
-0.1564344643,
-0.1391731002,
-0.1218693426,
-0.1045284625,
-0.08715574194,
-0.06975647293,
-0.05233595543,
-0.03489949589,
-0.01745240562,
0.0
};
double sin(double x) {
if (x < 0.0) {
x += 3.141592654 * 2.0 * 100.0;
}
double z = fmod(x, 3.141592654 * 2.0);
int i = z * 360.0 / (3.141582654 * 2.0);
return bad_sine_table[i];
}
double cos(double x) {
return sin(x + 3.141592654 / 2.0);
}