2 // a limited and redundant graphics system which allows placing text on the screen
3 // in the case of startup errors or before the real data files are extracted.
5 #include <SDL/SDL_ttf.h>
7 #include "../common/basics.h"
8 #include "../common/BList.h"
10 #include "nxsurface.h"
13 #include "safemode.fdh"
15 using namespace safemode
;
16 using namespace Graphics
;
18 static BList textlist
;
28 static SDL_Surface
*sdl_screen
= NULL
;
29 static TTF_Font
*font
;
31 extern const char *fontfile
; // from font.cpp
32 extern int pointsize
[]; // from font.cpp
35 static int fontheight
= 0;
36 static SDL_Color textcolor
;
37 static bool initilized
= false;
38 static bool have_status
= false;
45 if (screen
) sdl_screen
= screen
->GetSDLSurface();
46 if (!sdl_screen
) throw "graphics not initilized";
50 throw stprintf("couldn't initialize SDL_ttf: %s", TTF_GetError());
53 font
= TTF_OpenFont(fontfile
, pointsize
[SCALE
]);
56 throw stprintf("couldn't open font: '%s': %s", fontfile
, TTF_GetError());
59 static SDL_Color black
;
60 SDL_Surface
*temp
= TTF_RenderText_Solid(font
, "M", black
);
64 SDL_FreeSurface(temp
);
68 throw "initial render failed";
76 moveto(SM_UPPER_THIRD
);
81 catch(const char *error
)
83 staterr("safemode::init: failed startup: %s", error
);
89 void safemode::close()
93 FillRect(0, 0, SCREEN_WIDTH
, SCREEN_HEIGHT
, 0, 0, 0);
98 if (font
) TTF_CloseFont(font
);
104 void c------------------------------() {}
107 void safemode::moveto(int y
)
112 nexty
= ((SCREEN_HEIGHT
* SCALE
) / 2) - (fontheight
/ 2);
116 nexty
= ((SCREEN_HEIGHT
* SCALE
) / 4) - (fontheight
/ 2);
120 nexty
= ((SCREEN_HEIGHT
* SCALE
) / 4) - (fontheight
/ 2);
121 nexty
= (SCREEN_HEIGHT
* SCALE
) - nexty
;
125 nexty
= ((SCREEN_HEIGHT
* SCALE
) / 2) - (fontheight
/ 2);
126 nexty
-= (32 * SCALE
);
135 bool safemode::print(const char *fmt
, ...)
142 vsnprintf(buffer
, sizeof(buffer
), fmt
, ar
);
145 stat("safemode print: '%s'", buffer
);
149 sfc
= TTF_RenderText_Solid(font
, buffer
, textcolor
);
152 staterr("safemode::print: failed to render string: '%s'", fmt
);
156 TextRecord
*tr
= new TextRecord
;
157 tr
->x
= (((SCREEN_WIDTH
* SCALE
) / 2) - (sfc
->w
/ 2));
160 textlist
.AddItem(tr
);
164 nexty
+= (fontheight
+ (1 * SCALE
));
168 void safemode::clear()
170 while(textlist
.CountItems())
171 delete (TextRecord
*)textlist
.RemoveItem(textlist
.CountItems() - 1);
173 moveto(SM_UPPER_THIRD
);
179 FillRect(0, 0, SCREEN_WIDTH
, SCREEN_HEIGHT
, 0x20, 0x20, 0x20);
183 TextRecord
*tr
= (TextRecord
*)textlist
.ItemAt(i
);
190 SDL_BlitSurface(tr
->sfc
, NULL
, sdl_screen
, &dstrect
);
193 SDL_Flip(sdl_screen
);
197 int safemode::run_until_key(bool delay
)
199 stat("run_until_key()");
200 uint32_t start
= SDL_GetTicks();
209 if (delay
&& (SDL_GetTicks() - start
) < 500)
212 while(last_sdl_key
== -1);
218 void safemode::status(const char *fmt
, ...)
224 vsnprintf(buffer
, sizeof(buffer
), fmt
, ar
);
234 void safemode::clearstatus()
238 // remove all except the header
239 while(textlist
.CountItems() > 1)
240 delete (TextRecord
*)textlist
.RemoveItem(textlist
.CountItems() - 1);
248 void c------------------------------() {}
251 TextRecord::TextRecord()
256 TextRecord::~TextRecord()
260 SDL_FreeSurface(sfc
);