1 /* Synaesthesia - program to display sound graphically
2 Copyright (C) 1997 Paul Francis Harrison
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 675 Mass Ave, Cambridge, MA 02139, USA.
18 The author may be contacted at:
19 pfh@yoyo.cc.monash.edu.au
21 27 Bond St., Mt. Waverley, 3149, Melbourne, Australia
35 static SDL_Surface
*surface
;
36 static SDL_Color sdlPalette
[256];
37 static bool fullscreen
;
38 static int scaling
; //currently only supports 1, 2
40 static void createSurface() {
41 Uint32 videoflags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
|
42 SDL_RESIZABLE
| (fullscreen
?SDL_FULLSCREEN
:0);
45 /* Get available fullscreen modes */
46 SDL_Rect
**modes
= SDL_ListModes(0, videoflags
);
50 if(modes
== (SDL_Rect
**)-1){
51 /* All resolutions ok */
54 /* Is there a resolution that will fit the display nicely */
55 for(int i
=0;modes
[i
];i
++)
56 if (modes
[i
]->w
< outWidth
*2 || modes
[i
]->h
< outHeight
*2)
62 surface
= SDL_SetVideoMode(outWidth
*scaling
, outHeight
*scaling
, 8, videoflags
);
63 SDL_SetColors(surface
, sdlPalette
, 0, 256);
66 error("setting video mode");
69 void SdlScreen::setPalette(unsigned char *palette
) {
70 for(int i
=0;i
<256;i
++) {
71 sdlPalette
[i
].r
= palette
[i
*3+0];
72 sdlPalette
[i
].g
= palette
[i
*3+1];
73 sdlPalette
[i
].b
= palette
[i
*3+2];
76 SDL_SetColors(surface
, sdlPalette
, 0, 256);
79 bool SdlScreen::init(int xHint
,int yHint
,int width
,int height
,bool fullscreen
)
85 ::fullscreen
= fullscreen
;
88 if ( SDL_Init(SDL_INIT_VIDEO
) < 0 ) {
90 printf(str
, "Could not initialize SDL library: %s\n",SDL_GetError());
94 SDL_WM_SetCaption("Synaesthesia","synaesthesia");
104 void SdlScreen::end(void) {
108 void SdlScreen::toggleFullScreen(void) {
109 fullscreen
= !fullscreen
;
113 void SdlScreen::inputUpdate(int &mouseX
,int &mouseY
,int &mouseButtons
,char &keyHit
) {
115 bool resized
= false;
119 while ( SDL_PollEvent(&event
) > 0 ) {
120 switch (event
.type
) {
121 case SDL_MOUSEBUTTONUP
:
122 case SDL_MOUSEBUTTONDOWN
:
123 if ( event
.button
.state
== SDL_PRESSED
)
124 mouseButtons
|= 1 << event
.button
.button
;
126 mouseButtons
&= ~( 1 << event
.button
.button
);
127 mouseX
= event
.button
.x
/ scaling
;
128 mouseY
= event
.button
.y
/ scaling
;
130 case SDL_MOUSEMOTION
:
131 mouseX
= event
.motion
.x
/ scaling
;
132 mouseY
= event
.motion
.y
/ scaling
;
134 case SDL_ACTIVEEVENT
:
135 /* Lost focus, hide mouse */
136 if (!event
.active
.gain
) {
141 ///* Ignore key releases */
142 //if ( event.key.state == SDL_RELEASED ) {
145 /* Ignore ALT-TAB for windows */
146 if ( (event
.key
.keysym
.sym
== SDLK_LALT
) ||
147 (event
.key
.keysym
.sym
== SDLK_TAB
) ) {
151 if (event
.key
.keysym
.unicode
> 255)
154 keyHit
= event
.key
.keysym
.unicode
;
156 case SDL_VIDEORESIZE
:
157 event
.resize
.w
&= ~3;
158 allocOutput(event
.resize
.w
,event
.resize
.h
);
170 void SdlScreen::show(void) {
171 attempt(SDL_LockSurface(surface
),"locking screen for output.");
174 register uint32_t *ptr2
= (uint32_t*)output
;
175 uint32_t *ptr1
= (uint32_t*)( surface
->pixels
);
176 int i
= outWidth
*outHeight
/sizeof(*ptr2
);
179 // Asger Alstrup Nielsen's (alstrup@diku.dk)
180 // optimized 32 bit screen loop
181 register unsigned int const r1
= *(ptr2
++);
182 register unsigned int const r2
= *(ptr2
++);
186 register unsigned int const v
=
187 ((r1
& 0x000000f0ul
) >> 4)
188 | ((r1
& 0x0000f000ul
) >> 8)
189 | ((r1
& 0x00f00000ul
) >> 12)
190 | ((r1
& 0xf0000000ul
) >> 16);
192 ( ((r2
& 0x000000f0ul
) << 12)
193 | ((r2
& 0x0000f000ul
) << 8)
194 | ((r2
& 0x00f00000ul
) << 4)
195 | ((r2
& 0xf0000000ul
)));
197 register unsigned int const v
=
198 ((r2
& 0x000000f0ul
) >> 4)
199 | ((r2
& 0x0000f000ul
) >> 8)
200 | ((r2
& 0x00f00000ul
) >> 12)
201 | ((r2
& 0xf0000000ul
) >> 16);
203 ( ((r1
& 0x000000f0ul
) << 12)
204 | ((r1
& 0x0000f000ul
) << 8)
205 | ((r1
& 0x00f00000ul
) << 4)
206 | ((r1
& 0xf0000000ul
)));
212 // SDL has no standard image scaling routine (!)
213 uint8_t *pixels
= (uint8_t*)(surface
->pixels
);
214 for(int y
=0;y
<outHeight
;y
++) {
215 uint32_t *p1
= (uint32_t*)(output
+y
*outWidth
*2);
216 uint32_t *p2
= (uint32_t*)(pixels
+y
*2*outWidth
*2);
217 uint32_t *p3
= (uint32_t*)(pixels
+(y
*2+1)*outWidth
*2);
218 for(int x
=0;x
<outWidth
;x
+=2) {
219 uint32_t v
= *(p1
++);
220 v
= ((v
&0x000000f0) >> 4)
221 | ((v
&0x0000f000) >> 8)
222 | ((v
&0x00f00000) >> 4)
223 | ((v
&0xf0000000) >> 8);
231 SDL_UnlockSurface(surface
);
233 //SDL_UpdateRect(surface, 0, 0, 0, 0);