Usermode/AxWin3 - Added window invalidation, sub-windows render now
[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 #include <stdio.h>
14 #include <video.h>
15
16 // === PROTOTYPES ===
17 void    Video_Setup(void);
18 void    Video_SetCursorPos(short X, short Y);
19 void    Video_Update(void);
20 void    Video_FillRect(short X, short Y, short W, short H, uint32_t Color);
21 void    Video_DrawRect(short X, short Y, short W, short H, uint32_t Color);
22
23 // === GLOBALS ===
24  int    giVideo_CursorX;
25  int    giVideo_CursorY;
26 uint32_t        *gpScreenBuffer;
27
28 // === CODE ===
29 void Video_Setup(void)
30 {
31          int    tmpInt;
32         
33         // Open terminal
34         giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
35         if( giTerminalFD == -1 )
36         {
37                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
38                 exit(-1);
39         }
40         
41         // Set width
42         tmpInt = giScreenWidth;
43         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, &tmpInt );
44         if(tmpInt != giScreenWidth)
45         {
46                 fprintf(stderr, "Warning: Selected width (%i) is invalid, clipped to %i\n",
47                         giScreenWidth, tmpInt);
48                 giScreenWidth = tmpInt;
49         }
50         
51         // Set height
52         tmpInt = giScreenHeight;
53         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, &tmpInt );
54         if(tmpInt != giScreenHeight)
55         {
56                 fprintf(stderr, "Warning: Selected height (%i) is invalid, clipped to %i\n",
57                         giScreenHeight, tmpInt);
58                 giScreenHeight = tmpInt;
59         }
60         
61         // Set mode to video
62         tmpInt = TERM_MODE_FB;
63         ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
64         
65         // Force VT to be shown
66         ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
67         
68         // Create local framebuffer (back buffer)
69         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
70         Video_FillRect(0, 0, giScreenWidth, giScreenHeight, 0x8080FF);
71
72         // Set cursor position and bitmap
73         ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
74         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
75
76         Video_Update();
77 }
78
79 void Video_Update(void)
80 {
81         _SysDebug("Video_Update - gpScreenBuffer[0] = 0x%x", gpScreenBuffer[0]);
82         seek(giTerminalFD, 0, 1);
83         write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
84         _SysDebug("Video_Update - Done");
85 }
86
87 void Video_SetCursorPos(short X, short Y)
88 {
89         struct {
90                 uint16_t        x;
91                 uint16_t        y;
92         } pos;
93         pos.x = giVideo_CursorX = X;
94         pos.y = giVideo_CursorY = Y;
95         ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
96 }
97
98 void Video_FillRect(short X, short Y, short W, short H, uint32_t Color)
99 {
100          int    i;
101         uint32_t        *buf = gpScreenBuffer + Y*giScreenWidth + X;
102         
103         _SysDebug("Video_FillRect: (X=%i, Y=%i, W=%i, H=%i, Color=%08x)",
104                 X, Y, W, H, Color);
105         
106         if(W < 0 || X < 0 || X >= giScreenWidth)        return ;
107         if(X + W > giScreenWidth)       W = giScreenWidth - X;
108         
109         if(H < 0 || Y < 0 || Y >= giScreenHeight)       return ;
110         if(Y + H > giScreenHeight)      H = giScreenHeight - Y;
111         
112         while( H -- )
113         {
114                 for( i = W; i --; )
115                         *buf++ = Color;
116                 buf += giScreenWidth - W;
117         }
118 }
119
120 void Video_DrawRect(short X, short Y, short W, short H, uint32_t Color)
121 {       
122         Video_FillRect(X, Y, W, 1, Color);
123         Video_FillRect(X, Y+H-1, W, 1, Color);
124         Video_FillRect(X, Y, 1, H, Color);
125         Video_FillRect(X+W-1, Y, 1, H, Color);
126 }
127
128 /**
129  * \brief Blit an entire buffer to the screen
130  * \note Assumes Pitch = 4*W
131  */
132 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
133 {
134          int    i;
135         uint32_t        *buf;
136
137         _SysDebug("Video_Blit: (%p (%i, %i) %ix%i)", Source, DstX, DstY, W, H);
138         
139         if( DstX >= giScreenWidth)      return ;
140         if( DstY >= giScreenHeight)     return ;
141         // TODO: Handle -ve X/Y by clipping
142         if( DstX < 0 || DstY < 0 )      return ;
143         // TODO: Handle out of bounds by clipping too
144         if( DstX + W > giScreenWidth )  return;
145         if( DstY + H > giScreenHeight )
146                 H = giScreenWidth - DstY;
147
148         if( W <= 0 || H <= 0 )  return;
149         
150         _SysDebug(" Clipped to (%i, %i) %ix%i", DstX, DstY, W, H);
151         _SysDebug(" Source[0] = 0x%x", Source[0]);
152         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
153         while( H -- )
154         {
155                 for( i = W; i --; )
156                         *buf++ = *Source++;
157                 buf += giScreenWidth - W;
158         }
159 }
160
161 /**
162  * \brief Draw an image to the screen
163  * \todo Maybe have support for an offset in the image
164  */
165 void Video_DrawImage(short X, short Y, short W, short H, tImage *Image)
166 {
167          int    x, y;
168         uint8_t *buf = (uint8_t *)(gpScreenBuffer + Y*giScreenWidth + X);
169         uint8_t *data;
170         
171         // Sanity please
172         if( !Image )
173                 return ;
174         
175         // Bounds Check
176         if( X >= giScreenWidth )        return ;
177         if( Y >= giScreenHeight )       return ;
178         
179         // Wrap to image size
180         if( W > Image->Width )  W = Image->Width;
181         if( H > Image->Height ) H = Image->Height;
182         
183         // Wrap to screen size
184         if( X + W > giScreenWidth )     W = giScreenWidth - X;
185         if( Y + H > giScreenHeight )    H = giScreenHeight - Y;
186         
187         // Do the render
188         data = Image->Data;
189         switch( Image->Format )
190         {
191         case IMGFMT_BGRA:
192                 for( y = 0; y < H; y ++ )
193                 {
194                          int    r, g, b, a;     // New
195                          int    or, og, ob;     // Original
196                         for( x = 0; x < W; x ++ )
197                         {
198                                 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
199                                 if( a == 0 )    continue;       // 100% transparent
200                                 ob = buf[x*4+0]; og = buf[x*4+1]; or = buf[x*4+2];
201                                 // Handle Alpha
202                                 switch(a)
203                                 {
204                                 // Transparent: Handled above
205                                 // Solid
206                                 case 0xFF:      break;
207                                 // Half
208                                 case 0x80:
209                                         r = (or + r) / 2;
210                                         g = (og + g) / 2;
211                                         b = (ob + b) / 2;
212                                         break;
213                                 // General
214                                 default:
215                                         r = (or * (255-a) + r * a) / 255;
216                                         g = (og * (255-a) + g * a) / 255;
217                                         b = (ob * (255-a) + b * a) / 255;
218                                         break;
219                                 }
220                                 buf[x*4+0] = b; buf[x*4+1] = g; buf[x*4+2] = r;
221                         }
222                         data += Image->Width * 4;
223                         buf += giScreenWidth * 4;
224                 }
225                 break;
226         
227         // RGB
228         case IMGFMT_RGB:
229                 for( y = 0; y < H; y ++ )
230                 {
231                         for( x = 0; x < W; x ++ )
232                         {
233                                 buf[x*4+0] = data[x*3+2];       // Blue
234                                 buf[x*4+1] = data[x*3+1];       // Green
235                                 buf[x*4+2] = data[x*3+0];       // Red
236                         }
237                         data += W * 3;
238                         buf += giScreenWidth * 4;
239                 }
240                 break;
241         default:
242                 _SysDebug("ERROR: Unknown image format %i\n", Image->Format);
243                 break;
244         }
245 }

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