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

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