2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
10 #include <acess/devices/terminal.h>
12 #include "resources/cursor.h"
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);
27 uint32_t *gpScreenBuffer;
28 int giVideo_FirstDirtyLine;
29 int giVideo_LastDirtyLine;
32 void Video_Setup(void)
38 giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
39 if( giTerminalFD == -1 )
41 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
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 )
50 fprintf(stderr, "stdout is not an Acess VT, can't start");
51 _SysDebug("stdout is not an Acess VT, can't start");
57 tmpInt = TERM_MODE_FB;
58 ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
61 giScreenWidth = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, NULL );
62 giScreenHeight = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, NULL );
64 giVideo_LastDirtyLine = giScreenHeight;
66 // Force VT to be shown
67 ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
69 // Create local framebuffer (back buffer)
70 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
72 // Set cursor position and bitmap
73 ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
74 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
77 void Video_Update(void)
79 int ofs = giVideo_FirstDirtyLine*giScreenWidth;
80 int size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
82 if( giVideo_LastDirtyLine == 0 ) return;
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;
94 void Video_SetCursorPos(short X, short Y)
100 pos.x = giVideo_CursorX = X;
101 pos.y = giVideo_CursorY = Y;
102 ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
105 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
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;
116 dest = gpScreenBuffer + Y * giScreenWidth + X;
119 for( i = W; i --; dest ++ ) *dest = Colour;
120 dest += giScreenWidth - W;
125 * \brief Blit an entire buffer to the screen
126 * \note Assumes Pitch = 4*W
128 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
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;
141 if( W <= 0 || H <= 0 ) return;
143 if( DstX < giVideo_FirstDirtyLine )
144 giVideo_FirstDirtyLine = DstY;
145 if( DstY + H > giVideo_LastDirtyLine )
146 giVideo_LastDirtyLine = DstY + H;
148 buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
149 if(W != giScreenWidth)
153 memcpy(buf, Source, W*4);
155 buf += giScreenWidth;
160 memcpy(buf, Source, giScreenWidth*H*4);
164 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
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;
171 ao = (_orig >> 24) & 0xFF;
172 ro = (_orig >> 16) & 0xFF;
173 go = (_orig >> 8) & 0xFF;
174 bo = (_orig >> 0) & 0xFF;
176 an = (_new >> 24) & 0xFF;
177 rn = (_new >> 16) & 0xFF;
178 gn = (_new >> 8) & 0xFF;
179 bn = (_new >> 0) & 0xFF;
181 if( _alpha == 0x80 ) {
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;
198 return (ao << 24) | (ro << 16) | (go << 8) | bo;