convert line ends
[canaan.git] / prj / cam / src / deepc / render / dpcutils.cpp
blobe1cf971a47bffe6fea3a9c3e5bc0bcc2e5cf7a3b
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 #include <appagg.h>
7 #include <string.h>
9 #include <dpcutils.h>
10 #include <string.h>
11 #include <dev2d.h>
12 #include <mprintf.h>
14 #include <resapi.h>
15 #include <resistr.h>
16 #include <imgrstyp.h>
17 #include <palrstyp.h>
18 #include <strrstyp.h>
19 #include <fonrstyp.h>
21 #include <objtype.h>
22 #include <dpcgame.h>
23 #include <dpccurm.h>
24 #include <dpcfsys.h>
25 #include <dpcinv.h>
27 #include <palmgr.h>
29 grs_font *gDPCFont = NULL;
30 IRes *gDPCFontRes = NULL;
32 grs_font *gDPCFontAA = NULL;
33 IRes *gDPCFontAARes = NULL;
35 grs_font *gDPCFontMono = NULL;
36 IRes *gDPCFontMonoRes = NULL;
38 grs_font *gDPCFontDimmed = NULL;
39 IRes *gDPCFontDimmedRes = NULL;
41 grs_font *gDPCFontBlue = NULL;
42 IRes *gDPCFontBlueRes = NULL;
44 uint gDPCTextColor = 0;
46 // For the DrawCursor methods:
47 static IRes *gPendingHnd = NULL;
49 //--------------------------------------------------------------------------------------
50 void DPCUtilsInit(void)
52 AutoAppIPtr(ResMan);
53 IRes *fontpal = LoadPCX("fontpal");
54 AssertMsg(fontpal, "Cannot find GUI font palette");
55 grs_bitmap *fontpalbmp = (grs_bitmap *) fontpal->Lock();
57 gDPCFontMonoRes = pResMan->Bind("mainfont",RESTYPE_FONT, NULL, "fonts\\");
58 if (gDPCFontMonoRes != NULL)
60 gDPCFontMono = (grs_font *)gDPCFontMonoRes->Lock();
63 //gDPCFontAA = gDPCFont;
64 //gDPCBoldFontAA = gDPCFont;
66 gDPCFontAARes = pResMan->Bind("mainaa",RESTYPE_FONT, NULL, "fonts\\");
67 if (gDPCFontAARes != NULL)
69 gDPCFontAA = (grs_font *)gDPCFontAARes->Lock();
70 AssertMsg(gDPCFontAA, "Cannot lock mainaa font");
71 gDPCFontAA->pal_id = fontpalbmp->align;
74 gDPCFontDimmedRes = pResMan->Bind("Dimmed",RESTYPE_FONT, NULL, "fonts\\");
75 if (gDPCFontDimmedRes != NULL)
77 gDPCFontDimmed = (grs_font *)gDPCFontDimmedRes->Lock();
78 AssertMsg(gDPCFontDimmed, "Cannot lock dimmed font");
79 gDPCFontDimmed->pal_id = fontpalbmp->align;
82 gDPCFontBlueRes = pResMan->Bind("BlueAA",RESTYPE_FONT, NULL, "fonts\\");
83 if (gDPCFontBlueRes != NULL)
85 gDPCFontBlue = (grs_font *)gDPCFontBlueRes->Lock();
86 AssertMsg(gDPCFontBlue, "Cannot lock blue font");
87 gDPCFontBlue->pal_id = fontpalbmp->align;
90 // what is our default font?
91 gDPCFont = gDPCFontAA;
93 fontpal->Unlock();
94 SafeFreeHnd(&fontpal);
95 gPendingHnd = LoadPCX("hourglas", DPC_INTERFACE_PATH);
98 //--------------------------------------------------------------------------------------
99 void DPCUtilInitColor()
101 int color[3] = {0,255,190}; // { 0, 255, 0};
102 gDPCTextColor = FindColor(color);
106 //--------------------------------------------------------------------------------------
107 void DPCUtilsTerm(void)
109 gDPCFontMonoRes->Unlock();
110 gDPCFontMonoRes->Release();
111 gDPCFontAARes->Unlock();
112 gDPCFontAARes->Release();
113 gDPCFontDimmedRes->Unlock();
114 gDPCFontDimmedRes->Release();
115 gDPCFontBlueRes->Unlock();
116 gDPCFontBlueRes->Release();
117 SafeFreeHnd(&gPendingHnd);
120 //--------------------------------------------------------------------------------------
121 // Utility routine to load correct .PCX bitmap (based on current res).
122 // Ideally, in the long run, this should take an ISearchPath instead of
123 // the current char *path; it'll be more efficient.
125 // For the moment, this is doing a global Lock(), since we have a couple
126 // of DataPeeks around the system. If we can rid ourselves of the DataPeeks,
127 // then we can eliminate this Lock, and the Unlock in SafeFreeHnd.
129 // The returned resource should be freed with SafeFreeHnd().
130 //--------------------------------------------------------------------------------------
131 IRes *LoadPCX(const char *name, char *path, eDPCLoadFlags flags)
134 char hires[16], lores[16], name[16];
136 sscanf(str, "%s %s", hires, lores);
137 if (gHires)
138 strcpy(name, hires);
139 else
140 strcpy(name, lores);
142 // We are not currently assuming that image files are .pcx. This is more
143 // flexible, but possibly slower. We should keep an eye on the time
144 // impact of this:
145 // strcat(name, ".PCX");
147 AutoAppIPtr(ResMan);
148 IRes *pRes = pResMan->Bind(name, RESTYPE_IMAGE, NULL, path);
149 if (!pRes)
151 return NULL;
153 // @NOTE: We are holding a permanent Lock on all images here (which is
154 // eventually Unlocked by SafeFreeHnd). This *may* want to go away later,
155 // but we need to think about the Palette ramifications...
156 grs_bitmap *pbm = (grs_bitmap *) pRes->Lock();
158 // Now that we have the Image, load the Palette as well.
159 // @TBD: We should create an ImageAndPalette resource type, which will
160 // load both of them at a shot, instead of having to do two disk hits.
161 if ((pbm->align == 0) && !(flags & DPCLoadNoPalette))
163 IRes *pPallRes = pResMan->Retype(pRes, RESTYPE_PALETTE, 0);
164 if (pPallRes)
166 uchar *pPall = (uchar *) pPallRes->Lock();
167 pbm->align = palmgr_alloc_pal(pPall);
168 pPallRes->Unlock();
169 // keep it in memory, so don't drop it.
170 //pPallRes->Drop();
171 SafeRelease(pPallRes);
175 return pRes;
178 //--------------------------------------------------------------------------------------
179 // Draw some art, specified by handle
180 //--------------------------------------------------------------------------------------
181 BOOL DrawByHandle(IRes *drawhand, Point pt)
183 if (drawhand != NULL)
185 grs_bitmap *bm = (grs_bitmap *) drawhand->Lock();
186 gr_bitmap(bm, pt.x, pt.y);
187 drawhand->Unlock();
188 return(TRUE);
190 return(FALSE);
193 BOOL DrawByHandleCenter(IRes *drawhand, Point pt)
195 if (drawhand != NULL)
197 grs_bitmap *bm = (grs_bitmap *) drawhand->Lock();
198 gr_bitmap(bm, pt.x - (bm->w / 2), pt.y - (bm->h / 2));
199 drawhand->Unlock();
200 return(TRUE);
202 return(FALSE);
205 BOOL DrawByHandleCenterRotate(IRes *drawhand, Point pt, fixang theta)
207 if (drawhand != NULL)
209 grs_bitmap *bm = (grs_bitmap *) drawhand->Lock();
210 gr_rotate_bitmap(bm, theta, fix_make(pt.x,0), fix_make(pt.y,0));
211 drawhand->Unlock();
213 return(TRUE);
215 return(FALSE);
217 //--------------------------------------------------------------------------------------
218 // Similar to the above, but will do any additional manipulation appropriate
219 // for all cursors. At the moment, this just means that it will tack on the
220 // "pending" icon if a frob request is pending.
221 // This may be getting a little high-level for dpcutils; should we break
222 // it out?
223 //--------------------------------------------------------------------------------------
224 BOOL DrawCursorByHandle(IRes *drawhand, Point pt)
226 if (drawhand != NULL)
228 grs_bitmap *bm = (grs_bitmap *) drawhand->Lock();
229 gr_bitmap(bm, pt.x, pt.y);
230 if (DPCFrobPending() && gPendingHnd)
232 grs_bitmap *bm2;
233 bm2 = (grs_bitmap *) gPendingHnd->Lock();
234 gr_bitmap(bm2, pt.x + bm->w, pt.y + bm->h);
235 gPendingHnd->Unlock();
237 drawhand->Unlock();
238 return(TRUE);
240 return(FALSE);
243 BOOL DrawCursorByHandleCenter(IRes *drawhand, Point pt)
245 grs_bitmap *bm;
246 int dx,dy;
247 if (drawhand != NULL)
249 bm = (grs_bitmap *) drawhand->Lock();
250 dx = pt.x - (bm->w / 2);
251 dy = pt.y - (bm->h / 2);
252 gr_bitmap(bm, dx, dy);
253 if (DPCFrobPending() && gPendingHnd)
255 grs_bitmap *bm2;
256 bm2 = (grs_bitmap *) gPendingHnd->Lock();
257 gr_bitmap(bm2, pt.x + (bm->w / 2), pt.y + (bm->h / 2));
258 gPendingHnd->Unlock();
260 drawhand->Unlock();
262 // add in a stack count, potentially
263 if (DPC_cursor_mode == SCM_DRAGOBJ)
265 char temp[32];
266 if (DPCObjGetQuantity(drag_obj,temp))
268 gr_set_fcolor(gDPCTextColor);
269 gr_font_string(gDPCFontMono, temp, dx + 3 , dy + 3);
272 return(TRUE);
274 return(FALSE);
277 //--------------------------------------------------------------------------------------
278 // Free a IRes *, making sure not to free a NULL, and clearing the value afterwards
279 //--------------------------------------------------------------------------------------
280 void SafeFreeHnd(IRes **hndPtr)
282 if (*hndPtr != NULL)
284 (*hndPtr)->Unlock();
285 (*hndPtr)->Release();
286 *hndPtr = NULL;
290 //--------------------------------------------------------------------------------------
291 // Return the pixel value at x,y within the bitmap specified by the handle
292 //--------------------------------------------------------------------------------------
293 DWORD HandleGetPix(IRes *handle, Point loc)
295 DWORD retval = 0;
296 grs_bitmap *bm;
297 if (handle != NULL)
299 bm = (grs_bitmap *) handle->Lock();
300 if ((loc.x >= bm->w) || (loc.y >= bm->h) || (loc.x < 0) || (loc.y < 0))
301 retval = 0;
302 else
303 retval = gr_get_pixel_bm(bm,loc.x,loc.y);
304 handle->Unlock();
306 return(retval);
309 //--------------------------------------------------------------------------------------
310 // just does the mechanical UI elements, no state changes
311 IRes *gCursorHnd = NULL;
312 extern IRes *gDefaultHnd;
314 void RemoveCursor()
316 gCursorHnd = gDefaultHnd;
319 //--------------------------------------------------------------------------------------
320 // Returns the cursor to it's default state
321 //--------------------------------------------------------------------------------------
322 // hmm, should this check to make sure that isn't going to pop down to nothing?
323 void ClearCursor(void)
325 if (DPC_cursor_mode != SCM_NORMAL)
327 //mprintf("Clearing cursor\n");
329 RemoveCursor();
330 DPC_cursor_mode = SCM_NORMAL;
331 drag_obj = OBJ_NULL;
335 //--------------------------------------------------------------------------------------
336 // Given a bitmap handle, makes that bitmap the current cursor
337 //--------------------------------------------------------------------------------------
338 // do we also want a grs_bitmap version of this?
339 bool SetCursorByHandle(IRes *hnd) // , Cursor *cursorp)
341 RemoveCursor();
343 if (hnd != NULL)
345 gCursorHnd = hnd;
346 return(TRUE);
348 return(FALSE);
350 //--------------------------------------------------------------------------------------
351 // takes in a color triplet
352 int FindColor(int *color)
354 // Lookup the correct color
355 int icol = 0;
356 for (int i = 2; i >= 0; i--)
358 icol <<= 8;
359 icol |= color[i] & 0xFF;
361 return(gr_make_screen_fcolor(icol));
363 //--------------------------------------------------------------------------------------
364 char *string_basepath = "strings/";
365 BOOL DPCStringFetch(char *temp,int bufsize, const char *name, const char *table,int offset)
367 AutoAppIPtr(ResMan);
368 char usename[255];
369 if (strlen(name) == 0)
370 return(FALSE);
372 if (offset == -1)
373 strcpy(usename,name);
374 else
375 sprintf(usename,"%s%d",name,offset);
377 cAutoIPtr<IRes> res = pResMan->Bind(table,
378 RESTYPE_STRING,
379 NULL,
380 string_basepath);
381 cAutoIPtr<IStringRes> strres (IID_IStringRes,res);
382 if (strres == NULL)
383 return(FALSE);
384 const char* s = strres->StringLock(usename);
386 if (s)
388 strncpy(temp,s,bufsize);
389 strres->StringUnlock(usename);
390 return(TRUE);
392 else
394 strcpy(temp,"");
395 return(FALSE);
398 //--------------------------------------------------------------------------------------
399 void DrawVerticalString(char *text, int x, int y, int dy)
401 char s[2] = {'\0'};
402 Point drawpt;
403 //int w;
405 drawpt.x = x;
406 drawpt.y = y;
408 gr_set_fcolor(gDPCTextColor);
410 for (int j=0; j < strlen(text); j++)
412 s[0] = text[j];
413 //w = gr_font_string_width(gDPCFont, s);
414 gr_font_string(gDPCFont, s, drawpt.x, drawpt.y);
415 drawpt.y = drawpt.y + dy;
418 //--------------------------------------------------------------------------------------