blob: c162d0b5eff136520d8a01fd34295ed9a1ebf935 [file] [log] [blame]
adamdunkelsbc70b4d2003-04-24 17:10:32 +00001/*
2 * Copyright (c) 2002, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop environment
34 *
adamdunkels2af72c22003-08-24 22:31:23 +000035 * $Id: ssfire.c,v 1.4 2003/08/24 22:31:23 adamdunkels Exp $
adamdunkelsbc70b4d2003-04-24 17:10:32 +000036 *
37 */
38
39#include <stdlib.h>
40
41#include "ctk.h"
42#include "ctk-draw.h"
adamdunkels7f782822003-08-09 23:28:49 +000043#include "ctk-mouse.h"
adamdunkelsbc70b4d2003-04-24 17:10:32 +000044#include "dispatcher.h"
45#include "loader.h"
46
adamdunkelsbc70b4d2003-04-24 17:10:32 +000047
48static DISPATCHER_SIGHANDLER(ssfire_sighandler, s, data);
49static void ssfire_idle(void);
50static struct dispatcher_proc p =
51 {DISPATCHER_PROC("Fire screensaver", ssfire_idle,
52 ssfire_sighandler,
53 NULL)};
54static ek_id_t id;
55
56
57static unsigned char flames[8*17];
58
adamdunkelsbc70b4d2003-04-24 17:10:32 +000059
60static const unsigned char flamecolors[16] =
61 {COLOR_BLACK, COLOR_BLACK, COLOR_BLACK,
62 COLOR_RED, COLOR_LIGHTRED, COLOR_YELLOW, COLOR_WHITE,
63 COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
64 COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
65 COLOR_WHITE};
66
67
adamdunkels7f782822003-08-09 23:28:49 +000068static void fire_init(void);
adamdunkelsbc70b4d2003-04-24 17:10:32 +000069
70/*-----------------------------------------------------------------------------------*/
adamdunkels2af72c22003-08-24 22:31:23 +000071LOADER_INIT_FUNC(ssfire_init, arg)
adamdunkelsbc70b4d2003-04-24 17:10:32 +000072{
adamdunkels2af72c22003-08-24 22:31:23 +000073 arg_free(arg);
74
adamdunkelsbc70b4d2003-04-24 17:10:32 +000075 if(id == EK_ID_NONE) {
76 id = dispatcher_start(&p);
adamdunkelsbc70b4d2003-04-24 17:10:32 +000077 dispatcher_listen(ctk_signal_screensaver_stop);
adamdunkels7f782822003-08-09 23:28:49 +000078 ctk_mode_set(CTK_MODE_SCREENSAVER);
79 ctk_mouse_hide();
80 fire_init();
adamdunkelsbc70b4d2003-04-24 17:10:32 +000081 }
82}
83/*-----------------------------------------------------------------------------------*/
84static void
85fire_quit(void)
86{
87 dispatcher_exit(&p);
88 id = EK_ID_NONE;
89 LOADER_UNLOAD();
90}
91/*-----------------------------------------------------------------------------------*/
92static void
93fire_init(void)
94{
95 unsigned char *ptr, *cptr;
96
adamdunkels2af72c22003-08-24 22:31:23 +000097 /* Fill screen with inverted spaces. */
98 cptr = COLOR_RAM;
99 for(ptr = (unsigned char *)0x0400;
100 ptr != (unsigned char *)0x07e8;
101 ++ptr) {
102 *ptr = 0xa0;
103 *cptr++ = 0x00;
104 }
105
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000106 SID.v3.freq = 0xffff;
107 SID.v3.ctrl = 0x80;
108 SID.amp = 0;
109
110 VIC.ctrl1 = 0x1b; /* $D011 */
111 VIC.addr = 0x17; /* $D018 */
112 VIC.ctrl2 = 0xc8; /* $D016 */
113 VIC.bordercolor = 0x00; /* $D020 */
114 VIC.bgcolor0 = 0x00; /* $D021 */
115 CIA2.pra = 0x03; /* $DD00 */
116
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000117}
118/*-----------------------------------------------------------------------------------*/
119static
120DISPATCHER_SIGHANDLER(ssfire_sighandler, s, data)
121{
122 DISPATCHER_SIGHANDLER_ARGS(s, data);
123
adamdunkels7f782822003-08-09 23:28:49 +0000124 if(s == ctk_signal_screensaver_stop ||
125 s == dispatcher_signal_quit) {
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000126 fire_quit();
127 ctk_draw_init();
adamdunkelseb4d92a2003-07-31 23:34:04 +0000128 ctk_desktop_redraw(NULL);
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000129 }
130}
131/*-----------------------------------------------------------------------------------*/
adamdunkels2af72c22003-08-24 22:31:23 +0000132static unsigned char *flameptr, *colorptr1, *colorptr2;
133static unsigned char x, y;
134
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000135void
136ssfire_idle(void)
137{
adamdunkels2af72c22003-08-24 22:31:23 +0000138
adamdunkels7f782822003-08-09 23:28:49 +0000139 if(ctk_mode_get() == CTK_MODE_SCREENSAVER) {
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000140
adamdunkels7f782822003-08-09 23:28:49 +0000141 /* Calculate new flames. */
142 asm("ldx #0");
143 asm("loop:");
144 asm("lda _flames+7,x");
145 asm("clc");
146 asm("adc _flames+8,x");
147 asm("adc _flames+9,x");
148 asm("adc _flames+16,x");
149 asm("lsr");
150 asm("lsr");
151 asm("sta _flames,x");
152 asm("inx");
153 asm("cpx #(8*15)");
154 asm("bne loop");
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000155
adamdunkels7f782822003-08-09 23:28:49 +0000156 /* Fill last line with pseudo-random data from noise generator on
157 voice 3. */
158 asm("ldx #$05");
159 asm("loop2:");
160 asm("ldy #$20");
161 asm("delay:");
162 asm("dey");
163 asm("bne delay");
164 asm("lda $d41b");
165 asm("and #$0f");
166 asm("sta _flames+8*15+1,x");
167 asm("dex");
168 asm("bpl loop2");
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000169
adamdunkels7f782822003-08-09 23:28:49 +0000170 /* Display flames on screen. */
171 flameptr = flames;
172 colorptr1 = COLOR_RAM + 40*10;
173 colorptr2 = colorptr1 + 0x20;
174 for(y = 0; y < 15; ++y) {
175 for(x = 0; x < 8; ++x) {
adamdunkels2af72c22003-08-24 22:31:23 +0000176 colorptr1[x] = colorptr2[x] = flamecolors[flameptr[x]];
adamdunkels7f782822003-08-09 23:28:49 +0000177 }
adamdunkels2af72c22003-08-24 22:31:23 +0000178 colorptr1 += 0x28;
179 colorptr2 += 0x28;
180 flameptr += 8;
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000181 }
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000182
183 }
184}
185/*-----------------------------------------------------------------------------------*/
186