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

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