1 /* Synaesthesia - program to display sound graphically
3 Copyright (C) 2009 RafaĆ Rzepecki <divided.mind@gmail.com>
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 static uint32_t raw_palette
[256];
30 static uint32_t *fbuf
;
31 static unsigned int framecount
= 0, dropped
= 0;
32 static struct timeval frametime
;
33 static struct timeval framelen
;
35 bool RawScreen::init(int xHint
,int yHint
,int widthHint
,int heightHint
,bool fullscreen
) {
36 fprintf(stderr
, "Using raw video output, size %dx%d, frame size %d bytes.\n", widthHint
, heightHint
, widthHint
* heightHint
* 3);
38 outHeight
= heightHint
;
39 fbuf
= (uint32_t *)malloc(widthHint
* heightHint
* 4);
41 gettimeofday(&frametime
, 0);
43 framelen
.tv_usec
= 1000000/fps
;
48 void RawScreen::setPalette(unsigned char *palette
) {
49 for(int i
=0;i
<256;i
++) {
50 raw_palette
[i
] = palette
[i
*3+0]
52 | palette
[i
*3+2] << 16;
56 void RawScreen::end() {
57 fprintf(stderr
, "%d frames, %d dropped\n", framecount
, dropped
);
60 void RawScreen::inputUpdate(int &mouseX
,int &mouseY
,int &mouseButtons
,char &keyHit
) {
64 void RawScreen::show(void) {
65 register uint32_t *ptr2
= (uint32_t*)output
;
66 register uint32_t *ptr1
= fbuf
;
67 int i
= outHeight
* outWidth
/sizeof(uint32_t);
68 // Asger Alstrup Nielsen's (alstrup@diku.dk)
69 // optimized 32 bit screen loop
71 //Original bytewize version:
72 //unsigned char v = (*(ptr2++)&15*16);
73 //*(ptr1++) = v|(*(ptr2++)>>4);
74 register uint32_t const r1
= *(ptr2
++);
75 register uint32_t const r2
= *(ptr2
++);
77 //Fade will continue even after value > 16
78 //thus black pixel will be written when values just > 0
79 //thus no need to write true black
82 register uint32_t const v
=
83 ((r1
& 0x000000f0ul
) >> 4)
84 | ((r1
& 0x0000f000ul
) >> 8)
85 | ((r1
& 0x00f00000ul
) >> 12)
86 | ((r1
& 0xf0000000ul
) >> 16);
87 register uint32_t const p
= v
|
88 ( ((r2
& 0x000000f0ul
) << 12)
89 | ((r2
& 0x0000f000ul
) << 8)
90 | ((r2
& 0x00f00000ul
) << 4)
91 | ((r2
& 0xf0000000ul
)));
92 const unsigned char *o
= reinterpret_cast<const unsigned char *>(&p
);
93 *(ptr1
++) = raw_palette
[o
[0]];
94 *(ptr1
++) = raw_palette
[o
[1]];
95 *(ptr1
++) = raw_palette
[o
[2]];
96 *(ptr1
++) = raw_palette
[o
[3]];
98 register uint32_t const v
=
99 ((r2
& 0x000000f0ul
) >> 4)
100 | ((r2
& 0x0000f000ul
) >> 8)
101 | ((r2
& 0x00f00000ul
) >> 12)
102 | ((r2
& 0xf0000000ul
) >> 16);
103 register uint32_t const p
= v
|
104 ( ((r1
& 0x000000f0ul
) << 12)
105 | ((r1
& 0x0000f000ul
) << 8)
106 | ((r1
& 0x00f00000ul
) << 4)
107 | ((r1
& 0xf0000000ul
)));
108 const unsigned char *o
= reinterpret_cast<const unsigned char *>(&p
);
109 *(ptr1
++) = raw_palette
[o
[0]];
110 *(ptr1
++) = raw_palette
[o
[1]];
111 *(ptr1
++) = raw_palette
[o
[2]];
112 *(ptr1
++) = raw_palette
[o
[3]];
119 static struct timespec ts
= {0, 10000};
121 gettimeofday(&tod
, 0);
123 while (timercmp(&tod
, &frametime
, <)) {
124 gettimeofday(&tod
, 0);
129 while(timercmp(&tod
, &frametime
, >)) {
131 write(1, fbuf
, outWidth
* outHeight
* 4);
132 timeradd(&frametime
, &framelen
, &frametime
);