comparison src/rovascope/paranormal.c @ 408:290588854a9d trunk

[svn] - rovascope -- a variant of the paranormal visualization engine that is uses random data instead of presets.
author nenolod
date Sat, 06 Jan 2007 01:57:34 -0800
parents src/paranormal/paranormal.c@b185ed2f8fa2
children f305ebcc8136
comparison
equal deleted inserted replaced
407:3c0cb7e84e0d 408:290588854a9d
1 /* FIXME: add fullscreen / screenshots */
2
3 #ifdef HAVE_CONFIG_H
4 # include <config.h>
5 #endif
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <math.h>
11
12 #include <SDL.h>
13
14 #include <gtk/gtk.h>
15 #include <gdk/gdk.h>
16 #include <gdk/gdkx.h>
17
18 #include "paranormal.h"
19 #include "actuators.h"
20
21 /* SDL stuffs */
22 SDL_Surface *screen;
23
24 /* Globals */
25 struct pn_rc *pn_rc;
26 struct pn_image_data *pn_image_data;
27 struct pn_sound_data *pn_sound_data;
28
29 /* Trig Pre-Computes */
30 float sin_val[360];
31 float cos_val[360];
32
33 gboolean pn_new_beat;
34
35 extern SDL_mutex *config_mutex;
36
37 /* **************** drawing doodads **************** */
38
39 static void
40 blit_to_screen (void)
41 {
42 int j;
43
44 SDL_LockSurface (screen);
45
46 /* FIXME: add scaling support */
47
48 SDL_SetPalette (screen, SDL_LOGPAL|SDL_PHYSPAL,
49 (SDL_Color*)pn_image_data->cmap, 0, 256);
50 SDL_SetAlpha (screen, 0, 255);
51
52 for (j=0; j<pn_image_data->height; j++)
53 memcpy (screen->pixels + j*screen->pitch,
54 pn_image_data->surface[0] + j*pn_image_data->width,
55 pn_image_data->width);
56
57
58 SDL_UnlockSurface (screen);
59
60 SDL_UpdateRect (screen, 0, 0, 0, 0);
61 }
62
63 static void
64 resize_video (guint w, guint h)
65 {
66 pn_image_data->width = w;
67 pn_image_data->height = h;
68
69 if (pn_image_data->surface[0])
70 g_free (pn_image_data->surface[0]);
71 if (pn_image_data->surface[1])
72 g_free (pn_image_data->surface[1]);
73
74 pn_image_data->surface[0] = g_malloc0 (w * h);
75 pn_image_data->surface[1] = g_malloc0 (w * h);
76
77 screen = SDL_SetVideoMode (w, h, 8, SDL_HWSURFACE |
78 SDL_HWPALETTE | SDL_RESIZABLE);
79 if (! screen)
80 pn_fatal_error ("Unable to create a new SDL window: %s",
81 SDL_GetError ());
82 }
83
84 static void
85 take_screenshot (void)
86 {
87 char fname[32];
88 struct stat buf;
89 int i=0;
90
91 do
92 sprintf (fname, "pn_%05d.bmp", ++i);
93 while (stat (fname, &buf) == 0);
94
95 SDL_SaveBMP (screen, fname);
96 }
97
98 /* FIXME: This should resize the video to a user-set
99 fullscreen res */
100 static void
101 toggle_fullscreen (void)
102 {
103 SDL_WM_ToggleFullScreen (screen);
104 if (SDL_ShowCursor (SDL_QUERY) == SDL_ENABLE)
105 SDL_ShowCursor (SDL_DISABLE);
106 else
107 SDL_ShowCursor (SDL_ENABLE);
108 }
109
110 /* **************** basic renderer management **************** */
111 void
112 pn_init (void)
113 {
114 int i;
115 #ifdef FULLSCREEN_HACK
116 char SDL_windowhack[32];
117 GdkScreen *screen;
118 #endif
119
120 pn_sound_data = g_new0 (struct pn_sound_data, 1);
121 pn_image_data = g_new0 (struct pn_image_data, 1);
122
123 #ifdef FULLSCREEN_HACK
124 screen = gdk_screen_get_default();
125 sprintf(SDL_windowhack,"SDL_WINDOWID=%d",
126 GDK_WINDOW_XWINDOW(gdk_screen_get_root_window(screen)));
127 putenv(SDL_windowhack);
128 #endif
129
130 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
131 pn_fatal_error ("Unable to initialize SDL: %s", SDL_GetError ());
132
133 #ifndef FULLSCREEN_HACK
134 resize_video (640, 360);
135 #else
136 resize_video (1280, 1024);
137 #endif
138
139 SDL_WM_SetCaption ("Rovascope " VERSION, PACKAGE);
140
141 for(i=0; i<360; i++)
142 {
143 sin_val[i] = sin(i*(M_PI/180.0));
144 cos_val[i] = cos(i*(M_PI/180.0));
145 }
146 }
147
148 void
149 pn_cleanup (void)
150 {
151 SDL_FreeSurface (screen);
152 SDL_Quit ();
153
154
155 if (pn_image_data)
156 {
157 if (pn_image_data->surface[0])
158 g_free (pn_image_data->surface[0]);
159 if (pn_image_data->surface[1])
160 g_free (pn_image_data->surface[1]);
161 g_free (pn_image_data);
162 }
163 if (pn_sound_data)
164 g_free (pn_sound_data);
165 }
166
167 /* Renders one frame and handles the SDL window */
168 void
169 pn_render (void)
170 {
171 SDL_Event event;
172
173 /* Handle window events */
174 while (SDL_PollEvent (&event))
175 {
176 switch (event.type)
177 {
178 case SDL_QUIT:
179 pn_quit ();
180 g_assert_not_reached ();
181 case SDL_KEYDOWN:
182 switch (event.key.keysym.sym)
183 {
184 case SDLK_ESCAPE:
185 pn_quit ();
186 g_assert_not_reached ();
187 case SDLK_RETURN:
188 if (event.key.keysym.mod & (KMOD_ALT | KMOD_META))
189 toggle_fullscreen ();
190 break;
191 case SDLK_BACKQUOTE:
192 take_screenshot ();
193 break;
194 default:
195 break;
196 }
197 break;
198 case SDL_VIDEORESIZE:
199 resize_video (event.resize.w, event.resize.h);
200 break;
201 }
202 }
203
204 pn_new_beat = pn_is_new_beat();
205
206 if (pn_rc->actuator)
207 {
208 exec_actuator (pn_rc->actuator);
209 blit_to_screen ();
210 }
211
212 if (pn_new_beat && (rand() % 4 == 0))
213 {
214 struct pn_actuator *a;
215 GSList *list = *(GSList **)pn_rc->actuator->data;
216
217 container_remove_actuator (pn_rc->actuator, list->data);
218
219 SDL_mutexP(config_mutex);
220 a = rovascope_get_random_actuator();
221 container_add_actuator (pn_rc->actuator, a);
222 SDL_mutexV(config_mutex);
223 }
224 }
225
226 /* this MUST be called if a builtin's output is to surface[1]
227 (by the builtin, after it is done) */
228 void
229 pn_swap_surfaces (void)
230 {
231 guchar *tmp = pn_image_data->surface[0];
232 pn_image_data->surface[0] = pn_image_data->surface[1];
233 pn_image_data->surface[1] = tmp;
234 }