~ubuntu-branches/ubuntu/trusty/sdlpango/trusty

« back to all changes in this revision

Viewing changes to test/testbench.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2006-09-28 23:50:02 UTC
  • Revision ID: james.westby@ubuntu.com-20060928235002-qn0dl0ktodhmieax
Tags: upstream-0.1.2
ImportĀ upstreamĀ versionĀ 0.1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  testbench.cpp -- A companion library to SDL for working with Pango.
 
2
    Copyright (C) 2004 NAKAMURA Ken'ichi
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
17
*/
 
18
 
 
19
#include "SDL_Pango.h"
 
20
#include "stdio.h"
 
21
#include "stdlib.h"
 
22
 
 
23
SDLPango_Context *context;
 
24
char *text;
 
25
 
 
26
int resizeLoop(SDL_Surface **framebuf)
 
27
{
 
28
    SDL_Event event;
 
29
 
 
30
    while( SDL_PollEvent( &event ) ){
 
31
        switch( event.type ){
 
32
        case SDL_QUIT:
 
33
            return 0;
 
34
 
 
35
        case  SDL_VIDEORESIZE:
 
36
            *framebuf = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, SDL_SWSURFACE | SDL_RESIZABLE);
 
37
            break;
 
38
 
 
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;
 
45
 
 
46
        default:
 
47
            break;
 
48
        }
 
49
    }
 
50
 
 
51
    return -1;
 
52
}
 
53
 
 
54
char *readFile(const char *filename)
 
55
{
 
56
    long file_size;
 
57
    char *text;
 
58
    FILE *file = fopen(filename, "rb");
 
59
    if(! file)
 
60
        exit(1);
 
61
 
 
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';
 
68
 
 
69
    return text;
 
70
}
 
71
 
 
72
int main(int argc, char *argv[])
 
73
{
 
74
    SDL_Surface *framebuf;
 
75
    SDL_Surface *surface;
 
76
    if(argc == 1)
 
77
        exit(1);
 
78
 
 
79
    SDL_Init(SDL_INIT_VIDEO);
 
80
    SDLPango_Init();
 
81
 
 
82
    framebuf = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE | SDL_RESIZABLE);
 
83
 
 
84
    context = SDLPango_CreateContext();
 
85
 
 
86
#ifdef SET_DPI
 
87
    SDLPango_SetDpi(context, 200.0, 200.0);
 
88
#endif
 
89
 
 
90
    SDLPango_SetDefaultColor(context, MATRIX_TRANSPARENT_BACK_WHITE_LETTER);
 
91
 
 
92
    SDLPango_SetMinimumSize(context, 640, 0);
 
93
 
 
94
#ifdef SET_BASE_DIRECTION
 
95
    SDLPango_SetBaseDirection(context, SDLPANGO_DIRECTION_RTL);
 
96
#endif
 
97
 
 
98
    text = readFile(argv[1]);
 
99
 
 
100
    surface = NULL;
 
101
    SDLPango_SetMarkup(context, text, -1);
 
102
 
 
103
    while(resizeLoop(&framebuf)) {
 
104
        SDL_Surface *surface;
 
105
 
 
106
        SDLPango_SetMinimumSize(context, framebuf->w, 0);
 
107
 
 
108
#ifdef GET_LAUOUT_WIDTH
 
109
        {
 
110
            int w, h;
 
111
            w = SDLPango_GetLayoutWidth(context);
 
112
            h = SDLPango_GetLayoutHeight(context);
 
113
        }
 
114
#endif
 
115
 
 
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
 
124
 
 
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);
 
128
 
 
129
        SDL_FreeSurface(surface);
 
130
    }
 
131
 
 
132
    free(text);
 
133
 
 
134
    SDLPango_FreeContext(context);
 
135
 
 
136
    SDL_Quit();
 
137
    return 0;
 
138
}
 
139