2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
10 #include <acess/devices/terminal.h>
12 #include "resources/cursor.h"
17 void Video_Setup(void);
18 void Video_SetCursorPos(short X, short Y);
19 void Video_Update(void);
20 void Video_FillRect(short X, short Y, short W, short H, uint32_t Color);
21 void Video_DrawRect(short X, short Y, short W, short H, uint32_t Color);
26 uint32_t *gpScreenBuffer;
29 void Video_Setup(void)
34 giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
35 if( giTerminalFD == -1 )
37 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
42 tmpInt = giScreenWidth;
43 tmpInt = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, &tmpInt );
44 if(tmpInt != giScreenWidth)
46 fprintf(stderr, "Warning: Selected width (%i) is invalid, clipped to %i\n",
47 giScreenWidth, tmpInt);
48 giScreenWidth = tmpInt;
52 tmpInt = giScreenHeight;
53 tmpInt = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, &tmpInt );
54 if(tmpInt != giScreenHeight)
56 fprintf(stderr, "Warning: Selected height (%i) is invalid, clipped to %i\n",
57 giScreenHeight, tmpInt);
58 giScreenHeight = tmpInt;
62 tmpInt = TERM_MODE_FB;
63 ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
65 // Force VT to be shown
66 ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
68 // Create local framebuffer (back buffer)
69 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
70 Video_FillRect(0, 0, giScreenWidth, giScreenHeight, 0x8080FF);
72 // Set cursor position and bitmap
73 ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
74 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
79 void Video_Update(void)
81 _SysDebug("Video_Update - gpScreenBuffer[0] = 0x%x", gpScreenBuffer[0]);
82 seek(giTerminalFD, 0, 1);
83 write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
84 _SysDebug("Video_Update - Done");
87 void Video_SetCursorPos(short X, short Y)
93 pos.x = giVideo_CursorX = X;
94 pos.y = giVideo_CursorY = Y;
95 ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
98 void Video_FillRect(short X, short Y, short W, short H, uint32_t Color)
101 uint32_t *buf = gpScreenBuffer + Y*giScreenWidth + X;
103 _SysDebug("Video_FillRect: (X=%i, Y=%i, W=%i, H=%i, Color=%08x)",
106 if(W < 0 || X < 0 || X >= giScreenWidth) return ;
107 if(X + W > giScreenWidth) W = giScreenWidth - X;
109 if(H < 0 || Y < 0 || Y >= giScreenHeight) return ;
110 if(Y + H > giScreenHeight) H = giScreenHeight - Y;
116 buf += giScreenWidth - W;
120 void Video_DrawRect(short X, short Y, short W, short H, uint32_t Color)
122 Video_FillRect(X, Y, W, 1, Color);
123 Video_FillRect(X, Y+H-1, W, 1, Color);
124 Video_FillRect(X, Y, 1, H, Color);
125 Video_FillRect(X+W-1, Y, 1, H, Color);
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)
137 _SysDebug("Video_Blit: (%p (%i, %i) %ix%i)", Source, DstX, DstY, W, H);
139 // TODO: Handle -ve X/Y by clipping
140 if( DstX < 0 || DstY < 0 ) return ;
141 // TODO: Handle out of bounds by clipping too
142 if( DstX + W > giScreenWidth ) return;
143 if( DstY + H > giScreenHeight ) return;
145 if( W <= 0 || H <= 0 ) return;
147 _SysDebug(" Clipped to (%i, %i) %ix%i", DstX, DstY, W, H);
148 _SysDebug(" Source[0] = 0x%x", Source[0]);
149 buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
154 buf += giScreenWidth - W;
159 * \brief Draw an image to the screen
160 * \todo Maybe have support for an offset in the image
162 void Video_DrawImage(short X, short Y, short W, short H, tImage *Image)
165 uint8_t *buf = (uint8_t *)(gpScreenBuffer + Y*giScreenWidth + X);
173 if( X >= giScreenWidth ) return ;
174 if( Y >= giScreenHeight ) return ;
176 // Wrap to image size
177 if( W > Image->Width ) W = Image->Width;
178 if( H > Image->Height ) H = Image->Height;
180 // Wrap to screen size
181 if( X + W > giScreenWidth ) W = giScreenWidth - X;
182 if( Y + H > giScreenHeight ) H = giScreenHeight - Y;
186 switch( Image->Format )
189 for( y = 0; y < H; y ++ )
191 int r, g, b, a; // New
192 int or, og, ob; // Original
193 for( x = 0; x < W; x ++ )
195 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
196 if( a == 0 ) continue; // 100% transparent
197 ob = buf[x*4+0]; og = buf[x*4+1]; or = buf[x*4+2];
201 // Transparent: Handled above
212 r = (or * (255-a) + r * a) / 255;
213 g = (og * (255-a) + g * a) / 255;
214 b = (ob * (255-a) + b * a) / 255;
217 buf[x*4+0] = b; buf[x*4+1] = g; buf[x*4+2] = r;
219 data += Image->Width * 4;
220 buf += giScreenWidth * 4;
226 for( y = 0; y < H; y ++ )
228 for( x = 0; x < W; x ++ )
230 buf[x*4+0] = data[x*3+2]; // Blue
231 buf[x*4+1] = data[x*3+1]; // Green
232 buf[x*4+2] = data[x*3+0]; // Red
235 buf += giScreenWidth * 4;
239 _SysDebug("ERROR: Unknown image format %i\n", Image->Format);