plat_sdl.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2013
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <SDL/SDL_ttf.h>
  11. #include "../libpicofe/input.h"
  12. #include "../libpicofe/plat.h"
  13. #include "../libpicofe/plat_sdl.h"
  14. #include "../libpicofe/in_sdl.h"
  15. #include "../libpicofe/gl.h"
  16. #include "emu.h"
  17. #include "configfile_fk.h"
  18. #include "menu_pico.h"
  19. #include "input_pico.h"
  20. #include "plat_sdl.h"
  21. #include "version.h"
  22. #include <pico/pico_int.h>
  23. #define RES_HW_SCREEN_HORIZONTAL 240
  24. #define RES_HW_SCREEN_VERTICAL 240
  25. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  26. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  27. #define ABS(x) (((x) < 0) ? (-x) : (x))
  28. #define AVERAGE(z, x) ((((z) & 0xF7DEF7DE) >> 1) + (((x) & 0xF7DEF7DE) >> 1))
  29. #define AVERAGEHI(AB) ((((AB) & 0xF7DE0000) >> 1) + (((AB) & 0xF7DE) << 15))
  30. #define AVERAGELO(CD) ((((CD) & 0xF7DE) >> 1) + (((CD) & 0xF7DE0000) >> 17))
  31. // Support math
  32. #define Half(A) (((A) >> 1) & 0x7BEF)
  33. #define Quarter(A) (((A) >> 2) & 0x39E7)
  34. // Error correction expressions to piece back the lower bits together
  35. #define RestHalf(A) ((A) & 0x0821)
  36. #define RestQuarter(A) ((A) & 0x1863)
  37. // Error correction expressions for quarters of pixels
  38. #define Corr1_3(A, B) Quarter(RestQuarter(A) + (RestHalf(B) << 1) + RestQuarter(B))
  39. #define Corr3_1(A, B) Quarter((RestHalf(A) << 1) + RestQuarter(A) + RestQuarter(B))
  40. // Error correction expressions for halves
  41. #define Corr1_1(A, B) ((A) & (B) & 0x0821)
  42. // Quarters
  43. #define Weight1_3(A, B) (Quarter(A) + Half(B) + Quarter(B) + Corr1_3(A, B))
  44. #define Weight3_1(A, B) (Half(A) + Quarter(A) + Quarter(B) + Corr3_1(A, B))
  45. // Halves
  46. #define Weight1_1(A, B) (Half(A) + Half(B) + Corr1_1(A, B))
  47. static void *shadow_fb;
  48. static struct area { int w, h; } area;
  49. static struct in_pdata in_sdl_platform_data = {
  50. .defbinds = in_sdl_defbinds,
  51. .key_map = in_sdl_key_map,
  52. .joy_map = in_sdl_joy_map,
  53. };
  54. static struct in_pdata in_sdl_platform_data_SMS = {
  55. .defbinds = in_sdl_defbinds_SMS,
  56. .key_map = in_sdl_key_map,
  57. .joy_map = in_sdl_joy_map,
  58. };
  59. /* YUV stuff */
  60. static int yuv_ry[32], yuv_gy[32], yuv_by[32];
  61. static unsigned char yuv_u[32 * 2], yuv_v[32 * 2];
  62. static unsigned char yuv_y[256];
  63. static struct uyvy { uint32_t y:8; uint32_t vyu:24; } yuv_uyvy[65536];
  64. SDL_Surface * hw_screen = NULL;
  65. SDL_Surface * virtual_hw_screen = NULL;
  66. static SDL_Surface * sms_game_screen = NULL;
  67. static SDL_Surface * gg_game_screen = NULL;
  68. void clear_screen(SDL_Surface *surface, uint16_t color)
  69. {
  70. if(surface){
  71. uint16_t *dest_ptr = (uint16_t *)surface->pixels;
  72. uint32_t x, y;
  73. for(y = 0; y < surface->h; y++)
  74. {
  75. for(x = 0; x < surface->w; x++, dest_ptr++)
  76. {
  77. *dest_ptr = color;
  78. }
  79. }
  80. }
  81. }
  82. void bgr_to_uyvy_init(void)
  83. {
  84. int i, v;
  85. /* init yuv converter:
  86. y0 = (int)((0.299f * r0) + (0.587f * g0) + (0.114f * b0));
  87. y1 = (int)((0.299f * r1) + (0.587f * g1) + (0.114f * b1));
  88. u = (int)(8 * 0.565f * (b0 - y0)) + 128;
  89. v = (int)(8 * 0.713f * (r0 - y0)) + 128;
  90. */
  91. for (i = 0; i < 32; i++) {
  92. yuv_ry[i] = (int)(0.299f * i * 65536.0f + 0.5f);
  93. yuv_gy[i] = (int)(0.587f * i * 65536.0f + 0.5f);
  94. yuv_by[i] = (int)(0.114f * i * 65536.0f + 0.5f);
  95. }
  96. for (i = -32; i < 32; i++) {
  97. v = (int)(8 * 0.565f * i) + 128;
  98. if (v < 0)
  99. v = 0;
  100. if (v > 255)
  101. v = 255;
  102. yuv_u[i + 32] = v;
  103. v = (int)(8 * 0.713f * i) + 128;
  104. if (v < 0)
  105. v = 0;
  106. if (v > 255)
  107. v = 255;
  108. yuv_v[i + 32] = v;
  109. }
  110. // valid Y range seems to be 16..235
  111. for (i = 0; i < 256; i++) {
  112. yuv_y[i] = 16 + 219 * i / 32;
  113. }
  114. // everything combined into one large array for speed
  115. for (i = 0; i < 65536; i++) {
  116. int r = (i >> 11) & 0x1f, g = (i >> 6) & 0x1f, b = (i >> 0) & 0x1f;
  117. int y = (yuv_ry[r] + yuv_gy[g] + yuv_by[b]) >> 16;
  118. yuv_uyvy[i].y = yuv_y[y];
  119. #if CPU_IS_LE
  120. yuv_uyvy[i].vyu = (yuv_v[r-y + 32] << 16) | (yuv_y[y] << 8) | yuv_u[b-y + 32];
  121. #else
  122. yuv_uyvy[i].vyu = (yuv_v[b-y + 32] << 16) | (yuv_y[y] << 8) | yuv_u[r-y + 32];
  123. #endif
  124. }
  125. }
  126. void rgb565_to_uyvy(void *d, const void *s, int w, int h, int pitch, int x2)
  127. {
  128. uint32_t *dst = d;
  129. const uint16_t *src = s;
  130. int i;
  131. if (x2) while (h--) {
  132. for (i = w; i > 0; src += 4, dst += 4, i -= 4)
  133. {
  134. struct uyvy *uyvy0 = yuv_uyvy + src[0], *uyvy1 = yuv_uyvy + src[1];
  135. struct uyvy *uyvy2 = yuv_uyvy + src[2], *uyvy3 = yuv_uyvy + src[3];
  136. #if CPU_IS_LE
  137. dst[0] = (uyvy0->y << 24) | uyvy0->vyu;
  138. dst[1] = (uyvy1->y << 24) | uyvy1->vyu;
  139. dst[2] = (uyvy2->y << 24) | uyvy2->vyu;
  140. dst[3] = (uyvy3->y << 24) | uyvy3->vyu;
  141. #else
  142. dst[0] = uyvy0->y | (uyvy0->vyu << 8);
  143. dst[1] = uyvy1->y | (uyvy1->vyu << 8);
  144. dst[2] = uyvy2->y | (uyvy2->vyu << 8);
  145. dst[3] = uyvy3->y | (uyvy3->vyu << 8);
  146. #endif
  147. }
  148. src += pitch - w;
  149. } else while (h--) {
  150. for (i = w; i > 0; src += 4, dst += 2, i -= 4)
  151. {
  152. struct uyvy *uyvy0 = yuv_uyvy + src[0], *uyvy1 = yuv_uyvy + src[1];
  153. struct uyvy *uyvy2 = yuv_uyvy + src[2], *uyvy3 = yuv_uyvy + src[3];
  154. #if CPU_IS_LE
  155. dst[0] = (uyvy1->y << 24) | uyvy0->vyu;
  156. dst[1] = (uyvy3->y << 24) | uyvy2->vyu;
  157. #else
  158. dst[0] = uyvy1->y | (uyvy0->vyu << 8);
  159. dst[1] = uyvy3->y | (uyvy2->vyu << 8);
  160. #endif
  161. }
  162. src += pitch - w;
  163. }
  164. }
  165. // Nearest neighboor
  166. void flip_NN(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  167. int w2=new_w;
  168. int h2=new_h;
  169. int x_ratio = (int)((virtual_screen->w<<16)/w2) +1;
  170. int y_ratio = (int)((virtual_screen->h<<16)/h2) +1;
  171. //int x_ratio = (int)((w1<<16)/w2) ;
  172. //int y_ratio = (int)((h1<<16)/h2) ;
  173. //printf("virtual_screen->w=%d, virtual_screen->h=%d\n", virtual_screen->w, virtual_screen->h);
  174. int x2, y2 ;
  175. for (int i=0;i<h2;i++) {
  176. if(i>=RES_HW_SCREEN_VERTICAL){
  177. continue;
  178. }
  179. //printf("\n\ny=%d\n", i);
  180. for (int j=0;j<w2;j++) {
  181. if(j>=RES_HW_SCREEN_HORIZONTAL){
  182. continue;
  183. }
  184. //printf("x=%d, ",j);
  185. x2 = ((j*x_ratio)>>16) ;
  186. y2 = ((i*y_ratio)>>16) ;
  187. //printf("y=%d, x=%d, y2=%d, x2=%d, (y2*virtual_screen->w)+x2=%d\n", i, j, y2, x2, (y2*virtual_screen->w)+x2);
  188. *(uint16_t*)(hardware_screen->pixels+(i* ((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2 ) +j)*sizeof(uint16_t)) =
  189. *(uint16_t*)(virtual_screen->pixels + ((y2*virtual_screen->w)+x2) *sizeof(uint16_t)) ;
  190. }
  191. }
  192. }
  193. // Nearest neighboor with possible out of screen coordinates (for cropping)
  194. void flip_NN_AllowOutOfScreen(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  195. int w2=new_w;
  196. int h2=new_h;
  197. int x_ratio = (int)((virtual_screen->w<<16)/w2) +1;
  198. int y_ratio = (int)((virtual_screen->h<<16)/h2) +1;
  199. //int x_ratio = (int)((w1<<16)/w2) ;
  200. //int y_ratio = (int)((h1<<16)/h2) ;
  201. //printf("virtual_screen->w=%d, virtual_screen->h=%d\n", virtual_screen->w, virtual_screen->h);
  202. int x2, y2 ;
  203. /// --- Compute padding for centering when out of bounds ---
  204. int x_padding = 0;
  205. if(w2>RES_HW_SCREEN_HORIZONTAL){
  206. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  207. }
  208. for (int i=0;i<h2;i++) {
  209. if(i>=RES_HW_SCREEN_VERTICAL){
  210. continue;
  211. }
  212. //printf("\n\ny=%d\n", i);
  213. for (int j=0;j<w2;j++) {
  214. if(j>=RES_HW_SCREEN_HORIZONTAL){
  215. continue;
  216. }
  217. //printf("x=%d, ",j);
  218. x2 = ((j*x_ratio)>>16) ;
  219. y2 = ((i*y_ratio)>>16) ;
  220. //printf("y=%d, x=%d, y2=%d, x2=%d, (y2*virtual_screen->w)+x2=%d\n", i, j, y2, x2, (y2*virtual_screen->w)+x2);
  221. *(uint16_t*)(hardware_screen->pixels+(i* ((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2 ) +j)*sizeof(uint16_t)) =
  222. *(uint16_t*)(virtual_screen->pixels + ((y2*virtual_screen->w)+x2 + x_padding) *sizeof(uint16_t)) ;
  223. }
  224. }
  225. }
  226. /// Nearest neighboor optimized with possible out of screen coordinates (for cropping)
  227. void flip_NNOptimized_AllowOutOfScreen(SDL_Surface *src_surface, SDL_Surface *dst_surface, int new_w, int new_h){
  228. int x_ratio = (int)((src_surface->w<<16)/new_w);
  229. int y_ratio = (int)((src_surface->h<<16)/new_h);
  230. int x2, y2;
  231. /*printf("src_surface dimensions: %dx%d, new dimensions: %dx%d\n",
  232. src_surface->w, src_surface->h, new_w, new_h);*/
  233. /// --- Compute padding for centering when out of bounds ---
  234. int y_padding_dst = MAX((RES_HW_SCREEN_VERTICAL-new_h)/2, 0);
  235. int x_padding_dst = MAX((RES_HW_SCREEN_HORIZONTAL-new_w)/2, 0);
  236. /*printf("padding_dst_x: %d, padding_dst_y: %d\n", x_padding_dst, y_padding_dst);*/
  237. /// --- Compute offset coordinates in src surface
  238. int x_offset_src = (new_w>RES_HW_SCREEN_HORIZONTAL) ? (new_w-RES_HW_SCREEN_HORIZONTAL)/2 + 1 : 0;
  239. int x_offset_src_ratio = x_offset_src*src_surface->w/new_w;
  240. /*printf("x_offset_src: %d,\n", x_offset_src);*/
  241. for (int i=0;i<new_h;i++)
  242. {
  243. if(i>=RES_HW_SCREEN_VERTICAL){
  244. continue;
  245. }
  246. uint16_t* t = (uint16_t*)(dst_surface->pixels +
  247. ( (i+y_padding_dst)* dst_surface->w + x_padding_dst )*sizeof(uint16_t));
  248. y2 = ((i*y_ratio)>>16);
  249. uint16_t* p = (uint16_t*)(src_surface->pixels + (y2*src_surface->w + x_offset_src_ratio) *sizeof(uint16_t));
  250. int rat = 0;
  251. for (int j=0;j<new_w;j++)
  252. {
  253. if(j>=RES_HW_SCREEN_HORIZONTAL){
  254. continue;
  255. }
  256. x2 = (rat>>16);
  257. *t++ = p[x2];
  258. rat += x_ratio;
  259. //printf("y=%d, x=%d, y2=%d, x2=%d, (y2*src_surface->w)+x2=%d\n", i, j, y2, x2, (y2*src_surface->w)+x2);
  260. }
  261. }
  262. }
  263. /// Nearest neighboor with 2D bilinear and interp by the number of pixel diff, not 2
  264. void flip_NNOptimized_MissingPixelsBilinear(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  265. int w1=virtual_screen->w;
  266. int h1=virtual_screen->h;
  267. int w2=new_w;
  268. int h2=new_h;
  269. int y_padding = (RES_HW_SCREEN_VERTICAL-new_h)/2;
  270. //int x_ratio = (int)((w1<<16)/w2) +1;
  271. //int y_ratio = (int)((h1<<16)/h2) +1;
  272. int x_ratio = (int)((w1<<16)/w2);
  273. int y_ratio = (int)((h1<<16)/h2);
  274. int x1, y1;
  275. /*int cnt_yes_x_yes_y, cnt_yes_x_no_y, cnt_no_x_yes_y, cnt_no_x_no_y;
  276. cnt_yes_x_yes_y= cnt_yes_x_no_y= cnt_no_x_yes_y= cnt_no_x_no_y = 0;*/
  277. for (int i=0;i<h2;i++)
  278. {
  279. uint16_t* t = (uint16_t*)(hardware_screen->pixels+((i+y_padding)*w2)*sizeof(uint16_t));
  280. y1 = ((i*y_ratio)>>16);
  281. int px_diff_next_y = MAX( (((i+1)*y_ratio)>>16) - y1, 1);
  282. //printf("px_diff_next_y:%d\n", px_diff_next_y);
  283. uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y1*w1) *sizeof(uint16_t));
  284. int rat = 0;
  285. for (int j=0;j<w2;j++)
  286. {
  287. // ------ current x value ------
  288. x1 = (rat>>16);
  289. int px_diff_next_x = MAX( ((rat+x_ratio)>>16) - x1, 1);
  290. // ------ optimized bilinear (to put in function) -------
  291. uint16_t * cur_p;
  292. int cur_y_offset;
  293. uint32_t red_comp = 0;
  294. uint32_t green_comp = 0;
  295. uint32_t blue_comp = 0;
  296. for(int cur_px_diff_y=0; cur_px_diff_y<px_diff_next_y; cur_px_diff_y++){
  297. cur_y_offset = (y1+cur_px_diff_y<h1)?(w1*cur_px_diff_y):0;
  298. for(int cur_px_diff_x=0; cur_px_diff_x<px_diff_next_x; cur_px_diff_x++){
  299. cur_p = (x1+cur_px_diff_x<w1)?(p+x1+cur_px_diff_x+cur_y_offset):(p+x1+cur_y_offset);
  300. red_comp += (*cur_p)&0xF800;
  301. green_comp += (*cur_p)&0x07E0;
  302. blue_comp += (*cur_p)&0x001F;
  303. }
  304. }
  305. red_comp = (red_comp / (px_diff_next_x*px_diff_next_y) )&0xF800;
  306. green_comp = (green_comp / (px_diff_next_x*px_diff_next_y) )&0x07E0;
  307. blue_comp = (blue_comp / (px_diff_next_x*px_diff_next_y) )&0x001F;
  308. *t++ = red_comp+green_comp+blue_comp;
  309. // ------ next pixel ------
  310. rat += x_ratio;
  311. }
  312. }
  313. }
  314. /// Nearest neighbor with 2D bilinear and interpolation with left and right pixels, pseudo gaussian weighting
  315. void flip_NNOptimized_LeftAndRightBilinear(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  316. int w1=virtual_screen->w;
  317. int h1=virtual_screen->h;
  318. int w2=new_w;
  319. int h2=new_h;
  320. int y_padding = (RES_HW_SCREEN_VERTICAL-new_h)/2;
  321. //int x_ratio = (int)((w1<<16)/w2) +1;
  322. //int y_ratio = (int)((h1<<16)/h2) +1;
  323. int x_ratio = (int)((w1<<16)/w2);
  324. int y_ratio = (int)((h1<<16)/h2);
  325. int x1, y1;
  326. uint16_t green_mask = 0x07E0;
  327. /// --- Compute padding for centering when out of bounds ---
  328. int x_padding = 0;
  329. if(w2>RES_HW_SCREEN_HORIZONTAL){
  330. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  331. }
  332. int x_padding_ratio = x_padding*w1/w2;
  333. /// --- Interp params ---
  334. int px_diff_prev_x = 0;
  335. int px_diff_next_x = 0;
  336. uint32_t ponderation_factor;
  337. uint16_t * cur_p;
  338. uint16_t * cur_p_left;
  339. uint16_t * cur_p_right;
  340. uint32_t red_comp, green_comp, blue_comp;
  341. //int cnt_interp = 0; int cnt_no_interp = 0;
  342. //printf("virtual_screen->w=%d, virtual_screen->w=%d\n", virtual_screen->w, virtual_screen->h);
  343. for (int i=0;i<h2;i++)
  344. {
  345. if(i>=RES_HW_SCREEN_VERTICAL){
  346. continue;
  347. }
  348. uint16_t* t = (uint16_t*)(hardware_screen->pixels+( (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2))*sizeof(uint16_t));
  349. y1 = ((i*y_ratio)>>16);
  350. uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y1*w1 + x_padding_ratio) *sizeof(uint16_t));
  351. int rat = 0;
  352. for (int j=0;j<w2;j++)
  353. {
  354. if(j>=RES_HW_SCREEN_HORIZONTAL){
  355. continue;
  356. }
  357. // ------ current x value ------
  358. x1 = (rat>>16);
  359. px_diff_next_x = ((rat+x_ratio)>>16) - x1;
  360. // ------ adapted bilinear with 3x3 gaussian blur -------
  361. cur_p = p+x1;
  362. if(px_diff_prev_x > 1 || px_diff_next_x > 1){
  363. red_comp=((*cur_p)&0xF800) << 1;
  364. green_comp=((*cur_p)&0x07E0) << 1;
  365. blue_comp=((*cur_p)&0x001F) << 1;
  366. ponderation_factor = 2;
  367. // ---- Interpolate current and left ----
  368. if(px_diff_prev_x > 1 && x1>0){
  369. cur_p_left = p+x1-1;
  370. red_comp += ((*cur_p_left)&0xF800);
  371. green_comp += ((*cur_p_left)&0x07E0);
  372. blue_comp += ((*cur_p_left)&0x001F);
  373. ponderation_factor++;
  374. }
  375. // ---- Interpolate current and right ----
  376. if(px_diff_next_x > 1 && x1+1<w1){
  377. cur_p_right = p+x1+1;
  378. red_comp += ((*cur_p_right)&0xF800);
  379. green_comp += ((*cur_p_right)&0x07E0);
  380. blue_comp += ((*cur_p_right)&0x001F);
  381. ponderation_factor++;
  382. }
  383. /// --- Compute new px value ---
  384. if(ponderation_factor==4){
  385. red_comp = (red_comp >> 2)&0xF800;
  386. green_comp = (green_comp >> 2)&green_mask;
  387. blue_comp = (blue_comp >> 2)&0x001F;
  388. }
  389. else if(ponderation_factor==2){
  390. red_comp = (red_comp >> 1)&0xF800;
  391. green_comp = (green_comp >> 1)&green_mask;
  392. blue_comp = (blue_comp >> 1)&0x001F;
  393. }
  394. else{
  395. red_comp = (red_comp / ponderation_factor )&0xF800;
  396. green_comp = (green_comp / ponderation_factor )&green_mask;
  397. blue_comp = (blue_comp / ponderation_factor )&0x001F;
  398. }
  399. /// --- write pixel ---
  400. *t++ = red_comp+green_comp+blue_comp;
  401. }
  402. else{
  403. /// --- copy pixel ---
  404. *t++ = (*cur_p);
  405. }
  406. /// save number of pixels to interpolate
  407. px_diff_prev_x = px_diff_next_x;
  408. // ------ next pixel ------
  409. rat += x_ratio;
  410. }
  411. }
  412. //printf("cnt_interp = %d, int cnt_no_interp = %d\n", cnt_interp, cnt_no_interp);
  413. }
  414. /// Nearest neighbor with 2D bilinear and interpolation with left, right, up and down pixels, pseudo gaussian weighting
  415. void flip_NNOptimized_LeftRightUpDownBilinear(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  416. int w1=virtual_screen->w;
  417. int h1=virtual_screen->h;
  418. int w2=new_w;
  419. int h2=new_h;
  420. int y_padding = (RES_HW_SCREEN_VERTICAL-new_h)/2;
  421. //int x_ratio = (int)((w1<<16)/w2) +1;
  422. //int y_ratio = (int)((h1<<16)/h2) +1;
  423. int x_ratio = (int)((w1<<16)/w2);
  424. int y_ratio = (int)((h1<<16)/h2);
  425. int x1, y1;
  426. uint16_t green_mask = 0x07E0;
  427. /// --- Compute padding for centering when out of bounds ---
  428. int x_padding = 0;
  429. if(w2>RES_HW_SCREEN_HORIZONTAL){
  430. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  431. }
  432. int x_padding_ratio = x_padding*w1/w2;
  433. /// --- Interp params ---
  434. int px_diff_prev_x = 0;
  435. int px_diff_next_x = 0;
  436. int px_diff_prev_y = 0;
  437. int px_diff_next_y = 0;
  438. uint32_t ponderation_factor;
  439. uint16_t * cur_p;
  440. uint16_t * cur_p_left;
  441. uint16_t * cur_p_right;
  442. uint16_t * cur_p_up;
  443. uint16_t * cur_p_down;
  444. uint32_t red_comp, green_comp, blue_comp;
  445. //int cnt_interp = 0; int cnt_no_interp = 0;
  446. //printf("virtual_screen->w=%d, virtual_screen->w=%d\n", virtual_screen->w, virtual_screen->h);
  447. ///Debug
  448. for (int i=0;i<h2;i++)
  449. {
  450. if(i>=RES_HW_SCREEN_VERTICAL){
  451. continue;
  452. }
  453. uint16_t* t = (uint16_t*)(hardware_screen->pixels+( (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2))*sizeof(uint16_t));
  454. // ------ current and next y value ------
  455. y1 = ((i*y_ratio)>>16);
  456. px_diff_next_y = MAX( (((i+1)*y_ratio)>>16) - y1, 1);
  457. uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y1*w1+x_padding_ratio) *sizeof(uint16_t));
  458. int rat = 0;
  459. for (int j=0;j<w2;j++)
  460. {
  461. if(j>=RES_HW_SCREEN_HORIZONTAL){
  462. continue;
  463. }
  464. // ------ current x value ------
  465. x1 = (rat>>16);
  466. px_diff_next_x = ((rat+x_ratio)>>16) - x1;
  467. // ------ adapted bilinear with 3x3 gaussian blur -------
  468. cur_p = p+x1;
  469. if(px_diff_prev_x > 1 || px_diff_next_x > 1 || px_diff_prev_y > 1 || px_diff_next_y > 1){
  470. red_comp=((*cur_p)&0xF800) << 1;
  471. green_comp=((*cur_p)&0x07E0) << 1;
  472. blue_comp=((*cur_p)&0x001F) << 1;
  473. ponderation_factor = 2;
  474. // ---- Interpolate current and left ----
  475. if(px_diff_prev_x > 1 && x1>0){
  476. cur_p_left = p+x1-1;
  477. red_comp += ((*cur_p_left)&0xF800);
  478. green_comp += ((*cur_p_left)&0x07E0);
  479. blue_comp += ((*cur_p_left)&0x001F);
  480. ponderation_factor++;
  481. }
  482. // ---- Interpolate current and right ----
  483. if(px_diff_next_x > 1 && x1+1<w1){
  484. cur_p_right = p+x1+1;
  485. red_comp += ((*cur_p_right)&0xF800);
  486. green_comp += ((*cur_p_right)&0x07E0);
  487. blue_comp += ((*cur_p_right)&0x001F);
  488. ponderation_factor++;
  489. }
  490. // ---- Interpolate current and up ----
  491. if(px_diff_prev_y > 1 && y1 > 0){
  492. cur_p_up = p+x1-w1;
  493. red_comp += ((*cur_p_up)&0xF800);
  494. green_comp += ((*cur_p_up)&0x07E0);
  495. blue_comp += ((*cur_p_up)&0x001F);
  496. ponderation_factor++;
  497. }
  498. // ---- Interpolate current and down ----
  499. if(px_diff_next_y > 1 && y1 + 1 < h1){
  500. cur_p_down = p+x1+w1;
  501. red_comp += ((*cur_p_down)&0xF800);
  502. green_comp += ((*cur_p_down)&0x07E0);
  503. blue_comp += ((*cur_p_down)&0x001F);
  504. ponderation_factor++;
  505. }
  506. /// --- Compute new px value ---
  507. if(ponderation_factor==4){
  508. red_comp = (red_comp >> 2)&0xF800;
  509. green_comp = (green_comp >> 2)&green_mask;
  510. blue_comp = (blue_comp >> 2)&0x001F;
  511. }
  512. else if(ponderation_factor==2){
  513. red_comp = (red_comp >> 1)&0xF800;
  514. green_comp = (green_comp >> 1)&green_mask;
  515. blue_comp = (blue_comp >> 1)&0x001F;
  516. }
  517. else{
  518. red_comp = (red_comp / ponderation_factor )&0xF800;
  519. green_comp = (green_comp / ponderation_factor )&green_mask;
  520. blue_comp = (blue_comp / ponderation_factor )&0x001F;
  521. }
  522. /// --- write pixel ---
  523. *t++ = red_comp+green_comp+blue_comp;
  524. }
  525. else{
  526. /// --- copy pixel ---
  527. *t++ = (*cur_p);
  528. }
  529. /// save number of pixels to interpolate
  530. px_diff_prev_x = px_diff_next_x;
  531. // ------ next pixel ------
  532. rat += x_ratio;
  533. }
  534. px_diff_prev_y = px_diff_next_y;
  535. }
  536. //printf("cnt_interp = %d, int cnt_no_interp = %d\n", cnt_interp, cnt_no_interp);
  537. }
  538. /// Nearest neighbor with 2D bilinear and interpolation with left, right, up and down pixels, pseudo gaussian weighting
  539. void flip_NNOptimized_LeftRightUpDownBilinear_Optimized4(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  540. int w1=virtual_screen->w;
  541. int h1=virtual_screen->h;
  542. int w2=new_w;
  543. int h2=new_h;
  544. int y_padding = (RES_HW_SCREEN_VERTICAL-new_h)/2;
  545. int x_ratio = (int)((w1<<16)/w2);
  546. int y_ratio = (int)((h1<<16)/h2);
  547. int x1, y1;
  548. uint16_t green_mask = 0x07E0;
  549. /// --- Compute padding for centering when out of bounds ---
  550. int x_padding = 0;
  551. if(w2>RES_HW_SCREEN_HORIZONTAL){
  552. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  553. }
  554. int x_padding_ratio = x_padding*w1/w2;
  555. /// --- Interp params ---
  556. int px_diff_prev_x = 0;
  557. int px_diff_next_x = 0;
  558. int px_diff_prev_y = 0;
  559. int px_diff_next_y = 0;
  560. uint32_t ponderation_factor;
  561. uint8_t left_px_missing, right_px_missing, up_px_missing, down_px_missing;
  562. int supposed_pond_factor;
  563. uint16_t * cur_p;
  564. uint16_t * cur_p_left;
  565. uint16_t * cur_p_right;
  566. uint16_t * cur_p_up;
  567. uint16_t * cur_p_down;
  568. uint32_t red_comp, green_comp, blue_comp;
  569. //printf("virtual_screen->w=%d, virtual_screen->w=%d\n", virtual_screen->w, virtual_screen->h);
  570. ///Debug
  571. /*int occurence_pond[7];
  572. memset(occurence_pond, 0, 7*sizeof(int));*/
  573. for (int i=0;i<h2;i++)
  574. {
  575. if(i>=RES_HW_SCREEN_VERTICAL){
  576. continue;
  577. }
  578. uint16_t* t = (uint16_t*)(hardware_screen->pixels+( (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2))*sizeof(uint16_t));
  579. // ------ current and next y value ------
  580. y1 = ((i*y_ratio)>>16);
  581. px_diff_next_y = MAX( (((i+1)*y_ratio)>>16) - y1, 1);
  582. uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y1*w1+x_padding_ratio) *sizeof(uint16_t));
  583. int rat = 0;
  584. for (int j=0;j<w2;j++)
  585. {
  586. if(j>=RES_HW_SCREEN_HORIZONTAL){
  587. continue;
  588. }
  589. // ------ current x value ------
  590. x1 = (rat>>16);
  591. px_diff_next_x = ((rat+x_ratio)>>16) - x1;
  592. // ------ adapted bilinear with 3x3 gaussian blur -------
  593. cur_p = p+x1;
  594. if(px_diff_prev_x > 1 || px_diff_next_x > 1 || px_diff_prev_y > 1 || px_diff_next_y > 1){
  595. red_comp=((*cur_p)&0xF800) << 1;
  596. green_comp=((*cur_p)&0x07E0) << 1;
  597. blue_comp=((*cur_p)&0x001F) << 1;
  598. ponderation_factor = 2;
  599. left_px_missing = (px_diff_prev_x > 1 && x1>0);
  600. right_px_missing = (px_diff_next_x > 1 && x1+1<w1);
  601. up_px_missing = (px_diff_prev_y > 1 && y1 > 0);
  602. down_px_missing = (px_diff_next_y > 1 && y1 + 1 < h1);
  603. supposed_pond_factor = 2 + left_px_missing + right_px_missing +
  604. up_px_missing + down_px_missing;
  605. // ---- Interpolate current and up ----
  606. if(up_px_missing){
  607. cur_p_up = p+x1-w1;
  608. if(supposed_pond_factor==3){
  609. red_comp += ((*cur_p_up)&0xF800) << 1;
  610. green_comp += ((*cur_p_up)&0x07E0) << 1;
  611. blue_comp += ((*cur_p_up)&0x001F) << 1;
  612. ponderation_factor+=2;
  613. }
  614. else if(supposed_pond_factor==4 ||
  615. (supposed_pond_factor==5 && !down_px_missing )){
  616. red_comp += ((*cur_p_up)&0xF800);
  617. green_comp += ((*cur_p_up)&0x07E0);
  618. blue_comp += ((*cur_p_up)&0x001F);
  619. ponderation_factor++;
  620. }
  621. }
  622. // ---- Interpolate current and left ----
  623. if(left_px_missing){
  624. cur_p_left = p+x1-1;
  625. if(supposed_pond_factor==3){
  626. red_comp += ((*cur_p_left)&0xF800) << 1;
  627. green_comp += ((*cur_p_left)&0x07E0) << 1;
  628. blue_comp += ((*cur_p_left)&0x001F) << 1;
  629. ponderation_factor+=2;
  630. }
  631. else if(supposed_pond_factor==4 ||
  632. (supposed_pond_factor==5 && !right_px_missing )){
  633. red_comp += ((*cur_p_left)&0xF800);
  634. green_comp += ((*cur_p_left)&0x07E0);
  635. blue_comp += ((*cur_p_left)&0x001F);
  636. ponderation_factor++;
  637. }
  638. }
  639. // ---- Interpolate current and down ----
  640. if(down_px_missing){
  641. cur_p_down = p+x1+w1;
  642. if(supposed_pond_factor==3){
  643. red_comp += ((*cur_p_down)&0xF800) << 1;
  644. green_comp += ((*cur_p_down)&0x07E0) << 1;
  645. blue_comp += ((*cur_p_down)&0x001F) << 1;
  646. ponderation_factor+=2;
  647. }
  648. else if(supposed_pond_factor>=4){
  649. red_comp += ((*cur_p_down)&0xF800);
  650. green_comp += ((*cur_p_down)&0x07E0);
  651. blue_comp += ((*cur_p_down)&0x001F);
  652. ponderation_factor++;
  653. }
  654. }
  655. // ---- Interpolate current and right ----
  656. if(right_px_missing){
  657. cur_p_right = p+x1+1;
  658. if(supposed_pond_factor==3){
  659. red_comp += ((*cur_p_right)&0xF800) << 1;
  660. green_comp += ((*cur_p_right)&0x07E0) << 1;
  661. blue_comp += ((*cur_p_right)&0x001F) << 1;
  662. ponderation_factor+=2;
  663. }
  664. else if(supposed_pond_factor>=4){
  665. red_comp += ((*cur_p_right)&0xF800);
  666. green_comp += ((*cur_p_right)&0x07E0);
  667. blue_comp += ((*cur_p_right)&0x001F);
  668. ponderation_factor++;
  669. }
  670. }
  671. /// --- Compute new px value ---
  672. if(ponderation_factor==4){
  673. red_comp = (red_comp >> 2)&0xF800;
  674. green_comp = (green_comp >> 2)&green_mask;
  675. blue_comp = (blue_comp >> 2)&0x001F;
  676. }
  677. else if(ponderation_factor==2){
  678. red_comp = (red_comp >> 1)&0xF800;
  679. green_comp = (green_comp >> 1)&green_mask;
  680. blue_comp = (blue_comp >> 1)&0x001F;
  681. }
  682. else{
  683. red_comp = (red_comp / ponderation_factor )&0xF800;
  684. green_comp = (green_comp / ponderation_factor )&green_mask;
  685. blue_comp = (blue_comp / ponderation_factor )&0x001F;
  686. }
  687. /// Debug
  688. //occurence_pond[ponderation_factor] += 1;
  689. /// --- write pixel ---
  690. *t++ = red_comp+green_comp+blue_comp;
  691. }
  692. else{
  693. /// --- copy pixel ---
  694. *t++ = (*cur_p);
  695. /// Debug
  696. //occurence_pond[1] += 1;
  697. }
  698. /// save number of pixels to interpolate
  699. px_diff_prev_x = px_diff_next_x;
  700. // ------ next pixel ------
  701. rat += x_ratio;
  702. }
  703. px_diff_prev_y = px_diff_next_y;
  704. }
  705. /// Debug
  706. /*printf("pond: [%d, %d, %d, %d, %d, %d]\n", occurence_pond[1], occurence_pond[2], occurence_pond[3],
  707. occurence_pond[4], occurence_pond[5], occurence_pond[6]);*/
  708. }
  709. /// Nearest neighbor with 2D bilinear and interpolation with left, right, up and down pixels, pseudo gaussian weighting
  710. void flip_NNOptimized_LeftRightUpDownBilinear_Optimized8(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  711. int w1=virtual_screen->w;
  712. int h1=virtual_screen->h;
  713. int w2=new_w;
  714. int h2=new_h;
  715. int y_padding = (RES_HW_SCREEN_VERTICAL-new_h)/2;
  716. //int x_ratio = (int)((w1<<16)/w2) +1;
  717. //int y_ratio = (int)((h1<<16)/h2) +1;
  718. int x_ratio = (int)((w1<<16)/w2);
  719. int y_ratio = (int)((h1<<16)/h2);
  720. int x1, y1;
  721. #ifdef BLACKER_BLACKS
  722. /// Optimization for blacker blacks (our screen do not handle green value of 1 very well)
  723. uint16_t green_mask = 0x07C0;
  724. #else
  725. uint16_t green_mask = 0x07E0;
  726. #endif
  727. /// --- Compute padding for centering when out of bounds ---
  728. int x_padding = 0;
  729. if(w2>RES_HW_SCREEN_HORIZONTAL){
  730. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  731. }
  732. int x_padding_ratio = x_padding*w1/w2;
  733. /// --- Interp params ---
  734. int px_diff_prev_x = 0;
  735. int px_diff_next_x = 0;
  736. int px_diff_prev_y = 0;
  737. int px_diff_next_y = 0;
  738. uint32_t ponderation_factor;
  739. uint8_t left_px_missing, right_px_missing, up_px_missing, down_px_missing;
  740. int supposed_pond_factor;
  741. uint16_t * cur_p;
  742. uint16_t * cur_p_left;
  743. uint16_t * cur_p_right;
  744. uint16_t * cur_p_up;
  745. uint16_t * cur_p_down;
  746. uint32_t red_comp, green_comp, blue_comp;
  747. //printf("virtual_screen->w=%d, virtual_screen->w=%d\n", virtual_screen->w, virtual_screen->h);
  748. ///Debug
  749. /*int occurence_pond[9];
  750. memset(occurence_pond, 0, 9*sizeof(int));*/
  751. for (int i=0;i<h2;i++)
  752. {
  753. if(i>=RES_HW_SCREEN_VERTICAL){
  754. continue;
  755. }
  756. uint16_t* t = (uint16_t*)(hardware_screen->pixels+( (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2))*sizeof(uint16_t));
  757. // ------ current and next y value ------
  758. y1 = ((i*y_ratio)>>16);
  759. px_diff_next_y = MAX( (((i+1)*y_ratio)>>16) - y1, 1);
  760. uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y1*w1+x_padding_ratio) *sizeof(uint16_t));
  761. int rat = 0;
  762. for (int j=0;j<w2;j++)
  763. {
  764. if(j>=RES_HW_SCREEN_HORIZONTAL){
  765. continue;
  766. }
  767. // ------ current x value ------
  768. x1 = (rat>>16);
  769. px_diff_next_x = ((rat+x_ratio)>>16) - x1;
  770. // ------ adapted bilinear with 3x3 gaussian blur -------
  771. cur_p = p+x1;
  772. if(px_diff_prev_x > 1 || px_diff_next_x > 1 || px_diff_prev_y > 1 || px_diff_next_y > 1){
  773. red_comp=((*cur_p)&0xF800) << 1;
  774. green_comp=((*cur_p)&0x07E0) << 1;
  775. blue_comp=((*cur_p)&0x001F) << 1;
  776. ponderation_factor = 2;
  777. left_px_missing = (px_diff_prev_x > 1 && x1>0);
  778. right_px_missing = (px_diff_next_x > 1 && x1+1<w1);
  779. up_px_missing = (px_diff_prev_y > 1 && y1 > 0);
  780. down_px_missing = (px_diff_next_y > 1 && y1 + 1 < h1);
  781. supposed_pond_factor = 2 + left_px_missing + right_px_missing +
  782. up_px_missing + down_px_missing;
  783. // ---- Interpolate current and up ----
  784. if(up_px_missing){
  785. cur_p_up = p+x1-w1;
  786. if(supposed_pond_factor==3){
  787. red_comp += ((*cur_p_up)&0xF800) << 1;
  788. green_comp += ((*cur_p_up)&0x07E0) << 1;
  789. blue_comp += ((*cur_p_up)&0x001F) << 1;
  790. ponderation_factor+=2;
  791. }
  792. else if(supposed_pond_factor == 4 ||
  793. (supposed_pond_factor == 5 && !down_px_missing) ||
  794. supposed_pond_factor == 6 ){
  795. red_comp += ((*cur_p_up)&0xF800);
  796. green_comp += ((*cur_p_up)&0x07E0);
  797. blue_comp += ((*cur_p_up)&0x001F);
  798. ponderation_factor++;
  799. }
  800. }
  801. // ---- Interpolate current and left ----
  802. if(left_px_missing){
  803. cur_p_left = p+x1-1;
  804. if(supposed_pond_factor==3){
  805. red_comp += ((*cur_p_left)&0xF800) << 1;
  806. green_comp += ((*cur_p_left)&0x07E0) << 1;
  807. blue_comp += ((*cur_p_left)&0x001F) << 1;
  808. ponderation_factor+=2;
  809. }
  810. else if(supposed_pond_factor == 4 ||
  811. (supposed_pond_factor == 5 && !right_px_missing) ||
  812. supposed_pond_factor == 6 ){
  813. red_comp += ((*cur_p_left)&0xF800);
  814. green_comp += ((*cur_p_left)&0x07E0);
  815. blue_comp += ((*cur_p_left)&0x001F);
  816. ponderation_factor++;
  817. }
  818. }
  819. // ---- Interpolate current and down ----
  820. if(down_px_missing){
  821. cur_p_down = p+x1+w1;
  822. if(supposed_pond_factor==3 || supposed_pond_factor==6){
  823. red_comp += ((*cur_p_down)&0xF800) << 1;
  824. green_comp += ((*cur_p_down)&0x07E0) << 1;
  825. blue_comp += ((*cur_p_down)&0x001F) << 1;
  826. ponderation_factor+=2;
  827. }
  828. else if(supposed_pond_factor >= 4 && supposed_pond_factor != 6){
  829. red_comp += ((*cur_p_down)&0xF800);
  830. green_comp += ((*cur_p_down)&0x07E0);
  831. blue_comp += ((*cur_p_down)&0x001F);
  832. ponderation_factor++;
  833. }
  834. }
  835. // ---- Interpolate current and right ----
  836. if(right_px_missing){
  837. cur_p_right = p+x1+1;
  838. if(supposed_pond_factor==3 || supposed_pond_factor==6){
  839. red_comp += ((*cur_p_right)&0xF800) << 1;
  840. green_comp += ((*cur_p_right)&0x07E0) << 1;
  841. blue_comp += ((*cur_p_right)&0x001F) << 1;
  842. ponderation_factor+=2;
  843. }
  844. else if(supposed_pond_factor >= 4 && supposed_pond_factor != 6){
  845. red_comp += ((*cur_p_right)&0xF800);
  846. green_comp += ((*cur_p_right)&0x07E0);
  847. blue_comp += ((*cur_p_right)&0x001F);
  848. ponderation_factor++;
  849. }
  850. }
  851. /// --- Compute new px value ---
  852. if(ponderation_factor==8){
  853. red_comp = (red_comp >> 3)&0xF800;
  854. green_comp = (green_comp >> 3)&green_mask;
  855. blue_comp = (blue_comp >> 3)&0x001F;
  856. }
  857. else if(ponderation_factor==4){
  858. red_comp = (red_comp >> 2)&0xF800;
  859. green_comp = (green_comp >> 2)&green_mask;
  860. blue_comp = (blue_comp >> 2)&0x001F;
  861. }
  862. else if(ponderation_factor==2){
  863. red_comp = (red_comp >> 1)&0xF800;
  864. green_comp = (green_comp >> 1)&green_mask;
  865. blue_comp = (blue_comp >> 1)&0x001F;
  866. }
  867. else{
  868. red_comp = (red_comp / ponderation_factor )&0xF800;
  869. green_comp = (green_comp / ponderation_factor )&green_mask;
  870. blue_comp = (blue_comp / ponderation_factor )&0x001F;
  871. }
  872. /// Debug
  873. //occurence_pond[ponderation_factor] += 1;
  874. /// --- write pixel ---
  875. *t++ = red_comp+green_comp+blue_comp;
  876. }
  877. else{
  878. /// --- copy pixel ---
  879. *t++ = (*cur_p);
  880. /// Debug
  881. //occurence_pond[1] += 1;
  882. }
  883. /// save number of pixels to interpolate
  884. px_diff_prev_x = px_diff_next_x;
  885. // ------ next pixel ------
  886. rat += x_ratio;
  887. }
  888. px_diff_prev_y = px_diff_next_y;
  889. }
  890. /// Debug
  891. /*printf("pond: [%d, %d, %d, %d, %d, %d, %d, %d]\n", occurence_pond[1], occurence_pond[2], occurence_pond[3],
  892. occurence_pond[4], occurence_pond[5], occurence_pond[6],
  893. occurence_pond[7], occurence_pond[8]);*/
  894. }
  895. /// Nearest neighbor with full 2D uniform bilinear (interpolation with missing left, right, up and down pixels)
  896. void flip_NNOptimized_FullBilinear_Uniform(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  897. int w1=virtual_screen->w;
  898. int h1=virtual_screen->h;
  899. int w2=new_w;
  900. int h2=new_h;
  901. int y_padding = (RES_HW_SCREEN_VERTICAL-new_h)/2;
  902. //int x_ratio = (int)((w1<<16)/w2) +1;
  903. //int y_ratio = (int)((h1<<16)/h2) +1;
  904. int x_ratio = (int)((w1<<16)/w2);
  905. int y_ratio = (int)((h1<<16)/h2);
  906. int x1, y1;
  907. int px_diff_prev_x = 1;
  908. int px_diff_prev_y = 1;
  909. //int cnt_interp = 0; int cnt_no_interp = 0;
  910. //printf("virtual_screen->w=%d, virtual_screen->w=%d\n", virtual_screen->w, virtual_screen->h);
  911. /// ---- Compute padding for centering when out of bounds ----
  912. int x_padding = 0;
  913. if(w2>RES_HW_SCREEN_HORIZONTAL){
  914. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  915. }
  916. int x_padding_ratio = x_padding*w1/w2;
  917. /// ---- Copy and interpolate pixels ----
  918. for (int i=0;i<h2;i++)
  919. {
  920. if(i>=RES_HW_SCREEN_VERTICAL){
  921. continue;
  922. }
  923. uint16_t* t = (uint16_t*)(hardware_screen->pixels+( (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2))*sizeof(uint16_t));
  924. // ------ current and next y value ------
  925. y1 = ((i*y_ratio)>>16);
  926. int px_diff_next_y = MAX( (((i+1)*y_ratio)>>16) - y1, 1);
  927. uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y1*w1 + x_padding_ratio) *sizeof(uint16_t));
  928. int rat = 0;
  929. for (int j=0;j<w2;j++)
  930. {
  931. if(j>=RES_HW_SCREEN_HORIZONTAL){
  932. continue;
  933. }
  934. // ------ current and next x value ------
  935. x1 = (rat>>16);
  936. int px_diff_next_x = MAX( ((rat+x_ratio)>>16) - x1, 1);
  937. // ------ bilinear uniformly weighted --------
  938. uint32_t red_comp=0, green_comp=0, blue_comp=0, ponderation_factor=0;
  939. uint16_t * cur_p;
  940. int cur_y_offset;
  941. //printf("\npx_diff_prev_y=%d, px_diff_prev_x=%d, px_diff_next_y=%d, px_diff_next_x=%d, interp_px=", px_diff_prev_y, px_diff_prev_x, px_diff_next_y, px_diff_next_x);
  942. for(int cur_px_diff_y=-(px_diff_prev_y-1); cur_px_diff_y<px_diff_next_y; cur_px_diff_y++){
  943. if(y1 + cur_px_diff_y >= h1 || y1 < -cur_px_diff_y){
  944. continue;
  945. }
  946. cur_y_offset = w1*cur_px_diff_y;
  947. //printf("cur_diff_y=%d-> ", cur_px_diff_y);
  948. for(int cur_px_diff_x=-(px_diff_prev_x-1); cur_px_diff_x<px_diff_next_x; cur_px_diff_x++){
  949. if(x1 + cur_px_diff_x >= w1 || x1 < -cur_px_diff_x){
  950. continue;
  951. }
  952. cur_p = (p+cur_y_offset+x1+cur_px_diff_x);
  953. //printf("{y=%d,x=%d}, ", y1+cur_px_diff_y, x1+cur_px_diff_x);
  954. red_comp += ((*cur_p)&0xF800);
  955. green_comp += ((*cur_p)&0x07E0);
  956. blue_comp += ((*cur_p)&0x001F);
  957. ponderation_factor++;
  958. }
  959. }
  960. //printf("\n");
  961. /// ------ Ponderation -------
  962. red_comp = (red_comp / ponderation_factor )&0xF800;
  963. green_comp = (green_comp / ponderation_factor )&0x07E0;
  964. blue_comp = (blue_comp / ponderation_factor )&0x001F;
  965. *t++ = red_comp+green_comp+blue_comp;
  966. /// ------ x Interpolation values -------
  967. px_diff_prev_x = px_diff_next_x;
  968. // ------ next pixel ------
  969. rat += x_ratio;
  970. }
  971. /// ------ y Interpolation values -------
  972. px_diff_prev_y = px_diff_next_y;
  973. }
  974. //printf("cnt_interp = %d, int cnt_no_interp = %d\n", cnt_interp, cnt_no_interp);
  975. }
  976. /// Nearest neighbor with full 2D uniform bilinear (interpolation with missing left, right, up and down pixels)
  977. void flip_NNOptimized_FullBilinear_GaussianWeighted(SDL_Surface *virtual_screen, SDL_Surface *hardware_screen, int new_w, int new_h){
  978. int w1=virtual_screen->w;
  979. int h1=virtual_screen->h;
  980. int w2=new_w;
  981. int h2=new_h;
  982. //printf("virtual_screen->w=%d, virtual_screen->w=%d\n", virtual_screen->w, virtual_screen->h);
  983. int y_padding = (RES_HW_SCREEN_VERTICAL-new_h)/2;
  984. int x_ratio = (int)((w1<<16)/w2);
  985. int y_ratio = (int)((h1<<16)/h2);
  986. int x1, y1;
  987. int px_diff_prev_x = 1;
  988. int px_diff_prev_y = 1;
  989. //int cnt_interp = 0; int cnt_no_interp = 0;
  990. /// ---- Compute padding for centering when out of bounds ----
  991. int x_padding = 0;
  992. if(w2>RES_HW_SCREEN_HORIZONTAL){
  993. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  994. }
  995. int x_padding_ratio = x_padding*w1/w2;
  996. /// ---- Interpolation params ----
  997. uint32_t max_pix_interpolate = 3;
  998. if(max_pix_interpolate > 3 || max_pix_interpolate<1){
  999. printf("ERROR cannot interpolate more than 3x3 px in flip_NNOptimized_FullBilinear_GaussianWeighted\n");
  1000. return;
  1001. }
  1002. /// ---- Convolutional mask ----
  1003. int mask_weight_5x5[] = {36, 24, 6, 24, 16, 4, 6, 4, 1};
  1004. int mask_weight_3x3[] = {4, 2, 2, 1};
  1005. int mask_weight_1x1[] = {1};
  1006. int *mask_weight;
  1007. if(max_pix_interpolate==3){
  1008. mask_weight = mask_weight_5x5;
  1009. }
  1010. else if(max_pix_interpolate==2){
  1011. mask_weight = mask_weight_3x3;
  1012. }
  1013. else{
  1014. mask_weight = mask_weight_1x1;
  1015. }
  1016. /// ---- Copy and interpolate pixels ----
  1017. for (int i=0;i<h2;i++)
  1018. {
  1019. if(i>=RES_HW_SCREEN_VERTICAL){
  1020. continue;
  1021. }
  1022. uint16_t* t = (uint16_t*)(hardware_screen->pixels+( (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2))*sizeof(uint16_t));
  1023. // ------ current and next y value ------
  1024. y1 = ((i*y_ratio)>>16);
  1025. int px_diff_next_y = MIN( MAX( (((i+1)*y_ratio)>>16) - y1, 1), max_pix_interpolate);
  1026. uint16_t* p = (uint16_t*)(virtual_screen->pixels + (y1*w1 + x_padding_ratio) *sizeof(uint16_t));
  1027. int rat = 0;
  1028. for (int j=0;j<w2;j++)
  1029. {
  1030. if(j>=RES_HW_SCREEN_HORIZONTAL){
  1031. continue;
  1032. }
  1033. // ------ current and next x value ------
  1034. x1 = (rat>>16);
  1035. int px_diff_next_x = MIN( MAX( ((rat+x_ratio)>>16) - x1, 1), max_pix_interpolate); //we interpolate max "max_pix_interpolate" pix in each dim
  1036. // ------ bilinear uniformly weighted --------
  1037. uint32_t red_comp=0, green_comp=0, blue_comp=0;
  1038. int ponderation_factor=0;
  1039. uint16_t * cur_p;
  1040. int cur_y_offset;
  1041. //printf("\npx_diff_prev_y=%d, px_diff_prev_x=%d, px_diff_next_y=%d, px_diff_next_x=%d, interp_px=", px_diff_prev_y, px_diff_prev_x, px_diff_next_y, px_diff_next_x);
  1042. for(int cur_px_diff_y=-(px_diff_prev_y-1); cur_px_diff_y<px_diff_next_y; cur_px_diff_y++){
  1043. if(y1 + cur_px_diff_y >= h1 || y1 < -cur_px_diff_y){
  1044. continue;
  1045. }
  1046. cur_y_offset = w1*cur_px_diff_y;
  1047. //printf("cur_diff_y=%d-> ", cur_px_diff_y);
  1048. for(int cur_px_diff_x=-(px_diff_prev_x-1); cur_px_diff_x<px_diff_next_x; cur_px_diff_x++){
  1049. if(x1 + cur_px_diff_x >= w1 || x1 < -cur_px_diff_x){
  1050. continue;
  1051. }
  1052. cur_p = (p+cur_y_offset+x1+cur_px_diff_x);
  1053. int weight = mask_weight[ABS(cur_px_diff_y)*max_pix_interpolate+ABS(cur_px_diff_x)];
  1054. red_comp += ((*cur_p)&0xF800) * weight;
  1055. green_comp += ((*cur_p)&0x07E0) * weight;
  1056. blue_comp += ((*cur_p)&0x001F) * weight;
  1057. ponderation_factor += weight;
  1058. }
  1059. }
  1060. //printf("\n");
  1061. /// ------ Ponderation -------
  1062. red_comp = (red_comp / ponderation_factor) & 0xF800;
  1063. green_comp = (green_comp / ponderation_factor )&0x07E0;
  1064. blue_comp = (blue_comp / ponderation_factor) & 0x001F;
  1065. *t++ = red_comp+green_comp+blue_comp;
  1066. /// ------ x Interpolation values -------
  1067. px_diff_prev_x = px_diff_next_x;
  1068. // ------ next pixel ------
  1069. rat += x_ratio;
  1070. }
  1071. /// ------ y Interpolation values -------
  1072. px_diff_prev_y = px_diff_next_y;
  1073. }
  1074. //printf("cnt_interp = %d, int cnt_no_interp = %d\n", cnt_interp, cnt_no_interp);
  1075. }
  1076. /// Interpolation with left, right pixels, pseudo gaussian weighting for downscaling - operations on 16bits
  1077. void flip_Downscale_LeftRightGaussianFilter_Optimized(SDL_Surface *src_surface, SDL_Surface *dst_surface, int new_w, int new_h){
  1078. int w1=src_surface->w;
  1079. int h1=src_surface->h;
  1080. int w2=dst_surface->w;
  1081. int h2=dst_surface->h;
  1082. //printf("src = %dx%d\n", w1, h1);
  1083. int x_ratio = (int)((w1<<16)/w2);
  1084. int y_ratio = (int)((h1<<16)/h2);
  1085. int y_padding = (RES_HW_SCREEN_VERTICAL-h2)/2;
  1086. int x1, y1;
  1087. uint16_t *src_screen = (uint16_t *)src_surface->pixels;
  1088. uint16_t *dst_screen = (uint16_t *)dst_surface->pixels;
  1089. /// --- Compute padding for centering when out of bounds ---
  1090. int x_padding = 0;
  1091. if(w2>RES_HW_SCREEN_HORIZONTAL){
  1092. x_padding = (w2-RES_HW_SCREEN_HORIZONTAL)/2 + 1;
  1093. }
  1094. int x_padding_ratio = x_padding*w1/w2;
  1095. /// --- Interp params ---
  1096. int px_diff_prev_x = 0;
  1097. int px_diff_next_x = 0;
  1098. uint8_t left_px_missing, right_px_missing;
  1099. uint16_t * cur_p;
  1100. uint16_t * cur_p_left;
  1101. uint16_t * cur_p_right;
  1102. for (int i=0;i<h2;i++)
  1103. {
  1104. if(i>=RES_HW_SCREEN_VERTICAL){
  1105. continue;
  1106. }
  1107. uint16_t* t = (uint16_t*)(dst_screen +
  1108. (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2) );
  1109. // ------ current and next y value ------
  1110. y1 = ((i*y_ratio)>>16);
  1111. uint16_t* p = (uint16_t*)(src_screen + (y1*w1+x_padding_ratio) );
  1112. int rat = 0;
  1113. for (int j=0;j<w2;j++)
  1114. {
  1115. if(j>=RES_HW_SCREEN_HORIZONTAL){
  1116. continue;
  1117. }
  1118. // ------ current x value ------
  1119. x1 = (rat>>16);
  1120. px_diff_next_x = ((rat+x_ratio)>>16) - x1;
  1121. //printf("x1=%d, px_diff_prev_x=%d, px_diff_next_x=%d\n", x1, px_diff_prev_x, px_diff_next_x);
  1122. // ------ adapted bilinear with 3x3 gaussian blur -------
  1123. cur_p = p+x1;
  1124. if(px_diff_prev_x > 1 || px_diff_next_x > 1 ){
  1125. left_px_missing = (px_diff_prev_x > 1 && x1>0);
  1126. right_px_missing = (px_diff_next_x > 1 && x1+1<w1);
  1127. cur_p_left = cur_p-1;
  1128. cur_p_right = cur_p+1;
  1129. // ---- Interpolate current and left ----
  1130. if(left_px_missing && !right_px_missing){
  1131. *t++ = Weight1_1(*cur_p, *cur_p_left);
  1132. //*t++ = Weight1_1(*cur_p, Weight1_3(*cur_p, *cur_p_left));
  1133. }
  1134. // ---- Interpolate current and right ----
  1135. else if(right_px_missing && !left_px_missing){
  1136. *t++ = Weight1_1(*cur_p, *cur_p_right);
  1137. //*t++ = Weight1_1(*cur_p, Weight1_3(*cur_p, *cur_p_right));
  1138. }
  1139. // ---- Interpolate with Left and right pixels
  1140. else{
  1141. *t++ = Weight1_1(Weight1_1(*cur_p, *cur_p_left), Weight1_1(*cur_p, *cur_p_right));
  1142. }
  1143. }
  1144. else{
  1145. /// --- copy pixel ---
  1146. *t++ = (*cur_p);
  1147. /// Debug
  1148. //occurence_pond[1] += 1;
  1149. }
  1150. /// save number of pixels to interpolate
  1151. px_diff_prev_x = px_diff_next_x;
  1152. // ------ next pixel ------
  1153. rat += x_ratio;
  1154. }
  1155. }
  1156. }
  1157. /// Interpolation with left, right pixels, pseudo gaussian weighting for downscaling - operations on 16bits
  1158. void flip_Downscale_LeftRightGaussianFilter_OptimizedWidth320(SDL_Surface *src_surface, SDL_Surface *dst_surface, int new_w, int new_h){
  1159. int w1=src_surface->w;
  1160. int h1=src_surface->h;
  1161. int w2=dst_surface->w;
  1162. int h2=dst_surface->h;
  1163. if(w1!=320){
  1164. printf("src_surface->w (%d) != 320\n", src_surface->w);
  1165. return;
  1166. }
  1167. //printf("src = %dx%d\n", w1, h1);
  1168. int y_ratio = (int)((h1<<16)/h2);
  1169. int y_padding = (RES_HW_SCREEN_VERTICAL-h2)/2;
  1170. int y1;
  1171. uint16_t *src_screen = (uint16_t *)src_surface->pixels;
  1172. uint16_t *dst_screen = (uint16_t *)dst_surface->pixels;
  1173. /* Interpolation */
  1174. for (int i=0;i<h2;i++)
  1175. {
  1176. if(i>=RES_HW_SCREEN_VERTICAL){
  1177. continue;
  1178. }
  1179. uint16_t* t = (uint16_t*)(dst_screen +
  1180. (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2) );
  1181. // ------ current and next y value ------
  1182. y1 = ((i*y_ratio)>>16);
  1183. uint16_t* p = (uint16_t*)(src_screen + (y1*w1) );
  1184. for (int j=0;j<80;j++)
  1185. {
  1186. /* Horizontaly:
  1187. * Before(4):
  1188. * (a)(b)(c)(d)
  1189. * After(3):
  1190. * (aaab)(bc)(cddd)
  1191. */
  1192. uint16_t _a = *(p );
  1193. uint16_t _b = *(p + 1);
  1194. uint16_t _c = *(p + 2);
  1195. uint16_t _d = *(p + 3);
  1196. *(t ) = Weight3_1( _a, _b );
  1197. *(t + 1) = Weight1_1( _b, _c );
  1198. *(t + 2) = Weight1_3( _c, _d );
  1199. // ------ next dst pixel ------
  1200. t+=3;
  1201. p+=4;
  1202. }
  1203. }
  1204. }
  1205. /// Interpolation with left, right pixels, pseudo gaussian weighting for downscaling - operations on 16bits
  1206. void flip_Downscale_OptimizedWidth320_mergeUpDown(SDL_Surface *src_surface, SDL_Surface *dst_surface, int new_w, int new_h){
  1207. int w1=src_surface->w;
  1208. int h1=src_surface->h;
  1209. int w2=dst_surface->w;
  1210. int h2=dst_surface->h;
  1211. if(w1!=320){
  1212. printf("src_surface->w (%d) != 320\n", src_surface->w);
  1213. return;
  1214. }
  1215. //printf("src = %dx%d\n", w1, h1);
  1216. int y_ratio = (int)((h1<<16)/h2);
  1217. int y_padding = (RES_HW_SCREEN_VERTICAL-h2)/2;
  1218. int y1=0, prev_y1=-1, prev_prev_y1=-2;
  1219. uint16_t *src_screen = (uint16_t *)src_surface->pixels;
  1220. uint16_t *dst_screen = (uint16_t *)dst_surface->pixels;
  1221. uint16_t *prev_t, *t_init=dst_screen;
  1222. /* Interpolation */
  1223. for (int i=0;i<h2;i++)
  1224. {
  1225. if(i>=RES_HW_SCREEN_VERTICAL){
  1226. continue;
  1227. }
  1228. prev_t = t_init;
  1229. t_init = (uint16_t*)(dst_screen +
  1230. (i+y_padding)*((w2>RES_HW_SCREEN_HORIZONTAL)?RES_HW_SCREEN_HORIZONTAL:w2) );
  1231. uint16_t *t = t_init;
  1232. // ------ current and next y value ------
  1233. prev_prev_y1 = prev_y1;
  1234. prev_y1 = y1;
  1235. y1 = ((i*y_ratio)>>16);
  1236. uint16_t* p = (uint16_t*)(src_screen + (y1*w1) );
  1237. for (int j=0;j<80;j++)
  1238. {
  1239. /* Horizontaly:
  1240. * Before(4):
  1241. * (a)(b)(c)(d)
  1242. * After(3):
  1243. * (aaab)(bc)(cddd)
  1244. */
  1245. uint16_t _a = *(p );
  1246. uint16_t _b = *(p + 1);
  1247. uint16_t _c = *(p + 2);
  1248. uint16_t _d = *(p + 3);
  1249. *(t ) = Weight3_1( _a, _b );
  1250. *(t + 1) = Weight1_1( _b, _c );
  1251. *(t + 2) = Weight1_3( _c, _d );
  1252. if(prev_y1 == prev_prev_y1 && y1 != prev_y1){
  1253. //printf("we are here %d\n", ++count);
  1254. *(prev_t ) = Weight1_1(*(t ), *(prev_t ));
  1255. *(prev_t + 1) = Weight1_1(*(t + 1), *(prev_t + 1));
  1256. *(prev_t + 2) = Weight1_1(*(t + 2), *(prev_t + 2));
  1257. }
  1258. // ------ next dst pixel ------
  1259. t+=3;
  1260. prev_t+=3;
  1261. p+=4;
  1262. }
  1263. }
  1264. }
  1265. void upscale_160x144_to_240x240_bilinearish(SDL_Surface *src_surface, SDL_Surface *dst_surface)
  1266. {
  1267. if (src_surface->w != 160)
  1268. {
  1269. printf("src_surface->w (%d) != 160 \n", src_surface->w);
  1270. return;
  1271. }
  1272. if (src_surface->h != 144)
  1273. {
  1274. printf("src_surface->h (%d) != 144 \n", src_surface->h);
  1275. return;
  1276. }
  1277. uint16_t *Src16 = (uint16_t *) src_surface->pixels;
  1278. uint16_t *Dst16 = (uint16_t *) dst_surface->pixels;
  1279. // There are 80 blocks of 2 pixels horizontally, and 48 of 3 horizontally.
  1280. // Horizontally: 240=80*3 160=80*2
  1281. // Vertically: 240=48*5 144=48*3
  1282. // Each block of 2*3 becomes 3x5.
  1283. uint32_t BlockX, BlockY;
  1284. uint16_t *BlockSrc;
  1285. uint16_t *BlockDst;
  1286. uint16_t _a, _b, _ab, __a, __b, __ab;
  1287. for (BlockY = 0; BlockY < 48; BlockY++)
  1288. {
  1289. BlockSrc = Src16 + BlockY * 160 * 3;
  1290. BlockDst = Dst16 + BlockY * 240 * 5;
  1291. for (BlockX = 0; BlockX < 80; BlockX++)
  1292. {
  1293. /* Horizontaly:
  1294. * Before(2):
  1295. * (a)(b)
  1296. * After(3):
  1297. * (a)(ab)(b)
  1298. */
  1299. /* Verticaly:
  1300. * Before(3):
  1301. * (1)(2)(3)
  1302. * After(5):
  1303. * (1)(12)(2)(23)(3)
  1304. */
  1305. // -- Line 1 --
  1306. _a = *(BlockSrc );
  1307. _b = *(BlockSrc + 1);
  1308. _ab = Weight1_1( _a, _b);
  1309. *(BlockDst ) = _a;
  1310. *(BlockDst + 1) = _ab;
  1311. *(BlockDst + 2) = _b;
  1312. // -- Line 2 --
  1313. __a = *(BlockSrc + 160 * 1 );
  1314. __b = *(BlockSrc + 160 * 1 + 1);
  1315. __ab = Weight1_1( __a, __b);
  1316. *(BlockDst + 240 * 1 ) = Weight1_1(_a, __a);
  1317. *(BlockDst + 240 * 1 + 1) = Weight1_1(_ab, __ab);
  1318. *(BlockDst + 240 * 1 + 2) = Weight1_1(_b, __b);
  1319. // -- Line 3 --
  1320. *(BlockDst + 240 * 2 ) = __a;
  1321. *(BlockDst + 240 * 2 + 1) = __ab;
  1322. *(BlockDst + 240 * 2 + 2) = __b;
  1323. // -- Line 4 --
  1324. _a = __a;
  1325. _b = __b;
  1326. _ab = __ab;
  1327. __a = *(BlockSrc + 160 * 2 );
  1328. __b = *(BlockSrc + 160 * 2 + 1);
  1329. __ab = Weight1_1( __a, __b);
  1330. *(BlockDst + 240 * 3 ) = Weight1_1(_a, __a);
  1331. *(BlockDst + 240 * 3 + 1) = Weight1_1(_ab, __ab);
  1332. *(BlockDst + 240 * 3 + 2) = Weight1_1(_b, __b);
  1333. // -- Line 5 --
  1334. *(BlockDst + 240 * 4 ) = __a;
  1335. *(BlockDst + 240 * 4 + 1) = __ab;
  1336. *(BlockDst + 240 * 4 + 2) = __b;
  1337. BlockSrc += 2;
  1338. BlockDst += 3;
  1339. }
  1340. }
  1341. }
  1342. void upscale_160x144_to_240x216_bilinearish(SDL_Surface *src_surface, SDL_Surface *dst_surface)
  1343. {
  1344. if (src_surface->w != 160)
  1345. {
  1346. printf("src_surface->w (%d) != 160 \n", src_surface->w);
  1347. return;
  1348. }
  1349. if (src_surface->h != 144)
  1350. {
  1351. printf("src_surface->h (%d) != 144 \n", src_surface->h);
  1352. return;
  1353. }
  1354. /* Y padding for centering */
  1355. uint32_t y_padding = (240 - 216) / 2 + 1;
  1356. uint16_t *Src16 = (uint16_t *) src_surface->pixels;
  1357. uint16_t *Dst16 = ((uint16_t *) dst_surface->pixels) + y_padding * 240;
  1358. // There are 80 blocks of 2 pixels horizontally, and 72 of 2 horizontally.
  1359. // Horizontally: 240=80*3 160=80*2
  1360. // Vertically: 216=72*3 144=72*2
  1361. // Each block of 2*3 becomes 3x5.
  1362. uint32_t BlockX, BlockY;
  1363. uint16_t *BlockSrc;
  1364. uint16_t *BlockDst;
  1365. volatile uint16_t _a, _b, _ab, __a, __b, __ab;
  1366. for (BlockY = 0; BlockY < 72; BlockY++)
  1367. {
  1368. BlockSrc = Src16 + BlockY * 160 * 2;
  1369. BlockDst = Dst16 + BlockY * 240 * 3;
  1370. for (BlockX = 0; BlockX < 80; BlockX++)
  1371. {
  1372. /* Horizontaly:
  1373. * Before(2):
  1374. * (a)(b)
  1375. * After(3):
  1376. * (a)(ab)(b)
  1377. */
  1378. /* Verticaly:
  1379. * Before(2):
  1380. * (1)(2)
  1381. * After(3):
  1382. * (1)(12)(2)
  1383. */
  1384. // -- Line 1 --
  1385. _a = *(BlockSrc );
  1386. _b = *(BlockSrc + 1);
  1387. _ab = Weight1_1( _a, _b);
  1388. *(BlockDst ) = _a;
  1389. *(BlockDst + 1) = _ab;
  1390. *(BlockDst + 2) = _b;
  1391. // -- Line 2 --
  1392. __a = *(BlockSrc + 160 * 1 );
  1393. __b = *(BlockSrc + 160 * 1 + 1);
  1394. __ab = Weight1_1( __a, __b);
  1395. *(BlockDst + 240 * 1 ) = Weight1_1(_a, __a);
  1396. *(BlockDst + 240 * 1 + 1) = Weight1_1(_ab, __ab);
  1397. *(BlockDst + 240 * 1 + 2) = Weight1_1(_b, __b);
  1398. // -- Line 3 --
  1399. *(BlockDst + 240 * 2 ) = __a;
  1400. *(BlockDst + 240 * 2 + 1) = __ab;
  1401. *(BlockDst + 240 * 2 + 2) = __b;
  1402. BlockSrc += 2;
  1403. BlockDst += 3;
  1404. }
  1405. }
  1406. }
  1407. void SDL_Rotate_270(SDL_Surface * hw_surface, SDL_Surface * virtual_hw_surface){
  1408. int i, j;
  1409. uint16_t *source_pixels = (uint16_t*) virtual_hw_surface->pixels;
  1410. uint16_t *dest_pixels = (uint16_t*) hw_surface->pixels;
  1411. /// --- Checking for right pixel format ---
  1412. //printf("Source bpb = %d, Dest bpb = %d\n", virtual_hw_surface->format->BitsPerPixel, hw_surface->format->BitsPerPixel);
  1413. if(virtual_hw_surface->format->BitsPerPixel != 16){
  1414. printf("Error in SDL_FastBlit, Wrong virtual_hw_surface pixel format: %d bpb, expected: 16 bpb\n", virtual_hw_surface->format->BitsPerPixel);
  1415. return;
  1416. }
  1417. if(hw_surface->format->BitsPerPixel != 16){
  1418. printf("Error in SDL_FastBlit, Wrong hw_surface pixel format: %d bpb, expected: 16 bpb\n", hw_surface->format->BitsPerPixel);
  1419. return;
  1420. }
  1421. /// --- Checking if same dimensions ---
  1422. if(hw_surface->w != virtual_hw_surface->w || hw_surface->h != virtual_hw_surface->h){
  1423. printf("Error in SDL_FastBlit, hw_surface (%dx%d) and virtual_hw_surface (%dx%d) have different dimensions\n",
  1424. hw_surface->w, hw_surface->h, virtual_hw_surface->w, virtual_hw_surface->h);
  1425. return;
  1426. }
  1427. /// --- Pixel copy and rotation (270) ---
  1428. uint16_t *cur_p_src, *cur_p_dst;
  1429. for(i=0; i<virtual_hw_surface->h; i++){
  1430. for(j=0; j<virtual_hw_surface->w; j++){
  1431. cur_p_src = source_pixels + i*virtual_hw_surface->w + j;
  1432. cur_p_dst = dest_pixels + (hw_surface->h-1-j)*hw_surface->w + i;
  1433. *cur_p_dst = *cur_p_src;
  1434. }
  1435. }
  1436. }
  1437. void scale_for_gg(SDL_Surface *src_surface,
  1438. SDL_Surface *dst_surface,
  1439. ENUM_ASPECT_RATIOS_TYPES aspect_ratio){
  1440. //printf("In %s\n", __func__);
  1441. switch(aspect_ratio){
  1442. case ASPECT_RATIOS_TYPE_STRETCHED:
  1443. upscale_160x144_to_240x240_bilinearish(src_surface, dst_surface);
  1444. break;
  1445. case ASPECT_RATIOS_TYPE_MANUAL:
  1446. /*;uint32_t h_scaled = src_surface->h*RES_HW_SCREEN_HORIZONTAL/src_surface->w;
  1447. ;uint32_t h_zoomed = h_scaled + aspect_ratio_factor_percent*(RES_HW_SCREEN_VERTICAL - h_scaled)/100;
  1448. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1449. MAX(src_surface->w*h_zoomed/src_surface->h, RES_HW_SCREEN_HORIZONTAL),
  1450. MIN(h_zoomed, RES_HW_SCREEN_VERTICAL));*/
  1451. ;uint32_t h_min = src_surface->h;
  1452. ;uint32_t h_zoomed = h_min + aspect_ratio_factor_percent*(RES_HW_SCREEN_VERTICAL - h_min)/100;
  1453. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1454. src_surface->w*h_zoomed/src_surface->h,
  1455. MIN(h_zoomed, RES_HW_SCREEN_VERTICAL));
  1456. break;
  1457. case ASPECT_RATIOS_TYPE_CROPPED:
  1458. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1459. src_surface->w*RES_HW_SCREEN_VERTICAL/src_surface->h,
  1460. RES_HW_SCREEN_VERTICAL);
  1461. break;
  1462. case ASPECT_RATIOS_TYPE_SCALED:
  1463. upscale_160x144_to_240x216_bilinearish(src_surface, dst_surface);
  1464. break;
  1465. default:
  1466. printf("Wrong aspect ratio value: %d\n", aspect_ratio);
  1467. aspect_ratio = ASPECT_RATIOS_TYPE_STRETCHED;
  1468. break;
  1469. }
  1470. }
  1471. void scale_for_SMS(SDL_Surface *src_surface,
  1472. SDL_Surface *dst_surface,
  1473. ENUM_ASPECT_RATIOS_TYPES aspect_ratio){
  1474. //printf("In %s\n", __func__);
  1475. switch(aspect_ratio){
  1476. case ASPECT_RATIOS_TYPE_STRETCHED:
  1477. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1478. RES_HW_SCREEN_HORIZONTAL,
  1479. RES_HW_SCREEN_VERTICAL);
  1480. break;
  1481. case ASPECT_RATIOS_TYPE_MANUAL:
  1482. ;uint32_t h_scaled = MIN(src_surface->h*RES_HW_SCREEN_HORIZONTAL/src_surface->w,
  1483. RES_HW_SCREEN_VERTICAL);
  1484. uint32_t h_zoomed = MIN(h_scaled + aspect_ratio_factor_percent*(RES_HW_SCREEN_VERTICAL - h_scaled)/100,
  1485. RES_HW_SCREEN_VERTICAL);
  1486. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1487. MAX(src_surface->w*h_zoomed/src_surface->h, RES_HW_SCREEN_HORIZONTAL),
  1488. MIN(h_zoomed, RES_HW_SCREEN_VERTICAL));
  1489. break;
  1490. case ASPECT_RATIOS_TYPE_CROPPED:
  1491. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1492. src_surface->w,
  1493. src_surface->h);
  1494. break;
  1495. case ASPECT_RATIOS_TYPE_SCALED:
  1496. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1497. RES_HW_SCREEN_HORIZONTAL,
  1498. src_surface->h*RES_HW_SCREEN_HORIZONTAL/src_surface->w);
  1499. break;
  1500. default:
  1501. printf("Wrong aspect ratio value: %d\n", aspect_ratio);
  1502. aspect_ratio = ASPECT_RATIOS_TYPE_STRETCHED;
  1503. break;
  1504. }
  1505. }
  1506. void scale_for_genesis(SDL_Surface *src_surface,
  1507. SDL_Surface *dst_surface,
  1508. ENUM_ASPECT_RATIOS_TYPES aspect_ratio){
  1509. //printf("In %s\n", __func__);
  1510. uint16_t hres_max;
  1511. switch(aspect_ratio){
  1512. case ASPECT_RATIOS_TYPE_STRETCHED:
  1513. if(src_surface->w == 320 && src_surface->h < RES_HW_SCREEN_VERTICAL){
  1514. flip_Downscale_OptimizedWidth320_mergeUpDown(src_surface, dst_surface,
  1515. RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL);
  1516. }
  1517. else if(src_surface->w == 320){
  1518. flip_Downscale_LeftRightGaussianFilter_OptimizedWidth320(src_surface, dst_surface,
  1519. RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL);
  1520. }
  1521. else{
  1522. flip_Downscale_LeftRightGaussianFilter_Optimized(src_surface, dst_surface,
  1523. RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL);
  1524. /*flip_Downscale_LeftRightGaussianFilter(src_surface, hw_screen,
  1525. RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL);*/
  1526. }
  1527. break;
  1528. case ASPECT_RATIOS_TYPE_MANUAL:
  1529. hres_max= MIN(RES_HW_SCREEN_VERTICAL, src_surface->h);
  1530. ;uint32_t h_scaled = MIN(src_surface->h*RES_HW_SCREEN_HORIZONTAL/src_surface->w,
  1531. RES_HW_SCREEN_VERTICAL);
  1532. uint32_t h_zoomed = MIN(h_scaled + aspect_ratio_factor_percent*(hres_max - h_scaled)/100,
  1533. RES_HW_SCREEN_VERTICAL);
  1534. flip_NNOptimized_LeftRightUpDownBilinear_Optimized8(src_surface, dst_surface,
  1535. MAX(src_surface->w*h_zoomed/src_surface->h, RES_HW_SCREEN_HORIZONTAL),
  1536. MIN(h_zoomed, RES_HW_SCREEN_VERTICAL));
  1537. break;
  1538. case ASPECT_RATIOS_TYPE_CROPPED:
  1539. /*flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1540. MAX(src_surface->w*RES_HW_SCREEN_VERTICAL/src_surface->h, RES_HW_SCREEN_HORIZONTAL),
  1541. RES_HW_SCREEN_VERTICAL);*/
  1542. hres_max= MIN(RES_HW_SCREEN_VERTICAL, src_surface->h);
  1543. flip_NNOptimized_AllowOutOfScreen(src_surface, dst_surface,
  1544. MAX(src_surface->w*hres_max/src_surface->h, RES_HW_SCREEN_HORIZONTAL),
  1545. hres_max);
  1546. break;
  1547. case ASPECT_RATIOS_TYPE_SCALED:
  1548. flip_NNOptimized_LeftRightUpDownBilinear_Optimized8(src_surface, dst_surface,
  1549. RES_HW_SCREEN_HORIZONTAL,
  1550. MIN(src_surface->h*RES_HW_SCREEN_HORIZONTAL/src_surface->w, RES_HW_SCREEN_VERTICAL));
  1551. break;
  1552. default:
  1553. printf("Wrong aspect ratio value: %d\n", aspect_ratio);
  1554. aspect_ratio = ASPECT_RATIOS_TYPE_STRETCHED;
  1555. flip_NNOptimized_LeftRightUpDownBilinear_Optimized8(src_surface, dst_surface,
  1556. RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL);
  1557. break;
  1558. }
  1559. }
  1560. static int clear_buf_cnt, clear_stat_cnt;
  1561. void plat_video_set_size(int w, int h)
  1562. {
  1563. if (area.w != w || area.h != h) {
  1564. area = (struct area) { w, h };
  1565. if (plat_sdl_change_video_mode(w, h, 0) < 0) {
  1566. // failed, revert to original resolution
  1567. plat_sdl_change_video_mode(g_screen_width, g_screen_height, 0);
  1568. w = g_screen_width, h = g_screen_height;
  1569. }
  1570. if (!plat_sdl_overlay && !plat_sdl_gl_active) {
  1571. g_screen_width = w;
  1572. g_screen_height = h;
  1573. g_screen_ppitch = w;
  1574. g_screen_ptr = plat_sdl_screen->pixels;
  1575. }
  1576. }
  1577. }
  1578. void plat_video_flip(void)
  1579. {
  1580. if (plat_sdl_overlay != NULL) {
  1581. SDL_Rect dstrect =
  1582. { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
  1583. SDL_LockYUVOverlay(plat_sdl_overlay);
  1584. rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
  1585. area.w, area.h, g_screen_ppitch,
  1586. plat_sdl_overlay->w >= 2*area.w);
  1587. SDL_UnlockYUVOverlay(plat_sdl_overlay);
  1588. SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
  1589. }
  1590. else if (plat_sdl_gl_active) {
  1591. gl_flip(shadow_fb, g_screen_ppitch, g_screen_height);
  1592. }
  1593. /*else {
  1594. if (SDL_MUSTLOCK(plat_sdl_screen)) {
  1595. SDL_UnlockSurface(plat_sdl_screen);
  1596. SDL_Flip(plat_sdl_screen);
  1597. SDL_LockSurface(plat_sdl_screen);
  1598. } else
  1599. SDL_Flip(plat_sdl_screen);
  1600. g_screen_ptr = plat_sdl_screen->pixels;
  1601. plat_video_set_buffer(g_screen_ptr);
  1602. if (clear_buf_cnt) {
  1603. memset(g_screen_ptr, 0, plat_sdl_screen->w*plat_sdl_screen->h * 2);
  1604. clear_buf_cnt--;
  1605. }
  1606. }*/
  1607. else {
  1608. if (SDL_MUSTLOCK(plat_sdl_screen))
  1609. SDL_UnlockSurface(plat_sdl_screen);
  1610. /* Surface with game data */
  1611. SDL_Surface *game_surface;
  1612. /* Sega Game Gear -> 160*144 res in 320*240 surface */
  1613. //if ((PicoIn.AHW & PAHW_SMS) && (Pico.m.hardware & 0x3) == 0x3){
  1614. if ((PicoIn.AHW & PAHW_SMS) && (Pico.m.hardware & 0x01)){
  1615. /* Copy Game Gear game pixels */
  1616. int offset_y = (plat_sdl_screen->h - gg_game_screen->h)/2;
  1617. int offset_x = (plat_sdl_screen->w - gg_game_screen->w)/2 - 1;
  1618. int y;
  1619. for(y=0; y<gg_game_screen->h; y++){
  1620. memcpy((uint16_t*)gg_game_screen->pixels + gg_game_screen->w*y,
  1621. (uint16_t*)plat_sdl_screen->pixels + plat_sdl_screen->w*(y+offset_y) + offset_x,
  1622. gg_game_screen->w*sizeof(uint16_t));
  1623. }
  1624. game_surface = gg_game_screen;
  1625. }
  1626. /* Sega Master System -> 256*192 res in 320*240 surface */
  1627. else if (PicoIn.AHW & PAHW_SMS){
  1628. /* Copy sms game pixels */
  1629. int offset_y = (plat_sdl_screen->h - sms_game_screen->h)/2;
  1630. int offset_x = (plat_sdl_screen->w - sms_game_screen->w)/2 + 1;
  1631. int y;
  1632. for(y=0; y<sms_game_screen->h; y++){
  1633. memcpy((uint16_t*)sms_game_screen->pixels + sms_game_screen->w*y,
  1634. (uint16_t*)plat_sdl_screen->pixels + plat_sdl_screen->w*(y+offset_y) + offset_x,
  1635. sms_game_screen->w*sizeof(uint16_t));
  1636. }
  1637. game_surface = sms_game_screen;
  1638. }
  1639. else{
  1640. game_surface = plat_sdl_screen;
  1641. }
  1642. /// --------------Optimized Flip depending on aspect ratio -------------
  1643. static int prev_aspect_ratio;
  1644. if(prev_aspect_ratio != aspect_ratio || need_screen_cleared){
  1645. /*printf("aspect ratio changed: %s\n", aspect_ratio_name[aspect_ratio]);
  1646. printf("game_surface res = %dx%d\n", game_surface->w, game_surface->h);*/
  1647. clear_screen(virtual_hw_screen, 0);
  1648. prev_aspect_ratio = aspect_ratio;
  1649. need_screen_cleared = 0;
  1650. }
  1651. /** Rescale for console */
  1652. /** Game Gear */
  1653. if((PicoIn.AHW & PAHW_SMS) && (Pico.m.hardware & 0x01)){
  1654. scale_for_gg(game_surface, virtual_hw_screen, aspect_ratio);
  1655. }
  1656. /** SMS */
  1657. else if(PicoIn.AHW & PAHW_SMS){
  1658. scale_for_SMS(game_surface, virtual_hw_screen, aspect_ratio);
  1659. }
  1660. /** Genesis */
  1661. else{
  1662. scale_for_genesis(game_surface, virtual_hw_screen, aspect_ratio);
  1663. }
  1664. // Rotate
  1665. //SDL_Rotate_270(hw_screen, virtual_hw_screen);
  1666. //SDL_BlitSurface(virtual_hw_screen, NULL, hw_screen, NULL);
  1667. memcpy(hw_screen->pixels, virtual_hw_screen->pixels, hw_screen->w*hw_screen->h*sizeof(uint16_t));
  1668. /// --- Real Flip ---
  1669. SDL_Flip(hw_screen);
  1670. /*g_screen_ptr = plat_sdl_screen->pixels;
  1671. PicoDrawSetOutBuf(g_screen_ptr, g_screen_ppitch * 2);*/
  1672. }
  1673. /*if (clear_stat_cnt) {
  1674. unsigned short *d = (unsigned short *)g_screen_ptr + g_screen_ppitch * g_screen_height;
  1675. int l = g_screen_ppitch * 8;
  1676. memset((int *)(d - l), 0, l * 2);
  1677. clear_stat_cnt--;
  1678. }*/
  1679. }
  1680. void plat_video_wait_vsync(void)
  1681. {
  1682. }
  1683. void plat_video_clear_status(void)
  1684. {
  1685. clear_stat_cnt = 3; // do it thrice in case of triple buffering
  1686. }
  1687. void plat_video_clear_buffers(void)
  1688. {
  1689. if (plat_sdl_overlay != NULL || plat_sdl_gl_active)
  1690. memset(shadow_fb, 0, plat_sdl_screen->w*plat_sdl_screen->h * 2);
  1691. else {
  1692. memset(g_screen_ptr, 0, plat_sdl_screen->w*plat_sdl_screen->h * 2);
  1693. clear_buf_cnt = 3; // do it thrice in case of triple buffering
  1694. }
  1695. }
  1696. void plat_video_menu_enter(int is_rom_loaded)
  1697. {
  1698. if (SDL_MUSTLOCK(plat_sdl_screen))
  1699. SDL_UnlockSurface(plat_sdl_screen);
  1700. plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 1);
  1701. g_screen_ptr = shadow_fb;
  1702. plat_video_set_buffer(g_screen_ptr);
  1703. }
  1704. void plat_video_menu_begin(void)
  1705. {
  1706. if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
  1707. g_menuscreen_ptr = shadow_fb;
  1708. }
  1709. else {
  1710. if (SDL_MUSTLOCK(plat_sdl_screen))
  1711. SDL_LockSurface(plat_sdl_screen);
  1712. g_menuscreen_ptr = plat_sdl_screen->pixels;
  1713. }
  1714. }
  1715. void plat_video_menu_end(void)
  1716. {
  1717. if (plat_sdl_overlay != NULL) {
  1718. SDL_Rect dstrect =
  1719. { 0, 0, plat_sdl_screen->w, plat_sdl_screen->h };
  1720. SDL_LockYUVOverlay(plat_sdl_overlay);
  1721. rgb565_to_uyvy(plat_sdl_overlay->pixels[0], shadow_fb,
  1722. g_menuscreen_w, g_menuscreen_h, g_menuscreen_pp, 0);
  1723. SDL_UnlockYUVOverlay(plat_sdl_overlay);
  1724. SDL_DisplayYUVOverlay(plat_sdl_overlay, &dstrect);
  1725. }
  1726. else if (plat_sdl_gl_active) {
  1727. gl_flip(g_menuscreen_ptr, g_menuscreen_pp, g_menuscreen_h);
  1728. }
  1729. else {
  1730. if (SDL_MUSTLOCK(plat_sdl_screen))
  1731. SDL_UnlockSurface(plat_sdl_screen);
  1732. flip_NNOptimized_LeftAndRightBilinear(plat_sdl_screen, virtual_hw_screen, RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL);
  1733. memcpy(hw_screen->pixels, virtual_hw_screen->pixels, hw_screen->w*hw_screen->h*sizeof(uint16_t));
  1734. SDL_Flip(hw_screen);
  1735. //SDL_Rotate_270(hw_screen, virtual_hw_screen);
  1736. //SDL_Flip(plat_sdl_screen);
  1737. }
  1738. g_menuscreen_ptr = NULL;
  1739. }
  1740. void plat_video_menu_leave(void)
  1741. {
  1742. }
  1743. void plat_video_loop_prepare(void)
  1744. {
  1745. // take over any new vout settings
  1746. plat_sdl_change_video_mode(g_menuscreen_w, g_menuscreen_h, 0);
  1747. // switch over to scaled output if available, but keep the aspect ratio
  1748. if (plat_sdl_overlay != NULL || plat_sdl_gl_active) {
  1749. g_screen_width = (240 * g_menuscreen_w / g_menuscreen_h) & ~1;
  1750. g_screen_height = 240;
  1751. g_screen_ppitch = g_screen_width;
  1752. plat_sdl_change_video_mode(g_screen_width, g_screen_height, 0);
  1753. g_screen_ptr = shadow_fb;
  1754. }
  1755. else {
  1756. g_screen_width = g_menuscreen_w;
  1757. g_screen_height = g_menuscreen_h;
  1758. g_screen_ppitch = g_menuscreen_pp;
  1759. if (SDL_MUSTLOCK(plat_sdl_screen))
  1760. SDL_LockSurface(plat_sdl_screen);
  1761. g_screen_ptr = plat_sdl_screen->pixels;
  1762. }
  1763. plat_video_set_buffer(g_screen_ptr);
  1764. plat_video_set_size(g_screen_width, g_screen_height);
  1765. }
  1766. void plat_early_init(void)
  1767. {
  1768. }
  1769. static void plat_sdl_quit(void)
  1770. {
  1771. // for now..
  1772. engineState = PGS_Quit;
  1773. //exit(1);
  1774. }
  1775. void plat_init(void)
  1776. {
  1777. int shadow_size;
  1778. int ret;
  1779. ret = plat_sdl_init();
  1780. if (ret != 0)
  1781. exit(1);
  1782. SDL_ShowCursor(0);
  1783. #if defined(__RG350__) || defined(__GCW0__) || defined(__OPENDINGUX__)
  1784. // opendingux on JZ47x0 may falsely report a HW overlay, fix to window
  1785. plat_target.vout_method = 0;
  1786. #endif
  1787. if(TTF_Init())
  1788. {
  1789. fprintf(stderr, "Error TTF_Init: %s\n", TTF_GetError());
  1790. exit(EXIT_FAILURE);
  1791. }
  1792. hw_screen = SDL_SetVideoMode(RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL, 16, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF);
  1793. if(hw_screen == NULL)
  1794. {
  1795. fprintf(stderr, "Error SDL_SetVideoMode: %s\n", SDL_GetError());
  1796. exit(EXIT_FAILURE);
  1797. }
  1798. plat_sdl_quit_cb = plat_sdl_quit;
  1799. SDL_WM_SetCaption("PicoDrive " VERSION, NULL);
  1800. virtual_hw_screen = SDL_CreateRGBSurface(SDL_SWSURFACE,
  1801. RES_HW_SCREEN_HORIZONTAL, RES_HW_SCREEN_VERTICAL, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
  1802. if (virtual_hw_screen == NULL) {
  1803. fprintf(stderr, "virtual_hw_screen failed: %s\n", SDL_GetError());
  1804. }
  1805. sms_game_screen = SDL_CreateRGBSurface(SDL_SWSURFACE,
  1806. 256, 192, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
  1807. if (sms_game_screen == NULL) {
  1808. fprintf(stderr, "sms_game_screen failed: %s\n", SDL_GetError());
  1809. }
  1810. gg_game_screen = SDL_CreateRGBSurface(SDL_SWSURFACE,
  1811. 160, 144, 16, 0xFFFF, 0xFFFF, 0xFFFF, 0);
  1812. if (gg_game_screen == NULL) {
  1813. fprintf(stderr, "gg_game_screen failed: %s\n", SDL_GetError());
  1814. }
  1815. g_menuscreen_w = plat_sdl_screen->w;
  1816. g_menuscreen_h = plat_sdl_screen->h;
  1817. g_menuscreen_pp = g_menuscreen_w;
  1818. g_menuscreen_ptr = NULL;
  1819. shadow_size = g_menuscreen_w * g_menuscreen_h * 2;
  1820. if (shadow_size < 320 * 480 * 2)
  1821. shadow_size = 320 * 480 * 2;
  1822. shadow_fb = calloc(1, shadow_size);
  1823. g_menubg_ptr = calloc(1, shadow_size);
  1824. if (shadow_fb == NULL || g_menubg_ptr == NULL) {
  1825. fprintf(stderr, "OOM\n");
  1826. exit(1);
  1827. }
  1828. g_screen_width = 320;
  1829. g_screen_height = 240;
  1830. g_screen_ppitch = 320;
  1831. g_screen_ptr = shadow_fb;
  1832. in_sdl_platform_data.kmap_size = in_sdl_key_map_sz,
  1833. in_sdl_platform_data.jmap_size = in_sdl_joy_map_sz,
  1834. in_sdl_platform_data.key_names = *in_sdl_key_names,
  1835. /** Done later depending on SMS or genesis */
  1836. /*in_sdl_init(&in_sdl_platform_data, plat_sdl_event_handler);
  1837. in_probe();*/
  1838. init_menu_SDL();
  1839. bgr_to_uyvy_init();
  1840. }
  1841. void plat_set_sms_input(void){
  1842. in_sdl_init(&in_sdl_platform_data_SMS, plat_sdl_event_handler);
  1843. in_probe();
  1844. }
  1845. void plat_set_genesis_input(void){
  1846. in_sdl_init(&in_sdl_platform_data, plat_sdl_event_handler);
  1847. in_probe();
  1848. }
  1849. void plat_finish(void)
  1850. {
  1851. SDL_FreeSurface(virtual_hw_screen);
  1852. SDL_FreeSurface(sms_game_screen);
  1853. SDL_FreeSurface(gg_game_screen);
  1854. deinit_menu_SDL();
  1855. free(shadow_fb);
  1856. shadow_fb = NULL;
  1857. free(g_menubg_ptr);
  1858. g_menubg_ptr = NULL;
  1859. TTF_Quit();
  1860. plat_sdl_finish();
  1861. }