NXEngine v1.0.0.5
[NXEngine.git] / graphics / safemode.cpp
blob399ec17fd6a74827b0a69b6cd7535283d28563d2
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.
4 #include <stdarg.h>
5 #include "../nx.h"
6 #include "safemode.h"
7 #include "safemode.fdh"
9 using namespace safemode;
10 using namespace Graphics;
12 static int nexty = 0;
13 static bool initilized = false;
14 static SDL_Rect printrect;
15 static NXColor backcolor(0x20, 0x20, 0x20);
17 static bool have_status = false;
18 static SDL_Rect statusrect;
20 bool safemode::init()
22 moveto(SM_UPPER_THIRD);
23 initilized = true;
25 return 0;
28 void safemode::close()
30 if (initilized)
32 ClearScreen(BLACK);
33 screen->Flip();
35 clear();
40 void c------------------------------() {}
43 void safemode::moveto(int y)
45 switch(y)
47 case SM_CENTER:
48 nexty = (SCREEN_HEIGHT / 2) - (GetFontHeight() / 2);
49 break;
51 case SM_UPPER_THIRD:
52 nexty = (SCREEN_HEIGHT / 4) - (GetFontHeight() / 2);
53 break;
55 case SM_LOWER_THIRD:
56 nexty = (SCREEN_HEIGHT / 4) - (GetFontHeight() / 2);
57 nexty = (SCREEN_HEIGHT - nexty);
58 break;
60 case SM_MIDUPPER_Y:
61 nexty = (SCREEN_HEIGHT / 2) - (GetFontHeight() / 2);
62 nexty -= 32;
63 break;
65 default:
66 nexty = y;
67 break;
71 bool safemode::print(const char *fmt, ...)
73 va_list ar;
74 char buffer[128];
76 va_start(ar, fmt);
77 vsnprintf(buffer, sizeof(buffer), fmt, ar);
78 va_end(ar);
80 stat("safemode print: '%s'", buffer);
82 int fontwidth = GetFontWidth(buffer, 0, false);
83 int fontheight = GetFontHeight();
85 if (buffer[0])
87 int x = ((SCREEN_WIDTH / 2) - (fontwidth / 2));
89 printrect.x = x;
90 printrect.y = nexty;
91 printrect.w = fontwidth;
92 printrect.h = fontheight;
94 font_draw(x, nexty, buffer, 0, &greenfont);
95 screen->Flip();
98 SDL_Delay(50);
100 nexty += (fontheight + 1);
101 return 0;
104 void safemode::clear()
106 ClearScreen(backcolor);
107 moveto(SM_UPPER_THIRD);
111 int safemode::run_until_key(bool delay)
113 stat("run_until_key()");
114 uint32_t start = SDL_GetTicks();
116 last_sdl_key = -1;
119 input_poll();
120 SDL_Delay(50);
122 if (delay && (SDL_GetTicks() - start) < 500)
123 last_sdl_key = -1;
125 while(last_sdl_key == -1);
127 stat("returning key %d", last_sdl_key);
128 return last_sdl_key;
132 void safemode::status(const char *fmt, ...)
134 va_list ar;
135 char buffer[128];
137 va_start(ar, fmt);
138 vsnprintf(buffer, sizeof(buffer), fmt, ar);
139 va_end(ar);
141 clearstatus();
142 moveto(SM_CENTER);
143 print("%s", buffer);
145 statusrect = printrect;
146 have_status = true;
149 void safemode::clearstatus()
151 if (have_status)
153 FillRect(statusrect.x, statusrect.y, \
154 statusrect.x + (statusrect.w - 1), \
155 statusrect.y + (statusrect.h - 1),
156 backcolor);
158 moveto(SM_CENTER);
159 have_status = false;