Usermode/AxWin3 - Working on window rendering, root window renders 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         // TODO: Handle -ve X/Y by clipping
140         if( DstX < 0 || DstY < 0 )      return ;
141         // TODO: Handle out of bounds by clipping too
142         if( DstX + W > giScreenWidth )  return;
143         if( DstY + H > giScreenHeight ) return;
144
145         if( W <= 0 || H <= 0 )  return;
146         
147         _SysDebug(" Clipped to (%i, %i) %ix%i", DstX, DstY, W, H);
148         _SysDebug(" Source[0] = 0x%x", Source[0]);
149         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
150         while( H -- )
151         {
152                 for( i = W; i --; )
153                         *buf++ = *Source++;
154                 buf += giScreenWidth - W;
155         }
156 }
157
158 /**
159  * \brief Draw an image to the screen
160  * \todo Maybe have support for an offset in the image
161  */
162 void Video_DrawImage(short X, short Y, short W, short H, tImage *Image)
163 {
164          int    x, y;
165         uint8_t *buf = (uint8_t *)(gpScreenBuffer + Y*giScreenWidth + X);
166         uint8_t *data;
167         
168         // Sanity please
169         if( !Image )
170                 return ;
171         
172         // Bounds Check
173         if( X >= giScreenWidth )        return ;
174         if( Y >= giScreenHeight )       return ;
175         
176         // Wrap to image size
177         if( W > Image->Width )  W = Image->Width;
178         if( H > Image->Height ) H = Image->Height;
179         
180         // Wrap to screen size
181         if( X + W > giScreenWidth )     W = giScreenWidth - X;
182         if( Y + H > giScreenHeight )    H = giScreenHeight - Y;
183         
184         // Do the render
185         data = Image->Data;
186         switch( Image->Format )
187         {
188         case IMGFMT_BGRA:
189                 for( y = 0; y < H; y ++ )
190                 {
191                          int    r, g, b, a;     // New
192                          int    or, og, ob;     // Original
193                         for( x = 0; x < W; x ++ )
194                         {
195                                 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
196                                 if( a == 0 )    continue;       // 100% transparent
197                                 ob = buf[x*4+0]; og = buf[x*4+1]; or = buf[x*4+2];
198                                 // Handle Alpha
199                                 switch(a)
200                                 {
201                                 // Transparent: Handled above
202                                 // Solid
203                                 case 0xFF:      break;
204                                 // Half
205                                 case 0x80:
206                                         r = (or + r) / 2;
207                                         g = (og + g) / 2;
208                                         b = (ob + b) / 2;
209                                         break;
210                                 // General
211                                 default:
212                                         r = (or * (255-a) + r * a) / 255;
213                                         g = (og * (255-a) + g * a) / 255;
214                                         b = (ob * (255-a) + b * a) / 255;
215                                         break;
216                                 }
217                                 buf[x*4+0] = b; buf[x*4+1] = g; buf[x*4+2] = r;
218                         }
219                         data += Image->Width * 4;
220                         buf += giScreenWidth * 4;
221                 }
222                 break;
223         
224         // RGB
225         case IMGFMT_RGB:
226                 for( y = 0; y < H; y ++ )
227                 {
228                         for( x = 0; x < W; x ++ )
229                         {
230                                 buf[x*4+0] = data[x*3+2];       // Blue
231                                 buf[x*4+1] = data[x*3+1];       // Green
232                                 buf[x*4+2] = data[x*3+0];       // Red
233                         }
234                         data += W * 3;
235                         buf += giScreenWidth * 4;
236                 }
237                 break;
238         default:
239                 _SysDebug("ERROR: Unknown image format %i\n", Image->Format);
240                 break;
241         }
242 }

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