~ubuntu-branches/ubuntu/karmic/starvoyager/karmic

« back to all changes in this revision

Viewing changes to graphic.cc

  • Committer: Bazaar Package Importer
  • Author(s): Idan Sofer
  • Date: 2003-08-01 16:55:13 UTC
  • Revision ID: james.westby@ubuntu.com-20030801165513-0ueb129iym99k7tg
Tags: upstream-0.4.4
ImportĀ upstreamĀ versionĀ 0.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        graphic.cc
 
3
        
 
4
        (c) Richard Thrippleton
 
5
        Licensing terms are in the 'LICENSE' file
 
6
        If that file is not included with this source then permission is not given to use this source in any way whatsoever.
 
7
*/
 
8
 
 
9
#include <SDL.h>
 
10
#include <string.h>
 
11
#include "SDL_rotozoom.h"
 
12
#include "SDL_gfxPrimitives.h"
 
13
#include "calc.h"
 
14
#include "error.h"
 
15
#include "interface.h"
 
16
#include "graphic.h"
 
17
 
 
18
void graphic::init()
 
19
{
 
20
        screen=NULL;
 
21
        for(int i=0;i<ISIZE;i++)
 
22
                graphics[i]=NULL;
 
23
        nd=0;
 
24
}
 
25
 
 
26
void graphic::setup(bool big,bool full)
 
27
{
 
28
        Uint32 flags; //Flags for setting video;
 
29
        SDL_Surface* tmp; //Temporary holding place while sprites are converted
 
30
        char* path; //Path to load bmp from
 
31
 
 
32
        if(SDL_InitSubSystem(SDL_INIT_VIDEO)==-1)
 
33
                throw error(SDL_GetError());
 
34
        flags=SDL_DOUBLEBUF|SDL_HWSURFACE;
 
35
        if(full)
 
36
                flags=flags|SDL_FULLSCREEN;
 
37
        if(big)
 
38
                screen=SDL_SetVideoMode(800,600,0,flags);
 
39
        else
 
40
                screen=SDL_SetVideoMode(600,400,0,flags);
 
41
        if(!screen)
 
42
                throw error(SDL_GetError());
 
43
        crct.x=0;
 
44
        crct.y=0;
 
45
        crct.w=screen->w;
 
46
        crct.h=screen->h;
 
47
        SDL_WM_SetCaption("Star Voyager","Star Voyager");
 
48
        SDL_ShowCursor(0);
 
49
 
 
50
        cols[BLACK]=SDL_MapRGB(screen->format,0,0,0);
 
51
        cols[RED]=SDL_MapRGB(screen->format,255,0,0);
 
52
        cols[LIGHTRED]=SDL_MapRGB(screen->format,255,100,100);
 
53
        cols[GREEN]=SDL_MapRGB(screen->format,0,255,0);
 
54
        cols[LIGHTGREEN]=SDL_MapRGB(screen->format,100,255,100);
 
55
        cols[BLUE]=SDL_MapRGB(screen->format,0,0,255);
 
56
        cols[LIGHTBLUE]=SDL_MapRGB(screen->format,100,100,255);
 
57
        cols[YELLOW]=SDL_MapRGB(screen->format,255,255,0);
 
58
        cols[ORANGE]=SDL_MapRGB(screen->format,255,100,100);
 
59
        cols[PURPLE]=SDL_MapRGB(screen->format,255,0,255);
 
60
        cols[GREY]=SDL_MapRGB(screen->format,180,180,180);
 
61
        cols[DARKGREY]=SDL_MapRGB(screen->format,64,64,64);
 
62
        cols[WHITE]=SDL_MapRGB(screen->format,255,255,255);
 
63
 
 
64
        path=new char[strlen(DATADIR)+32];
 
65
        sprintf(path,"%s/gfx/font.bmp",DATADIR);
 
66
        font=SDL_LoadBMP(path);
 
67
        delete[] path;
 
68
        if(!font)
 
69
                throw error(SDL_GetError());
 
70
        tmp=SDL_ConvertSurface(font,screen->format,SDL_SWSURFACE);
 
71
        SDL_FreeSurface(font);
 
72
        font=tmp;
 
73
        SDL_SetColorKey(font,SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[BLACK]);
 
74
 
 
75
        path=new char[strlen(DATADIR)+32];
 
76
        sprintf(path,"%s/gfx/haze.bmp",DATADIR);
 
77
        cloak=SDL_LoadBMP(path);
 
78
        delete[] path;
 
79
        if(!cloak)
 
80
                throw error(SDL_GetError());
 
81
        tmp=SDL_ConvertSurface(cloak,screen->format,SDL_SWSURFACE);
 
82
        SDL_FreeSurface(cloak);
 
83
        cloak=tmp;
 
84
        SDL_SetColorKey(cloak,SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[WHITE]);
 
85
}
 
86
 
 
87
void graphic::blit()
 
88
{
 
89
        SDL_Flip(screen);
 
90
}
 
91
 
 
92
graphic* graphic::get(int indx)
 
93
{
 
94
        if(!(indx>=0 && indx<ISIZE))
 
95
                return NULL;
 
96
        if(!graphics[indx])
 
97
        {
 
98
                graphics[indx]=new graphic(indx);
 
99
        }
 
100
        return graphics[indx];
 
101
}
 
102
 
 
103
void graphic::string(char* str,int x,short y,bool opq)
 
104
{
 
105
        SDL_Rect srct,drct; //Source rect and destination rect
 
106
        int i; //Position in text
 
107
        char c; //Character in use
 
108
        char l; //Letter to print
 
109
 
 
110
 
 
111
        if(opq)
 
112
        {
 
113
                drct.x=x;
 
114
                drct.y=y;
 
115
                drct.w=6*strlen(str);
 
116
                drct.h=5;
 
117
                SDL_FillRect(screen,&drct,cols[BLACK]);
 
118
        }
 
119
 
 
120
        i=0;
 
121
        srct.y=1;
 
122
        srct.w=5;
 
123
        srct.h=5;
 
124
        drct.x=x;
 
125
        drct.y=y;
 
126
        drct.w=5;
 
127
        drct.h=5;
 
128
        while(str[i]!=0)
 
129
        {
 
130
                c=str[i];
 
131
                if(c=='\n')
 
132
                        c=32;
 
133
                if(c>32 && c<127)
 
134
                {
 
135
                        l=c-33;
 
136
                        srct.x=l*6+1;
 
137
                        SDL_BlitSurface(font,&srct,screen,&drct);
 
138
                        drct.x+=6;              
 
139
                }
 
140
                if(c==' ')
 
141
                        drct.x+=6;
 
142
                i++;
 
143
        }
 
144
        srct.x=x;
 
145
        srct.y=y;
 
146
        srct.w=drct.x-x;
 
147
        srct.h=5;
 
148
        if(nd<1024)
 
149
        {
 
150
                dtyp[nd]=DTYP_RECT;
 
151
                dpos[nd]=srct;
 
152
                nd++;
 
153
        }
 
154
}
 
155
 
 
156
void graphic::box(sbox* box,int col)
 
157
{
 
158
        SDL_Rect rect; //Rectangle to draw
 
159
 
 
160
        rect.x=box->x;
 
161
        rect.y=box->y;
 
162
        rect.w=box->w;
 
163
        rect.h=box->h;
 
164
        SDL_FillRect(screen,&rect,cols[col]);
 
165
        if(nd<1024)
 
166
        {
 
167
                dtyp[nd]=DTYP_RECT;
 
168
                dpos[nd]=rect;  
 
169
                nd++;
 
170
        }
 
171
}
 
172
 
 
173
void graphic::clip(sbox* box)
 
174
{
 
175
        SDL_Rect rect; //Rectangle to clip with
 
176
 
 
177
        rect.x=box->x;
 
178
        rect.y=box->y;
 
179
        rect.w=box->w;
 
180
        rect.h=box->h;
 
181
        crct=rect;
 
182
        SDL_SetClipRect(screen,&rect);
 
183
        if(nd<1024)
 
184
        {
 
185
                dtyp[nd]=DTYP_CLIP;
 
186
                dpos[nd]=rect;  
 
187
                nd++;
 
188
        }
 
189
}
 
190
 
 
191
void graphic::pix(int x,short y,short col)
 
192
{
 
193
        fastPixelColor(screen,x,y,cols[col]);
 
194
        if(nd<1024)
 
195
        {
 
196
                dtyp[nd]=DTYP_PIX;
 
197
                dpos[nd].x=x;
 
198
                dpos[nd].y=y;
 
199
                nd++;
 
200
        }
 
201
}
 
202
 
 
203
sbox graphic::dimension()
 
204
{
 
205
        sbox out; //Value to output
 
206
 
 
207
        out.x=0;
 
208
        out.y=0;
 
209
        out.w=screen->w;
 
210
        out.h=screen->h;
 
211
        return out;
 
212
}
 
213
 
 
214
void graphic::line(int x1,short y1,short x2,short y2,short col)
 
215
{
 
216
        if(col>=0 && col<16)
 
217
        {
 
218
                lineColor(screen,x1,y1,x2,y2,cols[col]);
 
219
                if(nd<1024)
 
220
                {
 
221
                        dtyp[nd]=DTYP_LINE;
 
222
                        dpos[nd].x=x1;
 
223
                        dpos[nd].y=y1;
 
224
                        dpos[nd].w=x2;
 
225
                        dpos[nd].h=y2;
 
226
                        nd++;
 
227
                }
 
228
        }
 
229
}
 
230
 
 
231
void graphic::draw(int x,short y,short rot,short zout,short haze,bool trg)
 
232
{
 
233
        graphic* tspr; //Targetting sprite
 
234
        SDL_Rect dst; //Destination rect
 
235
        int hw,hh; //Half-width and half-height, for centering
 
236
 
 
237
        if(miss)
 
238
        {
 
239
                string("Graphic missing",x,y,true);
 
240
        }
 
241
        else
 
242
        {
 
243
                if(!(rot>=0 && rot<36 && zout>=0 && zout<4))
 
244
                        return;
 
245
                if(!rots[rot][zout-1])
 
246
                        calculate(rot,zout);
 
247
                hw=(rots[rot][zout-1]->w)/2;
 
248
                hh=(rots[rot][zout-1]->h)/2;
 
249
                dst.x=x-hw;
 
250
                dst.y=y-hh;
 
251
                dst.w=hw*2;
 
252
                dst.h=hh*2;
 
253
                SDL_BlitSurface(rots[rot][zout-1],NULL,screen,&dst);
 
254
                if(trg)
 
255
                {
 
256
                        tspr=get(TRG);
 
257
                        if(tspr)
 
258
                        {
 
259
                                tspr->draw(x,y-hh,0,1,0,false);
 
260
                                tspr->draw(x,y+hh,18,1,0,false);
 
261
                                tspr->draw(x-hw,y,27,1,0,false);
 
262
                                tspr->draw(x+hw,y,9,1,0,false);
 
263
                        }
 
264
                }
 
265
                if(nd<1024)
 
266
                {
 
267
                        dtyp[nd]=DTYP_RECT;
 
268
                        dpos[nd]=dst;
 
269
                        nd++;
 
270
                }
 
271
                if(haze>0 && hw*2>cloak->w && hh*2>cloak->h)
 
272
                {
 
273
                        dst.w=2;
 
274
                        dst.h=2;
 
275
                        for(int i=0;i<haze;i++)
 
276
                        {
 
277
                                dst.x=x-hw+calc::rnd(hw*2-cloak->w);
 
278
                                dst.y=y-hh+calc::rnd(hh*2-cloak->h);
 
279
                                SDL_BlitSurface(cloak,NULL,screen,&dst);
 
280
                        }
 
281
                }
 
282
 
 
283
        }
 
284
}
 
285
 
 
286
void graphic::embed()
 
287
{
 
288
        nd=0;
 
289
}
 
290
 
 
291
void graphic::clean()
 
292
{
 
293
        int j; //Loop limiter
 
294
 
 
295
        j=nd;
 
296
        SDL_SetClipRect(screen,NULL);
 
297
        for(int i=0;i<nd;i++)
 
298
        {
 
299
                switch(dtyp[i])
 
300
                {
 
301
                        case DTYP_CLIP:
 
302
                        SDL_SetClipRect(screen,&dpos[i]);
 
303
                        break;
 
304
                        
 
305
                        case DTYP_PIX:
 
306
                        fastPixelColor(screen,dpos[i].x,dpos[i].y,cols[BLACK]);
 
307
                        break;
 
308
                        
 
309
                        case DTYP_LINE:
 
310
                        lineColor(screen,dpos[i].x,dpos[i].y,dpos[i].w,dpos[i].h,cols[BLACK]);
 
311
                        break;
 
312
                
 
313
                        case DTYP_RECT:
 
314
                        SDL_FillRect(screen,&dpos[i],cols[BLACK]);
 
315
                        break;
 
316
                }
 
317
        }
 
318
        nd=0;
 
319
}
 
320
 
 
321
graphic::graphic(int indx)
 
322
{
 
323
        self=indx;
 
324
        miss=false;
 
325
        for(int i=0;i<36;i++)
 
326
                for(int j=0;j<4;j++)
 
327
                        rots[i][j]=NULL;
 
328
        try
 
329
        {
 
330
                load();
 
331
        }
 
332
        catch(error it)
 
333
        {
 
334
                miss=true;
 
335
        }
 
336
}
 
337
 
 
338
void graphic::load()
 
339
{
 
340
        SDL_Surface* tmp; //Temporary holding place while sprites are converted
 
341
        char* path; //Path to load sprite from
 
342
 
 
343
        path=new char[strlen(DATADIR)+32];
 
344
        sprintf(path,"%s/gfx/%i.bmp",DATADIR,self);
 
345
        orig=SDL_LoadBMP(path);
 
346
        delete[] path;
 
347
        if(!orig)
 
348
                throw error(SDL_GetError());
 
349
        tmp=SDL_ConvertSurface(orig,screen->format,SDL_SWSURFACE);
 
350
        SDL_FreeSurface(orig);
 
351
        orig=tmp;
 
352
        SDL_SetColorKey(orig,SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[BLACK]);
 
353
        calculate(0,1);
 
354
}
 
355
 
 
356
void graphic::calculate(int rot,short zout)
 
357
{
 
358
        int ang; //Angle to rotate by
 
359
 
 
360
        ang=360-rot*10;
 
361
        if(self>=16)
 
362
                rots[rot][zout-1]=rotozoomSurface(orig,ang,1.0/zout,1);
 
363
        else
 
364
                rots[rot][zout-1]=rotozoomSurface(orig,ang,1.0/zout,0);
 
365
        SDL_SetColorKey(rots[rot][zout-1],SDL_SRCCOLORKEY|SDL_RLEACCEL,cols[BLACK]);
 
366
        if(!rots[rot][zout-1])
 
367
                throw error(SDL_GetError());
 
368
}
 
369
 
 
370
graphic* graphic::graphics[ISIZE];
 
371
SDL_Surface* graphic::screen;
 
372
SDL_Surface* graphic::font;
 
373
SDL_Surface* graphic::cloak;
 
374
unsigned long graphic::cols[16];
 
375
int graphic::nd;
 
376
int graphic::dtyp[1024];
 
377
SDL_Rect graphic::dpos[1024];
 
378
SDL_Rect graphic::crct;