Usermode/AxWin3 - Cleaning up bugs, adding image loading and text printing
[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         // Set width
43         tmpInt = giScreenWidth;
44         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, &tmpInt );
45         if(tmpInt != giScreenWidth)
46         {
47                 fprintf(stderr, "Warning: Selected width (%i) is invalid, clipped to %i\n",
48                         giScreenWidth, tmpInt);
49                 giScreenWidth = tmpInt;
50         }
51         
52         // Set height
53         tmpInt = giScreenHeight;
54         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, &tmpInt );
55         if(tmpInt != giScreenHeight)
56         {
57                 fprintf(stderr, "Warning: Selected height (%i) is invalid, clipped to %i\n",
58                         giScreenHeight, tmpInt);
59                 giScreenHeight = tmpInt;
60         }
61         
62         // Set mode to video
63         tmpInt = TERM_MODE_FB;
64         ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
65         
66         // Force VT to be shown
67         ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
68         
69         // Create local framebuffer (back buffer)
70         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
71
72         // Set cursor position and bitmap
73         ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
74         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
75 }
76
77 void Video_Update(void)
78 {
79         _SysDebug("Video_Update - gpScreenBuffer[0] = 0x%x", gpScreenBuffer[0]);
80         seek(giTerminalFD, 0, 1);
81         write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
82         _SysDebug("Video_Update - Done");
83 }
84
85 void Video_SetCursorPos(short X, short Y)
86 {
87         struct {
88                 uint16_t        x;
89                 uint16_t        y;
90         } pos;
91         pos.x = giVideo_CursorX = X;
92         pos.y = giVideo_CursorY = Y;
93         ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
94 }
95
96 /**
97  * \brief Blit an entire buffer to the screen
98  * \note Assumes Pitch = 4*W
99  */
100 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
101 {
102          int    i;
103         uint32_t        *buf;
104
105 //      _SysDebug("Video_Blit: (%p (%i, %i) %ix%i)", Source, DstX, DstY, W, H);
106         
107         if( DstX >= giScreenWidth)      return ;
108         if( DstY >= giScreenHeight)     return ;
109         // TODO: Handle -ve X/Y by clipping
110         if( DstX < 0 || DstY < 0 )      return ;
111         // TODO: Handle out of bounds by clipping too
112         if( DstX + W > giScreenWidth )  return;
113         if( DstY + H > giScreenHeight )
114                 H = giScreenWidth - DstY;
115
116         if( W <= 0 || H <= 0 )  return;
117         
118 //      _SysDebug(" Clipped to (%i, %i) %ix%i", DstX, DstY, W, H);
119 //      _SysDebug(" Source[0] = 0x%x", Source[0]);
120         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
121         while( H -- )
122         {
123                 for( i = W; i --; )
124                         *buf++ = *Source++;
125                 buf += giScreenWidth - W;
126         }
127 }
128
129 tColour Video_AlphaBlend(tColour _orig, tColour _new, uint8_t _alpha)
130 {
131         uint16_t        ao,ro,go,bo;
132         uint16_t        an,rn,gn,bn;
133         if( _alpha == 0 )       return _orig;
134         if( _alpha == 255 )     return _new;
135         
136         ao = (_orig >> 24) & 0xFF;
137         ro = (_orig >> 16) & 0xFF;
138         go = (_orig >>  8) & 0xFF;
139         bo = (_orig >>  0) & 0xFF;
140         
141         an = (_new >> 24) & 0xFF;
142         rn = (_new >> 16) & 0xFF;
143         gn = (_new >>  8) & 0xFF;
144         bn = (_new >>  0) & 0xFF;
145
146         if( _alpha == 0x80 ) {
147                 ao = (ao + an) / 2;
148                 ro = (ro + rn) / 2;
149                 go = (go + gn) / 2;
150                 bo = (bo + bn) / 2;
151         }
152         else {
153                 ao = ao*(255-_alpha) + an*_alpha;
154                 ro = ro*(255-_alpha) + rn*_alpha;
155                 go = go*(255-_alpha) + gn*_alpha;
156                 bo = bo*(255-_alpha) + bn*_alpha;
157                 ao /= 255*2;
158                 ro /= 255*2;
159                 go /= 255*2;
160                 bo /= 255*2;
161         }
162
163         return (ao << 24) | (ro << 16) | (go << 8) | bo;
164 }
165

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