blob: ec3b2bfa77480f62ff9819ed943a0ac138734c09 [file] [log] [blame]
adamdunkelsbc70b4d2003-04-24 17:10:32 +00001/*
adamdunkels0a08fda2004-07-04 18:33:07 +00002 * Copyright (c) 2002-2004, Adam Dunkels.
adamdunkelsbc70b4d2003-04-24 17:10:32 +00003 * 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.
adamdunkels0a08fda2004-07-04 18:33:07 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkelsbc70b4d2003-04-24 17:10:32 +000015 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This file is part of the Contiki desktop environment
31 *
oliverschmidt60720ec2005-05-11 22:23:08 +000032 * $Id: ssfire.c,v 1.7 2005/05/11 22:23:08 oliverschmidt Exp $
adamdunkelsbc70b4d2003-04-24 17:10:32 +000033 *
34 */
35
36#include <stdlib.h>
37
38#include "ctk.h"
39#include "ctk-draw.h"
adamdunkels7f782822003-08-09 23:28:49 +000040#include "ctk-mouse.h"
adamdunkels0a08fda2004-07-04 18:33:07 +000041#include "ek.h"
adamdunkelsbc70b4d2003-04-24 17:10:32 +000042#include "loader.h"
43
adamdunkelsbc70b4d2003-04-24 17:10:32 +000044
adamdunkels0a08fda2004-07-04 18:33:07 +000045/*static DISPATCHER_SIGHANDLER(ssfire_sighandler, s, data);
adamdunkelsbc70b4d2003-04-24 17:10:32 +000046static void ssfire_idle(void);
47static struct dispatcher_proc p =
48 {DISPATCHER_PROC("Fire screensaver", ssfire_idle,
49 ssfire_sighandler,
50 NULL)};
adamdunkels0a08fda2004-07-04 18:33:07 +000051 static ek_id_t id;*/
adamdunkelsbc70b4d2003-04-24 17:10:32 +000052
adamdunkels0a08fda2004-07-04 18:33:07 +000053EK_EVENTHANDLER(ssfire_eventhandler, ev, data);
54EK_POLLHANDLER(ssfire_pollhandler);
55EK_PROCESS(p, "Fire screensaver", EK_PRIO_LOWEST,
56 ssfire_eventhandler, ssfire_pollhandler, NULL);
57static ek_id_t id = EK_ID_NONE;
adamdunkelsbc70b4d2003-04-24 17:10:32 +000058
59static unsigned char flames[8*17];
60
adamdunkelsbc70b4d2003-04-24 17:10:32 +000061
62static const unsigned char flamecolors[16] =
oliverschmidt60720ec2005-05-11 22:23:08 +000063 {COLOR_BLACK, COLOR_BLACK, COLOR_BLACK, COLOR_RED,
64 COLOR_LIGHTRED, COLOR_YELLOW, COLOR_WHITE, COLOR_WHITE,
65 COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
66 COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE};
adamdunkelsbc70b4d2003-04-24 17:10:32 +000067
68
adamdunkelsbc70b4d2003-04-24 17:10:32 +000069/*-----------------------------------------------------------------------------------*/
adamdunkels2af72c22003-08-24 22:31:23 +000070LOADER_INIT_FUNC(ssfire_init, arg)
adamdunkelsbc70b4d2003-04-24 17:10:32 +000071{
adamdunkels2af72c22003-08-24 22:31:23 +000072 arg_free(arg);
73
adamdunkelsbc70b4d2003-04-24 17:10:32 +000074 if(id == EK_ID_NONE) {
adamdunkels0a08fda2004-07-04 18:33:07 +000075 id = ek_start(&p);
adamdunkelsbc70b4d2003-04-24 17:10:32 +000076 }
77}
78/*-----------------------------------------------------------------------------------*/
79static void
80fire_quit(void)
81{
adamdunkels0a08fda2004-07-04 18:33:07 +000082 ek_exit();
adamdunkelsbc70b4d2003-04-24 17:10:32 +000083 id = EK_ID_NONE;
84 LOADER_UNLOAD();
85}
86/*-----------------------------------------------------------------------------------*/
87static void
88fire_init(void)
89{
90 unsigned char *ptr, *cptr;
91
adamdunkels2af72c22003-08-24 22:31:23 +000092 /* Fill screen with inverted spaces. */
93 cptr = COLOR_RAM;
94 for(ptr = (unsigned char *)0x0400;
95 ptr != (unsigned char *)0x07e8;
96 ++ptr) {
97 *ptr = 0xa0;
98 *cptr++ = 0x00;
99 }
100
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000101 SID.v3.freq = 0xffff;
102 SID.v3.ctrl = 0x80;
103 SID.amp = 0;
104
105 VIC.ctrl1 = 0x1b; /* $D011 */
106 VIC.addr = 0x17; /* $D018 */
107 VIC.ctrl2 = 0xc8; /* $D016 */
108 VIC.bordercolor = 0x00; /* $D020 */
109 VIC.bgcolor0 = 0x00; /* $D021 */
110 CIA2.pra = 0x03; /* $DD00 */
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000111}
112/*-----------------------------------------------------------------------------------*/
adamdunkels0a08fda2004-07-04 18:33:07 +0000113EK_EVENTHANDLER(ssfire_eventhandler, ev, data)
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000114{
adamdunkels0a08fda2004-07-04 18:33:07 +0000115 EK_EVENTHANDLER_ARGS(ev, data);
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000116
adamdunkels0a08fda2004-07-04 18:33:07 +0000117 if(ev == EK_EVENT_INIT) {
118 ctk_mode_set(CTK_MODE_SCREENSAVER);
119 ctk_mouse_hide();
120 fire_init();
121 } else if(ev == ctk_signal_screensaver_stop ||
122 ev == EK_EVENT_REQUEST_EXIT) {
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000123 fire_quit();
124 ctk_draw_init();
adamdunkelseb4d92a2003-07-31 23:34:04 +0000125 ctk_desktop_redraw(NULL);
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000126 }
127}
128/*-----------------------------------------------------------------------------------*/
oliverschmidt60720ec2005-05-11 22:23:08 +0000129#pragma optimize(push, off)
130static void
131fire_burn(void)
132{
133 /* Calculate new flames. */
134 asm("ldy #$00");
135loop1:
136 asm("lda %v+7,y", flames);
137 asm("clc");
138 asm("adc %v+8,y", flames);
139 asm("adc %v+9,y", flames);
140 asm("adc %v+16,y", flames);
141 asm("lsr");
142 asm("lsr");
143 asm("sta %v,y", flames);
144 asm("iny");
145 asm("cpy #(8*15)");
146 asm("bne %g", loop1);
147
148 /* Fill last line with pseudo-random data from noise generator on
149 voice 3. */
150 asm("ldy #$05");
151loop2:
152 asm("ldx #$20");
153delay:
154 asm("dex");
155 asm("bne %g", delay);
156 asm("lda $D41B");
157 asm("and #$0F");
158 asm("sta %v+8*15+1,y", flames);
159 asm("dey");
160 asm("bpl %g", loop2);
161}
162#pragma optimize(pop)
163/*-----------------------------------------------------------------------------------*/
adamdunkels2af72c22003-08-24 22:31:23 +0000164static unsigned char *flameptr, *colorptr1, *colorptr2;
165static unsigned char x, y;
166
adamdunkels0a08fda2004-07-04 18:33:07 +0000167EK_POLLHANDLER(ssfire_pollhandler)
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000168{
adamdunkels2af72c22003-08-24 22:31:23 +0000169
adamdunkels7f782822003-08-09 23:28:49 +0000170 if(ctk_mode_get() == CTK_MODE_SCREENSAVER) {
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000171
oliverschmidt60720ec2005-05-11 22:23:08 +0000172 fire_burn();
173
adamdunkels7f782822003-08-09 23:28:49 +0000174 /* Display flames on screen. */
175 flameptr = flames;
176 colorptr1 = COLOR_RAM + 40*10;
177 colorptr2 = colorptr1 + 0x20;
178 for(y = 0; y < 15; ++y) {
179 for(x = 0; x < 8; ++x) {
adamdunkels2af72c22003-08-24 22:31:23 +0000180 colorptr1[x] = colorptr2[x] = flamecolors[flameptr[x]];
adamdunkels7f782822003-08-09 23:28:49 +0000181 }
adamdunkels2af72c22003-08-24 22:31:23 +0000182 colorptr1 += 0x28;
183 colorptr2 += 0x28;
184 flameptr += 8;
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000185 }
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000186 }
187}
188/*-----------------------------------------------------------------------------------*/