plat_sdl.c 68 KB

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