Compare commits

...
276 Commits
Author SHA1 Message Date
Themaister 5bc9ca216e Clean up shader compilation slightly. 2011-06-02 13:49:22 +02:00
Themaister 838a85a719 Could make coordinate flipping consistent ... Finally. 2011-06-01 12:19:48 +02:00
Themaister b880039541 Clarify more ... 2011-05-31 23:08:02 +02:00
Themaister c646166fe1 Make an "empty" buffer for performance. Also attempt to fall back if
vsync fails on start.
2011-05-31 23:03:01 +02:00
Themaister 44c52ca4f8 Add -pedantic for shits 'n giggles.
Fix portability issues with void* -> void (*)(void) casts.
Use a more ... bizarre technique ;)
2011-05-31 15:03:59 +02:00
Themaister 9c9eee5d01 Should fix zip loading of [!] files... 2011-05-31 14:29:32 +02:00
Themaister 7bbaa6ee31 Add pixel size uniform. 2011-05-30 14:32:31 +02:00
Themaister f24aed9358 (addr) is redundant... 2011-05-30 00:04:26 +02:00
Themaister 02344f9048 Bitmasking. 2011-05-29 23:58:04 +02:00
Themaister 200d2b598b Slight cleanup. 2011-05-28 15:06:46 +02:00
Themaister 3213bb13ea Fix color conversion for XVideo. 2011-05-28 14:37:16 +02:00
Themaister 53fd0b3751 Fix up color conversion for SDL. 2011-05-28 14:33:23 +02:00
Themaister 9b6e6a1215 Goddamnit, it was ARGB after all o.O weird. 2011-05-28 14:11:37 +02:00
Themaister e5a9d78b70 Fixed typo. 2011-05-28 11:29:40 +02:00
Themaister f22e964cc0 Err ... 32-bit color was RGBA, not ARGB. 2011-05-27 20:38:42 +02:00
Themaister 9fb827d238 Stuff seems to work. 2011-05-27 03:48:59 +02:00
Themaister 24f802023a Start adding tracker stuff to Cg as well. 2011-05-27 02:25:26 +02:00
Themaister ef2b755e17 Merge branch 'master' of github.com:Themaister/SSNES 2011-05-26 23:56:28 +02:00
Themaister 7cc8c01a1e Fix slight bug. Also add missing scale_type_x/y. 2011-05-26 23:55:46 +02:00
Themaister ca509c5b9f Fix slight bug. 2011-05-26 23:55:27 +02:00
Themaister 12a39e160b Should fix weird viewport bug with some FBO shaders. 2011-05-26 22:25:30 +02:00
Themaister 7c5f36be61 Fix build when config file support isn't compiled in. 2011-05-26 20:44:12 +02:00
Themaister aec44b093e Add new transition_previous. 2011-05-26 16:23:11 +02:00
Themaister ee83c31183 Reorder set-param and texture upload. 2011-05-26 12:53:24 +02:00
Themaister f2ad94f1c3 Finally figure it all out ... Damn. 2011-05-26 12:34:14 +02:00
Themaister 3aed471d57 This bug is driving me nuts. It cannot possibly be my bug ... 2011-05-26 02:03:38 +02:00
Themaister 3ee19ec75f Found something ... unsettling. 2011-05-26 00:23:36 +02:00
Themaister b0d1a98cd1 Fix when too many variables are used. 2011-05-25 20:55:38 +02:00
Themaister 94ff67ee15 Stuff seems to work :) 2011-05-25 17:31:07 +02:00
Themaister 8d2a7e4593 More work on state monitor ... compiles. 2011-05-25 15:58:12 +02:00
Themaister 4e7a003497 Start adding snes state tracker uniforms. 2011-05-25 15:15:20 +02:00
Themaister 380384654c Add hold bind to joyconfig. 2011-05-24 11:56:55 +02:00
Themaister 2144ef84fd Add fast-forward (hold) mode.
Also purge default player 2-5 binds ...
2011-05-24 02:31:21 +02:00
Themaister 83d913b6d2 Add possibility to build Imlib on win32/64. 2011-05-24 01:28:00 +02:00
Themaister 2782c9da91 Ifdef CG debugging. 2011-05-23 21:43:08 +02:00
Themaister 2f539cd326 Seems to work in Cg as well ... :) 2011-05-23 21:04:25 +02:00
Themaister f4af518fb4 Yay ... <_< 2011-05-23 20:08:16 +02:00
Themaister eebe0ae5d0 Getting there ... But passN textures aren't set properly ;x 2011-05-23 19:57:52 +02:00
Themaister f9c5c7c8a5 Path to check initial texture with GLSL. 2011-05-23 17:43:29 +02:00
Themaister 8bc3d3ac5c Add possibility for audio driver to override sample rate. 2011-05-23 14:04:31 +02:00
Themaister 6a3386ad1a Should fix stupid SDL message rendering bug when no font is set. 2011-05-23 11:45:44 +02:00
Themaister 5dced40c98 LUT textures work :) 2011-05-22 18:48:43 +02:00
Themaister 3f58932c2b Seems to work :) Yay! 2011-05-22 17:36:18 +02:00
Themaister 373b9dcc37 Some fixups. 2011-05-22 17:07:47 +02:00
Themaister 993bdfb75f Start working on Cg preset spec. 2011-05-22 17:02:09 +02:00
Themaister 70000607a2 Some EQ-ish stuff for lulz. 2011-05-21 20:09:43 +02:00
Themaister 1b64e4b902 Fix macro for input plugin. 2011-05-20 19:58:11 +02:00
Themaister 2166db91e2 Fix awkward compiler warning. 2011-05-19 22:51:23 +02:00
Themaister 4f50b6a749 Clarify default path in $HOME. 2011-05-19 15:51:30 +02:00
Themaister 676f659306 Fix Makefile to work when mkdir -p is being stupid. 2011-05-19 15:40:39 +02:00
Themaister 2f858b7948 Clean up ugly inline stuff. :\ 2011-05-18 22:40:42 +02:00
Themaister 36d9adbc0f *Shrug* windows. Works at least ... :D 2011-05-18 22:11:34 +02:00
Themaister 9473b392df TGA loader (nolib). 2011-05-18 21:49:23 +02:00
Themaister 3bf050d5aa Use MultiTexCoord1 for image coords. 2011-05-18 20:38:04 +02:00
Themaister 8a0ccc99df Make sure we don't load too many pictures. 2011-05-18 20:27:27 +02:00
Themaister c938f3d8de Add lookup texture support. :D yay. 2011-05-18 20:22:27 +02:00
Themaister d1c7b774f0 Remove default buttons. 2011-05-18 14:07:31 +02:00
Themaister 6b6877afc1 DSP config option to ssnes-joyconfig. 2011-05-17 19:41:57 +02:00
Themaister c370cba5b5 Update config. 2011-05-17 19:37:05 +02:00
Themaister 05d62a93c9 Update gitignore. 2011-05-17 19:34:57 +02:00
Themaister 24cb2e6844 Update filter a bit. Add a Makefile. 2011-05-17 19:34:24 +02:00
Themaister 7f1cd62c8a Update DSP API for config callback.
Clean up API a bit to have separate
SSNES_*_API_VERSION defines.
2011-05-17 19:20:41 +02:00
Themaister 5f5013871a Should fix tilde/grave key. 2011-05-17 18:52:00 +02:00
Themaister aebe6bcd23 Add example SSE3-optimized IIR filter. 2011-05-17 02:45:10 +02:00
Themaister ac21fb81e2 Prepare for 0.5 release. 2011-05-15 22:18:36 +02:00
Themaister 74afe7f95b Win32 doesn't like %g in strftime ... 2011-05-15 22:01:24 +02:00
Themaister c8161a31d6 Everything implemented. 2011-05-15 21:57:16 +02:00
Themaister 628217c6ae Fix up screenshot formatting. 2011-05-15 17:30:00 +02:00
Themaister efed98d5b6 Yay, works :) 2011-05-15 17:16:29 +02:00
Themaister 29addd605e Start adding screenshot support. 2011-05-15 16:54:43 +02:00
Themaister 0c121571b3 Update TODO. 2011-05-15 16:17:57 +02:00
Themaister 6244671b04 Small API cleanup. 2011-05-15 15:57:47 +02:00
Themaister 21b046cdd1 Add more binds to X11 input. 2011-05-15 15:54:46 +02:00
Themaister b32224d406 Check for API mismatch. 2011-05-15 01:49:37 +02:00
Themaister 5d5865cfd8 Add external API for audio driver. 2011-05-15 01:46:11 +02:00
Themaister 4a78f2fef1 Add a clarifying comment. 2011-05-14 20:53:38 +02:00
Themaister b521becd3c Fix up external video lib a bit. 2011-05-14 20:52:35 +02:00
Themaister f10ed12b74 Fix doc bug. 2011-05-13 22:05:16 +02:00
Themaister 8d051ab413 Fix up case where plugin resamples. 2011-05-13 21:43:32 +02:00
Themaister 4cc17a74f7 Clean up error handling a bit. 2011-05-13 21:22:37 +02:00
Themaister 5ae4235545 Some fixes. 2011-05-13 21:19:05 +02:00
Themaister 8ceb8225ad DSP plugin interface, weeee :D 2011-05-13 21:05:28 +02:00
Themaister 20a3087d1c Make input_scale more clear. 2011-05-13 19:42:37 +02:00
Themaister 1f45dc512b 80-cols comments. 2011-05-12 01:30:30 +02:00
Themaister 13dee59fc8 Fix some more ... :v 2011-05-12 00:35:29 +02:00
Themaister 5f78063449 Fix up call conventions, etc. 2011-05-12 00:34:08 +02:00
Themaister ea6bd8c5c5 Fix up Win32 build a bit, and a crash. :D 2011-05-11 22:53:54 +02:00
Themaister bdc85f2def Fix up Win32 build. 2011-05-11 21:37:50 +02:00
Themaister efa731fc6d Fix inconsistency with ext input. 2011-05-11 21:12:14 +02:00
Themaister 80d50b046d Fix crash. 2011-05-11 18:47:36 +02:00
Themaister 1855a7ea0d Add driver stuff. 2011-05-11 18:40:03 +02:00
Themaister f5c11071be Start hooking up input driver. 2011-05-11 18:26:00 +02:00
Themaister 4b9e46ff06 Update config. 2011-05-11 17:57:31 +02:00
Themaister 75d618140b Some more work on external API. 2011-05-11 17:52:16 +02:00
Themaister 59c6de69b0 Frame counter for shaders. Not in spec currently. 2011-05-11 13:24:59 +02:00
Themaister dc20c58189 Update path. 2011-05-11 12:19:33 +02:00
Themaister c6eecf706a Add Win64 Makefile. 2011-05-11 12:13:55 +02:00
Themaister f2ee22d259 Start working on video interface. 2011-05-05 20:23:08 +02:00
Themaister b48322222a Add overscan cropping. 2011-05-05 14:13:12 +02:00
Themaister bbf5a1ee72 Should fix aspect ratio calculation. 2011-05-05 11:38:57 +02:00
Themaister c0851877bf Update TODO. 2011-05-05 00:13:51 +02:00
Themaister 51d96676af Update TODO list. 2011-05-04 22:26:37 +02:00
Themaister 9f3b86e893 Update joyconfig. 2011-05-04 22:25:27 +02:00
Themaister ac85037fc1 And some more ... 2011-05-04 22:16:25 +02:00
Themaister fa8e948e3e Some additional keybinds. 2011-05-04 22:10:17 +02:00
Themaister 252cce07a6 Update TODO again. 2011-05-01 23:41:39 +02:00
Themaister 17581106ba Update TODO. 2011-05-01 17:10:26 +02:00
Themaister 1bd8424fb4 A TODO list to keep track of things. 2011-05-01 17:05:49 +02:00
Themaister 23db525296 Fix nasty cheat handling bug. 2011-04-29 12:36:28 +02:00
Themaister d679f8b24f v0.4.1 updates. 2011-04-26 19:00:50 +02:00
Themaister d2f2ab6a9d Windowed width with xscale depends on aspect ratio. 2011-04-24 02:44:02 +02:00
Themaister 3e1e08ca9a Variadic shifts. Optimizes the path when format matches. 2011-04-23 19:15:15 +02:00
Themaister bb93f20be9 Hopefully make conversions more portable ... 2011-04-23 18:33:56 +02:00
Themaister 54dc289cde SDL fixes. 2011-04-23 18:06:48 +02:00
Themaister fcebad2589 FreeConsole() to avoid ugly console. 2011-04-23 14:47:50 +02:00
Themaister 4b60f98603 Might fix some odd warnings. 2011-04-22 04:27:53 +02:00
Themaister d304960c21 0.4.1-beta I suppose. 2011-04-22 03:17:31 +02:00
Themaister 8da13b716a Simple font rendering with SDL. 2011-04-22 03:13:09 +02:00
Themaister a6c08a24d7 Fix 32-bit 2011-04-21 14:30:54 +02:00
Themaister 6713272809 Hrm... Seems to work, except for 32-bit -> 32-bit <_< 2011-04-21 13:12:45 +02:00
Themaister 8d7cf23b40 Meh, SDL performs like shit :p 2011-04-21 03:39:03 +02:00
Themaister d317a9773f Start on SDL video driver. 2011-04-21 03:23:44 +02:00
Themaister e8806c48c9 Default keybinds and stuff. 2011-04-17 16:55:31 +02:00
Themaister abd7bab43a Cheat support... Somewhat. 2011-04-17 16:53:19 +02:00
Themaister 66464c2cce Parsing seems to work at least. 2011-04-17 15:58:18 +02:00
Themaister 092fa9d531 Get initial cheat support down. 2011-04-17 13:30:59 +02:00
Themaister 1005d7fa9b SHA256 support. Now onto cheats ... 2011-04-17 12:29:58 +02:00
Themaister 079549cbd2 Fix an edge case with strlcat(). 2011-04-11 21:28:03 +02:00
Themaister 0f0a4d0a5e Fix fullscreen state flickering with XVideo. 2011-04-09 13:15:14 +02:00
Themaister f0bbeb901d Fix some error handling for XVideo. 2011-04-08 20:53:11 +02:00
Themaister 8027a25016 Fix include issue in windows. 2011-04-07 21:31:58 +02:00
Themaister a1426c96f8 Update for Win32. 2011-04-07 21:26:02 +02:00
Themaister a4c901df57 Bit more logging when saving SRAM to be sure. 0.4! :D 2011-04-07 21:20:11 +02:00
Themaister f2cc19d1a7 Some fixups for SGB etc. 2011-04-07 21:06:31 +02:00
Themaister ad55829890 Convert to strlcpy/strlcat. More sane. 2011-04-03 22:29:36 +02:00
Themaister 6b8eea6363 Start with strlcpy/strlcat ... 2011-04-03 22:16:59 +02:00
Themaister b2d214ee79 Should fix path issues with UPS patching.
When filename had two dots ('.') in it,
using fill_pathname on g_extern.basename was wrong.
Added fill_pathname_noext to countermeasure this.
2011-04-03 21:43:25 +02:00
Themaister f5fb113563 Should fix case where we have 1 pass and scale attributes. 2011-03-30 23:09:29 +02:00
Themaister 844a523706 Should fix error with scaling. 2011-03-30 14:57:45 +02:00
Themaister d05379b340 Should fix FBO issue. 2011-03-30 11:12:27 +02:00
Themaister 006948b973 Warmup for 0.4-beta. Some FBO stuff is still borked. 2011-03-30 11:05:56 +02:00
Themaister e5196026a1 Add more precise error messages. 2011-03-30 00:10:16 +02:00
Themaister 66d18e4ad7 Adhere more closely to XML shader specs. 2011-03-29 23:45:10 +02:00
Themaister b539df1ccd Added settings to ssnes-joyconfig. 2011-03-29 23:40:12 +02:00
Themaister c0117bfbb6 Remove debugging fprintf. 2011-03-29 23:38:11 +02:00
Themaister 074234f182 Oh damn you Windows and your spoony file handling... 2011-03-29 23:21:56 +02:00
Themaister bab03ba8c5 No need for these. 2011-03-29 19:27:53 +02:00
Themaister 4878a283ed Properly deinit Cg. 2011-03-29 19:18:06 +02:00
Themaister d3eed4f8c4 Properly ifdef. 2011-03-29 19:15:02 +02:00
Themaister 647db4d5de Hrm. 2011-03-29 19:13:14 +02:00
Themaister 3455f83fa7 Hopefully fixes awkward issue with scale not being applied correctly. 2011-03-29 19:09:10 +02:00
Themaister f730ba4043 Oh yeah :) Can toggle shader in run-time! 2011-03-29 18:59:06 +02:00
Themaister caf87b1198 Font handling and stuff. 2011-03-29 18:28:31 +02:00
Themaister 1706763a4c Start supporting shaders hiding in a directory. 2011-03-29 18:04:41 +02:00
Themaister c1b8cdb35b Fix issue with size attribute first. 2011-03-27 21:34:33 +02:00
Themaister 936b7f5395 Seems to work now. 2011-03-27 21:21:46 +02:00
Themaister a01e3347f7 Seems to work :) 2011-03-27 20:54:14 +02:00
Themaister 142411c08c Fix some garbage. 2011-03-27 20:33:14 +02:00
Themaister 29808c4421 Start on outscale/size support. 2011-03-27 20:29:47 +02:00
Themaister 48dcd174a7 Add resetting support. (About time, ey?! :D) 2011-03-24 20:41:28 +01:00
Themaister 6c85e6e702 Configurable FBO support. 2011-03-23 23:48:13 +01:00
Themaister e484e22e2f Fix up UPS :) Should work fine now at least for .sfc base roms. 2011-03-23 23:31:33 +01:00
Themaister 8ef54de812 Start adding UPS support. 2011-03-23 12:18:24 +01:00
Themaister c1d5a6e140 Fixes scaling bug for shaders. 2011-03-23 11:05:18 +01:00
Themaister ea153f9df2 Install ssnes-zip 2011-03-20 22:48:13 +01:00
Themaister 1ba9854070 Typo 2011-03-20 21:46:34 +01:00
Themaister 012e955a05 RAR. Untested. 2011-03-20 19:46:04 +01:00
Themaister 37c0b49a99 7z 2011-03-20 19:16:22 +01:00
Themaister de30ce9ef5 Start on a zip loader wrapper. 2011-03-20 13:40:01 +01:00
Themaister 027a21278e Whoops 2011-03-20 02:13:44 +01:00
Themaister f77fe62ab0 Should fix viewport bug when rendering fonts. 2011-03-20 02:10:02 +01:00
Themaister d42c35e1aa Update makefile for win32. 2011-03-19 20:42:49 +01:00
Themaister d5761b6385 Can disable netplay. 2011-03-19 20:41:07 +01:00
Themaister bb3ab3b49c Lots of cleanups for XVideo.
Render video @ 2x scale to combat chroma downsampling. Fix 32-bit
rendering.
2011-03-19 18:53:37 +01:00
Themaister 0441725045 XSync. 2011-03-19 17:19:51 +01:00
Themaister ac4e6e89a4 Bigger buffer for gfx_window_title. 2011-03-19 16:54:29 +01:00
Themaister 12d21cf7a6 Make title handling more sane. 2011-03-26 18:34:58 +01:00
Themaister 3fda3effda Fix crash in XVideo font rendering. 2011-03-26 18:04:18 +01:00
Themaister 7e07b821aa Forgot Makefile.win32 2011-03-17 22:29:43 +01:00
Themaister e502a8b566 Updates for 0.3.2 2011-03-17 22:28:44 +01:00
Themaister ec51ceb010 Add usage of snes_library_id(). 2011-03-17 01:25:44 +01:00
Themaister 3fa037daee Fix minor issues. 2011-03-16 21:48:44 +01:00
Themaister 46c6955cfe Fix odd viewport problem with shaders. 2011-03-16 12:47:31 +01:00
Themaister 252425c35c Some cleanups. 2011-03-16 12:28:02 +01:00
Themaister d309d472e1 Code cleanup. Accumulate scale values. 2011-03-15 18:14:01 +01:00
Themaister 898c4e8c6c Works quite decently now. :) 2011-03-14 23:59:31 +01:00
Themaister 56ac7b4649 Something's working :D 2011-03-14 23:48:19 +01:00
Themaister 9dd05b7101 New params, but is broken for now. 2011-03-14 23:20:51 +01:00
Themaister e2daab9ce9 Fetch some scale values (this is not in the standard (yet!)) 2011-03-14 22:51:03 +01:00
Themaister ef19ea7a85 Add fbo structures, ready to implement. :v 2011-03-14 22:09:35 +01:00
Themaister 01cf24f15f Add filter attributes. 2011-03-14 21:28:30 +01:00
Themaister f870080497 Render fonts. Very dirty. :( 2011-03-13 20:52:06 +01:00
Themaister c5942ec5a2 Fix Jack pkgconf version. 2011-03-13 18:09:27 +01:00
Themaister 50d78fb804 Endian fixes 2011-03-13 15:49:00 +01:00
Themaister 8053aca3c3 Render 32-bit 2011-03-13 15:28:57 +01:00
Themaister 0f3171edea Title 2011-03-13 15:11:53 +01:00
Themaister 6b478b9dda Fullscreen :D 2011-03-13 14:46:27 +01:00
Themaister caaa9cea40 Hide cursor 2011-03-13 14:26:22 +01:00
Themaister ea7f8f5552 Add proper close handling. 2011-03-13 14:12:43 +01:00
Themaister 69caca6017 Fixes for resize. 2011-03-13 13:11:33 +01:00
Themaister 703cfadf63 Fix up X input. 2011-03-13 12:02:06 +01:00
Themaister adfd461c3e Start on XVideo 2011-03-13 04:51:09 +01:00
Themaister bb79dbaa98 Getting ready for an eventual XVideo driver. 2011-03-13 00:44:49 +01:00
Themaister 394de09907 Should hopefully fix flickering :v 2011-03-12 19:28:56 +01:00
Themaister 560cafccaf Futile attempt to try and fix font flickering. 2011-03-12 19:09:25 +01:00
Themaister 0b09032164 Hrm 2011-03-12 18:42:59 +01:00
Themaister 1ee7162784 It's not copypasta anymore :D 2011-03-12 17:01:57 +01:00
Themaister 4949a131f8 Multi-passing in XML should work. 2011-03-12 16:33:01 +01:00
Themaister 8b241c3deb Preparation for new XML shader specs. 2011-03-12 15:30:57 +01:00
Themaister 78c457b996 Update Roar driver. 2011-03-11 19:23:32 +01:00
Themaister 0be1b0b88b More build fixes. 2011-03-07 20:22:55 +01:00
Themaister 3ef0276b2a Will probably not need, but hey. 2011-03-07 20:17:27 +01:00
Themaister 42d19f1a35 Fix build for Win32. 2011-03-07 20:05:03 +01:00
Themaister 1cf9381cb0 Fix colormaps and stuff. 2011-03-07 19:56:40 +01:00
Themaister f94dc125e5 An initial take on bsnes CPU filters. 2011-03-07 19:12:14 +01:00
Themaister 96b978d55a Start on modular CPU filters. Abandon the old CPU filter stuff. 2011-03-07 17:22:03 +01:00
Themaister ec367d333d Add error message when FBO fails. 2011-03-07 00:03:10 +01:00
Themaister 9535097894 Fix build for Win32. 2011-03-06 22:18:25 +01:00
Themaister b541c27226 FBO seems to work! :D 2011-03-06 19:56:35 +01:00
Themaister 41b2af66d5 Add settings. 2011-03-06 18:19:31 +01:00
Themaister 84b2b9c7a5 Fix up Jack deprecation. 2011-03-06 16:58:33 +01:00
Themaister 52c8d96dca FBOs. Hacky atm, need more flexibility. 2011-03-06 16:52:49 +01:00
Themaister 8c6519a53b Should fix an awkward bug with Quickbuild. 2011-03-03 22:06:58 +01:00
Themaister 336adb9f85 Preparations for 0.3.1 2011-03-01 21:45:04 +01:00
Themaister 7053006e77 Add more verbose messages, should fix netplay hangup on Win32. 2011-03-01 14:46:58 +01:00
Themaister bbf4804d12 Obey CFLAGS and LDFLAGS when testing libs (nice when profiling.) 2011-02-28 22:30:06 +01:00
Themaister 7c16424ba3 Shader enabling, and automatic resolution detection. 2011-02-28 16:59:31 +01:00
Themaister de9dc686ac Add hicontrast. 2011-02-27 20:42:02 +01:00
Themaister 069e9b574f Some waterpaint shader :) 2011-02-27 01:16:33 +01:00
Themaister d5319a879a Had missed out pause binding completely. 2011-02-27 00:24:50 +01:00
Themaister f64880aee1 Try glFlush() to remedy occational font flicker... 2011-02-26 23:56:45 +01:00
Themaister fed1857918 Might fix odd font flickering. 2011-02-26 19:32:59 +01:00
Themaister 9aeb40155e Add a comment or two. 2011-02-26 18:49:41 +01:00
Themaister e49aeb1071 Remove redundant define. 2011-02-26 18:48:17 +01:00
Themaister 13f8ec6f3b Added "nul" binds. 2011-02-26 18:47:32 +01:00
Themaister f75c6df270 Should force granularity to 1 when using movies. 2011-02-26 15:09:04 +01:00
Themaister 60b0afce95 Is this it? :D 2011-02-26 01:13:52 +01:00
Themaister f24653768f It does work, somewhat! :D 2011-02-26 00:59:17 +01:00
Themaister f135972a6b Arghh... This is so goddamn annoying :( 2011-02-26 00:31:13 +01:00
Themaister 42c68e21a2 Movie rewind still very broken :( 2011-02-25 12:37:05 +01:00
Themaister e8865060ca A start on movie rewind. 2011-02-25 11:47:27 +01:00
Themaister b07827a856 Should fix GLSL issue on OSX. 2011-02-22 23:53:01 +01:00
Themaister 0735398e64 Updates for 0.3. 2011-02-22 22:24:58 +01:00
Themaister ec2d10e069 Add default path to OSX and check in APPDATA for Win32. 2011-02-22 11:28:28 +01:00
Themaister 4cdb6151e5 Add netplay_client_swap_input. 2011-02-20 12:12:53 +01:00
Themaister e482b1bd18 Fix potential problem. Some cleanup. 2011-02-18 23:51:51 +01:00
Themaister 9be3c13a07 Hrm. This stuff is very error prone. :V 2011-02-18 15:50:57 +01:00
Themaister 8a4bcacf88 hrm... 2011-02-18 14:49:15 +01:00
Themaister 6b9b37b528 Should fix an extreme edge case. 2011-02-18 13:39:31 +01:00
Themaister 8e87c1ff19 Could this be it? :v 2011-02-18 03:42:43 +01:00
Themaister aa648c1a49 Hm... Still borked. Seems hard to fix. 2011-02-18 03:14:32 +01:00
Themaister 056a590671 Hm. More progress, but still not quite right. 2011-02-18 02:16:32 +01:00
Themaister 97f2503794 Some initial work on UDP networking. Still borked. 2011-02-18 02:01:47 +01:00
Themaister 143079efbe Should fix abrupt application termination on Unix when netplay ends. 2011-02-16 18:33:45 +01:00
Themaister 7bcc173c5f Should fix some issues with Win32 netplay. 2011-02-15 17:29:03 +01:00
Themaister 0485f783b3 Updates for Win32. 2011-02-15 16:42:55 +01:00
Themaister f3c07325bd Fix Windows build. 2011-02-15 15:57:59 +01:00
Themaister e8eabb0102 Update docs. 2011-02-15 15:42:29 +01:00
Themaister b73f5e77ff Cleanups, add --frames. 2011-02-15 15:32:26 +01:00
Themaister d07712b308 Woot. looks like it works o.O 2011-02-15 15:10:03 +01:00
Themaister 89ba64d27f Borked, borked... 2011-02-14 22:37:57 +01:00
Themaister 1a6f112d8a More changes to netplay. Still quite broken, but it's not far away! 2011-02-14 20:11:46 +01:00
Themaister 70e8681487 Started to work on a look-ahead algorithm.
Still very broken, but frame-by-frame sync works at least :)
2011-02-14 16:10:53 +01:00
Themaister 17ae0f72df Do not try to poll when we've disconnected. 2011-02-13 21:19:20 +01:00
Themaister 466485e62e Lots more error messages. 2011-02-13 20:24:54 +01:00
Themaister f6ee0845f2 Add some message. 2011-02-13 19:50:45 +01:00
Themaister df1eee1a74 Redundant... 2011-02-13 19:00:27 +01:00
Themaister 46b46ba0b1 Update docs a bit. 2011-02-13 18:47:48 +01:00
Themaister a763848644 Fix some issues with message queues. 2011-02-13 18:19:37 +01:00
Themaister 9ba2d6c9fa Send SRAM to client. 2011-02-13 17:45:14 +01:00
Themaister fd1367e257 Update build for Win32 2011-02-13 16:49:18 +01:00
Themaister 85dbc05ded Some start on netplay. :) 2011-02-13 16:40:24 +01:00
Themaister 1eea7786bc Should fix possible error where game can hang for prolonged time on
exit.
2011-02-11 19:54:42 +01:00
Themaister d039bedad4 Fix up some path handling with srm/states. 2011-02-11 14:27:19 +01:00
Themaister ad6719426c Fix up autosaving a bit. 2011-02-11 13:44:31 +01:00
Themaister 719e4942ce Add autosave possibility. 2011-02-10 21:16:59 +01:00
96 changed files with 10084 additions and 4371 deletions

No files matched your search

+2
View File
@@ -1,4 +1,6 @@
*.o
*.so
*.dll
ssnes
/config.h
/config.mk
+42 -19
View File
@@ -2,11 +2,11 @@ include config.mk
TARGET = ssnes tools/ssnes-joyconfig
OBJ = ssnes.o file.o driver.o settings.o dynamic.o message.o rewind.o movie.o
OBJ = ssnes.o file.o driver.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o ups.o strl.o screenshot.o
JOYCONFIG_OBJ = tools/ssnes-joyconfig.o conf/config_file.o
HEADERS = $(wildcard */*.h) $(wildcard *.h)
LIBS =
LIBS = -lm
DEFINES = -DHAVE_CONFIG_H
ifneq ($(findstring Darwin,$(shell uname -a)),)
@@ -27,6 +27,10 @@ ifeq ($(HAVE_CONFIGFILE), 1)
OBJ += conf/config_file.o
endif
ifeq ($(HAVE_NETPLAY), 1)
OBJ += netplay.o
endif
ifeq ($(HAVE_RSOUND), 1)
OBJ += audio/rsound.o
LIBS += -lrsound
@@ -53,7 +57,8 @@ endif
endif
ifeq ($(HAVE_JACK),1)
OBJ += audio/jack.o
LIBS += -ljack
LIBS += $(JACK_LIBS)
DEFINES += $(JACK_CFLAGS)
endif
ifeq ($(HAVE_PULSE), 1)
OBJ += audio/pulse.o
@@ -62,7 +67,7 @@ ifeq ($(HAVE_PULSE), 1)
endif
ifeq ($(HAVE_SDL), 1)
OBJ += gfx/gl.o input/sdl.o audio/sdl.o audio/buffer.o
OBJ += gfx/sdl.o gfx/gl.o input/sdl.o audio/sdl.o audio/buffer.o
DEFINES += $(SDL_CFLAGS)
LIBS += $(SDL_LIBS)
ifeq ($(OSX),1)
@@ -72,23 +77,25 @@ else
endif
endif
ifeq ($(HAVE_XVIDEO), 1)
OBJ += gfx/xvideo.o input/x11_input.o
LIBS += -lXv -lX11
endif
ifeq ($(HAVE_CG), 1)
OBJ += gfx/shader_cg.o
LIBS += -lCg -lCgGL
endif
ifeq ($(HAVE_XML), 1)
OBJ += gfx/shader_glsl.o
OBJ += gfx/shader_glsl.o gfx/image.o gfx/snes_state.o sha256.o cheats.o
LIBS += $(XML_LIBS)
DEFINES += $(XML_CFLAGS)
endif
ifeq ($(HAVE_FILTER), 1)
OBJ += hqflt/hq.o
OBJ += hqflt/grayscale.o
OBJ += hqflt/bleed.o
OBJ += hqflt/ntsc.o
OBJ += hqflt/snes_ntsc/snes_ntsc.o
ifeq ($(HAVE_DYLIB), 1)
OBJ += gfx/ext.o audio/ext.o
LIBS += -ldl
endif
ifeq ($(HAVE_FREETYPE), 1)
@@ -97,6 +104,11 @@ ifeq ($(HAVE_FREETYPE), 1)
DEFINES += $(FREETYPE_CFLAGS)
endif
ifeq ($(HAVE_IMLIB), 1)
LIBS += $(IMLIB_LIBS)
DEFINES += $(IMLIB_CFLAGS)
endif
ifeq ($(HAVE_FFMPEG), 1)
OBJ += record/ffemu.o
LIBS += $(AVCODEC_LIBS) $(AVCORE_LIBS) $(AVFORMAT_LIBS) $(AVUTIL_LIBS) $(SWSCALE_LIBS)
@@ -109,11 +121,20 @@ else
LIBS += $(libsnes)
endif
ifeq ($(HAVE_STRL), 1)
DEFINES += -DHAVE_STRL
endif
ifneq ($(V),1)
Q := @
endif
CFLAGS = -Wall -O3 -g -std=gnu99 -I.
CFLAGS += -Wall -O3 -g -I. -pedantic
ifneq ($(findstring icc,$(CC)),)
CFLAGS += -std=c99
else
CFLAGS += -std=gnu99
endif
all: $(TARGET) config.mk
@@ -134,18 +155,22 @@ tools/ssnes-joyconfig: $(JOYCONFIG_OBJ)
@$(if $(Q), $(shell echo echo CC $<),)
install: $(TARGET)
mkdir -p $(DESTDIR)$(PREFIX)/bin
mkdir -p $(DESTDIR)/etc
mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1
mkdir -p $(DESTDIR)$(PREFIX)/bin 2>/dev/null || /bin/true
mkdir -p $(DESTDIR)/etc 2>/dev/null || /bin/true
mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1 2>/dev/null || /bin/true
install -m755 $(TARGET) $(DESTDIR)$(PREFIX)/bin
install -m644 ssnes.cfg $(DESTDIR)/etc/ssnes.cfg
install -m644 docs/ssnes.1 $(DESTDIR)$(PREFIX)/share/man/man1
install -m644 docs/ssnes-joyconfig.1 $(DESTDIR)$(PREFIX)/share/man/man1
install -m755 ssnes-zip $(DESTDIR)$(PREFIX)/bin
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/{ssnes,ssnes-joyconfig}
rm -f $(DESTDIR)$(PREFIX)/bin/ssnes
rm -f $(DESTDIR)$(PREFIX)/bin/ssnes-joyconfig
rm -f $(DESTDIR)$(PREFIX)/bin/ssnes-zip
rm -f $(DESTDIR)/etc/ssnes.cfg
rm -f $(DESTDIR)$(PREFIX)/share/man/man1/{ssnes,ssnes-joyconfig}.1
rm -f $(DESTDIR)$(PREFIX)/share/man/man1/ssnes.1
rm -f $(DESTDIR)$(PREFIX)/share/man/man1/ssnes-joyconfig.1
clean:
rm -f *.o
@@ -153,8 +178,6 @@ clean:
rm -f conf/*.o
rm -f gfx/*.o
rm -f record/*.o
rm -f hqflt/*.o
rm -f hqflt/snes_ntsc/*.o
rm -f input/*.o
rm -f tools/*.o
rm -f $(TARGET)
+40 -5
View File
@@ -1,6 +1,6 @@
TARGET = ssnes.exe
JTARGET = ssnes-joyconfig.exe
OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o
OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o ups.o strl.o screenshot.o
JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o
CC = gcc
@@ -12,9 +12,14 @@ HAVE_XML = 1
HAVE_FREETYPE = 1
HAVE_XAUDIO = 1
HAVE_RSOUND = 1
HAVE_DYLIB = 1
HAVE_NETPLAY = 1
HAVE_FBO = 1
HAVE_CG = 0
HAVE_IMLIB = 0
libsnes ?= -lsnes
LIBS =
LIBS = -lm
DEFINES = -I. -DHAVE_CONFIGFILE
LDFLAGS = -L. -static-libgcc -s
@@ -30,11 +35,17 @@ else
endif
ifeq ($(HAVE_SDL), 1)
OBJ += gfx/gl.o input/sdl.o audio/sdl.o audio/buffer.o
OBJ += gfx/sdl.o gfx/gl.o input/sdl.o audio/sdl.o audio/buffer.o
LIBS += $(SDL_LIBS) -lopengl32
DEFINES += $(SDL_CFLAGS) -DHAVE_SDL
endif
ifeq ($(HAVE_CG), 1)
OBJ += gfx/shader_cg.o
LIBS += -lcg -lcgGL
DEFINES += -DHAVE_CG
endif
ifeq ($(HAVE_XAUDIO), 1)
OBJ += audio/xaudio.o
DEFINES += -DHAVE_XAUDIO
@@ -47,11 +58,21 @@ ifeq ($(HAVE_RSOUND), 1)
endif
ifeq ($(HAVE_XML), 1)
OBJ += gfx/shader_glsl.o
OBJ += gfx/shader_glsl.o gfx/image.o gfx/snes_state.o sha256.o cheats.o
DEFINES += $(XML_CFLAGS) -DHAVE_XML
LIBS += -lxml2
endif
ifeq ($(HAVE_DYLIB), 1)
DEFINES += -DHAVE_DYLIB
endif
ifeq ($(HAVE_NETPLAY), 1)
DEFINES += -DHAVE_NETPLAY
OBJ += netplay.o
LIBS += -lws2_32
endif
ifeq ($(HAVE_FREETYPE), 1)
OBJ += gfx/fonts.o
DEFINES += -DHAVE_FREETYPE -Ifreetype2
@@ -64,6 +85,20 @@ else
LIBS += $(libsnes)
endif
ifeq ($(HAVE_FBO), 1)
DEFINES += -DHAVE_FBO
endif
ifeq ($(HAVE_DYLIB), 1)
OBJ += gfx/ext.o audio/ext.o
endif
ifeq ($(HAVE_IMLIB), 1)
LIBS += -lImlib2
DEFINES += -DHAVE_IMLIB
endif
ifneq ($(V),1)
Q := @
endif
@@ -99,7 +134,7 @@ clean:
rm -f tools/*.o
dist: all
zip -r ssnes-win32-0.2.2.zip $(TARGET) ssnes.cfg snes.dll libxml2.dll iconv.dll zlib1.dll SDL.dll libsamplerate-0.dll freetype6.dll xaudio-c.dll rsound.dll pthreadGC2.dll README.win32.txt $(JTARGET)
zip -r ssnes-win32-0.5.1.zip $(TARGET) ssnes.cfg snes.dll libxml2.dll iconv.dll zlib1.dll SDL.dll freetype6.dll xaudio-c.dll rsound.dll pthreadGC2.dll README.win32.txt $(JTARGET)
libs:
wget https://github.com/downloads/Themaister/SSNES/SSNES-win32-libs.zip --no-check-certificate
+142
View File
@@ -0,0 +1,142 @@
TARGET = ssnes.exe
JTARGET = ssnes-joyconfig.exe
OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o ups.o strl.o screenshot.o
JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o
CC = gcc
CXX = g++
HAVE_SRC = 0
HAVE_SDL = 1
HAVE_XML = 1
HAVE_FREETYPE = 1
HAVE_XAUDIO = 1
HAVE_RSOUND = 0
HAVE_DYLIB = 1
HAVE_NETPLAY = 1
HAVE_FBO = 1
HAVE_CG = 0
HAVE_IMLIB = 0
libsnes ?= -lsnes
LIBS = -lm
DEFINES = -I. -DHAVE_CONFIGFILE
LDFLAGS = -L. -static-libgcc -s
SRC_LIBS = -lsamplerate-0
SDL_LIBS = -lmingw32 -lSDL -lSDLmain -lwinmm -mwindows -lm -luser32 -lgdi32 -lwinmm -ldxguid
SDL_CFLAGS = -ISDL
ifeq ($(HAVE_SRC), 1)
LIBS += $(SRC_LIBS)
DEFINES += $(SRC_CFLAGS) -DHAVE_SRC
else
OBJ += audio/hermite.o
endif
ifeq ($(HAVE_SDL), 1)
OBJ += gfx/sdl.o gfx/gl.o input/sdl.o audio/sdl.o audio/buffer.o
LIBS += $(SDL_LIBS) -lopengl32
DEFINES += $(SDL_CFLAGS) -DHAVE_SDL
endif
ifeq ($(HAVE_CG), 1)
OBJ += gfx/shader_cg.o
LIBS += -lcg -lcgGL
DEFINES += -DHAVE_CG
endif
ifeq ($(HAVE_XAUDIO), 1)
OBJ += audio/xaudio.o
DEFINES += -DHAVE_XAUDIO
endif
ifeq ($(HAVE_RSOUND), 1)
OBJ += audio/rsound.o
DEFINES += -DHAVE_RSOUND
LIBS += -lrsound
endif
ifeq ($(HAVE_XML), 1)
OBJ += gfx/shader_glsl.o gfx/image.o gfx/snes_state.o sha256.o cheats.o
DEFINES += $(XML_CFLAGS) -DHAVE_XML -DLIBXML_STATIC
LIBS += -lxml2 -lz -lws2_32
endif
ifeq ($(HAVE_DYLIB), 1)
DEFINES += -DHAVE_DYLIB
endif
ifeq ($(HAVE_NETPLAY), 1)
DEFINES += -DHAVE_NETPLAY
OBJ += netplay.o
LIBS += -lws2_32
endif
ifeq ($(HAVE_FREETYPE), 1)
OBJ += gfx/fonts.o
DEFINES += -DHAVE_FREETYPE -Ifreetype2
LIBS += -lz -lfreetype6
endif
ifeq ($(DYNAMIC), 1)
DEFINES += -DHAVE_DYNAMIC
else
LIBS += $(libsnes)
endif
ifeq ($(HAVE_FBO), 1)
DEFINES += -DHAVE_FBO
endif
ifeq ($(HAVE_DYLIB), 1)
OBJ += gfx/ext.o audio/ext.o
endif
ifeq ($(HAVE_IMLIB), 1)
LIBS += -lImlib2
DEFINES += -DHAVE_IMLIB
endif
ifneq ($(V),1)
Q := @
endif
CFLAGS = -Wall -O3 -std=gnu99 -I.
all: $(TARGET) $(JTARGET)
$(TARGET): $(OBJ)
$(Q)$(CXX) -o $@ $(OBJ) $(LIBS) $(LDFLAGS)
@$(if $(Q), $(shell echo echo LD $@),)
%.o: %.c
$(Q)$(CC) $(CFLAGS) $(DEFINES) -c -o $@ $<
@$(if $(Q), $(shell echo echo CC $<),)
$(JTARGET): $(JOBJ)
$(Q)$(CC) -o $@ $(JOBJ) $(SDL_LIBS) $(LDFLAGS)
@$(if $(Q), $(shell echo echo LD $@),)
clean:
rm -f *.o
rm -f audio/*.o
rm -f conf/*.o
rm -f gfx/*.o
rm -f record/*.o
rm -f hqflt/*.o
rm -f input/*.o
rm -f hqflt/snes_ntsc/*.o
rm -f $(TARGET)
rm -f ssnes-joyconfig.exe
rm -f tools/*.o
dist: all
zip -r ssnes-win64-0.5.zip $(TARGET) ssnes.cfg snes.dll xaudio-c.dll README.win32.txt $(JTARGET)
libs:
wget https://github.com/downloads/Themaister/SSNES/ssnes-win64-libs.zip --no-check-certificate
unzip ssnes-win64-libs.zip
.PHONY: all install uninstall clean dist libs
+1 -1
View File
@@ -49,7 +49,7 @@ static bool find_float_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
return false;
}
static void* __alsa_init(const char* device, int rate, int latency)
static void* __alsa_init(const char* device, unsigned rate, unsigned latency)
{
alsa_t *alsa = calloc(1, sizeof(alsa_t));
if ( alsa == NULL )
+160
View File
@@ -0,0 +1,160 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "ext/ssnes_audio.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include "driver.h"
#include "dynamic.h"
#include "general.h"
#include <sys/types.h>
typedef struct audio_ext
{
dylib_t lib;
const ssnes_audio_driver_t *driver;
void *handle;
bool is_float;
} audio_ext_t;
static void audio_ext_free(void *data)
{
audio_ext_t *ext = data;
if (ext)
{
if (ext->driver && ext->handle)
ext->driver->free(ext->handle);
if (ext->lib)
dylib_close(ext->lib);
free(ext);
}
}
static void* audio_ext_init(const char *device, unsigned rate, unsigned latency)
{
if (!(*g_settings.audio.external_driver))
{
SSNES_ERR("Please define an external audio driver.\n");
return NULL;
}
audio_ext_t *ext = calloc(1, sizeof(*ext));
if (!ext)
return NULL;
ext->lib = dylib_load(g_settings.audio.external_driver);
if (!ext->lib)
{
SSNES_ERR("Failed to load external library \"%s\"\n", g_settings.audio.external_driver);
goto error;
}
const ssnes_audio_driver_t* (*plugin_load)(void) = (const ssnes_audio_driver_t* (*)(void))dylib_proc(ext->lib, "ssnes_audio_driver_init");
if (!plugin_load)
{
SSNES_ERR("Failed to find symbol \"ssnes_audio_driver_init\" in plugin.\n");
goto error;
}
ext->driver = plugin_load();
if (!ext->driver)
{
SSNES_ERR("Received invalid driver from plugin.\n");
goto error;
}
SSNES_LOG("Loaded external audio driver: \"%s\"\n", ext->driver->ident ? ext->driver->ident : "Unknown");
if (ext->driver->api_version != SSNES_AUDIO_API_VERSION)
{
SSNES_ERR("API mismatch in external video plugin. SSNES: %d, Plugin: %d ...\n", SSNES_AUDIO_API_VERSION, ext->driver->api_version);
goto error;
}
ssnes_audio_driver_info_t info = {
.device = device,
.sample_rate = rate,
.latency = latency
};
ext->handle = ext->driver->init(&info);
if (!ext->handle)
{
SSNES_ERR("Failed to init audio driver.\n");
goto error;
}
if (ext->driver->sample_rate)
g_settings.audio.out_rate = ext->driver->sample_rate(ext->handle);
return ext;
error:
audio_ext_free(ext);
return NULL;
}
static ssize_t audio_ext_write(void *data, const void *buf, size_t size)
{
audio_ext_t *ext = data;
unsigned frame_size = ext->is_float ? (2 * sizeof(float)) : (2 * sizeof(int16_t));
size /= frame_size;
int ret = ext->driver->write(ext->handle, buf, size);
if (ret < 0)
return -1;
return ret * frame_size;
}
static bool audio_ext_start(void *data)
{
audio_ext_t *ext = data;
return ext->driver->start(ext->handle);
}
static bool audio_ext_stop(void *data)
{
audio_ext_t *ext = data;
return ext->driver->stop(ext->handle);
}
static void audio_ext_set_nonblock_state(void *data, bool toggle)
{
audio_ext_t *ext = data;
ext->driver->set_nonblock_state(ext->handle, toggle);
}
static bool audio_ext_use_float(void *data)
{
audio_ext_t *ext = data;
ext->is_float = ext->driver->use_float(ext->handle);
return ext->is_float;
}
const audio_driver_t audio_ext = {
.init = audio_ext_init,
.write = audio_ext_write,
.stop = audio_ext_stop,
.start = audio_ext_start,
.set_nonblock_state = audio_ext_set_nonblock_state,
.use_float = audio_ext_use_float,
.free = audio_ext_free,
.ident = "ext"
};
+16
View File
@@ -0,0 +1,16 @@
TARGET = ssnes_iir_filter.so
OBJ = ssnes_iir_filter.o
all: $(TARGET)
$(TARGET): $(OBJ)
$(CC) -o $@ -shared $(OBJ) -lm
%.o: %.c
$(CC) -c -o $@ $< -g -std=c99 -fPIC -O3 -march=native $(CFLAGS)
clean:
rm -f $(TARGET)
rm -f $(OBJ)
.PHONY: clean
+16
View File
@@ -0,0 +1,16 @@
TARGET = iir_filter
OBJ = ssnes_iir_filter.o
all: $(TARGET)
$(TARGET): $(OBJ)
$(CC) -o $@ $(OBJ) -lm
%.o: %.c
$(CC) -c -o $@ $< -g -std=c99 -O3 -march=native $(CFLAGS) -DTEST -DLOG
clean:
rm -f $(TARGET)
rm -f $(OBJ)
.PHONY: clean
+128
View File
@@ -0,0 +1,128 @@
/////
// API header for external SSNES audio driver plugins.
//
//
#ifndef __SSNES_AUDIO_DRIVER_PLUGIN_H
#define __SSNES_AUDIO_DRIVER_PLUGIN_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#ifdef SSNES_DLL_IMPORT
#define SSNES_API_EXPORT __declspec(dllimport)
#else
#define SSNES_API_EXPORT __declspec(dllexport)
#endif
#define SSNES_API_CALLTYPE __cdecl
#else
#define SSNES_API_EXPORT
#define SSNES_API_CALLTYPE
#endif
#ifndef SSNES_TRUE
#define SSNES_TRUE 1
#endif
#ifndef SSNES_FALSE
#define SSNES_FALSE 0
#endif
#ifndef SSNES_OK
#define SSNES_OK 1
#endif
#ifndef SSNES_ERROR
#define SSNES_ERROR 0
#endif
#define SSNES_AUDIO_API_VERSION 2
typedef struct ssnes_audio_driver_info
{
// A hint for a subdevice of the audio driver.
// This is driver independent, and not relevant for all
// audio drivers. I.e. ALSA driver might use "hw:0",
// OSS "/dev/audio", etc.
const char *device;
// Audio sample rate.
unsigned sample_rate;
// Maximum audio latency requested for output,
// measured in milliseconds.
// If driver is not able to provide this latency, it can
// be disregarded.
unsigned latency;
} ssnes_audio_driver_info_t;
typedef struct ssnes_audio_driver
{
// Initializes the device.
void* (*init)(const ssnes_audio_driver_info_t *info);
// Write data in buffer to audio driver.
// A frame here is defined as one combined sample of left and right
// channels. (I.e. 44.1kHz, 16-bit stereo has 88.2k samples/s, and
// 44.1k frames/s.)
//
// Samples are interleaved in format LRLRLRLRLR ...
// If the driver returns true in use_float(), a floating point
// format will be used, with range [-1.0, 1.0].
// If not, signed 16-bit samples in native byte ordering will be used.
//
// This function returns the number of frames successfully written.
// If an error occurs, -1 should be returned.
// Note that non-blocking behavior that cannot write at this time
// should return 0 as returning -1 will terminate the driver.
//
// Unless said otherwise with set_nonblock_state(), all writes
// are blocking, and it should block till it has written all frames.
int (*write)(void *data, const void *buffer, unsigned frames);
// Temporarily pauses the audio driver.
int (*stop)(void *data);
// Resumes audio driver from the paused state.
int (*start)(void *data);
// If state is true, nonblocking operation is assumed.
// This is typically used for fast-forwarding. If driver cannot
// implement nonblocking writes, this can be disregarded, but should
// log a message to stderr.
void (*set_nonblock_state)(void *data, int state);
// Stops and frees the audio driver.
void (*free)(void *data);
// If true is returned, the audio driver is capable of using
// floating point data. This will likely increase performance as the
// resampler unit uses floating point. The sample range is
// [-1.0, 1.0].
int (*use_float)(void *data);
// The driver might be forced to use a certain output frequency
// (i.e. Jack), and thus to avoid double resampling, the driver
// can request SSNES to resample to a different sample rate.
// This function can be set to NULL if the driver does not
// desire to override the sample rate.
unsigned (*sample_rate)(void *data);
// Human readable identification string for the driver.
const char *ident;
// Must be set to SSNES_AUDIO_API_VERSION.
// Used for detecting API mismatch.
int api_version;
} ssnes_audio_driver_t;
SSNES_API_EXPORT const ssnes_audio_driver_t* SSNES_API_CALLTYPE
ssnes_audio_driver_init(void);
#ifdef __cplusplus
}
#endif
#endif
+124
View File
@@ -0,0 +1,124 @@
/////
// API header for external SSNES DSP plugins.
//
//
#ifndef __SSNES_DSP_PLUGIN_H
#define __SSNES_DSP_PLUGIN_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#ifdef SSNES_DLL_IMPORT
#define SSNES_API_EXPORT __declspec(dllimport)
#else
#define SSNES_API_EXPORT __declspec(dllexport)
#endif
#define SSNES_API_CALLTYPE __cdecl
#else
#define SSNES_API_EXPORT
#define SSNES_API_CALLTYPE
#endif
#ifndef SSNES_FALSE
#define SSNES_FALSE 0
#endif
#ifndef SSNES_TRUE
#define SSNES_TRUE 1
#endif
#define SSNES_DSP_API_VERSION 2
typedef struct ssnes_dsp_info
{
// Input sample rate that the DSP plugin receives. This is generally ~32kHz.
// Some small variance is allowed due to syncing behavior.
float input_rate;
// SSNES requests that the DSP plugin resamples the
// input to a certain frequency.
//
// However, the plugin might ignore this
// using the resample field in ssnes_dsp_output_t (see below).
float output_rate;
} ssnes_dsp_info_t;
typedef struct ssnes_dsp_output
{
// The DSP plugin has to provide the buffering for the output samples.
// This is for performance reasons to avoid redundant copying of data.
// The samples are laid out in interleaving order: LRLRLRLR
// The range of the samples are [-1.0, 1.0].
// This range cannot be exceeded without horrible audio glitches.
const float *samples;
// Frames which the DSP plugin outputted for the current process.
// One frame is here defined as a combined sample of
// left and right channels.
// (I.e. 44.1kHz, 16bit stereo will have
// 88.2k samples/sec and 44.1k frames/sec.)
unsigned frames;
// If true, the DSP plugin did not resample the input audio,
// and requests resampling to the proper frequency to be
// performed outside the plugin.
// If false,
// it is assumed that the output has the same sample rate as given
// in output_rate.
int should_resample;
} ssnes_dsp_output_t;
typedef struct ssnes_dsp_input
{
// Input data for the DSP. The samples are interleaved in order: LRLRLRLR
const float *samples;
// Number of frames for input data.
// One frame is here defined as a combined sample of
// left and right channels.
// (I.e. 44.1kHz, 16bit stereo will have
// 88.2k samples/sec and 44.1k frames/sec.)
unsigned frames;
} ssnes_dsp_input_t;
typedef struct ssnes_dsp_plugin
{
// Creates a handle of the plugin. Returns NULL if failed.
void* (*init)(const ssnes_dsp_info_t *info);
// Processes input data.
// The plugin is allowed to return variable sizes for output data.
void (*process)(void *data, ssnes_dsp_output_t *output,
const ssnes_dsp_input_t *input);
// Frees the handle.
void (*free)(void *data);
// API version used to compile the plugin.
// Used to detect mismatches in API.
// Must be set to SSNES_DSP_API_VERSION on compile.
int api_version;
// Signal plugin that it may open a configuring window or
// something similiar. The behavior of this function
// is thus plugin dependent. Implementing this is optional,
// and can be set to NULL.
void (*config)(void *data);
// Human readable identification string.
const char *ident;
} ssnes_dsp_plugin_t;
// Called by SSNES at startup to get the callback struct.
// This is NOT dynamically allocated!
SSNES_API_EXPORT const ssnes_dsp_plugin_t* SSNES_API_CALLTYPE
ssnes_dsp_plugin_init(void);
#ifdef __cplusplus
}
#endif
#endif
+418
View File
@@ -0,0 +1,418 @@
#include "ssnes_dsp.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <complex.h>
// Simple IIR/EQ filter implementation, optimized for SSE3.
#ifdef __SSE3__ // Build with -march=native or -msse3 to let this be detected! D:
#define USE_SSE3
#endif
#ifdef USE_SSE3
#include <pmmintrin.h>
#endif
// Make a test build with standalone main()
//#define TEST
#ifdef TEST
#include <stdio.h>
#include <assert.h>
#endif
// Log filter coeffs for debugging
//#define LOG
#define min(x, y) ((x) < (y) ? (x) : (y))
#ifndef M_PI
#define M_PI 3.14159265
#endif
// Taps needs to be power-of-two.
#define TAPS (2048)
static const float lp_freq = 5000.0;
static const float hp_freq = 500.0;
static const float bp_freq = 300.0;
static const unsigned bp_q = 500;
#ifdef USE_SSE3
typedef union
{
__m128 vec[TAPS / 4 + 1]; // Wraparound for unaligned reads.
float f[TAPS + 4];
} vec_filt;
#else
typedef float vec_filt[TAPS + 4];
#endif
typedef struct
{
vec_filt buffer[2];
vec_filt iir_buffer[2];
vec_filt fir_coeff;
vec_filt iir_coeff;
float out_buffer[4096];
unsigned buf_ptr;
} dsp_state;
// Convolve polynoms.
static void conv(float * restrict a, const float * restrict b, unsigned a_size, unsigned b_size)
{
float output[a_size + b_size - 1];
// Lead-in
for (int i = 0; i < b_size - 1; i++)
{
output[i] = 0.0;
for (int j = 0; j < b_size; j++)
{
if ((i - j) >= 0)
output[i] += a[i - j] * b[j];
}
}
// Mid-section
for (unsigned i = b_size - 1; i < a_size; i++)
{
output[i] = 0.0;
for (unsigned j = 0; j < b_size; j++)
output[i] += a[i - j] * b[j];
}
// Lead-out
for (unsigned i = a_size; i < a_size + b_size - 1; i++)
{
output[i] = 0.0;
for (unsigned j = 0; j < b_size; j++)
{
if ((i - j) < a_size)
output[i] += a[i - j] * b[j];
}
}
memcpy(a, output, sizeof(output));
}
#ifdef LOG
static void print_coeff(const char *key, const float *coeff, unsigned elem)
{
for (unsigned i = 0; i < elem; i++)
fprintf(stderr, "%4s [%3u] = %9.5f\n", key, i, coeff[i]);
}
static void print_filter_coeffs(const float *fir, const float *iir, unsigned elem)
{
print_coeff("FIR", fir, elem);
print_coeff("IIR", iir, elem);
}
#endif
// Simulate plain 1-pole RC circuit.
// dVc/dt = (Vin - Vc) / RC =>
// y[n] = y[n - 1] * (1 - Ts/RC) + x[n] * Ts/RC
// RCw = 1 => RC = 1/(2*pi*fc)
static void generate_low_pass(float fir_out[1], float iir_out[2], float freq, float input_rate)
{
float ts = 1.0 / input_rate;
float t = 1.0 / (2 * M_PI * freq);
fir_out[0] = ts / t;
iir_out[0] = 1.0;
iir_out[1] = -(1.0 - ts / t);
#ifdef LOG
print_coeff("FLOW", fir_out, 1);
print_coeff("ILOW", iir_out, 2);
#endif
}
// 1-pole RC circuit (Vo taken over R).
static void generate_high_pass(float fir_out[2], float iir_out[2], float freq, float input_rate)
{
float ts = 1.0 / input_rate;
float t = 1.0 / (2 * M_PI * freq);
fir_out[0] = 1.0;
fir_out[1] = -1.0;
iir_out[0] = 1.0;
iir_out[1] = -(1.0 - ts / t);
#ifdef LOG
print_coeff("FHI", fir_out, 2);
print_coeff("IHI", iir_out, 2);
#endif
}
static void generate_band_pass(float *fir_out, unsigned num_fir, float freq, float input_rate)
{
float w_norm = 2.0 * M_PI * freq / input_rate;
for (unsigned i = 0; i < num_fir; i++)
fir_out[i] = cosf(i * w_norm);
complex float amp = 0.0;
for (unsigned i = 0; i < num_fir; i++)
amp += cosf(i * w_norm) * cexp(-I * w_norm * i);
float abs = cabs(amp);
for (unsigned i = 0; i < num_fir; i++)
fir_out[i] /= abs;
}
// Generic low-pass. Differential equation.
static void generate_filter_coeffs(float * restrict fir_out, float * restrict iir_out, float input_rate)
{
fir_out[0] = 1.0;
iir_out[0] = 1.0;
unsigned fir_length = 1;
unsigned iir_length = 1;
/*
// Low pass
float lp_fir[1];
float lp_iir[2];
generate_low_pass(lp_fir, lp_iir, lp_freq, input_rate);
conv(fir_out, lp_fir, fir_length, 1);
conv(iir_out, lp_iir, iir_length, 2);
fir_length += 0;
iir_length += 1;
// High pass
float hp_fir[2];
float hp_iir[2];
generate_high_pass(hp_fir, hp_iir, hp_freq, input_rate);
conv(fir_out, hp_fir, fir_length, 2);
conv(iir_out, hp_iir, iir_length, 2);
fir_length += 1;
iir_length += 1;
*/
// Band pass
float bp_fir[bp_q];
generate_band_pass(bp_fir, bp_q, bp_freq, input_rate);
conv(fir_out, bp_fir, fir_length, bp_q);
fir_length += bp_q - 1;
iir_length += 0;
// Adjust from Z rational polynomial model to discrete domain.
// We don't care about first param as it's implicitly 1.
for (unsigned i = 0; i < TAPS; i++)
iir_out[i] = -1.0 * iir_out[i + 1];
}
static void* dsp_init(const ssnes_dsp_info_t *info)
{
(void)info;
dsp_state *state = calloc(1, sizeof(*state));
if (!state)
return NULL;
fprintf(stderr, "Input rate = %.2f\n", (float)info->input_rate);
#ifdef USE_SSE3
generate_filter_coeffs(state->fir_coeff.f, state->iir_coeff.f, info->input_rate);
#ifdef LOG
print_filter_coeffs(state->fir_coeff.f, state->iir_coeff.f, 3);
#endif
#else
generate_filter_coeffs(state->fir_coeff, state->iir_coeff, info->input_rate);
#ifdef LOG
print_filter_coeffs(state->fir_coeff, state->iir_coeff, 3);
#endif
#endif
#ifdef USE_SSE3
memcpy(state->fir_coeff.f + TAPS, state->fir_coeff.f, 4 * sizeof(float));
memcpy(state->iir_coeff.f + TAPS, state->iir_coeff.f, 4 * sizeof(float));
#endif
return state;
}
#ifdef USE_SSE3
static void calculate_iir(float *out, dsp_state *dsp)
{
unsigned buf_ptr = (dsp->buf_ptr - 1) & (TAPS - 1);
unsigned iir_ptr = (TAPS - dsp->buf_ptr) & (TAPS - 1);
const float * restrict samples_left = dsp->buffer[0].f;
const float * restrict samples_right = dsp->buffer[1].f;
const float * restrict iir_left = dsp->iir_buffer[0].f;
const float * restrict iir_right = dsp->iir_buffer[1].f;
__m128 sum_left = _mm_setzero_ps();
__m128 sum_right = _mm_setzero_ps();
for (unsigned i = 0; i < TAPS; i += 4)
{
__m128 left = _mm_load_ps(samples_left + i);
__m128 right = _mm_load_ps(samples_right + i);
const __m128 ileft = _mm_load_ps(iir_left + i);
const __m128 iright = _mm_load_ps(iir_right + i);
// Need unaligned reads here.
const __m128 fir = _mm_loadu_ps(dsp->fir_coeff.f + iir_ptr);
const __m128 iir = _mm_loadu_ps(dsp->iir_coeff.f + iir_ptr);
const __m128 fir_res_left = _mm_mul_ps(fir, left);
const __m128 fir_res_right = _mm_mul_ps(fir, right);
const __m128 iir_res_left = _mm_mul_ps(iir, ileft);
const __m128 iir_res_right = _mm_mul_ps(iir, iright);
left = _mm_add_ps(fir_res_left, iir_res_left);
right = _mm_add_ps(fir_res_right, iir_res_right);
sum_left = _mm_add_ps(sum_left, left);
sum_right = _mm_add_ps(sum_right, right);
iir_ptr = (iir_ptr + 4) & (TAPS - 1);
}
__m128 res = _mm_hadd_ps(sum_left, sum_right);
res = _mm_hadd_ps(res, res);
union
{
__m128 vec;
float f[4];
} u;
u.vec = res;
out[0] = u.f[0];
out[1] = u.f[1];
dsp->iir_buffer[0].f[buf_ptr] = u.f[0];
dsp->iir_buffer[1].f[buf_ptr] = u.f[1];
}
#else
static void calculate_iir(float *out, dsp_state *dsp)
{
const float * restrict samples_left = dsp->buffer[0];
const float * restrict samples_right = dsp->buffer[1];
const float * restrict iir_left = dsp->iir_buffer[0];
const float * restrict iir_right = dsp->iir_buffer[1];
const float * restrict fir = dsp->fir_coeff;
const float * restrict iir = dsp->iir_coeff;
unsigned iir_ptr = 0;
unsigned sample_ptr = dsp->buf_ptr;
float sum[2] = { 0.0f, 0.0f };
for (unsigned i = 0; i < TAPS; i++)
{
sum[0] += fir[iir_ptr] * samples_left[sample_ptr] + iir[iir_ptr] * iir_left[sample_ptr];
sum[1] += fir[iir_ptr] * samples_right[sample_ptr] + iir[iir_ptr] * iir_right[sample_ptr];
iir_ptr++;
sample_ptr = (sample_ptr + 1) & (TAPS - 1);
}
// Stick our output value in the IIR buffer.
for (unsigned i = 0; i < 2; i++)
{
out[i] = sum[i];
dsp->iir_buffer[i][(dsp->buf_ptr - 1) & (TAPS - 1)] = sum[i];
}
}
#endif
static void dsp_process(void *data, ssnes_dsp_output_t *output, const ssnes_dsp_input_t *input)
{
dsp_state *dsp = data;
output->should_resample = SSNES_TRUE;
output->frames = input->frames;
output->samples = dsp->out_buffer;
for (unsigned i = 0; i < input->frames; i++)
{
#ifdef USE_SSE3
dsp->buffer[0].f[dsp->buf_ptr] = input->samples[(i << 1) + 0];
dsp->buffer[1].f[dsp->buf_ptr] = input->samples[(i << 1) + 1];
#else
dsp->buffer[0][dsp->buf_ptr] = input->samples[(i << 1) + 0];
dsp->buffer[1][dsp->buf_ptr] = input->samples[(i << 1) + 1];
#endif
calculate_iir(&dsp->out_buffer[i << 1], dsp);
dsp->buf_ptr = (dsp->buf_ptr - 1) & (TAPS - 1);
}
}
static void dsp_config(void *data)
{
(void)data;
// Normally we unhide a GUI window or something,
// but we're just going to print to the log instead.
fprintf(stderr, "DSP_CONFIG\n");
}
static void dsp_free(void *data)
{
free(data);
}
const ssnes_dsp_plugin_t dsp_plug = {
.init = dsp_init,
.process = dsp_process,
.free = dsp_free,
.config = dsp_config,
.api_version = SSNES_DSP_API_VERSION,
.ident = "IIR filter"
};
SSNES_API_EXPORT const ssnes_dsp_plugin_t*
SSNES_API_CALLTYPE ssnes_dsp_plugin_init(void)
{
return &dsp_plug;
}
#ifdef TEST
int main(void)
{
const ssnes_dsp_plugin_t *plug = ssnes_dsp_plugin_init();
assert(plug);
ssnes_dsp_info_t info = {
.input_rate = 44100,
.output_rate = 44100
};
void *handle = plug->init(&info); // Info isn't used.
assert(handle);
int16_t buf[64];
float fbuf[64];
for (;;)
{
size_t rd = fread(buf, sizeof(int16_t), 64, stdin);
for (unsigned i = 0; i < rd; i++)
fbuf[i] = (float)buf[i];
ssnes_dsp_input_t input = {
.samples = fbuf,
.frames = rd >> 1
};
ssnes_dsp_output_t output;
plug->process(handle, &output, &input);
for (unsigned i = 0; i < output.frames << 1; i++)
{
int32_t sample = (int32_t)output.samples[i];
buf[i] = (sample > 0x7fff) ? 0x7fff : ((sample < -0x8000) ? -0x8000 : (int16_t)sample);
}
fwrite(buf, sizeof(int16_t), output.frames << 1, stdout);
if (rd < 64)
break;
}
plug->free(handle);
}
#endif
+10 -2
View File
@@ -103,7 +103,15 @@ static size_t find_buffersize(jack_t *jd, int latency)
{
int frames = latency * g_settings.audio.out_rate / 1000;
int jack_latency = jack_port_get_total_latency(jd->client, jd->ports[0]);
jack_latency_range_t range;
int jack_latency = 0;
for (int i = 0; i < 2; i++)
{
jack_port_get_latency_range(jd->ports[i], JackPlaybackLatency, &range);
if (range.max > jack_latency)
jack_latency = range.max;
}
SSNES_LOG("JACK: Jack latency is %d frames.\n", jack_latency);
int buffer_frames = frames - jack_latency;
@@ -116,7 +124,7 @@ static size_t find_buffersize(jack_t *jd, int latency)
return buffer_frames * sizeof(jack_default_audio_sample_t);
}
static void* __jack_init(const char* device, int rate, int latency)
static void* __jack_init(const char* device, unsigned rate, unsigned latency)
{
jack_t *jd = calloc(1, sizeof(jack_t));
if ( jd == NULL )
+1 -1
View File
@@ -56,7 +56,7 @@ typedef struct al
} al_t;
static void* __al_init(const char* device, int rate, int latency)
static void* __al_init(const char* device, unsigned rate, unsigned latency)
{
(void)device;
al_t *al = calloc(1, sizeof(al_t));
+1 -1
View File
@@ -25,7 +25,7 @@
#include <errno.h>
#include <stdio.h>
static void* __oss_init(const char* device, int rate, int latency)
static void* __oss_init(const char* device, unsigned rate, unsigned latency)
{
int *fd = calloc(1, sizeof(int));
if ( fd == NULL )
+1 -1
View File
@@ -115,7 +115,7 @@ static void stream_latency_update_cb(pa_stream *s, void *data)
pa_threaded_mainloop_signal(pa->mainloop, 0);
}
static void* __pulse_init(const char* device, int rate, int latency)
static void* __pulse_init(const char* device, unsigned rate, unsigned latency)
{
pa_t *pa = calloc(1, sizeof(*pa));
if (!pa)
+11 -8
View File
@@ -22,6 +22,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include "general.h"
typedef struct
{
@@ -29,7 +30,7 @@ typedef struct
bool nonblocking;
} roar_t;
static void* __roar_init(const char* device, int rate, int latency)
static void* __roar_init(const char* device, unsigned rate, unsigned latency)
{
int err;
roar_t *roar = calloc(1, sizeof(roar_t));
@@ -37,13 +38,14 @@ static void* __roar_init(const char* device, int rate, int latency)
return NULL;
roar_vs_t *vss;
if ( (vss = roar_vs_new_simple(NULL, NULL, rate, 2, ROAR_CODEC_PCM_S_LE, 16, ROAR_DIR_PLAY, &err)) == NULL )
if ((vss = roar_vs_new_simple(device, "SSNES", rate, 2, ROAR_CODEC_PCM_S, 16, ROAR_DIR_PLAY, &err)) == NULL)
{
fprintf(stderr, "roar_vs: \"%s\"\n", roar_vs_strerr(err));
SSNES_ERR("RoarAudio: \"%s\"\n", roar_vs_strerr(err));
free(roar);
return NULL;
}
roar_vs_role(vss, ROAR_ROLE_GAME, NULL);
roar->vss = vss;
return roar;
@@ -54,19 +56,20 @@ static ssize_t __roar_write(void* data, const void* buf, size_t size)
roar_t *roar = data;
ssize_t rc;
if ( size == 0 )
if (size == 0)
return 0;
int err;
size_t written = 0;
while (written < size)
{
if ((rc = roar_vs_write(roar->vss, (const char*)buf + written, size - written, &err)) < size)
size_t write_amt = size - written;
if ((rc = roar_vs_write(roar->vss, (const char*)buf + written, write_amt, &err)) < write_amt)
{
if (rc < 0)
if (roar->nonblocking)
return rc;
else if (rc < 0)
return -1;
else if (roar->nonblocking)
return 0;
}
written += rc;
}
+1 -1
View File
@@ -54,7 +54,7 @@ static void err_cb(void *userdata)
SDL_CondSignal(rsd->cond);
}
static void* __rsd_init(const char* device, int rate, int latency)
static void* __rsd_init(const char* device, unsigned rate, unsigned latency)
{
rsd_t *rsd = calloc(1, sizeof(rsd_t));
if ( rsd == NULL )
+1 -14
View File
@@ -51,19 +51,6 @@ static void sdl_audio_cb(void *data, Uint8 *stream, int len)
memset(stream + write_size, 0, len - write_size);
}
// Interesting hack from http://www-graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
static inline uint32_t next_pow2(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
static inline int find_num_frames(int rate, int latency)
{
int frames = (rate * latency) / 1000;
@@ -71,7 +58,7 @@ static inline int find_num_frames(int rate, int latency)
return next_pow2(frames);
}
static void* sdl_audio_init(const char* device, int rate, int latency)
static void* sdl_audio_init(const char* device, unsigned rate, unsigned latency)
{
(void)device;
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
+1 -14
View File
@@ -67,20 +67,7 @@ static bool init_lib(void)
return true;
}
// Interesting hack from http://www-graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
static inline uint32_t next_pow2(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
static void* __xa_init(const char* device, int rate, int latency)
static void* __xa_init(const char* device, unsigned rate, unsigned latency)
{
if (!init_lib())
return NULL;
+144
View File
@@ -0,0 +1,144 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "autosave.h"
#include "SDL.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "general.h"
struct autosave
{
volatile bool quit;
SDL_mutex *lock;
SDL_mutex *cond_lock;
SDL_cond *cond;
SDL_Thread *thread;
void *buffer;
const void *snes_buffer;
const char *path;
size_t bufsize;
unsigned interval;
};
static int autosave_thread(void *data)
{
autosave_t *save = data;
while (!save->quit)
{
autosave_lock(save);
memcpy(save->buffer, save->snes_buffer, save->bufsize);
autosave_unlock(save);
// Should probably deal with this more elegantly.
FILE *file = fopen(save->path, "wb");
if (file)
{
SSNES_LOG("Autosaving SRAM to \"%s\"\n", save->path);
bool failed = false;
failed |= fwrite(save->buffer, 1, save->bufsize, file) != save->bufsize;
failed |= fflush(file) != 0;
failed |= fclose(file) != 0;
if (failed)
SSNES_WARN("Failed to autosave SRAM! Disk might be full.\n");
}
SDL_mutexP(save->cond_lock);
if (!save->quit)
SDL_CondWaitTimeout(save->cond, save->cond_lock, save->interval * 1000);
SDL_mutexV(save->cond_lock);
}
return 0;
}
autosave_t *autosave_new(const char *path, const void *data, size_t size, unsigned interval)
{
autosave_t *handle = calloc(1, sizeof(*handle));
if (!handle)
return NULL;
handle->bufsize = size;
handle->interval = interval;
handle->path = path;
handle->buffer = malloc(size);
handle->snes_buffer = data;
if (!handle->buffer)
{
free(handle);
return NULL;
}
handle->lock = SDL_CreateMutex();
handle->cond_lock = SDL_CreateMutex();
handle->cond = SDL_CreateCond();
handle->thread = SDL_CreateThread(autosave_thread, handle);
return handle;
}
void autosave_lock(autosave_t *handle)
{
SDL_mutexP(handle->lock);
}
void autosave_unlock(autosave_t *handle)
{
SDL_mutexV(handle->lock);
}
void autosave_free(autosave_t *handle)
{
SDL_mutexP(handle->cond_lock);
handle->quit = true;
SDL_mutexV(handle->cond_lock);
SDL_CondSignal(handle->cond);
SDL_WaitThread(handle->thread, NULL);
SDL_DestroyMutex(handle->lock);
SDL_DestroyMutex(handle->cond_lock);
SDL_DestroyCond(handle->cond);
free(handle->buffer);
free(handle);
}
void lock_autosave(void)
{
for (unsigned i = 0; i < sizeof(g_extern.autosave)/sizeof(g_extern.autosave[0]); i++)
{
if (g_extern.autosave[i])
autosave_lock(g_extern.autosave[i]);
}
}
void unlock_autosave(void)
{
for (unsigned i = 0; i < sizeof(g_extern.autosave)/sizeof(g_extern.autosave[0]); i++)
{
if (g_extern.autosave[i])
autosave_unlock(g_extern.autosave[i]);
}
}
+33
View File
@@ -0,0 +1,33 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_AUTOSAVE_H
#define __SSNES_AUTOSAVE_H
#include <stddef.h>
typedef struct autosave autosave_t;
autosave_t *autosave_new(const char *path, const void *data, size_t size, unsigned interval);
void autosave_lock(autosave_t *handle);
void autosave_unlock(autosave_t *handle);
void autosave_free(autosave_t *handle);
void lock_autosave(void);
void unlock_autosave(void);
#endif
+261
View File
@@ -0,0 +1,261 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "cheats.h"
#include "sha256.h"
#include "dynamic.h"
#include "general.h"
#include "strl.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
struct cheat
{
char *desc;
bool state;
char *code;
};
struct cheat_manager
{
struct cheat *cheats;
unsigned ptr;
unsigned size;
unsigned buf_size;
};
static char* strcat_alloc(char *dest, const char *input)
{
size_t dest_len = dest ? strlen(dest) : 0;
size_t input_len = strlen(input);
size_t required_len = dest_len + input_len + 1;
char *output = realloc(dest, required_len);
assert(output);
if (dest)
strlcat(output, input, required_len);
else
strlcpy(output, input, required_len);
return output;
}
static void xml_grab_cheat(struct cheat *cht, xmlNodePtr ptr)
{
memset(cht, 0, sizeof(struct cheat));
bool first = true;
for (xmlNodePtr node = ptr; node; node = node->next)
{
if (strcmp((const char*)node->name, "description") == 0)
{
cht->desc = (char*)xmlNodeGetContent(node);
}
else if (strcmp((const char*)node->name, "code") == 0)
{
if (!first)
cht->code = strcat_alloc(cht->code, "+");
xmlChar *code = xmlNodeGetContent(node);
assert(code);
cht->code = strcat_alloc(cht->code, (const char*)code);
xmlFree(code);
first = false;
}
}
}
static void xml_grab_cheats(cheat_manager_t *handle, xmlNodePtr ptr)
{
xmlNodePtr node = NULL;
for (node = ptr; node; node = node->next)
{
if (strcmp((const char*)node->name, "name") == 0)
{
xmlChar *name = xmlNodeGetContent(node);
if (name)
{
SSNES_LOG("Found cheat for game: \"%s\"\n", name);
xmlFree(name);
}
}
else if (strcmp((const char*)node->name, "cheat") == 0)
{
if (handle->size == handle->buf_size)
{
handle->buf_size *= 2;
handle->cheats = realloc(handle->cheats, handle->buf_size * sizeof(struct cheat));
assert(handle->cheats);
}
xml_grab_cheat(&handle->cheats[handle->size++], node->children);
}
}
}
cheat_manager_t* cheat_manager_new(const char *path)
{
psnes_cheat_reset();
xmlParserCtxtPtr ctx = NULL;
xmlDocPtr doc = NULL;
cheat_manager_t *handle = calloc(1, sizeof(struct cheat_manager));
if (!handle)
return NULL;
handle->buf_size = 32;
handle->cheats = malloc(handle->buf_size * sizeof(struct cheat));
if (!handle->cheats)
{
handle->buf_size = 0;
goto error;
}
ctx = xmlNewParserCtxt();
if (!ctx)
goto error;
doc = xmlCtxtReadFile(ctx, path, NULL, 0);
if (!doc)
{
SSNES_ERR("Failed to parse XML file: %s\n", path);
goto error;
}
if (ctx->valid == 0)
{
SSNES_ERR("Cannot validate XML file: %s\n", path);
goto error;
}
xmlNodePtr head = xmlDocGetRootElement(doc);
xmlNodePtr cur = NULL;
for (cur = head; cur; cur = cur->next)
{
if (cur->type == XML_ELEMENT_NODE && strcmp((const char*)cur->name, "database") == 0)
break;
}
if (!cur)
goto error;
for (cur = cur->children; cur; cur = cur->next)
{
if (cur->type != XML_ELEMENT_NODE)
continue;
if (strcmp((const char*)cur->name, "cartridge") == 0)
{
xmlChar *sha256 = xmlGetProp(cur, (const xmlChar*)"sha256");
if (!sha256)
continue;
if (memcmp(sha256, g_extern.sha256, 64) == 0)
{
xmlFree(sha256);
break;
}
xmlFree(sha256);
}
}
if (!cur)
goto error;
xml_grab_cheats(handle, cur->children);
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctx);
return handle;
error:
cheat_manager_free(handle);
if (doc)
xmlFreeDoc(doc);
if (ctx)
xmlFreeParserCtxt(ctx);
return NULL;
}
void cheat_manager_free(cheat_manager_t *handle)
{
if (!handle)
return;
if (handle->cheats)
{
for (unsigned i = 0; i < handle->size; i++)
{
xmlFree(handle->cheats[i].desc);
free(handle->cheats[i].code);
}
free(handle->cheats);
}
free(handle);
}
static void cheat_manager_update(cheat_manager_t *handle)
{
msg_queue_clear(g_extern.msg_queue);
char msg[256];
snprintf(msg, sizeof(msg), "Cheat: #%u [%s]: %s", handle->ptr, handle->cheats[handle->ptr].state ? "ON" : "OFF", handle->cheats[handle->ptr].desc);
msg_queue_push(g_extern.msg_queue, msg, 1, 180);
SSNES_LOG("%s\n", msg);
}
void cheat_manager_toggle(cheat_manager_t *handle)
{
handle->cheats[handle->ptr].state ^= true;
unsigned index = 0;
psnes_cheat_reset();
for (unsigned i = 0; i < handle->size; i++)
{
if (handle->cheats[handle->ptr].state)
psnes_cheat_set(index++, true, handle->cheats[handle->ptr].code);
}
cheat_manager_update(handle);
}
void cheat_manager_index_next(cheat_manager_t *handle)
{
handle->ptr = (handle->ptr + 1) % handle->size;
cheat_manager_update(handle);
}
void cheat_manager_index_prev(cheat_manager_t *handle)
{
if (handle->ptr == 0)
handle->ptr = handle->size - 1;
else
handle->ptr--;
cheat_manager_update(handle);
}
+30
View File
@@ -0,0 +1,30 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_CHEATS_H
#define __SSNES_CHEATS_H
typedef struct cheat_manager cheat_manager_t;
cheat_manager_t* cheat_manager_new(const char *path);
void cheat_manager_free(cheat_manager_t *handle);
void cheat_manager_index_next(cheat_manager_t *handle);
void cheat_manager_index_prev(cheat_manager_t *handle);
void cheat_manager_toggle(cheat_manager_t *handle);
#endif
+16
View File
@@ -223,6 +223,22 @@ bool config_get_int(config_file_t *conf, const char *key, int *in)
return false;
}
bool config_get_hex(config_file_t *conf, const char *key, unsigned *in)
{
struct entry_list *list = conf->entries;
while (list != NULL)
{
if (strcmp(key, list->key) == 0)
{
*in = strtol(list->value, NULL, 16);
return true;
}
list = list->next;
}
return false;
}
bool config_get_char(config_file_t *conf, const char *key, char *in)
{
struct entry_list *list = conf->entries;
+2
View File
@@ -42,6 +42,8 @@ void config_file_free(config_file_t *conf);
bool config_get_double(config_file_t *conf, const char *entry, double *in);
// Extracts an int from config file.
bool config_get_int(config_file_t *conf, const char *entry, int *in);
// Extracts an int from config file. (Hexadecimal)
bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in);
// Extracts a single char. If value consists of several chars, this is an error.
bool config_get_char(config_file_t *conf, const char *entry, char *in);
// Extracts an allocated string in *in. This must be free()-d if this function succeeds.
+69 -86
View File
@@ -33,7 +33,7 @@
#ifdef HAVE_SDL
#include "SDL.h"
#else
#error HAVE_SDL is not defined!
#error "HAVE_SDL is not defined!"
#endif
#ifdef HAVE_SRC
@@ -43,6 +43,9 @@
///////////////// Drivers
#define VIDEO_GL 0
#define VIDEO_XVIDEO 11
#define VIDEO_SDL 13
#define VIDEO_EXT 14
////////////////////////
#define AUDIO_RSOUND 1
#define AUDIO_OSS 2
@@ -53,11 +56,23 @@
#define AUDIO_SDL 8
#define AUDIO_XAUDIO 9
#define AUDIO_PULSE 10
#define AUDIO_EXT 15
////////////////////////
#define INPUT_SDL 7
#define INPUT_X 12
////////////////////////
#if defined(HAVE_SDL)
#define VIDEO_DEFAULT_DRIVER VIDEO_GL
#elif defined(HAVE_XVIDEO)
#define VIDEO_DEFAULT_DRIVER VIDEO_XVIDEO
#elif defined(HAVE_SDL)
#define VIDEO_DEFAULT_DRIVER VIDEO_SDL
#elif defined(HAVE_DYLIB)
#define VIDEO_DEFAULT_DRIVER VIDEO_EXT
#else
#error "Need at least one video driver!"
#endif
#if defined(HAVE_ALSA)
#define AUDIO_DEFAULT_DRIVER AUDIO_ALSA
@@ -77,11 +92,19 @@
#define AUDIO_DEFAULT_DRIVER AUDIO_RSOUND
#elif defined(HAVE_ROAR)
#define AUDIO_DEFAULT_DRIVER AUDIO_ROAR
#elif defined(HAVE_DYLIB)
#define AUDIO_DEFAULT_DRIVER AUDIO_EXT
#else
#error Need at least one audio driver!
#error "Need at least one audio driver!"
#endif
#if defined(HAVE_SDL)
#define INPUT_DEFAULT_DRIVER INPUT_SDL
#elif defined(HAVE_XVIDEO)
#define INPUT_DEFAULT_DRIVER INPUT_X
#else
#error "Need at least one input driver!"
#endif
////////////////
@@ -94,8 +117,11 @@ static const float yscale = 3.0; // Real y res = 224 * yscale
// Fullscreen
static const bool fullscreen = false; // To start in Fullscreen or not
static const unsigned fullscreen_x = 1280;
static const unsigned fullscreen_y = 720;
static const unsigned fullscreen_x = 0; // Fullscreen resolution. A value of 0 uses the desktop resolution.
static const unsigned fullscreen_y = 0;
// Force 16-bit colors.
static const bool force_16bit = false;
// Video VSYNC (recommended)
static const bool vsync = true;
@@ -106,6 +132,9 @@ static const bool video_smooth = true;
// On resize and fullscreen, rendering area will stay 4:3
static const bool force_aspect = true;
// Crop overscanned frames (7/8 or 15/15 for interlaced frames).
static const bool crop_overscan = false;
// Font size for on-screen messages.
static const unsigned font_size = 48;
@@ -113,6 +142,12 @@ static const unsigned font_size = 48;
static const float message_pos_offset_x = 0.05;
static const float message_pos_offset_y = 0.05;
// Render-to-texture before rendering to screen (multi-pass shaders)
static const bool render_to_texture = false;
static const float fbo_scale_x = 2.0;
static const float fbo_scale_y = 2.0;
static const bool second_pass_smooth = true;
#define SNES_ASPECT_RATIO (4.0/3)
////////////////
@@ -159,6 +194,12 @@ static const unsigned rewind_granularity = 1;
// Pause gameplay when gameplay loses focus.
static const bool pause_nonactive = true;
// Saves non-volatile SRAM at a regular interval. It is measured in seconds. A value of 0 disables autosave.
static const unsigned autosave_interval = 0;
// When being client over netplay, use keybinds for player 1 rather than player 2.
static const bool netplay_client_swap_input = false;
////////////////////
// Keybinds, Joypad
@@ -174,19 +215,20 @@ static const bool pause_nonactive = true;
// Player 1
static const struct snes_keybind snes_keybinds_1[] = {
// SNES button | keyboard key | js btn | js axis |
{ SNES_DEVICE_ID_JOYPAD_A, SDLK_x, 1, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_B, SDLK_z, 0, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_X, SDLK_s, 3, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_Y, SDLK_a, 2, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_L, SDLK_q, 4, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_R, SDLK_w, 5, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_LEFT, SDLK_LEFT, 11, AXIS_NEG(0) },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, SDLK_RIGHT, 12, AXIS_POS(0) },
{ SNES_DEVICE_ID_JOYPAD_UP, SDLK_UP, 13, AXIS_NEG(1) },
{ SNES_DEVICE_ID_JOYPAD_DOWN, SDLK_DOWN, 14, AXIS_POS(1) },
{ SNES_DEVICE_ID_JOYPAD_START, SDLK_RETURN, 7, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_SELECT, SDLK_RSHIFT, 6, AXIS_NONE },
{ SSNES_FAST_FORWARD_KEY, SDLK_SPACE, 10, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_A, SDLK_x, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_B, SDLK_z, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_X, SDLK_s, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_Y, SDLK_a, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_L, SDLK_q, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_R, SDLK_w, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_LEFT, SDLK_LEFT, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, SDLK_RIGHT, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_UP, SDLK_UP, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_DOWN, SDLK_DOWN, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_START, SDLK_RETURN, NO_BTN, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_SELECT, SDLK_RSHIFT, NO_BTN, AXIS_NONE },
{ SSNES_FAST_FORWARD_KEY, SDLK_SPACE, NO_BTN, AXIS_NONE },
{ SSNES_FAST_FORWARD_HOLD_KEY, SDLK_l, NO_BTN, AXIS_NONE },
{ SSNES_SAVE_STATE_KEY, SDLK_F2, NO_BTN, AXIS_NONE },
{ SSNES_LOAD_STATE_KEY, SDLK_F4, NO_BTN, AXIS_NONE },
{ SSNES_FULLSCREEN_TOGGLE_KEY, SDLK_f, NO_BTN, AXIS_NONE },
@@ -198,78 +240,19 @@ static const struct snes_keybind snes_keybinds_1[] = {
{ SSNES_REWIND, SDLK_r, NO_BTN, AXIS_NONE },
{ SSNES_MOVIE_RECORD_TOGGLE, SDLK_o, NO_BTN, AXIS_NONE },
{ SSNES_PAUSE_TOGGLE, SDLK_p, NO_BTN, AXIS_NONE },
{ SSNES_RESET, SDLK_h, NO_BTN, AXIS_NONE },
{ SSNES_SHADER_NEXT, SDLK_m, NO_BTN, AXIS_NONE },
{ SSNES_SHADER_PREV, SDLK_n, NO_BTN, AXIS_NONE },
{ SSNES_CHEAT_INDEX_PLUS, SDLK_y, NO_BTN, AXIS_NONE },
{ SSNES_CHEAT_INDEX_MINUS, SDLK_t, NO_BTN, AXIS_NONE },
{ SSNES_CHEAT_TOGGLE, SDLK_u, NO_BTN, AXIS_NONE },
{ SSNES_SCREENSHOT, SDLK_PRINT, NO_BTN, AXIS_NONE },
{ SSNES_DSP_CONFIG, SDLK_c, NO_BTN, AXIS_NONE },
{ -1 }
};
// Player 2
static const struct snes_keybind snes_keybinds_2[] = {
// SNES button | keyboard key | js btn | js axis |
{ SNES_DEVICE_ID_JOYPAD_A, SDLK_b, 1, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_B, SDLK_v, 0, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_X, SDLK_g, 3, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_Y, SDLK_f, 2, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_L, SDLK_r, 4, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_R, SDLK_t, 5, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_LEFT, SDLK_j, 11, AXIS_NEG(0) },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, SDLK_l, 12, AXIS_POS(0) },
{ SNES_DEVICE_ID_JOYPAD_UP, SDLK_i, 13, AXIS_NEG(1) },
{ SNES_DEVICE_ID_JOYPAD_DOWN, SDLK_k, 14, AXIS_POS(1) },
{ SNES_DEVICE_ID_JOYPAD_START, SDLK_p, 6, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_SELECT, SDLK_o, 7, AXIS_NONE },
{ -1 }
};
// Player 3
static const struct snes_keybind snes_keybinds_3[] = {
// SNES button | keyboard key | js btn | js axis |
{ SNES_DEVICE_ID_JOYPAD_A, SDLK_b, 1, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_B, SDLK_v, 0, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_X, SDLK_g, 3, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_Y, SDLK_f, 2, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_L, SDLK_r, 4, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_R, SDLK_t, 5, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_LEFT, SDLK_j, 11, AXIS_NEG(0) },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, SDLK_l, 12, AXIS_POS(0) },
{ SNES_DEVICE_ID_JOYPAD_UP, SDLK_i, 13, AXIS_NEG(1) },
{ SNES_DEVICE_ID_JOYPAD_DOWN, SDLK_k, 14, AXIS_POS(1) },
{ SNES_DEVICE_ID_JOYPAD_START, SDLK_p, 6, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_SELECT, SDLK_o, 7, AXIS_NONE },
{ -1 }
};
// Player 4
static const struct snes_keybind snes_keybinds_4[] = {
// SNES button | keyboard key | js btn | js axis |
{ SNES_DEVICE_ID_JOYPAD_A, SDLK_b, 1, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_B, SDLK_v, 0, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_X, SDLK_g, 3, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_Y, SDLK_f, 2, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_L, SDLK_r, 4, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_R, SDLK_t, 5, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_LEFT, SDLK_j, 11, AXIS_NEG(0) },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, SDLK_l, 12, AXIS_POS(0) },
{ SNES_DEVICE_ID_JOYPAD_UP, SDLK_i, 13, AXIS_NEG(1) },
{ SNES_DEVICE_ID_JOYPAD_DOWN, SDLK_k, 14, AXIS_POS(1) },
{ SNES_DEVICE_ID_JOYPAD_START, SDLK_p, 6, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_SELECT, SDLK_o, 7, AXIS_NONE },
{ -1 }
};
// Player 5
static const struct snes_keybind snes_keybinds_5[] = {
// SNES button | keyboard key | js btn | js axis |
{ SNES_DEVICE_ID_JOYPAD_A, SDLK_b, 1, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_B, SDLK_v, 0, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_X, SDLK_g, 3, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_Y, SDLK_f, 2, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_L, SDLK_r, 4, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_R, SDLK_t, 5, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_LEFT, SDLK_j, 11, AXIS_NEG(0) },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, SDLK_l, 12, AXIS_POS(0) },
{ SNES_DEVICE_ID_JOYPAD_UP, SDLK_i, 13, AXIS_NEG(1) },
{ SNES_DEVICE_ID_JOYPAD_DOWN, SDLK_k, 14, AXIS_POS(1) },
{ SNES_DEVICE_ID_JOYPAD_START, SDLK_p, 6, AXIS_NONE },
{ SNES_DEVICE_ID_JOYPAD_SELECT, SDLK_o, 7, AXIS_NONE },
// Player 2-5
static const struct snes_keybind snes_keybinds_rest[] = {
{ -1 }
};
+21 -3
View File
@@ -62,10 +62,10 @@ static const bool _xaudio_supp = true;
static const bool _xaudio_supp = false;
#endif
#ifdef HAVE_FILTER
static const bool _filter_supp = true;
#ifdef HAVE_DYLIB
static const bool _dylib_supp = true;
#else
static const bool _filter_supp = false;
static const bool _dylib_supp = false;
#endif
#ifdef HAVE_CG
@@ -80,6 +80,18 @@ static const bool _xml_supp = true;
static const bool _xml_supp = false;
#endif
#ifdef HAVE_IMLIB
static const bool _imlib_supp = true;
#else
static const bool _imlib_supp = false;
#endif
#ifdef HAVE_FBO
static const bool _fbo_supp = true;
#else
static const bool _fbo_supp = false;
#endif
#ifdef HAVE_DYNAMIC
static const bool _dynamic_supp = true;
#else
@@ -110,4 +122,10 @@ static const bool _freetype_supp = true;
static const bool _freetype_supp = false;
#endif
#ifdef HAVE_NETPLAY
static const bool _netplay_supp = true;
#else
static const bool _netplay_supp = false;
#endif
#endif
+27 -1
View File
@@ -47,7 +47,7 @@ Should this not be defined, \fBssnes\fR will look in platform specific paths to
/etc/ssnes.cfg (when installed), or ssnes.cfg in the source tarball serves as a skeleton configuration file.
.IP
Unix-like systems will look in $XDG_CONFIG_HOME/ssnes/ssnes.cfg first. Then it will try $HOME/.ssnesrc. Last, it will try /etc/ssnes.cfg. If no configuration is found, default settings will be assumed. A configuration file does not need to define every possible option, only those that should be overridden.
Unix-like systems will look in $XDG_CONFIG_HOME/ssnes/ssnes.cfg first. Then it will try $HOME/.ssnes.cfg. Last, it will try /etc/ssnes.cfg. If no configuration is found, default settings will be assumed. A configuration file does not need to define every possible option, only those that should be overridden.
.IP
Windows will look in ssnes.cfg in same folder where ssnes.exe resides.
@@ -109,5 +109,31 @@ It also requires to play back with the same libsnes backend that was used for re
\fB--verbose, -v\fR
Activates verbose logging.
.TP
\fB--host, -H\fR
Be the host of netplay. Waits until a player connects. The host will always assume player 1.
.TP
\fB--connect SERVER, -C SERVER\fR
Connect to a host of netplay. Will always assume player 2.
.TP
\fB--frames FRAMES, -F FRAMES\fR
Sync frames to use when using netplay. More frames allow for more latency, but requires more CPU power.
Set FRAMES to 0 to have perfect sync. 0 frames is only suitable for LAN. Defaults to 0.
.TP
\fB--port PORT\fR
Network port used for netplay. This defaults to 55435. This option affects both TCP and UDP.
.TP
\fB--ups PATCH, -U PATCH\fR
Attempts to apply an UPS patch to the current ROM image. No files are altered.
If this flag is not specified, SSNES will look for a .ups file with same basename as ROM specified.
.TP
\fB-D, --detach\fR
Detach from the current console. This is currently only relevant for Microsoft Windows.
.SH "SEE ALSO"
\fBssnes-joyconfig\fR(1)
+225 -17
View File
@@ -18,10 +18,11 @@
#include "driver.h"
#include "general.h"
#include "file.h"
#include <stdio.h>
#include <string.h>
#include "hqflt/filters.h"
#include <assert.h>
#include <math.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -55,11 +56,21 @@ static const audio_driver_t *audio_drivers[] = {
#ifdef HAVE_PULSE
&audio_pulse,
#endif
#ifdef HAVE_DYLIB
&audio_ext,
#endif
};
static const video_driver_t *video_drivers[] = {
#ifdef HAVE_SDL
&video_gl,
&video_sdl,
#endif
#ifdef HAVE_XVIDEO
&video_xvideo,
#endif
#ifdef HAVE_DYLIB
&video_ext,
#endif
};
@@ -67,6 +78,9 @@ static const input_driver_t *input_drivers[] = {
#ifdef HAVE_SDL
&input_sdl,
#endif
#ifdef HAVE_XVIDEO
&input_x,
#endif
};
static void find_audio_driver(void)
@@ -135,6 +149,71 @@ void uninit_drivers(void)
uninit_audio();
}
static void init_dsp_plugin(void)
{
if (!(*g_settings.audio.dsp_plugin))
return;
g_extern.audio_data.dsp_lib = dylib_load(g_settings.audio.dsp_plugin);
if (!g_extern.audio_data.dsp_lib)
{
SSNES_ERR("Failed to open DSP plugin: \"%s\" ...\n", g_settings.audio.dsp_plugin);
return;
}
const ssnes_dsp_plugin_t* (*SSNES_API_CALLTYPE plugin_init)(void) =
(const ssnes_dsp_plugin_t *(*)(void))dylib_proc(g_extern.audio_data.dsp_lib, "ssnes_dsp_plugin_init");
if (!plugin_init)
{
SSNES_ERR("Failed to find symbol \"ssnes_dsp_plugin_init\" in DSP plugin.\n");
goto error;
}
g_extern.audio_data.dsp_plugin = plugin_init();
if (!g_extern.audio_data.dsp_plugin)
{
SSNES_ERR("Failed to get a valid DSP plugin.\n");
goto error;
}
if (g_extern.audio_data.dsp_plugin->api_version != SSNES_DSP_API_VERSION)
{
SSNES_ERR("DSP plugin API mismatch! SSNES: %d, Plugin: %d\n", SSNES_DSP_API_VERSION, g_extern.audio_data.dsp_plugin->api_version);
goto error;
}
SSNES_LOG("Loaded DSP plugin: \"%s\"\n", g_extern.audio_data.dsp_plugin->ident ? g_extern.audio_data.dsp_plugin->ident : "Unknown");
ssnes_dsp_info_t info = {
.input_rate = g_settings.audio.in_rate,
.output_rate = g_settings.audio.out_rate
};
g_extern.audio_data.dsp_handle = g_extern.audio_data.dsp_plugin->init(&info);
if (!g_extern.audio_data.dsp_handle)
{
SSNES_ERR("Failed to init DSP plugin.\n");
goto error;
}
return;
error:
if (g_extern.audio_data.dsp_lib)
dylib_close(g_extern.audio_data.dsp_lib);
g_extern.audio_data.dsp_plugin = NULL;
g_extern.audio_data.dsp_lib = NULL;
}
static void deinit_dsp_plugin(void)
{
if (g_extern.audio_data.dsp_lib && g_extern.audio_data.dsp_plugin)
{
g_extern.audio_data.dsp_plugin->free(g_extern.audio_data.dsp_handle);
dylib_close(g_extern.audio_data.dsp_lib);
}
}
#define AUDIO_CHUNK_SIZE_BLOCKING 64
#define AUDIO_CHUNK_SIZE_NONBLOCKING 2048 // So we don't get complete line-noise when fast-forwarding audio.
#define AUDIO_MAX_RATIO 16
@@ -185,6 +264,8 @@ void init_audio(void)
g_extern.audio_data.data_ptr = 0;
assert((g_extern.audio_data.outsamples = malloc(max_bufsamples * sizeof(float) * AUDIO_MAX_RATIO)));
assert((g_extern.audio_data.conv_outsamples = malloc(max_bufsamples * sizeof(int16_t) * AUDIO_MAX_RATIO)));
init_dsp_plugin();
}
void uninit_audio(void)
@@ -210,39 +291,160 @@ void uninit_audio(void)
free(g_extern.audio_data.data); g_extern.audio_data.data = NULL;
free(g_extern.audio_data.outsamples); g_extern.audio_data.outsamples = NULL;
free(g_extern.audio_data.conv_outsamples); g_extern.audio_data.conv_outsamples = NULL;
deinit_dsp_plugin();
}
#ifdef HAVE_DYLIB
static void init_filter(void)
{
if (g_extern.filter.active)
return;
if (*g_settings.video.filter_path == '\0')
return;
SSNES_LOG("Loading bSNES filter from \"%s\"\n", g_settings.video.filter_path);
g_extern.filter.lib = dylib_load(g_settings.video.filter_path);
if (!g_extern.filter.lib)
{
SSNES_ERR("Failed to load filter \"%s\"\n", g_settings.video.filter_path);
return;
}
g_extern.filter.psize =
(void (*)(unsigned*, unsigned*))dylib_proc(g_extern.filter.lib, "filter_size");
g_extern.filter.prender =
(void (*)(uint32_t*, uint32_t*,
unsigned, const uint16_t*,
unsigned, unsigned, unsigned))dylib_proc(g_extern.filter.lib, "filter_render");
if (!g_extern.filter.psize || !g_extern.filter.prender)
{
SSNES_ERR("Failed to find functions in filter...\n");
dylib_close(g_extern.filter.lib);
g_extern.filter.lib = NULL;
return;
}
g_extern.filter.active = true;
unsigned width = 512;
unsigned height = 512;
g_extern.filter.psize(&width, &height);
unsigned pow2_x = next_pow2(ceil(width));
unsigned pow2_y = next_pow2(ceil(height));
unsigned maxsize = pow2_x > pow2_y ? pow2_x : pow2_y;
g_extern.filter.scale = maxsize / 256;
g_extern.filter.buffer = malloc(256 * 256 * g_extern.filter.scale * g_extern.filter.scale * sizeof(uint32_t));
g_extern.filter.pitch = 256 * g_extern.filter.scale * sizeof(uint32_t);
assert(g_extern.filter.buffer);
g_extern.filter.colormap = malloc(32768 * sizeof(uint32_t));
assert(g_extern.filter.colormap);
// Set up conversion map from 16-bit XRGB1555 to 32-bit ARGB.
for (int i = 0; i < 32768; i++)
{
unsigned r = (i >> 10) & 31;
unsigned g = (i >> 5) & 31;
unsigned b = (i >> 0) & 31;
r = (r << 3) | (r >> 2);
g = (g << 3) | (g >> 2);
b = (b << 3) | (b >> 2);
g_extern.filter.colormap[i] = (r << 16) | (g << 8) | (b << 0);
}
}
#endif
static void deinit_filter(void)
{
if (g_extern.filter.active)
{
g_extern.filter.active = false;
dylib_close(g_extern.filter.lib);
g_extern.filter.lib = NULL;
free(g_extern.filter.buffer);
free(g_extern.filter.colormap);
}
}
#ifdef HAVE_XML
static void init_shader_dir(void)
{
if (!*g_settings.video.shader_dir)
return;
g_extern.shader_dir.elems = dir_list_new(g_settings.video.shader_dir, ".shader");
g_extern.shader_dir.size = 0;
g_extern.shader_dir.ptr = 0;
if (g_extern.shader_dir.elems)
{
while (g_extern.shader_dir.elems[g_extern.shader_dir.size])
{
SSNES_LOG("Found shader \"%s\"\n", g_extern.shader_dir.elems[g_extern.shader_dir.size]);
g_extern.shader_dir.size++;
}
}
}
static void deinit_shader_dir(void)
{
// It handles NULL, no worries :D
dir_list_free(g_extern.shader_dir.elems);
}
#endif
void init_video_input(void)
{
int scale = 2;
#ifdef HAVE_DYLIB
init_filter();
#endif
#ifdef HAVE_XML
init_shader_dir();
#endif
// We use at least 512x512 textures to accomodate for hi-res games.
unsigned scale = 2;
find_video_driver();
find_input_driver();
// We multiply scales with 2 to allow for hi-res games.
#if HAVE_FILTER
switch (g_settings.video.filter)
if (g_extern.filter.active)
scale = g_extern.filter.scale;
unsigned width;
unsigned height;
if (g_settings.video.fullscreen)
{
case FILTER_HQ2X:
scale = 4;
break;
case FILTER_HQ4X:
case FILTER_NTSC:
scale = 8;
break;
default:
break;
width = g_settings.video.fullscreen_x;
height = g_settings.video.fullscreen_y;
}
else
{
if (g_settings.video.force_aspect)
{
width = 224 * g_settings.video.xscale * g_settings.video.aspect_ratio;
height = 224 * g_settings.video.yscale;
}
else
{
width = 256 * g_settings.video.xscale;
height = 224 * g_settings.video.yscale;
}
}
#endif
video_info_t video = {
.width = (g_settings.video.fullscreen) ? g_settings.video.fullscreen_x : (296 * g_settings.video.xscale),
.height = (g_settings.video.fullscreen) ? g_settings.video.fullscreen_y : (224 * g_settings.video.yscale),
.width = width,
.height = height,
.fullscreen = g_settings.video.fullscreen,
.vsync = g_settings.video.vsync,
.force_aspect = g_settings.video.force_aspect,
.smooth = g_settings.video.smooth,
.input_scale = scale,
.rgb32 = g_extern.filter.active
};
const input_driver_t *tmp = driver.input;
@@ -282,6 +484,12 @@ void uninit_video_input(void)
if ( driver.input_data != driver.video_data && driver.input )
driver.input->free(driver.input_data);
deinit_filter();
#ifdef HAVE_XML
deinit_shader_dir();
#endif
}
driver_t driver;
+22 -6
View File
@@ -28,6 +28,7 @@
enum
{
SSNES_FAST_FORWARD_KEY = 0x666, // Hurr, durr
SSNES_FAST_FORWARD_HOLD_KEY,
SSNES_LOAD_STATE_KEY,
SSNES_SAVE_STATE_KEY,
SSNES_FULLSCREEN_TOGGLE_KEY,
@@ -39,8 +40,17 @@ enum
SSNES_REWIND,
SSNES_MOVIE_RECORD_TOGGLE,
SSNES_PAUSE_TOGGLE,
SSNES_RESET,
SSNES_SHADER_NEXT,
SSNES_SHADER_PREV,
SSNES_CHEAT_INDEX_PLUS,
SSNES_CHEAT_INDEX_MINUS,
SSNES_CHEAT_TOGGLE,
SSNES_SCREENSHOT,
SSNES_DSP_CONFIG
};
struct snes_keybind
{
int id;
@@ -51,18 +61,19 @@ struct snes_keybind
typedef struct video_info
{
int width;
int height;
unsigned width;
unsigned height;
bool fullscreen;
bool vsync;
bool force_aspect;
bool smooth;
int input_scale; // HQ2X => 2, HQ4X => 4, None => 1
bool rgb32; // Use 32-bit RGBA rather than native XBGR1555.
} video_info_t;
typedef struct audio_driver
{
void* (*init)(const char* device, int rate, int latency);
void* (*init)(const char* device, unsigned rate, unsigned latency);
ssize_t (*write)(void* data, const void* buf, size_t size);
bool (*stop)(void* data);
bool (*start)(void* data);
@@ -78,7 +89,6 @@ typedef struct audio_driver
#define AXIS_NEG_GET(x) (((uint32_t)(x) >> 16) & 0xFFFFU)
#define AXIS_POS_GET(x) ((uint32_t)(x) & 0xFFFFU)
#define AXIS_NONE (0xFFFFFFFFU)
#define NO_BTN ((uint16_t)0xFFFFU) // I hope no joypad will ever have this many buttons ... ;)
@@ -104,13 +114,14 @@ typedef struct input_driver
typedef struct video_driver
{
void* (*init)(video_info_t *video, const input_driver_t **input, void **input_data);
void* (*init)(const video_info_t *video, const input_driver_t **input, void **input_data);
// Should the video driver act as an input driver as well? :) The video init might preinitialize an input driver to override the settings in case the video driver relies on input driver for event handling, e.g.
bool (*frame)(void* data, const uint16_t* frame, unsigned width, unsigned height, unsigned pitch, const char *msg); // msg is for showing a message on the screen along with the video frame.
bool (*frame)(void* data, const void* frame, unsigned width, unsigned height, unsigned pitch, const char *msg); // msg is for showing a message on the screen along with the video frame.
void (*set_nonblock_state)(void* data, bool toggle); // Should we care about syncing to vblank? Fast forwarding.
// Is the window still active?
bool (*alive)(void *data);
bool (*focus)(void *data); // Does the window have focus?
bool (*xml_shader)(void *data, const char *path); // Sets XML-shader. Might not be implemented.
void (*free)(void* data);
const char *ident;
} video_driver_t;
@@ -146,8 +157,13 @@ extern const audio_driver_t audio_jack;
extern const audio_driver_t audio_sdl;
extern const audio_driver_t audio_xa;
extern const audio_driver_t audio_pulse;
extern const audio_driver_t audio_ext;
extern const video_driver_t video_gl;
extern const video_driver_t video_xvideo;
extern const video_driver_t video_sdl;
extern const video_driver_t video_ext;
extern const input_driver_t input_sdl;
extern const input_driver_t input_x;
////////////////////////////////////////////////
#endif
+96 -52
View File
@@ -18,6 +18,7 @@
#include "dynamic.h"
#include "general.h"
#include <string.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -25,31 +26,28 @@
#include <libsnes.hpp>
#ifdef HAVE_DYNAMIC
#ifdef _WIN32
#include <windows.h>
#define SYM(x) do { \
p##x = ((void*)GetProcAddress(lib_handle, #x)); \
if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \
} while(0)
#else
#include <dlfcn.h>
#define SYM(x) do { \
p##x = dlsym(lib_handle, #x); \
if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \
} while(0)
#endif // _WIN32
#endif // HAVE_DYNAMIC
#endif
#ifdef HAVE_DYNAMIC
#ifdef _WIN32
static HMODULE lib_handle;
#else
static void *lib_handle = NULL;
#endif
#define DLSYM(lib, x) dylib_proc(lib, #x)
#define SYM(type, x) do { \
p##x = (type)DLSYM(lib_handle, x); \
if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \
} while(0)
#define OPT_SYM(type, x) do { \
p##x = (type)DLSYM(lib_handle, x); \
} while(0)
static dylib_t lib_handle = NULL;
#endif
void (*psnes_init)(void);
void (*psnes_set_video_refresh)(snes_video_refresh_t);
@@ -57,8 +55,13 @@ void (*psnes_set_audio_sample)(snes_audio_sample_t);
void (*psnes_set_input_poll)(snes_input_poll_t);
void (*psnes_set_input_state)(snes_input_state_t);
void (*psnes_reset)(void);
void (*psnes_run)(void);
void (*psnes_cheat_reset)(void);
void (*psnes_cheat_set)(unsigned, bool, const char*);
const char *(*psnes_library_id)(void) = NULL;
unsigned (*psnes_library_revision_minor)(void);
unsigned (*psnes_library_revision_major)(void);
@@ -97,40 +100,45 @@ void (*psnes_term)(void);
static void load_dynamic(void)
{
SSNES_LOG("Loading dynamic libsnes from: \"%s\"\n", g_settings.libsnes);
#ifdef _WIN32
lib_handle = LoadLibrary(g_settings.libsnes);
#else
lib_handle = dlopen(g_settings.libsnes, RTLD_LAZY);
#endif
lib_handle = dylib_load(g_settings.libsnes);
if (!lib_handle)
{
SSNES_ERR("Failed to open dynamic library: \"%s\"\n", g_settings.libsnes);
exit(1);
}
SYM(snes_init);
SYM(snes_set_video_refresh);
SYM(snes_set_audio_sample);
SYM(snes_set_input_poll);
SYM(snes_set_input_state);
SYM(snes_library_revision_minor);
SYM(snes_library_revision_major);
SYM(snes_run);
SYM(snes_get_region);
SYM(snes_load_cartridge_normal);
SYM(snes_load_cartridge_super_game_boy);
SYM(snes_load_cartridge_bsx);
SYM(snes_load_cartridge_bsx_slotted);
SYM(snes_load_cartridge_sufami_turbo);
SYM(snes_set_controller_port_device);
SYM(snes_serialize_size);
SYM(snes_serialize);
SYM(snes_unserialize);
SYM(snes_set_cartridge_basename);
SYM(snes_get_memory_data);
SYM(snes_get_memory_size);
SYM(snes_unload_cartridge);
SYM(snes_term);
SYM(void (*)(void), snes_init);
SYM(void (*)(snes_video_refresh_t), snes_set_video_refresh);
SYM(void (*)(snes_audio_sample_t), snes_set_audio_sample);
SYM(void (*)(snes_input_poll_t), snes_set_input_poll);
SYM(void (*)(snes_input_state_t), snes_set_input_state);
OPT_SYM(const char *(*)(void), snes_library_id);
SYM(unsigned (*)(void), snes_library_revision_minor);
SYM(unsigned (*)(void), snes_library_revision_major);
SYM(void (*)(void), snes_cheat_reset);
SYM(void (*)(unsigned, bool, const char*), snes_cheat_set);
SYM(void (*)(void), snes_reset);
SYM(void (*)(void), snes_run);
SYM(bool (*)(void), snes_get_region);
SYM(bool (*)(const char*, const uint8_t*, unsigned), snes_load_cartridge_normal);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_super_game_boy);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_bsx);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_bsx_slotted);
SYM(bool (*)(const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned), snes_load_cartridge_sufami_turbo);
SYM(void (*)(bool, unsigned), snes_set_controller_port_device);
SYM(unsigned (*)(void), snes_serialize_size);
SYM(bool (*)(uint8_t*, unsigned), snes_serialize);
SYM(bool (*)(const uint8_t*, unsigned), snes_unserialize);
SYM(void (*)(const char*), snes_set_cartridge_basename);
SYM(uint8_t *(*)(unsigned), snes_get_memory_data);
SYM(unsigned (*)(unsigned), snes_get_memory_size);
SYM(void (*)(void), snes_unload_cartridge);
SYM(void (*)(void), snes_term);
}
#endif
@@ -148,6 +156,9 @@ static void set_statics(void)
SSYM(snes_set_input_state);
SSYM(snes_library_revision_minor);
SSYM(snes_library_revision_major);
SSYM(snes_cheat_reset);
SSYM(snes_cheat_set);
SSYM(snes_reset);
SSYM(snes_run);
SSYM(snes_get_region);
SSYM(snes_load_cartridge_normal);
@@ -169,6 +180,9 @@ static void set_statics(void)
void init_dlsym(void)
{
// Guarantee that we can do "dirty" casting. Every OS that this program supports should pass this ...
assert(sizeof(void*) == sizeof(void (*)(void)));
#ifdef HAVE_DYNAMIC
if (strlen(g_settings.libsnes) > 0)
load_dynamic();
@@ -186,12 +200,42 @@ void uninit_dlsym(void)
{
#ifdef HAVE_DYNAMIC
if (lib_handle)
{
#ifdef _WIN32
FreeLibrary(lib_handle);
#else
dlclose(lib_handle);
#endif
}
dylib_close(lib_handle);
#endif
}
// Platform independent dylib loading.
dylib_t dylib_load(const char *path)
{
#ifdef _WIN32
return LoadLibrary(path);
#else
return dlopen(path, RTLD_LAZY);
#endif
}
function_t dylib_proc(dylib_t lib, const char *proc)
{
#ifdef _WIN32
function_t sym = (function_t)GetProcAddress(lib, proc);
#else
// Dirty hack to workaround the non-legality of void* -> fn-pointer casts.
void *ptr_sym = dlsym(lib, proc);
function_t sym;
memcpy(&sym, &ptr_sym, sizeof(void*));
#endif
if (!sym)
SSNES_WARN("Failed to load symbol \"%s\"\n", proc);
return sym;
}
void dylib_close(dylib_t lib)
{
#ifdef _WIN32
FreeLibrary(lib);
#else
dlclose(lib);
#endif
}
+12 -1
View File
@@ -24,6 +24,13 @@
void init_dlsym(void);
void uninit_dlsym(void);
typedef void *dylib_t;
typedef void (*function_t)(void);
dylib_t dylib_load(const char *path);
void dylib_close(dylib_t lib);
function_t dylib_proc(dylib_t lib, const char *proc);
extern void (*psnes_init)(void);
extern void (*psnes_set_video_refresh)(snes_video_refresh_t);
@@ -31,6 +38,10 @@ extern void (*psnes_set_audio_sample)(snes_audio_sample_t);
extern void (*psnes_set_input_poll)(snes_input_poll_t);
extern void (*psnes_set_input_state)(snes_input_state_t);
extern void (*psnes_cheat_reset)(void);
extern void (*psnes_cheat_set)(unsigned, bool, const char*);
extern const char* (*psnes_library_id)(void);
extern unsigned (*psnes_library_revision_minor)(void);
extern unsigned (*psnes_library_revision_major)(void);
@@ -49,7 +60,6 @@ extern bool (*psnes_load_cartridge_sufami_turbo)(
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned);
extern void (*psnes_set_controller_port_device)(bool, unsigned);
extern bool (*psnes_get_region)(void);
@@ -58,6 +68,7 @@ extern unsigned (*psnes_serialize_size)(void);
extern bool (*psnes_serialize)(uint8_t*, unsigned);
extern bool (*psnes_unserialize)(const uint8_t*, unsigned);
extern void (*psnes_reset)(void);
extern void (*psnes_run)(void);
extern void (*psnes_set_cartridge_basename)(const char*);
+254 -107
View File
@@ -23,94 +23,24 @@
#include <string.h>
#include "dynamic.h"
#include "movie.h"
#include "ups.h"
#include "strl.h"
#ifdef HAVE_XML
#include "sha256.h"
#endif
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#else
#include <sys/types.h>
#include <dirent.h>
#endif
// Load SNES rom only. Applies a hack for headered ROMs.
static ssize_t read_rom_file(FILE* file, void** buf)
{
ssize_t ret;
if (file == NULL) // stdin
{
#ifdef _WIN32
setmode(0, O_BINARY);
#endif
SSNES_LOG("Reading ROM from stdin ...\n");
size_t buf_size = 0xFFFFF; // Some initial guesstimate.
size_t buf_ptr = 0;
char *rom_buf = malloc(buf_size);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
for(;;)
{
size_t ret = fread(rom_buf + buf_ptr, 1, buf_size - buf_ptr, stdin);
buf_ptr += ret;
// We've reached the end
if (buf_ptr < buf_size)
break;
rom_buf = realloc(rom_buf, buf_size * 2);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
buf_size *= 2;
}
if ((buf_ptr & 0x7fff) == 512)
{
memmove(rom_buf, rom_buf + 512, buf_ptr - 512);
buf_ptr -= 512;
}
*buf = rom_buf;
ret = buf_ptr;
}
else
{
fseek(file, 0, SEEK_END);
long length = ftell(file);
rewind(file);
if ((length & 0x7fff) == 512)
{
length -= 512;
fseek(file, 512, SEEK_SET);
}
void *rom_buf = malloc(length);
if ( rom_buf == NULL )
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
if ( fread(rom_buf, 1, length, file) < length )
{
SSNES_ERR("Didn't read whole file.\n");
free(rom_buf);
return -1;
}
*buf = rom_buf;
ret = length;
}
g_extern.cart_crc = crc32_calculate(*buf, ret);
return ret;
}
// Generic file loader.
static ssize_t read_file(const char *path, void **buf)
ssize_t read_file(const char *path, void **buf)
{
void *rom_buf = NULL;
FILE *file = fopen(path, "rb");
@@ -143,6 +73,121 @@ error:
return -1;
}
// Load SNES rom only. Applies a hack for headered ROMs.
static ssize_t read_rom_file(FILE* file, void** buf)
{
ssize_t ret = 0;
uint8_t *ret_buf = NULL;
if (file == NULL) // stdin
{
#ifdef _WIN32
setmode(0, O_BINARY);
#endif
SSNES_LOG("Reading ROM from stdin ...\n");
size_t buf_size = 0xFFFFF; // Some initial guesstimate.
size_t buf_ptr = 0;
uint8_t *rom_buf = malloc(buf_size);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
for(;;)
{
size_t ret = fread(rom_buf + buf_ptr, 1, buf_size - buf_ptr, stdin);
buf_ptr += ret;
// We've reached the end
if (buf_ptr < buf_size)
break;
rom_buf = realloc(rom_buf, buf_size * 2);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
buf_size *= 2;
}
ret_buf = rom_buf;
ret = buf_ptr;
}
else
{
fseek(file, 0, SEEK_END);
ret = ftell(file);
rewind(file);
void *rom_buf = malloc(ret);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
if (fread(rom_buf, 1, ret, file) < ret)
{
SSNES_ERR("Didn't read whole file.\n");
free(rom_buf);
return -1;
}
ret_buf = rom_buf;
}
// Patch with UPS.
ssize_t ups_patch_size;
void *ups_patch = NULL;
if (*g_extern.ups_name && (ups_patch_size = read_file(g_extern.ups_name, &ups_patch)) >= 0)
{
SSNES_LOG("Found UPS file in \"%s\", attempting to patch ...\n", g_extern.ups_name);
size_t target_size = ret * 4; // Just to be sure ...
uint8_t *patched_rom = malloc(target_size);
if (patched_rom)
{
ups_error_t err = ups_apply_patch(ups_patch, ups_patch_size, ret_buf, ret, patched_rom, &target_size);
if (err == UPS_SUCCESS)
{
free(ret_buf);
ret_buf = patched_rom;
ret = target_size;
SSNES_LOG("ROM patched successfully (UPS)!\n");
}
else
{
free(patched_rom);
SSNES_LOG("ROM failed to patch (UPS).\n");
}
free(ups_patch);
}
}
else if (*g_extern.ups_name)
SSNES_LOG("Could not find UPS patch in: \"%s\".\n", g_extern.ups_name);
// Remove copier header if present (512 first bytes).
if ((ret & 0x7fff) == 512)
{
memmove(ret_buf, ret_buf + 512, ret - 512);
ret -= 512;
}
g_extern.cart_crc = crc32_calculate(ret_buf, ret);
#ifdef HAVE_XML
sha256_hash(g_extern.sha256, ret_buf, ret);
SSNES_LOG("SHA256 sum: %s\n", g_extern.sha256);
#endif
*buf = ret_buf;
return ret;
}
// Dump stuff to file.
static bool dump_to_file(const char *path, const void *data, size_t size)
{
@@ -221,7 +266,7 @@ void save_ram_file(const char* path, int type)
size_t size = psnes_get_memory_size(type);
uint8_t *data = psnes_get_memory_data(type);
if ( data && size > 0 )
if (data && size > 0)
dump_to_file(path, data, size);
}
@@ -240,14 +285,7 @@ static bool load_sgb_rom(void)
goto error;
}
extra_rom = fopen(g_extern.gb_rom_path, "rb");
if (!extra_rom)
{
SSNES_ERR("Couldn't open GameBoy ROM!\n");
goto error;
}
if ((extra_rom_len = read_rom_file(extra_rom, &extra_rom_buf)) == -1)
if ((extra_rom_len = read_file(g_extern.gb_rom_path, &extra_rom_buf)) == -1)
{
SSNES_ERR("Cannot read GameBoy rom.\n");
goto error;
@@ -294,14 +332,7 @@ static bool load_bsx_rom(bool slotted)
goto error;
}
extra_rom = fopen(g_extern.bsx_rom_path, "rb");
if (!extra_rom)
{
SSNES_ERR("Couldn't open BSX game rom!\n");
goto error;
}
if ((extra_rom_len = read_rom_file(extra_rom, &extra_rom_buf)) == -1)
if ((extra_rom_len = read_file(g_extern.bsx_rom_path, &extra_rom_buf)) == -1)
{
SSNES_ERR("Cannot read BSX game rom.\n");
goto error;
@@ -368,16 +399,9 @@ static bool load_sufami_rom(void)
{
if (strlen(roms[i]) > 0)
{
extra_rom[i] = fopen(roms[i], "rb");
if (!extra_rom[i])
if ((extra_rom_len[i] = read_file(roms[i], &extra_rom_buf[i])) == -1)
{
SSNES_ERR("Couldn't open BSX game rom!\n");
goto error;
}
if ((extra_rom_len[i] = read_rom_file(extra_rom[i], &extra_rom_buf[i])) == -1)
{
SSNES_ERR("Cannot read BSX game rom.\n");
SSNES_ERR("Cannot read Sufami game rom.\n");
goto error;
}
}
@@ -482,3 +506,126 @@ bool init_rom_file(enum ssnes_game_type type)
return true;
}
// Yep, this is C alright ;)
char** dir_list_new(const char *dir, const char *ext)
{
size_t path_len = strlen(dir);
size_t cur_ptr = 0;
size_t cur_size = 32;
char **dir_list = NULL;
#ifdef _WIN32
WIN32_FIND_DATAW ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
size_t final_off = MAX_PATH + path_len + 2;
wchar_t wchar_buf[MAX_PATH + 1];
char utf8_buf[MAX_PATH + 3];
strlcpy(utf8_buf, dir, sizeof(utf8_buf));
strlcat(utf8_buf, "/*", sizeof(utf8_buf));
utf8_buf[MAX_PATH + 2] = '\0';
int ret = MultiByteToWideChar(CP_UTF8, 0, utf8_buf, strlen(utf8_buf), wchar_buf, MAX_PATH);
wchar_buf[ret] = 0;
hFind = FindFirstFileW(wchar_buf, &ffd);
if (hFind == INVALID_HANDLE_VALUE)
goto error;
#else
DIR *directory = NULL;
const struct dirent *entry = NULL;
size_t final_off = sizeof(entry->d_name) + path_len + 2;
directory = opendir(dir);
if (!directory)
goto error;
#endif
dir_list = calloc(cur_size, sizeof(char*));
if (!dir_list)
goto error;
#ifdef _WIN32 // Hard to read? Blame non-POSIX heathens!
do
#else
while ((entry = readdir(directory)))
#endif
{
// Not a perfect search of course, but hopefully good enough in practice.
#ifdef _WIN32
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
int ret = WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, wcslen(ffd.cFileName), utf8_buf, MAX_PATH, NULL, NULL);
utf8_buf[ret] = '\0';
if (ext && !strstr(utf8_buf, ext))
continue;
#else
if (ext && !strstr(entry->d_name, ext))
continue;
#endif
dir_list[cur_ptr] = malloc(final_off);
if (!dir_list[cur_ptr])
goto error;
strcpy(dir_list[cur_ptr], dir);
#ifdef _WIN32
dir_list[cur_ptr][path_len] = '\\';
strcpy(&dir_list[cur_ptr][path_len + 1], utf8_buf);
#else
dir_list[cur_ptr][path_len] = '/';
strcpy(&dir_list[cur_ptr][path_len + 1], entry->d_name);
#endif
dir_list[cur_ptr][final_off - 1] = '\0';
cur_ptr++;
if (cur_ptr + 1 == cur_size) // Need to reserve for NULL.
{
cur_size *= 2;
dir_list = realloc(dir_list, cur_size * sizeof(char*));
if (!dir_list)
goto error;
// Make sure it's all NULL'd out since we cannot rely on realloc to do this.
memset(dir_list + cur_ptr, 0, (cur_size - cur_ptr) * sizeof(char*));
}
}
#ifdef _WIN32
while (FindNextFileW(hFind, &ffd) != 0);
#endif
#ifdef _WIN32
FindClose(hFind);
#else
closedir(directory);
#endif
return dir_list;
error:
SSNES_ERR("Failed to open directory: \"%s\"\n", dir);
#ifdef _WIN32
if (hFind != INVALID_HANDLE_VALUE)
FindClose(hFind);
#else
if (directory)
closedir(directory);
#endif
dir_list_free(dir_list);
return NULL;
}
void dir_list_free(char **dir_list)
{
if (!dir_list)
return;
char **orig = dir_list;
while (*dir_list)
free(*dir_list++);
free(orig);
}
+6
View File
@@ -26,6 +26,8 @@
#include <sys/types.h>
#include "general.h"
ssize_t read_file(const char *path, void **buf);
bool load_state(const char* path);
bool save_state(const char* path);
@@ -34,4 +36,8 @@ void save_ram_file(const char* path, int type);
bool init_rom_file(enum ssnes_game_type type);
// Returns a NULL-terminated list of files in a directory with full paths.
char** dir_list_new(const char *dir, const char *ext);
void dir_list_free(char **dir_list);
#endif
+112 -8
View File
@@ -26,6 +26,11 @@
#include "message.h"
#include "rewind.h"
#include "movie.h"
#include "autosave.h"
#include "netplay.h"
#include "dynamic.h"
#include "cheats.h"
#include "audio/ext/ssnes_dsp.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -39,8 +44,18 @@
#define MAX_PLAYERS 5
#define MAX_BINDS 25 // Needs to be increased every time there are new binds added.
#define MAX_BINDS 34 // Needs to be increased every time there are new binds added.
#define SSNES_NO_JOYPAD 0xFFFF
enum ssnes_shader_type
{
SSNES_SHADER_CG,
SSNES_SHADER_BSNES,
SSNES_SHADER_AUTO,
SSNES_SHADER_NONE
};
// All config related settings go here.
struct settings
{
struct
@@ -54,16 +69,28 @@ struct settings
bool vsync;
bool smooth;
bool force_aspect;
bool crop_overscan;
float aspect_ratio;
char cg_shader_path[256];
char bsnes_shader_path[256];
unsigned filter;
char filter_path[256];
enum ssnes_shader_type shader_type;
bool render_to_texture;
double fbo_scale_x;
double fbo_scale_y;
char second_pass_shader[256];
bool second_pass_smooth;
char shader_dir[256];
char font_path[256];
unsigned font_size;
float msg_pos_x;
float msg_pos_y;
bool force_16bit;
char external_driver[256];
} video;
struct
@@ -77,6 +104,9 @@ struct settings
unsigned latency;
bool sync;
int src_quality;
char dsp_plugin[256];
char external_driver[256];
} audio;
struct
@@ -85,15 +115,20 @@ struct settings
struct snes_keybind binds[MAX_PLAYERS][MAX_BINDS];
float axis_threshold;
unsigned joypad_map[MAX_PLAYERS];
bool netplay_client_swap_input;
} input;
char libsnes[256];
char cheat_database[256];
char screenshot_directory[256];
bool rewind_enable;
unsigned rewind_buffer_size;
unsigned rewind_granularity;
bool pause_nonactive;
unsigned autosave_interval;
};
enum ssnes_game_type
@@ -105,6 +140,8 @@ enum ssnes_game_type
SSNES_CART_SUFAMI,
};
// All run-time- / command line flag-related globals go here.
struct global
{
bool verbose;
@@ -124,6 +161,8 @@ struct global
char gb_rom_path[256];
char bsx_rom_path[256];
char sufami_rom_path[2][256];
bool has_set_save_path;
bool has_set_state_path;
#ifdef HAVE_CONFIGFILE
char config_path[256];
@@ -136,6 +175,7 @@ struct global
char savefile_name_asrm[512];
char savefile_name_bsrm[512];
char savestate_name[256];
char ups_name[512];
unsigned state_slot;
@@ -157,26 +197,76 @@ struct global
float *outsamples;
int16_t *conv_outsamples;
dylib_t dsp_lib;
const ssnes_dsp_plugin_t *dsp_plugin;
void *dsp_handle;
} audio_data;
struct
{
bool active;
uint32_t *buffer;
uint32_t *colormap;
unsigned pitch;
dylib_t lib;
unsigned scale;
void (*psize)(unsigned *width, unsigned *height);
void (*prender)(uint32_t *colormap, uint32_t *output, unsigned outpitch,
const uint16_t *input, unsigned pitch, unsigned width, unsigned height);
} filter;
msg_queue_t *msg_queue;
// Rewind support.
state_manager_t *state_manager;
void *state_buf;
bool frame_is_reverse;
// Movie record support
bsv_movie_t *bsv_movie;
char bsv_movie_path[256];
bool bsv_movie_end;
bool bsv_movie_playback;
// Pausing support
bool is_paused;
// Autosave support.
autosave_t *autosave[2];
// Netplay.
netplay_t *netplay;
char netplay_server[256];
bool netplay_enable;
bool netplay_is_client;
unsigned netplay_sync_frames;
uint16_t netplay_port;
// FFmpeg record.
#ifdef HAVE_FFMPEG
ffemu_t *rec;
char record_path[256];
bool recording;
#endif
char title_buf[64];
struct
{
char** elems;
size_t size;
size_t ptr;
} shader_dir;
char sha256[64 + 1];
bool do_screenshot;
#ifdef HAVE_XML
cheat_manager_t *cheat;
#endif
};
void parse_config(void);
@@ -184,20 +274,34 @@ void parse_config(void);
extern struct settings g_settings;
extern struct global g_extern;
#define SSNES_LOG(msg, args...) do { \
#define SSNES_LOG(...) do { \
if (g_extern.verbose) \
fprintf(stderr, "SSNES: " msg, ##args); \
fprintf(stderr, "SSNES: " __VA_ARGS__); \
fflush(stderr); \
} while(0)
#define SSNES_ERR(msg, args...) do { \
fprintf(stderr, "SSNES [ERROR] :: " msg, ##args); \
#define SSNES_ERR(...) do { \
fprintf(stderr, "SSNES [ERROR] :: " __VA_ARGS__); \
fflush(stderr); \
} while(0)
#define SSNES_WARN(msg, args...) do { \
fprintf(stderr, "SSNES [WARN] :: " msg, ##args); \
#define SSNES_WARN(...) do { \
fprintf(stderr, "SSNES [WARN] :: " __VA_ARGS__); \
fflush(stderr); \
} while(0)
static inline uint32_t next_pow2(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
#endif
+314
View File
@@ -0,0 +1,314 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
// Loader for external API plugins.
#define SSNES_DLL_IMPORT
#include "ext/ssnes_video.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include "dynamic.h"
#include "general.h"
static bool g_input_dead = true;
static bool g_video_dead = true;
static dylib_t g_lib = NULL;
/////////// Input hook
typedef struct
{
const ssnes_input_driver_t *driver;
void *handle;
} input_ext_t;
static void* input_ext_init(void)
{
g_input_dead = false;
return calloc(1, sizeof(input_ext_t));
}
static void input_ext_poll(void *data)
{
input_ext_t *ext = data;
ext->driver->poll(ext->handle);
}
static int16_t input_ext_input_state(void *data, const struct snes_keybind **snes_keybinds, bool port, unsigned device, unsigned index, unsigned id)
{
input_ext_t *ext = data;
unsigned player = 0;
if (device == SNES_DEVICE_MULTITAP)
player = (port == SNES_PORT_1) ? 1 : index + 2;
else
player = (port == SNES_PORT_1) ? 1 : 2;
const struct snes_keybind *ssnes_bind = NULL;
for (unsigned i = 0; g_settings.input.binds[player - 1][i].id != -1; i++)
{
if (g_settings.input.binds[player - 1][i].id == id)
{
ssnes_bind = &g_settings.input.binds[player - 1][i];
break;
}
}
if (ssnes_bind)
{
struct ssnes_keybind bind = {
.key = ssnes_bind->key,
.joykey = ssnes_bind->joykey,
.joyaxis = ssnes_bind->joyaxis
};
return ext->driver->input_state(ext->handle, &bind, player);
}
else
return 0;
}
static bool input_ext_key_pressed(void *data, int key)
{
input_ext_t *ext = data;
const struct snes_keybind *ssnes_bind = NULL;
for (unsigned i = 0; g_settings.input.binds[0][i].id != -1; i++)
{
if (g_settings.input.binds[0][i].id == key)
{
ssnes_bind = &g_settings.input.binds[0][i];
break;
}
}
if (ssnes_bind)
{
struct ssnes_keybind bind = {
.key = ssnes_bind->key,
.joykey = ssnes_bind->joykey,
.joyaxis = ssnes_bind->joyaxis
};
return ext->driver->input_state(ext->handle, &bind, 1);
}
else
return false;
}
static void input_ext_free(void *data)
{
input_ext_t *ext = data;
if (ext)
{
if (ext->driver && ext->handle)
ext->driver->free(ext->handle);
if (g_video_dead)
{
dylib_close(g_lib);
g_lib = NULL;
}
g_input_dead = true;
free(ext);
}
}
static const input_driver_t input_ext = {
.init = input_ext_init,
.poll = input_ext_poll,
.input_state = input_ext_input_state,
.key_pressed = input_ext_key_pressed,
.free = input_ext_free,
.ident = "ext"
};
//////////// Video hook
typedef struct
{
const ssnes_video_driver_t *driver;
void *handle;
} ext_t;
static void video_ext_free(void *data)
{
ext_t *ext = data;
if (ext)
{
if (ext->driver && ext->handle)
ext->driver->free(ext->handle);
if (g_input_dead)
{
dylib_close(g_lib);
g_lib = NULL;
}
g_video_dead = true;
free(ext);
}
}
static bool video_ext_focus(void *data)
{
ext_t *ext = data;
return ext->driver->focus(ext->handle);
}
static bool video_ext_alive(void *data)
{
ext_t *ext = data;
return ext->driver->alive(ext->handle);
}
static void video_ext_set_nonblock_state(void *data, bool state)
{
ext_t *ext = data;
ext->driver->set_nonblock_state(ext->handle, state);
}
static bool video_ext_frame(void *data, const void *frame, unsigned width, unsigned height, unsigned pitch, const char *msg)
{
ext_t *ext = data;
return ext->driver->frame(ext->handle, frame, width, height, pitch, msg);
}
static void* setup_input(ext_t *ext, const ssnes_input_driver_t *driver)
{
int joypad_index[5];
for (unsigned i = 0; i < 5; i++)
joypad_index[i] = g_settings.input.joypad_map[i] == SSNES_NO_JOYPAD ? -1 : g_settings.input.joypad_map[i];
void *handle = driver->init(joypad_index, g_settings.input.axis_threshold);
if (!handle)
return NULL;
input_ext_t *wrap_handle = input_ext.init();
if (!wrap_handle)
return NULL;
wrap_handle->handle = handle;
wrap_handle->driver = driver;
return wrap_handle;
}
static bool setup_video(ext_t *ext, const video_info_t *video, const input_driver_t **input, void **input_data)
{
SSNES_LOG("Loaded driver: \"%s\"\n", ext->driver->ident ? ext->driver->ident : "Unknown");
if (SSNES_GRAPHICS_API_VERSION != ext->driver->api_version)
{
SSNES_ERR("API version mismatch detected!\n");
SSNES_ERR("Required API version: %d, Library version: %d\n", SSNES_GRAPHICS_API_VERSION, ext->driver->api_version);
return false;
}
ssnes_video_info_t info = {
.width = video->width,
.height = video->height,
.fullscreen = video->fullscreen,
.vsync = video->vsync,
.force_aspect = video->force_aspect,
.aspect_ratio = g_settings.video.aspect_ratio,
.smooth = video->smooth,
.input_scale = video->input_scale,
.color_format = video->rgb32 ? SSNES_COLOR_FORMAT_ARGB8888 : SSNES_COLOR_FORMAT_XRGB1555,
.xml_shader = g_settings.video.bsnes_shader_path,
.cg_shader = g_settings.video.cg_shader_path,
.ttf_font = *g_settings.video.font_path ? g_settings.video.font_path : NULL,
.ttf_font_size = g_settings.video.font_size
};
const ssnes_input_driver_t *input_driver = NULL;
ext->handle = ext->driver->init(&info, &input_driver);
if (!ext->handle)
return false;
*input = input_driver ? &input_ext : NULL;
if (input_driver)
*input_data = setup_input(ext, input_driver);
else
*input_data = NULL;
return true;
}
static void* video_ext_init(const video_info_t *video, const input_driver_t **input, void **input_data)
{
ext_t *ext = calloc(1, sizeof(*ext));
if (!ext)
return NULL;
if (!(*g_settings.video.external_driver))
{
SSNES_ERR("External driver needs video_external_driver path to be set!\n");
goto error;
}
g_lib = dylib_load(g_settings.video.external_driver);
if (!g_lib)
{
SSNES_ERR("Failed to open library: \"%s\"\n", g_settings.video.external_driver);
goto error;
}
const ssnes_video_driver_t* (*video_init)(void) =
(const ssnes_video_driver_t *(*)(void))dylib_proc(g_lib, "ssnes_video_init");
if (!video_init)
{
SSNES_ERR("Couldn't find function ssnes_video_init in library ...\n");
goto error;
}
ext->driver = video_init();
if (!ext->driver)
{
SSNES_ERR("External driver returned invalid driver handle.\n");
goto error;
}
if (!setup_video(ext, video, input, input_data))
{
SSNES_ERR("Failed to start driver.\n");
goto error;
}
g_video_dead = false;
return ext;
error:
video_ext_free(ext);
return NULL;
}
const video_driver_t video_ext = {
.init = video_ext_init,
.frame = video_ext_frame,
.alive = video_ext_alive,
.set_nonblock_state = video_ext_set_nonblock_state,
.focus = video_ext_focus,
.free = video_ext_free,
.ident = "ext"
};
+251
View File
@@ -0,0 +1,251 @@
/////
// API header for external SSNES video and input plugins.
//
//
#ifndef __SSNES_VIDEO_DRIVER_H
#define __SSNES_VIDEO_DRIVER_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#ifdef SSNES_DLL_IMPORT
#define SSNES_API_EXPORT __declspec(dllimport)
#else
#define SSNES_API_EXPORT __declspec(dllexport)
#endif
#define SSNES_API_CALLTYPE __cdecl
#else
#define SSNES_API_EXPORT
#define SSNES_API_CALLTYPE
#endif
#define SSNES_GRAPHICS_API_VERSION 1
// Since we don't want to rely on C++ or C99 for a proper boolean type,
// and make sure return semantics are perfectly clear ... ;)
#ifndef SSNES_OK
#define SSNES_OK 1
#endif
#ifndef SSNES_ERROR
#define SSNES_ERROR 0
#endif
#ifndef SSNES_TRUE
#define SSNES_TRUE 1
#endif
#ifndef SSNES_FALSE
#define SSNES_FALSE 0
#endif
#define SSNES_COLOR_FORMAT_XRGB1555 0
#define SSNES_COLOR_FORMAT_ARGB8888 1
typedef struct ssnes_video_info
{
// Width of window.
// If fullscreen mode is requested,
// a width of 0 means the resolution of the desktop should be used.
unsigned width;
// Height of window.
// If fullscreen mode is requested,
// a height of 0 means the resolutiof the desktop should be used.
unsigned height;
// If true, start the window in fullscreen mode.
int fullscreen;
// If true, VSync should be enabled.
int vsync;
// If true, the output image should have the aspect ratio
// as set in aspect_ratio.
int force_aspect;
// Aspect ratio. Only takes effect if force_aspect is enabled.
float aspect_ratio;
// Requests that the image is smoothed,
// using bilinear filtering or otherwise.
// If this cannot be implemented efficiently, this can be disregarded.
// If smooth is false, nearest-neighbor scaling is requested.
int smooth;
// input_scale defines the maximum size of the picture that will
// ever be used with the frame callback.
// The maximum resolution is a multiple of 256x256 size,
// so an input scale of 2
// means you should allocate a texture or of 512x512.
// It is normally set to 2 due to the possibility of
// pseudo-hires on the SNES.
// CPU filters or otherwise might increase this value.
unsigned input_scale;
// Defines the coloring format used of the input frame.
// XRGB1555 format is 16-bit and has byte ordering: 0RRRRRGGGGGBBBBB,
// in native endian.
// ARGB8888 is AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB, native endian.
// Alpha channel should be disregarded.
int color_format;
// If non-NULL, requests the use of an XML shader.
// Can be disregarded.
const char *xml_shader;
// If non-NULL, requests the use of a Cg shader.
// Can be disregarded.
// If both are non-NULL,
// Cg or XML could be used at the discretion of the plugin.
const char *cg_shader;
// Requestes that a certain
// TTF font is used for rendering messages to the screen.
// Can be disregarded.
const char *ttf_font;
unsigned ttf_font_size;
} ssnes_video_info_t;
// Some convenience macros.
// Extract which axes to test for in negative or positive direction.
// May be equal to SSNES_NO_AXIS, which means testing should not occur.
#define SSNES_AXIS_NEG_GET(x) (((unsigned)(x) >> 16) & 0xFFFFU)
#define SSNES_AXIS_POS_GET(x) ((unsigned)(x) & 0xFFFFU)
// I hope no joypad will ever have this many buttons or axes ... ;)
// If joykey is this value, do not check that button.
#define SSNES_NO_AXIS ((unsigned short)0xFFFFU)
#define SSNES_NO_BTN ((unsigned short)0xFFFFU)
// Masks to test on joykey which hat direction is to be tested for.
#define SSNES_HAT_UP_MASK (1 << 15)
#define SSNES_HAT_DOWN_MASK (1 << 14)
#define SSNES_HAT_LEFT_MASK (1 << 13)
#define SSNES_HAT_RIGHT_MASK (1 << 12)
#define SSNES_HAT_MAP(x, hat) ((x & ((1 << 12) - 1)) | hat)
#define SSNES_HAT_MASK (SSNES_HAT_UP_MASK | SSNES_HAT_DOWN_MASK | \
SSNES_HAT_LEFT_MASK | SSNES_HAT_RIGHT_MASK)
// Test this on the joykey. If true, we want to test for a joypad hat
// rather than a button.
#define SSNES_GET_HAT_DIR(x) (x & SSNES_HAT_MASK)
// Gets the joypad hat to be tested for.
// Only valid when SSNES_GET_HAT_DIR() returns true.
#define SSNES_GET_HAT(x) (x & (~SSNES_HAT_MASK))
// key, joykey and joyaxis are all checked at the same time.
// If any one of these are pressed, return 1 in state callback.
struct ssnes_keybind
{
// If analog_x is true, we request an analog device to be polled
// rather than normal keys.
// The returned value should be the delta of
// last frame and current frame in the X-axis.
int analog_x;
// If analog_y is true, we request an analog device to be polled
// rather than normal keys.
// The returned value should be the delta of
// last frame and current frame in the Y-axis.
int analog_y;
// Keyboard key. The key values use the SDL keysyms,
// which probably need to be transformed to the native format.
unsigned short key;
// Joypad key. Joypad POV (hats) are embedded into this key as well.
unsigned short joykey;
// Joypad axis. Negative and positive axes are embedded into this variable.
unsigned joyaxis;
};
typedef struct ssnes_input_driver
{
// Inits input driver.
// Joypad index denotes which joypads are desired for the various players.
// Should an entry be negative,
// do not open joypad for that player.
// Threshold states the minimum offset that a joypad axis
// has to be held for it to be registered.
// The range of this is [0, 1],
// where 0 means any displacement will register,
// and 1 means the axis has to be pressed all the way to register.
void* (*init)(const int joypad_index[5], float axis_threshold);
// Polls input. Called once every frame.
void (*poll)(void* data);
// Queries input state for a certain key on a certain player.
// Players are 1 - 5.
// For digital inputs, pressed key is 1, not pressed key is 0.
// Analog values have same range as a signed 16-bit integer.
int (*input_state)(void* data, const struct ssnes_keybind *bind,
unsigned player);
// Frees the input struct.
void (*free)(void* data);
// Human readable indentification string.
const char *ident;
} ssnes_input_driver_t;
typedef struct ssnes_video_driver
{
// Inits the video driver. Returns an opaque handle pointer to the driver.
// Returns NULL on error.
//
// Should the video driver request that a certain input driver is used,
// it is possible to set the driver to *input.
// If no certain driver is desired, set *input to NULL.
void* (*init)(const ssnes_video_info_t *video,
const ssnes_input_driver_t **input);
// Updates frame on the screen.
// Frame can be either XRGB1555 or ARGB32 format
// depending on rgb32 setting in ssnes_video_info_t.
// Pitch is the distance in bytes between two scanlines in memory.
//
// When msg is non-NULL,
// it's a message that should be displayed to the user.
int (*frame)(void* data, const void* frame,
unsigned width, unsigned height, unsigned pitch, const char *msg);
// Requests nonblocking operation.
// True = VSync is turned off.
// False = VSync is turned on.
void (*set_nonblock_state)(void* data, int toggle);
// This must return false when the user exits the emulator.
int (*alive)(void *data);
// Does the window have focus?
int (*focus)(void *data);
// Frees the video driver.
void (*free)(void* data);
// A human-readable identification of the video driver.
const char *ident;
// Needs to be defined to SSNES_GRAPHICS_API_VERSION.
// This is used to detect API mismatches.
int api_version;
} ssnes_video_driver_t;
// Called by SSNES on startup to get a driver handle.
// This is NOT dynamically allocated.
SSNES_API_EXPORT const ssnes_video_driver_t* SSNES_API_CALLTYPE
ssnes_video_init(void);
#ifdef __cplusplus
}
#endif
#endif
+58
View File
@@ -0,0 +1,58 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "gfx_common.h"
#include "general.h"
#include <sys/time.h>
static float tv_to_fps(const struct timeval *tv, const struct timeval *new_tv, int frames)
{
float time = new_tv->tv_sec - tv->tv_sec + (new_tv->tv_usec - tv->tv_usec)/1000000.0;
return frames/time;
}
bool gfx_window_title(char *buf, size_t size)
{
static int frames = 0;
static struct timeval tv;
struct timeval new_tv;
bool ret = false;
if (frames == 0)
{
gettimeofday(&tv, NULL);
snprintf(buf, size, "%s", g_extern.title_buf);
ret = true;
}
if ((frames % 180) == 0 && frames > 0)
{
gettimeofday(&new_tv, NULL);
struct timeval tmp_tv = tv;
tv = new_tv;
float fps = tv_to_fps(&tmp_tv, &new_tv, 180);
snprintf(buf, size, "%s || FPS: %6.1f || Frames: %d", g_extern.title_buf, fps, frames);
ret = true;
}
frames++;
return ret;
}
+26
View File
@@ -0,0 +1,26 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GFX_COMMON_H
#define __GFX_COMMON_H
#include <stddef.h>
#include <stdbool.h>
bool gfx_window_title(char *buf, size_t size);
#endif
+836 -179
View File
File diff suppressed because it is too large. Load diff
+67 -1
View File
@@ -19,6 +19,20 @@
#define __GL_COMMON_H
#include "general.h"
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#else
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#endif
static inline bool gl_check_error(void)
{
@@ -29,7 +43,7 @@ static inline bool gl_check_error(void)
SSNES_ERR("GL: Invalid enum.\n");
break;
case GL_INVALID_VALUE:
SSNES_ERR("GL: Invalid value.\n");
SSNES_ERR("GL: Invalid value. (You're not alone.)\n");
break;
case GL_INVALID_OPERATION:
SSNES_ERR("GL: Invalid operation.\n");
@@ -56,4 +70,56 @@ static inline bool gl_check_error(void)
return false;
}
struct gl_fbo_rect
{
unsigned img_width;
unsigned img_height;
unsigned max_img_width;
unsigned max_img_height;
unsigned width;
unsigned height;
};
enum gl_scale_type
{
SSNES_SCALE_ABSOLUTE,
SSNES_SCALE_INPUT,
SSNES_SCALE_VIEWPORT
};
struct gl_fbo_scale
{
enum gl_scale_type type_x;
enum gl_scale_type type_y;
float scale_x;
float scale_y;
unsigned abs_x;
unsigned abs_y;
bool valid;
};
struct gl_tex_info
{
GLuint tex;
float input_size[2];
float tex_size[2];
float coord[8];
};
// Not legal to cast void* to fn-pointer. Need dirty hack to be compilant.
// sizeof(void*) == sizeof(void (*)(void)) is asserted earlier.
#define SDL_SYM_WRAP(sym, symbol) { \
void *sym__ = SDL_GL_GetProcAddress(symbol); \
memcpy(&(sym), &sym__, sizeof(void*)); \
}
// Windows ... <_<
#if (defined(HAVE_XML) || defined(HAVE_CG)) && defined(_WIN32)
extern PFNGLCLIENTACTIVETEXTUREPROC pglClientActiveTexture;
extern PFNGLACTIVETEXTUREPROC pglActiveTexture;
#else
#define pglClientActiveTexture glClientActiveTexture
#define pglActiveTexture glActiveTexture
#endif
#endif
+142
View File
@@ -0,0 +1,142 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "image.h"
#include "file.h"
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "general.h"
#ifdef HAVE_IMLIB
#include <Imlib2.h>
bool texture_image_load(const char *path, struct texture_image *out_img)
{
Imlib_Image img;
img = imlib_load_image(path);
if (!img)
return false;
imlib_context_set_image(img);
out_img->width = imlib_image_get_width();
out_img->height = imlib_image_get_height();
size_t size = out_img->width * out_img->height * sizeof(uint32_t);
out_img->pixels = malloc(size);
if (!out_img->pixels)
{
imlib_free_image();
return false;
}
const uint32_t *read = imlib_image_get_data_for_reading_only();
// Convert ARGB -> RGBA.
for (unsigned i = 0; i < size / sizeof(uint32_t); i++)
out_img->pixels[i] = (read[i] >> 24) | (read[i] << 8);
imlib_free_image();
return true;
}
#else
bool texture_image_load(const char *path, struct texture_image *out_img)
{
// TODO: Check more gracefully.
if (!strstr(path, ".tga"))
return false;
void *raw_buf = NULL;
ssize_t len = read_file(path, &raw_buf);
if (len < 0)
return false;
uint8_t *buf = raw_buf;
if (buf[2] != 2) // Uncompressed RGB
{
free(buf);
return false;
}
unsigned width = 0;
unsigned height = 0;
uint8_t info[6];
memcpy(info, buf + 12, 6);
width = info[0] + ((unsigned)info[1] * 256);
height = info[2] + ((unsigned)info[3] * 256);
unsigned bits = info[4];
SSNES_LOG("Loaded TGA: (%ux%u @ %u bpp)\n", width, height, bits);
unsigned size = width * height * sizeof(uint32_t);
out_img->pixels = malloc(size);
out_img->width = width;
out_img->height = height;
if (!out_img->pixels)
{
free(buf);
return false;
}
const uint8_t *tmp = buf + 18;
if (bits == 32)
{
for (unsigned i = 0; i < width * height; i++)
{
uint32_t r = tmp[i * 4 + 2];
uint32_t g = tmp[i * 4 + 1];
uint32_t b = tmp[i * 4 + 0];
uint32_t a = tmp[i * 4 + 3];
out_img->pixels[i] = (r << 24) | (g << 16) | (b << 8) | a;
}
}
else if (bits == 24)
{
for (unsigned i = 0; i < width * height; i++)
{
uint32_t r = tmp[i * 3 + 2];
uint32_t g = tmp[i * 3 + 1];
uint32_t b = tmp[i * 3 + 0];
uint32_t a = 0xff;
out_img->pixels[i] = (r << 24) | (g << 16) | (b << 8) | a;
}
}
else
{
free(buf);
free(out_img->pixels);
out_img->pixels = NULL;
return false;
}
free(buf);
return true;
}
#endif
+34
View File
@@ -0,0 +1,34 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_IMAGE_H
#define __SSNES_IMAGE_H
#include <stdint.h>
#include <stdbool.h>
struct texture_image
{
unsigned width;
unsigned height;
uint32_t *pixels;
};
bool texture_image_load(const char *path, struct texture_image* img);
#endif
+503
View File
@@ -0,0 +1,503 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "SDL.h"
#include "driver.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "general.h"
#include "input/ssnes_sdl_input.h"
#include "gfx_common.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_FREETYPE
#include "fonts.h"
#endif
static void convert_15bit_15bit_direct(uint16_t *out, unsigned outpitch, const uint16_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt);
static void convert_32bit_32bit_direct(uint32_t *out, unsigned outpitch, const uint32_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt);
static void convert_15bit_15bit_shift(uint16_t *out, unsigned outpitch, const uint16_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt);
static void convert_32bit_32bit_shift(uint32_t *out, unsigned outpitch, const uint32_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt);
typedef struct sdl_video sdl_video_t;
struct sdl_video
{
SDL_Surface *screen, *buffer;
bool quitting;
bool rgb32;
bool upsample;
void (*convert_15_func)(uint16_t*, unsigned, const uint16_t*, unsigned, unsigned, unsigned, const SDL_PixelFormat*);
void (*convert_32_func)(uint32_t*, unsigned, const uint32_t*, unsigned, unsigned, unsigned, const SDL_PixelFormat*);
#ifdef HAVE_FREETYPE
font_renderer_t *font;
#endif
};
static void sdl_gfx_free(void *data)
{
sdl_video_t *vid = data;
if (!vid)
return;
if (vid->buffer)
SDL_FreeSurface(vid->buffer);
SDL_QuitSubSystem(SDL_INIT_VIDEO);
#ifdef HAVE_FREETYPE
if (vid->font)
font_renderer_free(vid->font);
#endif
free(vid);
}
static void sdl_init_font(sdl_video_t *vid, const char *font_path, unsigned font_size)
{
#ifdef HAVE_FREETYPE
if (*font_path)
{
vid->font = font_renderer_new(font_path, font_size);
if (!vid->font)
SSNES_WARN("Failed to init font.\n");
}
#else
(void)vid;
(void)font_path;
(void)font_size;
#endif
}
// Not very optimized, but hey :D
static void sdl_render_msg_15(sdl_video_t *vid, SDL_Surface *buffer, const char *msg, unsigned width, unsigned height, const SDL_PixelFormat *fmt)
{
#ifdef HAVE_FREETYPE
if (!vid->font)
return;
struct font_output_list out;
font_renderer_msg(vid->font, msg, &out);
struct font_output *head = out.head;
int base_x = g_settings.video.msg_pos_x * width;
int base_y = (1.0 - g_settings.video.msg_pos_y) * height;
unsigned rshift = fmt->Rshift;
unsigned gshift = fmt->Gshift;
unsigned bshift = fmt->Bshift;
while (head)
{
int rbase_x = base_x + head->off_x;
int rbase_y = base_y - head->off_y;
if (rbase_y >= 0)
{
for (int y = 0; y < head->height && (y + rbase_y) < height; y++)
{
if (rbase_x < 0)
continue;
const uint8_t *a = head->output + head->pitch * y;
uint16_t *out = (uint16_t*)buffer->pixels + (rbase_y - head->height + y) * (buffer->pitch >> 1) + rbase_x;
for (int x = 0; x < head->width && (x + rbase_x) < width; x++)
{
unsigned blend = a[x];
unsigned out_pix = out[x];
unsigned r = (out_pix >> rshift) & 0x1f;
unsigned g = (out_pix >> gshift) & 0x1f;
unsigned b = (out_pix >> bshift) & 0x1f;
unsigned out_r = (r * (256 - blend) + 0x1f * blend) >> 8;
unsigned out_g = (g * (256 - blend) + 0x1f * blend) >> 8;
unsigned out_b = (b * (256 - blend) + 0x1f * blend) >> 8;
out[x] = (out_r << rshift) | (out_g << gshift) | (out_b << bshift);
}
}
}
head = head->next;
}
font_renderer_free_output(&out);
#else
(void)vid;
(void)buffer;
(void)msg;
(void)width;
(void)height;
#endif
}
static void sdl_render_msg_32(sdl_video_t *vid, SDL_Surface *buffer, const char *msg, unsigned width, unsigned height, const SDL_PixelFormat *fmt)
{
#ifdef HAVE_FREETYPE
if (!vid->font)
return;
struct font_output_list out;
font_renderer_msg(vid->font, msg, &out);
struct font_output *head = out.head;
int base_x = g_settings.video.msg_pos_x * width;
int base_y = (1.0 - g_settings.video.msg_pos_y) * height;
unsigned rshift = fmt->Rshift;
unsigned gshift = fmt->Gshift;
unsigned bshift = fmt->Bshift;
while (head)
{
int rbase_x = base_x + head->off_x;
int rbase_y = base_y - head->off_y;
if (rbase_y >= 0)
{
for (int y = 0; y < head->height && (y + rbase_y) < height; y++)
{
if (rbase_x < 0)
continue;
const uint8_t *a = head->output + head->pitch * y;
uint32_t *out = (uint32_t*)buffer->pixels + (rbase_y - head->height + y) * (buffer->pitch >> 2) + rbase_x;
for (int x = 0; x < head->width && (x + rbase_x) < width; x++)
{
unsigned blend = a[x];
unsigned out_pix = out[x];
unsigned r = (out_pix >> rshift) & 0xff;
unsigned g = (out_pix >> gshift) & 0xff;
unsigned b = (out_pix >> bshift) & 0xff;
unsigned out_r = (r * (256 - blend) + 0xff * blend) >> 8;
unsigned out_g = (g * (256 - blend) + 0xff * blend) >> 8;
unsigned out_b = (b * (256 - blend) + 0xff * blend) >> 8;
out[x] = (out_r << rshift) | (out_g << gshift) | (out_b << bshift);
}
}
}
head = head->next;
}
font_renderer_free_output(&out);
#else
(void)vid;
(void)buffer;
(void)msg;
(void)width;
(void)height;
#endif
}
static void* sdl_gfx_init(const video_info_t *video, const input_driver_t **input, void **input_data)
{
SDL_InitSubSystem(SDL_INIT_VIDEO);
sdl_video_t *vid = calloc(1, sizeof(*vid));
if (!vid)
return NULL;
const SDL_VideoInfo *video_info = SDL_GetVideoInfo();
assert(video_info);
unsigned full_x = video_info->current_w;
unsigned full_y = video_info->current_h;
SSNES_LOG("Detecting desktop resolution %ux%u.\n", full_x, full_y);
if (!video->fullscreen)
SSNES_LOG("Creating window @ %ux%u\n", video->width, video->height);
vid->screen = SDL_SetVideoMode(video->width, video->height, (g_settings.video.force_16bit || !video->rgb32) ? 15 : 32, SDL_HWSURFACE | SDL_HWACCEL | SDL_DOUBLEBUF | (video->fullscreen ? SDL_FULLSCREEN : 0));
if (!vid->screen && !g_settings.video.force_16bit && !video->rgb32)
{
vid->upsample = true;
vid->screen = SDL_SetVideoMode(video->width, video->height, 32, SDL_HWSURFACE | SDL_HWACCEL | SDL_DOUBLEBUF | (video->fullscreen ? SDL_FULLSCREEN : 0));
SSNES_WARN("SDL: 15-bit colors failed, attempting 32-bit colors.\n");
}
if (!vid->screen)
{
SSNES_ERR("Failed to init SDL surface: %s\n", SDL_GetError());
goto error;
}
SDL_ShowCursor(SDL_DISABLE);
const SDL_PixelFormat *fmt = vid->screen->format;
if (!g_settings.video.force_16bit && (video->rgb32 || vid->upsample))
{
SSNES_LOG("SDL: Creating 32-bit buffer.\n");
vid->buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, 256 * video->input_scale, 256 * video->input_scale, 32,
fmt->Rmask, fmt->Bmask, fmt->Gmask, fmt->Amask);
}
else
{
SSNES_LOG("SDL: Creating 15-bit buffer.\n");
vid->buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, 256 * video->input_scale, 256 * video->input_scale, 15,
fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask);
}
SSNES_LOG("[Debug]: SDL Pixel format: Rshift = %u, Gshift = %u, Bshift = %u\n",
(unsigned)fmt->Rshift, (unsigned)fmt->Gshift, (unsigned)fmt->Bshift);
if (!vid->buffer)
{
SSNES_ERR("SDL_CreateRGBSurface failed: %s\n", SDL_GetError());
goto error;
}
sdl_input_t *sdl_input = input_sdl.init();
if (sdl_input)
{
sdl_input->quitting = &vid->quitting;
*input = &input_sdl;
*input_data = sdl_input;
}
else
*input = NULL;
vid->rgb32 = video->rgb32;
sdl_init_font(vid, g_settings.video.font_path, g_settings.video.font_size);
if (fmt->Rshift == 10 && fmt->Gshift == 5 && fmt->Bshift == 0)
{
SSNES_LOG("SDL: 15-bit format matches. Fast blit.\n");
vid->convert_15_func = convert_15bit_15bit_direct;
}
else
{
SSNES_LOG("SDL: 15-bit format does not match. Needs conversion.\n");
vid->convert_15_func = convert_15bit_15bit_shift;
}
if (fmt->Rshift == 16 && fmt->Gshift == 8 && fmt->Bshift == 0)
{
SSNES_LOG("SDL: 32-bit format matches. Fast blit.\n");
vid->convert_32_func = convert_32bit_32bit_direct;
}
else
{
SSNES_LOG("SDL: 32-bit format does not match. Needs conversion.\n");
vid->convert_32_func = convert_32bit_32bit_shift;
}
return vid;
error:
sdl_gfx_free(vid);
return NULL;
}
static inline uint16_t conv_pixel_32_15(uint32_t pix, const SDL_PixelFormat *fmt)
{
uint16_t r = ((pix & 0x00f80000) >> 19) << fmt->Rshift;
uint16_t g = ((pix & 0x0000f800) >> 11) << fmt->Gshift;
uint16_t b = ((pix & 0x000000f8) >> 3) << fmt->Bshift;
return r | g | b;
}
static inline uint32_t conv_pixel_15_32(uint16_t pix, const SDL_PixelFormat *fmt)
{
uint32_t r = ((pix >> 10) & 0x1f) << (fmt->Rshift + 3);
uint32_t g = ((pix >> 5) & 0x1f) << (fmt->Gshift + 3);
uint32_t b = ((pix >> 0) & 0x1f) << (fmt->Bshift + 3);
return r | g | b;
}
static void convert_32bit_15bit(uint16_t *out, unsigned outpitch, const uint32_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt)
{
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
out[x] = conv_pixel_32_15(input[x], fmt);
out += outpitch >> 1;
input += pitch >> 2;
}
}
static void convert_15bit_32bit(uint32_t *out, unsigned outpitch, const uint16_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt)
{
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
out[x] = conv_pixel_15_32(input[x], fmt);
out += outpitch >> 2;
input += pitch >> 1;
}
}
static void convert_15bit_15bit_direct(uint16_t *out, unsigned outpitch, const uint16_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt)
{
for (unsigned y = 0; y < height; y++)
{
uint16_t *dest = out + ((y * outpitch) >> 1);
const uint16_t *src = input + ((y * pitch) >> 1);
memcpy(dest, src, width * sizeof(uint16_t));
}
(void)fmt;
}
static void convert_32bit_32bit_direct(uint32_t *out, unsigned outpitch, const uint32_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt)
{
for (unsigned y = 0; y < height; y++)
{
uint32_t *dest = out + ((y * outpitch) >> 2);
const uint32_t *src = input + ((y * pitch) >> 2);
memcpy(dest, src, width * sizeof(uint32_t));
}
(void)fmt;
}
static void convert_15bit_15bit_shift(uint16_t *out, unsigned outpitch, const uint16_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt)
{
for (unsigned y = 0; y < height; y++)
{
uint16_t *dest = out + ((y * outpitch) >> 1);
const uint16_t *src = input + ((y * pitch) >> 1);
for (unsigned x = 0; x < width; x++)
{
uint16_t color = src[x];
uint16_t r = ((color >> 10) & 0x1f) << fmt->Rshift;
uint16_t g = ((color >> 5) & 0x1f) << fmt->Gshift;
uint16_t b = ((color >> 0) & 0x1f) << fmt->Bshift;
dest[x] = r | g | b;
}
}
}
static void convert_32bit_32bit_shift(uint32_t *out, unsigned outpitch, const uint32_t *input, unsigned width, unsigned height, unsigned pitch, const SDL_PixelFormat *fmt)
{
for (unsigned y = 0; y < height; y++)
{
uint32_t *dest = out + ((y * outpitch) >> 2);
const uint32_t *src = input + ((y * pitch) >> 2);
for (unsigned x = 0; x < width; x++)
{
uint32_t color = src[x];
uint32_t r = ((color >> 16) & 0xff) << fmt->Rshift;
uint32_t g = ((color >> 8) & 0xff) << fmt->Gshift;
uint32_t b = ((color >> 0) & 0xff) << fmt->Bshift;
dest[x] = r | g | b;
}
}
}
static bool sdl_gfx_frame(void *data, const void* frame, unsigned width, unsigned height, unsigned pitch, const char *msg)
{
sdl_video_t *vid = data;
if (SDL_MUSTLOCK(vid->buffer))
SDL_LockSurface(vid->buffer);
// :(
// 15-bit -> 32-bit (Sometimes 15-bit won't work on "modern" OSes :\)
if (vid->upsample)
{
convert_15bit_32bit(vid->buffer->pixels, vid->buffer->pitch, frame, width, height, pitch, vid->screen->format);
}
// 15-bit -> 15-bit
else if (!vid->rgb32)
{
vid->convert_15_func(vid->buffer->pixels, vid->buffer->pitch, frame, width, height, pitch, vid->screen->format);
}
// 32-bit -> 15-bit
else if (vid->rgb32 && g_settings.video.force_16bit)
{
convert_32bit_15bit(vid->buffer->pixels, vid->buffer->pitch, frame, width, height, pitch, vid->screen->format);
}
// 32-bit -> 32-bit
else
{
vid->convert_32_func(vid->buffer->pixels, vid->buffer->pitch, frame, width, height, pitch, vid->screen->format);
}
if (msg)
{
if ((!vid->rgb32 || g_settings.video.force_16bit) && !vid->upsample)
sdl_render_msg_15(vid, vid->buffer, msg, width, height, vid->screen->format);
else
sdl_render_msg_32(vid, vid->buffer, msg, width, height, vid->screen->format);
}
if (SDL_MUSTLOCK(vid->buffer))
SDL_UnlockSurface(vid->buffer);
SDL_Rect src = {
.x = 0,
.y = 0,
.w = width,
.h = height
};
SDL_Rect dest = {
.x = 0,
.y = 0,
.w = vid->screen->w,
.h = vid->screen->h
};
SDL_SoftStretch(vid->buffer, &src, vid->screen, &dest);
char buf[128];
if (gfx_window_title(buf, sizeof(buf)))
SDL_WM_SetCaption(buf, NULL);
SDL_Flip(vid->screen);
return true;
}
static void sdl_gfx_set_nonblock_state(void *data, bool state)
{
(void)data; // Can SDL even do this?
(void)state;
}
static bool sdl_gfx_alive(void *data)
{
sdl_video_t *vid = data;
return !vid->quitting;
}
static bool sdl_gfx_focus(void *data)
{
(void)data;
return (SDL_GetAppState() & (SDL_APPINPUTFOCUS | SDL_APPACTIVE)) == (SDL_APPINPUTFOCUS | SDL_APPACTIVE);
}
const video_driver_t video_sdl = {
.init = sdl_gfx_init,
.frame = sdl_gfx_frame,
.alive = sdl_gfx_alive,
.set_nonblock_state = sdl_gfx_set_nonblock_state,
.focus = sdl_gfx_focus,
.free = sdl_gfx_free,
.ident = "sdl"
};
+804 -65
View File
@@ -19,6 +19,17 @@
#include <Cg/cg.h>
#include <Cg/cgGL.h>
#include "general.h"
#include <string.h>
#include "strl.h"
#include "conf/config_file.h"
#include "image.h"
#include "dynamic.h"
#ifdef HAVE_CONFIGFILE
#include "snes_state.h"
#endif
//#define SSNES_CG_DEBUG
// Used when we call deactivate() since just unbinding the program didn't seem to work... :(
static const char* stock_cg_program =
@@ -32,7 +43,7 @@ static const char* stock_cg_program =
""
" out float4 oPosition : POSITION,"
" out float4 oColor : COLOR,"
" out float2 otexCoord : TEXCOORD"
" out float2 otexCoord : TEXCOORD0"
")"
"{"
" oPosition = mul(modelViewProj, position);"
@@ -40,51 +51,192 @@ static const char* stock_cg_program =
" otexCoord = texCoord;"
"}"
""
""
"struct output "
"float4 main_fragment(float2 tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0) : COLOR"
"{"
" float4 color : COLOR;"
"};"
""
"output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0) "
"{"
" output OUT;"
" OUT.color = tex2D(decal, texCoord);"
" return OUT;"
" return tex2D(s0, tex);"
"}";
#ifdef SSNES_CG_DEBUG
static void cg_error_handler(CGcontext ctx, CGerror error, void *data)
{
(void)ctx;
(void)data;
switch (error)
{
case CG_INVALID_PARAM_HANDLE_ERROR:
SSNES_ERR("Invalid param handle.\n");
break;
case CG_INVALID_PARAMETER_ERROR:
SSNES_ERR("Invalid parameter.\n");
break;
default:
break;
}
SSNES_ERR("CG error!: \"%s\".\n", cgGetErrorString(error));
}
#endif
static CGcontext cgCtx;
static CGprogram cgFPrg;
static CGprogram cgVPrg;
static CGprogram cgSFPrg;
static CGprogram cgSVPrg;
static CGprofile cgFProf;
static CGprofile cgVProf;
static CGparameter cg_video_size, cg_texture_size, cg_output_size;
static CGparameter cg_Vvideo_size, cg_Vtexture_size, cg_Voutput_size; // Vertexes
static CGparameter cg_mvp_matrix;
struct cg_fbo_params
{
CGparameter vid_size_f;
CGparameter tex_size_f;
CGparameter vid_size_v;
CGparameter tex_size_v;
CGparameter tex;
CGparameter coord;
};
#define MAX_SHADERS 16
#define MAX_TEXTURES 8
#define MAX_VARIABLES 64
struct cg_program
{
CGprogram vprg;
CGprogram fprg;
CGparameter vid_size_f;
CGparameter tex_size_f;
CGparameter out_size_f;
CGparameter frame_cnt_f;
CGparameter vid_size_v;
CGparameter tex_size_v;
CGparameter out_size_v;
CGparameter frame_cnt_v;
CGparameter mvp;
struct cg_fbo_params fbo[MAX_SHADERS];
struct cg_fbo_params orig;
};
#define FILTER_UNSPEC 0
#define FILTER_LINEAR 1
#define FILTER_NEAREST 2
static struct cg_program prg[MAX_SHADERS];
static bool cg_active = false;
static CGprofile cgVProf, cgFProf;
static unsigned active_index = 0;
static unsigned cg_shader_num = 0;
static struct gl_fbo_scale cg_scale[MAX_SHADERS];
static unsigned fbo_smooth[MAX_SHADERS];
static unsigned lut_textures[MAX_TEXTURES];
static unsigned lut_textures_num = 0;
static char lut_textures_uniform[MAX_TEXTURES][64];
static snes_tracker_t *snes_tracker = NULL;
void gl_cg_set_proj_matrix(void)
{
if (cg_active)
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
if (cg_active && prg[active_index].mvp)
cgGLSetStateMatrixParameter(prg[active_index].mvp, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
}
#define set_param_2f(param, x, y) \
if (param) cgGLSetParameter2f(param, x, y)
#define set_param_1f(param, x) \
if (param) cgGLSetParameter1f(param, x)
void gl_cg_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height)
unsigned out_width, unsigned out_height,
unsigned frame_count,
const struct gl_tex_info *info,
const struct gl_tex_info *fbo_info,
unsigned fbo_info_cnt)
{
if (cg_active)
if (cg_active && active_index > 0)
{
cgGLSetParameter2f(cg_video_size, width, height);
cgGLSetParameter2f(cg_texture_size, tex_width, tex_height);
cgGLSetParameter2f(cg_output_size, out_width, out_height);
// Set frame.
set_param_2f(prg[active_index].vid_size_f, width, height);
set_param_2f(prg[active_index].tex_size_f, tex_width, tex_height);
set_param_2f(prg[active_index].out_size_f, out_width, out_height);
set_param_1f(prg[active_index].frame_cnt_f, (float)frame_count);
cgGLSetParameter2f(cg_Vvideo_size, width, height);
cgGLSetParameter2f(cg_Vtexture_size, tex_width, tex_height);
cgGLSetParameter2f(cg_Voutput_size, out_width, out_height);
set_param_2f(prg[active_index].vid_size_v, width, height);
set_param_2f(prg[active_index].tex_size_v, tex_width, tex_height);
set_param_2f(prg[active_index].out_size_v, out_width, out_height);
set_param_1f(prg[active_index].frame_cnt_v, (float)frame_count);
// Set orig texture.
CGparameter param = prg[active_index].orig.tex;
if (param)
{
cgGLSetTextureParameter(param, info->tex);
//fprintf(stderr, "ORIGtex = (%d) %d\n", cgGLGetTextureParameter(param), cgGLGetTextureEnum(param) - GL_TEXTURE0);
cgGLEnableTextureParameter(param);
}
set_param_2f(prg[active_index].orig.vid_size_v, info->input_size[0], info->input_size[1]);
set_param_2f(prg[active_index].orig.vid_size_f, info->input_size[0], info->input_size[1]);
set_param_2f(prg[active_index].orig.tex_size_v, info->tex_size[0], info->tex_size[1]);
set_param_2f(prg[active_index].orig.tex_size_f, info->tex_size[0], info->tex_size[1]);
if (prg[active_index].orig.coord)
{
cgGLSetParameterPointer(prg[active_index].orig.coord, 2, GL_FLOAT, 0, info->coord);
cgGLEnableClientState(prg[active_index].orig.coord);
}
// Set lookup textures.
for (unsigned i = 0; i < lut_textures_num; i++)
{
CGparameter param = cgGetNamedParameter(prg[active_index].fprg, lut_textures_uniform[i]);
if (param)
{
cgGLSetTextureParameter(param, lut_textures[i]);
cgGLEnableTextureParameter(param);
//fprintf(stderr, "LUTtex = (%d) %d\n", cgGLGetTextureParameter(param), cgGLGetTextureEnum(param) - GL_TEXTURE0);
}
}
// Set FBO textures.
if (active_index > 2)
{
for (unsigned i = 0; i < fbo_info_cnt; i++)
{
if (prg[active_index].fbo[i].tex)
{
cgGLSetTextureParameter(prg[active_index].fbo[i].tex, fbo_info[i].tex);
cgGLEnableTextureParameter(prg[active_index].fbo[i].tex);
}
set_param_2f(prg[active_index].fbo[i].vid_size_v,
fbo_info[i].input_size[0], fbo_info[i].input_size[1]);
set_param_2f(prg[active_index].fbo[i].vid_size_f,
fbo_info[i].input_size[0], fbo_info[i].input_size[1]);
set_param_2f(prg[active_index].fbo[i].tex_size_v,
fbo_info[i].tex_size[0], fbo_info[i].tex_size[1]);
set_param_2f(prg[active_index].fbo[i].tex_size_f,
fbo_info[i].tex_size[0], fbo_info[i].tex_size[1]);
if (prg[active_index].fbo[i].coord)
{
cgGLSetParameterPointer(prg[active_index].fbo[i].coord, 2, GL_FLOAT, 0, fbo_info[i].coord);
cgGLEnableClientState(prg[active_index].fbo[i].coord);
}
}
}
// Set state parameters
if (snes_tracker)
{
struct snes_tracker_uniform info[MAX_VARIABLES];
unsigned cnt = snes_get_uniform(snes_tracker, info, MAX_VARIABLES, frame_count);
for (unsigned i = 0; i < cnt; i++)
{
CGparameter param_v = cgGetNamedParameter(prg[active_index].vprg, info[i].id);
CGparameter param_f = cgGetNamedParameter(prg[active_index].fprg, info[i].id);
set_param_1f(param_v, info[i].value);
set_param_1f(param_f, info[i].value);
}
}
}
}
@@ -92,17 +244,548 @@ void gl_cg_deinit(void)
{
if (cg_active)
cgDestroyContext(cgCtx);
cg_active = false;
cg_shader_num = 0;
memset(prg, 0, sizeof(prg));
memset(cg_scale, 0, sizeof(cg_scale));
memset(fbo_smooth, 0, sizeof(fbo_smooth));
glDeleteTextures(lut_textures_num, lut_textures);
lut_textures_num = 0;
if (snes_tracker)
{
snes_tracker_free(snes_tracker);
snes_tracker = NULL;
}
}
static bool load_plain(const char *path)
{
SSNES_LOG("Loading Cg file: %s\n", path);
if (strlen(g_settings.video.second_pass_shader) > 0)
SSNES_LOG("Loading 2nd pass: %s\n", g_settings.video.second_pass_shader);
prg[0].fprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgFProf, "main_fragment", 0);
prg[0].vprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgVProf, "main_vertex", 0);
prg[1].fprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgFProf, "main_fragment", 0);
prg[1].vprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgVProf, "main_vertex", 0);
if (strlen(g_settings.video.second_pass_shader) > 0)
{
prg[2].fprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, g_settings.video.second_pass_shader, cgFProf, "main_fragment", 0);
prg[2].vprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, g_settings.video.second_pass_shader, cgVProf, "main_vertex", 0);
cg_shader_num = 2;
}
else
{
prg[2] = prg[0];
cg_shader_num = 1;
}
for (int i = 0; i < cg_shader_num + 1; i++)
{
if (!prg[i].fprg || !prg[i].vprg)
{
CGerror err = cgGetError();
SSNES_ERR("CG error: %s\n", cgGetErrorString(err));
return false;
}
cgGLLoadProgram(prg[i].fprg);
cgGLLoadProgram(prg[i].vprg);
}
return true;
}
#define print_buf(buf, ...) snprintf(buf, sizeof(buf), __VA_ARGS__)
#ifdef HAVE_CONFIGFILE
static bool load_textures(const char *dir_path, config_file_t *conf)
{
char *textures;
if (!config_get_string(conf, "textures", &textures)) // No textures here ...
return true;
const char *id = strtok(textures, ";");;
while (id && lut_textures_num < MAX_TEXTURES)
{
char *path;
if (!config_get_string(conf, id, &path))
{
SSNES_ERR("Cannot find path to texture \"%s\" ...\n", id);
goto error;
}
char id_filter[64];
print_buf(id_filter, "%s_linear", id);
bool smooth;
if (!config_get_bool(conf, id_filter, &smooth))
smooth = true;
char image_path[512];
print_buf(image_path, "%s%s", dir_path, path);
SSNES_LOG("Loading image from: \"%s\".\n", image_path);
struct texture_image img;
if (!texture_image_load(image_path, &img))
{
SSNES_ERR("Failed to load picture ...\n");
free(path);
goto error;
}
strlcpy(lut_textures_uniform[lut_textures_num], id, sizeof(lut_textures_uniform[lut_textures_num]));
glGenTextures(1, &lut_textures[lut_textures_num]);
glBindTexture(GL_TEXTURE_2D, lut_textures[lut_textures_num]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, smooth ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, smooth ? GL_LINEAR : GL_NEAREST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ROW_LENGTH, img.width);
glTexImage2D(GL_TEXTURE_2D,
0, GL_RGBA, img.width, img.height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, img.pixels);
lut_textures_num++;
free(img.pixels);
free(path);
id = strtok(NULL, ";");;
}
glBindTexture(GL_TEXTURE_2D, 0);
free(textures);
return true;
error:
if (textures)
free(textures);
pglActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
return false;
}
static bool load_imports(config_file_t *conf)
{
char *imports = NULL;
if (!config_get_string(conf, "imports", &imports))
return true;
struct snes_tracker_uniform_info info[MAX_VARIABLES];
unsigned info_cnt = 0;
const char *id = strtok(imports, ";");
while (id && info_cnt < MAX_VARIABLES)
{
char semantic_buf[64];
char wram_buf[64];
char apuram_buf[64];
char oam_buf[64];
char cgram_buf[64];
char vram_buf[64];
char mask_buf[64];
print_buf(semantic_buf, "%s_semantic", id);
print_buf(wram_buf, "%s_wram", id);
print_buf(apuram_buf, "%s_apuram", id);
print_buf(oam_buf, "%s_oam", id);
print_buf(cgram_buf, "%s_cgram", id);
print_buf(vram_buf, "%s_vram", id);
print_buf(mask_buf, "%s_mask", id);
char *semantic = NULL;
config_get_string(conf, semantic_buf, &semantic);
if (!semantic)
{
SSNES_ERR("No semantic for import variable.\n");
goto error;
}
enum snes_tracker_type tracker_type;
enum snes_ram_type ram_type;
if (strcmp(semantic, "capture") == 0)
tracker_type = SSNES_STATE_CAPTURE;
else if (strcmp(semantic, "transition") == 0)
tracker_type = SSNES_STATE_TRANSITION;
else if (strcmp(semantic, "capture_previous") == 0)
tracker_type = SSNES_STATE_CAPTURE_PREV;
else if (strcmp(semantic, "transition_previous") == 0)
tracker_type = SSNES_STATE_TRANSITION_PREV;
else
{
SSNES_ERR("Invalid semantic.\n");
goto error;
}
unsigned addr;
if (config_get_hex(conf, wram_buf, &addr))
ram_type = SSNES_STATE_WRAM;
else if (config_get_hex(conf, apuram_buf, &addr))
ram_type = SSNES_STATE_APURAM;
else if (config_get_hex(conf, oam_buf, &addr))
ram_type = SSNES_STATE_OAM;
else if (config_get_hex(conf, cgram_buf, &addr))
ram_type = SSNES_STATE_CGRAM;
else if (config_get_hex(conf, vram_buf, &addr))
ram_type = SSNES_STATE_VRAM;
else
{
SSNES_ERR("No address assigned to semantic.\n");
goto error;
}
int memtype = 0;
switch (ram_type)
{
case SSNES_STATE_WRAM:
memtype = SNES_MEMORY_WRAM;
break;
case SSNES_STATE_APURAM:
memtype = SNES_MEMORY_APURAM;
break;
case SSNES_STATE_VRAM:
memtype = SNES_MEMORY_VRAM;
break;
case SSNES_STATE_OAM:
memtype = SNES_MEMORY_OAM;
break;
case SSNES_STATE_CGRAM:
memtype = SNES_MEMORY_CGRAM;
break;
default:
break;
}
if (addr >= psnes_get_memory_size(memtype))
{
SSNES_ERR("Address out of bounds.\n");
goto error;
}
unsigned bitmask = 0;
if (!config_get_hex(conf, mask_buf, &bitmask))
bitmask = 0;
strlcpy(info[info_cnt].id, id, sizeof(info[info_cnt].id));
info[info_cnt].addr = addr;
info[info_cnt].type = tracker_type;
info[info_cnt].ram_type = ram_type;
info[info_cnt].mask = bitmask;
info_cnt++;
free(semantic);
id = strtok(NULL, ";");
}
struct snes_tracker_info tracker_info = {
.wram = psnes_get_memory_data(SNES_MEMORY_WRAM),
.vram = psnes_get_memory_data(SNES_MEMORY_VRAM),
.cgram = psnes_get_memory_data(SNES_MEMORY_CGRAM),
.oam = psnes_get_memory_data(SNES_MEMORY_OAM),
.apuram = psnes_get_memory_data(SNES_MEMORY_APURAM),
.info = info,
.info_elem = info_cnt
};
snes_tracker = snes_tracker_init(&tracker_info);
if (!snes_tracker)
SSNES_WARN("Failed to init SNES tracker.\n");
free(imports);
return true;
error:
free(imports);
return false;
}
#endif
static bool load_preset(const char *path)
{
#ifdef HAVE_CONFIGFILE
// Create passthrough shader.
prg[0].fprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgFProf, "main_fragment", 0);
prg[0].vprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgVProf, "main_vertex", 0);
if (!prg[0].fprg || !prg[0].vprg)
{
SSNES_ERR("Failed to compile passthrough shader, is something wrong with your environment?\n");
return false;
}
cgGLLoadProgram(prg[0].fprg);
cgGLLoadProgram(prg[0].vprg);
SSNES_LOG("Loading Cg meta-shader: %s\n", path);
config_file_t *conf = config_file_new(path);
if (!conf)
{
SSNES_ERR("Failed to load preset.\n");
goto error;
}
int shaders;
if (!config_get_int(conf, "shaders", &shaders))
{
SSNES_ERR("Cannot find \"shaders\" param.\n");
goto error;
}
if (shaders < 1)
{
SSNES_ERR("Need to define at least 1 shader!\n");
goto error;
}
cg_shader_num = shaders;
if (shaders > MAX_SHADERS - 2)
{
SSNES_WARN("Too many shaders ... Capping shader amount to %d.\n", MAX_SHADERS - 2);
cg_shader_num = shaders = MAX_SHADERS - 2;
}
// If we aren't using last pass non-FBO shader,
// this shader will be assumed to be "fixed-function".
// Just use prg[0] for that pass, which will be
// pass-through.
prg[shaders + 1] = prg[0];
// Check filter params.
for (unsigned i = 0; i < shaders; i++)
{
bool smooth;
char filter_name_buf[64];
print_buf(filter_name_buf, "filter_linear%u", i);
if (config_get_bool(conf, filter_name_buf, &smooth))
fbo_smooth[i + 1] = smooth ? FILTER_LINEAR : FILTER_NEAREST;
}
// Bigass for-loop ftw. Check scaling params.
for (unsigned i = 0; i < shaders; i++)
{
char *scale_type = NULL;
char *scale_type_x = NULL;
char *scale_type_y = NULL;
bool has_scale_type;
bool has_scale_type_x;
bool has_scale_type_y;
char scale_name_buf[64];
print_buf(scale_name_buf, "scale_type%u", i);
has_scale_type = config_get_string(conf, scale_name_buf, &scale_type);
print_buf(scale_name_buf, "scale_type_x%u", i);
has_scale_type_x = config_get_string(conf, scale_name_buf, &scale_type_x);
print_buf(scale_name_buf, "scale_type_y%u", i);
has_scale_type_y = config_get_string(conf, scale_name_buf, &scale_type_y);
if (!has_scale_type && !has_scale_type_x && !has_scale_type_y)
continue;
if (has_scale_type)
{
if (scale_type_x)
free(scale_type_x);
if (scale_type_y)
free(scale_type_y);
scale_type_x = strdup(scale_type);
scale_type_y = strdup(scale_type);
free(scale_type);
}
char attr_name_buf[64];
double fattr;
int iattr;
struct gl_fbo_scale *scale = &cg_scale[i + 1]; // Shader 0 is passthrough shader. Start at 1.
scale->valid = true;
scale->type_x = SSNES_SCALE_INPUT;
scale->type_y = SSNES_SCALE_INPUT;
scale->scale_x = 1.0;
scale->scale_y = 1.0;
scale->abs_x = 256;
scale->abs_y = 224;
if (strcmp(scale_type_x, "source") == 0)
scale->type_x = SSNES_SCALE_INPUT;
else if (strcmp(scale_type_x, "viewport") == 0)
scale->type_x = SSNES_SCALE_VIEWPORT;
else if (strcmp(scale_type_x, "absolute") == 0)
scale->type_x = SSNES_SCALE_ABSOLUTE;
else
{
SSNES_ERR("Invalid attribute.\n");
goto error;
}
if (strcmp(scale_type_y, "source") == 0)
scale->type_y = SSNES_SCALE_INPUT;
else if (strcmp(scale_type_y, "viewport") == 0)
scale->type_y = SSNES_SCALE_VIEWPORT;
else if (strcmp(scale_type_y, "absolute") == 0)
scale->type_y = SSNES_SCALE_ABSOLUTE;
else
{
SSNES_ERR("Invalid attribute.\n");
goto error;
}
if (scale->type_x == SSNES_SCALE_ABSOLUTE)
{
print_buf(attr_name_buf, "scale%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_x = iattr;
else
{
print_buf(attr_name_buf, "scale_x%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_x = iattr;
}
}
else
{
print_buf(attr_name_buf, "scale%u", i);
if (config_get_double(conf, attr_name_buf, &fattr))
scale->scale_x = fattr;
else
{
print_buf(attr_name_buf, "scale_x%u", i);
if (config_get_double(conf, attr_name_buf, &fattr))
scale->scale_x = fattr;
}
}
if (scale->type_y == SSNES_SCALE_ABSOLUTE)
{
print_buf(attr_name_buf, "scale%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_y = iattr;
else
{
print_buf(attr_name_buf, "scale_y%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_y = iattr;
}
}
else
{
print_buf(attr_name_buf, "scale%u", i);
if (config_get_double(conf, attr_name_buf, &fattr))
scale->scale_y = fattr;
else
{
print_buf(attr_name_buf, "scale_y%u", i);
if (config_get_double(conf, attr_name_buf, &fattr))
scale->scale_y = fattr;
}
}
if (scale_type_x)
free(scale_type_x);
if (scale_type_y)
free(scale_type_y);
}
// Basedir.
char dir_path[256];
strlcpy(dir_path, path, sizeof(dir_path));
char *ptr = strrchr(dir_path, '/');
if (!ptr) ptr = strrchr(dir_path, '\\');
if (ptr)
ptr[1] = '\0';
else // No directory.
dir_path[0] = '\0';
// Finally load shaders :)
for (unsigned i = 0; i < shaders; i++)
{
char *shader_path;
char attr_buf[64];
char path_buf[512];
print_buf(attr_buf, "shader%u", i);
if (config_get_string(conf, attr_buf, &shader_path))
{
strlcpy(path_buf, dir_path, sizeof(path_buf));
strlcat(path_buf, shader_path, sizeof(path_buf));
free(shader_path);
}
else
{
SSNES_ERR("Didn't find shader path in config ...\n");
goto error;
}
SSNES_LOG("Loading Cg shader: \"%s\".\n", path_buf);
struct cg_program *prog = &prg[i + 1];
prog->fprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path_buf, cgFProf, "main_fragment", 0);
prog->vprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path_buf, cgVProf, "main_vertex", 0);
if (!prog->fprg || !prog->vprg)
{
CGerror err = cgGetError();
SSNES_ERR("CG error: %s\n", cgGetErrorString(err));
goto error;
}
cgGLLoadProgram(prog->fprg);
cgGLLoadProgram(prog->vprg);
}
if (!load_textures(dir_path, conf))
{
SSNES_ERR("Failed to load lookup textures ...\n");
goto error;
}
if (!load_imports(conf))
{
SSNES_ERR("Failed to load imports ...\n");
goto error;
}
config_file_free(conf);
return true;
error:
if (conf)
config_file_free(conf);
return false;
#else
(void)path;
SSNES_ERR("No config file support compiled in.\n");
return false;
#endif
}
bool gl_cg_init(const char *path)
{
SSNES_LOG("Loading Cg file: %s\n", path);
cgCtx = cgCreateContext();
if (cgCtx == NULL)
{
SSNES_ERR("Failed to create Cg context\n");
return false;
}
#ifdef SSNES_CG_DEBUG
cgGLSetDebugMode(CG_TRUE);
cgSetErrorHandler(cg_error_handler, NULL);
#endif
cgFProf = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgVProf = cgGLGetLatestProfile(CG_GL_VERTEX);
if (cgFProf == CG_PROFILE_UNKNOWN || cgVProf == CG_PROFILE_UNKNOWN)
@@ -112,54 +795,110 @@ bool gl_cg_init(const char *path)
}
cgGLSetOptimalOptions(cgFProf);
cgGLSetOptimalOptions(cgVProf);
cgFPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgFProf, "main_fragment", 0);
cgVPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgVProf, "main_vertex", 0);
cgSFPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgFProf, "main_fragment", 0);
cgSVPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgVProf, "main_vertex", 0);
if (cgFPrg == NULL || cgVPrg == NULL || cgSFPrg == NULL || cgSVPrg == NULL)
{
CGerror err = cgGetError();
SSNES_ERR("CG error: %s\n", cgGetErrorString(err));
return false;
}
cgGLLoadProgram(cgFPrg);
cgGLLoadProgram(cgVPrg);
cgGLLoadProgram(cgSFPrg);
cgGLLoadProgram(cgSVPrg);
cgGLEnableProfile(cgFProf);
cgGLEnableProfile(cgVProf);
cgGLBindProgram(cgFPrg);
cgGLBindProgram(cgVPrg);
cg_video_size = cgGetNamedParameter(cgFPrg, "IN.video_size");
cg_texture_size = cgGetNamedParameter(cgFPrg, "IN.texture_size");
cg_output_size = cgGetNamedParameter(cgFPrg, "IN.output_size");
cg_Vvideo_size = cgGetNamedParameter(cgVPrg, "IN.video_size");
cg_Vtexture_size = cgGetNamedParameter(cgVPrg, "IN.texture_size");
cg_Voutput_size = cgGetNamedParameter(cgVPrg, "IN.output_size");
cg_mvp_matrix = cgGetNamedParameter(cgVPrg, "modelViewProj");
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cg_mvp_matrix = cgGetNamedParameter(cgSVPrg, "modelViewProj");
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
if (strstr(path, ".cgp"))
{
if (!load_preset(path))
return false;
}
else
{
if (!load_plain(path))
return false;
}
prg[0].mvp = cgGetNamedParameter(prg[0].vprg, "modelViewProj");
if (prg[0].mvp)
cgGLSetStateMatrixParameter(prg[0].mvp, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
for (unsigned i = 1; i < cg_shader_num + 1; i++)
{
cgGLBindProgram(prg[i].fprg);
cgGLBindProgram(prg[i].vprg);
prg[i].vid_size_f = cgGetNamedParameter(prg[i].fprg, "IN.video_size");
prg[i].tex_size_f = cgGetNamedParameter(prg[i].fprg, "IN.texture_size");
prg[i].out_size_f = cgGetNamedParameter(prg[i].fprg, "IN.output_size");
prg[i].frame_cnt_f = cgGetNamedParameter(prg[i].fprg, "IN.frame_count");
prg[i].vid_size_v = cgGetNamedParameter(prg[i].vprg, "IN.video_size");
prg[i].tex_size_v = cgGetNamedParameter(prg[i].vprg, "IN.texture_size");
prg[i].out_size_v = cgGetNamedParameter(prg[i].vprg, "IN.output_size");
prg[i].frame_cnt_v = cgGetNamedParameter(prg[i].vprg, "IN.frame_count");
prg[i].mvp = cgGetNamedParameter(prg[i].vprg, "modelViewProj");
if (prg[i].mvp)
cgGLSetStateMatrixParameter(prg[i].mvp, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
prg[i].orig.tex = cgGetNamedParameter(prg[i].fprg, "ORIG.texture");
prg[i].orig.vid_size_v = cgGetNamedParameter(prg[i].vprg, "ORIG.video_size");
prg[i].orig.vid_size_f = cgGetNamedParameter(prg[i].fprg, "ORIG.video_size");
prg[i].orig.tex_size_v = cgGetNamedParameter(prg[i].vprg, "ORIG.texture_size");
prg[i].orig.tex_size_f = cgGetNamedParameter(prg[i].fprg, "ORIG.texture_size");
prg[i].orig.coord = cgGetNamedParameter(prg[i].vprg, "ORIG.tex_coord");
for (unsigned j = 0; j < i - 1; j++)
{
char attr_buf[64];
snprintf(attr_buf, sizeof(attr_buf), "PASS%u.texture", j + 1);
prg[i].fbo[j].tex = cgGetNamedParameter(prg[i].fprg, attr_buf);
snprintf(attr_buf, sizeof(attr_buf), "PASS%u.video_size", j + 1);
prg[i].fbo[j].vid_size_v = cgGetNamedParameter(prg[i].vprg, attr_buf);
prg[i].fbo[j].vid_size_f = cgGetNamedParameter(prg[i].fprg, attr_buf);
snprintf(attr_buf, sizeof(attr_buf), "PASS%u.texture_size", j + 1);
prg[i].fbo[j].tex_size_v = cgGetNamedParameter(prg[i].vprg, attr_buf);
prg[i].fbo[j].tex_size_f = cgGetNamedParameter(prg[i].fprg, attr_buf);
snprintf(attr_buf, sizeof(attr_buf), "PASS%u.tex_coord", j + 1);
prg[i].fbo[j].coord = cgGetNamedParameter(prg[i].vprg, attr_buf);
}
}
cgGLBindProgram(prg[1].fprg);
cgGLBindProgram(prg[1].vprg);
cg_active = true;
return true;
}
void gl_cg_activate(void)
void gl_cg_use(unsigned index)
{
if (cg_active)
if (cg_active && prg[index].vprg && prg[index].fprg)
{
cgGLBindProgram(cgFPrg);
cgGLBindProgram(cgVPrg);
active_index = index;
cgGLBindProgram(prg[index].vprg);
cgGLBindProgram(prg[index].fprg);
}
}
// Just load a dummy shader.
void gl_cg_deactivate(void)
unsigned gl_cg_num(void)
{
if (cg_active)
return cg_shader_num;
else
return 0;
}
bool gl_cg_filter_type(unsigned index, bool *smooth)
{
if (cg_active)
{
cgGLBindProgram(cgSFPrg);
cgGLBindProgram(cgSVPrg);
if (fbo_smooth[index] == FILTER_UNSPEC)
return false;
*smooth = (fbo_smooth[index] == FILTER_LINEAR);
return true;
}
else
return false;
}
void gl_cg_shader_scale(unsigned index, struct gl_fbo_scale *scale)
{
if (cg_active)
*scale = cg_scale[index];
else
scale->valid = false;
}
+11 -3
View File
@@ -20,6 +20,7 @@
#define __SSNES_CG_H
#include <stdbool.h>
#include "gl_common.h"
bool gl_cg_init(const char *path);
@@ -29,9 +30,16 @@ void gl_cg_set_proj_matrix(void);
void gl_cg_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height);
unsigned out_width, unsigned out_height,
unsigned frame_count,
const struct gl_tex_info *info,
const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt);
void gl_cg_activate(void);
void gl_cg_deactivate(void);
void gl_cg_use(unsigned index);
unsigned gl_cg_num(void);
bool gl_cg_filter_type(unsigned index, bool *smooth);
void gl_cg_shader_scale(unsigned index, struct gl_fbo_scale *scale);
#endif
+822 -85
View File
File diff suppressed because it is too large. Load diff
+11 -3
View File
@@ -20,6 +20,7 @@
#define __SSNES_GLSL_H
#include <stdbool.h>
#include "gl_common.h"
bool gl_glsl_init(const char *path);
@@ -29,9 +30,16 @@ void gl_glsl_set_proj_matrix(void);
void gl_glsl_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height);
unsigned out_width, unsigned out_height,
unsigned frame_counter,
const struct gl_tex_info *info,
const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt);
void gl_glsl_activate(void);
void gl_glsl_deactivate(void);
void gl_glsl_use(unsigned index);
unsigned gl_glsl_num(void);
bool gl_glsl_filter_type(unsigned index, bool *smooth);
void gl_glsl_shader_scale(unsigned index, struct gl_fbo_scale *scale);
#endif
+158
View File
@@ -0,0 +1,158 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "snes_state.h"
#include <stdlib.h>
#include <assert.h>
#include "strl.h"
struct snes_tracker_internal
{
char id[64];
const uint8_t *ptr;
uint32_t addr;
uint8_t mask;
enum snes_tracker_type type;
uint8_t prev[2];
int frame_count;
uint8_t old_value;
};
struct snes_tracker
{
struct snes_tracker_internal *info;
unsigned info_elem;
};
snes_tracker_t* snes_tracker_init(const struct snes_tracker_info *info)
{
snes_tracker_t *tracker = calloc(1, sizeof(*tracker));
if (!tracker)
return NULL;
tracker->info = calloc(info->info_elem, sizeof(struct snes_tracker_internal));
tracker->info_elem = info->info_elem;
for (unsigned i = 0; i < info->info_elem; i++)
{
strlcpy(tracker->info[i].id, info->info[i].id, sizeof(tracker->info[i].id));
tracker->info[i].addr = info->info[i].addr;
tracker->info[i].type = info->info[i].type;
tracker->info[i].mask = (info->info[i].mask == 0) ? 0xff : info->info[i].mask;
assert(info->wram && info->vram && info->cgram &&
info->oam && info->apuram);
switch (info->info[i].ram_type)
{
case SSNES_STATE_WRAM:
tracker->info[i].ptr = info->wram;
break;
case SSNES_STATE_APURAM:
tracker->info[i].ptr = info->apuram;
break;
case SSNES_STATE_OAM:
tracker->info[i].ptr = info->oam;
break;
case SSNES_STATE_CGRAM:
tracker->info[i].ptr = info->cgram;
break;
case SSNES_STATE_VRAM:
tracker->info[i].ptr = info->vram;
break;
default:
tracker->info[i].ptr = NULL;
}
}
return tracker;
}
void snes_tracker_free(snes_tracker_t *tracker)
{
free(tracker->info);
free(tracker);
}
#define fetch() (info->ptr[info->addr] & info->mask)
static void update_element(
struct snes_tracker_uniform *uniform,
struct snes_tracker_internal *info,
unsigned frame_count)
{
uniform->id = info->id;
switch (info->type)
{
case SSNES_STATE_CAPTURE:
uniform->value = fetch();
break;
case SSNES_STATE_CAPTURE_PREV:
if (info->prev[0] != fetch())
{
info->prev[1] = info->prev[0];
info->prev[0] = fetch();
}
uniform->value = info->prev[1];
break;
case SSNES_STATE_TRANSITION:
if (info->old_value != fetch())
{
info->old_value = fetch();
info->frame_count = frame_count;
}
uniform->value = info->frame_count;
break;
case SSNES_STATE_TRANSITION_PREV:
if (info->prev[0] != fetch())
{
info->old_value = fetch();
info->prev[1] = info->prev[0];
info->prev[0] = frame_count;
info->frame_count = frame_count;
}
uniform->value = info->prev[1];
break;
default:
break;
}
}
#undef fetch
unsigned snes_get_uniform(snes_tracker_t *tracker, struct snes_tracker_uniform *uniforms, unsigned elem, unsigned frame_count)
{
unsigned elems = tracker->info_elem < elem ? tracker->info_elem : elem;
for (unsigned i = 0; i < elems; i++)
update_element(&uniforms[i], &tracker->info[i], frame_count);
return elems;
}
+74
View File
@@ -0,0 +1,74 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_SNES_STATE_H
#define __SSNES_SNES_STATE_H
#include <stdint.h>
enum snes_tracker_type
{
SSNES_STATE_CAPTURE,
SSNES_STATE_TRANSITION,
SSNES_STATE_CAPTURE_PREV,
SSNES_STATE_TRANSITION_PREV
};
enum snes_ram_type
{
SSNES_STATE_WRAM,
SSNES_STATE_APURAM,
SSNES_STATE_OAM,
SSNES_STATE_CGRAM,
SSNES_STATE_VRAM
};
struct snes_tracker_uniform_info
{
char id[64];
uint32_t addr;
enum snes_tracker_type type;
enum snes_ram_type ram_type;
uint8_t mask;
};
struct snes_tracker_info
{
const uint8_t *wram;
const uint8_t *vram;
const uint8_t *cgram;
const uint8_t *oam;
const uint8_t *apuram;
const struct snes_tracker_uniform_info *info;
unsigned info_elem;
};
struct snes_tracker_uniform
{
const char *id;
int value;
};
typedef struct snes_tracker snes_tracker_t;
snes_tracker_t* snes_tracker_init(const struct snes_tracker_info *info);
void snes_tracker_free(snes_tracker_t *tracker);
unsigned snes_get_uniform(snes_tracker_t *tracker, struct snes_tracker_uniform *uniforms, unsigned elem, unsigned frame_count);
#endif
+724
View File
@@ -0,0 +1,724 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#include "general.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <math.h>
#ifdef HAVE_FREETYPE
#include "fonts.h"
#endif
#include "gfx_common.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
// Adapted from bSNES and MPlayer source.
typedef struct xv xv_t;
struct xv
{
Display *display;
GC gc;
Window window;
Colormap colormap;
XShmSegmentInfo shminfo;
Atom quit_atom;
bool focus;
XvPortID port;
int depth;
int visualid;
XvImage *image;
uint32_t fourcc;
unsigned width;
unsigned height;
bool keep_aspect;
uint8_t *ytable;
uint8_t *utable;
uint8_t *vtable;
unsigned luma_i;
unsigned chroma_i;
#ifdef HAVE_FREETYPE
font_renderer_t *font;
#endif
void (*render_func)(xv_t*, const void *frame, unsigned width, unsigned height, unsigned pitch);
};
static void xv_set_nonblock_state(void *data, bool state)
{
xv_t *xv = data;
Atom atom = XInternAtom(xv->display, "XV_SYNC_TO_VBLANK", true);
if (atom != None && xv->port)
XvSetPortAttribute(xv->display, xv->port, atom, !state);
else
SSNES_WARN("Failed to set SYNC_TO_VBLANK attribute.\n");
}
static volatile sig_atomic_t g_quit = 0;
static void sighandler(int sig)
{
g_quit = 1;
}
static void init_yuv_tables(xv_t *xv)
{
xv->ytable = malloc(0x8000);
xv->utable = malloc(0x8000);
xv->vtable = malloc(0x8000);
for (unsigned i = 0; i < 0x8000; i++)
{
// Extract RGB555 color data from i
uint8_t r = (i >> 10) & 0x1F, g = (i >> 5) & 0x1F, b = (i) & 0x1F;
r = (r << 3) | (r >> 2); //R5->R8
g = (g << 3) | (g >> 2); //G5->G8
b = (b << 3) | (b >> 2); //B5->B8
int y = (int)(+((double)r * 0.257) + ((double)g * 0.504) + ((double)b * 0.098) + 16.0);
int u = (int)(-((double)r * 0.148) - ((double)g * 0.291) + ((double)b * 0.439) + 128.0);
int v = (int)(+((double)r * 0.439) - ((double)g * 0.368) - ((double)b * 0.071) + 128.0);
xv->ytable[i] = y < 0 ? 0 : y > 255 ? 255 : y;
xv->utable[i] = u < 0 ? 0 : u > 255 ? 255 : u;
xv->vtable[i] = v < 0 ? 0 : v > 255 ? 255 : v;
}
}
// Source: MPlayer
static void hide_mouse(xv_t *xv)
{
Cursor no_ptr;
Pixmap bm_no;
XColor black, dummy;
Colormap colormap;
static char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
colormap = DefaultColormap(xv->display, DefaultScreen(xv->display));
if (!XAllocNamedColor(xv->display, colormap, "black", &black, &dummy))
return;
bm_no = XCreateBitmapFromData(xv->display, xv->window, bm_no_data, 8, 8);
no_ptr = XCreatePixmapCursor(xv->display, bm_no, bm_no, &black, &black, 0, 0);
XDefineCursor(xv->display, xv->window, no_ptr);
XFreeCursor(xv->display, no_ptr);
if (bm_no != None)
XFreePixmap(xv->display, bm_no);
XFreeColors(xv->display, colormap, &black.pixel, 1, 0);
}
static Atom XA_NET_WM_STATE;
static Atom XA_NET_WM_STATE_FULLSCREEN;
#define XA_INIT(x) XA##x = XInternAtom(xv->display, #x, False)
#define _NET_WM_STATE_ADD 1
// Source: MPlayer
static void set_fullscreen(xv_t *xv)
{
XA_INIT(_NET_WM_STATE);
XA_INIT(_NET_WM_STATE_FULLSCREEN);
if (!XA_NET_WM_STATE || !XA_NET_WM_STATE_FULLSCREEN)
{
SSNES_WARN("X11: Cannot set fullscreen.\n");
return;
}
XEvent xev;
xev.xclient.type = ClientMessage;
xev.xclient.serial = 0;
xev.xclient.send_event = True;
xev.xclient.message_type = XA_NET_WM_STATE;
xev.xclient.window = xv->window;
xev.xclient.format = 32;
xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
xev.xclient.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
xev.xclient.data.l[2] = 0;
xev.xclient.data.l[3] = 0;
xev.xclient.data.l[4] = 0;
XSendEvent(xv->display, DefaultRootWindow(xv->display), False,
SubstructureRedirectMask | SubstructureNotifyMask,
&xev);
}
static void xv_init_font(xv_t *xv, const char *font_path, unsigned font_size)
{
#ifdef HAVE_FREETYPE
if (*font_path)
{
xv->font = font_renderer_new(font_path, font_size);
if (!xv->font)
SSNES_WARN("Failed to init font.\n");
}
#endif
}
// We render @ 2x scale to combat chroma downsampling. Also makes fonts more bearable :)
static void render16_yuy2(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint16_t *input = input_;
uint8_t *output = (uint8_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint16_t p = *input++;
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = y0;
output[1] = output[img_width + 1] = u;
output[2] = output[img_width + 2] = y0;
output[3] = output[img_width + 3] = v;
output += 4;
}
input += (pitch >> 1) - width;
output += (xv->width - width) << 2;
}
}
static void render16_uyvy(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint16_t *input = input_;
uint8_t *output = (uint8_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint16_t p = *input++;
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = u;
output[1] = output[img_width + 1] = y0;
output[2] = output[img_width + 2] = v;
output[3] = output[img_width + 3] = y0;
output += 4;
}
input += (pitch >> 1) - width;
output += (xv->width - width) << 2;
}
}
static void render32_yuy2(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint32_t *input = input_;
uint8_t *output = (uint8_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint32_t p = *input++;
p = ((p >> 9) & 0x7c00) | ((p >> 6) & 0x03e0) | ((p >> 3) & 0x1f); // ARGB -> RGB15
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = y0;
output[1] = output[img_width + 1] = u;
output[2] = output[img_width + 2] = y0;
output[3] = output[img_width + 3] = v;
output += 4;
}
input += (pitch >> 2) - width;
output += (xv->width - width) << 2;
}
}
static void render32_uyvy(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint32_t *input = input_;
uint16_t *output = (uint16_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint32_t p = *input++;
p = ((p >> 9) & 0x7c00) | ((p >> 6) & 0x03e0) | ((p >> 3) & 0x1f); // ARGB -> RGB15
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = u;
output[1] = output[img_width + 1] = y0;
output[2] = output[img_width + 2] = v;
output[3] = output[img_width + 3] = y0;
output += 4;
}
input += (pitch >> 2) - width;
output += (xv->width - width) << 2;
}
}
static void* xv_init(const video_info_t *video, const input_driver_t **input, void **input_data)
{
xv_t *xv = calloc(1, sizeof(*xv));
if (!xv)
return NULL;
xv->display = XOpenDisplay(NULL);
if (!XShmQueryExtension(xv->display))
{
SSNES_ERR("XVideo: XShm extension not found.\n");
goto error;
}
xv->keep_aspect = video->force_aspect;
// Find an appropriate Xv port.
xv->port = 0;
XvAdaptorInfo *adaptor_info;
unsigned adaptor_count = 0;
XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &adaptor_count, &adaptor_info);
for (unsigned i = 0; i < adaptor_count; i++)
{
// Find adaptor that supports both input (memory->drawable) and image (drawable->screen) masks.
if (adaptor_info[i].num_formats < 1) continue;
if (!(adaptor_info[i].type & XvInputMask)) continue;
if (!(adaptor_info[i].type & XvImageMask)) continue;
xv->port = adaptor_info[i].base_id;
xv->depth = adaptor_info[i].formats->depth;
xv->visualid = adaptor_info[i].formats->visual_id;
break;
}
XvFreeAdaptorInfo(adaptor_info);
if (xv->port == 0)
{
SSNES_ERR("XVideo: Failed to find valid XvPort.\n");
goto error;
}
XVisualInfo visualtemplate;
visualtemplate.visualid = xv->visualid;
visualtemplate.screen = DefaultScreen(xv->display);
visualtemplate.depth = xv->depth;
visualtemplate.visual = 0;
int visualmatches = 0;
XVisualInfo *visualinfo = XGetVisualInfo(xv->display, VisualIDMask | VisualScreenMask | VisualDepthMask, &visualtemplate, &visualmatches);
if (visualmatches < 1 || !visualinfo->visual)
{
if (visualinfo) XFree(visualinfo);
SSNES_ERR("XVideo: Unable to find Xv-compatible visual.\n");
goto error;
}
xv->colormap = XCreateColormap(xv->display, DefaultRootWindow(xv->display), visualinfo->visual, AllocNone);
XSetWindowAttributes attributes;
attributes.colormap = xv->colormap;
attributes.border_pixel = 0;
attributes.event_mask = StructureNotifyMask | DestroyNotify | ClientMessage;
unsigned width = video->fullscreen ? ((video->width == 0) ? 256 : video->width) : video->width;
unsigned height = video->fullscreen ? ((video->height == 0) ? 224 : video->height) : video->height;
xv->window = XCreateWindow(xv->display, DefaultRootWindow(xv->display),
0, 0, width, height,
0, xv->depth, InputOutput, visualinfo->visual,
CWColormap | CWBorderPixel | CWEventMask, &attributes);
XFree(visualinfo);
XSetWindowBackground(xv->display, xv->window, 0);
XMapWindow(xv->display, xv->window);
char buf[128];
if (gfx_window_title(buf, sizeof(buf)))
XStoreName(xv->display, xv->window, buf);
if (video->fullscreen)
set_fullscreen(xv);
hide_mouse(xv);
xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
// Set colorkey to auto paint, so that Xv video output is always visible
Atom atom = XInternAtom(xv->display, "XV_AUTOPAINT_COLORKEY", true);
if(atom != None) XvSetPortAttribute(xv->display, xv->port, atom, 1);
int format_count;
XvImageFormatValues *format = XvListImageFormats(xv->display, xv->port, &format_count);
bool has_format = false;
for (int i = 0; i < format_count; i++)
{
if (format[i].type == XvYUV && format[i].bits_per_pixel == 16 && format[i].format == XvPacked)
{
if (format[i].component_order[0] == 'Y' && format[i].component_order[1] == 'U'
&& format[i].component_order[2] == 'Y' && format[i].component_order[3] == 'V')
{
has_format = true;
xv->fourcc = format[i].id;
xv->render_func = video->rgb32 ? render32_yuy2 : render16_yuy2;
xv->luma_i = 0; xv->chroma_i = 1;
break;
}
}
}
if (!has_format) for (int i = 0; i < format_count; i++)
{
if (format[i].type == XvYUV && format[i].bits_per_pixel == 16 && format[i].format == XvPacked)
{
if (format[i].component_order[0] == 'U' && format[i].component_order[1] == 'Y'
&& format[i].component_order[2] == 'V' && format[i].component_order[3] == 'Y')
{
has_format = true;
xv->fourcc = format[i].id;
xv->render_func = video->rgb32 ? render32_uyvy : render16_uyvy;
xv->chroma_i = 0; xv->luma_i = 1;
break;
}
}
}
free(format);
if (!has_format)
{
SSNES_ERR("XVideo: unable to find a supported image format.\n");
goto error;
}
xv->width = 512;
xv->height = 512;
xv->image = XvShmCreateImage(xv->display, xv->port, xv->fourcc, NULL, xv->width, xv->height, &xv->shminfo);
if (!xv->image)
{
SSNES_ERR("XVideo: XShmCreateImage failed.\n");
goto error;
}
xv->width = xv->image->width;
xv->height = xv->image->height;
xv->shminfo.shmid = shmget(IPC_PRIVATE, xv->image->data_size, IPC_CREAT | 0777);
xv->shminfo.shmaddr = xv->image->data = shmat(xv->shminfo.shmid, NULL, 0);
xv->shminfo.readOnly = false;
if (!XShmAttach(xv->display, &xv->shminfo))
{
SSNES_ERR("XVideo: XShmAttach failed.\n");
goto error;
}
XSync(xv->display, False);
memset(xv->image->data, 128, xv->image->data_size);
xv->quit_atom = XInternAtom(xv->display, "WM_DELETE_WINDOW", False);
if (xv->quit_atom)
XSetWMProtocols(xv->display, xv->window, &xv->quit_atom, 1);
struct sigaction sa;
sa.sa_handler = sighandler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
xv_set_nonblock_state(xv, !video->vsync);
xv->focus = true;
xv_init_font(xv, g_settings.video.font_path, g_settings.video.font_size);
void *xinput = input_x.init();
if (xinput)
{
*input = &input_x;
*input_data = xinput;
}
else
*input = NULL;
init_yuv_tables(xv);
return xv;
error:
free(xv);
return NULL;
}
static bool check_resize(xv_t *xv, unsigned width, unsigned height)
{
// We render @ 2x scale to combat chroma downsampling.
if (xv->width != (width << 1) || xv->height != (height << 1))
{
xv->width = width << 1;
xv->height = height << 1;
XShmDetach(xv->display, &xv->shminfo);
shmdt(xv->shminfo.shmaddr);
shmctl(xv->shminfo.shmid, IPC_RMID, NULL);
XFree(xv->image);
memset(&xv->shminfo, 0, sizeof(xv->shminfo));
xv->image = XvShmCreateImage(xv->display, xv->port, xv->fourcc, NULL, xv->width, xv->height, &xv->shminfo);
if (xv->image == None)
{
SSNES_ERR("Failed to create image.\n");
return false;
}
xv->width = xv->image->width;
xv->height = xv->image->height;
xv->shminfo.shmid = shmget(IPC_PRIVATE, xv->image->data_size, IPC_CREAT | 0777);
if (xv->shminfo.shmid < 0)
{
SSNES_ERR("Failed to init SHM.\n");
return false;
}
xv->shminfo.shmaddr = xv->image->data = shmat(xv->shminfo.shmid, NULL, 0);
xv->shminfo.readOnly = false;
if (!XShmAttach(xv->display, &xv->shminfo))
{
SSNES_ERR("Failed to reattch XvShm image.\n");
return false;
}
XSync(xv->display, False);
memset(xv->image->data, 128, xv->image->data_size);
}
return true;
}
static void calc_out_rect(bool keep_aspect, unsigned *x, unsigned *y, unsigned *width, unsigned *height, unsigned vp_width, unsigned vp_height)
{
if (!keep_aspect)
{
*x = 0; *y = 0; *width = vp_width; *height = vp_height;
}
else
{
float desired_aspect = g_settings.video.aspect_ratio;
float device_aspect = (float)vp_width / vp_height;
// If the aspect ratios of screen and desired aspect ratio are sufficiently equal (floating point stuff),
// assume they are actually equal.
if (fabs(device_aspect - desired_aspect) < 0.0001)
{
*x = 0; *y = 0; *width = vp_width; *height = vp_height;
}
else if (device_aspect > desired_aspect)
{
float delta = (desired_aspect / device_aspect - 1.0) / 2.0 + 0.5;
*x = vp_width * (0.5 - delta); *y = 0; *width = 2.0 * vp_width * delta; *height = vp_height;
}
else
{
float delta = (device_aspect / desired_aspect - 1.0) / 2.0 + 0.5;
*x = 0; *y = vp_height * (0.5 - delta); *width = vp_width; *height = 2.0 * vp_height * delta;
}
}
}
// TODO: Is there some way to render directly like GL? :(
static void xv_render_msg(xv_t *xv, const char *msg, unsigned width, unsigned height)
{
#ifdef HAVE_FREETYPE
if (!xv->font)
return;
struct font_output_list out;
font_renderer_msg(xv->font, msg, &out);
struct font_output *head = out.head;
int _base_x = g_settings.video.msg_pos_x * width;
int _base_y = height - g_settings.video.msg_pos_y * height;
unsigned luma_i = xv->luma_i;
unsigned chroma_i = xv->chroma_i;
while (head)
{
int base_x = _base_x + head->off_x;
int base_y = _base_y - head->off_y;
if (base_y >= 0)
{
for (int y = 0; y < head->height && (base_y + y) < height; y++)
{
if (base_x < 0)
continue;
const uint8_t *a = head->output + head->pitch * y;
uint8_t *out = (uint8_t*)xv->image->data + (base_y - head->height + y) * (width << 1) + (base_x << 1);
for (int x = 0; x < (head->width << 1) && (base_x + x) < width; x += 2)
{
// Blend luma
uint8_t blend = a[x >> 1];
unsigned blended = blend + (((256 - blend) * (unsigned)out[x + luma_i]) >> 8);
blended = blended > 255 ? 255 : blended;
out[x + luma_i] = blended;
// Blend chroma
blended = (128 * blend + ((256 - blend) * (unsigned)out[x + chroma_i])) >> 8;
blended = blended > 255 ? 255 : blended;
out[x + chroma_i] = blended;
}
}
}
head = head->next;
}
font_renderer_free_output(&out);
#else
(void)xv;
(void)msg;
(void)width;
(void)height;
#endif
}
static bool xv_frame(void *data, const void* frame, unsigned width, unsigned height, unsigned pitch, const char *msg)
{
xv_t *xv = data;
if (!check_resize(xv, width, height))
return false;
XWindowAttributes target;
XGetWindowAttributes(xv->display, xv->window, &target);
xv->render_func(xv, frame, width, height, pitch);
if (msg)
xv_render_msg(xv, msg, xv->width, xv->height);
unsigned x, y, owidth, oheight;
calc_out_rect(xv->keep_aspect, &x, &y, &owidth, &oheight, target.width, target.height);
XvShmPutImage(xv->display, xv->port, xv->window, xv->gc, xv->image,
0, 0, width << 1, height << 1,
x, y, owidth, oheight,
true);
XSync(xv->display, False);
char buf[128];
if (gfx_window_title(buf, sizeof(buf)))
XStoreName(xv->display, xv->window, buf);
return true;
}
static bool xv_alive(void *data)
{
xv_t *xv = data;
XEvent event;
while (XPending(xv->display))
{
XNextEvent(xv->display, &event);
switch (event.type)
{
case ClientMessage:
if (event.xclient.data.l[0] == xv->quit_atom)
return false;
break;
case DestroyNotify:
return false;
case MapNotify: // Find something that works better.
xv->focus = true;
break;
case UnmapNotify:
xv->focus = false;
break;
default:
break;
}
}
return !g_quit;
}
static bool xv_focus(void *data)
{
xv_t *xv = data;
return xv->focus;
}
static void xv_free(void *data)
{
xv_t *xv = data;
XShmDetach(xv->display, &xv->shminfo);
shmdt(xv->shminfo.shmaddr);
shmctl(xv->shminfo.shmid, IPC_RMID, NULL);
XFree(xv->image);
if(xv->window)
XUnmapWindow(xv->display, xv->window);
if(xv->colormap)
XFreeColormap(xv->display, xv->colormap);
XCloseDisplay(xv->display);
free(xv->ytable);
free(xv->utable);
free(xv->vtable);
#ifdef HAVE_FREETYPE
if (xv->font)
font_renderer_free(xv->font);
#endif
free(xv);
}
const video_driver_t video_xvideo = {
.init = xv_init,
.frame = xv_frame,
.alive = xv_alive,
.set_nonblock_state = xv_set_nonblock_state,
.focus = xv_focus,
.free = xv_free,
.ident = "xvideo"
};
-45
View File
@@ -1,45 +0,0 @@
#include "bleed.h"
#include <stdlib.h>
#include <string.h>
void bleed_filter(uint16_t *data, int width, int height)
{
uint16_t tmp[4];
int ptr = 0;
uint16_t r[4];
uint16_t g[4];
uint16_t b[4];
uint16_t bleed_r;
uint16_t bleed_g;
uint16_t bleed_b;
float rand_map[4];
for (int h = 0; h < height; h++ )
{
memcpy(tmp, data, sizeof(tmp));
ptr = 3;
for (int i = 0; i < 4; i++)
{
rand_map[i] = (rand() % 20)/1000.0; // 0.02 * 4 = 0.08
}
for (int i = 3; i < width; i++, ptr++)
{
tmp[ptr & 0x3] = data[i];
for (int y = 0; y < 4; y++)
{
r[y] = (tmp[(ptr + y) & 0x3] >> 10) & 0x1F;
g[y] = (tmp[(ptr + y) & 0x3] >> 5) & 0x1F;
b[y] = (tmp[(ptr + y) & 0x3] >> 0) & 0x1F;
}
bleed_r = r[0] * (0.05 + rand_map[0] ) + r[1] * (0.10 + rand_map[1] ) + r[2] * (0.20 + rand_map[2]) + r[3] * (0.57 + rand_map[3] ); // 0.92
bleed_g = g[0] * (0.03 + rand_map[2]/3) + g[1] * (0.10 + rand_map[0]/3) + g[2] * (0.20 + rand_map[3]/3) + g[3] * (0.63 + rand_map[1]/3); // 0.96
bleed_b = b[0] * (0.05 + rand_map[3] ) + b[1] * (0.10 + rand_map[2] ) + b[2] * (0.20 + rand_map[1]) + b[3] * (0.57 + rand_map[0] ); // 0.92
data[i] = (bleed_r << 10) | (bleed_g << 5) | (bleed_b);
}
data += width;
}
}
-3
View File
@@ -1,3 +0,0 @@
#include <stdint.h>
void bleed_filter(uint16_t *data, int width, int height);
-94
View File
@@ -1,94 +0,0 @@
/*
Copyright (C) 2007 guest(r) - [email protected]
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
The 2xSaL shader processes a gfx. surface and redraws it 2x finer.
A linear post-resize can fit the image to any resolution.
Note: set scaler to normal2x.
*/
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
struct deltas
{
float2 UL, UR, DL, DR;
};
output main_fragment (float2 tex : TEXCOORD0, uniform input IN, uniform sampler2D s_p : TEXUNIT0)
{
float2 texsize = IN.texture_size;
float dx = pow(texsize.x, -1.0) * 0.25;
float dy = pow(texsize.y, -1.0) * 0.25;
float3 dt = float3(1.0, 1.0, 1.0);
deltas VAR = {
tex + float2(-dx, -dy),
tex + float2(dx, -dy),
tex + float2(-dx, dy),
tex + float2(dx, dy)
};
float3 c00 = tex2D(s_p, VAR.UL).xyz;
float3 c20 = tex2D(s_p, VAR.UR).xyz;
float3 c02 = tex2D(s_p, VAR.DL).xyz;
float3 c22 = tex2D(s_p, VAR.DR).xyz;
float m1=dot(abs(c00-c22),dt)+0.001;
float m2=dot(abs(c02-c20),dt)+0.001;
output OUT;
OUT.color = float4((m1*(c02+c20)+m2*(c22+c00))/(2.0*(m1+m2)),1.0);
return OUT;
}
-95
View File
@@ -1,95 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
float Luminance = 0.09f;
static const float fMiddleGray = 0.18f;
static const float fWhiteCutoff = 0.8f;
#define NUM 13
float2 PixelOffsets[NUM] =
{
{ -0.006, -0.006 },
{ -0.005, -0.005 },
{ -0.004, -0.004 },
{ -0.003, -0.003 },
{ -0.002, -0.002 },
{ -0.001, -0.001 },
{ 0.000, 0.000 },
{ 0.001, 0.001 },
{ 0.002, 0.002 },
{ 0.003, 0.003 },
{ 0.004, 0.004 },
{ 0.005, 0.005 },
{ 0.006, 0.006 },
};
static const float BlurWeights[NUM] =
{
0.002216,
0.008764,
0.026995,
0.064759,
0.120985,
0.176033,
0.199471,
0.176033,
0.120985,
0.064759,
0.026995,
0.008764,
0.002216,
};
struct output
{
float4 color : COLOR;
};
output main_fragment( float2 Tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0 )
{
float2 BloomSettings;
BloomSettings.x = 1;
BloomSettings.y = 0.3;
float3 pixel;
float3 Color = 0;
for(int i = 0; i < NUM; i++)
{
pixel = tex2D(s0, Tex + PixelOffsets[i] * 5.0f)+BloomSettings.y;
pixel *= fMiddleGray / (Luminance + 0.001f);
pixel *= (1.0f + (pixel / (fWhiteCutoff * fWhiteCutoff)));
pixel -= 5.0f;
pixel = max(pixel,0.0f);
pixel /= (10.0f + pixel);
Color += pixel * BlurWeights[i];
}
Color *= BloomSettings.x;
output OUT;
OUT.color = float4(Color,1.0) + tex2D(s0,Tex);
return OUT;
}
-55
View File
@@ -1,55 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
const float2 samples[12]
= {
-0.326212, -0.405805,
-0.840144, -0.073580,
-0.695914, 0.457137,
-0.203345, 0.620716,
0.962340, -0.194983,
0.473434, -0.480026,
0.519456, 0.767022,
0.185461, -0.893124,
0.507431, 0.064425,
0.896420, 0.412458,
-0.321940, -0.932615,
-0.791559, -0.597705
};
struct output
{
float4 col : COLOR;
};
output main_fragment(in float2 Tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0 )
{
float4 color = tex2D( s0, Tex.xy);
float BlurFactor = 0.0025; //set between 0.001 and 0.05
for (int i = 0; i < 12; i++)
{
color += tex2D(s0, Tex + BlurFactor * samples[i]);
}
output OUT;
OUT.col = color / 13;
return OUT;
}
-100
View File
@@ -1,100 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
#define TEX2D(c) tex2D(decal,(c))
#define PI 3.141592653589
#define phase 0.0
#define gamma 2.5
#define distortion 0.04
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
float2 barrelDistortion(float2 coord)
{
float2 cc = coord - 0.5;
float dist = dot(cc, cc);
return coord + cc * (dist + distortion * dist * dist) * distortion;
}
output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0, uniform input IN)
{
output OUT;
float2 rubyInputSize = IN.video_size;
float2 rubyOutputSize = IN.output_size;
float2 rubyTextureSize = IN.texture_size;
float2 xy = barrelDistortion(texCoord.xy);
float2 one = 1.0/rubyTextureSize;
xy = xy + float2(0.0 , -0.5 * (phase + (1-phase) * rubyInputSize.y/rubyOutputSize.y) * one.y);
float4 texels[8];
texels[0] = TEX2D(xy + float2(-one.x,0.0));
texels[1] = TEX2D(xy);
texels[2] = TEX2D(xy + float2(one.x, 0.0));
texels[3] = TEX2D(xy + float2(2 * one.x, 0.0));
texels[4] = TEX2D(xy + float2(-one.x,one.y));
texels[5] = TEX2D(xy + float2(0.0, one.y));
texels[6] = TEX2D(xy + one);
texels[7] = TEX2D(xy + float2(2 * one.x, one.y));
float2 uv_ratio = frac(xy*rubyTextureSize);
float4 col, col2;
float4 coeffs = float4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x);
coeffs = (sin(PI * coeffs) * sin(PI * coeffs / 2.0)) / (coeffs * coeffs);
coeffs = coeffs / (coeffs.x+coeffs.y+coeffs.z+coeffs.w);
col = clamp(coeffs.x * texels[0] + coeffs.y * texels[1] + coeffs.z * texels[2] + coeffs.w * texels[3], 0.0, 1.0);
col2 = clamp(coeffs.x * texels[4] + coeffs.y * texels[5] + coeffs.z * texels[6] + coeffs.w * texels[7], 0.0, 1.0);
col = pow(col, gamma);
col2 = pow(col2, gamma);
float4 wid = 2 + 2 * pow(col, 4.0);
float4 weights = uv_ratio.y/0.3;
weights = 0.51*exp(-pow(weights*sqrt(2/wid),wid))/0.3/(0.6+0.2*wid);
wid = 2 + 4 * pow(col2,4.0);
float4 weights2 = (1.0-uv_ratio.y)/0.3;
weights2 = 0.51*exp(-pow(weights2*sqrt(2/wid),wid))/0.3/(0.6+0.2*wid);
float4 mcol = 1.0;
if ( fmod(xy.x*rubyOutputSize.x,2.0) < 1.0)
mcol.g = 0.7;
else
mcol.rb = 0.7;
//OUT.color = pow(mcol*(col * weights + col2 * weights2), 1.0/2.2);
OUT.color = 1.0;
return OUT;
}
-94
View File
@@ -1,94 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
const float mx = 0.325; // start smoothing wt.
const float k = -0.250; // wt. decrease factor
const float max_w = 0.25; // max filter weigth
const float min_w =-0.05; // min filter weigth
const float lum_add = 0.25; // effects smoothing
output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0, uniform input IN)
{
float x = 0.5 * (1.0 / IN.texture_size.x);
float y = 0.5 * (1.0 / IN.texture_size.y);
float2 dg1 = float2( x, y);
float2 dg2 = float2(-x, y);
float2 dx = float2(x, 0.0);
float2 dy = float2(0.0, y);
float4 TexCoord[5];
TexCoord[0] = float4(texCoord, 0.0, 0.0);
TexCoord[1].xy = TexCoord[0].xy - dg1;
TexCoord[1].zw = TexCoord[0].xy - dy;
TexCoord[2].xy = TexCoord[0].xy - dg2;
TexCoord[2].zw = TexCoord[0].xy + dx;
TexCoord[3].xy = TexCoord[0].xy + dg1;
TexCoord[3].zw = TexCoord[0].xy + dy;
TexCoord[4].xy = TexCoord[0].xy + dg2;
TexCoord[4].zw = TexCoord[0].xy - dx;
float3 c00 = tex2D(decal, TexCoord[1].xy).xyz;
float3 c10 = tex2D(decal, TexCoord[1].zw).xyz;
float3 c20 = tex2D(decal, TexCoord[2].xy).xyz;
float3 c01 = tex2D(decal, TexCoord[4].zw).xyz;
float3 c11 = tex2D(decal, TexCoord[0].xy).xyz;
float3 c21 = tex2D(decal, TexCoord[2].zw).xyz;
float3 c02 = tex2D(decal, TexCoord[4].xy).xyz;
float3 c12 = tex2D(decal, TexCoord[3].zw).xyz;
float3 c22 = tex2D(decal, TexCoord[3].xy).xyz;
float3 dt = float3(1.0, 1.0, 1.0);
float md1 = dot(abs(c00 - c22), dt);
float md2 = dot(abs(c02 - c20), dt);
float w1 = dot(abs(c22 - c11), dt) * md2;
float w2 = dot(abs(c02 - c11), dt) * md1;
float w3 = dot(abs(c00 - c11), dt) * md2;
float w4 = dot(abs(c20 - c11), dt) * md1;
float t1 = w1 + w3;
float t2 = w2 + w4;
float ww = max(t1, t2) + 0.0001;
c11 = (w1 * c00 + w2 * c20 + w3 * c22 + w4 * c02 + ww * c11) / (t1 + t2 + ww);
float lc1 = k / (0.12 * dot(c10 + c12 + c11, dt) + lum_add);
float lc2 = k / (0.12 * dot(c01 + c21 + c11, dt) + lum_add);
w1 = clamp(lc1 * dot(abs(c11 - c10), dt) + mx, min_w, max_w);
w2 = clamp(lc2 * dot(abs(c11 - c21), dt) + mx, min_w, max_w);
w3 = clamp(lc1 * dot(abs(c11 - c12), dt) + mx, min_w, max_w);
w4 = clamp(lc2 * dot(abs(c11 - c01), dt) + mx, min_w, max_w);
output OUT;
OUT.color = float4(w1 * c10 + w2 * c21 + w3 * c12 + w4 * c01 + (1.0 - w1 - w2 - w3 - w4) * c11, 1.0);
return OUT;
}
-87
View File
@@ -1,87 +0,0 @@
/*
Author: Themaister
License: Public domain
*/
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
struct deltas
{
float2 UL, UR, DL, DR;
};
float3 quad_inter(float3 x0, float3 x1, float3 x2, float x)
{
float3 poly[3];
poly[2] = 0.5*x0 - x1 + 0.5*x2;
poly[1] = -1.5*x0 + 2.0*x1 - 0.5*x2;
poly[0] = x0;
return poly[2] * x * x + poly[1] * x + poly[0];
}
output main_fragment (float2 tex : TEXCOORD0, uniform input IN, uniform sampler2D s0 : TEXUNIT0)
{
float2 texsize = IN.texture_size;
float sharpness = 2.0;
float dx = float(pow(sharpness * texsize.x, -1.0));
float dy = float(pow(sharpness * texsize.y, -1.0));
float3 c00 = tex2D(s0, tex + float2(-dx, -dy)).xyz;
float3 c01 = tex2D(s0, tex + float2(-dx, 0)).xyz;
float3 c02 = tex2D(s0, tex + float2(-dx, dy)).xyz;
float3 c10 = tex2D(s0, tex + float2(0, -dy)).xyz;
float3 c11 = tex2D(s0, tex + float2(0, 0)).xyz;
float3 c12 = tex2D(s0, tex + float2(0, dy)).xyz;
float3 c20 = tex2D(s0, tex + float2(dx, -dy)).xyz;
float3 c21 = tex2D(s0, tex + float2(dx, 0)).xyz;
float3 c22 = tex2D(s0, tex + float2(dx, dy)).xyz;
float frac_amt_x = frac(tex.x * texsize.x);
float frac_amt_y = frac(tex.y * texsize.y);
float3 loval = quad_inter(c00, c10, c20, frac_amt_x + 0.5);
float3 midval = quad_inter(c01, c11, c21, frac_amt_x + 0.5);
float3 hival = quad_inter(c02, c12, c22, frac_amt_x + 0.5);
float3 res = quad_inter(loval, midval, hival, frac_amt_y + 0.5);
output OUT;
// Bilinear!
// float3 first = lerp(c00, c20, frac(tex.x * texsize.x + 0.5));
// float3 second = lerp(c02, c22, frac(tex.x * texsize.x + 0.5));
// float3 res = lerp(first, second, frac(tex.y * texsize.y + 0.5));
// OUT.color = float4(res, 1.0);
OUT.color = float4(res, 1.0);
return OUT;
}
-46
View File
@@ -1,46 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0, uniform input IN)
{
output OUT;
float winHeight = IN.output_size.y;
float4 tex_color = tex2D(decal, texCoord);
OUT.color = (1.0 - 0.60 * floor(fmod(texCoord.y * winHeight, 2.0))) * tex_color;
return OUT;
}
-38
View File
@@ -1,38 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
output main_fragment( in float2 Tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0 )
{
float4 Color = tex2D( s0, Tex.xy );
float Sharpenfactor = 30; //Make this between 10 and 50;
Color -= tex2D( s0, Tex.xy+0.0001)*Sharpenfactor;
Color += tex2D( s0, Tex.xy-0.0001)*Sharpenfactor;
Color.a = 1.0;
output OUT;
OUT.color = Color;
return OUT;
}
-21
View File
@@ -1,21 +0,0 @@
/* Very simple grayscale filter.
* Author: Hans-Kristian Arntzen
* License: Public domain
*/
#include "grayscale.h"
// Input format 0RRRRRGGGGGBBBBB. Colors themselves could be switched around.
void grayscale_filter(uint16_t *data, int width, int height)
{
for (int i = 0; i < width * height; i++ )
{
int r, g, b, color;
r = (data[i] >> 10) & 0x1F;
g = (data[i] >> 5) & 0x1F;
b = (data[i] >> 0) & 0x1F;
color = (int)(r * 0.3 + g * 0.59 + b * 0.11);
data[i] = (color << 10) | (color << 5) | color;
}
}
-7
View File
@@ -1,7 +0,0 @@
/* Very simple grayscale filter.
* Author: Hans-Kristian Arntzen
* License: Public domain
*/
#include <stdint.h>
void grayscale_filter(uint16_t *data, int width, int height);
-555
View File
@@ -1,555 +0,0 @@
/* This source code is Copyright (C) 2006-2009 by WolfWings. */
/* It is, however, released under the ISC license. */
/* en.wikipedia.org/wiki/ISC_Licence */
/* --------------------------------------------------------- */
/* It is a formula-level rederiviation of the HQ2x technique */
/* and is, thus, not a derivative work of the original code, */
/* only the original equations behind the code. */
#include <stdlib.h>
#include <string.h>
#include "pastlib.h"
static const uint8_t blends_2x[14*4] = {
/* 5 2 4 1 */
8, 4, 4, 0,
8, 4, 0, 4,
8, 0, 4, 4,
16, 0, 0, 0,
12, 4, 0, 0,
12, 0, 4, 0,
12, 0, 0, 4,
4, 6, 6, 0, /* Interp9 */
12, 2, 2, 0, /* Interp7 */
14, 1, 1, 0, /* Interp10*/
10, 4, 2, 0, /* Interp6 */
10, 2, 4, 0, /* Interp6 */
4, 6, 6, 0, /* Added for secondary blending used in HQ4x */
8, 4, 4, 0, /* Added for secondary blending used in HQ4x */
};
static const uint8_t blends_4x[14*16] = {
/* 5 2 4 1 5 2 4 1 5 2 4 1 5 2 4 1 */
8, 4, 4, 0, 10, 4, 2, 0, 10, 2, 4, 0, 12, 2, 2, 0, /* Needs to be split. B */
10, 0, 0, 6, 10, 4, 0, 2, 12, 0, 0, 4, 14, 0, 0, 2,
10, 0, 0, 6, 10, 0, 4, 2, 12, 0, 0, 4, 14, 0, 0, 2,
16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, /* Solid colors */
10, 6, 0, 0, 10, 6, 0, 0, 14, 2, 0, 0, 14, 2, 0, 0,
10, 0, 6, 0, 14, 0, 2, 0, 10, 0, 6, 0, 14, 0, 2, 0,
10, 0, 0, 6, 12, 0, 0, 4, 12, 0, 0, 4, 14, 0, 0, 2,
0, 8, 8, 0, 4, 8, 4, 0, 0, 6,10, 0, 12, 2, 2, 0, /* Needs to be split. A */
8, 4, 4, 0, 12, 4, 0, 0, 12, 0, 4, 0, 16, 0, 0, 0,
8, 4, 4, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0,
12, 4, 0, 0, 4,12, 0, 0, 10, 0, 4, 0, 14, 0, 2, 0,
12, 0, 4, 0, 10, 4, 0, 0, 4, 0,12, 0, 14, 2, 0, 0,
0, 8, 8, 0, 0,10, 6, 0, 4, 4, 8, 0, 12, 2, 2, 0, /* Needs to be split. A */
0, 8, 8, 0, 8, 8, 0, 0, 8, 0, 8, 0, 16, 0, 0, 0, /* Needs to be split. B */
};
static const uint8_t tree_hq[0x800] = {
/* 6 6 6 6 6 6 6 6
* 2 2 2 2 2 2 2 2
* 4 4 4 4 4 4 4 4
* 8 8 8 8 8 8 8 8
*/
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8, /* */
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8, /* 3 */
0, 0, 5,10, 4, 4,13,13, 0, 0, 5, 5,11, 4,13,13, /* 1 */
0, 0, 5,10, 4, 4,12,13, 0, 0, 5, 5,11, 4,13,13, /* 31 */
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8, /* 7 */
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6, /* 3 7 */
0, 0, 5,10, 4, 4, 7,13, 0, 0, 5, 5,11, 4,13,13, /* 17 */
0, 0, 5,10, 4, 4, 9, 9, 0, 0, 5,10,11,11, 9, 9, /* 317 */
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8, /* 9 */
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6, /* 3 9 */
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13, /* 1 9 */
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13, /* 31 9 */
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6, /* 79 */
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6, /* 3 79 */
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13, /* 179 */
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9, /* 3179 */
/* DIFF26 */
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8,
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5,11, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5,11, 4,13,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7,13, 0, 0, 5, 5,11, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5,11,11, 9, 9,
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
/* DIFF24 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5,10,11,11, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
/* DIFF26 DIFF24 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11,11, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
/* DIFF84 */
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8,
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 5,10, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5,10, 4, 4,12,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 7,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5,10, 4, 4, 9, 9, 0, 0, 5,10, 4, 4, 9, 9,
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
/* DIFF26 DIFF84 */
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8,
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
/* DIFF24 DIFF84 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5,10, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
/* DIFF26 DIFF24 DIFF84 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3};
#define RB(y,u,v) (((y)&0x3FF)<<22)|(((u)&0x3FF)<<11)|((v)&0x3FF)
#define GO(y,v) ((((y)+64)&0x3FF)<<22)|(16<<11)|(((v)+16)&0x3FF)
static const uint32_t DiffTable[128] = {
RB(-85, 160, 80), RB(-83, 155, 77), RB(-80, 150, 75), RB(-77, 145, 72),
RB(-75, 140, 70), RB(-72, 135, 67), RB(-69, 130, 65), RB(-67, 125, 62),
RB(-64, 120, 60), RB(-61, 115, 57), RB(-59, 110, 55), RB(-56, 105, 52),
RB(-53, 100, 50), RB(-51, 95, 47), RB(-48, 90, 45), RB(-45, 85, 42),
RB(-43, 80, 40), RB(-40, 75, 37), RB(-37, 70, 35), RB(-35, 65, 32),
RB(-32, 60, 30), RB(-29, 55, 27), RB(-27, 50, 25), RB(-24, 45, 22),
RB(-21, 40, 20), RB(-19, 35, 17), RB(-16, 30, 15), RB(-13, 25, 12),
RB(-11, 20, 10), RB( -8, 15, 7), RB( -5, 10, 5), RB( -3, 5, 2),
RB( 0, 0, 0), RB( 2, -5, -2), RB( 5, -10, -5), RB( 7, -15, -7),
RB( 10, -20, -10), RB( 13, -25, -12), RB( 15, -30, -15), RB( 18, -35, -17),
RB( 21, -40, -20), RB( 23, -45, -22), RB( 26, -50, -25), RB( 29, -55, -27),
RB( 31, -60, -30), RB( 34, -65, -32), RB( 37, -70, -35), RB( 39, -75, -37),
RB( 42, -80, -40), RB( 45, -85, -42), RB( 47, -90, -45), RB( 50, -95, -47),
RB( 53,-100, -50), RB( 55,-105, -52), RB( 58,-110, -55), RB( 61,-115, -57),
RB( 63,-120, -60), RB( 66,-125, -62), RB( 69,-130, -65), RB( 71,-135, -67),
RB( 74,-140, -70), RB( 77,-145, -72), RB( 79,-150, -75), RB( 82,-155, -77),
GO(-84, -160), GO(-81, -155), GO(-78, -150), GO(-76, -145),
GO(-73, -140), GO(-70, -135), GO(-68, -130), GO(-65, -125),
GO(-63, -120), GO(-60, -115), GO(-57, -110), GO(-55, -105),
GO(-52, -100), GO(-49, -95), GO(-47, -90), GO(-44, -85),
GO(-42, -80), GO(-39, -75), GO(-36, -70), GO(-34, -65),
GO(-31, -60), GO(-28, -55), GO(-26, -50), GO(-23, -45),
GO(-21, -40), GO(-18, -35), GO(-15, -30), GO(-13, -25),
GO(-10, -20), GO( -7, -15), GO( -5, -10), GO( -2, -5),
GO( 0, 0), GO( 2, 4), GO( 5, 9), GO( 7, 14),
GO( 10, 19), GO( 13, 24), GO( 15, 29), GO( 18, 34),
GO( 21, 39), GO( 23, 44), GO( 26, 49), GO( 28, 54),
GO( 31, 59), GO( 34, 64), GO( 36, 69), GO( 39, 74),
GO( 42, 79), GO( 44, 84), GO( 47, 89), GO( 49, 94),
GO( 52, 99), GO( 55, 104), GO( 57, 109), GO( 60, 114),
GO( 63, 119), GO( 65, 124), GO( 68, 129), GO( 70, 134),
GO( 73, 139), GO( 76, 144), GO( 78, 149), GO( 81, 154)};
inline static int ExpandedDiff (uint32_t c0, uint32_t c1) {
uint32_t r, g;
g = c0;
g += 0x4008020;
g -= c1;
r = DiffTable[(int)((unsigned char)g) + 0];
r ^= (0x3FF << 11);
g >>= 10;
r += DiffTable[(int)((unsigned char)g) + 0];
g >>= 11;
r += DiffTable[(int)((unsigned char)g) + 64];
r &= 0xE01F03E0;
return r;
}
#define DIFF56 0x001
#define DIFF52 0x002
#define DIFF54 0x004
#define DIFF58 0x008
#define DIFF53 0x010
#define DIFF51 0x020
#define DIFF57 0x040
#define DIFF59 0x080
#define DIFF26 0x100
#define DIFF24 0x200
#define DIFF84 0x400
#define DIFF86 0x800
/* lastLineDiffs is previous line's 52, 53, 26, all >> 1 */
static uint8_t lastLineDiffs[__PAST_LIBRARY_WIDTH];
#define RotatePattern(x) ((((x) & 0x777) << 1) | (((x) & 0x888) >> 3))
void ProcessHQ2x(const pixel * restrict in, pixel * restrict out) {
signed int y, x;
unsigned int pattern, newpattern;
uint32_t pixels[9];
int prevline, nextline;
memset(lastLineDiffs, 0, sizeof(lastLineDiffs));
prevline = 1;
nextline = 1 + __PAST_LIBRARY_WIDTH;
y = __PAST_LIBRARY_HEIGHT - 1;
do {
pixels[1-1] =
pixels[2-1] = RGBUnpack(in[prevline-1]); /* Pixel 2 */
pixels[3-1] = RGBUnpack(in[prevline ]); /* Pixel 3 */
pixels[4-1] =
pixels[5-1] = RGBUnpack(in[ 0]); /* Pixel 5 */
pixels[6-1] = RGBUnpack(in[ 1]); /* Pixel 6 */
pixels[7-1] =
pixels[8-1] = RGBUnpack(in[nextline-1]); /* Pixel 8 */
pixels[9-1] = RGBUnpack(in[nextline ]); /* Pixel 9 */
pattern = 0;
x = __PAST_LIBRARY_WIDTH - 1;
do {
newpattern = 0;
if (pattern & DIFF26) newpattern |= DIFF51;
if (pattern & DIFF56) newpattern |= DIFF54;
if (pattern & DIFF86) newpattern |= DIFF57;
if (pattern & DIFF53) newpattern |= DIFF24;
if (pattern & DIFF59) newpattern |= DIFF84;
if (lastLineDiffs[x] & (DIFF52 >> 1)) newpattern |= DIFF58;
if (lastLineDiffs[x] & (DIFF26 >> 1)) newpattern |= DIFF59;
if (lastLineDiffs[x] & (DIFF53 >> 1)) newpattern |= DIFF86;
pattern = newpattern;
if (ExpandedDiff(pixels[5-1], pixels[2-1])) pattern |= DIFF52;
if (ExpandedDiff(pixels[5-1], pixels[3-1])) pattern |= DIFF53;
if (ExpandedDiff(pixels[5-1], pixels[6-1])) pattern |= DIFF56;
if (ExpandedDiff(pixels[2-1], pixels[6-1])) pattern |= DIFF26;
lastLineDiffs[x] = pattern >> 1;
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 0] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[2-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[4-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[1-1] * blends_2x[(newpattern * 4) + 3])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 1] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[6-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[2-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[3-1] * blends_2x[(newpattern * 4) + 3])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*2)+1] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[8-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[6-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[9-1] * blends_2x[(newpattern * 4) + 3])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*2) ] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[4-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[8-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[7-1] * blends_2x[(newpattern * 4) + 3])) / 16);
out += 2;
in++;
x--;
if (x == 0) {
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
continue;
}
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[3-1] = RGBUnpack(in[prevline]); /* Pixel 3 */
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[6-1] = RGBUnpack(in[1 ]); /* Pixel 6 */
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
pixels[9-1] = RGBUnpack(in[nextline]); /* Pixel 9 */
} while (x >= 0);
prevline = 1 - __PAST_LIBRARY_WIDTH;
out += (__PAST_LIBRARY_WIDTH * 2);
y--;
if (y > 0) {
continue;
}
nextline = 0;
} while (y >= 0);
}
void ProcessHQ4x(const pixel * restrict in, pixel * restrict out) {
signed int y, x;
unsigned int pattern, newpattern;
uint32_t pixels[9];
int prevline, nextline;
memset(lastLineDiffs, 0, sizeof(lastLineDiffs));
prevline = 1;
nextline = 1 + __PAST_LIBRARY_WIDTH;
y = __PAST_LIBRARY_HEIGHT - 1;
do {
pixels[1-1] =
pixels[2-1] = RGBUnpack(in[prevline-1]); /* Pixel 2 */
pixels[3-1] = RGBUnpack(in[prevline ]); /* Pixel 3 */
pixels[4-1] =
pixels[5-1] = RGBUnpack(in[ 0]); /* Pixel 5 */
pixels[6-1] = RGBUnpack(in[ 1]); /* Pixel 6 */
pixels[7-1] =
pixels[8-1] = RGBUnpack(in[nextline-1]); /* Pixel 8 */
pixels[9-1] = RGBUnpack(in[nextline ]); /* Pixel 9 */
pattern = 0;
x = __PAST_LIBRARY_WIDTH - 1;
do {
newpattern = 0;
if (pattern & DIFF26) newpattern |= DIFF51;
if (pattern & DIFF56) newpattern |= DIFF54;
if (pattern & DIFF86) newpattern |= DIFF57;
if (pattern & DIFF53) newpattern |= DIFF24;
if (pattern & DIFF59) newpattern |= DIFF84;
if (lastLineDiffs[x] & (DIFF52 >> 1)) newpattern |= DIFF58;
if (lastLineDiffs[x] & (DIFF26 >> 1)) newpattern |= DIFF59;
if (lastLineDiffs[x] & (DIFF53 >> 1)) newpattern |= DIFF86;
pattern = newpattern;
if (ExpandedDiff(pixels[5-1], pixels[2-1])) pattern |= DIFF52;
if (ExpandedDiff(pixels[5-1], pixels[3-1])) pattern |= DIFF53;
if (ExpandedDiff(pixels[5-1], pixels[6-1])) pattern |= DIFF56;
if (ExpandedDiff(pixels[2-1], pixels[6-1])) pattern |= DIFF26;
lastLineDiffs[x] = pattern >> 1;
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 0] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[ 1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4) ] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4)+1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4)+3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[ 2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4)+2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*0xC)+3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[(__PAST_LIBRARY_WIDTH*0xC)+2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8)+3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8)+2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*0xC) ] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8) ] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[(__PAST_LIBRARY_WIDTH*0xC)+1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8)+1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
out += 4;
in++;
x--;
if (x == 0) {
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
continue;
}
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[3-1] = RGBUnpack(in[prevline]); /* Pixel 3 */
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[6-1] = RGBUnpack(in[1 ]); /* Pixel 6 */
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
pixels[9-1] = RGBUnpack(in[nextline]); /* Pixel 9 */
} while (x >= 0);
prevline = 1 - __PAST_LIBRARY_WIDTH;
out += (__PAST_LIBRARY_WIDTH * 0xC);
y--;
if (y > 0) {
continue;
}
nextline = 0;
} while (y >= 0);
}
-19
View File
@@ -1,19 +0,0 @@
#include "ntsc.h"
#include <stdlib.h>
#include <stdbool.h>
void ntsc_filter(uint16_t * restrict out, const uint16_t * restrict in, int width, int height)
{
static int phase = 0;
static snes_ntsc_t ntsc;
static bool inited = false;
if (inited == false)
{
snes_ntsc_init(&ntsc, &snes_ntsc_composite);
inited = true;
}
snes_ntsc_blit(&ntsc, in, width, phase, width, height, out, SNES_NTSC_OUT_WIDTH(width) * sizeof(uint16_t));
phase ^= 1;
}
-9
View File
@@ -1,9 +0,0 @@
#ifndef _NTSC_FILTER_H
#define _NTSC_FILTER_H
#include <stdint.h>
#include "snes_ntsc/snes_ntsc.h"
void ntsc_filter(uint16_t * restrict out, const uint16_t * restrict in, int width, int height);
#endif
-54
View File
@@ -1,54 +0,0 @@
/* This source code is Copyright (C) 2006-2009 by WolfWings. */
/* It is, however, released under the ISC license. */
/* en.wikipedia.org/wiki/ISC_Licence */
/* --------------------------------------------------------- */
/* It is a formula-level rederiviation of the HQ2x technique */
/* and is, thus, not a derivative work of the original code, */
/* only the original equations behind the code. */
#ifndef __PAST_LIBRARY_H
#define __PAST_LIBRARY_H
#include <stdint.h>
#ifndef __PAST_LIBRARY_WIDTH
#define __PAST_LIBRARY_WIDTH 256
#endif
#ifndef __PAST_LIBRARY_HEIGHT
#define __PAST_LIBRARY_HEIGHT 224
#endif
typedef uint16_t pixel;
/* These two functions are what need to be modified to interface other
* pixel formats with the library. They need to return values inside a
* format like this: (Red/Blue can be reversed.)
*
* 0000-00GG-GGG0-0000-0RRR-RR00-000B-BBBB
*
* This is VERY fast to create from 15-bit (NOT 16-bit) RGB/BGR, as
* below.
*
* If you can't return data in this format, you'll have to rebuild the
* ExpandedDiff function and DiffTable array to compensate, and possibly
* change the blending functions in the Process* functions to handle any
* guard-bits and masking.
*/
static inline uint32_t RGBUnpack(pixel i) {
uint32_t o = i;
o = (o * 0x10001);
o = o & 0x03E07C1F;
return o;
}
static inline pixel RGBPack(uint32_t x) {
x &= 0x03E07C1F;
x |= (x >> 16);
return x;
}
void ProcessHQ2x(const pixel * restrict inbuffer, pixel * restrict outbuffer);
void ProcessHQ4x(const pixel * restrict inbuffer, pixel * restrict outbuffer);
#endif /* __PAST_LIBRARY_H */
-251
View File
@@ -1,251 +0,0 @@
/* snes_ntsc 0.2.2. http://www.slack.net/~ant/ */
#include "snes_ntsc.h"
/* Copyright (C) 2006-2007 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
snes_ntsc_setup_t const snes_ntsc_monochrome = { 0,-1, 0, 0,.2, 0,.2,-.2,-.2,-1, 1, 0, 0 };
snes_ntsc_setup_t const snes_ntsc_composite = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };
snes_ntsc_setup_t const snes_ntsc_svideo = { 0, 0, 0, 0,.2, 0,.2, -1, -1, 0, 1, 0, 0 };
snes_ntsc_setup_t const snes_ntsc_rgb = { 0, 0, 0, 0,.2, 0,.7, -1, -1,-1, 1, 0, 0 };
#define alignment_count 3
#define burst_count 3
#define rescale_in 8
#define rescale_out 7
#define artifacts_mid 1.0f
#define fringing_mid 1.0f
#define std_decoder_hue 0
#define rgb_bits 7 /* half normal range to allow for doubled hires pixels */
#define gamma_size 32
#include "snes_ntsc_impl.h"
/* 3 input pixels -> 8 composite samples */
pixel_info_t const snes_ntsc_pixels [alignment_count] = {
{ PIXEL_OFFSET( -4, -9 ), { 1, 1, .6667f, 0 } },
{ PIXEL_OFFSET( -2, -7 ), { .3333f, 1, 1, .3333f } },
{ PIXEL_OFFSET( 0, -5 ), { 0, .6667f, 1, 1 } },
};
static void merge_kernel_fields( snes_ntsc_rgb_t* io )
{
int n;
for ( n = burst_size; n; --n )
{
snes_ntsc_rgb_t p0 = io [burst_size * 0] + rgb_bias;
snes_ntsc_rgb_t p1 = io [burst_size * 1] + rgb_bias;
snes_ntsc_rgb_t p2 = io [burst_size * 2] + rgb_bias;
/* merge colors without losing precision */
io [burst_size * 0] =
((p0 + p1 - ((p0 ^ p1) & snes_ntsc_rgb_builder)) >> 1) - rgb_bias;
io [burst_size * 1] =
((p1 + p2 - ((p1 ^ p2) & snes_ntsc_rgb_builder)) >> 1) - rgb_bias;
io [burst_size * 2] =
((p2 + p0 - ((p2 ^ p0) & snes_ntsc_rgb_builder)) >> 1) - rgb_bias;
++io;
}
}
static void correct_errors( snes_ntsc_rgb_t color, snes_ntsc_rgb_t* out )
{
int n;
for ( n = burst_count; n; --n )
{
unsigned i;
for ( i = 0; i < rgb_kernel_size / 2; i++ )
{
snes_ntsc_rgb_t error = color -
out [i ] - out [(i+12)%14+14] - out [(i+10)%14+28] -
out [i + 7] - out [i + 5 +14] - out [i + 3 +28];
DISTRIBUTE_ERROR( i+3+28, i+5+14, i+7 );
}
out += alignment_count * rgb_kernel_size;
}
}
void snes_ntsc_init( snes_ntsc_t* ntsc, snes_ntsc_setup_t const* setup )
{
int merge_fields;
int entry;
init_t impl;
if ( !setup )
setup = &snes_ntsc_composite;
init( &impl, setup );
merge_fields = setup->merge_fields;
if ( setup->artifacts <= -1 && setup->fringing <= -1 )
merge_fields = 1;
for ( entry = 0; entry < snes_ntsc_palette_size; entry++ )
{
/* Reduce number of significant bits of source color. Clearing the
low bits of R and B were least notictable. Modifying green was too
noticeable. */
int ir = entry >> 8 & 0x1E;
int ig = entry >> 4 & 0x1F;
int ib = entry << 1 & 0x1E;
#if SNES_NTSC_BSNES_COLORTBL
if ( setup->bsnes_colortbl )
{
int bgr15 = (ib << 10) | (ig << 5) | ir;
unsigned long rgb16 = setup->bsnes_colortbl [bgr15];
ir = rgb16 >> 11 & 0x1E;
ig = rgb16 >> 6 & 0x1F;
ib = rgb16 & 0x1E;
}
#endif
{
float rr = impl.to_float [ir];
float gg = impl.to_float [ig];
float bb = impl.to_float [ib];
float y, i, q = RGB_TO_YIQ( rr, gg, bb, y, i );
int r, g, b = YIQ_TO_RGB( y, i, q, impl.to_rgb, int, r, g );
snes_ntsc_rgb_t rgb = PACK_RGB( r, g, b );
snes_ntsc_rgb_t* out = ntsc->table [entry];
gen_kernel( &impl, y, i, q, out );
if ( merge_fields )
merge_kernel_fields( out );
correct_errors( rgb, out );
}
}
}
#ifndef SNES_NTSC_NO_BLITTERS
void snes_ntsc_blit( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input, long in_row_width,
int burst_phase, int in_width, int in_height, void* rgb_out, long out_pitch )
{
int chunk_count = (in_width - 1) / snes_ntsc_in_chunk;
for ( ; in_height; --in_height )
{
SNES_NTSC_IN_T const* line_in = input;
SNES_NTSC_BEGIN_ROW( ntsc, burst_phase,
snes_ntsc_black, snes_ntsc_black, SNES_NTSC_ADJ_IN( *line_in ) );
snes_ntsc_out_t* restrict line_out = (snes_ntsc_out_t*) rgb_out;
int n;
++line_in;
for ( n = chunk_count; n; --n )
{
/* order of input and output pixels must not be altered */
SNES_NTSC_COLOR_IN( 0, SNES_NTSC_ADJ_IN( line_in [0] ) );
SNES_NTSC_RGB_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, SNES_NTSC_ADJ_IN( line_in [1] ) );
SNES_NTSC_RGB_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, SNES_NTSC_ADJ_IN( line_in [2] ) );
SNES_NTSC_RGB_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
line_in += 3;
line_out += 7;
}
/* finish final pixels */
SNES_NTSC_COLOR_IN( 0, snes_ntsc_black );
SNES_NTSC_RGB_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, snes_ntsc_black );
SNES_NTSC_RGB_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, snes_ntsc_black );
SNES_NTSC_RGB_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
burst_phase = (burst_phase + 1) % snes_ntsc_burst_count;
input += in_row_width;
rgb_out = (char*) rgb_out + out_pitch;
}
}
void snes_ntsc_blit_hires( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input, long in_row_width,
int burst_phase, int in_width, int in_height, void* rgb_out, long out_pitch )
{
int chunk_count = (in_width - 2) / (snes_ntsc_in_chunk * 2);
for ( ; in_height; --in_height )
{
SNES_NTSC_IN_T const* line_in = input;
SNES_NTSC_HIRES_ROW( ntsc, burst_phase,
snes_ntsc_black, snes_ntsc_black, snes_ntsc_black,
SNES_NTSC_ADJ_IN( line_in [0] ),
SNES_NTSC_ADJ_IN( line_in [1] ) );
snes_ntsc_out_t* restrict line_out = (snes_ntsc_out_t*) rgb_out;
int n;
line_in += 2;
for ( n = chunk_count; n; --n )
{
/* twice as many input pixels per chunk */
SNES_NTSC_COLOR_IN( 0, SNES_NTSC_ADJ_IN( line_in [0] ) );
SNES_NTSC_HIRES_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, SNES_NTSC_ADJ_IN( line_in [1] ) );
SNES_NTSC_HIRES_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, SNES_NTSC_ADJ_IN( line_in [2] ) );
SNES_NTSC_HIRES_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 3, SNES_NTSC_ADJ_IN( line_in [3] ) );
SNES_NTSC_HIRES_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 4, SNES_NTSC_ADJ_IN( line_in [4] ) );
SNES_NTSC_HIRES_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 5, SNES_NTSC_ADJ_IN( line_in [5] ) );
SNES_NTSC_HIRES_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_HIRES_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
line_in += 6;
line_out += 7;
}
SNES_NTSC_COLOR_IN( 0, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 3, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 4, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 5, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_HIRES_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
burst_phase = (burst_phase + 1) % snes_ntsc_burst_count;
input += in_row_width;
rgb_out = (char*) rgb_out + out_pitch;
}
}
#endif
-236
View File
@@ -1,236 +0,0 @@
/* SNES NTSC video filter */
/* snes_ntsc 0.2.2 */
#ifndef SNES_NTSC_H
#define SNES_NTSC_H
#include "snes_ntsc_config.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Image parameters, ranging from -1.0 to 1.0. Actual internal values shown
in parenthesis and should remain fairly stable in future versions. */
typedef struct snes_ntsc_setup_t
{
/* Basic parameters */
double hue; /* -1 = -180 degrees +1 = +180 degrees */
double saturation; /* -1 = grayscale (0.0) +1 = oversaturated colors (2.0) */
double contrast; /* -1 = dark (0.5) +1 = light (1.5) */
double brightness; /* -1 = dark (0.5) +1 = light (1.5) */
double sharpness; /* edge contrast enhancement/blurring */
/* Advanced parameters */
double gamma; /* -1 = dark (1.5) +1 = light (0.5) */
double resolution; /* image resolution */
double artifacts; /* artifacts caused by color changes */
double fringing; /* color artifacts caused by brightness changes */
double bleed; /* color bleed (color resolution reduction) */
int merge_fields; /* if 1, merges even and odd fields together to reduce flicker */
float const* decoder_matrix; /* optional RGB decoder matrix, 6 elements */
unsigned long const* bsnes_colortbl; /* undocumented; set to 0 */
} snes_ntsc_setup_t;
/* Video format presets */
extern snes_ntsc_setup_t const snes_ntsc_composite; /* color bleeding + artifacts */
extern snes_ntsc_setup_t const snes_ntsc_svideo; /* color bleeding only */
extern snes_ntsc_setup_t const snes_ntsc_rgb; /* crisp image */
extern snes_ntsc_setup_t const snes_ntsc_monochrome;/* desaturated + artifacts */
/* Initializes and adjusts parameters. Can be called multiple times on the same
snes_ntsc_t object. Can pass NULL for either parameter. */
typedef struct snes_ntsc_t snes_ntsc_t;
void snes_ntsc_init( snes_ntsc_t* ntsc, snes_ntsc_setup_t const* setup );
/* Filters one or more rows of pixels. Input pixel format is set by SNES_NTSC_IN_FORMAT
and output RGB depth is set by SNES_NTSC_OUT_DEPTH. Both default to 16-bit RGB.
In_row_width is the number of pixels to get to the next input row. Out_pitch
is the number of *bytes* to get to the next output row. */
void snes_ntsc_blit( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input,
long in_row_width, int burst_phase, int in_width, int in_height,
void* rgb_out, long out_pitch );
void snes_ntsc_blit_hires( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input,
long in_row_width, int burst_phase, int in_width, int in_height,
void* rgb_out, long out_pitch );
/* Number of output pixels written by low-res blitter for given input width. Width
might be rounded down slightly; use SNES_NTSC_IN_WIDTH() on result to find rounded
value. Guaranteed not to round 256 down at all. */
#define SNES_NTSC_OUT_WIDTH( in_width ) \
((((in_width) - 1) / snes_ntsc_in_chunk + 1) * snes_ntsc_out_chunk)
/* Number of low-res input pixels that will fit within given output width. Might be
rounded down slightly; use SNES_NTSC_OUT_WIDTH() on result to find rounded
value. */
#define SNES_NTSC_IN_WIDTH( out_width ) \
(((out_width) / snes_ntsc_out_chunk - 1) * snes_ntsc_in_chunk + 1)
/* Interface for user-defined custom blitters */
enum { snes_ntsc_in_chunk = 3 }; /* number of input pixels read per chunk */
enum { snes_ntsc_out_chunk = 7 }; /* number of output pixels generated per chunk */
enum { snes_ntsc_black = 0 }; /* palette index for black */
enum { snes_ntsc_burst_count = 3 }; /* burst phase cycles through 0, 1, and 2 */
/* Begins outputting row and starts three pixels. First pixel will be cut off a bit.
Use snes_ntsc_black for unused pixels. Declares variables, so must be before first
statement in a block (unless you're using C++). */
#define SNES_NTSC_BEGIN_ROW( ntsc, burst, pixel0, pixel1, pixel2 ) \
char const* ktable = \
(char const*) (ntsc)->table + burst * (snes_ntsc_burst_size * sizeof (snes_ntsc_rgb_t));\
SNES_NTSC_BEGIN_ROW_6_( pixel0, pixel1, pixel2, SNES_NTSC_IN_FORMAT, ktable )
/* Begins input pixel */
#define SNES_NTSC_COLOR_IN( index, color ) \
SNES_NTSC_COLOR_IN_( index, color, SNES_NTSC_IN_FORMAT, ktable )
/* Generates output pixel. Bits can be 24, 16, 15, 14, 32 (treated as 24), or 0:
24: RRRRRRRR GGGGGGGG BBBBBBBB (8-8-8 RGB)
16: RRRRRGGG GGGBBBBB (5-6-5 RGB)
15: RRRRRGG GGGBBBBB (5-5-5 RGB)
14: BBBBBGG GGGRRRRR (5-5-5 BGR, native SNES format)
0: xxxRRRRR RRRxxGGG GGGGGxxB BBBBBBBx (native internal format; x = junk bits) */
#define SNES_NTSC_RGB_OUT( index, rgb_out, bits ) \
SNES_NTSC_RGB_OUT_14_( index, rgb_out, bits, 1 )
/* Hires equivalents */
#define SNES_NTSC_HIRES_ROW( ntsc, burst, pixel1, pixel2, pixel3, pixel4, pixel5 ) \
char const* ktable = \
(char const*) (ntsc)->table + burst * (snes_ntsc_burst_size * sizeof (snes_ntsc_rgb_t));\
unsigned const snes_ntsc_pixel1_ = (pixel1);\
snes_ntsc_rgb_t const* kernel1 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel1_ );\
unsigned const snes_ntsc_pixel2_ = (pixel2);\
snes_ntsc_rgb_t const* kernel2 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel2_ );\
unsigned const snes_ntsc_pixel3_ = (pixel3);\
snes_ntsc_rgb_t const* kernel3 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel3_ );\
unsigned const snes_ntsc_pixel4_ = (pixel4);\
snes_ntsc_rgb_t const* kernel4 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel4_ );\
unsigned const snes_ntsc_pixel5_ = (pixel5);\
snes_ntsc_rgb_t const* kernel5 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel5_ );\
snes_ntsc_rgb_t const* kernel0 = kernel1;\
snes_ntsc_rgb_t const* kernelx0;\
snes_ntsc_rgb_t const* kernelx1 = kernel1;\
snes_ntsc_rgb_t const* kernelx2 = kernel1;\
snes_ntsc_rgb_t const* kernelx3 = kernel1;\
snes_ntsc_rgb_t const* kernelx4 = kernel1;\
snes_ntsc_rgb_t const* kernelx5 = kernel1
#define SNES_NTSC_HIRES_OUT( x, rgb_out, bits ) {\
snes_ntsc_rgb_t raw_ =\
kernel0 [ x ] + kernel2 [(x+5)%7+14] + kernel4 [(x+3)%7+28] +\
kernelx0 [(x+7)%7+7] + kernelx2 [(x+5)%7+21] + kernelx4 [(x+3)%7+35] +\
kernel1 [(x+6)%7 ] + kernel3 [(x+4)%7+14] + kernel5 [(x+2)%7+28] +\
kernelx1 [(x+6)%7+7] + kernelx3 [(x+4)%7+21] + kernelx5 [(x+2)%7+35];\
SNES_NTSC_CLAMP_( raw_, 0 );\
SNES_NTSC_RGB_OUT_( rgb_out, (bits), 0 );\
}
/* private */
enum { snes_ntsc_entry_size = 128 };
enum { snes_ntsc_palette_size = 0x2000 };
typedef unsigned long snes_ntsc_rgb_t;
struct snes_ntsc_t {
snes_ntsc_rgb_t table [snes_ntsc_palette_size] [snes_ntsc_entry_size];
};
enum { snes_ntsc_burst_size = snes_ntsc_entry_size / snes_ntsc_burst_count };
#define SNES_NTSC_RGB16( ktable, n ) \
(snes_ntsc_rgb_t const*) (ktable + ((n & 0x001E) | (n >> 1 & 0x03E0) | (n >> 2 & 0x3C00)) * \
(snes_ntsc_entry_size / 2 * sizeof (snes_ntsc_rgb_t)))
#define SNES_NTSC_BGR15( ktable, n ) \
(snes_ntsc_rgb_t const*) (ktable + ((n << 9 & 0x3C00) | (n & 0x03E0) | (n >> 10 & 0x001E)) * \
(snes_ntsc_entry_size / 2 * sizeof (snes_ntsc_rgb_t)))
/* common 3->7 ntsc macros */
#define SNES_NTSC_BEGIN_ROW_6_( pixel0, pixel1, pixel2, ENTRY, table ) \
unsigned const snes_ntsc_pixel0_ = (pixel0);\
snes_ntsc_rgb_t const* kernel0 = ENTRY( table, snes_ntsc_pixel0_ );\
unsigned const snes_ntsc_pixel1_ = (pixel1);\
snes_ntsc_rgb_t const* kernel1 = ENTRY( table, snes_ntsc_pixel1_ );\
unsigned const snes_ntsc_pixel2_ = (pixel2);\
snes_ntsc_rgb_t const* kernel2 = ENTRY( table, snes_ntsc_pixel2_ );\
snes_ntsc_rgb_t const* kernelx0;\
snes_ntsc_rgb_t const* kernelx1 = kernel0;\
snes_ntsc_rgb_t const* kernelx2 = kernel0
#define SNES_NTSC_RGB_OUT_14_( x, rgb_out, bits, shift ) {\
snes_ntsc_rgb_t raw_ =\
kernel0 [x ] + kernel1 [(x+12)%7+14] + kernel2 [(x+10)%7+28] +\
kernelx0 [(x+7)%14] + kernelx1 [(x+ 5)%7+21] + kernelx2 [(x+ 3)%7+35];\
SNES_NTSC_CLAMP_( raw_, shift );\
SNES_NTSC_RGB_OUT_( rgb_out, bits, shift );\
}
/* common ntsc macros */
#define snes_ntsc_rgb_builder ((1L << 21) | (1 << 11) | (1 << 1))
#define snes_ntsc_clamp_mask (snes_ntsc_rgb_builder * 3 / 2)
#define snes_ntsc_clamp_add (snes_ntsc_rgb_builder * 0x101)
#define SNES_NTSC_CLAMP_( io, shift ) {\
snes_ntsc_rgb_t sub = (io) >> (9-(shift)) & snes_ntsc_clamp_mask;\
snes_ntsc_rgb_t clamp = snes_ntsc_clamp_add - sub;\
io |= clamp;\
clamp -= sub;\
io &= clamp;\
}
#define SNES_NTSC_COLOR_IN_( index, color, ENTRY, table ) {\
unsigned color_;\
kernelx##index = kernel##index;\
kernel##index = (color_ = (color), ENTRY( table, color_ ));\
}
/* x is always zero except in snes_ntsc library */
/* original routine */
#if 0
#define SNES_NTSC_RGB_OUT_( rgb_out, bits, x ) {\
if ( bits == 16 )\
rgb_out = (raw_>>(13-x)& 0xF800)|(raw_>>(8-x)&0x07E0)|(raw_>>(4-x)&0x001F);\
if ( bits == 24 || bits == 32 )\
rgb_out = (raw_>>(5-x)&0xFF0000)|(raw_>>(3-x)&0xFF00)|(raw_>>(1-x)&0xFF);\
if ( bits == 15 )\
rgb_out = (raw_>>(14-x)& 0x7C00)|(raw_>>(9-x)&0x03E0)|(raw_>>(4-x)&0x001F);\
if ( bits == 14 )\
rgb_out = (raw_>>(24-x)& 0x001F)|(raw_>>(9-x)&0x03E0)|(raw_<<(6+x)&0x7C00);\
if ( bits == 0 )\
rgb_out = raw_ << x;\
}
#endif
#define SNES_NTSC_RGB_OUT_( rgb_out, bits, x ) {\
rgb_out = (raw_>>(14-x)& 0x7C00)|(raw_>>(9-x)&0x03E0)|(raw_>>(4-x)&0x001F);\
}
// Custom SSNES routine for 15 bit.
/* custom bsnes routine -- hooks into bsnes colortable */
#if 0
#define SNES_NTSC_RGB_OUT_( rgb_out, bits, x ) {\
if ( bits == 16 ) {\
rgb_out = (raw_>>(13-x)& 0xF800)|(raw_>>(8-x)&0x07E0)|(raw_>>(4-x)&0x001F);\
rgb_out = ((rgb_out&0xf800)>>11)|((rgb_out&0x07c0)>>1)|((rgb_out&0x001f)<<10);\
rgb_out = colortable[rgb_out];\
} else if ( bits == 24 || bits == 32 ) {\
rgb_out = (raw_>>(5-x)&0xFF0000)|(raw_>>(3-x)&0xFF00)|(raw_>>(1-x)&0xFF);\
rgb_out = ((rgb_out&0xf80000)>>19)|((rgb_out&0x00f800)>>6)|((rgb_out&0x0000f8)<<7);\
rgb_out = colortable[rgb_out];\
} else if ( bits == 15 ) {\
rgb_out = (raw_>>(14-x)& 0x7C00)|(raw_>>(9-x)&0x03E0)|(raw_>>(4-x)&0x001F);\
rgb_out = ((rgb_out&0x7c00)>>10)|((rgb_out&0x03e0))|((rgb_out&0x001f)<<10);\
rgb_out = colortable[rgb_out];\
} else {\
rgb_out = raw_ << x;\
}\
}
#endif
#ifdef __cplusplus
}
#endif
#endif
-27
View File
@@ -1,27 +0,0 @@
/* Configure library by modifying this file */
#ifndef SNES_NTSC_CONFIG_H
#define SNES_NTSC_CONFIG_H
/* Format of source pixels */
/* #define SNES_NTSC_IN_FORMAT SNES_NTSC_RGB16 */
#define SNES_NTSC_IN_FORMAT SNES_NTSC_BGR15
/* The following affect the built-in blitter only; a custom blitter can
handle things however it wants. */
/* Bits per pixel of output. Can be 15, 16, 32, or 24 (same as 32). */
#define SNES_NTSC_OUT_DEPTH 15
/* Type of input pixel values */
#define SNES_NTSC_IN_T unsigned short
/* Each raw pixel input value is passed through this. You might want to mask
the pixel index if you use the high bits as flags, etc. */
#define SNES_NTSC_ADJ_IN( in ) (((in >> 10) & 0x1F) | (in & 0x3E0) | ((in << 10) & 0x7C00))
/* For each pixel, this is the basic operation:
output_color = SNES_NTSC_ADJ_IN( SNES_NTSC_IN_T ) */
#endif
-439
View File
@@ -1,439 +0,0 @@
/* snes_ntsc 0.2.2. http://www.slack.net/~ant/ */
/* Common implementation of NTSC filters */
#include <assert.h>
#include <math.h>
/* Copyright (C) 2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#define DISABLE_CORRECTION 0
#undef PI
#define PI 3.14159265358979323846f
#ifndef LUMA_CUTOFF
#define LUMA_CUTOFF 0.20
#endif
#ifndef gamma_size
#define gamma_size 1
#endif
#ifndef rgb_bits
#define rgb_bits 8
#endif
#ifndef artifacts_max
#define artifacts_max (artifacts_mid * 1.5f)
#endif
#ifndef fringing_max
#define fringing_max (fringing_mid * 2)
#endif
#ifndef STD_HUE_CONDITION
#define STD_HUE_CONDITION( setup ) 1
#endif
#define ext_decoder_hue (std_decoder_hue + 15)
#define rgb_unit (1 << rgb_bits)
#define rgb_offset (rgb_unit * 2 + 0.5f)
enum { burst_size = snes_ntsc_entry_size / burst_count };
enum { kernel_half = 16 };
enum { kernel_size = kernel_half * 2 + 1 };
typedef struct init_t
{
float to_rgb [burst_count * 6];
float to_float [gamma_size];
float contrast;
float brightness;
float artifacts;
float fringing;
float kernel [rescale_out * kernel_size * 2];
} init_t;
#define ROTATE_IQ( i, q, sin_b, cos_b ) {\
float t;\
t = i * cos_b - q * sin_b;\
q = i * sin_b + q * cos_b;\
i = t;\
}
static void init_filters( init_t* impl, snes_ntsc_setup_t const* setup )
{
#if rescale_out > 1
float kernels [kernel_size * 2];
#else
float* const kernels = impl->kernel;
#endif
/* generate luma (y) filter using sinc kernel */
{
/* sinc with rolloff (dsf) */
float const rolloff = 1 + (float) setup->sharpness * (float) 0.032;
float const maxh = 32;
float const pow_a_n = (float) pow( rolloff, maxh );
float sum;
int i;
/* quadratic mapping to reduce negative (blurring) range */
float to_angle = (float) setup->resolution + 1;
to_angle = PI / maxh * (float) LUMA_CUTOFF * (to_angle * to_angle + 1);
kernels [kernel_size * 3 / 2] = maxh; /* default center value */
for ( i = 0; i < kernel_half * 2 + 1; i++ )
{
int x = i - kernel_half;
float angle = x * to_angle;
/* instability occurs at center point with rolloff very close to 1.0 */
if ( x || pow_a_n > (float) 1.056 || pow_a_n < (float) 0.981 )
{
float rolloff_cos_a = rolloff * (float) cos( angle );
float num = 1 - rolloff_cos_a -
pow_a_n * (float) cos( maxh * angle ) +
pow_a_n * rolloff * (float) cos( (maxh - 1) * angle );
float den = 1 - rolloff_cos_a - rolloff_cos_a + rolloff * rolloff;
float dsf = num / den;
kernels [kernel_size * 3 / 2 - kernel_half + i] = dsf - (float) 0.5;
}
}
/* apply blackman window and find sum */
sum = 0;
for ( i = 0; i < kernel_half * 2 + 1; i++ )
{
float x = PI * 2 / (kernel_half * 2) * i;
float blackman = 0.42f - 0.5f * (float) cos( x ) + 0.08f * (float) cos( x * 2 );
sum += (kernels [kernel_size * 3 / 2 - kernel_half + i] *= blackman);
}
/* normalize kernel */
sum = 1.0f / sum;
for ( i = 0; i < kernel_half * 2 + 1; i++ )
{
int x = kernel_size * 3 / 2 - kernel_half + i;
kernels [x] *= sum;
assert( kernels [x] == kernels [x] ); /* catch numerical instability */
}
}
/* generate chroma (iq) filter using gaussian kernel */
{
float const cutoff_factor = -0.03125f;
float cutoff = (float) setup->bleed;
int i;
if ( cutoff < 0 )
{
/* keep extreme value accessible only near upper end of scale (1.0) */
cutoff *= cutoff;
cutoff *= cutoff;
cutoff *= cutoff;
cutoff *= -30.0f / 0.65f;
}
cutoff = cutoff_factor - 0.65f * cutoff_factor * cutoff;
for ( i = -kernel_half; i <= kernel_half; i++ )
kernels [kernel_size / 2 + i] = (float) exp( i * i * cutoff );
/* normalize even and odd phases separately */
for ( i = 0; i < 2; i++ )
{
float sum = 0;
int x;
for ( x = i; x < kernel_size; x += 2 )
sum += kernels [x];
sum = 1.0f / sum;
for ( x = i; x < kernel_size; x += 2 )
{
kernels [x] *= sum;
assert( kernels [x] == kernels [x] ); /* catch numerical instability */
}
}
}
/*
printf( "luma:\n" );
for ( i = kernel_size; i < kernel_size * 2; i++ )
printf( "%f\n", kernels [i] );
printf( "chroma:\n" );
for ( i = 0; i < kernel_size; i++ )
printf( "%f\n", kernels [i] );
*/
/* generate linear rescale kernels */
#if rescale_out > 1
{
float weight = 1.0f;
float* out = impl->kernel;
int n = rescale_out;
do
{
float remain = 0;
int i;
weight -= 1.0f / rescale_in;
for ( i = 0; i < kernel_size * 2; i++ )
{
float cur = kernels [i];
float m = cur * weight;
*out++ = m + remain;
remain = cur - m;
}
}
while ( --n );
}
#endif
}
static float const default_decoder [6] =
{ 0.956f, 0.621f, -0.272f, -0.647f, -1.105f, 1.702f };
static void init( init_t* impl, snes_ntsc_setup_t const* setup )
{
impl->brightness = (float) setup->brightness * (0.5f * rgb_unit) + rgb_offset;
impl->contrast = (float) setup->contrast * (0.5f * rgb_unit) + rgb_unit;
#ifdef default_palette_contrast
if ( !setup->palette )
impl->contrast *= default_palette_contrast;
#endif
impl->artifacts = (float) setup->artifacts;
if ( impl->artifacts > 0 )
impl->artifacts *= artifacts_max - artifacts_mid;
impl->artifacts = impl->artifacts * artifacts_mid + artifacts_mid;
impl->fringing = (float) setup->fringing;
if ( impl->fringing > 0 )
impl->fringing *= fringing_max - fringing_mid;
impl->fringing = impl->fringing * fringing_mid + fringing_mid;
init_filters( impl, setup );
/* generate gamma table */
if ( gamma_size > 1 )
{
float const to_float = 1.0f / (gamma_size - (gamma_size > 1));
float const gamma = 1.1333f - (float) setup->gamma * 0.5f;
/* match common PC's 2.2 gamma to TV's 2.65 gamma */
int i;
for ( i = 0; i < gamma_size; i++ )
impl->to_float [i] =
(float) pow( i * to_float, gamma ) * impl->contrast + impl->brightness;
}
/* setup decoder matricies */
{
float hue = (float) setup->hue * PI + PI / 180 * ext_decoder_hue;
float sat = (float) setup->saturation + 1;
float const* decoder = setup->decoder_matrix;
if ( !decoder )
{
decoder = default_decoder;
if ( STD_HUE_CONDITION( setup ) )
hue += PI / 180 * (std_decoder_hue - ext_decoder_hue);
}
{
float s = (float) sin( hue ) * sat;
float c = (float) cos( hue ) * sat;
float* out = impl->to_rgb;
int n;
n = burst_count;
do
{
float const* in = decoder;
int n = 3;
do
{
float i = *in++;
float q = *in++;
*out++ = i * c - q * s;
*out++ = i * s + q * c;
}
while ( --n );
if ( burst_count <= 1 )
break;
ROTATE_IQ( s, c, 0.866025f, -0.5f ); /* +120 degrees */
}
while ( --n );
}
}
}
/* kernel generation */
#define RGB_TO_YIQ( r, g, b, y, i ) (\
(y = (r) * 0.299f + (g) * 0.587f + (b) * 0.114f),\
(i = (r) * 0.596f - (g) * 0.275f - (b) * 0.321f),\
((r) * 0.212f - (g) * 0.523f + (b) * 0.311f)\
)
#define YIQ_TO_RGB( y, i, q, to_rgb, type, r, g ) (\
r = (type) (y + to_rgb [0] * i + to_rgb [1] * q),\
g = (type) (y + to_rgb [2] * i + to_rgb [3] * q),\
(type) (y + to_rgb [4] * i + to_rgb [5] * q)\
)
#define PACK_RGB( r, g, b ) ((r) << 21 | (g) << 11 | (b) << 1)
enum { rgb_kernel_size = burst_size / alignment_count };
enum { rgb_bias = rgb_unit * 2 * snes_ntsc_rgb_builder };
typedef struct pixel_info_t
{
int offset;
float negate;
float kernel [4];
} pixel_info_t;
#if rescale_in > 1
#define PIXEL_OFFSET_( ntsc, scaled ) \
(kernel_size / 2 + ntsc + (scaled != 0) + (rescale_out - scaled) % rescale_out + \
(kernel_size * 2 * scaled))
#define PIXEL_OFFSET( ntsc, scaled ) \
PIXEL_OFFSET_( ((ntsc) - (scaled) / rescale_out * rescale_in),\
(((scaled) + rescale_out * 10) % rescale_out) ),\
(1.0f - (((ntsc) + 100) & 2))
#else
#define PIXEL_OFFSET( ntsc, scaled ) \
(kernel_size / 2 + (ntsc) - (scaled)),\
(1.0f - (((ntsc) + 100) & 2))
#endif
extern pixel_info_t const snes_ntsc_pixels [alignment_count];
/* Generate pixel at all burst phases and column alignments */
static void gen_kernel( init_t* impl, float y, float i, float q, snes_ntsc_rgb_t* out )
{
/* generate for each scanline burst phase */
float const* to_rgb = impl->to_rgb;
int burst_remain = burst_count;
y -= rgb_offset;
do
{
/* Encode yiq into *two* composite signals (to allow control over artifacting).
Convolve these with kernels which: filter respective components, apply
sharpening, and rescale horizontally. Convert resulting yiq to rgb and pack
into integer. Based on algorithm by NewRisingSun. */
pixel_info_t const* pixel = snes_ntsc_pixels;
int alignment_remain = alignment_count;
do
{
/* negate is -1 when composite starts at odd multiple of 2 */
float const yy = y * impl->fringing * pixel->negate;
float const ic0 = (i + yy) * pixel->kernel [0];
float const qc1 = (q + yy) * pixel->kernel [1];
float const ic2 = (i - yy) * pixel->kernel [2];
float const qc3 = (q - yy) * pixel->kernel [3];
float const factor = impl->artifacts * pixel->negate;
float const ii = i * factor;
float const yc0 = (y + ii) * pixel->kernel [0];
float const yc2 = (y - ii) * pixel->kernel [2];
float const qq = q * factor;
float const yc1 = (y + qq) * pixel->kernel [1];
float const yc3 = (y - qq) * pixel->kernel [3];
float const* k = &impl->kernel [pixel->offset];
int n;
++pixel;
for ( n = rgb_kernel_size; n; --n )
{
float i = k[0]*ic0 + k[2]*ic2;
float q = k[1]*qc1 + k[3]*qc3;
float y = k[kernel_size+0]*yc0 + k[kernel_size+1]*yc1 +
k[kernel_size+2]*yc2 + k[kernel_size+3]*yc3 + rgb_offset;
if ( rescale_out <= 1 )
k--;
else if ( k < &impl->kernel [kernel_size * 2 * (rescale_out - 1)] )
k += kernel_size * 2 - 1;
else
k -= kernel_size * 2 * (rescale_out - 1) + 2;
{
int r, g, b = YIQ_TO_RGB( y, i, q, to_rgb, int, r, g );
*out++ = PACK_RGB( r, g, b ) - rgb_bias;
}
}
}
while ( alignment_count > 1 && --alignment_remain );
if ( burst_count <= 1 )
break;
to_rgb += 6;
ROTATE_IQ( i, q, -0.866025f, -0.5f ); /* -120 degrees */
}
while ( --burst_remain );
}
static void correct_errors( snes_ntsc_rgb_t color, snes_ntsc_rgb_t* out );
#if DISABLE_CORRECTION
#define CORRECT_ERROR( a ) { out [i] += rgb_bias; }
#define DISTRIBUTE_ERROR( a, b, c ) { out [i] += rgb_bias; }
#else
#define CORRECT_ERROR( a ) { out [a] += error; }
#define DISTRIBUTE_ERROR( a, b, c ) {\
snes_ntsc_rgb_t fourth = (error + 2 * snes_ntsc_rgb_builder) >> 2;\
fourth &= (rgb_bias >> 1) - snes_ntsc_rgb_builder;\
fourth -= rgb_bias >> 2;\
out [a] += fourth;\
out [b] += fourth;\
out [c] += fourth;\
out [i] += error - (fourth * 3);\
}
#endif
#define RGB_PALETTE_OUT( rgb, out_ )\
{\
unsigned char* out = (out_);\
snes_ntsc_rgb_t clamped = (rgb);\
SNES_NTSC_CLAMP_( clamped, (8 - rgb_bits) );\
out [0] = (unsigned char) (clamped >> 21);\
out [1] = (unsigned char) (clamped >> 11);\
out [2] = (unsigned char) (clamped >> 1);\
}
/* blitter related */
#ifndef restrict
#if defined (__GNUC__)
#define restrict __restrict__
#elif defined (_MSC_VER) && _MSC_VER > 1300
#define restrict __restrict
#else
/* no support for restricted pointers */
#define restrict
#endif
#endif
#include <limits.h>
#if SNES_NTSC_OUT_DEPTH <= 16
#if USHRT_MAX == 0xFFFF
typedef unsigned short snes_ntsc_out_t;
#else
#error "Need 16-bit int type"
#endif
#else
#if UINT_MAX == 0xFFFFFFFF
typedef unsigned int snes_ntsc_out_t;
#elif ULONG_MAX == 0xFFFFFFFF
typedef unsigned long snes_ntsc_out_t;
#else
#error "Need 32-bit int type"
#endif
#endif
+4 -2
View File
@@ -61,6 +61,8 @@ static void* sdl_input_init(void)
}
}
sdl->use_keyboard = true;
return sdl;
}
@@ -141,7 +143,7 @@ static bool sdl_axis_pressed(sdl_input_t *sdl, int port_num, uint32_t joyaxis)
static bool sdl_is_pressed(sdl_input_t *sdl, int port_num, const struct snes_keybind *key)
{
if (sdl_key_pressed(key->key))
if (sdl->use_keyboard && sdl_key_pressed(key->key))
return true;
if (sdl->joysticks[port_num] == NULL)
return false;
@@ -246,7 +248,7 @@ static int16_t sdl_input_state(void *data, const struct snes_keybind **binds, bo
switch (device)
{
case SNES_DEVICE_JOYPAD:
return sdl_joypad_device_state(data, binds, port == SNES_PORT_1 ? 0 : 1, device, index, id);
return sdl_joypad_device_state(data, binds, (port == SNES_PORT_1) ? 0 : 1, device, index, id);
case SNES_DEVICE_MULTITAP:
return sdl_joypad_device_state(data, binds, (port == SNES_PORT_2) ? 1 + index : 0, device, index, id);
case SNES_DEVICE_MOUSE:
+1
View File
@@ -27,6 +27,7 @@ typedef struct sdl_input
unsigned num_buttons[MAX_PLAYERS];
unsigned num_hats[MAX_PLAYERS];
unsigned num_joysticks;
bool use_keyboard;
// A video driver could pre-init with the SDL driver and have it handle resizing events...
bool *quitting;
+237
View File
@@ -0,0 +1,237 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#include "SDL.h"
#include <stdbool.h>
#include "general.h"
#include <stdint.h>
#include <stdlib.h>
#include "ssnes_sdl_input.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
typedef struct x11_input
{
sdl_input_t *sdl;
Display *display;
char state[32];
} x11_input_t;
struct key_bind
{
int x;
int sdl;
};
static int keysym_lut[SDLK_LAST];
static const struct key_bind lut_binds[] = {
{ XK_Left, SDLK_LEFT },
{ XK_Right, SDLK_RIGHT },
{ XK_Up, SDLK_UP },
{ XK_Down, SDLK_DOWN },
{ XK_Return, SDLK_RETURN },
{ XK_Tab, SDLK_TAB },
{ XK_Insert, SDLK_INSERT },
{ XK_Delete, SDLK_DELETE },
{ XK_Shift_R, SDLK_RSHIFT },
{ XK_Shift_L, SDLK_LSHIFT },
{ XK_Control_L, SDLK_LCTRL },
{ XK_Alt_L, SDLK_LALT },
{ XK_space, SDLK_SPACE },
{ XK_Escape, SDLK_ESCAPE },
{ XK_KP_Add, SDLK_KP_PLUS },
{ XK_KP_Subtract, SDLK_KP_MINUS },
{ XK_BackSpace, SDLK_BACKSPACE },
{ XK_KP_Enter, SDLK_KP_ENTER },
{ XK_KP_Add, SDLK_KP_PLUS },
{ XK_KP_Subtract, SDLK_KP_MINUS },
{ XK_KP_Multiply, SDLK_KP_MULTIPLY },
{ XK_KP_Divide, SDLK_KP_DIVIDE },
{ XK_grave, SDLK_BACKQUOTE },
{ XK_KP_0, SDLK_KP0 },
{ XK_KP_1, SDLK_KP1 },
{ XK_KP_2, SDLK_KP2 },
{ XK_KP_3, SDLK_KP3 },
{ XK_KP_4, SDLK_KP4 },
{ XK_KP_5, SDLK_KP5 },
{ XK_KP_6, SDLK_KP6 },
{ XK_KP_7, SDLK_KP7 },
{ XK_KP_8, SDLK_KP8 },
{ XK_KP_9, SDLK_KP9 },
{ XK_0, SDLK_0 },
{ XK_1, SDLK_1 },
{ XK_2, SDLK_2 },
{ XK_3, SDLK_3 },
{ XK_4, SDLK_4 },
{ XK_5, SDLK_5 },
{ XK_6, SDLK_6 },
{ XK_7, SDLK_7 },
{ XK_8, SDLK_8 },
{ XK_9, SDLK_9 },
{ XK_F1, SDLK_F1 },
{ XK_F2, SDLK_F2 },
{ XK_F3, SDLK_F3 },
{ XK_F4, SDLK_F4 },
{ XK_F5, SDLK_F5 },
{ XK_F6, SDLK_F6 },
{ XK_F7, SDLK_F7 },
{ XK_F8, SDLK_F8 },
{ XK_F9, SDLK_F9 },
{ XK_F10, SDLK_F10 },
{ XK_F11, SDLK_F11 },
{ XK_F12, SDLK_F12 },
{ XK_a, SDLK_a },
{ XK_b, SDLK_b },
{ XK_c, SDLK_c },
{ XK_d, SDLK_d },
{ XK_e, SDLK_e },
{ XK_f, SDLK_f },
{ XK_g, SDLK_g },
{ XK_h, SDLK_h },
{ XK_i, SDLK_i },
{ XK_j, SDLK_j },
{ XK_k, SDLK_k },
{ XK_l, SDLK_l },
{ XK_m, SDLK_m },
{ XK_n, SDLK_n },
{ XK_o, SDLK_o },
{ XK_p, SDLK_p },
{ XK_q, SDLK_q },
{ XK_r, SDLK_r },
{ XK_s, SDLK_s },
{ XK_t, SDLK_t },
{ XK_u, SDLK_u },
{ XK_v, SDLK_v },
{ XK_w, SDLK_w },
{ XK_x, SDLK_x },
{ XK_y, SDLK_y },
{ XK_z, SDLK_z },
};
static void init_lut(void)
{
memset(keysym_lut, 0, sizeof(keysym_lut));
for (unsigned i = 0; i < sizeof(lut_binds) / sizeof(lut_binds[0]); i++)
keysym_lut[lut_binds[i].sdl] = lut_binds[i].x;
}
static void* x_input_init(void)
{
x11_input_t *x11 = calloc(1, sizeof(*x11));
if (!x11)
return NULL;
x11->display = XOpenDisplay(NULL);
if (!x11->display)
{
free(x11);
return NULL;
}
x11->sdl = input_sdl.init();
if (!x11->sdl)
{
free(x11);
return NULL;
}
init_lut();
x11->sdl->use_keyboard = false;
return x11;
}
static bool x_key_pressed(x11_input_t *x11, int key)
{
key = keysym_lut[key];
int keycode = XKeysymToKeycode(x11->display, key);
bool ret = x11->state[keycode >> 3] & (1 << (keycode & 7));
return ret;
}
static bool x_is_pressed(x11_input_t *x11, const struct snes_keybind *binds, unsigned id)
{
for (int i = 0; binds[i].id != -1; i++)
{
if (binds[i].id == id)
return x_key_pressed(x11, binds[i].key);
}
return false;
}
static bool x_bind_button_pressed(void *data, int key)
{
x11_input_t *x11 = data;
bool pressed = x_is_pressed(x11, g_settings.input.binds[0], key);
if (!pressed)
return input_sdl.key_pressed(x11->sdl, key);
return pressed;
}
static int16_t x_input_state(void *data, const struct snes_keybind **binds, bool port, unsigned device, unsigned index, unsigned id)
{
x11_input_t *x11 = data;
bool pressed = false;
switch (device)
{
case SNES_DEVICE_JOYPAD:
pressed = x_is_pressed(x11, binds[(port == SNES_PORT_1) ? 0 : 1], id);
if (!pressed)
pressed = input_sdl.input_state(x11->sdl, binds, port, device, index, id);
return pressed;
case SNES_DEVICE_MULTITAP:
pressed = x_is_pressed(x11, binds[(port == SNES_PORT_2) ? 1 + index : 0], id);
if (!pressed)
pressed = input_sdl.input_state(x11->sdl, binds, port, device, index, id);
return pressed;
default:
return 0;
}
}
static void x_input_free(void *data)
{
x11_input_t *x11 = data;
input_sdl.free(x11->sdl);
XCloseDisplay(x11->display);
free(data);
}
static void x_input_poll(void *data)
{
x11_input_t *x11 = data;
XQueryKeymap(x11->display, x11->state);
input_sdl.poll(x11->sdl);
}
const input_driver_t input_x = {
.init = x_input_init,
.poll = x_input_poll,
.input_state = x_input_state,
.key_pressed = x_bind_button_pressed,
.free = x_input_free,
.ident = "x"
};
+10 -1099
View File
File diff suppressed because it is too large. Load diff
+68 -2
View File
@@ -20,6 +20,7 @@
#include <stdlib.h>
#include "general.h"
#include "dynamic.h"
#include <assert.h>
// CRC32 implementation taken from BSNES source :)
@@ -69,8 +70,7 @@ static const uint32_t crc32_table[256] = {
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
static inline uint32_t crc32_adjust(uint32_t crc32, uint8_t input)
uint32_t crc32_adjust(uint32_t crc32, uint8_t input)
{
return ((crc32 >> 8) & 0x00ffffff) ^ crc32_table[(crc32 ^ input) & 0xff];
}
@@ -87,6 +87,17 @@ struct bsv_movie
{
FILE *file;
uint8_t *state;
long *frame_pos; // A ring buffer keeping track of many key presses were requested per frame.
size_t frame_mask;
uint16_t current_frame_count;
size_t frame_ptr;
bool playback;
unsigned min_file_pos;
bool first_rewind;
bool did_rewind;
};
#define BSV_MAGIC 0x42535631
@@ -127,6 +138,7 @@ static inline uint16_t swap_if_big16(uint16_t val)
static bool init_playback(bsv_movie_t *handle, const char *path)
{
handle->playback = true;
handle->file = fopen(path, "rb");
if (!handle->file)
{
@@ -158,6 +170,8 @@ static bool init_playback(bsv_movie_t *handle, const char *path)
if (!handle->state)
return false;
handle->min_file_pos = sizeof(header) + state_size;
if (fread(handle->state, 1, state_size, handle->file) != state_size)
{
SSNES_ERR("Couldn't read state from movie.\n");
@@ -196,6 +210,8 @@ static bool init_record(bsv_movie_t *handle, const char *path)
if (!handle->state)
return false;
handle->min_file_pos = sizeof(header) + state_size;
psnes_serialize(handle->state, state_size);
fwrite(handle->state, 1, state_size, handle->file);
return true;
@@ -208,6 +224,7 @@ void bsv_movie_free(bsv_movie_t *handle)
if (handle->file)
fclose(handle->file);
free(handle->state);
free(handle->frame_pos);
free(handle);
}
}
@@ -240,9 +257,58 @@ bsv_movie_t *bsv_movie_init(const char *path, enum ssnes_movie_type type)
else if (!init_record(handle, path))
goto error;
if (!(handle->frame_pos = calloc((1 << 20), sizeof(long)))) // Just pick something really large :D
goto error;
handle->frame_mask = (1 << 20) - 1;
return handle;
error:
bsv_movie_free(handle);
return NULL;
}
void bsv_movie_set_frame_start(bsv_movie_t *handle)
{
//fprintf(stderr, "Starting frame: %u, Pos: %ld\n", (unsigned)handle->frame_ptr, ftell(handle->file));
handle->frame_pos[handle->frame_ptr] = ftell(handle->file);
}
void bsv_movie_set_frame_end(bsv_movie_t *handle)
{
//fprintf(stderr, "Frame++\n");
handle->frame_ptr = (handle->frame_ptr + 1) & handle->frame_mask;
handle->first_rewind = !handle->did_rewind;
handle->did_rewind = false;
}
void bsv_movie_frame_rewind(bsv_movie_t *handle)
{
//fprintf(stderr, "Frame--\n");
handle->did_rewind = true;
if (handle->frame_ptr <= 1)
{
handle->frame_ptr = 0;
fseek(handle->file, handle->min_file_pos, SEEK_SET);
}
else
{
handle->frame_ptr = (handle->frame_ptr - (handle->first_rewind ? 1 : 2)) & handle->frame_mask;
fseek(handle->file, handle->frame_pos[handle->frame_ptr], SEEK_SET);
}
if (ftell(handle->file) <= (long)handle->min_file_pos)
{
if (!handle->playback)
{
fseek(handle->file, 4 * sizeof(uint32_t), SEEK_SET);
psnes_serialize(handle->state, psnes_serialize_size());
fwrite(handle->state, 1, psnes_serialize_size(), handle->file);
}
else
fseek(handle->file, handle->min_file_pos, SEEK_SET);
}
}
+6 -3
View File
@@ -37,12 +37,15 @@ bool bsv_movie_get_input(bsv_movie_t *handle, int16_t *input);
// Recording
void bsv_movie_set_input(bsv_movie_t *handle, int16_t input);
// Used for rewinding while playback/record.
void bsv_movie_set_frame_start(bsv_movie_t *handle); // Debugging purposes.
void bsv_movie_set_frame_end(bsv_movie_t *handle);
void bsv_movie_frame_rewind(bsv_movie_t *handle);
void bsv_movie_free(bsv_movie_t *handle);
uint32_t crc32_calculate(const uint8_t *data, unsigned length);
uint32_t crc32_adjust(uint32_t crc32, uint8_t input);
#endif
+702
View File
@@ -0,0 +1,702 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "netplay.h"
#include "general.h"
#include "autosave.h"
#include "dynamic.h"
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <assert.h>
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
// Woohoo, Winsock has headers from the STONE AGE! :D
#define close(x) closesocket(x)
#define CONST_CAST (const char*)
#define NONCONST_CAST (char*)
#else
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#define CONST_CAST
#define NONCONST_CAST
#endif
#define PREV_PTR(x) ((x) == 0 ? handle->buffer_size - 1 : (x) - 1)
#define NEXT_PTR(x) ((x + 1) % handle->buffer_size)
struct delta_frame
{
uint8_t *state;
uint16_t real_input_state;
uint16_t simulated_input_state;
bool is_simulated;
uint16_t self_state;
bool used_real;
};
#define UDP_FRAME_PACKETS 8
struct netplay
{
struct snes_callbacks cbs;
int fd; // TCP connection for state sending, etc. Could perhaps be used for messaging later on. :)
int udp_fd; // UDP connection for game state updates.
unsigned port; // Which port is governed by netplay?
bool has_connection;
struct delta_frame *buffer;
size_t buffer_size;
size_t self_ptr; // Ptr where we are now.
size_t other_ptr; // Points to the last reliable state that self ever had.
size_t read_ptr; // Ptr to where we are reading. Generally, other_ptr <= read_ptr <= self_ptr.
size_t tmp_ptr; // A temporary pointer used on replay.
size_t state_size;
size_t is_replay; // Are we replaying old frames?
bool can_poll; // We don't want to poll several times on a frame.
uint32_t packet_buffer[UDP_FRAME_PACKETS * 2]; // To compat UDP packet loss we also send old data along with the packets.
uint32_t frame_count;
uint32_t read_frame_count;
struct addrinfo *addr;
struct sockaddr_storage their_addr;
bool has_client_addr;
};
void input_poll_net(void)
{
if (!netplay_should_skip(g_extern.netplay) && netplay_can_poll(g_extern.netplay))
{
netplay_callbacks(g_extern.netplay)->poll_cb();
netplay_poll(g_extern.netplay);
}
}
void video_frame_net(const uint16_t *data, unsigned width, unsigned height)
{
if (!netplay_should_skip(g_extern.netplay))
netplay_callbacks(g_extern.netplay)->frame_cb(data, width, height);
}
void audio_sample_net(uint16_t left, uint16_t right)
{
if (!netplay_should_skip(g_extern.netplay))
netplay_callbacks(g_extern.netplay)->sample_cb(left, right);
}
int16_t input_state_net(bool port, unsigned device, unsigned index, unsigned id)
{
if (netplay_is_alive(g_extern.netplay))
return netplay_input_state(g_extern.netplay, port, device, index, id);
else
return netplay_callbacks(g_extern.netplay)->state_cb(port, device, index, id);
}
static bool init_tcp_socket(netplay_t *handle, const char *server, uint16_t port)
{
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof(hints));
#ifdef _WIN32 // Lolol, no AF_UNSPEC, wtf.
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_STREAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
char port_buf[16];
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo(server, port_buf, &hints, &res) < 0)
return false;
if (!res)
return false;
handle->fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (handle->fd < 0)
{
SSNES_ERR("Failed to init socket...\n");
if (res)
freeaddrinfo(res);
return false;
}
if (server)
{
if (connect(handle->fd, res->ai_addr, res->ai_addrlen) < 0)
{
SSNES_ERR("Failed to connect to server.\n");
close(handle->fd);
freeaddrinfo(res);
return false;
}
}
else
{
int yes = 1;
setsockopt(handle->fd, SOL_SOCKET, SO_REUSEADDR, CONST_CAST &yes, sizeof(int));
if (bind(handle->fd, res->ai_addr, res->ai_addrlen) < 0 || listen(handle->fd, 1) < 0)
{
SSNES_ERR("Failed to bind socket.\n");
close(handle->fd);
freeaddrinfo(res);
return false;
}
int new_fd = accept(handle->fd, NULL, NULL);
if (new_fd < 0)
{
SSNES_ERR("Failed to accept socket.\n");
close(handle->fd);
freeaddrinfo(res);
return false;
}
close(handle->fd);
handle->fd = new_fd;
}
freeaddrinfo(res);
return true;
}
static bool init_udp_socket(netplay_t *handle, const char *server, uint16_t port)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
#ifdef _WIN32 // Lolol, no AF_UNSPEC, wtf.
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_DGRAM;
if (!server)
hints.ai_flags = AI_PASSIVE;
char port_buf[16];
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo(server, port_buf, &hints, &handle->addr) < 0)
return false;
if (!handle->addr)
return false;
handle->udp_fd = socket(handle->addr->ai_family, handle->addr->ai_socktype, handle->addr->ai_protocol);
if (handle->udp_fd < 0)
{
SSNES_ERR("Failed to init socket...\n");
return false;
}
if (!server)
{
// Note sure if we have to do this for UDP, but hey :)
int yes = 1;
setsockopt(handle->udp_fd, SOL_SOCKET, SO_REUSEADDR, CONST_CAST &yes, sizeof(int));
if (bind(handle->udp_fd, handle->addr->ai_addr, handle->addr->ai_addrlen) < 0)
{
SSNES_ERR("Failed to bind socket.\n");
close(handle->udp_fd);
}
freeaddrinfo(handle->addr);
handle->addr = NULL;
}
return true;
}
static bool init_socket(netplay_t *handle, const char *server, uint16_t port)
{
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
WSACleanup();
return false;
}
#else
signal(SIGPIPE, SIG_IGN); // Do not like SIGPIPE killing our app :(
#endif
if (!init_tcp_socket(handle, server, port))
return false;
if (!init_udp_socket(handle, server, port))
return false;
return true;
}
bool netplay_can_poll(netplay_t *handle)
{
return handle->can_poll;
}
static bool send_info(netplay_t *handle)
{
uint32_t header[3] = { htonl(g_extern.cart_crc), htonl(psnes_serialize_size()), htonl(psnes_get_memory_size(SNES_MEMORY_CARTRIDGE_RAM)) };
if (send(handle->fd, CONST_CAST header, sizeof(header), 0) != sizeof(header))
return false;
// Get SRAM data from Player 1 :)
uint8_t *sram = psnes_get_memory_data(SNES_MEMORY_CARTRIDGE_RAM);
unsigned sram_size = psnes_get_memory_size(SNES_MEMORY_CARTRIDGE_RAM);
while (sram_size > 0)
{
ssize_t ret = recv(handle->fd, NONCONST_CAST sram, sram_size, 0);
if (ret <= 0)
{
SSNES_ERR("Failed to receive SRAM data from host.\n");
return false;
}
sram += ret;
sram_size -= ret;
}
return true;
}
static bool get_info(netplay_t *handle)
{
uint32_t header[3];
if (recv(handle->fd, NONCONST_CAST header, sizeof(header), 0) != sizeof(header))
{
SSNES_ERR("Failed to receive header from client.\n");
return false;
}
if (g_extern.cart_crc != ntohl(header[0]))
{
SSNES_ERR("Cart CRC32s differ! Cannot use different games!\n");
return false;
}
if (psnes_serialize_size() != ntohl(header[1]))
{
SSNES_ERR("Serialization sizes differ, make sure you're using exact same libsnes implementations!\n");
return false;
}
if (psnes_get_memory_size(SNES_MEMORY_CARTRIDGE_RAM) != ntohl(header[2]))
{
SSNES_ERR("Cartridge SRAM sizes do not correspond!\n");
return false;
}
// Send SRAM data to our Player 2 :)
const uint8_t *sram = psnes_get_memory_data(SNES_MEMORY_CARTRIDGE_RAM);
unsigned sram_size = psnes_get_memory_size(SNES_MEMORY_CARTRIDGE_RAM);
while (sram_size > 0)
{
ssize_t ret = send(handle->fd, CONST_CAST sram, sram_size, 0);
if (ret <= 0)
{
SSNES_ERR("Failed to send SRAM data to client.\n");
return false;
}
sram += ret;
sram_size -= ret;
}
return true;
}
static void init_buffers(netplay_t *handle)
{
handle->buffer = calloc(handle->buffer_size, sizeof(*handle->buffer));
handle->state_size = psnes_serialize_size();
for (unsigned i = 0; i < handle->buffer_size; i++)
{
handle->buffer[i].state = malloc(handle->state_size);
handle->buffer[i].is_simulated = true;
}
}
netplay_t *netplay_new(const char *server, uint16_t port, unsigned frames, const struct snes_callbacks *cb)
{
netplay_t *handle = calloc(1, sizeof(*handle));
if (!handle)
return NULL;
handle->cbs = *cb;
handle->port = server ? 0 : 1;
if (!init_socket(handle, server, port))
{
free(handle);
return NULL;
}
if (server)
{
if (!send_info(handle))
{
close(handle->fd);
free(handle);
return NULL;
}
}
else
{
if (!get_info(handle))
{
close(handle->fd);
free(handle);
return NULL;
}
}
handle->buffer_size = frames + 1;
init_buffers(handle);
handle->has_connection = true;
return handle;
}
bool netplay_is_alive(netplay_t *handle)
{
return handle->has_connection;
}
static bool send_chunk(netplay_t *handle)
{
const struct sockaddr *addr = NULL;
if (handle->addr)
addr = handle->addr->ai_addr;
else if (handle->has_client_addr)
addr = (const struct sockaddr*)&handle->their_addr;
if (addr)
{
if (sendto(handle->udp_fd, CONST_CAST handle->packet_buffer, sizeof(handle->packet_buffer), 0, addr, sizeof(struct sockaddr)) != sizeof(handle->packet_buffer))
{
SSNES_WARN("Netplay connection hung up. Will continue without netplay.\n");
handle->has_connection = false;
return false;
}
}
return true;
}
#define MAX_RETRIES 64
#define RETRY_MS 500
static int poll_input(netplay_t *handle, bool block)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(handle->udp_fd, &fds);
FD_SET(handle->fd, &fds);
int max_fd = (handle->fd > handle->udp_fd ? handle->fd : handle->udp_fd) + 1;
struct timeval tv = {
.tv_sec = 0,
.tv_usec = block ? (RETRY_MS * 1000) : 0
};
int i = 0;
do
{
if (select(max_fd, &fds, NULL, NULL, &tv) < 0)
return -1;
// Somewhat hacky, but we aren't using the TCP connection for anything useful atm.
// Will probably add some proper messaging system here later.
if (FD_ISSET(handle->fd, &fds))
return -1;
if (FD_ISSET(handle->udp_fd, &fds))
return 1;
if (block && !send_chunk(handle))
{
SSNES_WARN("Netplay connection hung up. Will continue without netplay.\n");
handle->has_connection = false;
return -1;
}
if (block)
SSNES_LOG("Network lag of %d ms, resending packet... Attempt %d of %d ...\n", RETRY_MS, i, MAX_RETRIES);
// Seems to be necessary on Win32.
FD_ZERO(&fds);
FD_SET(handle->udp_fd, &fds);
FD_SET(handle->fd, &fds);
} while ((i++ < MAX_RETRIES) && block);
if (block)
return -1;
return 0;
}
// Grab our own input state and send this over the network.
static bool get_self_input_state(netplay_t *handle)
{
struct delta_frame *ptr = &handle->buffer[handle->self_ptr];
uint32_t state = 0;
if (handle->frame_count > 0) // First frame we always give zero input since relying on input from first frame screws up when we use -F 0.
{
snes_input_state_t cb = handle->cbs.state_cb;
for (int i = 0; i <= 11; i++)
{
int16_t tmp = cb(g_settings.input.netplay_client_swap_input ? 0 : !handle->port, SNES_DEVICE_JOYPAD, 0, i);
state |= tmp ? 1 << i : 0;
}
}
memmove(handle->packet_buffer, handle->packet_buffer + 2, sizeof (handle->packet_buffer) - 2 * sizeof(uint32_t));
handle->packet_buffer[(UDP_FRAME_PACKETS - 1) * 2] = htonl(handle->frame_count);
handle->packet_buffer[(UDP_FRAME_PACKETS - 1) * 2 + 1] = htonl(state);
if (!send_chunk(handle))
{
SSNES_WARN("Netplay connection hung up. Will continue without netplay.\n");
handle->has_connection = false;
return false;
}
ptr->self_state = state;
handle->self_ptr = NEXT_PTR(handle->self_ptr);
return true;
}
// TODO: Somewhat better prediction. :P
static void simulate_input(netplay_t *handle)
{
size_t ptr = PREV_PTR(handle->self_ptr);
size_t prev = PREV_PTR(handle->read_ptr);
handle->buffer[ptr].simulated_input_state = handle->buffer[prev].real_input_state;
handle->buffer[ptr].is_simulated = true;
handle->buffer[ptr].used_real = false;
}
static void parse_packet(netplay_t *handle, uint32_t *buffer, unsigned size)
{
for (unsigned i = 0; i < size * 2; i++)
buffer[i] = ntohl(buffer[i]);
for (unsigned i = 0; i < size && handle->read_frame_count <= handle->frame_count; i++)
{
uint32_t frame = buffer[2 * i];
uint32_t state = buffer[2 * i + 1];
if (frame == handle->read_frame_count)
{
handle->buffer[handle->read_ptr].is_simulated = false;
handle->buffer[handle->read_ptr].real_input_state = state;
handle->read_ptr = NEXT_PTR(handle->read_ptr);
handle->read_frame_count++;
}
}
}
static bool receive_data(netplay_t *handle, uint32_t *buffer, size_t size)
{
socklen_t addrlen = sizeof(handle->their_addr);
if (recvfrom(handle->udp_fd, NONCONST_CAST buffer, size, 0, (struct sockaddr*)&handle->their_addr, &addrlen) != size)
return false;
handle->has_client_addr = true;
return true;
}
// Poll network to see if we have anything new. If our network buffer is full, we simply have to block for new input data.
bool netplay_poll(netplay_t *handle)
{
if (!handle->has_connection)
return false;
handle->can_poll = false;
if (!get_self_input_state(handle))
return false;
// We skip reading the first frame so the host has a change to grab our host info so we don't block forever :')
if (handle->frame_count == 0)
{
handle->buffer[0].used_real = true;
handle->buffer[0].is_simulated = false;
handle->buffer[0].real_input_state = 0;
handle->read_ptr = NEXT_PTR(handle->read_ptr);
handle->read_frame_count++;
return true;
}
//fprintf(stderr, "Other ptr: %lu, Read ptr: %lu, Self ptr: %lu\n", handle->other_ptr, handle->read_ptr, handle->self_ptr);
if (handle->buffer_size > 1)
{
assert(handle->other_ptr != handle->self_ptr);
assert(handle->read_ptr != handle->self_ptr);
}
assert(handle->other_ptr == handle->read_ptr);
// We might have reached the end of the buffer, where we simply have to block.
int res = poll_input(handle, handle->other_ptr == NEXT_PTR(handle->self_ptr));
if (res == -1)
{
handle->has_connection = false;
SSNES_WARN("Netplay connection timed out. Will continue without netplay.\n");
return false;
}
if (res == 1)
{
uint32_t first_read = handle->read_frame_count;
do
{
uint32_t buffer[UDP_FRAME_PACKETS * 2];
if (!receive_data(handle, buffer, sizeof(buffer)))
{
SSNES_WARN("Netplay connection hung up. Will continue without netplay.\n");
handle->has_connection = false;
return false;
}
parse_packet(handle, buffer, UDP_FRAME_PACKETS);
} while ((handle->read_frame_count <= handle->frame_count) &&
poll_input(handle, (handle->other_ptr == NEXT_PTR(handle->self_ptr)) &&
(first_read == handle->read_frame_count)) == 1);
}
else
{
// Cannot allow this. Should not happen though.
if (NEXT_PTR(handle->self_ptr) == handle->other_ptr)
{
SSNES_WARN("Netplay connection hung up. Will continue without netplay.\n");
return false;
}
}
if (handle->read_ptr != handle->self_ptr)
simulate_input(handle);
else
handle->buffer[PREV_PTR(handle->self_ptr)].used_real = true;
return true;
}
int16_t netplay_input_state(netplay_t *handle, bool port, unsigned device, unsigned index, unsigned id)
{
uint16_t input_state = 0;
size_t ptr = 0;
if (handle->is_replay)
ptr = handle->tmp_ptr;
else
ptr = PREV_PTR(handle->self_ptr);
if ((port ? 1 : 0) == handle->port)
{
if (handle->buffer[ptr].is_simulated)
input_state = handle->buffer[ptr].simulated_input_state;
else
input_state = handle->buffer[ptr].real_input_state;
}
else
input_state = handle->buffer[ptr].self_state;
return ((1 << id) & input_state) ? 1 : 0;
}
void netplay_free(netplay_t *handle)
{
close(handle->fd);
close(handle->udp_fd);
for (unsigned i = 0; i < handle->buffer_size; i++)
free(handle->buffer[i].state);
free(handle->buffer);
if (handle->addr)
freeaddrinfo(handle->addr);
free(handle);
}
const struct snes_callbacks* netplay_callbacks(netplay_t *handle)
{
return &handle->cbs;
}
bool netplay_should_skip(netplay_t *handle)
{
return handle->is_replay && handle->has_connection;
}
void netplay_pre_frame(netplay_t *handle)
{
psnes_serialize(handle->buffer[handle->self_ptr].state, handle->state_size);
handle->can_poll = true;
}
// Here we check if we have new input and replay from recorded input.
void netplay_post_frame(netplay_t *handle)
{
handle->frame_count++;
// Nothing to do...
if (handle->other_ptr == handle->read_ptr)
return;
// Skip ahead if we predicted correctly. Skip until our simulation failed.
while (handle->other_ptr != handle->read_ptr)
{
struct delta_frame *ptr = &handle->buffer[handle->other_ptr];
if ((ptr->simulated_input_state != ptr->real_input_state) && !ptr->used_real)
break;
handle->other_ptr = NEXT_PTR(handle->other_ptr);
}
if (handle->other_ptr != handle->read_ptr)
{
// Replay frames
handle->is_replay = true;
handle->tmp_ptr = handle->other_ptr;
psnes_unserialize(handle->buffer[handle->other_ptr].state, handle->state_size);
while (handle->tmp_ptr != handle->self_ptr)
{
psnes_serialize(handle->buffer[handle->tmp_ptr].state, handle->state_size);
lock_autosave();
psnes_run();
unlock_autosave();
handle->tmp_ptr = NEXT_PTR(handle->tmp_ptr);
}
handle->other_ptr = handle->read_ptr;
handle->is_replay = false;
}
}
+61
View File
@@ -0,0 +1,61 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_NETPLAY_H
#define __SSNES_NETPLAY_H
#include <stdint.h>
#include <stdbool.h>
#include <libsnes.hpp>
void input_poll_net(void);
int16_t input_state_net(bool port, unsigned device, unsigned index, unsigned id);
void video_frame_net(const uint16_t *data, unsigned width, unsigned height);
void audio_sample_net(uint16_t left, uint16_t right);
typedef struct netplay netplay_t;
struct snes_callbacks
{
snes_video_refresh_t frame_cb;
snes_audio_sample_t sample_cb;
snes_input_poll_t poll_cb;
snes_input_state_t state_cb;
};
// Creates a new netplay handle. A NULL host means we're hosting (player 1). :)
netplay_t *netplay_new(const char *server, uint16_t port, unsigned frames, const struct snes_callbacks *cb);
void netplay_free(netplay_t *handle);
// Call this before running snes_run()
void netplay_pre_frame(netplay_t *handle);
// Call this after running snes_run()
void netplay_post_frame(netplay_t *handle);
// Checks if input port/index is controlled by netplay or not.
bool netplay_is_alive(netplay_t *handle);
bool netplay_poll(netplay_t *handle);
int16_t netplay_input_state(netplay_t *handle, bool port, unsigned device, unsigned index, unsigned id);
// If we're fast-forward replaying to resync, check if we should actually show frame.
bool netplay_should_skip(netplay_t *handle);
bool netplay_can_poll(netplay_t *handle);
const struct snes_callbacks* netplay_callbacks(netplay_t *handle);
#endif
+15 -2
View File
@@ -15,6 +15,9 @@ if [ $HAVE_DYNAMIC != yes ]; then
add_define_make libsnes $LIBSNES
fi
check_lib DYLIB -ldl dlopen
check_lib NETPLAY -lc socket
check_lib ALSA -lasound snd_pcm_open
check_header OSS sys/soundcard.h
@@ -24,9 +27,15 @@ else
check_lib AL -lopenal alcOpenDevice
fi
if [ "$OS" = "Darwin" ]; then
check_lib FBO "-framework OpenGL" glFramebufferTexture2D
else
check_lib FBO -lGL glFramebufferTexture2D
fi
check_pkgconf RSOUND rsound 1.1
check_lib ROAR -lroar roar_vs_new
check_lib JACK -ljack jack_client_open
check_pkgconf JACK jack 0.120.1
check_pkgconf PULSE libpulse
check_pkgconf SDL sdl 1.2.10
@@ -34,6 +43,7 @@ check_critical SDL "Cannot find SDL library."
check_lib CG -lCg cgCreateContext
check_pkgconf XML libxml-2.0
check_pkgconf IMLIB imlib2
if [ $HAVE_FFMPEG != no ]; then
check_pkgconf AVCODEC libavcodec
@@ -50,9 +60,12 @@ check_pkgconf SRC samplerate
check_lib DYNAMIC -ldl dlopen
check_pkgconf FREETYPE freetype2
check_lib XVIDEO -lXv XvShmCreateImage
check_lib STRL -lc strlcpy
# Creates config.mk and config.h.
VARS="ALSA OSS AL RSOUND ROAR JACK PULSE SDL FILTER CG XML DYNAMIC FFMPEG AVCODEC AVFORMAT AVCORE AVUTIL SWSCALE SRC CONFIGFILE FREETYPE"
VARS="ALSA OSS AL RSOUND ROAR JACK PULSE SDL DYLIB CG XML IMLIB DYNAMIC FFMPEG AVCODEC AVFORMAT AVCORE AVUTIL SWSCALE SRC CONFIGFILE FREETYPE XVIDEO NETPLAY FBO STRL"
create_config_make config.mk $VARS
create_config_header config.h $VARS
+6 -2
View File
@@ -1,7 +1,7 @@
. qb/qb.params.sh
PACKAGE_NAME=ssnes
PACKAGE_VERSION=0.2.2
PACKAGE_VERSION=0.5.1
# Adds a command line opt to ./configure --help
# $1: Variable (HAVE_ALSA, HAVE_OSS, etc)
@@ -10,11 +10,13 @@ PACKAGE_VERSION=0.2.2
add_command_line_enable DYNAMIC "Enable dynamic loading of libsnes library." no
add_command_line_string LIBSNES "libsnes library used" "-lsnes"
add_command_line_enable FFMPEG "Enable FFmpeg recording support" no
add_command_line_enable FILTER "Enable CPU filter support" no
add_command_line_enable DYLIB "Enable dynamic loading support" auto
add_command_line_enable NETPLAY "Enable netplay support" auto
add_command_line_enable SRC "Enable libsamplerate support" no
add_command_line_enable CONFIGFILE "Disable support for config file" yes
add_command_line_enable CG "Enable Cg shader support" auto
add_command_line_enable XML "Enable bSNES-style XML shader support" auto
add_command_line_enable FBO "Enable render-to-texture (FBO) support" auto
add_command_line_enable ALSA "Enable ALSA support" auto
add_command_line_enable OSS "Enable OSS support" auto
add_command_line_enable RSOUND "Enable RSound support" auto
@@ -23,3 +25,5 @@ add_command_line_enable AL "Enable OpenAL support" auto
add_command_line_enable JACK "Enable JACK support" auto
add_command_line_enable PULSE "Enable PulseAudio support" auto
add_command_line_enable FREETYPE "Enable FreeType support" auto
add_command_line_enable XVIDEO "Enable XVideo support" auto
add_command_line_enable IMLIB "Enable imlib2 support" auto
+2 -2
View File
@@ -50,7 +50,7 @@ check_lib()
extralibs="$4"
$CC -o $TEMP_EXE $TEMP_C $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
$CC -o $TEMP_EXE $TEMP_C $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $CFLAGS $LDFLAGS $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
echo $answer
@@ -75,7 +75,7 @@ check_lib_cxx()
extralibs="$4"
$CXX -o $TEMP_EXE $TEMP_CXX $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
$CXX -o $TEMP_EXE $TEMP_CXX $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $CFLAGS $LDFLAGS $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
echo $answer
+8 -1
View File
@@ -33,6 +33,7 @@ struct state_manager
size_t top_ptr;
size_t bottom_ptr;
size_t state_size;
bool first_pop;
};
state_manager_t *state_manager_new(size_t state_size, size_t buffer_size, void *init_buffer)
@@ -77,6 +78,11 @@ void state_manager_free(state_manager_t *state)
bool state_manager_pop(state_manager_t *state, void **data)
{
*data = state->tmp_state;
if (state->first_pop)
{
state->first_pop = false;
return true;
}
if (state->top_ptr == 0)
state->top_ptr = state->buf_size - 1;
@@ -126,7 +132,7 @@ static void generate_delta(state_manager_t *state, const void *data)
const uint32_t *new_state = data;
state->buffer[state->top_ptr++] = 0; // For each separate delta, we have a 0 value sentinel in between.
if (state->top_ptr == state->bottom_ptr) // Check if top_ptr and bottom_ptr crossed eachother, which means we need to delete old cruft.
if (state->top_ptr == state->bottom_ptr) // Check if top_ptr and bottom_ptr crossed each other, which means we need to delete old cruft.
crossed = true;
for (uint64_t i = 0; i < state->state_size; i++)
@@ -156,6 +162,7 @@ bool state_manager_push(state_manager_t *state, const void *data)
{
generate_delta(state, data);
memcpy(state->tmp_state, data, state->state_size * sizeof(uint32_t));
state->first_pop = true;
return true;
}
+111
View File
@@ -0,0 +1,111 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "screenshot.h"
#include "strl.h"
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "general.h"
// Simple 24bpp .BMP writer.
static void write_header(FILE *file, unsigned width, unsigned height)
{
unsigned line_size = (width * 3 + 3) & ~3;
unsigned size = line_size * height + 54;
unsigned size_array = line_size * height;
// Generic BMP stuff.
const uint8_t header[] = {
'B', 'M',
(size >> 0) & 0xff, (size >> 8) & 0xff, (size >> 16) & 0xff, (size >> 24) & 0xff,
0, 0, 0, 0,
54, 0, 0, 0,
40, 0, 0, 0,
(width >> 0) & 0xff, (width >> 8) & 0xff, (width >> 16) & 0xff, (width >> 24) & 0xff,
(height >> 0) & 0xff, (height >> 8) & 0xff, (height >> 16) & 0xff, (height >> 24) & 0xff,
1, 0,
24, 0,
0, 0, 0, 0,
(size_array >> 0) & 0xff, (size_array >> 8) & 0xff, (size_array >> 16) & 0xff, (size_array >> 24) & 0xff,
19, 11, 0, 0,
19, 11, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
fwrite(header, 1, sizeof(header), file);
}
static void dump_content(FILE *file, const uint16_t *frame, unsigned width, unsigned height, unsigned pitch)
{
pitch >>= 1;
unsigned line_size = (width * 3 + 3) & ~3;
uint8_t line[line_size];
memset(line, 0, sizeof(line));
// BMP likes reverse ordering for some reason :v
for (int j = height - 1; j >= 0; j--)
{
uint8_t *dst = line;
const uint16_t *src = frame + j * pitch;
for (unsigned i = 0; i < width; i++)
{
uint16_t pixel = *src++;
uint8_t b = (pixel & 0x1f) << 3;
uint8_t g = (pixel & 0x03e0) >> 2;
uint8_t r = (pixel & 0x7c00) >> 7;
*dst++ = b;
*dst++ = g;
*dst++ = r;
}
fwrite(line, 1, sizeof(line), file);
}
}
bool screenshot_dump(const char *folder, const uint16_t *frame,
unsigned width, unsigned height, unsigned pitch)
{
time_t cur_time;
time(&cur_time);
char timefmt[128];
strftime(timefmt, sizeof(timefmt), "SSNES-%m%d-%H%M%S.bmp", localtime(&cur_time));
char filename[256];
strlcpy(filename, folder, sizeof(filename));
strlcat(filename, "/", sizeof(filename));
strlcat(filename, timefmt, sizeof(filename));
FILE *file = fopen(filename, "wb");
if (!file)
{
SSNES_ERR("Failed to open file \"%s\" for screenshot.\n", filename);
return false;
}
write_header(file, width, height);
dump_content(file, frame, width, height, pitch);
fclose(file);
return true;
}
+27
View File
@@ -0,0 +1,27 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_SCREENSHOT_H
#define __SSNES_SCREENSHOT_H
#include <stdint.h>
#include <stdbool.h>
bool screenshot_dump(const char *folder, const uint16_t *frame,
unsigned width, unsigned height, unsigned pitch);
#endif
+254 -100
View File
@@ -18,8 +18,7 @@
#include "general.h"
#include "conf/config_file.h"
#include <assert.h>
#include <string.h>
#include "hqflt/filters.h"
#include "strl.h"
#include "config.def.h"
#ifdef HAVE_CONFIG_H
@@ -45,6 +44,15 @@ static void set_defaults(void)
case VIDEO_GL:
def_video = "gl";
break;
case VIDEO_XVIDEO:
def_video = "xvideo";
break;
case VIDEO_SDL:
def_video = "sdl";
break;
case VIDEO_EXT:
def_video = "ext";
break;
default:
break;
}
@@ -75,6 +83,9 @@ static void set_defaults(void)
case AUDIO_PULSE:
def_audio = "pulse";
break;
case AUDIO_EXT:
def_audio = "ext";
break;
default:
break;
}
@@ -84,29 +95,32 @@ static void set_defaults(void)
case INPUT_SDL:
def_input = "sdl";
break;
case INPUT_X:
def_input = "x";
break;
default:
break;
}
if (def_video)
strncpy(g_settings.video.driver, def_video, sizeof(g_settings.video.driver) - 1);
strlcpy(g_settings.video.driver, def_video, sizeof(g_settings.video.driver));
if (def_audio)
strncpy(g_settings.audio.driver, def_audio, sizeof(g_settings.audio.driver) - 1);
strlcpy(g_settings.audio.driver, def_audio, sizeof(g_settings.audio.driver));
if (def_input)
strncpy(g_settings.input.driver, def_input, sizeof(g_settings.input.driver) - 1);
strlcpy(g_settings.input.driver, def_input, sizeof(g_settings.input.driver));
g_settings.video.xscale = xscale;
g_settings.video.yscale = yscale;
g_settings.video.fullscreen = fullscreen;
g_settings.video.fullscreen_x = fullscreen_x;
g_settings.video.fullscreen_y = fullscreen_y;
g_settings.video.force_16bit = force_16bit;
g_settings.video.vsync = vsync;
g_settings.video.smooth = video_smooth;
g_settings.video.force_aspect = force_aspect;
g_settings.video.crop_overscan = crop_overscan;
g_settings.video.aspect_ratio = SNES_ASPECT_RATIO;
#ifdef HAVE_FILTER
g_settings.video.filter = FILTER_NONE;
#endif
g_settings.video.shader_type = SSNES_SHADER_AUTO;
#ifdef HAVE_FREETYPE
g_settings.video.font_size = font_size;
@@ -114,12 +128,19 @@ static void set_defaults(void)
g_settings.video.msg_pos_y = message_pos_offset_y;
#endif
#if defined(HAVE_CG) || defined(HAVE_XML)
g_settings.video.render_to_texture = render_to_texture;
g_settings.video.fbo_scale_x = fbo_scale_x;
g_settings.video.fbo_scale_y = fbo_scale_y;
g_settings.video.second_pass_smooth = second_pass_smooth;
#endif
g_settings.audio.enable = audio_enable;
g_settings.audio.out_rate = out_rate;
g_settings.audio.in_rate = in_rate;
g_settings.audio.rate_step = audio_rate_step;
if (audio_device)
strncpy(g_settings.audio.device, audio_device, sizeof(g_settings.audio.device));
strlcpy(g_settings.audio.device, audio_device, sizeof(g_settings.audio.device));
g_settings.audio.latency = out_latency;
g_settings.audio.sync = audio_sync;
@@ -131,21 +152,19 @@ static void set_defaults(void)
g_settings.rewind_buffer_size = rewind_buffer_size;
g_settings.rewind_granularity = rewind_granularity;
g_settings.pause_nonactive = pause_nonactive;
g_settings.autosave_interval = autosave_interval;
assert(sizeof(g_settings.input.binds[0]) >= sizeof(snes_keybinds_1));
assert(sizeof(g_settings.input.binds[1]) >= sizeof(snes_keybinds_2));
assert(sizeof(g_settings.input.binds[2]) >= sizeof(snes_keybinds_3));
assert(sizeof(g_settings.input.binds[3]) >= sizeof(snes_keybinds_4));
assert(sizeof(g_settings.input.binds[4]) >= sizeof(snes_keybinds_5));
assert(sizeof(g_settings.input.binds[1]) >= sizeof(snes_keybinds_rest));
memcpy(g_settings.input.binds[0], snes_keybinds_1, sizeof(snes_keybinds_1));
memcpy(g_settings.input.binds[1], snes_keybinds_2, sizeof(snes_keybinds_2));
memcpy(g_settings.input.binds[2], snes_keybinds_3, sizeof(snes_keybinds_3));
memcpy(g_settings.input.binds[3], snes_keybinds_4, sizeof(snes_keybinds_4));
memcpy(g_settings.input.binds[4], snes_keybinds_5, sizeof(snes_keybinds_5));
for (unsigned i = 1; i < 5; i++)
memcpy(g_settings.input.binds[i], snes_keybinds_rest, sizeof(snes_keybinds_rest));
g_settings.input.axis_threshold = AXIS_THRESHOLD;
g_settings.input.netplay_client_swap_input = netplay_client_swap_input;
for (int i = 0; i < MAX_PLAYERS; i++)
g_settings.input.joypad_map[i] = i;
}
#ifdef HAVE_CONFIGFILE
@@ -162,6 +181,64 @@ void parse_config(void)
#endif
}
#ifdef HAVE_CONFIGFILE
static config_file_t *open_default_config_file(void)
{
config_file_t *conf = NULL;
#ifdef _WIN32
// Just do something for now.
conf = config_file_new("ssnes.cfg");
if (!conf)
{
const char *appdata = getenv("APPDATA");
if (appdata)
{
char conf_path[strlen(appdata) + strlen("/ssnes.cfg ")];
strcpy(conf_path, appdata);
strcat(conf_path, "/ssnes.cfg");
conf = config_file_new(conf_path);
}
}
#elif defined(__APPLE__)
const char *home = getenv("HOME");
if (home)
{
char conf_path[strlen(home) + strlen("/.ssnes.cfg ")];
strcpy(conf_path, home);
strcat(conf_path, "/.ssnes.cfg");
conf = config_file_new(conf_path);
}
if (!conf)
conf = config_file_new("/etc/ssnes.cfg");
#else
const char *xdg = getenv("XDG_CONFIG_HOME");
if (!xdg)
SSNES_WARN("XDG_CONFIG_HOME is not defined. Will look for config in $HOME/.ssnes.cfg ...\n");
const char *home = getenv("HOME");
if (xdg)
{
char conf_path[strlen(xdg) + strlen("/ssnes/ssnes.cfg ")];
strcpy(conf_path, xdg);
strcat(conf_path, "/ssnes/ssnes.cfg");
conf = config_file_new(conf_path);
}
else if (home)
{
char conf_path[strlen(home) + strlen("/.ssnes.cfg ")];
strcpy(conf_path, home);
strcat(conf_path, "/.ssnes.cfg");
conf = config_file_new(conf_path);
}
// Try this as a last chance...
if (!conf)
conf = config_file_new("/etc/ssnes.cfg");
#endif
return conf;
}
#endif
// Macros to ease config getting.
#define CONFIG_GET_BOOL(var, key) if (config_get_bool(conf, key, &tmp_bool)) \
g_settings.var = tmp_bool
@@ -175,7 +252,7 @@ void parse_config(void)
#define CONFIG_GET_STRING(var, key) do { \
if (config_get_string(conf, key, &tmp_str)) \
{ \
strncpy(g_settings.var, tmp_str, sizeof(g_settings.var) - 1); \
strlcpy(g_settings.var, tmp_str, sizeof(g_settings.var)); \
free(tmp_str); \
} \
} while(0)
@@ -195,35 +272,7 @@ static void parse_config_file(void)
}
}
else
{
#ifdef _WIN32
// Just do something for now.
conf = config_file_new("ssnes.cfg");
#else
const char *xdg = getenv("XDG_CONFIG_HOME");
if (!xdg)
SSNES_WARN("XDG_CONFIG_HOME is not defined. Will look for config in $HOME/.ssnesrc ...\n");
const char *home = getenv("HOME");
if (xdg)
{
char conf_path[strlen(xdg) + strlen("/ssnes/ssnes.cfg ")];
strcpy(conf_path, xdg);
strcat(conf_path, "/ssnes/ssnes.cfg");
conf = config_file_new(conf_path);
}
else if (home)
{
char conf_path[strlen(home) + strlen("/.ssnesrc ")];
strcpy(conf_path, home);
strcat(conf_path, "/.ssnesrc");
conf = config_file_new(conf_path);
}
// Try this as a last chance...
if (!conf)
conf = config_file_new("/etc/ssnes.cfg");
#endif
}
conf = open_default_config_file();
if (conf == NULL)
return;
@@ -241,13 +290,20 @@ static void parse_config_file(void)
CONFIG_GET_INT(video.fullscreen_x, "video_fullscreen_x");
CONFIG_GET_INT(video.fullscreen_y, "video_fullscreen_y");
CONFIG_GET_BOOL(video.fullscreen, "video_fullscreen");
CONFIG_GET_BOOL(video.force_16bit, "video_force_16bit");
CONFIG_GET_BOOL(video.vsync, "video_vsync");
CONFIG_GET_BOOL(video.smooth, "video_smooth");
CONFIG_GET_BOOL(video.force_aspect, "video_force_aspect");
CONFIG_GET_BOOL(video.crop_overscan, "video_crop_overscan");
CONFIG_GET_DOUBLE(video.aspect_ratio, "video_aspect_ratio");
CONFIG_GET_STRING(video.cg_shader_path, "video_cg_shader");
CONFIG_GET_STRING(video.bsnes_shader_path, "video_bsnes_shader");
CONFIG_GET_STRING(video.second_pass_shader, "video_second_pass_shader");
CONFIG_GET_BOOL(video.render_to_texture, "video_render_to_texture");
CONFIG_GET_DOUBLE(video.fbo_scale_x, "video_fbo_scale_x");
CONFIG_GET_DOUBLE(video.fbo_scale_y, "video_fbo_scale_y");
CONFIG_GET_BOOL(video.second_pass_smooth, "video_second_pass_smooth");
#ifdef HAVE_FREETYPE
CONFIG_GET_STRING(video.font_path, "video_font_path");
@@ -256,40 +312,34 @@ static void parse_config_file(void)
CONFIG_GET_DOUBLE(video.msg_pos_y, "video_message_pos_y");
#endif
#ifdef HAVE_FILTER
if (config_get_string(conf, "video_filter", &tmp_str))
#ifdef HAVE_DYLIB
CONFIG_GET_STRING(video.filter_path, "video_filter");
CONFIG_GET_STRING(video.external_driver, "video_external_driver");
CONFIG_GET_STRING(audio.external_driver, "audio_external_driver");
#endif
#if defined(HAVE_CG) || defined(HAVE_XML)
if (config_get_string(conf, "video_shader_type", &tmp_str))
{
unsigned filter = 0;
if (strcasecmp(FILTER_HQ2X_STR, tmp_str) == 0)
filter = FILTER_HQ2X;
else if (strcasecmp(FILTER_HQ4X_STR, tmp_str) == 0)
filter = FILTER_HQ4X;
else if (strcasecmp(FILTER_GRAYSCALE_STR, tmp_str) == 0)
filter = FILTER_GRAYSCALE;
else if (strcasecmp(FILTER_BLEED_STR, tmp_str) == 0)
filter = FILTER_BLEED;
else if (strcasecmp(FILTER_NTSC_STR, tmp_str) == 0)
filter = FILTER_NTSC;
else
{
SSNES_ERR(
"Invalid filter... Valid filters are:\n"
"\t%s\n"
"\t%s\n"
"\t%s\n"
"\t%s\n"
"\t%s\n",
FILTER_HQ2X_STR, FILTER_HQ4X_STR, FILTER_GRAYSCALE_STR,
FILTER_BLEED_STR, FILTER_NTSC_STR);
exit(1);
}
if (strcmp("cg", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_CG;
else if (strcmp("bsnes", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_BSNES;
else if (strcmp("auto", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_AUTO;
else if (strcmp("none", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_NONE;
free(tmp_str);
g_settings.video.filter = filter;
}
#endif
#if defined(HAVE_XML)
CONFIG_GET_STRING(video.shader_dir, "video_shader_dir");
#endif
CONFIG_GET_DOUBLE(input.axis_threshold, "input_axis_threshold");
CONFIG_GET_BOOL(input.netplay_client_swap_input, "netplay_client_swap_input");
CONFIG_GET_INT(input.joypad_map[0], "input_player1_joypad_index");
CONFIG_GET_INT(input.joypad_map[1], "input_player2_joypad_index");
CONFIG_GET_INT(input.joypad_map[2], "input_player3_joypad_index");
@@ -297,7 +347,6 @@ static void parse_config_file(void)
CONFIG_GET_INT(input.joypad_map[4], "input_player5_joypad_index");
// Audio settings.
CONFIG_GET_BOOL(audio.enable, "audio_enable");
CONFIG_GET_INT(audio.out_rate, "audio_out_rate");
CONFIG_GET_DOUBLE(audio.in_rate, "audio_in_rate");
@@ -319,8 +368,10 @@ static void parse_config_file(void)
CONFIG_GET_STRING(video.driver, "video_driver");
CONFIG_GET_STRING(audio.driver, "audio_driver");
CONFIG_GET_STRING(audio.dsp_plugin, "audio_dsp_plugin");
CONFIG_GET_STRING(input.driver, "input_driver");
CONFIG_GET_STRING(libsnes, "libsnes_path");
CONFIG_GET_STRING(screenshot_directory, "screenshot_directory");
CONFIG_GET_BOOL(rewind_enable, "rewind_enable");
@@ -330,6 +381,9 @@ static void parse_config_file(void)
CONFIG_GET_INT(rewind_granularity, "rewind_granularity");
CONFIG_GET_BOOL(pause_nonactive, "pause_nonactive");
CONFIG_GET_INT(autosave_interval, "autosave_interval");
CONFIG_GET_STRING(cheat_database, "cheat_database_path");
read_keybinds(conf);
@@ -362,6 +416,7 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(player1_up, SNES_DEVICE_ID_JOYPAD_UP)
DECLARE_BIND(player1_down, SNES_DEVICE_ID_JOYPAD_DOWN)
DECLARE_BIND(toggle_fast_forward, SSNES_FAST_FORWARD_KEY)
DECLARE_BIND(hold_fast_forward, SSNES_FAST_FORWARD_HOLD_KEY)
DECLARE_BIND(save_state, SSNES_SAVE_STATE_KEY)
DECLARE_BIND(load_state, SSNES_LOAD_STATE_KEY)
DECLARE_BIND(state_slot_increase, SSNES_STATE_SLOT_PLUS)
@@ -372,6 +427,15 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
DECLARE_BIND(cheat_index_plus, SSNES_CHEAT_INDEX_PLUS)
DECLARE_BIND(cheat_index_minus, SSNES_CHEAT_INDEX_MINUS)
DECLARE_BIND(cheat_toggle, SSNES_CHEAT_TOGGLE)
DECLARE_BIND(screenshot, SSNES_SCREENSHOT)
DECLARE_BIND(dsp_config, SSNES_DSP_CONFIG)
},
{
DECLARE_BIND(player2_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -387,6 +451,7 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(player2_up, SNES_DEVICE_ID_JOYPAD_UP)
DECLARE_BIND(player2_down, SNES_DEVICE_ID_JOYPAD_DOWN)
DECLARE_BIND(toggle_fast_forward, SSNES_FAST_FORWARD_KEY)
DECLARE_BIND(hold_fast_forward, SSNES_FAST_FORWARD_HOLD_KEY)
DECLARE_BIND(save_state, SSNES_SAVE_STATE_KEY)
DECLARE_BIND(load_state, SSNES_LOAD_STATE_KEY)
DECLARE_BIND(state_slot_increase, SSNES_STATE_SLOT_PLUS)
@@ -397,6 +462,15 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
DECLARE_BIND(cheat_index_plus, SSNES_CHEAT_INDEX_PLUS)
DECLARE_BIND(cheat_index_minus, SSNES_CHEAT_INDEX_MINUS)
DECLARE_BIND(cheat_toggle, SSNES_CHEAT_TOGGLE)
DECLARE_BIND(screenshot, SSNES_SCREENSHOT)
DECLARE_BIND(dsp_config, SSNES_DSP_CONFIG)
},
{
DECLARE_BIND(player3_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -412,6 +486,7 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(player3_up, SNES_DEVICE_ID_JOYPAD_UP)
DECLARE_BIND(player3_down, SNES_DEVICE_ID_JOYPAD_DOWN)
DECLARE_BIND(toggle_fast_forward, SSNES_FAST_FORWARD_KEY)
DECLARE_BIND(hold_fast_forward, SSNES_FAST_FORWARD_HOLD_KEY)
DECLARE_BIND(save_state, SSNES_SAVE_STATE_KEY)
DECLARE_BIND(load_state, SSNES_LOAD_STATE_KEY)
DECLARE_BIND(state_slot_increase, SSNES_STATE_SLOT_PLUS)
@@ -422,6 +497,15 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
DECLARE_BIND(cheat_index_plus, SSNES_CHEAT_INDEX_PLUS)
DECLARE_BIND(cheat_index_minus, SSNES_CHEAT_INDEX_MINUS)
DECLARE_BIND(cheat_toggle, SSNES_CHEAT_TOGGLE)
DECLARE_BIND(screenshot, SSNES_SCREENSHOT)
DECLARE_BIND(dsp_config, SSNES_DSP_CONFIG)
},
{
DECLARE_BIND(player4_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -437,6 +521,7 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(player4_up, SNES_DEVICE_ID_JOYPAD_UP)
DECLARE_BIND(player4_down, SNES_DEVICE_ID_JOYPAD_DOWN)
DECLARE_BIND(toggle_fast_forward, SSNES_FAST_FORWARD_KEY)
DECLARE_BIND(hold_fast_forward, SSNES_FAST_FORWARD_HOLD_KEY)
DECLARE_BIND(save_state, SSNES_SAVE_STATE_KEY)
DECLARE_BIND(load_state, SSNES_LOAD_STATE_KEY)
DECLARE_BIND(state_slot_increase, SSNES_STATE_SLOT_PLUS)
@@ -447,6 +532,15 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
DECLARE_BIND(cheat_index_plus, SSNES_CHEAT_INDEX_PLUS)
DECLARE_BIND(cheat_index_minus, SSNES_CHEAT_INDEX_MINUS)
DECLARE_BIND(cheat_toggle, SSNES_CHEAT_TOGGLE)
DECLARE_BIND(screenshot, SSNES_SCREENSHOT)
DECLARE_BIND(dsp_config, SSNES_DSP_CONFIG)
},
{
DECLARE_BIND(player5_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -462,6 +556,7 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(player5_up, SNES_DEVICE_ID_JOYPAD_UP)
DECLARE_BIND(player5_down, SNES_DEVICE_ID_JOYPAD_DOWN)
DECLARE_BIND(toggle_fast_forward, SSNES_FAST_FORWARD_KEY)
DECLARE_BIND(hold_fast_forward, SSNES_FAST_FORWARD_HOLD_KEY)
DECLARE_BIND(save_state, SSNES_SAVE_STATE_KEY)
DECLARE_BIND(load_state, SSNES_LOAD_STATE_KEY)
DECLARE_BIND(state_slot_increase, SSNES_STATE_SLOT_PLUS)
@@ -472,6 +567,15 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
DECLARE_BIND(cheat_index_plus, SSNES_CHEAT_INDEX_PLUS)
DECLARE_BIND(cheat_index_minus, SSNES_CHEAT_INDEX_MINUS)
DECLARE_BIND(cheat_toggle, SSNES_CHEAT_TOGGLE)
DECLARE_BIND(screenshot, SSNES_SCREENSHOT)
DECLARE_BIND(dsp_config, SSNES_DSP_CONFIG)
},
};
@@ -489,17 +593,20 @@ static const struct key_map sdlk_map[] = {
{ "up", SDLK_UP },
{ "down", SDLK_DOWN },
{ "enter", SDLK_RETURN },
{ "kp_enter", SDLK_KP_ENTER },
{ "tab", SDLK_TAB },
{ "insert", SDLK_INSERT },
{ "del", SDLK_DELETE },
{ "end", SDLK_END },
{ "home", SDLK_HOME },
{ "rshift", SDLK_RSHIFT },
{ "shift", SDLK_LSHIFT },
{ "ctrl", SDLK_LCTRL },
{ "alt", SDLK_LALT },
{ "space", SDLK_SPACE },
{ "escape", SDLK_ESCAPE },
{ "kp_plus", SDLK_KP_PLUS },
{ "kp_minus", SDLK_KP_MINUS },
{ "add", SDLK_KP_PLUS },
{ "subtract", SDLK_KP_MINUS },
{ "f1", SDLK_F1 },
{ "f2", SDLK_F2 },
{ "f3", SDLK_F3 },
@@ -512,6 +619,38 @@ static const struct key_map sdlk_map[] = {
{ "f10", SDLK_F10 },
{ "f11", SDLK_F11 },
{ "f12", SDLK_F12 },
{ "num0", SDLK_0 },
{ "num1", SDLK_1 },
{ "num2", SDLK_2 },
{ "num3", SDLK_3 },
{ "num4", SDLK_4 },
{ "num5", SDLK_5 },
{ "num6", SDLK_6 },
{ "num7", SDLK_7 },
{ "num8", SDLK_8 },
{ "num9", SDLK_9 },
{ "pageup", SDLK_PAGEUP },
{ "pagedown", SDLK_PAGEDOWN },
{ "keypad0", SDLK_KP0 },
{ "keypad1", SDLK_KP1 },
{ "keypad2", SDLK_KP2 },
{ "keypad3", SDLK_KP3 },
{ "keypad4", SDLK_KP4 },
{ "keypad5", SDLK_KP5 },
{ "keypad6", SDLK_KP6 },
{ "keypad7", SDLK_KP7 },
{ "keypad8", SDLK_KP8 },
{ "keypad9", SDLK_KP9 },
{ "period", SDLK_PERIOD },
{ "capslock", SDLK_CAPSLOCK },
{ "numlock", SDLK_NUMLOCK },
{ "backspace", SDLK_BACKSPACE },
{ "multiply", SDLK_KP_MULTIPLY },
{ "divide", SDLK_KP_DIVIDE },
{ "print_screen", SDLK_PRINT },
{ "scroll_lock", SDLK_SCROLLOCK },
{ "tilde", SDLK_BACKQUOTE },
{ "nul", SDLK_UNKNOWN },
};
static struct snes_keybind *find_snes_bind(unsigned port, int id)
@@ -545,6 +684,7 @@ static int find_sdlk_key(const char *str)
return find_sdlk_bind(str);
}
// Yes, this function needs a good refactor :)
static void read_keybinds(config_file_t *conf)
{
char *tmp_key = NULL;
@@ -559,6 +699,7 @@ static void read_keybinds(config_file_t *conf)
if (!bind)
continue;
// Check keybind
if (bind_maps[j][i].key && config_get_string(conf, bind_maps[j][i].key, &tmp_key))
{
int key = find_sdlk_key(tmp_key);
@@ -570,40 +711,53 @@ static void read_keybinds(config_file_t *conf)
tmp_key = NULL;
}
// Check joybutton bind (hats too)
if (bind_maps[j][i].btn && config_get_string(conf, bind_maps[j][i].btn, &tmp_btn))
{
const char *btn = tmp_btn;
if (*btn++ == 'h')
if (strcmp(tmp_btn, "nul") == 0)
{
if (isdigit(*btn))
{
char *dir = NULL;
int hat = strtol(btn, &dir, 0);
int hat_dir = 0;
if (dir)
{
if (strcasecmp(dir, "up") == 0)
hat_dir = HAT_UP_MASK;
else if (strcasecmp(dir, "down") == 0)
hat_dir = HAT_DOWN_MASK;
else if (strcasecmp(dir, "left") == 0)
hat_dir = HAT_LEFT_MASK;
else if (strcasecmp(dir, "right") == 0)
hat_dir = HAT_RIGHT_MASK;
if (hat_dir)
bind->joykey = HAT_MAP(hat, hat_dir);
}
}
bind->joykey = NO_BTN;
}
else
bind->joykey = strtol(tmp_btn, NULL, 0);
{
if (*btn++ == 'h')
{
if (isdigit(*btn))
{
char *dir = NULL;
int hat = strtol(btn, &dir, 0);
int hat_dir = 0;
if (dir)
{
if (strcasecmp(dir, "up") == 0)
hat_dir = HAT_UP_MASK;
else if (strcasecmp(dir, "down") == 0)
hat_dir = HAT_DOWN_MASK;
else if (strcasecmp(dir, "left") == 0)
hat_dir = HAT_LEFT_MASK;
else if (strcasecmp(dir, "right") == 0)
hat_dir = HAT_RIGHT_MASK;
if (hat_dir)
bind->joykey = HAT_MAP(hat, hat_dir);
}
}
}
else
bind->joykey = strtol(tmp_btn, NULL, 0);
}
free(tmp_btn);
}
// Check joyaxis binds.
if (bind_maps[j][i].axis && config_get_string(conf, bind_maps[j][i].axis, &tmp_axis))
{
if (strlen(tmp_axis) >= 2 && (*tmp_axis == '+' || *tmp_axis == '-'))
if (strcmp(tmp_axis, "nul") == 0)
{
bind->joyaxis = AXIS_NONE;
}
else if (strlen(tmp_axis) >= 2 && (*tmp_axis == '+' || *tmp_axis == '-'))
{
int axis = strtol(tmp_axis + 1, NULL, 0);
if (*tmp_axis == '+')
+180
View File
@@ -0,0 +1,180 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
// SHA256 implementation from bSNES. Written by valditx.
//
#include "sha256.h"
#include <string.h>
#include <stdio.h>
#define PTR(t, a) ((t*)(a))
#define SWAP32(x) ((uint32_t)( \
(((uint32_t)(x) & 0x000000ff) << 24) | \
(((uint32_t)(x) & 0x0000ff00) << 8) | \
(((uint32_t)(x) & 0x00ff0000) >> 8) | \
(((uint32_t)(x) & 0xff000000) >> 24) \
))
#define ST32(a, d) *PTR(uint32_t, a) = (d)
#define ST32BE(a, d) ST32(a, SWAP32(d))
#define LD32(a) *PTR(uint32_t, a)
#define LD32BE(a) SWAP32(LD32(a))
#define LSL32(x, n) ((uint32_t)(x) << (n))
#define LSR32(x, n) ((uint32_t)(x) >> (n))
#define ROR32(x, n) (LSR32(x, n) | LSL32(x, 32 - (n)))
// First 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
static const uint32_t T_H[8] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
};
// First 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311
static const uint32_t T_K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
};
struct sha256_ctx
{
uint8_t in[64];
unsigned inlen;
uint32_t w[64];
uint32_t h[8];
uint64_t len;
};
static void sha256_init(struct sha256_ctx *p)
{
memset(p, 0, sizeof(struct sha256_ctx));
memcpy(p->h, T_H, sizeof(T_H));
}
static void sha256_block(struct sha256_ctx *p)
{
unsigned i;
uint32_t s0, s1;
uint32_t a, b, c, d, e, f, g, h;
uint32_t t1, t2, maj, ch;
for (i = 0; i < 16; i++)
p->w[i] = LD32BE(p->in + i * 4);
for (i = 16; i < 64; i++)
{
s0 = ROR32(p->w[i - 15], 7) ^ ROR32(p->w[i - 15], 18) ^ LSR32(p->w[i - 15], 3);
s1 = ROR32(p->w[i - 2], 17) ^ ROR32(p->w[i - 2], 19) ^ LSR32(p->w[i - 2], 10);
p->w[i] = p->w[i - 16] + s0 + p->w[i - 7] + s1;
}
a = p->h[0]; b = p->h[1]; c = p->h[2]; d = p->h[3];
e = p->h[4]; f = p->h[5]; g = p->h[6]; h = p->h[7];
for (i = 0; i < 64; i++)
{
s0 = ROR32(a, 2) ^ ROR32(a, 13) ^ ROR32(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
t2 = s0 + maj;
s1 = ROR32(e, 6) ^ ROR32(e, 11) ^ ROR32(e, 25);
ch = (e & f) ^ (~e & g);
t1 = h + s1 + ch + T_K[i] + p->w[i];
h = g; g = f; f = e; e = d + t1;
d = c; c = b; b = a; a = t1 + t2;
}
p->h[0] += a; p->h[1] += b; p->h[2] += c; p->h[3] += d;
p->h[4] += e; p->h[5] += f; p->h[6] += g; p->h[7] += h;
// Next block
p->inlen = 0;
}
static void sha256_chunk(struct sha256_ctx *p, const uint8_t *s, unsigned len)
{
unsigned l;
p->len += len;
while (len)
{
l = 64 - p->inlen;
l = (len < l) ? len : l;
memcpy(p->in + p->inlen, s, l);
s += l;
p->inlen += l;
len -= l;
if (p->inlen == 64)
sha256_block(p);
}
}
static void sha256_final(struct sha256_ctx *p)
{
uint64_t len;
p->in[p->inlen++] = 0x80;
if (p->inlen > 56)
{
memset(p->in + p->inlen, 0, 64 - p->inlen);
sha256_block(p);
}
memset(p->in + p->inlen, 0, 56 - p->inlen);
len = p->len << 3;
ST32BE(p->in + 56, len >> 32);
ST32BE(p->in + 60, len);
sha256_block(p);
}
static void sha256_subhash(struct sha256_ctx *p, uint32_t *t)
{
for (unsigned i = 0; i < 8; i++)
ST32BE(t++, p->h[i]);
}
void sha256_hash(char *out, const uint8_t *in, size_t size)
{
struct sha256_ctx sha;
union
{
uint32_t u32[8];
uint8_t u8[32];
} shahash;
sha256_init(&sha);
sha256_chunk(&sha, in, size);
sha256_final(&sha);
sha256_subhash(&sha, shahash.u32);
for (unsigned i = 0; i < 32; i++)
snprintf(out + 2 * i, 3, "%02x", (unsigned)shahash.u8[i]);
}
+28
View File
@@ -0,0 +1,28 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_SHA256_H
#define __SSNES_SHA256_H
#include <stdint.h>
#include <stddef.h>
// Hashes sha256 and outputs a human readable string for comparing with the cheat XML values.
void sha256_hash(char *out, const uint8_t *in, size_t size);
#endif
Executable
+95
View File
@@ -0,0 +1,95 @@
#!/bin/sh
###
## Wrapper script that enables loading of compressed roms for SSNES.
## Author: Themaister
## License: Do whatever you want with it.
#
die()
{
echo "Error: $1"
rm "$TMPROM"
rm -r "$TMPDIR"
exit 1
}
if [ -z "$1" ]; then
die "Need at least one argument"
fi
COMP_FILE="$1"
TMPROM="`mktemp`"
TMPDIR="`mktemp -d`"
shift
extract_zip()
{
which unzip >/dev/null 2>&1 || die "Cannot find unzip"
which zipinfo >/dev/null 2>&1 || die "Cannot find zipinfo"
FILENAME="`zipinfo -1 \"$1\" | grep '\.s[mf]c$' | head -n1`"
if [ -z "$FILENAME" ]; then
die "Cannot find .smc nor .sfc file in archive"
fi
# The people who made the [!] naming convention obviously have never used regex ... <_<
FILENAME_FIXED="`echo $FILENAME | sed -e 's|\[|\\\\[|g' -e 's|!|\\\\!|g' -e 's|\]|\\\\]|g'`"
unzip -p "$1" "$FILENAME_FIXED" > "$TMPROM"
}
extract_7z()
{
which 7z >/dev/null 2>&1 || die "Cannot find 7z"
7z x "$1" -o"$TMPDIR" >/dev/null 2>&1 || die "Couldn't extract from archive"
FILENAME="`ls \"$TMPDIR\" | grep '\.s[mf]c$' | head -n1`"
if [ -z "$FILENAME" ]; then
die "Cannot find .smc nor .sfc file in archive"
fi
mv "${TMPDIR}/${FILENAME}" "$TMPROM"
}
extract_rar()
{
which unrar >/dev/null 2>&1 || die "Cannot find unrar"
unrar x "$1" "$TMPDIR" >/dev/null 2>&1 || die "Couldn't extract from archive"
FILENAME="`ls \"$TMPDIR\" | grep '\.s[mf]c$' | head -n1`"
if [ -z "$FILENAME" ]; then
die "Cannot find .smc nor .sfc file in archive"
fi
mv "${TMPDIR}/${FILENAME}" "$TMPROM"
}
case "$COMP_FILE" in
*.zip )
extract_zip "$COMP_FILE"
EXTENSION=".zip"
;;
*.7z )
extract_7z "$COMP_FILE"
EXTENSION=".7z"
;;
*.rar )
extract_rar "$COMP_FILE"
EXTENSION=".rar"
;;
* )
die "Couldn't find valid zipped rom."
;;
esac
SAVENAME="`basename \"$COMP_FILE\" $EXTENSION`"
DIRNAME="`dirname \"$COMP_FILE\"`"
SAVENAME="${SAVENAME}.srm"
SSNES_PATH="`which ssnes`"
if [ -z "$SSNES_PATH" ]; then
die "Cannot find SSNES in path."
fi
echo "Temp ROM: $TMPROM"
echo "Savename: ${DIRNAME}/${SAVENAME}"
"$SSNES_PATH" "$TMPROM" -s "${DIRNAME}/${SAVENAME}" "$@"
rm "$TMPROM"
rm -r "$TMPDIR"
+621 -170
View File
File diff suppressed because it is too large. Load diff
+84 -8
View File
@@ -5,17 +5,26 @@
#### Video
# Windowed xscale and yscale (Real x res: 296 * xscale, real y scale: 224 * xscale)
# Video driver to use. "gl", "xvideo", "sdl" or "ext" (external API driver)
# video_driver = "gl"
# Path to external video driver using the SSNES driver API.
# video_external_driver =
# Windowed xscale and yscale (Real x res: 224 * xscale * aspect_ratio, real y scale: 224 * xscale)
# video_xscale = 3.0
# video_yscale = 3.0
# Fullscreen resolution
# video_fullscreen_x = 1920
# video_fullscreen_y = 1200
# Fullscreen resolution. Resolution of 0 uses the resolution of the desktop.
# video_fullscreen_x = 0
# video_fullscreen_y = 0
# Start in fullscreen. Can be changed at runtime.
# video_fullscreen = false
# Force 16-bit colors. Apparently some video cards in use today have troubles with 32-bit ...
# video_force_16bit = false
# Video vsync.
# video_vsync = true
@@ -28,14 +37,38 @@
# A floating point value for video aspect ratio (width / height)
# video_aspect_ratio = 1.333
# Forces cropping of overscanned frames. Crops away top 7 scanlines and 8 bottom scanlines. (15/15 for interlaced frames).
# video_crop_overscan = false
# Path to Cg shader.
# video_cg_shader = "/path/to/cg/shader.cg"
# Path to bSNES-style XML shader (GLSL only). If both Cg shader path and XML shader path are defined, Cg shader will take priority.
# Path to bSNES XML shader (GLSL only). If both Cg shader path and XML shader path are defined,
# Cg shader will take priority unless overridden in video_shader_type.
# video_bsnes_shader = "/path/to/bsnes/xml/shader.shader"
# CPU-based filter. Valid ones are: hq2x, hq4x, grayscale, bleed, ntsc.
# video_filter = ntsc
# Which shader type to use. Valid values are "cg", "bsnes", "none" and "auto"
# video_shader_type = auto
# Defines a directory where XML shaders are kept.
# video_shader_dir =
# Render to texture first. Useful when doing multi-pass shaders or control the output of shaders better.
# video_render_to_texture = false
# Defines the video scale of render-to-texture.
# The output FBO size is scaled by these amounts against the input size (typically 256 * 224 for SNES).
# video_fbo_scale_x = 2.0
# video_fbo_scale_y = 2.0
# Define shader to use for second pass (needs render-to-texture).
# video_second_pass_shader = "/path/to/second/shader.{cg,shader}"
# Defines if bilinear filtering is used during second pass (needs render-to-texture).
# video_second_pass_smooth = true
# CPU-based filter. Path to a bSNES CPU filter (*.filter)
# video_filter =
# Path to a TTF font used for rendering messages. This path must be defined to enable fonts.
# Do note that the _full_ path of the font is necessary!
@@ -66,12 +99,18 @@
# When altering audio_in_rate on-the-fly, define by how much each time.
# audio_rate_step = 0.25
# Audio driver backend. Depending on configuration possible candidates are: alsa, pulse, oss, jack, rsound, roar, openal, sdl and xaudio
# Audio driver backend. Depending on configuration possible candidates are: alsa, pulse, oss, jack, rsound, roar, openal, sdl, xaudio and ext (external driver).
# audio_driver =
# Path to external audio driver using the SSNES audio driver API.
# audio_external_driver =
# Override the default audio device the audio_driver uses. This is driver dependant. E.g. ALSA wants a PCM device, OSS wants a path (e.g. /dev/dsp), Jack wants portnames (e.g. system:playback1,system:playback_2), and so on ...
# audio_device =
# External DSP plugin that processes audio before it's sent to the driver.
# audio_dsp_plugin =
# Will sync (block) on audio. Recommended.
# audio_sync = true
@@ -90,6 +129,8 @@
# input_axis_threshold = 0.5
# Keyboard input. Will recognize normal keypresses and special keys like "left", "right", and so on.
# Keyboard input, Joypad and Joyaxis will all obey the "nul" bind, which disables the bind completely,
# rather than relying on a default.
# input_player1_a = x
# input_player1_b = z
# input_player1_y = a
@@ -184,6 +225,9 @@
# Toggles between fast-forwarding and normal speed.
# input_toggle_fast_forward = space
# Hold for fast-forward. Releasing button disables fast-forward.
# input_hold_fast_forward = l
# Key to exit emulator cleanly.
# Killing it in any hard way (SIGTERM, SIGKILL, etc, will terminate emulator without saving RAM, etc.)
# input_exit_emulator = escape
@@ -192,6 +236,10 @@
# input_rate_step_up = kp_plus
# input_rate_step_down = kp_minus
# Applies next and previous XML shader in directory.
# input_shader_next = m
# input_shader_prev = n
# Enable rewinding. This will take a performance hit when playing, so it is disabled by default.
# Do note that rewinding will only work properly when using bSNES libsnes core atm.
# rewind_enable = false
@@ -212,5 +260,33 @@
# Toggle between paused and non-paused state
# input_pause_toggle = p
# Reset the emulated SNES.
# input_reset = h
# Configures DSP plugin
# input_dsp_config = c
# Pause gameplay when window focus is lost.
# pause_nonactive = true
# Autosaves the non-volatile SRAM at a regular interval. This is disabled by default unless set otherwise.
# The interval is measured in seconds. A value of 0 disables autosave.
# autosave_interval =
# When being client over netplay, use keybinds for player 1.
# netplay_client_swap_input = false
# Path to XML cheat database (as used by bSNES).
# cheat_database_path =
# Cheats.
# input_cheat_index_plus = y
# input_cheat_index_minus = t
# input_cheat_toggle = u
# Take screenshot
# input_screenshot = print_screen
# Directory to dump screenshots to.
# screenshot_directory =
+52
View File
@@ -0,0 +1,52 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "strl.h"
#ifndef HAVE_STRL
// Implementation of strlcpy()/strlcat() based on OpenBSD.
size_t strlcpy(char *dest, const char *source, size_t size)
{
size_t src_size = 0;
size_t n = size;
if (n)
while (--n && (*dest++ = *source++)) src_size++;
if (!n)
{
if (size) *dest = '\0';
while (*source++) src_size++;
}
return src_size;
}
size_t strlcat(char *dest, const char *source, size_t size)
{
size_t len = strlen(dest);
dest += len;
if (len > size)
size = 0;
else
size -= len;
return len + strlcpy(dest, source, size);
}
#endif
+12 -20
View File
@@ -15,32 +15,24 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __SSNES_STRL_H
#define __SSNES_STRL_H
#ifndef __FILTERS_H
#define __FILTERS_H
#include <string.h>
#include <stddef.h>
#ifdef HAVE_CONFIG_H
#ifdef HAVE_CONFIG
#include "config.h"
#endif
#ifdef HAVE_FILTER
#ifndef HAVE_STRL
// Avoid possible naming collitions during link since we prefer to use the actual name.
#define strlcpy(dst, src, size) __strlcpy_ssnes(dst, src, size)
#define strlcat(dst, src, size) __strlcat_ssnes(dst, src, size)
#include "pastlib.h"
#include "grayscale.h"
#include "bleed.h"
#include "ntsc.h"
#define FILTER_NONE 0
#define FILTER_HQ2X 1
#define FILTER_HQ4X 2
#define FILTER_GRAYSCALE 3
#define FILTER_BLEED 4
#define FILTER_NTSC 5
#define FILTER_HQ2X_STR "hq2x"
#define FILTER_HQ4X_STR "hq4x"
#define FILTER_GRAYSCALE_STR "grayscale"
#define FILTER_BLEED_STR "bleed"
#define FILTER_NTSC_STR "ntsc"
size_t strlcpy(char *dest, const char *source, size_t size);
size_t strlcat(char *dest, const char *source, size_t size);
#endif
#endif
+10 -1
View File
@@ -86,14 +86,23 @@ static struct bind binds[] = {
MISC_BIND("Save state slot increase", state_slot_increase)
MISC_BIND("Save state slot decrease", state_slot_decrease)
MISC_BIND("Toggle fast forward", toggle_fast_forward)
MISC_BIND("Hold fast forward", hold_fast_forward)
MISC_BIND("Audio input rate step up", rate_step_up)
MISC_BIND("Audio input rate step down", rate_step_down)
MISC_BIND("Rewind", rewind)
MISC_BIND("Movie recording toggle", movie_record_toggle)
MISC_BIND("Pause", pause_toggle)
MISC_BIND("Reset", reset)
MISC_BIND("Next shader", shader_next)
MISC_BIND("Previous shader", shader_prev)
MISC_BIND("Toggle cheat on/off", cheat_toggle)
MISC_BIND("Cheat index plus", cheat_index_plus)
MISC_BIND("Cheat index minus", cheat_index_minus)
MISC_BIND("Screenshot", screenshot)
MISC_BIND("DSP config", dsp_config)
};
void get_binds(config_file_t *conf, int player, int joypad)
static void get_binds(config_file_t *conf, int player, int joypad)
{
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO) < 0)
{
+165
View File
@@ -0,0 +1,165 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "ups.h"
#include "movie.h"
struct ups_data
{
const uint8_t *patch_data, *source_data;
uint8_t *target_data;
unsigned patch_length, source_length, target_length;
unsigned patch_offset, source_offset, target_offset;
unsigned patch_checksum, source_checksum, target_checksum;
};
static uint8_t patch_read(struct ups_data *data)
{
if (data->patch_offset < data->patch_length)
{
uint8_t n = data->patch_data[data->patch_offset++];
data->patch_checksum = crc32_adjust(data->patch_checksum, n);
return n;
}
return 0x00;
}
static uint8_t source_read(struct ups_data *data)
{
if (data->source_offset < data->source_length)
{
uint8_t n = data->source_data[data->source_offset++];
data->source_checksum = crc32_adjust(data->source_checksum, n);
return n;
}
return 0x00;
}
static void target_write(struct ups_data *data, uint8_t n)
{
if (data->target_offset < data->target_length)
{
data->target_data[data->target_offset] = n;
data->target_checksum = crc32_adjust(data->target_checksum, n);
}
data->target_offset++;
}
static uint64_t decode(struct ups_data *data)
{
uint64_t offset = 0, shift = 1;
while (true)
{
uint8_t x = patch_read(data);
offset += (x & 0x7f) * shift;
if (x & 0x80)
break;
shift <<= 7;
offset += shift;
}
return offset;
}
ups_error_t ups_apply_patch(
const uint8_t *patchdata, size_t patchlength,
const uint8_t *sourcedata, size_t sourcelength,
uint8_t *targetdata, size_t *targetlength)
{
struct ups_data data = {
.patch_data = patchdata,
.source_data = sourcedata,
.target_data = targetdata,
.patch_length = patchlength,
.source_length = sourcelength,
.target_length = *targetlength,
.patch_checksum = ~0,
.source_checksum = ~0,
.target_checksum = ~0
};
if (data.patch_length < 18)
return UPS_PATCH_INVALID;
if (patch_read(&data) != 'U')
return UPS_PATCH_INVALID;
if (patch_read(&data) != 'P')
return UPS_PATCH_INVALID;
if (patch_read(&data) != 'S')
return UPS_PATCH_INVALID;
if (patch_read(&data) != '1')
return UPS_PATCH_INVALID;
unsigned source_read_length = decode(&data);
unsigned target_read_length = decode(&data);
if (data.source_length != source_read_length && data.source_length != target_read_length)
return UPS_SOURCE_INVALID;
*targetlength = (data.source_length == source_read_length ? target_read_length : source_read_length);
if (data.target_length < *targetlength)
return UPS_TARGET_TOO_SMALL;
data.target_length = *targetlength;
while (data.patch_offset < data.patch_length - 12)
{
unsigned length = decode(&data);
while (length--)
target_write(&data, source_read(&data));
while (true)
{
uint8_t patch_xor = patch_read(&data);
target_write(&data, patch_xor ^ source_read(&data));
if (patch_xor == 0) break;
}
}
while (data.source_offset < data.source_length)
target_write(&data, source_read(&data));
while (data.target_offset < data.target_length)
target_write(&data, source_read(&data));
uint32_t patch_read_checksum = 0, source_read_checksum = 0, target_read_checksum = 0;
for (unsigned i = 0; i < 4; i++)
source_read_checksum |= patch_read(&data) << (i * 8);
for (unsigned i = 0; i < 4; i++)
target_read_checksum |= patch_read(&data) << (i * 8);
uint32_t patch_result_checksum = ~data.patch_checksum;
data.source_checksum = ~data.source_checksum;
data.target_checksum = ~data.target_checksum;
for (unsigned i = 0; i < 4; i++)
patch_read_checksum |= patch_read(&data) << (i * 8);
if (patch_result_checksum != patch_read_checksum)
return UPS_PATCH_INVALID;
if (data.source_checksum == source_read_checksum && data.source_length == source_read_length)
{
if (data.target_checksum == target_read_checksum && data.target_length == target_read_length)
return UPS_SUCCESS;
return UPS_TARGET_INVALID;
}
else if (data.source_checksum == target_read_checksum && data.source_length == target_read_length)
{
if (data.target_checksum == source_read_checksum && data.target_length == source_read_length)
return UPS_SUCCESS;
return UPS_TARGET_INVALID;
}
else
return UPS_SOURCE_INVALID;
}
+44
View File
@@ -0,0 +1,44 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __UPS_H
#define __UPS_H
#include <stdint.h>
#include <stddef.h>
// UPS implementation from bSNES.
typedef enum ups_error
{
UPS_UNKNOWN,
UPS_SUCCESS,
UPS_PATCH_INVALID,
UPS_SOURCE_INVALID,
UPS_TARGET_INVALID,
UPS_TARGET_TOO_SMALL,
UPS_PATCH_CHECKSUM_INVALID,
UPS_SOURCE_CHECKSUM_INVALID,
UPS_TARGET_CHECKSUM_INVALID
} ups_error_t;
ups_error_t ups_apply_patch(
const uint8_t *patch_data, size_t patch_length,
const uint8_t *source_data, size_t source_length,
uint8_t *target_data, size_t *target_length);
#endif