[Git]Ignore work files.
[synaesthesia.git] / svgawrap.cc
blob1acfde9bf9957b99ff441070e98e06ca2868e3d6
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
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <termios.h>
27 #include <stdint.h>
28 #include "syna.h"
30 #if HAVE_LIBVGA
32 #include <vga.h>
33 #include <vgamouse.h>
35 static unsigned char *scr;
36 static int keyboardDevice;
38 static void setOnePalette(int i,int r,int g,int b) {
39 vga_setpalette(i,r/4,g/4,b/4);
42 bool SvgaScreen::init(int xHint,int yHint,int widthHint,int heightHint,bool fullscreen) {
43 attempt(vga_init(),"initializing svgalib");
44 if (!vga_hasmode(G320x200x256))
45 error("requesting 320x200 graphics mode");
47 attempt(vga_setmode(G320x200x256),"entering 320x200 graphics mode");
49 scr = vga_getgraphmem();
50 outWidth = 320;
51 outHeight = 200;
53 attemptNoDie(mouse_init("/dev/mouse",
54 vga_getmousetype(),MOUSE_DEFAULTSAMPLERATE),"initializing mouse");
55 mouse_setxrange(-1,321);
56 mouse_setyrange(-1,201);
57 mouse_setposition(160,34);
58 //mouse_setscale(32);
60 /*#define BOUND(x) ((x) > 255 ? 255 : (x))
61 #define PEAKIFY(x) BOUND((x) - (x)*(255-(x))/255/2)
62 int i;
63 for(i=0;i<256;i++)
64 setPalette(i,PEAKIFY((i&15*16)),
65 PEAKIFY((i&15)*16+(i&15*16)/4),
66 PEAKIFY((i&15)*16));
69 /* Get keyboard input descriptor */
70 if (!isatty(0)) {
71 char *tty;
73 //Ok, we're getting piped input, so we can't use stdin.
74 //Find out where stdout is going and read from it.
75 tty = ttyname(1);
77 keyboardDevice = -1;
79 if (tty) {
80 keyboardDevice = open(tty, O_RDONLY);
82 if (keyboardDevice != -1) {
83 termios term;
84 tcgetattr(keyboardDevice, &term);
85 cfmakeraw(&term);
86 tcsetattr(keyboardDevice, TCSANOW, &term);
89 } else
90 keyboardDevice = 0;
92 if (keyboardDevice != -1)
93 fcntl(keyboardDevice,F_SETFL,O_NONBLOCK);
95 return true;
98 void SvgaScreen::setPalette(unsigned char *palette) {
99 int i;
100 for(i=0;i<256;i++)
101 setOnePalette(i,palette[i*3],palette[i*3+1],palette[i*3+2]);
104 void SvgaScreen::end() {
105 if (keyboardDevice > 0)
106 close(keyboardDevice);
108 mouse_close();
109 vga_setmode(TEXT);
112 void SvgaScreen::inputUpdate(int &mouseX,int &mouseY,int &mouseButtons,char &keyHit) {
113 mouse_update();
114 mouseX = mouse_getx();
115 mouseY = mouse_gety();
116 mouseButtons = mouse_getbutton();
118 if (keyboardDevice == -1 || read(keyboardDevice, &keyHit, 1) != 1)
119 keyHit = 0;
122 void SvgaScreen::show(void) {
123 register uint32_t *ptr2 = (uint32_t*)output;
124 uint32_t *ptr1 = (uint32_t*)scr;
125 int i = 320*200/sizeof(uint32_t);
126 // Asger Alstrup Nielsen's (alstrup@diku.dk)
127 // optimized 32 bit screen loop
128 do {
129 //Original bytewize version:
130 //unsigned char v = (*(ptr2++)&15*16);
131 //*(ptr1++) = v|(*(ptr2++)>>4);
132 register uint32_t const r1 = *(ptr2++);
133 register uint32_t const r2 = *(ptr2++);
135 //Fade will continue even after value > 16
136 //thus black pixel will be written when values just > 0
137 //thus no need to write true black
138 //if (r1 || r2) {
139 #ifdef LITTLEENDIAN
140 register uint32_t const v =
141 ((r1 & 0x000000f0ul) >> 4)
142 | ((r1 & 0x0000f000ul) >> 8)
143 | ((r1 & 0x00f00000ul) >> 12)
144 | ((r1 & 0xf0000000ul) >> 16);
145 *(ptr1++) = v |
146 ( ((r2 & 0x000000f0ul) << 12)
147 | ((r2 & 0x0000f000ul) << 8)
148 | ((r2 & 0x00f00000ul) << 4)
149 | ((r2 & 0xf0000000ul)));
150 #else
151 register uint32_t const v =
152 ((r2 & 0x000000f0ul) >> 4)
153 | ((r2 & 0x0000f000ul) >> 8)
154 | ((r2 & 0x00f00000ul) >> 12)
155 | ((r2 & 0xf0000000ul) >> 16);
156 *(ptr1++) = v |
157 ( ((r1 & 0x000000f0ul) << 12)
158 | ((r1 & 0x0000f000ul) << 8)
159 | ((r1 & 0x00f00000ul) << 4)
160 | ((r1 & 0xf0000000ul)));
161 #endif
162 //} else {
163 // ptr1++;
164 //}
165 } while (--i);
168 #endif