1 
2 // graphics routines
3 #include "../sdl/include/LRSDL.h"
4 
5 #include <stdlib.h>
6 #include "../config.h"
7 #include "graphics.h"
8 #include "tileset.h"
9 #include "sprites.h"
10 #include "../dirnames.h"
11 #include "../nx.h"
12 #include "graphics.fdh"
13 
14 NXSurface *screen = NULL;				// created from SDL's screen
15 static NXSurface *drawtarget = NULL;	// target of DrawRect etc; almost always screen
16 int screen_bpp;
17 
18 const NXColor DK_BLUE(0, 0, 0x21);		// the popular dk blue backdrop color
19 const NXColor BLACK(0, 0, 0);			// pure black, only works if no colorkey
20 const NXColor CLEAR(0, 0, 0);			// the transparent/colorkey color
21 
22 static int current_res = -1;
23 
init(int resolution)24 bool Graphics::init(int resolution)
25 {
26 	screen_bpp = 16;	// the default
27 
28 	if (SetResolution(resolution, false))
29 		return 1;
30 
31 	if (Tileset::Init())
32 		return 1;
33 
34 	if (Sprites::Init())
35 		return 1;
36 
37 	return 0;
38 }
39 
close()40 void Graphics::close()
41 {
42 	NX_LOG("Graphics::Close()\n");
43 }
44 
45 /*
46 void c------------------------------() {}
47 */
48 extern unsigned pitch;
49 
InitVideo()50 bool Graphics::InitVideo()
51 {
52 SDL_Surface *sdl_screen;
53 
54 	if (drawtarget == screen) drawtarget = NULL;
55 	if (screen) delete screen;
56 
57    sdl_screen = (SDL_Surface*)AllocNewSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT);
58 	pitch = 320 << 1;
59 
60 	if (!sdl_screen)
61 	{
62 		NX_ERR("Graphics::InitVideo: error setting video mode\n");
63 		return 1;
64 	}
65 
66 	screen = new NXSurface(sdl_screen, false);
67 	if (!drawtarget) drawtarget = screen;
68 	return 0;
69 }
70 
FlushAll()71 bool Graphics::FlushAll()
72 {
73 	NX_LOG("Graphics::FlushAll()\n");
74 	Sprites::FlushSheets();
75 	Tileset::Reload();
76 	map_flush_graphics();
77 	return font_reload();
78 }
79 
SetFullscreen(bool enable)80 void Graphics::SetFullscreen(bool enable)
81 {
82 }
83 
84 // change the video mode to one of the available resolution codes, currently:
85 // 0 - 640x480, Fullscreen
86 // 1 - Windowed scale x1 (320x240)
87 // 2 - Windowed scale x2 (640x480)
88 // 3 - Windowed scale x3 (960x720)
SetResolution(int r,bool restoreOnFailure)89 bool Graphics::SetResolution(int r, bool restoreOnFailure)
90 {
91 	NX_LOG("Graphics::SetResolution(%d)\n", r);
92 	if (r == current_res)
93 		return 0;
94 
95 	if (Graphics::InitVideo())
96 		return 1;
97 
98 	if (Graphics::FlushAll())
99 		return 1;
100 
101 	return 0;
102 }
103 
104 // return a pointer to a null-terminated list of available resolutions.
GetResolutions()105 const char **Graphics::GetResolutions()
106 {
107 static const char *res_str[]   =
108 {
109 	"Fullscreen",
110 	"320x240", "640x480", "960x720",
111 	NULL
112 };
113 
114 	return res_str;
115 }
116 
117 /*
118 void c------------------------------() {}
119 */
120 
121 // copy a sprite into the current tileset.
122 // used to obtain the images for the star tiles (breakable blocks),
123 // and for animated motion tiles in Waterway.
CopySpriteToTile(int spr,int tileno,int offset_x,int offset_y)124 void Graphics::CopySpriteToTile(int spr, int tileno, int offset_x, int offset_y)
125 {
126 NXRect srcrect, dstrect;
127 
128 	srcrect.x = (sprites[spr].frame[0].dir[0].sheet_offset.x + offset_x);
129 	srcrect.y = (sprites[spr].frame[0].dir[0].sheet_offset.y + offset_y);
130 	srcrect.w = TILE_W;
131 	srcrect.h = TILE_H;
132 
133 	dstrect.x = (tileno % 16) * TILE_W;
134 	dstrect.y = (tileno / 16) * TILE_H;
135 	dstrect.w = TILE_W;
136 	dstrect.h = TILE_H;
137 
138 	NXSurface *tileset = Tileset::GetSurface();
139 	NXSurface *spritesheet = Sprites::get_spritesheet(sprites[spr].spritesheet);
140 
141 	if (tileset && spritesheet)
142 	{
143 		// blank out the old tile data with clear
144 		tileset->FillRect(&dstrect, CLEAR);
145 
146 		// copy the sprite over
147 		BlitSurface(spritesheet, &srcrect, tileset, &dstrect);
148 	}
149 }
150 
151 /*
152 void c------------------------------() {}
153 */
154 
155 // blit from one surface to another, just like SDL_BlitSurface.
BlitSurface(NXSurface * src,NXRect * srcrect,NXSurface * dst,NXRect * dstrect)156 void Graphics::BlitSurface(NXSurface *src, NXRect *srcrect, NXSurface *dst, NXRect *dstrect)
157 {
158 	dst->DrawSurface(src, dstrect->x, dstrect->y, \
159 					 srcrect->x, srcrect->y, srcrect->w, srcrect->h);
160 }
161 
162 /*
163 void c------------------------------() {}
164 */
165 
166 // draw the entire surface to the screen at the given coordinates.
DrawSurface(NXSurface * src,int x,int y)167 void Graphics::DrawSurface(NXSurface *src, int x, int y)
168 {
169 	drawtarget->DrawSurface(src, x, y);
170 }
171 
172 
173 // blit the specified portion of the surface to the screen
DrawSurface(NXSurface * src,int dstx,int dsty,int srcx,int srcy,int wd,int ht)174 void Graphics::DrawSurface(NXSurface *src, \
175 						   int dstx, int dsty, int srcx, int srcy, int wd, int ht)
176 {
177 	drawtarget->DrawSurface(src, dstx, dsty, srcx, srcy, wd, ht);
178 }
179 
180 
181 // blit the specified surface across the screen in a repeating pattern
BlitPatternAcross(NXSurface * sfc,int x_dst,int y_dst,int y_src,int height)182 void Graphics::BlitPatternAcross(NXSurface *sfc, int x_dst, int y_dst, int y_src, int height)
183 {
184 	drawtarget->BlitPatternAcross(sfc, x_dst, y_dst, y_src, height);
185 }
186 
187 /*
188 void c------------------------------() {}
189 */
190 
DrawRect(int x1,int y1,int x2,int y2,NXColor color)191 void Graphics::DrawRect(int x1, int y1, int x2, int y2, NXColor color)
192 {
193 	drawtarget->DrawRect(x1, y1, x2, y2, color);
194 }
195 
FillRect(int x1,int y1,int x2,int y2,NXColor color)196 void Graphics::FillRect(int x1, int y1, int x2, int y2, NXColor color)
197 {
198 	drawtarget->FillRect(x1, y1, x2, y2, color);
199 }
200 
DrawPixel(int x,int y,NXColor color)201 void Graphics::DrawPixel(int x, int y, NXColor color)
202 {
203 	drawtarget->DrawPixel(x, y, color);
204 }
205 
ClearScreen(NXColor color)206 void Graphics::ClearScreen(NXColor color)
207 {
208 	drawtarget->Clear(color.r, color.g, color.b);
209 }
210 
211 /*
212 void c------------------------------() {}
213 */
214 
DrawRect(int x1,int y1,int x2,int y2,uint8_t r,uint8_t g,uint8_t b)215 void Graphics::DrawRect(int x1, int y1, int x2, int y2, uint8_t r, uint8_t g, uint8_t b)
216 {
217 	drawtarget->DrawRect(x1, y1, x2, y2, r, g, b);
218 }
219 
FillRect(int x1,int y1,int x2,int y2,uint8_t r,uint8_t g,uint8_t b)220 void Graphics::FillRect(int x1, int y1, int x2, int y2, uint8_t r, uint8_t g, uint8_t b)
221 {
222 	drawtarget->FillRect(x1, y1, x2, y2, r, g, b);
223 }
224 
DrawPixel(int x,int y,uint8_t r,uint8_t g,uint8_t b)225 void Graphics::DrawPixel(int x, int y, uint8_t r, uint8_t g, uint8_t b)
226 {
227 	drawtarget->DrawPixel(x, y, r, g, b);
228 }
229 
ClearScreen(uint8_t r,uint8_t g,uint8_t b)230 void Graphics::ClearScreen(uint8_t r, uint8_t g, uint8_t b)
231 {
232 	drawtarget->Clear(r, g, b);
233 }
234 
235 /*
236 void c------------------------------() {}
237 */
238 
set_clip_rect(int x,int y,int w,int h)239 void Graphics::set_clip_rect(int x, int y, int w, int h)
240 {
241 	drawtarget->set_clip_rect(x, y, w, h);
242 }
243 
set_clip_rect(NXRect * rect)244 void Graphics::set_clip_rect(NXRect *rect)
245 {
246 	drawtarget->set_clip_rect(rect);
247 }
248 
clear_clip_rect()249 void Graphics::clear_clip_rect()
250 {
251 	drawtarget->clear_clip_rect();
252 }
253 
254 /*
255 void c------------------------------() {}
256 */
257 
258 // change the target surface of operation like DrawRect to something
259 // other than the screen.
SetDrawTarget(NXSurface * surface)260 void Graphics::SetDrawTarget(NXSurface *surface)
261 {
262 	drawtarget = surface;
263 }
264 
265 
266 
267 
268 
269 
270 
271 
272