2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
10 #include <acess/devices/terminal.h>
12 #include "resources/cursor.h"
16 void Video_Setup(void);
17 void Video_SetCursorPos(short X, short Y);
18 void Video_Update(void);
19 void Video_FillRect(short X, short Y, short W, short H, uint32_t Color);
20 void Video_DrawRect(short X, short Y, short W, short H, uint32_t Color);
25 uint32_t *gpScreenBuffer;
28 void Video_Setup(void)
33 giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
34 if( giTerminalFD == -1 )
36 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
41 tmpInt = giScreenWidth;
42 tmpInt = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, &tmpInt );
43 if(tmpInt != giScreenWidth)
45 fprintf(stderr, "Warning: Selected width (%i) is invalid, clipped to %i\n",
46 giScreenWidth, tmpInt);
47 giScreenWidth = tmpInt;
51 tmpInt = giScreenHeight;
52 tmpInt = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, &tmpInt );
53 if(tmpInt != giScreenHeight)
55 fprintf(stderr, "Warning: Selected height (%i) is invalid, clipped to %i\n",
56 giScreenHeight, tmpInt);
57 giScreenHeight = tmpInt;
61 tmpInt = TERM_MODE_FB;
62 ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
64 // Force VT to be shown
65 ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
67 // Create local framebuffer (back buffer)
68 gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
69 Video_FillRect(0, 0, giScreenWidth, giScreenHeight, 0x8080FF);
71 // Set cursor position and bitmap
72 ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
73 Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
78 void Video_Update(void)
80 //seek(giTerminalFD, 0, SEEK_SET);
81 seek(giTerminalFD, 0, 1);
82 write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
85 void Video_SetCursorPos(short X, short Y)
91 pos.x = giVideo_CursorX = X;
92 pos.y = giVideo_CursorY = Y;
93 ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
96 void Video_FillRect(short X, short Y, short W, short H, uint32_t Color)
99 uint32_t *buf = gpScreenBuffer + Y*giScreenWidth + X;
101 _SysDebug("Video_FillRect: (X=%i, Y=%i, W=%i, H=%i, Color=%08x)",
104 if(W < 0 || X < 0 || X >= giScreenWidth) return ;
105 if(X + W > giScreenWidth) W = giScreenWidth - X;
107 if(H < 0 || Y < 0 || Y >= giScreenHeight) return ;
108 if(Y + H > giScreenHeight) H = giScreenHeight - Y;
114 buf += giScreenWidth - W;
118 void Video_DrawRect(short X, short Y, short W, short H, uint32_t Color)
120 Video_FillRect(X, Y, W, 1, Color);
121 Video_FillRect(X, Y+H-1, W, 1, Color);
122 Video_FillRect(X, Y, 1, H, Color);
123 Video_FillRect(X+W-1, Y, 1, H, Color);
127 * \brief Draw an image to the screen
128 * \todo Maybe have support for an offset in the image
130 void Video_DrawImage(short X, short Y, short W, short H, tImage *Image)
133 uint8_t *buf = (uint8_t *)(gpScreenBuffer + Y*giScreenWidth + X);
141 if( X >= giScreenWidth ) return ;
142 if( Y >= giScreenHeight ) return ;
144 // Wrap to image size
145 if( W > Image->Width ) W = Image->Width;
146 if( H > Image->Height ) H = Image->Height;
148 // Wrap to screen size
149 if( X + W > giScreenWidth ) W = giScreenWidth - X;
150 if( Y + H > giScreenHeight ) H = giScreenHeight - Y;
154 switch( Image->Format )
157 for( y = 0; y < H; y ++ )
159 int r, g, b, a; // New
160 int or, og, ob; // Original
161 for( x = 0; x < W; x ++ )
163 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
164 if( a == 0 ) continue; // 100% transparent
165 ob = buf[x*4+0]; og = buf[x*4+1]; or = buf[x*4+2];
169 // Transparent: Handled above
180 r = (or * (255-a) + r * a) / 255;
181 g = (og * (255-a) + g * a) / 255;
182 b = (ob * (255-a) + b * a) / 255;
185 buf[x*4+0] = b; buf[x*4+1] = g; buf[x*4+2] = r;
187 data += Image->Width * 4;
188 buf += giScreenWidth * 4;
194 for( y = 0; y < H; y ++ )
196 for( x = 0; x < W; x ++ )
198 buf[x*4+0] = data[x*3+2]; // Blue
199 buf[x*4+1] = data[x*3+1]; // Green
200 buf[x*4+2] = data[x*3+0]; // Red
203 buf += giScreenWidth * 4;
207 _SysDebug("ERROR: Unknown image format %i\n", Image->Format);