2 * ViewBuffer - Mimicing a frame buffer output - but using a BView.
3 * Based on frame_buffer_console.
5 * Copyright 2005 Michael Lotz. All rights reserved.
6 * Distributed under the Haiku License.
8 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
9 * Distributed under the terms of the MIT License.
14 #include "ViewBuffer.h"
17 #define CHAR_HEIGHT 13
19 // Palette is (black and white are swapt like in normal BeOS Terminal)
28 // 8-15 - same but bright (we're ignoring those)
30 static uint32 sPalette32
[] = {
42 ViewBuffer::ViewBuffer(BRect frame
)
43 : BView(frame
, "ViewBuffer", B_FOLLOW_ALL
, B_WILL_DRAW
| B_FRAME_EVENTS
),
44 fColumns(frame
.IntegerWidth() / CHAR_WIDTH
),
45 fRows(frame
.IntegerHeight() / CHAR_HEIGHT
),
47 fResizeCallback(NULL
),
48 // initially, the cursor is hidden
52 SetFont(be_fixed_font
);
54 // initialize private palette
55 for (int i
= 0; i
< 8; i
++) {
56 fPalette
[i
].red
= (sPalette32
[i
] >> 16) & 0xff;
57 fPalette
[i
].green
= (sPalette32
[i
] >> 8) & 0xff;
58 fPalette
[i
].blue
= (sPalette32
[i
] >> 0) & 0xff;
59 fPalette
[i
].alpha
= 0xff;
62 // initialize glyph grid
63 uint32 size
= fRows
* fColumns
;
65 fGlyphGrid
= new uint16
[size
];
66 memset(fGlyphGrid
, 0, size
* sizeof(uint16
));
71 ViewBuffer::~ViewBuffer()
78 ViewBuffer::FrameResized(float width
, float height
)
80 int32 oldColumns
= fColumns
;
81 int32 oldRows
= fRows
;
83 fColumns
= (int32
)width
/ CHAR_WIDTH
;
84 fRows
= (int32
)height
/ CHAR_HEIGHT
;
87 uint16
* oldGlyphGrid
= fGlyphGrid
;
88 uint32 size
= fRows
* fColumns
;
90 fGlyphGrid
= new uint16
[size
];
91 memset(fGlyphGrid
, 0, size
* sizeof(uint16
));
94 // transfer old glyph grid into new one
95 if (oldGlyphGrid
&& fGlyphGrid
) {
96 int32 columns
= min_c(oldColumns
, fColumns
);
97 int32 rows
= min_c(oldRows
, fRows
);
98 for (int32 y
= 0; y
< rows
; y
++) {
99 for (int32 x
= 0; x
< columns
; x
++) {
100 fGlyphGrid
[y
* fColumns
+ x
] = oldGlyphGrid
[y
* oldColumns
+ x
];
104 delete[] oldGlyphGrid
;
107 fResizeCallback(fColumns
, fRows
, fResizeCallbackData
);
112 ViewBuffer::GetSize(int32
*width
, int32
*height
)
121 ViewBuffer::SetResizeCallback(resize_callback callback
, void *data
)
123 fResizeCallback
= callback
;
124 fResizeCallbackData
= data
;
129 ViewBuffer::ForegroundColor(uint8 attr
)
136 ViewBuffer::BackgroundColor(uint8 attr
)
138 return (attr
>> 4) & 0x7;
143 ViewBuffer::GetPaletteEntry(uint8 index
)
145 return fPalette
[index
];
150 ViewBuffer::PutGlyph(int32 x
, int32 y
, uint8 glyph
, uint8 attr
)
152 if (x
>= fColumns
|| y
>= fRows
)
155 RenderGlyph(x
, y
, glyph
, attr
);
160 ViewBuffer::FillGlyph(int32 x
, int32 y
, int32 width
, int32 height
, uint8 glyph
, uint8 attr
)
162 if (x
>= fColumns
|| y
>= fRows
)
165 int32 left
= x
+ width
;
169 int32 bottom
= y
+ height
;
173 for (; y
< bottom
; y
++) {
174 for (int32 x2
= x
; x2
< left
; x2
++) {
175 RenderGlyph(x2
, y
, glyph
, attr
);
182 ViewBuffer::RenderGlyph(int32 x
, int32 y
, uint8 glyph
, uint8 attr
)
189 _RenderGlyph(x
, y
, string
, attr
);
193 // remember the glyph in the grid
195 fGlyphGrid
[y
* fColumns
+ x
] = (glyph
<< 8) | attr
;
201 ViewBuffer::Draw(BRect updateRect
)
204 int32 startX
= max_c(0, (int32
)(updateRect
.left
/ CHAR_WIDTH
));
205 int32 endX
= min_c(fColumns
- 1, (int32
)(updateRect
.right
/ CHAR_WIDTH
) + 1);
206 int32 startY
= max_c(0, (int32
)(updateRect
.top
/ CHAR_HEIGHT
));
207 int32 endY
= min_c(fRows
- 1, (int32
)(updateRect
.bottom
/ CHAR_HEIGHT
) + 1);
212 for (int32 y
= startY
; y
<= endY
; y
++) {
213 for (int32 x
= startX
; x
<= endX
; x
++) {
214 uint16 grid
= fGlyphGrid
[y
* fColumns
+ x
];
215 uint8 glyph
= grid
>> 8;
216 uint8 attr
= grid
& 0x00ff;
218 _RenderGlyph(x
, y
, string
, attr
, false);
223 DrawCursor(fCursorX
, fCursorY
);
228 ViewBuffer::DrawCursor(int32 x
, int32 y
)
237 InvertRect(BRect(x
, y
, x
+ CHAR_WIDTH
, y
+ CHAR_HEIGHT
));
245 ViewBuffer::MoveCursor(int32 x
, int32 y
)
247 DrawCursor(fCursorX
, fCursorY
);
256 ViewBuffer::Blit(int32 srcx
, int32 srcy
, int32 width
, int32 height
, int32 destx
, int32 desty
)
258 // blit inside the glyph grid
260 int32 xOffset
= destx
- srcx
;
261 int32 yOffset
= desty
- srcy
;
266 uint16
* src
= fGlyphGrid
+ srcy
* fColumns
+ srcx
;
269 // copy from right to left
273 // copy from left to right
278 // copy from bottom to top
279 yIncrement
= -fColumns
;
280 src
+= (height
- 1) * fColumns
;
282 // copy from top to bottom
283 yIncrement
= fColumns
;
286 uint16
* dst
= src
+ yOffset
* fColumns
+ xOffset
;
288 for (int32 y
= 0; y
< height
; y
++) {
289 uint16
* srcHandle
= src
;
290 uint16
* dstHandle
= dst
;
291 for (int32 x
= 0; x
< width
; x
++) {
292 *dstHandle
= *srcHandle
;
293 srcHandle
+= xIncrement
;
294 dstHandle
+= xIncrement
;
301 height
*= CHAR_HEIGHT
;
306 BRect
source(srcx
, srcy
, srcx
+ width
, srcy
+ height
);
309 desty
*= CHAR_HEIGHT
;
310 BRect
dest(destx
, desty
, destx
+ width
, desty
+ height
);
313 CopyBits(source
, dest
);
321 ViewBuffer::Clear(uint8 attr
)
324 SetLowColor(GetPaletteEntry(BackgroundColor(attr
)));
325 SetViewColor(LowColor());
326 FillRect(Frame(), B_SOLID_LOW
);
335 memset(fGlyphGrid
, 0, fRows
* fColumns
* sizeof(uint16
));
340 ViewBuffer::_RenderGlyph(int32 x
, int32 y
, const char* string
, uint8 attr
, bool fill
)
342 BPoint
where(x
* CHAR_WIDTH
, (y
+ 1) * CHAR_HEIGHT
- 3);
344 SetHighColor(GetPaletteEntry(ForegroundColor(attr
)));
346 SetLowColor(GetPaletteEntry(BackgroundColor(attr
)));
347 FillRect(BRect(x
* CHAR_WIDTH
, y
* CHAR_HEIGHT
,
348 (x
+ 1) * CHAR_WIDTH
, (y
+ 1) * CHAR_HEIGHT
),
351 DrawString(string
, where
);