graphics.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2004-2006 Gilead Kutnick <exophase@adelphia.net>
  4. * Copyright (C) 2007 Alistair John Strachan <alistair@devzero.co.uk>
  5. * Copyright (C) 2017 Dr Lancer-X <drlancer@megazeux.org>
  6. *
  7. * YUV Renderers:
  8. * Copyright (C) 2007 Alan Williams <mralert@gmail.com>
  9. *
  10. * OpenGL #2 Renderer:
  11. * Copyright (C) 2007 Joel Bouchard Lamontagne <logicow@gmail.com>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <sys/stat.h>
  32. #include "graphics.h"
  33. #include "world.h"
  34. #include "configure.h"
  35. #include "event.h"
  36. #include "render.h"
  37. #include "render_layer.h"
  38. #include "renderers.h"
  39. #include "platform.h"
  40. #include "error.h"
  41. #ifdef CONFIG_PNG
  42. #include "pngops.h"
  43. #endif
  44. #ifdef CONFIG_SDL
  45. #include "SDL.h"
  46. #ifdef CONFIG_ICON
  47. #include "SDL_syswm.h"
  48. #endif // CONFIG_ICON
  49. #include "compat_sdl.h"
  50. #include "render_sdl.h"
  51. #endif // CONFIG_SDL
  52. #include "util.h"
  53. #define CURSOR_BLINK_RATE 115
  54. __editor_maybe_static struct graphics_data graphics;
  55. static boolean graphics_was_initialized;
  56. static const struct renderer_data renderers[] =
  57. {
  58. #if defined(CONFIG_RENDER_SOFT)
  59. { "software", render_soft_register },
  60. #endif
  61. #if defined(CONFIG_RENDER_SOFTSCALE)
  62. { "softscale", render_softscale_register },
  63. { "overlay1", render_softscale_register },
  64. { "overlay2", render_softscale_register },
  65. #endif
  66. #if defined(CONFIG_RENDER_GL_FIXED)
  67. { "opengl1", render_gl1_register },
  68. { "opengl2", render_gl2_register },
  69. #endif
  70. #if defined(CONFIG_RENDER_GL_PROGRAM)
  71. { "glsl", render_glsl_register },
  72. { "auto_glsl", render_auto_glsl_register },
  73. #endif
  74. #if defined(CONFIG_RENDER_YUV)
  75. { "overlay1", render_yuv1_register },
  76. { "overlay2", render_yuv2_register },
  77. #endif
  78. #if defined(CONFIG_RENDER_GP2X)
  79. { "gp2x", render_gp2x_register },
  80. #endif
  81. #if defined(CONFIG_NDS)
  82. { "nds", render_nds_register },
  83. #endif
  84. #if defined(CONFIG_3DS)
  85. { "3ds", render_ctr_register },
  86. #endif
  87. #if defined(CONFIG_WII)
  88. #if defined(CONFIG_RENDER_GX)
  89. { "gx", render_gx_register },
  90. #endif
  91. { "xfb", render_xfb_register },
  92. #endif
  93. { NULL, NULL }
  94. };
  95. static const struct rgb_color default_pal[16] =
  96. {
  97. /* r, g, b, unused */
  98. { 0, 0, 0, 0 },
  99. { 0, 0, 170, 0 },
  100. { 0, 170, 0, 0 },
  101. { 0, 170, 170, 0 },
  102. { 170, 0, 0, 0 },
  103. { 170, 0, 170, 0 },
  104. { 170, 85, 0, 0 },
  105. { 170, 170, 170, 0 },
  106. { 85, 85, 85, 0 },
  107. { 85, 85, 255, 0 },
  108. { 85, 255, 85, 0 },
  109. { 85, 255, 255, 0 },
  110. { 255, 85, 85, 0 },
  111. { 255, 85, 255, 0 },
  112. { 255, 255, 85, 0 },
  113. { 255, 255, 255, 0 }
  114. };
  115. static void remap_charbyte(struct graphics_data *graphics, Uint16 chr,
  116. Uint8 byte)
  117. {
  118. if(graphics->renderer.remap_charbyte)
  119. graphics->renderer.remap_charbyte(graphics, chr, byte);
  120. }
  121. static void remap_char(struct graphics_data *graphics, Uint16 chr)
  122. {
  123. if(graphics->renderer.remap_char)
  124. graphics->renderer.remap_char(graphics, chr);
  125. }
  126. static void remap_char_range(struct graphics_data *graphics, Uint16 first,
  127. Uint16 len)
  128. {
  129. if(graphics->renderer.remap_char_range)
  130. graphics->renderer.remap_char_range(graphics, first, len);
  131. }
  132. void ec_change_byte(Uint16 chr, Uint8 byte, Uint8 new_value)
  133. {
  134. chr = chr % PROTECTED_CHARSET_POSITION;
  135. graphics.charset[(chr * CHAR_SIZE) + byte] = new_value;
  136. remap_charbyte(&graphics, chr, byte);
  137. }
  138. void ec_change_char(Uint16 chr, char *matrix)
  139. {
  140. chr = chr % PROTECTED_CHARSET_POSITION;
  141. memcpy(graphics.charset + (chr * CHAR_SIZE), matrix, CHAR_SIZE);
  142. remap_char(&graphics, chr);
  143. }
  144. Uint8 ec_read_byte(Uint16 chr, Uint8 byte)
  145. {
  146. chr = chr % PROTECTED_CHARSET_POSITION;
  147. return graphics.charset[(chr * CHAR_SIZE) + byte];
  148. }
  149. void ec_read_char(Uint16 chr, char *matrix)
  150. {
  151. chr = chr % PROTECTED_CHARSET_POSITION;
  152. memcpy(matrix, graphics.charset + (chr * CHAR_SIZE), CHAR_SIZE);
  153. }
  154. void ec_clear_set(void)
  155. {
  156. if(layer_renderer_check(false))
  157. {
  158. memset(graphics.charset, 0, PROTECTED_CHARSET_POSITION * CHAR_SIZE);
  159. remap_char_range(&graphics, 0, FULL_CHARSET_SIZE);
  160. }
  161. // For compatibility with old renderers, clear only the first charset;
  162. // the second could be a duplicate of the protected set.
  163. else
  164. {
  165. memset(graphics.charset, 0, CHARSET_SIZE * CHAR_SIZE);
  166. remap_char_range(&graphics, 0, CHARSET_SIZE);
  167. }
  168. }
  169. Sint32 ec_load_set(char *name)
  170. {
  171. FILE *fp = fopen_unsafe(name, "rb");
  172. Uint16 count;
  173. if(fp)
  174. {
  175. count = fread(graphics.charset, CHAR_SIZE, PROTECTED_CHARSET_POSITION, fp);
  176. fclose(fp);
  177. if(count > 0)
  178. {
  179. // some renderers may want to map charsets to textures
  180. remap_char_range(&graphics, 0, count);
  181. return 0;
  182. }
  183. }
  184. return -1;
  185. }
  186. __editor_maybe_static void ec_load_set_secondary(const char *name,
  187. Uint8 *dest)
  188. {
  189. FILE *fp = fopen_unsafe(name, "rb");
  190. Uint16 count;
  191. if(fp)
  192. {
  193. count = fread(dest, CHAR_SIZE, CHARSET_SIZE, fp);
  194. fclose(fp);
  195. // This might have been somewhere in the charset, so
  196. // some renderers may want to map charsets to textures
  197. if(count > 0)
  198. remap_char_range(&graphics, 0, FULL_CHARSET_SIZE);
  199. }
  200. }
  201. Sint32 ec_load_set_var(char *name, Uint16 pos, int version)
  202. {
  203. Uint32 size = CHARSET_SIZE;
  204. FILE *fp = fopen_unsafe(name, "rb");
  205. Uint32 maxChars = PROTECTED_CHARSET_POSITION;
  206. Uint16 count;
  207. if(version < V290)
  208. maxChars = 256;
  209. if(fp)
  210. {
  211. size = ftell_and_rewind(fp) / CHAR_SIZE;
  212. if(size + pos >= 256 && maxChars > 256 && !layer_renderer_check(true))
  213. maxChars = 256;
  214. if(size + pos > maxChars)
  215. size = maxChars - pos;
  216. count = fread(graphics.charset + (pos * CHAR_SIZE), CHAR_SIZE, size, fp);
  217. fclose(fp);
  218. // some renderers may want to map charsets to textures
  219. if(count > 0)
  220. remap_char_range(&graphics, pos, count);
  221. return count;
  222. }
  223. return -1;
  224. }
  225. void ec_mem_load_set(Uint8 *chars, size_t len)
  226. {
  227. // This is used only for legacy and ZIP world loading and the default charsets
  228. // Use ec_clear_set() in conjunction with this for world loads.
  229. Uint16 count;
  230. if(!layer_renderer_check(false) && len > CHAR_SIZE * CHARSET_SIZE)
  231. len = CHAR_SIZE * CHARSET_SIZE;
  232. if(len > CHAR_SIZE * PROTECTED_CHARSET_POSITION)
  233. len = CHAR_SIZE * PROTECTED_CHARSET_POSITION;
  234. memcpy(graphics.charset, chars, len);
  235. // some renderers may want to map charsets to textures
  236. count = len / CHAR_SIZE;
  237. if(count > 0)
  238. remap_char_range(&graphics, 0, count);
  239. }
  240. void ec_mem_save_set(Uint8 *chars)
  241. {
  242. memcpy(chars, graphics.charset, CHAR_SIZE * CHARSET_SIZE);
  243. }
  244. void ec_mem_load_set_var(char *chars, size_t len, Uint16 pos, int version)
  245. {
  246. Uint32 maxChars = PROTECTED_CHARSET_POSITION;
  247. Uint32 offset = pos * CHAR_SIZE;
  248. Uint16 count = (len + CHAR_SIZE - 1) / CHAR_SIZE;
  249. if(version < V290)
  250. maxChars = 256;
  251. if(len + offset > (256 * CHAR_SIZE) && !layer_renderer_check(true))
  252. maxChars = 256;
  253. if(pos > maxChars)
  254. return;
  255. if(count > maxChars - pos)
  256. {
  257. count = maxChars - pos;
  258. len = count * CHAR_SIZE;
  259. }
  260. memcpy(graphics.charset + offset, chars, len);
  261. // some renderers may want to map charsets to textures
  262. if(count > 0)
  263. remap_char_range(&graphics, pos, count);
  264. }
  265. void ec_mem_save_set_var(Uint8 *chars, size_t len, Uint16 pos)
  266. {
  267. Uint32 offset = pos * CHAR_SIZE;
  268. Uint32 size = MIN(PROTECTED_CHARSET_POSITION * CHAR_SIZE - offset, len);
  269. if(pos < PROTECTED_CHARSET_POSITION)
  270. memcpy(chars, graphics.charset + offset, size);
  271. }
  272. __editor_maybe_static void ec_load_mzx(void)
  273. {
  274. ec_mem_load_set(graphics.default_charset, CHAR_SIZE * CHARSET_SIZE);
  275. }
  276. static void update_colors(struct rgb_color *palette, Uint32 count)
  277. {
  278. graphics.renderer.update_colors(&graphics, palette, count);
  279. }
  280. static Uint32 make_palette(struct rgb_color *palette)
  281. {
  282. Uint32 i;
  283. Uint32 paletteSize;
  284. // Is SMZX mode set?
  285. if(graphics.screen_mode)
  286. {
  287. // SMZX mode 1: set all colors to diagonals
  288. if(graphics.screen_mode == 1)
  289. {
  290. for(i = 0; i < SMZX_PAL_SIZE; i++)
  291. {
  292. palette[i].r =
  293. ((graphics.intensity_palette[i & 15].r << 1) +
  294. graphics.intensity_palette[i >> 4].r) / 3;
  295. palette[i].g =
  296. ((graphics.intensity_palette[i & 15].g << 1) +
  297. graphics.intensity_palette[i >> 4].g) / 3;
  298. palette[i].b =
  299. ((graphics.intensity_palette[i & 15].b << 1) +
  300. graphics.intensity_palette[i >> 4].b) / 3;
  301. }
  302. }
  303. else
  304. {
  305. memcpy(palette, graphics.intensity_palette,
  306. sizeof(struct rgb_color) * SMZX_PAL_SIZE);
  307. }
  308. paletteSize = SMZX_PAL_SIZE;
  309. }
  310. else
  311. {
  312. memcpy(palette, graphics.intensity_palette,
  313. sizeof(struct rgb_color) * PAL_SIZE);
  314. paletteSize = PAL_SIZE;
  315. }
  316. memcpy(palette + paletteSize, graphics.protected_palette,
  317. sizeof(struct rgb_color) * PROTECTED_PAL_SIZE);
  318. graphics.protected_pal_position = paletteSize;
  319. paletteSize += PROTECTED_PAL_SIZE;
  320. if(paletteSize > 256 && !layer_renderer_check(false))
  321. {
  322. // Renderers without layer support often have poor upport for large
  323. // palettes, so to be safe we restrict the palette size
  324. paletteSize = 256;
  325. }
  326. return paletteSize;
  327. }
  328. void update_palette(void)
  329. {
  330. struct rgb_color new_palette[FULL_PAL_SIZE];
  331. update_colors(new_palette, make_palette(new_palette));
  332. }
  333. static void init_palette(void)
  334. {
  335. Uint32 i;
  336. memcpy(graphics.palette, default_pal,
  337. sizeof(struct rgb_color) * PAL_SIZE);
  338. memcpy(graphics.protected_palette, default_pal,
  339. sizeof(struct rgb_color) * PAL_SIZE);
  340. memcpy(graphics.intensity_palette, default_pal,
  341. sizeof(struct rgb_color) * PAL_SIZE);
  342. memset(graphics.current_intensity, 0,
  343. sizeof(Uint32) * PAL_SIZE);
  344. for(i = 0; i < SMZX_PAL_SIZE; i++)
  345. graphics.saved_intensity[i] = 100;
  346. graphics.fade_status = 1;
  347. graphics.palette_dirty = true;
  348. }
  349. static int intensity(int component, int percent)
  350. {
  351. component = (component * percent) / 100;
  352. if(component > 255)
  353. component = 255;
  354. return component;
  355. }
  356. void set_color_intensity(Uint32 color, Uint32 percent)
  357. {
  358. if(graphics.fade_status)
  359. {
  360. graphics.saved_intensity[color] = percent;
  361. }
  362. else
  363. {
  364. int r = intensity(graphics.palette[color].r, percent);
  365. int g = intensity(graphics.palette[color].g, percent);
  366. int b = intensity(graphics.palette[color].b, percent);
  367. graphics.intensity_palette[color].r = r;
  368. graphics.intensity_palette[color].g = g;
  369. graphics.intensity_palette[color].b = b;
  370. graphics.current_intensity[color] = percent;
  371. graphics.palette_dirty = true;
  372. }
  373. }
  374. void set_palette_intensity(Uint32 percent)
  375. {
  376. Uint32 i, num_colors;
  377. if(graphics.screen_mode >= 2)
  378. num_colors = SMZX_PAL_SIZE;
  379. else
  380. num_colors = PAL_SIZE;
  381. for(i = 0; i < num_colors; i++)
  382. {
  383. set_color_intensity(i, percent);
  384. }
  385. graphics.palette_dirty = true;
  386. }
  387. void set_rgb(Uint32 color, Uint32 r, Uint32 g, Uint32 b)
  388. {
  389. int percent = graphics.current_intensity[color];
  390. r = r * 255 / 63;
  391. g = g * 255 / 63;
  392. b = b * 255 / 63;
  393. graphics.palette[color].r = r;
  394. graphics.intensity_palette[color].r = intensity(r, percent);
  395. graphics.palette[color].g = g;
  396. graphics.intensity_palette[color].g = intensity(g, percent);
  397. graphics.palette[color].b = b;
  398. graphics.intensity_palette[color].b = intensity(b, percent);
  399. graphics.palette_dirty = true;
  400. }
  401. void set_protected_rgb(Uint32 color, Uint32 r, Uint32 g, Uint32 b)
  402. {
  403. r = r * 255 / 63;
  404. g = g * 255 / 63;
  405. b = b * 255 / 63;
  406. graphics.protected_palette[color].r = r;
  407. graphics.protected_palette[color].g = g;
  408. graphics.protected_palette[color].b = b;
  409. graphics.palette_dirty = true;
  410. }
  411. void set_red_component(Uint32 color, Uint32 r)
  412. {
  413. int percent = graphics.current_intensity[color];
  414. r = r * 255 / 63;
  415. graphics.palette[color].r = r;
  416. graphics.intensity_palette[color].r = intensity(r, percent);
  417. graphics.palette_dirty = true;
  418. }
  419. void set_green_component(Uint32 color, Uint32 g)
  420. {
  421. int percent = graphics.current_intensity[color];
  422. g = g * 255 / 63;
  423. graphics.palette[color].g = g;
  424. graphics.intensity_palette[color].g = intensity(g, percent);
  425. graphics.palette_dirty = true;
  426. }
  427. void set_blue_component(Uint32 color, Uint32 b)
  428. {
  429. int percent = graphics.current_intensity[color];
  430. b = b * 255 / 63;
  431. graphics.palette[color].b = b;
  432. graphics.intensity_palette[color].b = intensity(b, percent);
  433. graphics.palette_dirty = true;
  434. }
  435. static Uint32 get_smzx_index_offset(Uint32 color, Uint32 index)
  436. {
  437. index %= 4;
  438. if(index == 1)
  439. index = 2;
  440. else if(index == 2)
  441. index = 1;
  442. return (color % SMZX_PAL_SIZE) * 4 + index;
  443. }
  444. Uint32 get_smzx_index(Uint32 col, Uint32 offset)
  445. {
  446. offset = get_smzx_index_offset(col, offset);
  447. return graphics.smzx_indices[offset];
  448. }
  449. void set_smzx_index(Uint32 col, Uint32 offset, Uint32 value)
  450. {
  451. // Setting the SMZX index is only supported for mode 3
  452. if(graphics.screen_mode != 3)
  453. return;
  454. offset = get_smzx_index_offset(col, offset);
  455. graphics.smzx_indices[offset] = value % SMZX_PAL_SIZE;
  456. graphics.palette_dirty = true;
  457. }
  458. Uint32 get_color_intensity(Uint32 color)
  459. {
  460. return graphics.current_intensity[color];
  461. }
  462. void get_rgb(Uint32 color, Uint8 *r, Uint8 *g, Uint8 *b)
  463. {
  464. *r = ((graphics.palette[color].r * 126) + 255) / 510;
  465. *g = ((graphics.palette[color].g * 126) + 255) / 510;
  466. *b = ((graphics.palette[color].b * 126) + 255) / 510;
  467. }
  468. Uint32 get_red_component(Uint32 color)
  469. {
  470. return ((graphics.palette[color].r * 126) + 255) / 510;
  471. }
  472. Uint32 get_green_component(Uint32 color)
  473. {
  474. return ((graphics.palette[color].g * 126) + 255) / 510;
  475. }
  476. Uint32 get_blue_component(Uint32 color)
  477. {
  478. return ((graphics.palette[color].b * 126) + 255) / 510;
  479. }
  480. Uint32 get_color_luma(Uint32 color)
  481. {
  482. int r = graphics.palette[color].r;
  483. int g = graphics.palette[color].g;
  484. int b = graphics.palette[color].b;
  485. return (Uint32)((r * .299) + (g * .587) + (b * .114) + .5);
  486. }
  487. void load_palette(const char *fname)
  488. {
  489. int file_size, i, r, g, b;
  490. FILE *pal_file;
  491. pal_file = fopen_unsafe(fname, "rb");
  492. if(!pal_file)
  493. return;
  494. file_size = ftell_and_rewind(pal_file);
  495. switch(graphics.screen_mode)
  496. {
  497. // Regular text mode, 16 colors
  498. case 0:
  499. file_size = MIN(file_size, 16 * 3);
  500. break;
  501. // SMZX modes, up to 256 colors
  502. default:
  503. file_size = MIN(file_size, 256 * 3);
  504. break;
  505. }
  506. for(i = 0; i < file_size / 3; i++)
  507. {
  508. r = fgetc(pal_file);
  509. g = fgetc(pal_file);
  510. b = fgetc(pal_file);
  511. set_rgb(i, r, g, b);
  512. }
  513. fclose(pal_file);
  514. }
  515. void load_palette_mem(char *pal, size_t len)
  516. {
  517. int size, r, g, b, i, j;
  518. switch(graphics.screen_mode)
  519. {
  520. // Regular text mode
  521. case 0:
  522. size = MIN(len, 16*3);
  523. break;
  524. // SMZX
  525. default:
  526. size = MIN(len, 256*3);
  527. break;
  528. }
  529. for(i = 0, j = 0; j+2 < size; i++)
  530. {
  531. r = pal[j++];
  532. g = pal[j++];
  533. b = pal[j++];
  534. set_rgb(i, r, g, b);
  535. }
  536. }
  537. void load_index_file(const char *fname)
  538. {
  539. FILE *idx_file;
  540. int i;
  541. if(get_screen_mode() != 3)
  542. return;
  543. idx_file = fopen_unsafe(fname, "rb");
  544. if(idx_file)
  545. {
  546. for(i = 0; i < SMZX_PAL_SIZE; i++)
  547. {
  548. set_smzx_index(i, 0, fgetc(idx_file));
  549. set_smzx_index(i, 1, fgetc(idx_file));
  550. set_smzx_index(i, 2, fgetc(idx_file));
  551. set_smzx_index(i, 3, fgetc(idx_file));
  552. }
  553. fclose(idx_file);
  554. }
  555. }
  556. void save_indices(void *buffer)
  557. {
  558. Uint8 *copy_buffer = buffer;
  559. int i;
  560. if(get_screen_mode() != 3)
  561. return;
  562. for(i = 0; i < SMZX_PAL_SIZE; i++)
  563. {
  564. *(copy_buffer++) = get_smzx_index(i, 0);
  565. *(copy_buffer++) = get_smzx_index(i, 1);
  566. *(copy_buffer++) = get_smzx_index(i, 2);
  567. *(copy_buffer++) = get_smzx_index(i, 3);
  568. }
  569. }
  570. void load_indices(void *buffer, size_t size)
  571. {
  572. Uint8 *copy_buffer = buffer;
  573. unsigned int i;
  574. if(get_screen_mode() != 3)
  575. return;
  576. if(size > SMZX_PAL_SIZE)
  577. size = SMZX_PAL_SIZE;
  578. // Truncate incomplete colors
  579. size -= (size & 3);
  580. for(i = 0; i < size; i++)
  581. {
  582. set_smzx_index(i, 0, *(copy_buffer++));
  583. set_smzx_index(i, 1, *(copy_buffer++));
  584. set_smzx_index(i, 2, *(copy_buffer++));
  585. set_smzx_index(i, 3, *(copy_buffer++));
  586. }
  587. }
  588. void load_indices_direct(void *buffer, size_t size)
  589. {
  590. memcpy(graphics.smzx_indices, buffer, size);
  591. graphics.palette_dirty = true;
  592. }
  593. void smzx_palette_loaded(int val)
  594. {
  595. graphics.default_smzx_loaded = val;
  596. }
  597. static void update_intensity_palette(void)
  598. {
  599. int i;
  600. for(i = 0; i < SMZX_PAL_SIZE; i++)
  601. {
  602. set_color_intensity(i, graphics.current_intensity[i]);
  603. }
  604. }
  605. static void swap_palettes(void)
  606. {
  607. struct rgb_color temp_colors[SMZX_PAL_SIZE];
  608. Uint32 temp_intensities[SMZX_PAL_SIZE];
  609. memcpy(temp_colors, graphics.backup_palette,
  610. sizeof(struct rgb_color) * SMZX_PAL_SIZE);
  611. memcpy(graphics.backup_palette, graphics.palette,
  612. sizeof(struct rgb_color) * SMZX_PAL_SIZE);
  613. memcpy(graphics.palette, temp_colors,
  614. sizeof(struct rgb_color) * SMZX_PAL_SIZE);
  615. memcpy(temp_intensities, graphics.backup_intensity,
  616. sizeof(Uint32) * SMZX_PAL_SIZE);
  617. if(graphics.fade_status)
  618. {
  619. memcpy(graphics.backup_intensity,
  620. graphics.saved_intensity, sizeof(Uint32) * SMZX_PAL_SIZE);
  621. memcpy(graphics.saved_intensity, temp_intensities,
  622. sizeof(Uint32) * SMZX_PAL_SIZE);
  623. }
  624. else
  625. {
  626. memcpy(graphics.backup_intensity,
  627. graphics.current_intensity, sizeof(Uint32) * SMZX_PAL_SIZE);
  628. memcpy(graphics.current_intensity, temp_intensities,
  629. sizeof(Uint32) * SMZX_PAL_SIZE);
  630. update_intensity_palette();
  631. }
  632. }
  633. Uint32 get_fade_status(void)
  634. {
  635. return graphics.fade_status;
  636. }
  637. void dialog_fadein(void)
  638. {
  639. graphics.dialog_fade_status = get_fade_status();
  640. if(graphics.dialog_fade_status)
  641. {
  642. clear_screen();
  643. insta_fadein();
  644. }
  645. }
  646. void dialog_fadeout(void)
  647. {
  648. if(graphics.dialog_fade_status)
  649. {
  650. insta_fadeout();
  651. }
  652. }
  653. static void fix_layer_screen_mode(void)
  654. {
  655. // Fix the screen mode for all active layers except the UI_LAYER.
  656. Uint32 i;
  657. for(i = 0; i < graphics.layer_count; i++)
  658. graphics.video_layers[i].mode = graphics.screen_mode;
  659. graphics.video_layers[UI_LAYER].mode = 0;
  660. }
  661. void set_screen_mode(Uint32 mode)
  662. {
  663. int i;
  664. char *pal_idx;
  665. char bg, fg;
  666. mode %= 4;
  667. if((mode >= 2) && (graphics.screen_mode < 2))
  668. {
  669. swap_palettes();
  670. graphics.screen_mode = mode;
  671. if(!graphics.default_smzx_loaded)
  672. {
  673. if(graphics.fade_status)
  674. {
  675. for(i = 0; i < SMZX_PAL_SIZE; i++)
  676. {
  677. graphics.current_intensity[i] = 0;
  678. }
  679. }
  680. set_palette_intensity(100);
  681. load_palette(mzx_res_get_by_id(SMZX_PAL));
  682. graphics.default_smzx_loaded = 1;
  683. }
  684. }
  685. else
  686. if((graphics.screen_mode >= 2) && (mode < 2))
  687. {
  688. swap_palettes();
  689. graphics.screen_mode = mode;
  690. }
  691. else
  692. {
  693. graphics.screen_mode = mode;
  694. }
  695. pal_idx = graphics.smzx_indices;
  696. if(mode == 1 || mode == 2)
  697. {
  698. for(i = 0; i < 256; i++)
  699. {
  700. bg = (i & 0xF0) >> 4;
  701. fg = i & 0x0F;
  702. pal_idx[0] = (bg << 4) | bg;
  703. pal_idx[1] = (bg << 4) | fg;
  704. pal_idx[2] = (fg << 4) | bg;
  705. pal_idx[3] = (fg << 4) | fg;
  706. pal_idx += 4;
  707. }
  708. }
  709. else
  710. if(mode == 3)
  711. {
  712. for(i = 0; i < 256; i++)
  713. {
  714. pal_idx[0] = (i + 0) & 0xFF;
  715. pal_idx[1] = (i + 2) & 0xFF;
  716. pal_idx[2] = (i + 1) & 0xFF;
  717. pal_idx[3] = (i + 3) & 0xFF;
  718. pal_idx += 4;
  719. }
  720. }
  721. fix_layer_screen_mode();
  722. graphics.palette_dirty = true;
  723. }
  724. Uint32 get_screen_mode(void)
  725. {
  726. return graphics.screen_mode;
  727. }
  728. /**
  729. * The cursor needs to be reasonably visible whenever possible. A good deal of
  730. * the time this is possible with the classic cursor behavior of using the
  731. * foreground color of whatever is beneath it. For completely solid chars, the
  732. * background color is instead used.
  733. *
  734. * This behavior doesn't work very well when the foreground and background
  735. * colors are very close (modes 0 and 1), so instead we switch the cursor to
  736. * protected white or black. Foreground and background have no meaning in modes
  737. * 2 and 3, so always use protected white or black in these modes.
  738. */
  739. static Uint16 get_cursor_color(void)
  740. {
  741. struct char_element *cursor_element =
  742. graphics.text_video + graphics.cursor_x + (graphics.cursor_y * SCREEN_W);
  743. Uint16 cursor_color;
  744. Uint32 cursor_char = cursor_element->char_value;
  745. Uint32 fg_color = cursor_element->fg_color;
  746. Uint32 bg_color = cursor_element->bg_color;
  747. int i;
  748. if(bg_color >= 0x10)
  749. {
  750. // UI- use protected palette white or black.
  751. if(bg_color <= 0x19)
  752. cursor_color = graphics.protected_pal_position + 0x0F;
  753. else
  754. cursor_color = graphics.protected_pal_position;
  755. }
  756. else
  757. if(graphics.screen_mode <= 1)
  758. {
  759. // Modes 0 and 1- use the (modified) classic cursor color logic.
  760. Uint32 *offset = (Uint32 *)(graphics.charset + cursor_char * CHAR_SIZE);
  761. Uint32 cursor_solid = 0xFFFFFFFF;
  762. // Choose FG by default.
  763. cursor_color = fg_color;
  764. // If the char under the cursor is completely solid, use the BG instead.
  765. for(i = 0; i < 3; i++)
  766. {
  767. cursor_solid &= *offset;
  768. offset++;
  769. }
  770. cursor_solid &= (*((Uint16 *)offset)) | 0xFFFF0000;
  771. if(cursor_solid == 0xFFFFFFFF)
  772. cursor_color = bg_color;
  773. if(fg_color < 0x10 && bg_color < 0x10)
  774. {
  775. // If the fg and bg colors are game colors and close in brightness/the
  776. // same, neither color fits well, so choose a protected palette color.
  777. int fg_luma = get_color_luma(fg_color);
  778. int bg_luma = get_color_luma(bg_color);
  779. if(abs(fg_luma - bg_luma) < 32)
  780. {
  781. cursor_color = graphics.protected_pal_position;
  782. if(fg_luma + bg_luma < 256)
  783. cursor_color |= 0x0F;
  784. }
  785. }
  786. // Offset adjust protected colors if necessary.
  787. if(cursor_color >= 0x10)
  788. cursor_color = graphics.protected_pal_position + (cursor_color & 0x0F);
  789. // Offset adjust mode 1 colors if necessary.
  790. else if(graphics.screen_mode == 1)
  791. cursor_color = (cursor_color << 4) | (cursor_color & 0x0F);
  792. }
  793. else
  794. {
  795. // Modes 2 and 3- read the entire char and pick protected white or black.
  796. Uint8 char_buffer[CHAR_W * CHAR_H];
  797. Uint32 sum = 0;
  798. Uint8 pal;
  799. bg_color &= 0x0F;
  800. fg_color &= 0x0F;
  801. pal = (bg_color << 4) | fg_color;
  802. dump_char(cursor_char, pal, graphics.screen_mode, char_buffer);
  803. for(i = 0; i < CHAR_W * CHAR_H; i += 2)
  804. sum += get_color_luma(char_buffer[i]);
  805. cursor_color = graphics.protected_pal_position;
  806. if(sum < 128 * (CHAR_W * CHAR_H / 2))
  807. cursor_color |= 0x0F;
  808. }
  809. return cursor_color;
  810. }
  811. static int compare_layers(const void *a, const void *b)
  812. {
  813. return (*(struct video_layer * const *)a)->draw_order -
  814. (*(struct video_layer * const *)b)->draw_order;
  815. }
  816. void update_screen(void)
  817. {
  818. Uint32 ticks = get_ticks();
  819. Uint32 layer;
  820. if((ticks - graphics.cursor_timestamp) > CURSOR_BLINK_RATE)
  821. {
  822. graphics.cursor_flipflop ^= 1;
  823. graphics.cursor_timestamp = ticks;
  824. }
  825. if(graphics.palette_dirty)
  826. {
  827. update_palette();
  828. graphics.palette_dirty = false;
  829. }
  830. if(graphics.requires_extended && graphics.renderer.render_layer)
  831. {
  832. for(layer = 0; layer < graphics.layer_count; layer++)
  833. {
  834. graphics.sorted_video_layers[layer] = &graphics.video_layers[layer];
  835. }
  836. qsort(graphics.sorted_video_layers, graphics.layer_count,
  837. sizeof(struct video_layer *), compare_layers);
  838. for(layer = 0; layer < graphics.layer_count; layer++)
  839. {
  840. if(graphics.sorted_video_layers[layer]->data &&
  841. !graphics.sorted_video_layers[layer]->empty)
  842. graphics.renderer.render_layer(&graphics,
  843. graphics.sorted_video_layers[layer]);
  844. }
  845. }
  846. else
  847. if(graphics.renderer.render_graph)
  848. {
  849. // Fallback if the layer renderer is unavailable or unnecessary
  850. graphics.renderer.render_graph(&graphics);
  851. }
  852. else
  853. if(graphics.renderer.render_layer)
  854. {
  855. // Fallback using the layer renderer
  856. graphics.text_video_layer.mode = graphics.screen_mode;
  857. graphics.text_video_layer.data = graphics.text_video;
  858. graphics.renderer.render_layer(&graphics, &(graphics.text_video_layer));
  859. }
  860. if(graphics.cursor_flipflop &&
  861. (graphics.cursor_mode != cursor_mode_invisible))
  862. {
  863. Uint16 cursor_color = get_cursor_color();
  864. Uint32 lines = 0;
  865. Uint32 offset = 0;
  866. switch(graphics.cursor_mode)
  867. {
  868. case cursor_mode_underline:
  869. lines = 2;
  870. offset = 12;
  871. break;
  872. case cursor_mode_solid:
  873. lines = 14;
  874. offset = 0;
  875. break;
  876. case cursor_mode_invisible:
  877. break;
  878. }
  879. graphics.renderer.render_cursor(&graphics,
  880. graphics.cursor_x, graphics.cursor_y, cursor_color, lines, offset);
  881. }
  882. if(graphics.mouse_status)
  883. {
  884. int mouse_x, mouse_y;
  885. get_real_mouse_position(&mouse_x, &mouse_y);
  886. mouse_x = (mouse_x / graphics.mouse_width_mul) * graphics.mouse_width_mul;
  887. mouse_y = (mouse_y / graphics.mouse_height_mul) * graphics.mouse_height_mul;
  888. graphics.renderer.render_mouse(&graphics, mouse_x, mouse_y,
  889. graphics.mouse_width_mul, graphics.mouse_height_mul);
  890. }
  891. graphics.renderer.sync_screen(&graphics);
  892. }
  893. // Very quick fade out. Saves intensity table for fade in. Be sure
  894. // to use in conjuction with the next function.
  895. void vquick_fadeout(void)
  896. {
  897. if(!has_video_initialized())
  898. {
  899. // If we're running without video there's no point waiting 11 frames.
  900. insta_fadeout();
  901. return;
  902. }
  903. if(!graphics.fade_status)
  904. {
  905. Sint32 i, i2, num_colors;
  906. Uint32 ticks;
  907. if(graphics.screen_mode >= 2)
  908. num_colors = SMZX_PAL_SIZE;
  909. else
  910. num_colors = PAL_SIZE;
  911. memcpy(graphics.saved_intensity, graphics.current_intensity,
  912. sizeof(Uint32) * num_colors);
  913. for(i = 10; i >= 0; i--)
  914. {
  915. ticks = get_ticks();
  916. for(i2 = 0; i2 < num_colors; i2++)
  917. set_color_intensity(i2, (graphics.saved_intensity[i2] * i / 10));
  918. graphics.palette_dirty = true;
  919. update_screen();
  920. ticks = get_ticks() - ticks;
  921. if(ticks <= 16)
  922. delay(16 - ticks);
  923. }
  924. graphics.fade_status = 1;
  925. }
  926. }
  927. // Very quick fade in. Uses intensity table saved from fade out. For
  928. // use in conjuction with the previous function.
  929. void vquick_fadein(void)
  930. {
  931. if(!has_video_initialized())
  932. {
  933. // If we're running without video there's no point waiting 11 frames.
  934. insta_fadein();
  935. return;
  936. }
  937. if(graphics.fade_status)
  938. {
  939. Uint32 i, i2, num_colors;
  940. Uint32 ticks;
  941. graphics.fade_status = 0;
  942. if(graphics.screen_mode >= 2)
  943. num_colors = SMZX_PAL_SIZE;
  944. else
  945. num_colors = PAL_SIZE;
  946. for(i = 0; i <= 10; i++)
  947. {
  948. ticks = get_ticks();
  949. for(i2 = 0; i2 < num_colors; i2++)
  950. set_color_intensity(i2, (graphics.saved_intensity[i2] * i / 10));
  951. graphics.palette_dirty = true;
  952. update_screen();
  953. ticks = get_ticks() - ticks;
  954. if(ticks <= 16)
  955. delay(16 - ticks);
  956. }
  957. }
  958. }
  959. // Instant fade out
  960. void insta_fadeout(void)
  961. {
  962. Uint32 i, num_colors;
  963. if(graphics.fade_status)
  964. return;
  965. if(graphics.screen_mode >= 2)
  966. num_colors = SMZX_PAL_SIZE;
  967. else
  968. num_colors = PAL_SIZE;
  969. for(i = 0; i < num_colors; i++)
  970. set_color_intensity(i, 0);
  971. delay(1);
  972. graphics.palette_dirty = true;
  973. update_screen(); // NOTE: this was called conditionally in 2.81e
  974. graphics.fade_status = true;
  975. }
  976. // Instant fade in
  977. void insta_fadein(void)
  978. {
  979. Uint32 i, num_colors;
  980. if(!graphics.fade_status)
  981. return;
  982. graphics.fade_status = false;
  983. if(graphics.screen_mode >= 2)
  984. num_colors = SMZX_PAL_SIZE;
  985. else
  986. num_colors = PAL_SIZE;
  987. for(i = 0; i < num_colors; i++)
  988. set_color_intensity(i, graphics.saved_intensity[i]);
  989. graphics.palette_dirty = true;
  990. update_screen(); // NOTE: this was called conditionally in 2.81e
  991. }
  992. void default_palette(void)
  993. {
  994. memcpy(graphics.palette, default_pal,
  995. sizeof(struct rgb_color) * PAL_SIZE);
  996. memcpy(graphics.intensity_palette, default_pal,
  997. sizeof(struct rgb_color) * PAL_SIZE);
  998. graphics.palette_dirty = true;
  999. }
  1000. void default_protected_palette(void)
  1001. {
  1002. memcpy(graphics.protected_palette, default_pal,
  1003. sizeof(struct rgb_color) * PAL_SIZE);
  1004. graphics.palette_dirty = true;
  1005. }
  1006. static boolean set_graphics_output(struct config_info *conf)
  1007. {
  1008. const char *video_output = conf->video_output;
  1009. const struct renderer_data *renderer = renderers;
  1010. int i = 0;
  1011. // The first renderer was NULL, this shouldn't happen
  1012. if(!renderer->name)
  1013. {
  1014. warn("No renderers built, please provide a valid config.h!\n");
  1015. return false;
  1016. }
  1017. while(renderer->name)
  1018. {
  1019. if(!strcasecmp(video_output, renderer->name))
  1020. break;
  1021. renderer++;
  1022. i++;
  1023. }
  1024. // If no match found, use first renderer in the renderer list
  1025. if(!renderer->name)
  1026. {
  1027. renderer = renderers;
  1028. i = 0;
  1029. }
  1030. renderer->reg(&graphics.renderer);
  1031. graphics.renderer_num = i;
  1032. debug("Video: using '%s' renderer.\n", renderer->name);
  1033. return true;
  1034. }
  1035. #if defined(CONFIG_PNG) && defined(CONFIG_SDL) && \
  1036. defined(CONFIG_ICON) && !defined(__WIN32__)
  1037. static boolean icon_w_h_constraint(png_uint_32 w, png_uint_32 h)
  1038. {
  1039. // Icons must be multiples of 16 and square
  1040. return (w == h) && ((w % 16) == 0) && ((h % 16) == 0);
  1041. }
  1042. static void *sdl_alloc_rgba_surface(png_uint_32 w, png_uint_32 h,
  1043. png_uint_32 *stride, void **pixels)
  1044. {
  1045. Uint32 rmask, gmask, bmask, amask;
  1046. SDL_Surface *s;
  1047. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1048. rmask = 0xff000000;
  1049. gmask = 0x00ff0000;
  1050. bmask = 0x0000ff00;
  1051. amask = 0x000000ff;
  1052. #else
  1053. rmask = 0x000000ff;
  1054. gmask = 0x0000ff00;
  1055. bmask = 0x00ff0000;
  1056. amask = 0xff000000;
  1057. #endif
  1058. s = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, rmask, gmask, bmask, amask);
  1059. if(!s)
  1060. return NULL;
  1061. *stride = s->pitch;
  1062. *pixels = s->pixels;
  1063. return s;
  1064. }
  1065. static SDL_Surface *png_read_icon(const char *name)
  1066. {
  1067. return png_read_file(name, NULL, NULL, icon_w_h_constraint,
  1068. sdl_alloc_rgba_surface);
  1069. }
  1070. #endif // CONFIG_PNG && CONFIG_SDL && CONFIG_ICON && !__WIN32__
  1071. void set_window_caption(const char *caption)
  1072. {
  1073. #ifdef CONFIG_SDL
  1074. SDL_Window *window = SDL_GetWindowFromID(sdl_window_id);
  1075. SDL_SetWindowTitle(window, caption);
  1076. #endif
  1077. }
  1078. char *get_default_caption(void)
  1079. {
  1080. return graphics.default_caption;
  1081. }
  1082. static void set_window_icon(void)
  1083. {
  1084. #if defined(CONFIG_SDL) && defined(CONFIG_ICON)
  1085. #ifdef __WIN32__
  1086. {
  1087. /* Roll our own icon code; the SDL stuff is a mess and for some
  1088. * reason limits us to 256 colour icons (with keying but no alpha).
  1089. *
  1090. * We also pull off some nifty WIN32 hackery to load the executable's
  1091. * icon resource; saves having an external dependency.
  1092. */
  1093. HICON icon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(1));
  1094. if(icon)
  1095. {
  1096. SDL_Window *window = SDL_GetWindowFromID(sdl_window_id);
  1097. SDL_SysWMinfo info;
  1098. SDL_VERSION(&info.version);
  1099. SDL_GetWindowWMInfo(window, &info);
  1100. SendMessage(SDL_SysWMinfo_GetWND(&info),
  1101. WM_SETICON, ICON_BIG, (LPARAM)icon);
  1102. }
  1103. }
  1104. #else // !__WIN32__
  1105. #if defined(CONFIG_PNG)
  1106. {
  1107. SDL_Surface *icon = png_read_icon("/usr/share/icons/megazeux.png");
  1108. if(icon)
  1109. {
  1110. SDL_Window *window = SDL_GetWindowFromID(sdl_window_id);
  1111. SDL_SetWindowIcon(window, icon);
  1112. SDL_FreeSurface(icon);
  1113. }
  1114. }
  1115. #endif // CONFIG_PNG
  1116. #endif // __WIN32__
  1117. #endif // CONFIG_SDL && CONFIG_ICON
  1118. }
  1119. static void new_empty_layer(struct video_layer *layer, int x, int y, Uint32 w,
  1120. Uint32 h, int draw_order)
  1121. {
  1122. // Layers are persistent and static
  1123. if(!layer->data || layer->w != w || layer->h != h)
  1124. layer->data = crealloc(layer->data, sizeof(struct char_element) * w * h);
  1125. layer->w = w;
  1126. layer->h = h;
  1127. layer->x = x;
  1128. layer->y = y;
  1129. layer->mode = graphics.screen_mode;
  1130. layer->draw_order = draw_order;
  1131. layer->transparent_col = -1;
  1132. layer->offset = 0;
  1133. }
  1134. Uint32 create_layer(int x, int y, Uint32 w, Uint32 h, int draw_order, int t_col,
  1135. int offset, boolean unbound)
  1136. {
  1137. Uint32 layer_idx = graphics.layer_count;
  1138. struct video_layer *layer = &graphics.video_layers[layer_idx];
  1139. new_empty_layer(layer, x, y, w, h, draw_order);
  1140. memset(layer->data, 0xFF, sizeof(struct char_element) * w * h);
  1141. layer->empty = true;
  1142. layer->transparent_col = t_col;
  1143. layer->offset = offset;
  1144. graphics.layer_count++;
  1145. if(!graphics.requires_extended && unbound)
  1146. graphics.requires_extended = true;
  1147. return layer_idx;
  1148. }
  1149. void set_layer_offset(Uint32 layer, int offset)
  1150. {
  1151. graphics.video_layers[layer].offset = offset;
  1152. }
  1153. void set_layer_mode(Uint32 layer, int mode)
  1154. {
  1155. // In general, we want the layer to use the screen mode, but some
  1156. // UI elements need to be able to change this.
  1157. graphics.video_layers[layer].mode = mode;
  1158. }
  1159. void move_layer(Uint32 layer, int x, int y)
  1160. {
  1161. graphics.video_layers[layer].x = x;
  1162. graphics.video_layers[layer].y = y;
  1163. if(!graphics.requires_extended && (x % CHAR_W != 0 || y % CHAR_H != 0))
  1164. graphics.requires_extended = true;
  1165. }
  1166. static void init_layers(void)
  1167. {
  1168. new_empty_layer(&graphics.video_layers[BOARD_LAYER],
  1169. 0, 0, SCREEN_W, SCREEN_H, LAYER_DRAWORDER_BOARD);
  1170. new_empty_layer(&graphics.video_layers[OVERLAY_LAYER],
  1171. 0, 0, SCREEN_W, SCREEN_H, LAYER_DRAWORDER_OVERLAY);
  1172. new_empty_layer(&graphics.video_layers[GAME_UI_LAYER],
  1173. 0, 0, SCREEN_W, SCREEN_H, LAYER_DRAWORDER_GAME_UI);
  1174. new_empty_layer(&graphics.video_layers[UI_LAYER],
  1175. 0, 0, SCREEN_W, SCREEN_H, LAYER_DRAWORDER_UI);
  1176. select_layer(UI_LAYER);
  1177. graphics.layer_count = NUM_DEFAULT_LAYERS;
  1178. graphics.layer_count_prev = graphics.layer_count;
  1179. blank_layers();
  1180. }
  1181. void select_layer(Uint32 layer)
  1182. {
  1183. graphics.current_layer = layer;
  1184. graphics.current_video = graphics.video_layers[layer].data;
  1185. }
  1186. void blank_layers(void)
  1187. {
  1188. // This clears the default layers and deletes all other layers
  1189. memset(graphics.video_layers[BOARD_LAYER].data, 0x00,
  1190. sizeof(struct char_element) * SCREEN_W * SCREEN_H);
  1191. memset(graphics.video_layers[OVERLAY_LAYER].data, 0xFF,
  1192. sizeof(struct char_element) * SCREEN_W * SCREEN_H);
  1193. memset(graphics.video_layers[GAME_UI_LAYER].data, 0xFF,
  1194. sizeof(struct char_element) * SCREEN_W * SCREEN_H);
  1195. memset(graphics.video_layers[UI_LAYER].data, 0xFF,
  1196. sizeof(struct char_element) * SCREEN_W * SCREEN_H);
  1197. graphics.video_layers[BOARD_LAYER].empty = false;
  1198. graphics.video_layers[OVERLAY_LAYER].empty = true;
  1199. graphics.video_layers[GAME_UI_LAYER].empty = true;
  1200. graphics.video_layers[UI_LAYER].empty = true;
  1201. // Delete the rest of the layers
  1202. destruct_extra_layers(0);
  1203. // Since the layers were cleared, their screen mode values need to be reset.
  1204. fix_layer_screen_mode();
  1205. }
  1206. void destruct_extra_layers(Uint32 first)
  1207. {
  1208. // Delete layers that have not persisted since the previous frame and
  1209. // make all extra layers available for use.
  1210. Uint32 i;
  1211. if(first < NUM_DEFAULT_LAYERS)
  1212. first = NUM_DEFAULT_LAYERS;
  1213. if(graphics.layer_count_prev > graphics.layer_count)
  1214. {
  1215. for(i = graphics.layer_count; i < graphics.layer_count_prev; i++)
  1216. {
  1217. free(graphics.video_layers[i].data);
  1218. graphics.video_layers[i].data = NULL;
  1219. }
  1220. }
  1221. graphics.layer_count_prev = graphics.layer_count;
  1222. if(graphics.layer_count > first)
  1223. graphics.layer_count = first;
  1224. // Extended graphics may be no longer needed after destroying layers
  1225. if(graphics.layer_count == NUM_DEFAULT_LAYERS)
  1226. graphics.requires_extended = false;
  1227. }
  1228. void destruct_layers(void)
  1229. {
  1230. Uint32 i;
  1231. for(i = 0; i < TEXTVIDEO_LAYERS; i++)
  1232. {
  1233. if(graphics.video_layers[i].data)
  1234. {
  1235. free(graphics.video_layers[i].data);
  1236. graphics.video_layers[i].data = NULL;
  1237. }
  1238. }
  1239. graphics.layer_count_prev = 0;
  1240. graphics.layer_count = 0;
  1241. }
  1242. static void dirty_ui(void)
  1243. {
  1244. if(graphics.requires_extended) return;
  1245. if(graphics.current_layer != UI_LAYER) return;
  1246. if(graphics.screen_mode == 0) return;
  1247. graphics.requires_extended = true;
  1248. }
  1249. static void dirty_current(void)
  1250. {
  1251. graphics.video_layers[graphics.current_layer].empty = false;
  1252. }
  1253. static int offset_adjust(int offset)
  1254. {
  1255. // Transform the given offset from screen space to layer space
  1256. struct video_layer *layer;
  1257. int x, y;
  1258. layer = &graphics.video_layers[graphics.current_layer];
  1259. x = (offset % SCREEN_W) - (layer->x / CHAR_W);
  1260. y = (offset / SCREEN_W) - (layer->y / CHAR_H);
  1261. return y * layer->w + x;
  1262. }
  1263. boolean init_video(struct config_info *conf, const char *caption)
  1264. {
  1265. graphics.screen_mode = 0;
  1266. graphics.fullscreen = conf->fullscreen;
  1267. graphics.fullscreen_windowed = conf->fullscreen_windowed;
  1268. graphics.resolution_width = conf->resolution_width;
  1269. graphics.resolution_height = conf->resolution_height;
  1270. graphics.window_width = conf->window_width;
  1271. graphics.window_height = conf->window_height;
  1272. graphics.mouse_status = false;
  1273. graphics.cursor_timestamp = get_ticks();
  1274. graphics.cursor_flipflop = 1;
  1275. graphics.system_mouse = conf->system_mouse;
  1276. memset(&(graphics.text_video_layer), 0, sizeof(struct video_layer));
  1277. graphics.text_video_layer.w = SCREEN_W;
  1278. graphics.text_video_layer.h = SCREEN_H;
  1279. graphics.text_video_layer.transparent_col = -1;
  1280. init_layers();
  1281. if(!set_graphics_output(conf))
  1282. return false;
  1283. if(conf->resolution_width == -1 && conf->resolution_height == -1)
  1284. {
  1285. #ifdef CONFIG_SDL
  1286. // TODO maybe be able to communicate with the renderer instead of this hack
  1287. boolean is_scaling = true;
  1288. int width;
  1289. int height;
  1290. if(!strcmp(conf->video_output, "software"))
  1291. is_scaling = false;
  1292. if(sdl_get_fullscreen_resolution(&width, &height, is_scaling))
  1293. {
  1294. graphics.resolution_width = width;
  1295. graphics.resolution_height = height;
  1296. }
  1297. else
  1298. #endif
  1299. {
  1300. // "Safe" resolution
  1301. graphics.resolution_width = 640;
  1302. graphics.resolution_height = 480;
  1303. }
  1304. }
  1305. snprintf(graphics.default_caption, 32, "%s", caption);
  1306. if(!graphics.renderer.init_video(&graphics, conf))
  1307. {
  1308. // Try falling back to the first registered renderer
  1309. strcpy(conf->video_output, "");
  1310. if(!set_graphics_output(conf))
  1311. return false;
  1312. // Fallback failed; bail out
  1313. if(!graphics.renderer.init_video(&graphics, conf))
  1314. return false;
  1315. }
  1316. #ifdef CONFIG_SDL
  1317. if(!graphics.system_mouse)
  1318. SDL_ShowCursor(SDL_DISABLE);
  1319. #endif
  1320. ec_load_set_secondary(mzx_res_get_by_id(MZX_DEFAULT_CHR),
  1321. graphics.default_charset);
  1322. ec_load_set_secondary(mzx_res_get_by_id(MZX_EDIT_CHR),
  1323. graphics.charset + (PROTECTED_CHARSET_POSITION * CHAR_SIZE));
  1324. #if NUM_CHARSETS > 2
  1325. if(!layer_renderer_check(false))
  1326. {
  1327. // As the renderer doesn't support advanced features, it
  1328. // may also not support the number of charsets we want
  1329. // Copy the protected charset to the end of the normal charset
  1330. // position
  1331. // TODO: This check ought to happen whenever the renderer changes, too
  1332. memcpy(graphics.charset + (CHARSET_SIZE * CHAR_SIZE),
  1333. graphics.charset + (PROTECTED_CHARSET_POSITION * CHAR_SIZE),
  1334. CHARSET_SIZE * CHAR_SIZE);
  1335. }
  1336. #endif
  1337. ec_clear_set();
  1338. ec_load_mzx();
  1339. init_palette();
  1340. graphics_was_initialized = true;
  1341. return true;
  1342. }
  1343. void quit_video(void)
  1344. {
  1345. if(graphics.renderer.free_video)
  1346. graphics.renderer.free_video(&graphics);
  1347. destruct_layers();
  1348. }
  1349. boolean has_video_initialized(void)
  1350. {
  1351. #ifdef CONFIG_SDL
  1352. // Dummy SDL driver should act as headless.
  1353. const char *sdl_driver = SDL_GetCurrentVideoDriver();
  1354. if(sdl_driver && !strcmp(sdl_driver, "dummy")) return false;
  1355. #endif /* CONFIG_SDL */
  1356. return graphics_was_initialized;
  1357. }
  1358. boolean set_video_mode(void)
  1359. {
  1360. int target_width, target_height;
  1361. int target_depth = graphics.bits_per_pixel;
  1362. boolean fullscreen = graphics.fullscreen;
  1363. boolean resize = graphics.allow_resize;
  1364. boolean ret;
  1365. #ifdef CONFIG_SDL
  1366. if(fullscreen && graphics.fullscreen_windowed)
  1367. {
  1368. // TODO maybe be able to communicate with the renderer instead of this hack
  1369. if(sdl_get_fullscreen_resolution(&target_width, &target_height, true))
  1370. {
  1371. graphics.resolution_width = target_width;
  1372. graphics.resolution_height = target_height;
  1373. }
  1374. }
  1375. #endif
  1376. if(fullscreen)
  1377. {
  1378. target_width = graphics.resolution_width;
  1379. target_height = graphics.resolution_height;
  1380. }
  1381. else
  1382. {
  1383. target_width = graphics.window_width;
  1384. target_height = graphics.window_height;
  1385. }
  1386. // If video mode fails, replace it with 'safe' defaults
  1387. if(!(graphics.renderer.check_video_mode(&graphics,
  1388. target_width, target_height, target_depth, fullscreen, resize)))
  1389. {
  1390. target_width = 640;
  1391. target_height = 350;
  1392. target_depth = 8;
  1393. fullscreen = false;
  1394. resize = false;
  1395. graphics.resolution_width = target_width;
  1396. graphics.resolution_height = target_height;
  1397. graphics.bits_per_pixel = target_depth;
  1398. graphics.fullscreen = fullscreen;
  1399. }
  1400. ret = graphics.renderer.set_video_mode(&graphics,
  1401. target_width, target_height, target_depth, fullscreen, resize);
  1402. if(ret)
  1403. {
  1404. set_window_caption(graphics.default_caption);
  1405. set_window_icon();
  1406. }
  1407. return ret;
  1408. }
  1409. boolean change_video_output(struct config_info *conf, const char *output)
  1410. {
  1411. char old_video_output[16];
  1412. boolean fallback = false;
  1413. boolean retval = true;
  1414. strncpy(old_video_output, conf->video_output, 16);
  1415. old_video_output[15] = 0;
  1416. strncpy(conf->video_output, output, 16);
  1417. conf->video_output[15] = 0;
  1418. if(graphics.renderer.free_video)
  1419. graphics.renderer.free_video(&graphics);
  1420. set_graphics_output(conf);
  1421. if(!graphics.renderer.init_video(&graphics, conf))
  1422. {
  1423. retval = false;
  1424. strcpy(conf->video_output, old_video_output);
  1425. if(!set_graphics_output(conf))
  1426. {
  1427. warn("Failed to roll back renderer!\n");
  1428. fallback = true;
  1429. }
  1430. else
  1431. if(!graphics.renderer.init_video(&graphics, conf))
  1432. {
  1433. warn("Failed to roll back video mode!\n");
  1434. fallback = true;
  1435. }
  1436. }
  1437. if(fallback)
  1438. {
  1439. // Attempt the first renderer in the list (unless that just failed).
  1440. if(!strcmp(conf->video_output, renderers->name))
  1441. {
  1442. warn("Aborting!\n");
  1443. exit(1);
  1444. }
  1445. strcpy(conf->video_output, renderers->name);
  1446. if(!set_graphics_output(conf))
  1447. {
  1448. warn("Failed to load fallback renderer, aborting!\n");
  1449. exit(1);
  1450. }
  1451. if(!graphics.renderer.init_video(&graphics, conf))
  1452. {
  1453. warn("Failed to set fallback video mode, aborting!\n");
  1454. exit(1);
  1455. }
  1456. }
  1457. update_palette();
  1458. return retval;
  1459. }
  1460. int get_available_video_output_list(const char **buffer, int buffer_len)
  1461. {
  1462. const struct renderer_data *renderer = renderers;
  1463. int i;
  1464. for(i = 0; i < buffer_len; i++, renderer++)
  1465. {
  1466. if(!renderer->name)
  1467. break;
  1468. buffer[i] = renderer->name;
  1469. }
  1470. return i;
  1471. }
  1472. int get_current_video_output(void)
  1473. {
  1474. return (int)(graphics.renderer_num);
  1475. }
  1476. boolean is_fullscreen(void)
  1477. {
  1478. return graphics.fullscreen;
  1479. }
  1480. void toggle_fullscreen(void)
  1481. {
  1482. graphics.fullscreen = !graphics.fullscreen;
  1483. graphics.palette_dirty = true;
  1484. set_video_mode();
  1485. update_screen();
  1486. }
  1487. void resize_screen(Uint32 w, Uint32 h)
  1488. {
  1489. if(!graphics.fullscreen && graphics.allow_resize)
  1490. {
  1491. graphics.window_width = w;
  1492. graphics.window_height = h;
  1493. set_video_mode();
  1494. graphics.renderer.resize_screen(&graphics, w, h);
  1495. }
  1496. }
  1497. void color_string_ext_special(const char *str, Uint32 x, Uint32 y,
  1498. Uint8 *color, Uint32 offset, Uint32 c_offset, boolean respect_newline)
  1499. {
  1500. int scr_off = (y * SCREEN_W) + x;
  1501. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1502. struct char_element *dest_copy = graphics.text_video + scr_off;
  1503. const char *src = str;
  1504. Uint8 cur_char = *src;
  1505. Uint8 next;
  1506. Uint8 bg_color = (*color >> 4) + c_offset;
  1507. Uint8 fg_color = (*color & 0x0F) + c_offset;
  1508. char next_str[2];
  1509. next_str[1] = 0;
  1510. dirty_ui();
  1511. dirty_current();
  1512. while(cur_char)
  1513. {
  1514. switch(cur_char)
  1515. {
  1516. // Color character
  1517. case '@':
  1518. {
  1519. str++;
  1520. next = *str;
  1521. // If 0, stop right there
  1522. if(!next)
  1523. goto exit_out;
  1524. // If the next isn't hex, count as one
  1525. if(isxdigit(next))
  1526. {
  1527. next_str[0] = next;
  1528. bg_color = (Uint8)(strtol(next_str, NULL, 16) + c_offset);
  1529. }
  1530. else
  1531. {
  1532. if(next == '@')
  1533. {
  1534. dest->char_value = '@' + offset;
  1535. dest->bg_color = bg_color;
  1536. dest->fg_color = fg_color;
  1537. *(dest_copy++) = *dest;
  1538. dest++;
  1539. }
  1540. else
  1541. {
  1542. str--;
  1543. }
  1544. }
  1545. break;
  1546. }
  1547. case '~':
  1548. {
  1549. str++;
  1550. next = *str;
  1551. // If 0, stop right there
  1552. if(!next)
  1553. goto exit_out;
  1554. // If the next isn't hex, count as one
  1555. if(isxdigit(next))
  1556. {
  1557. next_str[0] = next;
  1558. fg_color = (Uint8)(strtol(next_str, NULL, 16) + c_offset);
  1559. }
  1560. else
  1561. {
  1562. if(next == '~')
  1563. {
  1564. dest->char_value = '~' + offset;
  1565. dest->bg_color = bg_color;
  1566. dest->fg_color = fg_color;
  1567. *(dest_copy++) = *dest;
  1568. dest++;
  1569. }
  1570. else
  1571. {
  1572. str--;
  1573. }
  1574. }
  1575. break;
  1576. }
  1577. // Newline
  1578. case '\n':
  1579. {
  1580. if(respect_newline)
  1581. {
  1582. y++;
  1583. dest = graphics.current_video + offset_adjust((y * SCREEN_W) + x);
  1584. dest_copy = graphics.text_video + (y * SCREEN_W) + x;
  1585. break;
  1586. }
  1587. }
  1588. /* fallthrough */
  1589. default:
  1590. {
  1591. dest->char_value = cur_char + offset;
  1592. dest->bg_color = bg_color;
  1593. dest->fg_color = fg_color;
  1594. *(dest_copy++) = *dest;
  1595. dest++;
  1596. }
  1597. }
  1598. if(dest >= graphics.current_video + offset_adjust(SCREEN_W * SCREEN_H))
  1599. break;
  1600. str++;
  1601. cur_char = *str;
  1602. }
  1603. exit_out:
  1604. *color = (((bg_color - c_offset) << 4) & 0xF0) |
  1605. (((fg_color - c_offset) << 0) & 0x0F);
  1606. }
  1607. void color_string_ext(const char *str, Uint32 x, Uint32 y, Uint8 color,
  1608. Uint32 offset, Uint32 c_offset, boolean respect_newline)
  1609. {
  1610. color_string_ext_special(str, x, y, &color, offset,
  1611. c_offset, respect_newline);
  1612. }
  1613. // Write a normal string
  1614. void write_string_ext(const char *str, Uint32 x, Uint32 y,
  1615. Uint8 color, Uint32 tab_allowed, Uint32 offset,
  1616. Uint32 c_offset)
  1617. {
  1618. int scr_off = (y * SCREEN_W) + x;
  1619. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1620. struct char_element *dest_copy = graphics.text_video + scr_off;
  1621. const char *src = str;
  1622. Uint8 cur_char = *src;
  1623. Uint8 bg_color = (color >> 4) + c_offset;
  1624. Uint8 fg_color = (color & 0x0F) + c_offset;
  1625. dirty_ui();
  1626. dirty_current();
  1627. while(cur_char && (cur_char != 0))
  1628. {
  1629. switch(cur_char)
  1630. {
  1631. // Newline
  1632. case '\n':
  1633. {
  1634. y++;
  1635. dest = graphics.current_video + offset_adjust((y * SCREEN_W) + x);
  1636. dest_copy = graphics.text_video + (y * SCREEN_W) + x;
  1637. break;
  1638. }
  1639. case '\t':
  1640. {
  1641. if(tab_allowed)
  1642. {
  1643. dest += 5;
  1644. dest_copy += 5;
  1645. break;
  1646. }
  1647. }
  1648. /* fallthrough */
  1649. default:
  1650. {
  1651. dest->char_value = cur_char + offset;
  1652. dest->bg_color = bg_color;
  1653. dest->fg_color = fg_color;
  1654. *(dest_copy++) = *dest;
  1655. dest++;
  1656. }
  1657. }
  1658. if(dest >= graphics.current_video + offset_adjust(SCREEN_W * SCREEN_H))
  1659. break;
  1660. str++;
  1661. cur_char = *str;
  1662. }
  1663. }
  1664. void write_string_mask(const char *str, Uint32 x, Uint32 y,
  1665. Uint8 color, Uint32 tab_allowed)
  1666. {
  1667. int scr_off = (y * SCREEN_W) + x;
  1668. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1669. struct char_element *dest_copy = graphics.text_video + scr_off;
  1670. const char *src = str;
  1671. Uint8 cur_char = *src;
  1672. Uint8 bg_color = (color >> 4) + 16;
  1673. Uint8 fg_color = (color & 0x0F) + 16;
  1674. dirty_ui();
  1675. dirty_current();
  1676. while(cur_char && (cur_char != 0))
  1677. {
  1678. switch(cur_char)
  1679. {
  1680. // Newline
  1681. case '\n':
  1682. {
  1683. y++;
  1684. dest = graphics.current_video + offset_adjust((y * SCREEN_W) + x);
  1685. dest_copy = graphics.text_video + (y * SCREEN_W) + x;
  1686. break;
  1687. }
  1688. case '\t':
  1689. {
  1690. if(tab_allowed)
  1691. {
  1692. dest += 5;
  1693. dest_copy += 5;
  1694. break;
  1695. }
  1696. }
  1697. /* fallthrough */
  1698. default:
  1699. {
  1700. if((cur_char >= 32) && (cur_char <= 126))
  1701. dest->char_value = cur_char + PRO_CH;
  1702. else
  1703. dest->char_value = cur_char;
  1704. dest->bg_color = bg_color;
  1705. dest->fg_color = fg_color;
  1706. *(dest_copy++) = *dest;
  1707. dest++;
  1708. }
  1709. }
  1710. if(dest >= graphics.current_video + offset_adjust(SCREEN_W * SCREEN_H))
  1711. break;
  1712. str++;
  1713. cur_char = *str;
  1714. }
  1715. }
  1716. // Write a normal string, without carriage returns
  1717. void write_line_ext(const char *str, Uint32 x, Uint32 y,
  1718. Uint8 color, Uint32 tab_allowed, Uint32 offset,
  1719. Uint32 c_offset)
  1720. {
  1721. int scr_off = (y * SCREEN_W) + x;
  1722. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1723. struct char_element *dest_copy = graphics.text_video + scr_off;
  1724. const char *src = str;
  1725. Uint8 cur_char = *src;
  1726. Uint8 bg_color = (color >> 4) + c_offset;
  1727. Uint8 fg_color = (color & 0x0F) + c_offset;
  1728. dirty_ui();
  1729. dirty_current();
  1730. while(cur_char && (cur_char != '\n'))
  1731. {
  1732. switch(cur_char)
  1733. {
  1734. // Color character
  1735. case '\t':
  1736. {
  1737. if(tab_allowed)
  1738. {
  1739. dest += 10;
  1740. dest_copy += 10;
  1741. break;
  1742. }
  1743. }
  1744. /* fallthrough */
  1745. default:
  1746. {
  1747. dest->char_value = cur_char + offset;
  1748. dest->bg_color = bg_color;
  1749. dest->fg_color = fg_color;
  1750. *(dest_copy++) = *dest;
  1751. dest++;
  1752. }
  1753. }
  1754. str++;
  1755. cur_char = *str;
  1756. }
  1757. }
  1758. void write_line_mask(const char *str, Uint32 x, Uint32 y,
  1759. Uint8 color, Uint32 tab_allowed)
  1760. {
  1761. int scr_off = (y * SCREEN_W) + x;
  1762. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1763. struct char_element *dest_copy = graphics.text_video + scr_off;
  1764. const char *src = str;
  1765. Uint8 cur_char = *src;
  1766. Uint8 bg_color = (color >> 4) + 16;
  1767. Uint8 fg_color = (color & 0x0F) + 16;
  1768. dirty_ui();
  1769. dirty_current();
  1770. while(cur_char && (cur_char != '\n'))
  1771. {
  1772. switch(cur_char)
  1773. {
  1774. // Color character
  1775. case '\t':
  1776. {
  1777. if(tab_allowed)
  1778. {
  1779. dest += 10;
  1780. dest_copy += 10;
  1781. break;
  1782. }
  1783. }
  1784. /* fallthrough */
  1785. default:
  1786. {
  1787. if((cur_char >= 32) && (cur_char <= 127))
  1788. dest->char_value = cur_char + PRO_CH;
  1789. else
  1790. dest->char_value = cur_char;
  1791. dest->bg_color = bg_color;
  1792. dest->fg_color = fg_color;
  1793. *(dest_copy++) = *dest;
  1794. dest++;
  1795. }
  1796. }
  1797. str++;
  1798. cur_char = *str;
  1799. }
  1800. }
  1801. // Set rightalign to print the rightmost char at xy and proceed to the left
  1802. // minlen is the minimum length to print. Pad with 0.
  1803. void write_number(int number, char color, int x, int y,
  1804. int minlen, boolean rightalign, int base)
  1805. {
  1806. char temp[12];
  1807. minlen = CLAMP(minlen, 0, 11);
  1808. if(base == 10)
  1809. snprintf(temp, 12, "%0*d", minlen, number);
  1810. else
  1811. snprintf(temp, 12, "%0*x", minlen, number);
  1812. temp[11] = 0;
  1813. if(rightalign)
  1814. {
  1815. x -= strlen(temp) - 1;
  1816. if(x < 0)
  1817. x = 0;
  1818. }
  1819. write_string(temp, x, y, color, false);
  1820. }
  1821. static void color_line_ext(Uint32 length, Uint32 x, Uint32 y,
  1822. Uint8 color, Uint32 offset, Uint32 c_offset)
  1823. {
  1824. int scr_off = (y * SCREEN_W) + x;
  1825. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1826. struct char_element *dest_copy = graphics.text_video + scr_off;
  1827. Uint8 bg_color = (color >> 4) + c_offset;
  1828. Uint8 fg_color = (color & 0x0F) + c_offset;
  1829. Uint32 i;
  1830. dirty_ui();
  1831. dirty_current();
  1832. for(i = 0; i < length; i++)
  1833. {
  1834. dest->char_value = dest_copy->char_value;
  1835. dest->bg_color = bg_color;
  1836. dest->fg_color = fg_color;
  1837. *(dest_copy++) = *dest;
  1838. dest++;
  1839. }
  1840. }
  1841. void fill_line_ext(Uint32 length, Uint32 x, Uint32 y,
  1842. Uint8 chr, Uint8 color, Uint32 offset, Uint32 c_offset)
  1843. {
  1844. int scr_off = (y * SCREEN_W) + x;
  1845. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1846. struct char_element *dest_copy = graphics.text_video + scr_off;
  1847. Uint8 bg_color = (color >> 4) + c_offset;
  1848. Uint8 fg_color = (color & 0x0F) + c_offset;
  1849. Uint32 i;
  1850. dirty_ui();
  1851. dirty_current();
  1852. for(i = 0; i < length; i++)
  1853. {
  1854. dest->char_value = chr + offset;
  1855. dest->bg_color = bg_color;
  1856. dest->fg_color = fg_color;
  1857. *(dest_copy++) = *dest;
  1858. dest++;
  1859. }
  1860. }
  1861. void draw_char_mixed_pal_ext(Uint8 chr, Uint8 bg_color,
  1862. Uint8 fg_color, Uint32 x, Uint32 y, Uint32 offset)
  1863. {
  1864. int scr_off = (y * SCREEN_W) + x;
  1865. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1866. struct char_element *dest_copy = graphics.text_video + scr_off;
  1867. dest->char_value = chr + offset;
  1868. dest->bg_color = bg_color & 31;
  1869. dest->fg_color = fg_color & 31;
  1870. *(dest_copy++) = *dest;
  1871. dirty_ui();
  1872. dirty_current();
  1873. }
  1874. void draw_char_ext(Uint8 chr, Uint8 color, Uint32 x,
  1875. Uint32 y, Uint32 offset, Uint32 c_offset)
  1876. {
  1877. int scr_off = (y * SCREEN_W) + x;
  1878. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1879. struct char_element *dest_copy = graphics.text_video + scr_off;
  1880. dest->char_value = chr + offset;
  1881. dest->bg_color = (color >> 4) + c_offset;
  1882. dest->fg_color = (color & 0x0F) + c_offset;
  1883. *(dest_copy++) = *dest;
  1884. dirty_ui();
  1885. dirty_current();
  1886. }
  1887. void draw_char_linear_ext(Uint8 color, Uint8 chr,
  1888. Uint32 offset, Uint32 offset_b, Uint32 c_offset)
  1889. {
  1890. struct char_element *dest = graphics.current_video + offset_adjust(offset);
  1891. struct char_element *dest_copy = graphics.text_video + offset;
  1892. dest->char_value = chr + offset_b;
  1893. dest->bg_color = (color >> 4) + c_offset;
  1894. dest->fg_color = (color & 0x0F) + c_offset;
  1895. *(dest_copy++) = *dest;
  1896. dirty_ui();
  1897. dirty_current();
  1898. }
  1899. void draw_char_to_layer(Uint8 color, Uint8 chr,
  1900. Uint32 x, Uint32 y, Uint32 offset_b, Uint32 c_offset)
  1901. {
  1902. int w = graphics.video_layers[graphics.current_layer].w;
  1903. struct char_element *dest = graphics.current_video + (y * w) + x;
  1904. dest->char_value = chr + offset_b;
  1905. dest->bg_color = (color >> 4) + c_offset;
  1906. dest->fg_color = (color & 0x0F) + c_offset;
  1907. dirty_current();
  1908. }
  1909. void color_string(const char *string, Uint32 x, Uint32 y, Uint8 color)
  1910. {
  1911. color_string_ext(string, x, y, color, PRO_CH, 16, false);
  1912. }
  1913. void write_string(const char *string, Uint32 x, Uint32 y, Uint8 color,
  1914. Uint32 tab_allowed)
  1915. {
  1916. write_string_ext(string, x, y, color, tab_allowed, PRO_CH, 16);
  1917. }
  1918. void color_line(Uint32 length, Uint32 x, Uint32 y, Uint8 color)
  1919. {
  1920. color_line_ext(length, x, y, color, PRO_CH, 16);
  1921. }
  1922. void fill_line(Uint32 length, Uint32 x, Uint32 y, Uint8 chr,
  1923. Uint8 color)
  1924. {
  1925. fill_line_ext(length, x, y, chr, color, PRO_CH, 16);
  1926. }
  1927. void draw_char(Uint8 chr, Uint8 color, Uint32 x, Uint32 y)
  1928. {
  1929. draw_char_ext(chr, color, x, y, PRO_CH, 16);
  1930. }
  1931. void erase_char(Uint32 x, Uint32 y)
  1932. {
  1933. int scr_off = (y * SCREEN_W) + x;
  1934. struct char_element *dest = graphics.current_video + offset_adjust(scr_off);
  1935. dest->char_value = INVISIBLE_CHAR;
  1936. }
  1937. void erase_area(Uint32 x, Uint32 y, Uint32 x2, Uint32 y2)
  1938. {
  1939. Uint32 i, j;
  1940. for(i = y; i <= y2; i++)
  1941. for(j = x; j <= x2; j++)
  1942. erase_char(j, i);
  1943. }
  1944. Uint8 get_color_linear(Uint32 offset)
  1945. {
  1946. struct char_element *dest = graphics.text_video + offset;
  1947. return (dest->bg_color << 4) | (dest->fg_color & 0x0F);
  1948. }
  1949. void clear_screen(void)
  1950. {
  1951. // Hide the game screen by drawing blank chars over the UI.
  1952. Uint32 i;
  1953. struct char_element *dest = graphics.current_video;
  1954. struct char_element *dest_copy = graphics.text_video;
  1955. Uint32 current_layer = graphics.current_layer;
  1956. select_layer(UI_LAYER);
  1957. dirty_current();
  1958. for(i = 0; i < (SCREEN_W * SCREEN_H); i++)
  1959. {
  1960. dest->char_value = 0;
  1961. dest->fg_color = 16; // Protected black
  1962. dest->bg_color = 16; // Protected black
  1963. *(dest_copy++) = *dest;
  1964. dest++;
  1965. }
  1966. select_layer(current_layer);
  1967. update_screen();
  1968. }
  1969. void set_screen(struct char_element *src)
  1970. {
  1971. int offset = SCREEN_W * SCREEN_H;
  1972. int size = offset * sizeof(struct char_element);
  1973. memcpy(graphics.video_layers[UI_LAYER].data, src, size);
  1974. src += offset;
  1975. memcpy(graphics.video_layers[GAME_UI_LAYER].data, src, size);
  1976. src += offset;
  1977. memcpy(graphics.text_video, src, size);
  1978. }
  1979. void get_screen(struct char_element *dest)
  1980. {
  1981. int offset = SCREEN_W * SCREEN_H;
  1982. int size = offset * sizeof(struct char_element);
  1983. memcpy(dest, graphics.video_layers[UI_LAYER].data, size);
  1984. dest += offset;
  1985. memcpy(dest, graphics.video_layers[GAME_UI_LAYER].data, size);
  1986. dest += offset;
  1987. memcpy(dest, graphics.text_video, size);
  1988. }
  1989. void cursor_underline(void)
  1990. {
  1991. graphics.cursor_mode = cursor_mode_underline;
  1992. }
  1993. void cursor_solid(void)
  1994. {
  1995. graphics.cursor_mode = cursor_mode_solid;
  1996. }
  1997. void cursor_off(void)
  1998. {
  1999. graphics.cursor_mode = cursor_mode_invisible;
  2000. }
  2001. void move_cursor(Uint32 x, Uint32 y)
  2002. {
  2003. graphics.cursor_x = x;
  2004. graphics.cursor_y = y;
  2005. }
  2006. #ifdef CONFIG_HELPSYS
  2007. void set_cursor_mode(enum cursor_mode_types mode)
  2008. {
  2009. graphics.cursor_mode = mode;
  2010. }
  2011. enum cursor_mode_types get_cursor_mode(void)
  2012. {
  2013. return graphics.cursor_mode;
  2014. }
  2015. #endif // CONFIG_HELPSYS
  2016. void m_hide(void)
  2017. {
  2018. if(!graphics.system_mouse)
  2019. graphics.mouse_status = false;
  2020. }
  2021. void m_show(void)
  2022. {
  2023. if(!graphics.system_mouse)
  2024. graphics.mouse_status = true;
  2025. }
  2026. #ifdef CONFIG_ENABLE_SCREENSHOTS
  2027. #ifdef CONFIG_PNG
  2028. #define DUMP_FMT_EXT "png"
  2029. /* Trivial PNG dumper; this routine is a modification (simplification) of
  2030. * code pinched from http://www2.autistici.org/encelo/prog_sdldemos.php.
  2031. *
  2032. * Palette support was added, the original support was broken.
  2033. *
  2034. * Copyright (C) 2006 Angelo "Encelo" Theodorou
  2035. * Copyright (C) 2007 Alistair John Strachan <alistair@devzero.co.uk>
  2036. */
  2037. /*
  2038. static void dump_screen_real(Uint8 *pix, struct rgb_color *pal, int count,
  2039. const char *name)
  2040. {
  2041. png_write_screen(pix, pal, count, name);
  2042. }
  2043. */
  2044. static void dump_screen_real_32bpp(Uint32 *pix, const char *name)
  2045. {
  2046. png_write_screen_32bpp(pix, name);
  2047. }
  2048. #else
  2049. #define DUMP_FMT_EXT "bmp"
  2050. /*
  2051. static void dump_screen_real(Uint8 *pix, struct rgb_color *pal, int count,
  2052. const char *name)
  2053. {
  2054. FILE *file;
  2055. int i;
  2056. file = fopen_unsafe(name, "wb");
  2057. if(!file)
  2058. return;
  2059. // BMP header
  2060. fputw(0x4D42, file); // BM
  2061. fputd(14 + 40 + count * 4 + 640 * 350, file); // BMP + DIB + palette + image
  2062. fputd(0, file); // Reserved
  2063. fputd(14, file); // DIB header offset
  2064. // DIB header
  2065. fputd(40, file); // DIB header size (Windows 3/BITMAPINFOHEADER)
  2066. fputd(640, file); // Width in pixels
  2067. fputd(350, file); // Height in pixels
  2068. fputw(1, file); // Number of color planes
  2069. fputw(8, file); // Bits per pixel
  2070. fputd(0, file); // Compression method (none)
  2071. fputd(640 * 350, file); // Image data size
  2072. fputd(3780, file); // Horizontal dots per meter
  2073. fputd(3780, file); // Vertical dots per meter
  2074. fputd(count, file); // Number of colors in palette
  2075. fputd(0, file); // Number of important colors
  2076. // Color palette
  2077. for(i = 0; i < count; i++)
  2078. {
  2079. fputc(pal[i].b, file);
  2080. fputc(pal[i].g, file);
  2081. fputc(pal[i].r, file);
  2082. fputc(0, file);
  2083. }
  2084. // Image data
  2085. for(i = 349; i >= 0; i--)
  2086. {
  2087. fwrite(pix + i * 640, sizeof(Uint8), 640, file);
  2088. }
  2089. fclose(file);
  2090. }
  2091. */
  2092. static void dump_screen_real_32bpp(Uint32 *pix, const char *name)
  2093. {
  2094. FILE *file;
  2095. int i, x;
  2096. Uint8 rowbuffer[SCREEN_PIX_W * 3]; // 24bpp
  2097. Uint8 *rowbuffer_ptr;
  2098. Uint32 *pix_ptr;
  2099. file = fopen_unsafe(name, "wb");
  2100. if(!file)
  2101. return;
  2102. // BMP header
  2103. fputw(0x4D42, file); // BM
  2104. // BMP + DIB + image
  2105. fputd(14 + 40 + SCREEN_PIX_W * SCREEN_PIX_H * 3, file);
  2106. fputd(0, file); // Reserved
  2107. fputd(14, file); // DIB header offset
  2108. // DIB header
  2109. fputd(40, file); // DIB header size (Windows 3/BITMAPINFOHEADER)
  2110. fputd(SCREEN_PIX_W, file); // Width in pixels
  2111. fputd(SCREEN_PIX_H, file); // Height in pixels
  2112. fputw(1, file); // Number of color planes
  2113. fputw(24, file); // Bits per pixel
  2114. fputd(0, file); // Compression method (none)
  2115. fputd(SCREEN_PIX_W * SCREEN_PIX_H * 3, file); // Image data size
  2116. fputd(3780, file); // Horizontal dots per meter
  2117. fputd(3780, file); // Vertical dots per meter
  2118. fputd(0, file); // Number of colors in palette
  2119. fputd(0, file); // Number of important colors
  2120. // Image data
  2121. for(i = SCREEN_PIX_H - 1; i >= 0; i--)
  2122. {
  2123. pix_ptr = pix + i * SCREEN_PIX_W;
  2124. rowbuffer_ptr = rowbuffer;
  2125. for(x = 0; x < SCREEN_PIX_W; x++)
  2126. {
  2127. rowbuffer_ptr[0] = (*pix_ptr & 0x000000FF) >> 0;
  2128. rowbuffer_ptr[1] = (*pix_ptr & 0x0000FF00) >> 8;
  2129. rowbuffer_ptr[2] = (*pix_ptr & 0x00FF0000) >> 16;
  2130. rowbuffer_ptr += 3;
  2131. pix_ptr++;
  2132. }
  2133. fwrite(rowbuffer, SCREEN_PIX_W * 3, 1, file);
  2134. //fwrite(pix + i * 640, sizeof(Uint8), 640, file);
  2135. }
  2136. fclose(file);
  2137. }
  2138. #endif // CONFIG_PNG
  2139. #define MAX_NAME_SIZE 20
  2140. void dump_screen(void)
  2141. {
  2142. struct rgb_color palette[FULL_PAL_SIZE];
  2143. Uint32 backup_palette[FULL_PAL_SIZE];
  2144. size_t palette_size_bytes = sizeof(Uint32) * FULL_PAL_SIZE;
  2145. int palette_size;
  2146. char name[MAX_NAME_SIZE];
  2147. struct stat file_info;
  2148. Uint32 *ss;
  2149. int i;
  2150. Uint32 layer;
  2151. for(i = 0; i < 99999; i++)
  2152. {
  2153. snprintf(name, MAX_NAME_SIZE - 1, "screen%d.%s", i, DUMP_FMT_EXT);
  2154. name[MAX_NAME_SIZE - 1] = '\0';
  2155. if(stat(name, &file_info))
  2156. break;
  2157. }
  2158. ss = cmalloc(sizeof(Uint32) * 640 * 350);
  2159. // Unfortunately, render_layer wants flat_intensity_palette set, so we need
  2160. // to back this up, fill it, render the layer to memory, then copy the old
  2161. // palette back.
  2162. memcpy(backup_palette, graphics.flat_intensity_palette, palette_size_bytes);
  2163. palette_size = make_palette(palette);
  2164. for(i = 0; i < palette_size; i++)
  2165. {
  2166. #if PLATFORM_BYTE_ORDER == PLATFORM_BIG_ENDIAN
  2167. graphics.flat_intensity_palette[i] =
  2168. (palette[i].r << 8) | (palette[i].g << 16) | (palette[i].b << 24);
  2169. #else
  2170. graphics.flat_intensity_palette[i] =
  2171. (palette[i].r << 16) | (palette[i].g << 8) | (palette[i].b << 0);
  2172. #endif /* PLATFORM_BYTE_ORDER == PLATFORM_BIG_ENDIAN */
  2173. }
  2174. for(layer = 0; layer < graphics.layer_count; layer++)
  2175. graphics.sorted_video_layers[layer] = &graphics.video_layers[layer];
  2176. qsort(graphics.sorted_video_layers, graphics.layer_count,
  2177. sizeof(struct video_layer *), compare_layers);
  2178. for(layer = 0; layer < graphics.layer_count; layer++)
  2179. {
  2180. render_layer(ss, 32, 640 * sizeof(Uint32), &graphics,
  2181. graphics.sorted_video_layers[layer]);
  2182. }
  2183. memcpy(graphics.flat_intensity_palette, backup_palette, palette_size_bytes);
  2184. //dump_screen_real(ss, palette, make_palette(palette), name);
  2185. dump_screen_real_32bpp(ss, name);
  2186. free(ss);
  2187. }
  2188. #endif /* CONFIG_ENABLE_SCREENSHOTS */
  2189. void dump_char(Uint16 char_idx, Uint8 color, int mode, Uint8 *buffer)
  2190. {
  2191. // Dumps the specified char into a buffer. Expects CHAR_W * CHAR_H bytes.
  2192. int x, y;
  2193. Uint8 cols[4];
  2194. char_idx = char_idx % PROTECTED_CHARSET_POSITION;
  2195. color = color % SMZX_PAL_SIZE;
  2196. if(mode == -1)
  2197. mode = graphics.screen_mode;
  2198. if(mode == 0)
  2199. {
  2200. cols[0] = (color & 0xF0) >> 4;
  2201. cols[1] = color & 0x0F;
  2202. for(y = 0; y < CHAR_H; y++)
  2203. {
  2204. char row = graphics.charset[char_idx * CHAR_SIZE + y];
  2205. for(x = 0; x < CHAR_W; x++)
  2206. {
  2207. buffer[y * CHAR_W + x] = cols[(row >> (7-x)) & 0x01];
  2208. }
  2209. }
  2210. }
  2211. else
  2212. {
  2213. cols[0] = graphics.smzx_indices[color * 4 + 0];
  2214. cols[1] = graphics.smzx_indices[color * 4 + 1];
  2215. cols[2] = graphics.smzx_indices[color * 4 + 2];
  2216. cols[3] = graphics.smzx_indices[color * 4 + 3];
  2217. for(y = 0; y < CHAR_H; y++)
  2218. {
  2219. char row = graphics.charset[char_idx * CHAR_SIZE + y];
  2220. for(x = 0; x < CHAR_W; x += 2)
  2221. {
  2222. buffer[y * CHAR_W + x] = cols[(row >> (7-x)) & 0x03];
  2223. buffer[y * CHAR_W + x + 1] = cols[(row >> (7-x)) & 0x03];
  2224. }
  2225. }
  2226. }
  2227. }
  2228. void get_screen_coords(int screen_x, int screen_y, int *x, int *y,
  2229. int *min_x, int *min_y, int *max_x, int *max_y)
  2230. {
  2231. graphics.renderer.get_screen_coords(&graphics, screen_x, screen_y, x, y,
  2232. min_x, min_y, max_x, max_y);
  2233. }
  2234. void set_screen_coords(int x, int y, int *screen_x, int *screen_y)
  2235. {
  2236. graphics.renderer.set_screen_coords(&graphics, x, y, screen_x, screen_y);
  2237. }
  2238. void set_mouse_mul(int width_mul, int height_mul)
  2239. {
  2240. graphics.mouse_width_mul = width_mul;
  2241. graphics.mouse_height_mul = height_mul;
  2242. }
  2243. void focus_screen(int x, int y)
  2244. {
  2245. int pixel_x = x * graphics.resolution_width / SCREEN_W +
  2246. graphics.resolution_width / SCREEN_W / 2;
  2247. int pixel_y = y * graphics.resolution_height / SCREEN_H +
  2248. graphics.resolution_height / SCREEN_H / 2;
  2249. focus_pixel(pixel_x, pixel_y);
  2250. }
  2251. void focus_pixel(int x, int y)
  2252. {
  2253. if(graphics.renderer.focus_pixel)
  2254. graphics.renderer.focus_pixel(&graphics, x, y);
  2255. }
  2256. boolean switch_shader(const char *name)
  2257. {
  2258. if(graphics.renderer.switch_shader)
  2259. graphics.renderer.switch_shader(&graphics, name);
  2260. if(graphics.gl_scaling_shader[0])
  2261. return true;
  2262. return false;
  2263. }
  2264. boolean layer_renderer_check(boolean show_error)
  2265. {
  2266. if(!graphics.renderer.render_layer)
  2267. {
  2268. if(show_error)
  2269. {
  2270. error_message(E_NO_LAYER_RENDERER, 0, NULL);
  2271. set_error_suppression(E_NO_LAYER_RENDERER, 1);
  2272. }
  2273. return false;
  2274. }
  2275. return true;
  2276. }