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

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