Debian Codegraph @

src/gfx.cpp

raw
1 /**
2 * Graphics data to sort out
3
4 * Copyright (C) 2007, 2008, 2009, 2010, 2014, 2015 Sylvain Beucler
5
6 * This file is part of GNU FreeDink
7
8 * GNU FreeDink is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 3 of the
11 * License, or (at your option) any later version.
12
13 * GNU FreeDink is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <math.h>
29
30 #include "SDL.h"
31 #include "SDL_image.h"
32 #include "SDL2_framerate.h"
33
34 #include "freedink_xpm.h"
35 #include "DMod.h"
36 #include "io_util.h"
37 #include "gfx.h"
38 #include "IOGfxPrimitives.h"
39 #include "IOGfxDisplaySW.h"
40 #include "IOGfxDisplayGL2.h"
41 #include "ImageLoader.h"
42 #include "gfx_fonts.h"
43 #include "gfx_palette.h"
44 #include "gfx_sprites.h"
45 #include "paths.h"
46 #include "log.h"
47
48
49 /* Is the screen depth more than 8bit? */
50 int truecolor = 0;
51
52 /* Backbuffer */
53 IOGfxSurface* IOGFX_backbuffer = NULL;
54
55 /* Base/background scene */
56 /* Contains the background, which is reused for each new frame. It is
57 overwritten when switching to another screen. However, it can
58 change during a screen:
59 1) animated tiles (water & fire)
60 2) when a sprite is written on the background (eg when an enemy dies)
61 3) with various hacks such as fill_screen() and copy_bmp_to_screen() */
62 /* Those changes may conflict with each other (eg: an animated tile
63 overwrites half the carcass of a dead enemy). */
64 /* After the background is done, all the other operations are applied
65 on GFX_backbuffer, the double buffer which is directly used by the
66 physical screen. */
67 IOGfxSurface* IOGFX_background = NULL;
68
69 /* Temporary buffer for scrolling screen transition +
70 show_bmp/process_show_bmp */
71 IOGfxSurface* IOGFX_tmp1 = NULL;
72 /* Temporary buffer for scrolling screen transition +
73 load_sprite* */
74 IOGfxSurface* IOGFX_tmp2 = NULL;
75
76 /* Skip flipping the double buffer for this frame only - used when
77 setting up show_bmp and copy_bmp */
78 /*bool*/int abort_this_flip = /*false*/0;
79
80
81 /* Time elapsed since last fade computation; 0 is disabled */
82 Uint32 truecolor_fade_lasttick = 0;
83
84 FPSmanager framerate_manager;
85
86 /* Main window and associated renderer */
87 IOGfxDisplay* g_display = NULL;
88
89
90 /**
91 * Graphics subsystem initalization
92 */
93 int gfx_init(bool dinkgl, bool windowed, char* splash_path) {
94 if (dinkgl)
95 g_display = new IOGfxDisplayGL2(GFX_RES_W, GFX_RES_H, truecolor,
96 windowed ? SDL_WINDOW_RESIZABLE : SDL_WINDOW_FULLSCREEN_DESKTOP);
97 else
98 g_display = new IOGfxDisplaySW(GFX_RES_W, GFX_RES_H, truecolor,
99 windowed ? SDL_WINDOW_RESIZABLE : SDL_WINDOW_FULLSCREEN_DESKTOP);
100 /* Note: SDL_WINDOW_FULLSCREEN may not respect aspect ratio, _DESKTOP does */
101 if (!g_display->open()) {
102 // TODO: try dinkgl->soft fallback, when dinkgl is default
103 log_error("Could not open display");
104 return -1;
105 }
106 g_display->logDisplayInfo();
107
108 /* Default palette */
109 gfx_palette_reset();
110
111 /* Create and set the physical palette */
112 if (gfx_palette_set_from_bmp("Tiles/Ts01.bmp") < 0)
113 log_error("Failed to load default palette from Tiles/Ts01.bmp");
114
115 /* Set the reference palette */
116 gfx_palette_get_phys(GFX_ref_pal);
117
118 /* Initialize reference buffer */
119 ImageLoader::initBlitFormat(g_display->getFormat());
120
121 // TODO: make backbuffer resize quality linear instead of nearest
122 IOGFX_backbuffer = g_display->allocBuffer(GFX_RES_W, GFX_RES_H);
123 IOGFX_background = g_display->allocBuffer(GFX_RES_W, GFX_RES_H);
124 IOGFX_tmp1 = g_display->allocBuffer(GFX_RES_W, GFX_RES_H);
125 IOGFX_tmp2 = g_display->allocBuffer(GFX_RES_W, GFX_RES_H);
126 if (IOGFX_backbuffer == NULL || IOGFX_background == NULL
127 || IOGFX_tmp1 == NULL || IOGFX_tmp2 == NULL) {
128 log_error("Cannot create global buffers");
129 return -1;
130 }
131
132 /* Display splash picture, as early as possible */
133 {
134 IOGfxSurface* splash = NULL;
135 FILE* in = paths_dmodfile_fopen(splash_path, "rb");
136 if (in == NULL) {
137 in = paths_fallbackfile_fopen(splash_path, "rb");
138 }
139 if (in != NULL) {
140 SDL_Surface* image = ImageLoader::loadToBlitFormat(in);
141 if (image != NULL)
142 splash = g_display->upload(image);
143 }
144 if (splash == NULL) {
145 log_error("Cannot load splash image %s", splash_path);
146 } else {
147 /* Copy splash to the background buffer so that D-Mod can
148 start an effect from it (e.g. Pilgrim Quest's burning
149 splash screen effect) */
150 if (IOGFX_background->blit(splash, NULL, NULL) < 0)
151 log_error("Error blitting splash to temp buffer");
152 delete splash;
153 }
154
155 /* Copy splash screen (again) to the screen during loading time */
156 if (IOGFX_backbuffer->blit(IOGFX_background, NULL, NULL) < 0)
157 log_error("Error blitting splash to back buffer");
158
159 g_display->clear();
160 g_display->flipStretch(IOGFX_backbuffer);
161 }
162
163
164 /* Fonts system, default fonts */
165 if (gfx_fonts_init() < 0)
166 return -1; /* error message set in gfx_fonts_init */
167
168 /* make all pointers to NULL */
169 memset(&k, 0, sizeof(k));
170 memset(&seq, 0, sizeof(seq));
171
172
173 SDL_initFramerate(&framerate_manager);
174 /* The official v1.08 .exe runs 50-60 FPS in practice, despite the
175 documented intent of running 83 FPS (or 12ms delay). */
176 /* SDL_setFramerate(manager, 83); */
177 SDL_setFramerate(&framerate_manager, FPS);
178
179 return 0;
180 }
181
182 /**
183 * Unload graphics subsystem
184 */
185 void gfx_quit()
186 {
187 gfx_fonts_quit();
188
189 sprites_unload();
190
191 if (IOGFX_backbuffer != NULL) delete IOGFX_backbuffer;
192 if (IOGFX_background != NULL) delete IOGFX_background;
193 if (IOGFX_tmp1 != NULL) delete IOGFX_tmp1;
194 if (IOGFX_tmp2 != NULL) delete IOGFX_tmp2;
195
196 IOGFX_backbuffer = NULL;
197 IOGFX_background = NULL;
198 IOGFX_tmp1 = NULL;
199 IOGFX_tmp2 = NULL;
200
201 if (g_display != NULL) delete g_display;
202 g_display = NULL;
203 }
204
205 /**
206 * Print GFX memory usage
207 */
208 void gfx_log_meminfo()
209 {
210 int total = 0;
211
212 {
213 int sum = 0;
214 sum = IOGFX_backbuffer->getMemUsage();
215 log_debug("GFX screen = %8d", sum);
216 total += sum;
217 }
218
219 {
220 int sum = 0;
221 IOGfxSurface* s = NULL;
222 s = IOGFX_background;
223 sum += s->getMemUsage();
224 s = IOGFX_tmp1;
225 sum += s->getMemUsage();
226 s = IOGFX_tmp2;
227 sum += s->getMemUsage();
228 log_debug("GFX buf = %8d", sum);
229 total += sum;
230 }
231
232 {
233 int sum = 0;
234 int i = 0;
235 IOGfxSurface* s = NULL;
236 for (; i < MAX_SPRITES; i++)
237 {
238 s = GFX_k[i].k;
239 if (s != NULL)
240 sum += s->getMemUsage();
241 }
242 log_debug("GFX bmp = %8d", sum);
243 total += sum;
244 }
245
246 {
247 int sum = g_dmod.bgTilesets.getMemUsage();
248 log_debug("GFX tiles = %8d", sum);
249 total += sum;
250 }
251
252 log_debug("GFX total = %8d (+ ~150kB fonts)", total);
253 }