source: mainline/console/screenbuffer.c@ 51dbadf3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 51dbadf3 was a8b2b5b2, checked in by Josef Cejka <malyzelenyhnus@…>, 19 years ago

Support for console show/hide cursor.
Tetris without cursor.

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[3993b3d]1/*
2 * Copyright (C) 2006 Josef Cejka
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <screenbuffer.h>
30#include <malloc.h>
31#include <unistd.h>
32
[a9bd960c]33/** Store one character to screenbuffer. Its position is determined by scr->position_x and scr->position_y.
34 * @param scr screenbuffer
35 * @param c stored character
[3993b3d]36 */
[a9bd960c]37void screenbuffer_putchar(screenbuffer_t *scr, char c)
[3993b3d]38{
39 keyfield_t *field;
40
41 field = get_field_at(scr, scr->position_x, scr->position_y);
42
43 field->character = c;
44 field->style = scr->style;
45}
46
[a9bd960c]47/** Initilize screenbuffer. Allocate space for screen content in accordance to given size.
48 * @param scr initialized screenbuffer
[e87e18f]49 * @param size_x width in characters
50 * @param size_y height in characters
51 * @return pointer to screenbuffer (same as scr parameter) or NULL
[a9bd960c]52 */
[3993b3d]53screenbuffer_t *screenbuffer_init(screenbuffer_t *scr, int size_x, int size_y)
54{
55 if ((scr->buffer = (keyfield_t *)malloc(sizeof(keyfield_t) * size_x * size_y)) == NULL) {
56 return NULL;
57 }
58
59 scr->size_x = size_x;
60 scr->size_y = size_y;
[1f36e90]61 scr->style.fg_color = DEFAULT_FOREGROUND;
62 scr->style.bg_color = DEFAULT_BACKGROUND;
[a8b2b5b2]63 scr->is_cursor_visible = 1;
[10569b1]64
65 screenbuffer_clear(scr);
66
[3993b3d]67 return scr;
68}
69
[e87e18f]70/** Clear screenbuffer.
71 * @param scr screenbuffer
72 */
[3993b3d]73void screenbuffer_clear(screenbuffer_t *scr)
74{
75 unsigned int i;
76
[10569b1]77 for (i = 0; i < (scr->size_x * scr->size_y); i++) {
[3993b3d]78 scr->buffer[i].character = ' ';
79 scr->buffer[i].style = scr->style;
80 }
81
82 scr->top_line = 0;
83 scr->position_y = 0;
84 scr->position_x = 0;
85}
86
[e87e18f]87/** Clear one buffer line.
[3993b3d]88 * @param scr
89 * @param line One buffer line (not a screen line!)
90 */
91void screenbuffer_clear_line(screenbuffer_t *scr, unsigned int line)
92{
93 unsigned int i;
94
95 for (i = 0; i < scr->size_x; i++) {
96 scr->buffer[i + line*scr->size_x].character = ' ';
97 scr->buffer[i + line*scr->size_x].style = scr->style;
98 }
99}
100
[e87e18f]101/** Copy content buffer from screenbuffer to given memory.
102 * @param scr source screenbuffer
103 * @param dest destination
104 */
[3993b3d]105void screenbuffer_copy_buffer(screenbuffer_t *scr, keyfield_t *dest)
106{
107 unsigned int i;
108
109 for (i = 0; i < scr->size_x * scr->size_y; i++) {
110 dest[i] = scr->buffer[i];
111 }
112}
113
[e87e18f]114/** Set new cursor position in screenbuffer.
115 * @param scr
116 * @param x
117 * @param y
118 */
[3993b3d]119void screenbuffer_goto(screenbuffer_t *scr, unsigned int x, unsigned int y)
120{
121 scr->position_x = x % scr->size_x;
[a9bd960c]122 scr->position_y = y % scr->size_y;
123}
124
[e87e18f]125/** Set new style.
126 * @param scr
127 * @param fg_color
128 * @param bg_color
129 */
[a9bd960c]130void screenbuffer_set_style(screenbuffer_t *scr, unsigned int fg_color, unsigned int bg_color)
131{
132 scr->style.fg_color = fg_color;
133 scr->style.bg_color = bg_color;
[3993b3d]134}
135
Note: See TracBrowser for help on using the repository browser.