Mental note, adding resources is a good idea
[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;
108         
109         // Sanity please
110         if( !Image )
111                 return ;
112         
113         // Bounds Check
114         if( X >= giScreenWidth )        return ;
115         if( Y >= giScreenHeight )       return ;
116         
117         // Wrap to image size
118         if( W > Image->Width )  W = Image->Width;
119         if( H > Image->Height ) H = Image->Height;
120         
121         // Wrap to screen size
122         if( X + W > giScreenWidth )     W = giScreenWidth - X;
123         if( Y + H > giScreenHeight )    H = giScreenHeight - Y;
124         
125         // Do the render
126         data = Image->Data;
127         switch( Image->Format )
128         {
129         case IMGFMT_BGRA:
130                 for( y = 0; y < H; y ++ )
131                 {
132                          int    r, g, b, a;     // New
133                          int    or, og, ob;     // Original
134                         for( x = 0; x < W; x ++ ) {
135                                 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
136                                 if( a == 0 )    continue;       // 100% transparent
137                                 ob = buf[x*4+0]; og = buf[x*4+1]; or = buf[x*4+2];
138                                 // Handle Alpha
139                                 switch(a)
140                                 {
141                                 // Transparent: Handled above
142                                 // Solid
143                                 case 0xFF:      break;
144                                 // Half
145                                 case 0x80:
146                                         r = (or + r) / 2;
147                                         g = (og + g) / 2;
148                                         b = (ob + b) / 2;
149                                         break;
150                                 // General
151                                 default:
152                                         r = (or * (255-a) + r * a) / 255;
153                                         g = (og * (255-a) + g * a) / 255;
154                                         b = (ob * (255-a) + b * a) / 255;
155                                         break;
156                                 }
157                                 buf[x*4+0] = b; buf[x*4+1] = g; buf[x*4+2] = r;
158                         }
159                         data += Image->Width * 4;
160                         buf += giScreenWidth * 4;
161                 }
162                 break;
163         
164         // RGB
165         case IMGFMT_RGB:
166                 for( y = 0; y < H; y ++ )
167                 {
168                         for( x = 0; x < W; x ++ ) {
169                                 buf[x*4+0] = data[x*3+2];       // Blue
170                                 buf[x*4+1] = data[x*3+1];       // Green
171                                 buf[x*4+2] = data[x*3+0];       // Red
172                         }
173                         data += W * 3;
174                         buf += giScreenWidth * 4;
175                 }
176                 break;
177         }
178 }

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