Usermode/AxWin3 - Implemented sending messages to windows
[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
16 // === PROTOTYPES ===
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);
22
23 // === GLOBALS ===
24  int    giVideo_CursorX;
25  int    giVideo_CursorY;
26 uint32_t        *gpScreenBuffer;
27
28 // === CODE ===
29 void Video_Setup(void)
30 {
31          int    tmpInt;
32         
33         // Open terminal
34         giTerminalFD = open(gsTerminalDevice, OPENFLAG_READ|OPENFLAG_WRITE);
35         if( giTerminalFD == -1 )
36         {
37                 fprintf(stderr, "ERROR: Unable to open '%s' (%i)\n", gsTerminalDevice, _errno);
38                 exit(-1);
39         }
40         
41         // Set width
42         tmpInt = giScreenWidth;
43         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_WIDTH, &tmpInt );
44         if(tmpInt != giScreenWidth)
45         {
46                 fprintf(stderr, "Warning: Selected width (%i) is invalid, clipped to %i\n",
47                         giScreenWidth, tmpInt);
48                 giScreenWidth = tmpInt;
49         }
50         
51         // Set height
52         tmpInt = giScreenHeight;
53         tmpInt = ioctl( giTerminalFD, TERM_IOCTL_HEIGHT, &tmpInt );
54         if(tmpInt != giScreenHeight)
55         {
56                 fprintf(stderr, "Warning: Selected height (%i) is invalid, clipped to %i\n",
57                         giScreenHeight, tmpInt);
58                 giScreenHeight = tmpInt;
59         }
60         
61         // Set mode to video
62         tmpInt = TERM_MODE_FB;
63         ioctl( giTerminalFD, TERM_IOCTL_MODETYPE, &tmpInt );
64         
65         // Force VT to be shown
66         ioctl( giTerminalFD, TERM_IOCTL_FORCESHOW, NULL );
67         
68         // Create local framebuffer (back buffer)
69         gpScreenBuffer = malloc( giScreenWidth*giScreenHeight*4 );
70 //      memset(gpScreenBufferi
71 //      Video_FillRect(0, 0, giScreenWidth, giScreenHeight, 0x8080FF);
72
73         // Set cursor position and bitmap
74         ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
75         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
76
77         Video_Update();
78 }
79
80 void Video_Update(void)
81 {
82         _SysDebug("Video_Update - gpScreenBuffer[0] = 0x%x", gpScreenBuffer[0]);
83         seek(giTerminalFD, 0, 1);
84         write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
85         _SysDebug("Video_Update - Done");
86 }
87
88 void Video_SetCursorPos(short X, short Y)
89 {
90         struct {
91                 uint16_t        x;
92                 uint16_t        y;
93         } pos;
94         pos.x = giVideo_CursorX = X;
95         pos.y = giVideo_CursorY = Y;
96         ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
97 }
98
99 /**
100  * \brief Blit an entire buffer to the screen
101  * \note Assumes Pitch = 4*W
102  */
103 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
104 {
105          int    i;
106         uint32_t        *buf;
107
108 //      _SysDebug("Video_Blit: (%p (%i, %i) %ix%i)", Source, DstX, DstY, W, H);
109         
110         if( DstX >= giScreenWidth)      return ;
111         if( DstY >= giScreenHeight)     return ;
112         // TODO: Handle -ve X/Y by clipping
113         if( DstX < 0 || DstY < 0 )      return ;
114         // TODO: Handle out of bounds by clipping too
115         if( DstX + W > giScreenWidth )  return;
116         if( DstY + H > giScreenHeight )
117                 H = giScreenWidth - DstY;
118
119         if( W <= 0 || H <= 0 )  return;
120         
121 //      _SysDebug(" Clipped to (%i, %i) %ix%i", DstX, DstY, W, H);
122 //      _SysDebug(" Source[0] = 0x%x", Source[0]);
123         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
124         while( H -- )
125         {
126                 for( i = W; i --; )
127                         *buf++ = *Source++;
128                 buf += giScreenWidth - W;
129         }
130 }
131

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