autoreconf --force
[sdlpango.git] / test / testbench.c
blob60bdbfaf6755ae7525aa7abd689da87dd7049157
1 /* testbench.cpp -- A companion library to SDL for working with Pango.
2 Copyright (C) 2004 NAKAMURA Ken'ichi
4 This library 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 This library 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 this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include "SDL_Pango.h"
20 #include "stdio.h"
21 #include "stdlib.h"
23 SDLPango_Context *context;
24 char *text;
26 int resizeLoop(SDL_Surface **framebuf)
28 SDL_Event event;
30 while( SDL_PollEvent( &event ) ){
31 switch( event.type ){
32 case SDL_QUIT:
33 return 0;
35 case SDL_VIDEORESIZE:
36 *framebuf = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, SDL_SWSURFACE | SDL_RESIZABLE);
37 break;
39 case SDL_KEYUP:
40 if(event.key.keysym.sym == SDLK_RETURN)
41 SDLPango_SetMarkup(context, text, -1);
42 else if(event.key.keysym.sym == SDLK_SPACE)
43 SDLPango_SetText(context, text, -1);
44 break;
46 default:
47 break;
51 return -1;
54 char *readFile(const char *filename)
56 long file_size;
57 char *text;
58 FILE *file = fopen(filename, "rb");
59 if(! file)
60 exit(1);
62 fseek(file, 0, SEEK_END);
63 file_size = ftell(file);
64 fseek(file, 0, SEEK_SET);
65 text = (char *)malloc(file_size + 1);
66 fread(text, file_size, 1, file);
67 text[file_size] = '\0';
69 return text;
72 int main(int argc, char *argv[])
74 SDL_Surface *framebuf;
75 SDL_Surface *surface;
76 if(argc == 1)
77 exit(1);
79 SDL_Init(SDL_INIT_VIDEO);
80 SDLPango_Init();
82 framebuf = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE | SDL_RESIZABLE);
84 context = SDLPango_CreateContext();
86 #ifdef SET_DPI
87 SDLPango_SetDpi(context, 200.0, 200.0);
88 #endif
90 SDLPango_SetDefaultColor(context, MATRIX_TRANSPARENT_BACK_WHITE_LETTER);
92 SDLPango_SetMinimumSize(context, 640, 0);
94 #ifdef SET_BASE_DIRECTION
95 SDLPango_SetBaseDirection(context, SDLPANGO_DIRECTION_RTL);
96 #endif
98 text = readFile(argv[1]);
100 surface = NULL;
101 SDLPango_SetMarkup(context, text, -1);
103 while(resizeLoop(&framebuf)) {
104 SDL_Surface *surface;
106 SDLPango_SetMinimumSize(context, framebuf->w, 0);
108 #ifdef GET_LAUOUT_WIDTH
110 int w, h;
111 w = SDLPango_GetLayoutWidth(context);
112 h = SDLPango_GetLayoutHeight(context);
114 #endif
116 #ifdef CREATE_SURFACE_DRAW
117 surface = SDLPango_CreateSurfaceDraw(context);
118 #else
119 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, framebuf->w, framebuf->h,
120 32, (Uint32)(255 << (8 * 3)), (Uint32)(255 << (8 * 2)),
121 (Uint32)(255 << (8 * 1)), 255);
122 SDLPango_Draw(context, surface, 0, 0);
123 #endif
125 SDL_FillRect(framebuf, NULL, SDL_MapRGBA(framebuf->format, 0, 0, 0, 0));
126 SDL_BlitSurface(surface, NULL, framebuf, NULL);
127 SDL_UpdateRect(framebuf, 0, 0, framebuf->w, framebuf->h);
129 SDL_FreeSurface(surface);
132 free(text);
134 SDLPango_FreeContext(context);
136 SDL_Quit();
137 return 0;