Usermode/AxWin3 - Implementing more of the widget code
[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
71         // Set cursor position and bitmap
72         ioctl(giTerminalFD, TERM_IOCTL_SETCURSORBITMAP, &cCursorBitmap);
73         Video_SetCursorPos( giScreenWidth/2, giScreenHeight/2 );
74 }
75
76 void Video_Update(void)
77 {
78         _SysDebug("Video_Update - gpScreenBuffer[0] = 0x%x", gpScreenBuffer[0]);
79         seek(giTerminalFD, 0, 1);
80         write(giTerminalFD, gpScreenBuffer, giScreenWidth*giScreenHeight*4);
81         _SysDebug("Video_Update - Done");
82 }
83
84 void Video_SetCursorPos(short X, short Y)
85 {
86         struct {
87                 uint16_t        x;
88                 uint16_t        y;
89         } pos;
90         pos.x = giVideo_CursorX = X;
91         pos.y = giVideo_CursorY = Y;
92         ioctl(giTerminalFD, TERM_IOCTL_GETSETCURSOR, &pos);
93 }
94
95 /**
96  * \brief Blit an entire buffer to the screen
97  * \note Assumes Pitch = 4*W
98  */
99 void Video_Blit(uint32_t *Source, short DstX, short DstY, short W, short H)
100 {
101          int    i;
102         uint32_t        *buf;
103
104 //      _SysDebug("Video_Blit: (%p (%i, %i) %ix%i)", Source, DstX, DstY, W, H);
105         
106         if( DstX >= giScreenWidth)      return ;
107         if( DstY >= giScreenHeight)     return ;
108         // TODO: Handle -ve X/Y by clipping
109         if( DstX < 0 || DstY < 0 )      return ;
110         // TODO: Handle out of bounds by clipping too
111         if( DstX + W > giScreenWidth )  return;
112         if( DstY + H > giScreenHeight )
113                 H = giScreenWidth - DstY;
114
115         if( W <= 0 || H <= 0 )  return;
116         
117 //      _SysDebug(" Clipped to (%i, %i) %ix%i", DstX, DstY, W, H);
118 //      _SysDebug(" Source[0] = 0x%x", Source[0]);
119         buf = gpScreenBuffer + DstY*giScreenWidth + DstX;
120         while( H -- )
121         {
122                 for( i = W; i --; )
123                         *buf++ = *Source++;
124                 buf += giScreenWidth - W;
125         }
126 }
127

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