AcesNative+Usermode - libacessnative "works" (for some definitions)
[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 #include <video.h>
15 #include <wm.h>
16 #include <string.h>
17
18 // === IMPORTS ===
19 extern int      giTerminalFD_Input;
20
21 // === PROTOTYPES ===
22 void    Video_Setup(void);
23 void    Video_SetCursorPos(short X, short Y);
24 void    Video_Update(void);
25 void    Video_FillRect(int X, int Y, int W, int H, uint32_t Color);
26
27 // === GLOBALS ===
28  int    giVideo_CursorX;
29  int    giVideo_CursorY;
30 uint32_t        *gpScreenBuffer;
31  int    giVideo_FirstDirtyLine;
32  int    giVideo_LastDirtyLine;
33
34 // === CODE ===
35 void Video_Setup(void)
36 {
37          int    tmpInt;
38         
39         // Open terminal
40         #if 0
41         giTerminalFD = _SysOpen(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
42         if( giTerminalFD == -1 )
43         {
44                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
45                 exit(-1);
46         }
47         #else
48         giTerminalFD = 1;
49         giTerminalFD_Input = 0;
50         // Check that the console is a VT
51         // - _SysIOCtl(..., 0, NULL) returns the type, which should be 2
52         tmpInt = _SysIOCtl(1, 0, NULL);
53         if( tmpInt != 2 )
54         {
55                 fprintf(stderr, "stdout is not an Acess VT, can't start (2 exp, %i got)\n", tmpInt);
56                 _SysDebug("stdout is not an Acess VT, can't start");
57                 exit(-1);
58         }
59         #endif
60         
61         // Set mode to video
62         tmpInt = TERM_MODE_FB;
63         _SysIOCtl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
64         
65         // Get dimensions
66         giScreenWidth = _SysIOCtl( giTerminalFD, TERM_IOCTL_WIDTH, NULL );
67         giScreenHeight = _SysIOCtl( giTerminalFD, TERM_IOCTL_HEIGHT, NULL );
68
69         giVideo_LastDirtyLine = giScreenHeight;
70         
71         // Force VT to be shown
72         _SysIOCtl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
73         
74         // Create local framebuffer (back buffer)
75         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
76
77         // Set cursor position and bitmap
78         _SysIOCtl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
79         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
80 }
81
82 void Video_Update(void)
83 {
84          int    ofs = giVideo_FirstDirtyLine*giScreenWidth;
85          int    size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
86         
87         if( giVideo_LastDirtyLine == 0 )        return; 
88
89         _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
90                 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
91         _SysSeek(giTerminalFD, ofs*4, SEEK_SET);
92         _SysDebug("Video_Update - Sending FD %i %p 0x%x", giTerminalFD, gpScreenBuffer+ofs, size*4);
93         _SysWrite(giTerminalFD, gpScreenBuffer+ofs, size*4);
94         _SysDebug("Video_Update - Done");
95         giVideo_FirstDirtyLine = 0;
96         giVideo_LastDirtyLine = 0;
97 }
98
99 void Video_SetCursorPos(short X, short Y)
100 {
101         struct {
102                 uint16_t        x;
103                 uint16_t        y;
104         } pos;
105         pos.x = giVideo_CursorX = X;
106         pos.y = giVideo_CursorY = Y;
107         _SysIOCtl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
108 }
109
110 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
111 {
112         uint32_t        *dest;
113          int    i;
114         
115         if(X < 0 || Y < 0)      return;
116         if(W >= giScreenWidth)  return;
117         if(H >= giScreenHeight) return;
118         if(X + W >= giScreenWidth)      W = giScreenWidth - W;
119         if(Y + H >= giScreenHeight)     W = giScreenHeight - H;
120         
121         dest = gpScreenBuffer + Y * giScreenWidth + X;
122         while(H --)
123         {
124                 for( i = W; i --; dest ++ )     *dest = Colour;
125                 dest += giScreenWidth - W;
126         }
127 }
128
129 /**
130  * \brief Blit an entire buffer to the screen
131  * \note Assumes Pitch = 4*W
132  */
133 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
134 {
135         uint32_t        *buf;
136         short   drawW = W;
137
138         if( DstX >= giScreenWidth)      return ;
139         if( DstY >= giScreenHeight)     return ;
140         // Drawing oob to left/top, clip
141         if( DstX < 0 ) {
142                 Source += -DstX;
143                 drawW -= -DstX;
144                 DstX = 0;
145         }
146         if( DstY < 0 ) {
147                 Source += (-DstY)*W;
148                 H -= -DstY;
149                 DstY = 0;
150         }
151         if( drawW < 0 ) return ;
152         // Drawing oob to the right, clip
153         if( DstX + drawW > giScreenWidth ) {
154                 //int oldw = drawW;
155                 drawW = giScreenWidth - DstX;
156         }
157         if( DstY + H > giScreenHeight )
158                 H = giScreenHeight - DstY;
159
160         if( W <= 0 || H <= 0 )  return;
161
162         if( DstY < giVideo_FirstDirtyLine )
163                 giVideo_FirstDirtyLine = DstY;
164         if( DstY + H > giVideo_LastDirtyLine )
165                 giVideo_LastDirtyLine = DstY + H;
166         
167         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
168         if(drawW != giScreenWidth || W != giScreenWidth)
169         {
170                 while( H -- )
171                 {
172                         memcpy(buf, Source, drawW*4);
173                         Source += W;
174                         buf += giScreenWidth;
175                 }
176         }
177         else
178         {
179                 // Only valid if copying full scanlines on both ends
180                 memcpy(buf, Source, giScreenWidth*H*4);
181         }
182 }
183
184 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
185 {
186         uint16_t        ao,ro,go,bo;
187         uint16_t        an,rn,gn,bn;
188         if( _alpha == 0 )       return _orig;
189         if( _alpha == 255 )     return _new;
190         
191         ao = (_orig >> 24) & 0xFF;
192         ro = (_orig >> 16) & 0xFF;
193         go = (_orig >>  8) & 0xFF;
194         bo = (_orig >>  0) & 0xFF;
195         
196         an = (_new >> 24) & 0xFF;
197         rn = (_new >> 16) & 0xFF;
198         gn = (_new >>  8) & 0xFF;
199         bn = (_new >>  0) & 0xFF;
200
201         if( _alpha == 0x80 ) {
202                 ao = (ao + an) / 2;
203                 ro = (ro + rn) / 2;
204                 go = (go + gn) / 2;
205                 bo = (bo + bn) / 2;
206         }
207         else {
208                 ao = ao*(255-_alpha) + an*_alpha;
209                 ro = ro*(255-_alpha) + rn*_alpha;
210                 go = go*(255-_alpha) + gn*_alpha;
211                 bo = bo*(255-_alpha) + bn*_alpha;
212                 ao /= 255*2;
213                 ro /= 255*2;
214                 go /= 255*2;
215                 bo /= 255*2;
216         }
217
218         return (ao << 24) | (ro << 16) | (go << 8) | bo;
219 }
220

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