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

Last change on this file since 4075 was 4075, checked in by Jean-Yves Lamoureux, 11 years ago
  • Removed double includes of caca.h on most of the files
  • Property svn:keywords set to Id
File size: 4.7 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: screensaver.c 4075 2009-11-30 10:34:19Z 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
29#include "neercs.h"
30
31
32void screensaver_init(struct screen_list *screen_list)
33{
34    screensaver_flying_toasters_init(screen_list);
35}
36
37void screensaver_kill(struct screen_list *screen_list)
38{
39    screensaver_flying_toasters_kill(screen_list);
40}
41
42void draw_screensaver(struct screen_list *screen_list)
43{
44    screensaver_flying_toasters(screen_list);
45}
46
47
48
49
50/* Flying Toasters */
51
52#define COUNT 15
53#define PRECISION 100
54
55char toaster_text[3][99] = { {
56 "      __._ \n"
57 "   .-'== _',\n"
58 "  <|_= .-'  |\n"
59 "   | --|   \\'.-_ \n"
60 "   |   | \\  \" _.\n"
61 "    `-_|.-\\_.-\n"},
62{
63 "      __._ \n"
64 "   .-'== _',\n"
65 "  \\|_= .-'  |\n"
66 "   | --|  __'-.\n"
67 "   |   | ___.-\n"
68 "    `-_|.-\n"},
69{
70 "   _- __._\n"
71 "  /.-'== _',_.-.\n"
72 "  \\|_= .-'/  _.'\n"
73 "   | --| / .-\n"
74 "   |   |  _.|\n"
75 "    `-_|.-\n"}
76};
77
78char toaster_mask[3][99] = { {
79                             
80 "      __._ \n"
81 "   .-'== _',\n"
82 "  <|_=X.-'XX|\n"
83 "   |X--|XXX\\'.-_ \n"
84 "   |XXX|X\\XX\"X_.\n"
85 "    `-_|.-\\_.-\n"},
86    {
87
88 "      __._ \n"
89 "   .-'== _',\n"
90 "  \\|_= .-'XX|\n"
91 "   |X--|XX__'-.\n"
92 "   |XXX|X___.-\n"
93 "    `-_|.-\n"},
94{
95 "   _- __._\n"
96 "  /.-'== _',_.-.\n"
97 "  \\|_= .-'/XX_.'\n"
98 "   |X--|X/X.-\n"
99 "   |XXX|XX_.|\n"
100 "    `-_|.-\n"}
101};
102
103struct flying_toaster
104{
105    int x[COUNT], y[COUNT], s[COUNT];
106    caca_canvas_t **toaster;
107    caca_canvas_t **mask;
108};
109
110void screensaver_flying_toasters_init(struct screen_list *screen_list)
111{
112    struct flying_toaster *flying_toaster;
113    int w = caca_get_canvas_width(screen_list->cv);
114    int h = caca_get_canvas_height(screen_list->cv);
115    int i;
116
117    flying_toaster = malloc(sizeof(struct flying_toaster));
118    flying_toaster->toaster =
119        (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3);
120    flying_toaster->mask =
121        (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3);
122
123    for (i = 0; i < 3; i++)
124    {
125        flying_toaster->toaster[i] = caca_create_canvas(0, 0);
126        flying_toaster->mask[i] = caca_create_canvas(0, 0);
127        caca_import_canvas_from_memory(flying_toaster->toaster[i],
128                                       toaster_text[i],
129                                       strlen(toaster_text[i]), "utf8");
130        caca_import_canvas_from_memory(flying_toaster->mask[i],
131                                       toaster_mask[i],
132                                       strlen(toaster_mask[i]), "utf8");
133    }
134
135    for (i = 0; i < COUNT; i++)
136    {
137        flying_toaster->x[i] = (rand() % w) * PRECISION;
138        flying_toaster->y[i] = (rand() % h) * PRECISION;
139        flying_toaster->s[i] = (rand() % 3) * PRECISION;
140    }
141
142
143    screen_list->screensaver.data = flying_toaster;
144}
145
146void screensaver_flying_toasters_kill(struct screen_list *screen_list)
147{
148    struct flying_toaster *flying_toaster = screen_list->screensaver.data;
149
150    caca_free_canvas(flying_toaster->toaster[0]);
151    caca_free_canvas(flying_toaster->toaster[1]);
152    caca_free_canvas(flying_toaster->toaster[2]);
153    free(flying_toaster->toaster);
154    free(flying_toaster);
155    screen_list->screensaver.data = NULL;
156}
157
158void screensaver_flying_toasters(struct screen_list *screen_list)
159{
160    struct flying_toaster *d = screen_list->screensaver.data;
161    int i, w, h, x, y, s;
162    if (!d)
163        return;
164
165    w = caca_get_canvas_width(screen_list->cv);
166    h = caca_get_canvas_height(screen_list->cv);
167
168    caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLACK);
169    caca_clear_canvas(screen_list->cv);
170
171    for (i = 0; i < COUNT; i++)
172    {
173        x = d->x[i] / PRECISION;
174        y = d->y[i] / PRECISION;
175        s = d->s[i] / PRECISION;
176
177        caca_blit(screen_list->cv, x, y, d->toaster[s], d->mask[s]);
178
179        d->x[i] -= 40;
180        d->y[i] += 10;
181
182        if (d->x[i] / PRECISION + caca_get_canvas_width(d->toaster[s]) <= 0)
183            d->x[i] = ((rand() % w) / 3 + w) * PRECISION;
184        if (d->y[i] / PRECISION >= h)
185            d->y[i] = ((rand() % h) / 2 - h) * PRECISION;
186
187        d->s[i] = ((d->s[i] + 24) % (3 * PRECISION));
188    }
189}
Note: See TracBrowser for help on using the repository browser.