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

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