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)
37 giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
38 if( giTerminalFD == -1 )
40 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
46 tmpInt = TERM_MODE_FB;
47 ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
50 giScreenWidth = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, NULL );
51 giScreenHeight = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, NULL );
53 giVideo_LastDirtyLine = giScreenHeight;
55 // Force VT to be shown
56 ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
58 // Create local framebuffer (back buffer)
59 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
61 // Set cursor position and bitmap
62 ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
63 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
66 void Video_Update(void)
68 int ofs = giVideo_FirstDirtyLine*giScreenWidth;
69 int size = (giVideo_LastDirtyLine-giVideo_FirstDirtyLine)*giScreenWidth;
71 if( giVideo_LastDirtyLine == 0 ) return;
73 _SysDebug("Video_Update - Updating lines %i to %i (0x%x+0x%x px)",
74 giVideo_FirstDirtyLine, giVideo_LastDirtyLine, ofs, size);
75 seek(giTerminalFD, ofs*4, 1);
76 write(giTerminalFD, gpScreenBuffer+ofs, size*4);
77 _SysDebug("Video_Update - Done");
78 giVideo_FirstDirtyLine = 0;
79 giVideo_LastDirtyLine = 0;
82 void Video_SetCursorPos(short X, short Y)
88 pos.x = giVideo_CursorX = X;
89 pos.y = giVideo_CursorY = Y;
90 ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
93 void Video_FillRect(int X, int Y, int W, int H, uint32_t Colour)
98 if(X < 0 || Y < 0) return;
99 if(W >= giScreenWidth) return;
100 if(H >= giScreenHeight) return;
101 if(X + W >= giScreenWidth) W = giScreenWidth - W;
102 if(Y + H >= giScreenHeight) W = giScreenHeight - H;
104 dest = gpScreenBuffer + Y * giScreenWidth + X;
107 for( i = W; W --; dest ++) *dest = Colour;
108 dest += giScreenWidth - W;
113 * \brief Blit an entire buffer to the screen
114 * \note Assumes Pitch = 4*W
116 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
120 if( DstX >= giScreenWidth) return ;
121 if( DstY >= giScreenHeight) return ;
122 // TODO: Handle -ve X/Y by clipping
123 if( DstX < 0 || DstY < 0 ) return ;
124 // TODO: Handle out of bounds by clipping too
125 if( DstX + W > giScreenWidth ) return;
126 if( DstY + H > giScreenHeight )
127 H = giScreenWidth - DstY;
129 if( W <= 0 || H <= 0 ) return;
131 if( DstX < giVideo_FirstDirtyLine )
132 giVideo_FirstDirtyLine = DstY;
133 if( DstY + H > giVideo_LastDirtyLine )
134 giVideo_LastDirtyLine = DstY + H;
136 buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
137 if(W != giScreenWidth)
141 memcpy(buf, Source, W*4);
143 buf += giScreenWidth;
148 memcpy(buf, Source, giScreenWidth*H*4);
152 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
154 uint16_t ao,ro,go,bo;
155 uint16_t an,rn,gn,bn;
156 if( _alpha == 0 ) return _orig;
157 if( _alpha == 255 ) return _new;
159 ao = (_orig >> 24) & 0xFF;
160 ro = (_orig >> 16) & 0xFF;
161 go = (_orig >> 8) & 0xFF;
162 bo = (_orig >> 0) & 0xFF;
164 an = (_new >> 24) & 0xFF;
165 rn = (_new >> 16) & 0xFF;
166 gn = (_new >> 8) & 0xFF;
167 bn = (_new >> 0) & 0xFF;
169 if( _alpha == 0x80 ) {
176 ao = ao*(255-_alpha) + an*_alpha;
177 ro = ro*(255-_alpha) + rn*_alpha;
178 go = go*(255-_alpha) + gn*_alpha;
179 bo = bo*(255-_alpha) + bn*_alpha;
186 return (ao << 24) | (ro << 16) | (go << 8) | bo;