source: neercs/trunk/src/screensaver.c @ 2995

Last change on this file since 2995 was 2995, checked in by sam, 5 years ago

Port neercs to the unified libcaca 0.99.beta15 API.

File size: 4.2 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: main.c 2401 2008-06-15 12:50:12Z jylam $
8 *
9 *  This program is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16#include "config.h"
17
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <sys/types.h>
22#include <signal.h>
23#include <sys/wait.h>
24#include <errno.h>
25#include <unistd.h>
26
27#include <caca.h>
28#include <caca.h>
29
30#include "neercs.h"
31
32
33void screensaver_init(struct screen_list *screen_list)
34{
35
36    screensaver_flying_toasters_init(screen_list);
37}
38
39void screensaver_kill(struct screen_list *screen_list)
40{
41
42    screensaver_flying_toasters_kill(screen_list);
43}
44
45void draw_screensaver(struct screen_list *screen_list)
46{
47    screensaver_flying_toasters(screen_list);
48}
49
50
51
52
53/* Flying Toasters */
54
55#define COUNT 15
56#define PRECISION 100
57
58char toaster1_text[] = {
59"      __._ \n"
60"   .-'== _',\n"
61"  <|_= .-'  |\n"
62"   | --|   \\'.-_ \n"
63"   |   | \\  \" _.\n"
64"    `-_|.-\\_.-\n"};
65
66char toaster2_text[] = {
67"      __._ \n"
68"   .-'== _',\n"
69\\|_= .-'  |\n"
70"   | --|  __'-.\n"
71"   |   | ___.-\n"
72"    `-_|.-\n"
73};
74
75char toaster3_text[] = {
76"   _- __._\n"
77"  /.-'== _',_.-.\n"
78\\|_= .-'/  _.'\n"
79"   | --| / .-\n"
80"   |   |  _.|\n"
81"    `-_|.-\n"};
82
83struct flying_toaster
84{
85    int x[COUNT], y[COUNT], s[COUNT];
86    caca_canvas_t **toaster;
87};
88
89void screensaver_flying_toasters_init(struct screen_list *screen_list)
90{
91    struct flying_toaster *flying_toaster;
92    int w = caca_get_canvas_width(screen_list->cv);
93    int h = caca_get_canvas_height(screen_list->cv);
94    int i;
95
96    flying_toaster = malloc(sizeof(struct flying_toaster));
97    flying_toaster->toaster = (caca_canvas_t **)malloc(sizeof(caca_canvas_t*)*3);
98
99
100    flying_toaster->toaster[0] = caca_create_canvas(0, 0);
101    flying_toaster->toaster[1] = caca_create_canvas(0, 0);
102    flying_toaster->toaster[2] = caca_create_canvas(0, 0);
103
104
105    caca_import_memory(flying_toaster->toaster[0],
106                        toaster1_text,
107                        strlen(toaster1_text), "ansi");
108    caca_import_memory(flying_toaster->toaster[1],
109                        toaster2_text,
110                        strlen(toaster2_text), "ansi");
111    caca_import_memory(flying_toaster->toaster[2],
112                        toaster3_text,
113                        strlen(toaster3_text), "ansi");
114
115    for(i = 0; i < COUNT; i++)
116    {
117        flying_toaster->x[i] = (rand()%w) * PRECISION;
118        flying_toaster->y[i] = (rand()%h) * PRECISION;
119        flying_toaster->s[i] = (rand()%3) * PRECISION;
120    }
121
122
123    screen_list->screensaver_data = flying_toaster;
124}
125
126void screensaver_flying_toasters_kill(struct screen_list *screen_list)
127{
128    struct flying_toaster *flying_toaster = screen_list->screensaver_data;
129
130    caca_free_canvas(flying_toaster->toaster[0]);
131    caca_free_canvas(flying_toaster->toaster[1]);
132    caca_free_canvas(flying_toaster->toaster[2]);
133    free(flying_toaster->toaster);
134    free(flying_toaster);
135    screen_list->screensaver_data = NULL;
136}
137
138void screensaver_flying_toasters(struct screen_list *screen_list)
139{
140    struct flying_toaster *d = screen_list->screensaver_data;
141    int i, w, h;
142    if(!d) return;
143
144    w = caca_get_canvas_width(screen_list->cv);
145    h = caca_get_canvas_height(screen_list->cv);
146
147
148    caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLACK);
149    caca_clear_canvas(screen_list->cv);
150
151
152
153    for(i = 0; i < COUNT; i++)
154    {
155        caca_blit(screen_list->cv, d->x[i]/PRECISION, d->y[i]/PRECISION, d->toaster[d->s[i]/PRECISION], NULL);
156
157        d->x[i]-=40;
158        d->y[i]+=10;
159
160        if((d->x[i]/PRECISION) + caca_get_canvas_width(d->toaster[d->s[i]/PRECISION])<=0)
161            d->x[i] = ((rand()%w)+w) * PRECISION;
162        if((d->y[i]/PRECISION)>=h)
163            d->y[i] = ((rand()%h)-h) * PRECISION;
164
165        d->s[i] = ((d->s[i]+24)%(3*PRECISION));
166    }
167}
Note: See TracBrowser for help on using the repository browser.