2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
10 #include <acess/devices/terminal.h>
12 #include "resources/cursor.h"
19 extern int giTerminalFD_Input;
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);
30 uint32_t *gpScreenBuffer;
31 int giVideo_FirstDirtyLine;
32 int giVideo_LastDirtyLine;
35 void Video_Setup(void)
41 giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
42 if( giTerminalFD == -1 )
44 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
49 giTerminalFD_Input = 0;
50 // Check that the console is a VT
51 // - ioctl(..., 0, NULL) returns the type, which should be 2
52 if( ioctl(1, 0, NULL) != 2 )
54 fprintf(stderr, "stdout is not an Acess VT, can't start");
55 _SysDebug("stdout is not an Acess VT, can't start");
61 tmpInt = TERM_MODE_FB;
62 ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
65 giScreenWidth = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, NULL );
66 giScreenHeight = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, NULL );
68 giVideo_LastDirtyLine = giScreenHeight;
70 // Force VT to be shown
71 ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
73 // Create local framebuffer (back buffer)
74 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
76 // Set cursor position and bitmap
77 ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
78 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
81 void Video_Update(void)
83 int ofs = giVideo_FirstDirtyLine*giScreenWidth;
84 int size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
86 if( giVideo_LastDirtyLine == 0 ) return;
88 _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
89 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
90 seek(giTerminalFD, ofs*4, 1);
91 _SysDebug("Video_Update - Sending");
92 write(giTerminalFD, gpScreenBuffer+ofs, size*4);
93 _SysDebug("Video_Update - Done");
94 giVideo_FirstDirtyLine = 0;
95 giVideo_LastDirtyLine = 0;
98 void Video_SetCursorPos(short X, short Y)
104 pos.x = giVideo_CursorX = X;
105 pos.y = giVideo_CursorY = Y;
106 ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
109 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
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;
120 dest = gpScreenBuffer + Y * giScreenWidth + X;
123 for( i = W; i --; dest ++ ) *dest = Colour;
124 dest += giScreenWidth - W;
129 * \brief Blit an entire buffer to the screen
130 * \note Assumes Pitch = 4*W
132 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
136 if( DstX >= giScreenWidth) return ;
137 if( DstY >= giScreenHeight) return ;
138 // TODO: Handle -ve X/Y by clipping
139 if( DstX < 0 || DstY < 0 ) return ;
140 // TODO: Handle out of bounds by clipping too
141 if( DstX + W > giScreenWidth ) return;
142 if( DstY + H > giScreenHeight )
143 H = giScreenWidth - DstY;
145 if( W <= 0 || H <= 0 ) return;
147 if( DstX < giVideo_FirstDirtyLine )
148 giVideo_FirstDirtyLine = DstY;
149 if( DstY + H > giVideo_LastDirtyLine )
150 giVideo_LastDirtyLine = DstY + H;
152 buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
153 if(W != giScreenWidth)
157 memcpy(buf, Source, W*4);
159 buf += giScreenWidth;
164 memcpy(buf, Source, giScreenWidth*H*4);
168 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
170 uint16_t ao,ro,go,bo;
171 uint16_t an,rn,gn,bn;
172 if( _alpha == 0 ) return _orig;
173 if( _alpha == 255 ) return _new;
175 ao = (_orig >> 24) & 0xFF;
176 ro = (_orig >> 16) & 0xFF;
177 go = (_orig >> 8) & 0xFF;
178 bo = (_orig >> 0) & 0xFF;
180 an = (_new >> 24) & 0xFF;
181 rn = (_new >> 16) & 0xFF;
182 gn = (_new >> 8) & 0xFF;
183 bn = (_new >> 0) & 0xFF;
185 if( _alpha == 0x80 ) {
192 ao = ao*(255-_alpha) + an*_alpha;
193 ro = ro*(255-_alpha) + rn*_alpha;
194 go = go*(255-_alpha) + gn*_alpha;
195 bo = bo*(255-_alpha) + bn*_alpha;
202 return (ao << 24) | (ro << 16) | (go << 8) | bo;