1 /* sdlgen.h: The internal shared definitions of the SDL OS/hardware layer.
2  *
3  * Copyright (C) 2001-2006 by Brian Raiter, under the GNU General Public
4  * License. No warranty. See COPYING for details.
5  */
6 
7 #ifndef	_sdlgen_h_
8 #define	_sdlgen_h_
9 
10 #include	"SDL.h"
11 #include	"../gen.h"
12 #include	"../oshw.h"
13 
14 /* Structure to hold the definition of a font.
15  */
16 typedef	struct fontinfo {
17     signed char		h;		/* height of each character */
18     signed char		w[256];		/* width of each character */
19     void	       *memory;		/* memory allocated for the font */
20     unsigned char      *bits[256];	/* pointers to each glyph */
21 } fontinfo;
22 
23 /* Structure to hold a font's colors.
24  */
25 typedef	struct fontcolors { Uint32 c[3]; } fontcolors;
26 
27 #define	bkgndcolor(fc)	((fc).c[0])	/* the background color */
28 #define	halfcolor(fc)	((fc).c[1])	/* the antialiasing color */
29 #define	textcolor(fc)	((fc).c[2])	/* the main color of the glyphs */
30 
31 /* Flags to the puttext function.
32  */
33 #define	PT_CENTER	0x0001		/* center the text horizontally */
34 #define	PT_RIGHT	0x0002		/* right-align the text */
35 #define	PT_MULTILINE	0x0004		/* span lines & break at whitespace */
36 #define	PT_UPDATERECT	0x0008		/* return the unused area in rect */
37 #define	PT_CALCSIZE	0x0010		/* determine area needed for text */
38 #define	PT_DIM		0x0020		/* draw using the dim text color */
39 #define	PT_HILIGHT	0x0040		/* draw using the bold text color */
40 
41 /*
42  * Values global to this module. All the globals are placed in here,
43  * in order to minimize pollution of the main module's namespace.
44  */
45 
46 typedef	struct oshwglobals
47 {
48     /*
49      * Shared variables.
50      */
51 
52     short		wtile;		/* width of one tile in pixels */
53     short		htile;		/* height of one tile in pixels */
54     short		cptile;		/* size of one tile in pixels */
55     fontcolors		textclr;	/* color triplet for normal text */
56     fontcolors		dimtextclr;	/* color triplet for dim text */
57     fontcolors		hilightclr;	/* color triplet for bold text */
58     SDL_Surface	       *screen;		/* the display */
59     fontinfo		font;		/* the font */
60 
61     /*
62      * Shared functions.
63      */
64 
65     /* Process all pending events. If wait is TRUE and no events are
66      * currently pending, the function blocks until an event arrives.
67      */
68     void (*eventupdatefunc)(int wait);
69 
70     /* A callback function, to be called every time a keyboard key is
71      * pressed or released. scancode is an SDL key symbol. down is
72      * TRUE if the key was pressed or FALSE if it was released.
73      */
74     void (*keyeventcallbackfunc)(int scancode, int down);
75 
76     /* A callback function, to be called when a mouse button is
77      * pressed or released. xpos and ypos give the mouse's location.
78      * button is the number of the mouse button. down is TRUE if the
79      * button was pressed or FALSE if it was released.
80      */
81     void (*mouseeventcallbackfunc)(int xpos, int ypos, int button, int down);
82 
83     /* Given a pixel's coordinates, return an integer identifying the
84      * tile on the map view display under that pixel, or -1 if the
85      * pixel is not within the map view.
86      */
87     int (*windowmapposfunc)(int x, int y);
88 
89     /* Return a pointer to an image of a cell with the two given
90      * tiles. If the top image is transparent, the composite image is
91      * created using the overlay buffer. (Thus the caller should be
92      * done using the image returned before calling this function
93      * again.) timerval should hold the time of the game, for
94      * rendering animated cell tiles, or -1 if the game has not
95      * started.
96      */
97     SDL_Surface* (*getcellimagefunc)(SDL_Rect *rect,
98 				     int top, int bot, int timerval);
99 
100     /* Return a pointer to a tile image for the given creature or
101      * animation sequence with the specified direction, sub-position,
102      * and animation frame.
103      */
104     SDL_Surface* (*getcreatureimagefunc)(SDL_Rect *rect, int id, int dir,
105 					 int moving, int frame);
106 
107     /* Display a line (or more) of text in the program's font. The
108      * text is clipped to area if necessary. If area is taller than
109      * the font, the topmost line is used. len specifies the number of
110      * characters to render; -1 can be used if text is NUL-terminated.
111      * flags is some combination of PT_* flags defined above. When the
112      * PT_CALCSIZE flag is set, no drawing is done; instead the w and
113      * h fields of area area changed to define the smallest rectangle
114      * that encloses the text that would have been rendered. (If
115      * PT_MULTILINE is also set, only the h field is changed.) If
116      * PT_UPDATERECT is set instead, then the h field is changed, so
117      * as to exclude the rectangle that was drawn in.
118      */
119     void (*puttextfunc)(SDL_Rect *area, char const *text, int len, int flags);
120 
121     /* Determine the widths necessary to display the columns of the
122      * given table. area specifies an enclosing rectangle for the
123      * complete table. The return value is an array of rectangles, one
124      * for each column of the table. The rectangles y-coordinates and
125      * heights are taken from area, and the x-coordinates and widths
126      * are calculated so as to best render the columns of the table in
127      * the given space. The caller has the responsibility of freeing
128      * the returned array.
129      */
130     SDL_Rect *(*measuretablefunc)(SDL_Rect const *area,
131 				  tablespec const *table);
132 
133     /* Draw a single row of the given table. cols is an array of
134      * rectangles, one for each column. Each rectangle is altered by
135      * the function as per puttext's PT_UPDATERECT behavior. row
136      * points to an integer indicating the first table entry of the
137      * row to display; upon return, this value is updated to point to
138      * the first entry following the row. flags can be set to PT_DIM
139      * and/or PT_HIGHLIGHT; the values will be applied to every entry
140      * in the row.
141      */
142     int (*drawtablerowfunc)(tablespec const *table, SDL_Rect *cols,
143 			    int *row, int flags);
144 
145 } oshwglobals;
146 
147 /* oshw's structure of globals.
148  */
149 extern oshwglobals sdlg;
150 
151 /* Some convenience macros for the above functions.
152  */
153 #define eventupdate		(*sdlg.eventupdatefunc)
154 #define	keyeventcallback	(*sdlg.keyeventcallbackfunc)
155 #define	mouseeventcallback	(*sdlg.mouseeventcallbackfunc)
156 #define	windowmappos		(*sdlg.windowmapposfunc)
157 #define	puttext			(*sdlg.puttextfunc)
158 #define	measuretable		(*sdlg.measuretablefunc)
159 #define	drawtablerow		(*sdlg.drawtablerowfunc)
160 #define	createscroll		(*sdlg.createscrollfunc)
161 #define	scrollmove		(*sdlg.scrollmovefunc)
162 #define	getcreatureimage	(*sdlg.getcreatureimagefunc)
163 #define	getcellimage		(*sdlg.getcellimagefunc)
164 
165 /* The initialization functions for the various modules.
166  */
167 extern int _sdltimerinitialize(int showhistogram);
168 extern int _sdlresourceinitialize(void);
169 extern int _sdltextinitialize(void);
170 extern int _sdltileinitialize(void);
171 extern int _sdlinputinitialize(void);
172 extern int _sdloutputinitialize(int fullscreen);
173 extern int _sdlsfxinitialize(int silence, int soundbufsize);
174 
175 #endif
176