[Git]Ignore work files.
[synaesthesia.git] / rawwrap.cc
blob1ef3325cc44a4fa75693fe27e00a8ee017e5e241
1 /* Synaesthesia - program to display sound graphically
2 Raw output module.
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
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <sys/time.h>
26 #include <time.h>
27 #include "syna.h"
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);
37 outWidth = widthHint;
38 outHeight = heightHint;
39 fbuf = (uint32_t *)malloc(widthHint * heightHint * 4);
41 gettimeofday(&frametime, 0);
42 framelen.tv_sec = 0;
43 framelen.tv_usec = 1000000/fps;
45 return true;
48 void RawScreen::setPalette(unsigned char *palette) {
49 for(int i=0;i<256;i++) {
50 raw_palette[i] = palette[i*3+0]
51 | palette[i*3+1] << 8
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) {
61 keyHit = 0;
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
70 do {
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
80 //if (r1 || r2) {
81 #ifdef LITTLEENDIAN
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]];
97 #else
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]];
113 #endif
114 //} else {
115 // ptr1++;
117 } while (--i);
119 static struct timespec ts = {0, 10000};
120 struct timeval tod;
121 gettimeofday(&tod, 0);
123 while (timercmp(&tod, &frametime, <)) {
124 gettimeofday(&tod, 0);
125 nanosleep(&ts, 0);
128 int drop = 0;
129 while(timercmp(&tod, &frametime, >)) {
130 framecount++;
131 write(1, fbuf, outWidth * outHeight * 4);
132 timeradd(&frametime, &framelen, &frametime);
133 if (drop++) {
134 dropped++;