source: trunk/tools/neercs/old/screensaver.c @ 1656

Last change on this file since 1656 was 1656, checked in by sam, 7 years ago

neercs: start porting the old code to Windows; this breaks the Visual Studio
build until further fixes are made.

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