Usermode/AxWin3 - Working on IPC
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / video.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  *
5  * video.c
6  * - Video methods
7  */
8 #include "common.h"
9 #include <acess/sys.h>
10 #include <acess/devices/terminal.h>
11 #include <image.h>
12 #include "resources/cursor.h"
13
14 // === PROTOTYPES ===
15 void    Video_Setup(void);
16 void    Video_SetCursorPos(short X, short Y);
17 void    Video_Update(void);
18 void    Video_FillRect(short X, short Y, short W, short H, uint32_t Color);
19 void    Video_DrawRect(short X, short Y, short W, short H, uint32_t Color);
20
21 // === GLOBALS ===
22  int    giVideo_CursorX;
23  int    giVideo_CursorY;
24
25 // === CODE ===
26 void Video_Setup(void)
27 {
28          int    tmpInt;
29         
30         // Open terminal
31         giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
32         if( giTerminalFD == -1 )
33         {
34                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
35                 exit(-1);
36         }
37         
38         // Set width
39         tmpInt = giScreenWidth;
40         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, &tmpInt );
41         if(tmpInt != giScreenWidth)
42         {
43                 fprintf(stderr, "Warning: Selected width (%i) is invalid, clipped to %i\n",
44                         giScreenWidth, tmpInt);
45                 giScreenWidth = tmpInt;
46         }
47         
48         // Set height
49         tmpInt = giScreenHeight;
50         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, &tmpInt );
51         if(tmpInt != giScreenHeight)
52         {
53                 fprintf(stderr, "Warning: Selected height (%i) is invalid, clipped to %i\n",
54                         giScreenHeight, tmpInt);
55                 giScreenHeight = tmpInt;
56         }
57         
58         // Set mode to video
59         tmpInt = TERM_MODE_FB;
60         ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
61         
62         // Force VT to be shown
63         ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
64         
65         // Create local framebuffer (back buffer)
66         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
67         memset32( gpScreenBuffer, 0x8888FF, giScreenWidth*giScreenHeight );
68
69         // Set cursor position and bitmap
70         ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
71         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
72
73         Video_Update();
74 }
75
76 void Video_Update(void)
77 {
78         //seek(giTerminalFD, 0, SEEK_SET);
79         seek(giTerminalFD, 0, 1);
80         write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
81 }
82
83 void Video_SetCursorPos(short X, short Y)
84 {
85         struct {
86                 uint16_t        x;
87                 uint16_t        y;
88         } pos;
89         pos.x = giVideo_CursorX = X;
90         pos.y = giVideo_CursorY = Y;
91         ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
92 }
93
94 void Video_FillRect(short X, short Y, short W, short H, uint32_t Color)
95 {
96         uint32_t        *buf = gpScreenBuffer + Y*giScreenWidth + X;
97         
98         _SysDebug("Video_FillRect: (X=%i, Y=%i, W=%i, H=%i, Color=%08x)",
99                 X, Y, W, H, Color);
100         
101         if(W < 0 || X < 0 || X >= giScreenWidth)        return ;
102         if(X + W > giScreenWidth)       W = giScreenWidth - X;
103         
104         if(H < 0 || Y < 0 || Y >= giScreenHeight)       return ;
105         if(Y + H > giScreenHeight)      H = giScreenHeight - Y;
106         
107         while( H -- )
108         {
109                 memset32( buf, Color, W );
110                 buf += giScreenWidth;
111         }
112 }
113
114 void Video_DrawRect(short X, short Y, short W, short H, uint32_t Color)
115 {       
116         Video_FillRect(X, Y, W, 1, Color);
117         Video_FillRect(X, Y+H-1, W, 1, Color);
118         Video_FillRect(X, Y, 1, H, Color);
119         Video_FillRect(X+W-1, Y, 1, H, Color);
120 }
121
122 /**
123  * \brief Draw an image to the screen
124  * \todo Maybe have support for an offset in the image
125  */
126 void Video_DrawImage(short X, short Y, short W, short H, tImage *Image)
127 {
128          int    x, y;
129         uint8_t *buf = (uint8_t *)(gpScreenBuffer + Y*giScreenWidth + X);
130         uint8_t *data;
131         
132         // Sanity please
133         if( !Image )
134                 return ;
135         
136         // Bounds Check
137         if( X >= giScreenWidth )        return ;
138         if( Y >= giScreenHeight )       return ;
139         
140         // Wrap to image size
141         if( W > Image->Width )  W = Image->Width;
142         if( H > Image->Height ) H = Image->Height;
143         
144         // Wrap to screen size
145         if( X + W > giScreenWidth )     W = giScreenWidth - X;
146         if( Y + H > giScreenHeight )    H = giScreenHeight - Y;
147         
148         // Do the render
149         data = Image->Data;
150         switch( Image->Format )
151         {
152         case IMGFMT_BGRA:
153                 for( y = 0; y < H; y ++ )
154                 {
155                          int    r, g, b, a;     // New
156                          int    or, og, ob;     // Original
157                         for( x = 0; x < W; x ++ )
158                         {
159                                 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
160                                 if( a == 0 )    continue;       // 100% transparent
161                                 ob = buf[x*4+0]; og = buf[x*4+1]; or = buf[x*4+2];
162                                 // Handle Alpha
163                                 switch(a)
164                                 {
165                                 // Transparent: Handled above
166                                 // Solid
167                                 case 0xFF:      break;
168                                 // Half
169                                 case 0x80:
170                                         r = (or + r) / 2;
171                                         g = (og + g) / 2;
172                                         b = (ob + b) / 2;
173                                         break;
174                                 // General
175                                 default:
176                                         r = (or * (255-a) + r * a) / 255;
177                                         g = (og * (255-a) + g * a) / 255;
178                                         b = (ob * (255-a) + b * a) / 255;
179                                         break;
180                                 }
181                                 buf[x*4+0] = b; buf[x*4+1] = g; buf[x*4+2] = r;
182                         }
183                         data += Image->Width * 4;
184                         buf += giScreenWidth * 4;
185                 }
186                 break;
187         
188         // RGB
189         case IMGFMT_RGB:
190                 for( y = 0; y < H; y ++ )
191                 {
192                         for( x = 0; x < W; x ++ )
193                         {
194                                 buf[x*4+0] = data[x*3+2];       // Blue
195                                 buf[x*4+1] = data[x*3+1];       // Green
196                                 buf[x*4+2] = data[x*3+0];       // Red
197                         }
198                         data += W * 3;
199                         buf += giScreenWidth * 4;
200                 }
201                 break;
202         default:
203                 _SysDebug("ERROR: Unknown image format %i\n", Image->Format);
204                 break;
205         }
206 }

UCC git Repository :: git.ucc.asn.au