Usermode/AxWin3 - Added support for getting display dimensions
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / video.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  *
5  * video.c
6  * - Video methods
7  */
8 #include <common.h>
9 #include <acess/sys.h>
10 #include <acess/devices/terminal.h>
11 #include <image.h>
12 #include "resources/cursor.h"
13 #include <stdio.h>
14 #include <video.h>
15 #include <wm.h>
16
17 // === PROTOTYPES ===
18 void    Video_Setup(void);
19 void    Video_SetCursorPos(short X, short Y);
20 void    Video_Update(void);
21 void    Video_FillRect(short X, short Y, short W, short H, uint32_t Color);
22 void    Video_DrawRect(short X, short Y, short W, short H, uint32_t Color);
23
24 // === GLOBALS ===
25  int    giVideo_CursorX;
26  int    giVideo_CursorY;
27 uint32_t        *gpScreenBuffer;
28
29 // === CODE ===
30 void Video_Setup(void)
31 {
32          int    tmpInt;
33         
34         // Open terminal
35         giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
36         if( giTerminalFD == -1 )
37         {
38                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
39                 exit(-1);
40         }
41         
42         
43         // Set mode to video
44         tmpInt = TERM_MODE_FB;
45         ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
46         
47         // Get dimensions
48         giScreenWidth = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, NULL );
49         giScreenHeight = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, NULL );
50         
51         // Force VT to be shown
52         ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
53         
54         // Create local framebuffer (back buffer)
55         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
56
57         // Set cursor position and bitmap
58         ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
59         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
60 }
61
62 void Video_Update(void)
63 {
64         _SysDebug("Video_Update - gpScreenBuffer[0] = 0x%x", gpScreenBuffer[0]);
65         seek(giTerminalFD, 0, 1);
66         write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
67         _SysDebug("Video_Update - Done");
68 }
69
70 void Video_SetCursorPos(short X, short Y)
71 {
72         struct {
73                 uint16_t        x;
74                 uint16_t        y;
75         } pos;
76         pos.x = giVideo_CursorX = X;
77         pos.y = giVideo_CursorY = Y;
78         ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
79 }
80
81 /**
82  * \brief Blit an entire buffer to the screen
83  * \note Assumes Pitch = 4*W
84  */
85 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
86 {
87          int    i;
88         uint32_t        *buf;
89
90 //      _SysDebug("Video_Blit: (%p (%i, %i) %ix%i)", Source, DstX, DstY, W, H);
91         
92         if( DstX >= giScreenWidth)      return ;
93         if( DstY >= giScreenHeight)     return ;
94         // TODO: Handle -ve X/Y by clipping
95         if( DstX < 0 || DstY < 0 )      return ;
96         // TODO: Handle out of bounds by clipping too
97         if( DstX + W > giScreenWidth )  return;
98         if( DstY + H > giScreenHeight )
99                 H = giScreenWidth - DstY;
100
101         if( W <= 0 || H <= 0 )  return;
102         
103 //      _SysDebug(" Clipped to (%i, %i) %ix%i", DstX, DstY, W, H);
104 //      _SysDebug(" Source[0] = 0x%x", Source[0]);
105         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
106         while( H -- )
107         {
108                 for( i = W; i --; )
109                         *buf++ = *Source++;
110                 buf += giScreenWidth - W;
111         }
112 }
113
114 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
115 {
116         uint16_t        ao,ro,go,bo;
117         uint16_t        an,rn,gn,bn;
118         if( _alpha == 0 )       return _orig;
119         if( _alpha == 255 )     return _new;
120         
121         ao = (_orig >> 24) & 0xFF;
122         ro = (_orig >> 16) & 0xFF;
123         go = (_orig >>  8) & 0xFF;
124         bo = (_orig >>  0) & 0xFF;
125         
126         an = (_new >> 24) & 0xFF;
127         rn = (_new >> 16) & 0xFF;
128         gn = (_new >>  8) & 0xFF;
129         bn = (_new >>  0) & 0xFF;
130
131         if( _alpha == 0x80 ) {
132                 ao = (ao + an) / 2;
133                 ro = (ro + rn) / 2;
134                 go = (go + gn) / 2;
135                 bo = (bo + bn) / 2;
136         }
137         else {
138                 ao = ao*(255-_alpha) + an*_alpha;
139                 ro = ro*(255-_alpha) + rn*_alpha;
140                 go = go*(255-_alpha) + gn*_alpha;
141                 bo = bo*(255-_alpha) + bn*_alpha;
142                 ao /= 255*2;
143                 ro /= 255*2;
144                 go /= 255*2;
145                 bo /= 255*2;
146         }
147
148         return (ao << 24) | (ro << 16) | (go << 8) | bo;
149 }
150

UCC git Repository :: git.ucc.asn.au