1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
34 SDL_Surface
*display
= NULL
;
37 /* precomputed color pixels in display format */
38 static GP_Pixel white_pixel
, gray_pixel
, dark_gray_pixel
, black_pixel
,
39 red_pixel
, blue_pixel
;
41 /* draw using proportional font? */
42 static int font_flag
= 0;
45 static int tracking
= 0;
51 char *text
; /* null-terminated, malloc'd string */
52 struct FileLine
*next
; /* next line or NULL for the last line */
53 struct FileLine
*prev
;
56 struct FileLine
*first_line
= NULL
;
57 struct FileLine
*last_line
= NULL
;
59 void redraw_screen(void)
61 SDL_LockSurface(display
);
63 GP_Fill(&context
, gray_pixel
);
65 GP_TextStyle style
= GP_DEFAULT_TEXT_STYLE
;
69 style
.font
= &GP_default_console_font
;
72 style
.font
= &GP_default_proportional_font
;
81 style
.pixel_xspace
= 0;
82 style
.pixel_yspace
= 0;
83 style
.char_xspace
= tracking
;
85 /* Text alignment (we are always drawing to the right
86 * and below the starting point).
88 int align
= GP_ALIGN_RIGHT
|GP_VALIGN_BELOW
;
90 struct FileLine
*line
= first_line
;
92 for (i
= 0; i
< 30; i
++) {
96 GP_Text(&context
, &style
, 16, 16*i
+ 16, align
, line
->text
, black_pixel
);
100 SDL_UnlockSurface(display
);
103 static void warp_up(int lines
)
106 if (first_line
->prev
!= NULL
)
107 first_line
= first_line
->prev
;
113 static void warp_down(int lines
)
116 if (first_line
->next
!= NULL
)
117 first_line
= first_line
->next
;
123 void event_loop(void)
127 while (SDL_WaitEvent(&event
) > 0) {
128 switch (event
.type
) {
130 case SDL_VIDEOEXPOSE
:
136 switch (event
.key
.keysym
.sym
) {
139 font_flag
= (font_flag
+ 1) % 3;
141 font_flag
= (font_flag
+ 1) % 2;
181 static int read_file_head(const char *filename
)
183 FILE *f
= fopen(filename
, "r");
185 fprintf(stderr
, "Could not open file: %s\n", filename
);
191 int retval
= fscanf(f
, "%511[^\n]\n", buf
);
197 struct FileLine
*line
= malloc(sizeof(*line
));
198 line
->text
= strdup(buf
);
202 if (first_line
== NULL
) {
206 line
->prev
= last_line
;
207 last_line
->next
= line
;
216 int main(int argc
, char *argv
[])
219 fprintf(stderr
, "No file specified\n");
223 if (!read_file_head(argv
[1]))
227 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0) {
228 fprintf(stderr
, "Could not initialize SDL: %s\n", SDL_GetError());
232 /* Create a window with a software back surface */
233 display
= SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE
);
234 if (display
== NULL
) {
235 fprintf(stderr
, "Could not open display: %s\n", SDL_GetError());
239 /* Initialize a GP context from the SDL display */
240 GP_SDL_ContextFromSurface(&context
, display
);
242 /* Load colors suitable for the display */
243 GP_ColorNameToPixel(&context
, GP_COL_WHITE
, &white_pixel
);
244 GP_ColorNameToPixel(&context
, GP_COL_GRAY_LIGHT
, &gray_pixel
);
245 GP_ColorNameToPixel(&context
, GP_COL_GRAY_DARK
, &dark_gray_pixel
);
246 GP_ColorNameToPixel(&context
, GP_COL_BLACK
, &black_pixel
);
247 GP_ColorNameToPixel(&context
, GP_COL_RED
, &red_pixel
);
248 GP_ColorNameToPixel(&context
, GP_COL_BLUE
, &blue_pixel
);