⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nsaver.c

📁 一个嵌入式操作系统(microwindows)的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * The Original Code is NanoScreenSaver. * * The Initial Developer of the Original Code is Alex Holden. * Portions created by Alex Holden are Copyright (C) 2000, 2002 * Alex Holden <alex@alexholden.net>. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms * of the GNU General Public license (the  "[GNU] License"), in which case the * provisions of [GNU] License are applicable instead of those * above.  If you wish to allow use of your version of this file only * under the terms of the [GNU] License and not to allow others to use * your version of this file under the MPL, indicate your decision by * deleting  the provisions above and replace  them with the notice and * other provisions required by the [GNU] License.  If you do not delete * the provisions above, a recipient may use your version of this file * under either the MPL or the [GNU] License. *//* * A collection of screen savers for Nano-X by Alex Holden. */#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <math.h>#include <sys/time.h>#include <nano-X.h>#include <nxcolors.h>#include "nsaver.h"void *my_malloc(size_t size){	void *ret;	if(!(ret = malloc(size))) {		fprintf(stderr, "Out of memory\n");		exit(1);	}	return ret;}GR_COLOR get_random_colour(int min_brightness){	int r, g, b;	do {			r = RANDRANGE(0, 255);		g = RANDRANGE(0, 255);		b = RANDRANGE(0, 255);	} while((r + g + b) / 3 < min_brightness);	return(MWRGB(r, g, b));}void get_random_point_on_screen(nstate *state, GR_COORD *x, GR_COORD *y,							GR_COLOR *c){	if(x) *x = RANDRANGE(0, state->si.cols - 1);	if(y) *y = RANDRANGE(0, state->si.rows - 1);	if(c) *c = get_random_colour(GRP_MINBRIGHTNESS);}int not_square(int n){	while(n) {		if(n & 1) {			if(n - 1) return 1;			else return 0;		}		n >>= 1;	}	return 1;}void make_random_square(nstate *state, int min_brightness, int min_size,			int max_size, int need_square, GR_COORD *retx,			GR_COORD *rety, GR_SIZE *retsize){	int x1, x2, y, size, tmp;	GR_COLOR colour = get_random_colour(min_brightness); 	GrSetGCForeground(state->main_gc, colour);	do {		x1 = RANDRANGE(0, state->si.cols);		x2 = RANDRANGE(0, state->si.cols);		if(x1 > x2) {			tmp = x1;			x1 = x2;			x2 = tmp;		}		size = x2 - x1;	} while(size < min_size || size > max_size ||			(need_square && not_square(size)));	y = RANDRANGE(0, state->si.rows - size - 1);	*retx = x1;	*rety = y;	*retsize = size;}#ifdef HAVE_USLEEPvoid msleep(long ms){	usleep(ms * 1000);}#elsevoid msleep(long ms){	struct timespec req, rem;	req.tv_sec = ms / 1000000;	req.tv_nsec = (ms % 1000000) * 1000000;	while(nanosleep(&req, &rem) == -1) {		if(errno == EINTR) {			req.tv_sec = rem.tv_sec;			req.tv_nsec = rem.tv_nsec;			continue;		} else {			perror("nanosleep() failed");			return;		}	}}#endifGR_WINDOW_ID capture_screen(nstate *state){	GR_WINDOW_ID pid;#if CAPTURESCREEN_DELAY	/* This is a hack to give the system enough time to redraw the screen	 * before capturing it when we switch from one screensaver to another.	 * without it, we often capture an only partially redrawn screen. */	msleep(CAPTURESCREEN_DELAY);#endif		pid = GrNewPixmap(state->si.cols, state->si.rows, NULL);	GrCopyArea(pid, state->main_gc, 0, 0, state->si.cols,			state->si.rows, GR_ROOT_WINDOW_ID, 0, 0, 0);	return pid;}void saver1_init(nstate *state) {}void saver1_exposure(nstate *state){	GrClearWindow(state->main_window, 0);}void saver1_animate(nstate *state) {}void saver2_init(nstate *state){	s2state *s = my_malloc(sizeof(s2state));	state->priv = s;	s->pixels = SAVER2_MAXPIXELS;	state->animate_interval = SAVER2_DELAY;}void saver2_exposure(nstate *state){	GrClearWindow(state->main_window, 0);}void saver2_animate(nstate *state){	GR_COORD x, y;	GR_COLOR c;	int pixels = SAVER2_PIXELS_PER_FRAME;	s2state *s = state->priv;	while(pixels--) {		if(!(s->pixels--)) {			s->pixels = SAVER2_MAXPIXELS;			GrClearWindow(state->main_window, 0);		}		get_random_point_on_screen(state, &x, &y, &c);		GrSetGCForeground(state->main_gc, c);		GrPoint(state->main_window, state->main_gc, x, y);	}}void saver3_init(nstate *state){	s3state *s = my_malloc(sizeof(s3state));	state->priv = s;	s->maxsegments = SAVER3_MAXSEGMENTS;	s->lastx = 0;	s->lasty = 0;	state->animate_interval = SAVER3_DELAY;}void saver3_exposure(nstate *state){	GrClearWindow(state->main_window, 0);}void saver3_animate(nstate *state){	GR_COORD newx, newy;	GR_COLOR c;	s3state *s = state->priv;	int pixels = SAVER3_SEGMENTS_PER_FRAME;	while(pixels--) {		if(!(s->maxsegments--)) {			s->maxsegments = SAVER3_MAXSEGMENTS;			GrClearWindow(state->main_window, 0);		}		get_random_point_on_screen(state, &newx, &newy, &c);		GrSetGCForeground(state->main_gc, c);		GrLine(state->main_window, state->main_gc, s->lastx, s->lasty,							newx, newy);		s->lastx = newx;		s->lasty = newy;	}}void saver4_init(nstate *state){	int i;	GR_COORD x, y;	s4state *s = my_malloc(sizeof(s4state));	state->priv = s;	s->length = 0;	for(i = 0; i < SAVER4_NUMWORMS; i++) {		s->tip = 0;		get_random_point_on_screen(state, &x, &y, &s->worms[i].colour);		s->worms[i].x = x;		s->worms[i].points[0].x = x;		s->worms[i].y = y;		s->worms[i].points[0].y = y;		}	state->animate_interval = SAVER4_DELAY;}void saver4_exposure(nstate *state){	int i;	s4state *s = state->priv;	GrClearWindow(state->main_window, 0);	if(!s->length) return;	for(i = 0; i < SAVER4_NUMWORMS; i++) {		GrSetGCForeground(state->main_gc, s->worms[i].colour);		GrPoints(state->main_window, state->main_gc, s->length,							s->worms[i].points);	}}void saver4_get_new_worm_position(nstate *state, int worm, int newtip){	s4state *s = state->priv;	s4worm *w = &s->worms[worm];	w->d += FRANDRANGE(-SAVER4_MAXROTATION, SAVER4_MAXROTATION);	w->x += cos(w->d) * SAVER4_VELOCITY;	w->y += sin(w->d) * SAVER4_VELOCITY;	if(w->x < 0) w->x = state->si.cols - 1 + w->x;	if(w->x >= state->si.cols) w->x -= state->si.cols;	if(w->y < 0) w->y = state->si.rows - 1 + w->y;	if(w->y >= state->si.rows) w->y -= state->si.rows;	w->points[newtip].x = (GR_COORD)w->x;	w->points[newtip].y = (GR_COORD)w->y;}void saver4_animate(nstate *state){	int i, tail, newtip;	s4state *s = state->priv;	if(s->length == SAVER4_WORMLENGTH) tail = s->tip + 1;	else tail = 0;	if(tail == SAVER4_WORMLENGTH) tail = 0;	newtip = s->tip + 1;	if(newtip == SAVER4_WORMLENGTH) newtip = 0;		for(i = 0; i < SAVER4_NUMWORMS; i++) {		GrSetGCForeground(state->main_gc, GR_COLOR_BLACK);		GrFillRect(state->main_window, state->main_gc,					s->worms[i].points[tail].x,					s->worms[i].points[tail].y,					SAVER4_WORMTHICKNESS,					SAVER4_WORMTHICKNESS);		saver4_get_new_worm_position(state, i, newtip);		GrSetGCForeground(state->main_gc, s->worms[i].colour);		GrFillRect(state->main_window, state->main_gc,					s->worms[i].points[newtip].x,					s->worms[i].points[newtip].y,					SAVER4_WORMTHICKNESS,					SAVER4_WORMTHICKNESS);	}	s->tip = newtip;	if(s->length < SAVER4_WORMLENGTH) s->length++;}void saver5_init(nstate *state){	int i;	s5state *s = my_malloc(sizeof(s5state));	state->priv = s;	s->numstars = 0;	for(i = 0; i < SAVER5_NUMSTARS; i++) {		s->stars[i].angle = FRANDRANGE(0, (2 * M_PI));		s->stars[i].pos = 1;	}	state->animate_interval = SAVER5_DELAY;}int saver5_drawstar(nstate *state, s5state *s, int star, int delete){	int opp, adj;	GR_COORD x, y;	if(delete) GrSetGCForeground(state->main_gc, GR_COLOR_BLACK);	else GrSetGCForeground(state->main_gc, GR_COLOR_WHITE);	opp = (int)(sin(s->stars[star].angle) * s->stars[star].pos);	adj = (int)(cos(s->stars[star].angle) * s->stars[star].pos);	x = (state->si.cols / 2) + adj;	y = (state->si.rows / 2) + opp;	if((x < 0) || (y < 0) || (x >= state->si.cols) || (y >= state->si.rows))		return 1;	GrPoint(state->main_window, state->main_gc, x, y);	return 0;}void saver5_exposure(nstate *state){	int i;	s5state *s = state->priv;	GrClearWindow(state->main_window, 0);	for(i = 0; i < SAVER5_NUMSTARS; i++) {		saver5_drawstar(state, s, i, 0);	}}void saver5_animate(nstate *state){	int i;	double position, scale, increment;	s5state *s = state->priv;	if(s->numstars < SAVER5_NUMSTARS) {		s->numstars += SAVER5_STARS_INCREMENT;		if(s->numstars > SAVER5_NUMSTARS)			s->numstars = SAVER5_NUMSTARS;	}	for(i = 0; i < s->numstars; i++) {		saver5_drawstar(state, s, i, 1);		position = (double)s->stars[i].pos /				(double)(state->si.cols / 2);		scale = sin((position * M_PI_2) + M_PI + M_PI_2) + 1.0;		increment = (scale * SAVER5_STARS_ACCEL_RATE) + 1;		s->stars[i].pos += (int) increment;		if(saver5_drawstar(state, s, i, 0)) {			s->stars[i].pos = 1;			s->stars[i].angle = FRANDRANGE(0, (2 * M_PI));			saver5_drawstar(state, s, i, 0);		}	}}void saver6_init(nstate *state){	int i, n;	s6state *s = my_malloc(sizeof(s6state));	state->priv = s;	s->new_bolt_time = 0;	for(i = 0; i < SAVER6_MAXBOLTS; i++) {		s->bolts[i].duration = 0;		for(n = 0; n < SAVER6_MAXFORKS; n++) {			s->bolts[i].forks[n].valid = 0;		}	}	state->animate_interval = SAVER6_DELAY;}void saver6_drawfork(nstate *state, s6state *s, int bolt, int fork, int delete){	int i;	if(delete) GrSetGCForeground(state->main_gc, GR_COLOR_BLACK);	for(i = 0; i < SAVER6_THICKNESS; i++) {		if(!delete) {			if((i < 2) || (i >= SAVER6_THICKNESS - 2))				 GrSetGCForeground(state->main_gc,						 GR_COLOR_CORNFLOWERBLUE);			else GrSetGCForeground(state->main_gc, GR_COLOR_WHITE);		}		GrPoly(state->main_window, state->main_gc,				s->bolts[bolt].forks[fork].valid,				s->bolts[bolt].forks[fork].vertices[i]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -