1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
ezx81 - X11 ZX81 emulator
Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------
Config file
*/
static const char ident[]="$Id$";
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "gfx.h"
#include "exit.h"
#include "config.h"
#include "font.h"
static const char ident_h[]=EZX81_GFX_H;
static const char ident_fh[]=EZX81_FONT_H;
/* ---------------------------------------- MACROS
*/
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define SCR_W 320
#define SCR_H 200
#define LOCK do \
{ \
if (SDL_MUSTLOCK(surface)) \
if (SDL_LockSurface(surface)<0) \
Exit("Failed to lock surface: %s\n", \
SDL_GetError()); \
} while(0)
#define UNLOCK do \
{ \
if (SDL_MUSTLOCK(surface)) \
SDL_UnlockSurface(surface); \
} while(0)
/* ---------------------------------------- STATICS
*/
static SDL_Surface *surface;
static Uint32 ticks;
static Uint32 frame;
static int scale;
static void (*putpixel)(int x, int y, Uint32 col);
/* ---------------------------------------- PRIVATE FUNCTIONS
*/
/* Taken from SDL documentation
*/
static void normal_putpixel(int x, int y, Uint32 pixel)
{
int bpp;
Uint8 *p;
bpp=surface->format->BytesPerPixel;
p=(Uint8 *)surface->pixels+y*surface->pitch+x*bpp;
switch(bpp)
{
case 1:
*p=pixel;
break;
case 2:
*(Uint16 *)p=pixel;
break;
case 3:
if(SDL_BYTEORDER==SDL_BIG_ENDIAN)
{
p[0]=(pixel>>16)&0xff;
p[1]=(pixel>>8)&0xff;
p[2]=pixel&0xff;
} else
{
p[0]=pixel&0xff;
p[1]=(pixel>>8)&0xff;
p[2]=(pixel>>16)&0xff;
}
break;
case 4:
*(Uint32 *)p=pixel;
break;
}
}
static void scale_putpixel(int x, int y, Uint32 pixel)
{
int sx,sy;
x*=scale;
y*=scale;
for(sx=0;sx<scale;sx++)
for(sy=0;sy<scale;sy++)
normal_putpixel(x+sx,y+sy,pixel);
}
static void DoHLine(int x1, int x2, int y, Uint32 col)
{
for(;x1<=x2;x1++)
putpixel(x1,y,col);
}
static void DoVLine(int x, int y1, int y2, Uint32 col)
{
for(;y1<=y2;y1++)
putpixel(x,y1,col);
}
/* ---------------------------------------- EXPORTED INTERFACES
*/
void GFXInit(void)
{
if (IConfig(CONF_FULLSCREEN))
scale=1;
else
scale=IConfig(CONF_SCALE);
if (scale<0)
scale=1;
if (scale>1)
putpixel=scale_putpixel;
else
putpixel=normal_putpixel;
frame=1000/IConfig(CONF_FRAMES_PER_SEC);
if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO))
Exit("Failed to init SDL: %s\n",SDL_GetError());
printf("scale = %d\n",scale);
if (!(surface=SDL_SetVideoMode(SCR_W*scale,
SCR_H*scale,
0,
IConfig(CONF_FULLSCREEN) ?
SDL_FULLSCREEN : 0)))
{
Exit("Failed to open video: %s\n",SDL_GetError());
}
SDL_ShowCursor(SDL_DISABLE);
SDL_WM_SetCaption("eZX81","eZX81");
}
SDL_Surface *GFXGetSurface(void)
{
return surface;
}
Uint32 GFXRGB(Uint8 r, Uint8 g, Uint8 b)
{
return SDL_MapRGB(surface->format,r,g,b);
}
void GFXClear(Uint32 col)
{
SDL_FillRect(surface,NULL,col);
}
void GFXStartFrame(void)
{
ticks=SDL_GetTicks();
}
void GFXEndFrame(int delay)
{
SDL_UpdateRect(surface,0,0,0,0);
if (delay)
{
Uint32 diff;
diff=SDL_GetTicks()-ticks;
if (diff<frame)
SDL_Delay(frame-diff);
}
}
void GFXPlot(int x, int y, Uint32 col)
{
LOCK;
putpixel(x,y,col);
UNLOCK;
}
void GFXRect(int x, int y, int w, int h, Uint32 col, int solid)
{
LOCK;
if (solid)
{
SDL_Rect r;
r.x=x*scale;
r.y=y*scale;
r.w=w*scale;
r.h=h*scale;
SDL_FillRect(surface,&r,col);
}
else
{
DoHLine(x,x+w-1,y,col);
DoHLine(x,x+w-1,y+h-1,col);
DoVLine(x,y,y+h-1,col);
DoVLine(x+w-1,y,y+h-1,col);
}
UNLOCK;
}
void GFXHLine(int x1, int x2, int y, Uint32 col)
{
LOCK;
DoHLine(x1,x2,y,col);
UNLOCK;
}
void GFXVLine(int x, int y1, int y2, Uint32 col)
{
LOCK;
DoVLine(x,y1,y2,col);
UNLOCK;
}
void GFXPrint(int x, int y, Uint32 col, const char *format, ...)
{
char buff[257];
va_list va;
char *p;
int sx,sy;
va_start(va,format);
vsprintf(buff,format,va);
va_end(va);
p=buff;
LOCK;
while(*p)
{
for(sy=0;sy<8;sy++)
{
if (y+sy<SCR_H && x<SCR_W)
{
for(sx=0;sx<8;sx++)
{
if (font[(int)*p][sx+sy*8] && x+sx<SCR_W)
putpixel(x+sx,y+sy,col);
}
}
}
p++;
x+=8;
}
UNLOCK;
}
/* END OF FILE */
|