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

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