render_gp2x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2007 Alan Williams <mralert@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "platform.h"
  20. #include "graphics.h"
  21. #include "render.h"
  22. #include "render_sdl.h"
  23. #include "renderers.h"
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "SDL.h"
  27. struct gp2x_render_data
  28. {
  29. struct sdl_render_data sdl;
  30. Uint32 halfmask;
  31. Uint16 buffer[320*350];
  32. };
  33. static SDL_Surface *
  34. gp2x_get_screen_surface(struct gp2x_render_data *render_data)
  35. {
  36. return render_data->sdl.shadow ?
  37. render_data->sdl.shadow : render_data->sdl.screen;
  38. }
  39. static void gp2x_set_colors_mzx (struct graphics_data *graphics,
  40. Uint32 *char_colors, Uint8 bg, Uint8 fg)
  41. {
  42. struct gp2x_render_data *render_data = graphics->render_data;
  43. Uint32 cb_bg, cb_fg, cb_mx;
  44. cb_bg = graphics->flat_intensity_palette[bg];
  45. cb_fg = graphics->flat_intensity_palette[fg];
  46. if(cb_bg == cb_fg)
  47. cb_mx = cb_bg;
  48. else
  49. cb_mx = ((cb_bg >> 1) & render_data->halfmask) +
  50. ((cb_fg >> 1) & render_data->halfmask);
  51. #if PLATFORM_BYTE_ORDER == PLATFORM_BIG_ENDIAN
  52. char_colors[0] = (cb_bg << 16) | cb_bg;
  53. char_colors[1] = (cb_bg << 16) | cb_mx;
  54. char_colors[2] = (cb_bg << 16) | cb_mx;
  55. char_colors[3] = (cb_bg << 16) | cb_fg;
  56. char_colors[4] = (cb_mx << 16) | cb_bg;
  57. char_colors[5] = (cb_mx << 16) | cb_mx;
  58. char_colors[6] = (cb_mx << 16) | cb_mx;
  59. char_colors[7] = (cb_mx << 16) | cb_fg;
  60. char_colors[8] = (cb_mx << 16) | cb_bg;
  61. char_colors[9] = (cb_mx << 16) | cb_mx;
  62. char_colors[10] = (cb_mx << 16) | cb_mx;
  63. char_colors[11] = (cb_mx << 16) | cb_fg;
  64. char_colors[12] = (cb_fg << 16) | cb_bg;
  65. char_colors[13] = (cb_fg << 16) | cb_mx;
  66. char_colors[14] = (cb_fg << 16) | cb_mx;
  67. char_colors[15] = (cb_fg << 16) | cb_fg;
  68. #else
  69. char_colors[0] = (cb_bg << 16) | cb_bg;
  70. char_colors[1] = (cb_mx << 16) | cb_bg;
  71. char_colors[2] = (cb_mx << 16) | cb_bg;
  72. char_colors[3] = (cb_fg << 16) | cb_bg;
  73. char_colors[4] = (cb_bg << 16) | cb_mx;
  74. char_colors[5] = (cb_mx << 16) | cb_mx;
  75. char_colors[6] = (cb_mx << 16) | cb_mx;
  76. char_colors[7] = (cb_fg << 16) | cb_mx;
  77. char_colors[8] = (cb_bg << 16) | cb_mx;
  78. char_colors[9] = (cb_mx << 16) | cb_mx;
  79. char_colors[10] = (cb_mx << 16) | cb_mx;
  80. char_colors[11] = (cb_fg << 16) | cb_mx;
  81. char_colors[12] = (cb_bg << 16) | cb_fg;
  82. char_colors[13] = (cb_mx << 16) | cb_fg;
  83. char_colors[14] = (cb_mx << 16) | cb_fg;
  84. char_colors[15] = (cb_fg << 16) | cb_fg;
  85. #endif
  86. }
  87. static void gp2x_set_colors_smzx (struct graphics_data *graphics,
  88. Uint32 *char_colors, Uint8 bg, Uint8 fg)
  89. {
  90. Uint32 bb, bf, fb, ff;
  91. bg &= 0x0F;
  92. fg &= 0x0F;
  93. bb = graphics->flat_intensity_palette[(bg << 4) | bg];
  94. bf = graphics->flat_intensity_palette[(bg << 4) | fg];
  95. fb = graphics->flat_intensity_palette[(fg << 4) | bg];
  96. ff = graphics->flat_intensity_palette[(fg << 4) | fg];
  97. #if PLATFORM_BYTE_ORDER == PLATFORM_BIG_ENDIAN
  98. char_colors[0] = (bb << 16) | bb;
  99. char_colors[1] = (bb << 16) | bf;
  100. char_colors[2] = (bb << 16) | fb;
  101. char_colors[3] = (bb << 16) | ff;
  102. char_colors[4] = (bf << 16) | bb;
  103. char_colors[5] = (bf << 16) | bf;
  104. char_colors[6] = (bf << 16) | fb;
  105. char_colors[7] = (bf << 16) | ff;
  106. char_colors[8] = (fb << 16) | bb;
  107. char_colors[9] = (fb << 16) | bf;
  108. char_colors[10] = (fb << 16) | fb;
  109. char_colors[11] = (fb << 16) | ff;
  110. char_colors[12] = (ff << 16) | bb;
  111. char_colors[13] = (ff << 16) | bf;
  112. char_colors[14] = (ff << 16) | fb;
  113. char_colors[15] = (ff << 16) | ff;
  114. #else
  115. char_colors[0] = (bb << 16) | bb;
  116. char_colors[1] = (bf << 16) | bb;
  117. char_colors[2] = (fb << 16) | bb;
  118. char_colors[3] = (ff << 16) | bb;
  119. char_colors[4] = (bb << 16) | bf;
  120. char_colors[5] = (bf << 16) | bf;
  121. char_colors[6] = (fb << 16) | bf;
  122. char_colors[7] = (ff << 16) | bf;
  123. char_colors[8] = (bb << 16) | fb;
  124. char_colors[9] = (bf << 16) | fb;
  125. char_colors[10] = (fb << 16) | fb;
  126. char_colors[11] = (ff << 16) | fb;
  127. char_colors[12] = (bb << 16) | ff;
  128. char_colors[13] = (bf << 16) | ff;
  129. char_colors[14] = (fb << 16) | ff;
  130. char_colors[15] = (ff << 16) | ff;
  131. #endif
  132. }
  133. static void gp2x_set_colors_smzx3 (struct graphics_data *graphics,
  134. Uint32 *char_colors, Uint8 bg, Uint8 fg)
  135. {
  136. Uint8 base;
  137. Uint32 c0, c1, c2, c3;
  138. base = (bg << 4) | (fg & 0x0F);
  139. c0 = graphics->flat_intensity_palette[base];
  140. c1 = graphics->flat_intensity_palette[(base + 1) & 0xFF];
  141. c2 = graphics->flat_intensity_palette[(base + 2) & 0xFF];
  142. c3 = graphics->flat_intensity_palette[(base + 3) & 0xFF];
  143. #if PLATFORM_BYTE_ORDER == PLATFORM_BIG_ENDIAN
  144. char_colors[0] = (c0 << 16) | c0;
  145. char_colors[1] = (c0 << 16) | c2;
  146. char_colors[2] = (c0 << 16) | c1;
  147. char_colors[3] = (c0 << 16) | c3;
  148. char_colors[4] = (c2 << 16) | c0;
  149. char_colors[5] = (c2 << 16) | c2;
  150. char_colors[6] = (c2 << 16) | c1;
  151. char_colors[7] = (c2 << 16) | c3;
  152. char_colors[8] = (c1 << 16) | c0;
  153. char_colors[9] = (c1 << 16) | c2;
  154. char_colors[10] = (c1 << 16) | c1;
  155. char_colors[11] = (c1 << 16) | c3;
  156. char_colors[12] = (c3 << 16) | c0;
  157. char_colors[13] = (c3 << 16) | c2;
  158. char_colors[14] = (c3 << 16) | c1;
  159. char_colors[15] = (c3 << 16) | c3;
  160. #else
  161. char_colors[0] = (c0 << 16) | c0;
  162. char_colors[1] = (c2 << 16) | c0;
  163. char_colors[2] = (c1 << 16) | c0;
  164. char_colors[3] = (c3 << 16) | c0;
  165. char_colors[4] = (c0 << 16) | c2;
  166. char_colors[5] = (c2 << 16) | c2;
  167. char_colors[6] = (c1 << 16) | c2;
  168. char_colors[7] = (c3 << 16) | c2;
  169. char_colors[8] = (c0 << 16) | c1;
  170. char_colors[9] = (c2 << 16) | c1;
  171. char_colors[10] = (c1 << 16) | c1;
  172. char_colors[11] = (c3 << 16) | c1;
  173. char_colors[12] = (c0 << 16) | c3;
  174. char_colors[13] = (c2 << 16) | c3;
  175. char_colors[14] = (c1 << 16) | c3;
  176. char_colors[15] = (c3 << 16) | c3;
  177. #endif
  178. }
  179. static void (*gp2x_set_colors[4])
  180. (struct graphics_data *, Uint32 *, Uint8, Uint8) =
  181. {
  182. gp2x_set_colors_mzx,
  183. gp2x_set_colors_smzx,
  184. gp2x_set_colors_smzx,
  185. gp2x_set_colors_smzx3
  186. };
  187. static boolean gp2x_init_video(struct graphics_data *graphics,
  188. struct config_info *conf)
  189. {
  190. struct gp2x_render_data *render_data =
  191. cmalloc(sizeof(struct gp2x_render_data));
  192. if(!render_data)
  193. return false;
  194. memset(render_data, 0, sizeof(struct gp2x_render_data));
  195. graphics->render_data = render_data;
  196. graphics->allow_resize = 0;
  197. graphics->bits_per_pixel = 16;
  198. graphics->resolution_width = 320;
  199. graphics->resolution_height = 240;
  200. graphics->window_width = 320;
  201. graphics->window_height = 240;
  202. return set_video_mode();
  203. }
  204. static void gp2x_free_video(struct graphics_data *graphics)
  205. {
  206. sdl_destruct_window(graphics);
  207. free(graphics->render_data);
  208. graphics->render_data = NULL;
  209. }
  210. static boolean gp2x_set_video_mode(struct graphics_data *graphics,
  211. int width, int height, int depth, boolean fullscreen, boolean resize)
  212. {
  213. struct gp2x_render_data *render_data = graphics->render_data;
  214. SDL_PixelFormat *format;
  215. Uint32 halfmask;
  216. if(!sdl_set_video_mode(graphics, width, height, depth, fullscreen, resize))
  217. return false;
  218. format = gp2x_get_screen_surface(render_data)->format;
  219. halfmask = (format->Rmask >> 1) & format->Rmask;
  220. halfmask |= (format->Gmask >> 1) & format->Gmask;
  221. halfmask |= (format->Bmask >> 1) & format->Bmask;
  222. render_data->halfmask = halfmask;
  223. return true;
  224. }
  225. static void gp2x_update_colors(struct graphics_data *graphics,
  226. struct rgb_color *palette, Uint32 count)
  227. {
  228. struct gp2x_render_data *render_data = graphics->render_data;
  229. SDL_Surface *screen = gp2x_get_screen_surface(render_data);
  230. Uint32 i;
  231. for(i = 0; i < count; i++)
  232. {
  233. graphics->flat_intensity_palette[i] =
  234. SDL_MapRGBA(screen->format, palette[i].r, palette[i].g,
  235. palette[i].b, 255);
  236. }
  237. }
  238. static void gp2x_get_screen_coords(struct graphics_data *graphics,
  239. int screen_x, int screen_y, int *x, int *y, int *min_x, int *min_y,
  240. int *max_x, int *max_y)
  241. {
  242. *x = screen_x * 2;
  243. *y = screen_y * 35 / 24;
  244. *min_x = 0;
  245. *min_y = 0;
  246. *max_x = 319;
  247. *max_y = 239;
  248. }
  249. static void gp2x_set_screen_coords(struct graphics_data *graphics,
  250. int x, int y, int *screen_x, int *screen_y)
  251. {
  252. *screen_x = x / 2;
  253. *screen_y = y * 24 / 35;
  254. }
  255. static void gp2x_render_graph(struct graphics_data *graphics)
  256. {
  257. struct gp2x_render_data *render_data = graphics->render_data;
  258. render_graph8((Uint8 *)render_data->buffer, 640, graphics,
  259. gp2x_set_colors[graphics->screen_mode]);
  260. }
  261. static void gp2x_render_cursor(struct graphics_data *graphics,
  262. Uint32 x, Uint32 y, Uint16 color, Uint8 lines, Uint8 offset)
  263. {
  264. struct gp2x_render_data *render_data = graphics->render_data;
  265. Uint32 flatcolor = graphics->flat_intensity_palette[color];
  266. flatcolor |= flatcolor << 16;
  267. render_cursor((Uint32 *)render_data->buffer, 640, 8, x, y, flatcolor, lines,
  268. offset);
  269. }
  270. static void gp2x_render_mouse(struct graphics_data *graphics,
  271. Uint32 x, Uint32 y, Uint8 w, Uint8 h)
  272. {
  273. struct gp2x_render_data *render_data = graphics->render_data;
  274. render_mouse((Uint32 *)render_data->buffer, 640, 8, x, y, 0xFFFFFFFF,
  275. 0x0, w, h);
  276. }
  277. static void gp2x_sync_screen(struct graphics_data *graphics)
  278. {
  279. struct gp2x_render_data *render_data = graphics->render_data;
  280. SDL_Surface *screen = gp2x_get_screen_surface(render_data);
  281. Uint32 line_advance = screen->pitch / 2;
  282. Uint16 *dest = (Uint16*)screen->pixels;
  283. Uint16 *src = render_data->buffer;
  284. Uint32 i, j, skip = 0;
  285. SDL_LockSurface(screen);
  286. for(i = 0; i < 240; i++)
  287. {
  288. skip += 35 - 24;
  289. if(skip < 24)
  290. memcpy(dest, src, sizeof(Uint16) * 320);
  291. else
  292. {
  293. for(j = 0; j < 320; j++)
  294. {
  295. if(src[j] == src[j+320])
  296. dest[j] = src[j];
  297. else
  298. dest[j] = ((src[j] >> 1) & render_data->halfmask) +
  299. ((src[j+320] >> 1) & render_data->halfmask);
  300. }
  301. skip -= 24;
  302. src += 320;
  303. }
  304. src += 320;
  305. dest += line_advance;
  306. }
  307. SDL_UnlockSurface(screen);
  308. if(render_data->sdl.shadow)
  309. {
  310. SDL_BlitSurface(render_data->sdl.shadow,
  311. &render_data->sdl.shadow->clip_rect, render_data->sdl.screen,
  312. &render_data->sdl.screen->clip_rect);
  313. }
  314. #if SDL_VERSION_ATLEAST(2,0,0)
  315. SDL_UpdateWindowSurface(render_data->sdl.window);
  316. #else
  317. SDL_Flip(render_data->sdl.screen);
  318. #endif
  319. }
  320. void render_gp2x_register(struct renderer *renderer)
  321. {
  322. memset(renderer, 0, sizeof(struct renderer));
  323. renderer->init_video = gp2x_init_video;
  324. renderer->free_video = gp2x_free_video;
  325. renderer->check_video_mode = sdl_check_video_mode;
  326. renderer->set_video_mode = gp2x_set_video_mode;
  327. renderer->update_colors = gp2x_update_colors;
  328. renderer->resize_screen = resize_screen_standard;
  329. renderer->get_screen_coords = gp2x_get_screen_coords;
  330. renderer->set_screen_coords = gp2x_set_screen_coords;
  331. renderer->render_graph = gp2x_render_graph;
  332. renderer->render_cursor = gp2x_render_cursor;
  333. renderer->render_mouse = gp2x_render_mouse;
  334. renderer->sync_screen = gp2x_sync_screen;
  335. }