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

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