Aslai

media SDL

Dec 23rd, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.01 KB | None | 0 0
  1. #include "engine/media.h"
  2. #include "engine/mediaSDL.h"
  3.  
  4. void _SDL_init ( Context* me )
  5. {
  6.     me->_internal_draw_circle           = _SDL_draw_circle;
  7.     me->_internal_create_screen         = _SDL_create_screen;
  8.     me->_internal_draw_ellipse          = _SDL_draw_ellipse;
  9.     me->_internal_draw_image            = _SDL_draw_image;
  10.     me->_internal_draw_image_part       = _SDL_draw_image_part;
  11.     me->_internal_draw_line             = _SDL_draw_line;
  12.     me->_internal_draw_rectangle        = _SDL_draw_rectangle;
  13.     me->_internal_draw_surface          = _SDL_draw_surface;
  14.     me->_internal_draw_surface_part     = _SDL_draw_surface_part;
  15.     me->_internal_handle_io             = _SDL_handle_io;
  16.     me->_internal_image_from_surface    = _SDL_image_from_surface;
  17.     me->_internal_image_height          = _SDL_image_height;
  18.     me->_internal_image_load            = _SDL_image_load;
  19.     me->_internal_image_load_mem        = _SDL_image_load_mem;
  20.     me->_internal_image_unload          = _SDL_image_unload;
  21.     me->_internal_image_width           = _SDL_image_width;
  22.     me->_internal_repaint               = _SDL_repaint;
  23.     me->_internal_sound_getlength       = _SDL_sound_getlength;
  24.     me->_internal_sound_getsamplerate   = _SDL_sound_getsamplerate;
  25.     me->_internal_sound_load            = _SDL_sound_load;
  26.     me->_internal_sound_load_mem        = _SDL_sound_load_mem;
  27.     me->_internal_sound_play            = _SDL_sound_play;
  28.     me->_internal_sound_unload          = _SDL_sound_unload;
  29.     me->_internal_surface_create        = _SDL_surface_create;
  30.     me->_internal_surface_free          = _SDL_surface_free;
  31.     me->_internal_surface_from_image    = _SDL_surface_from_image;
  32.     me->_internal_surface_height        = _SDL_surface_height;
  33.     me->_internal_surface_width         = _SDL_surface_width;
  34.     me->_internal_cleanup               = _SDL_cleanup;
  35.     me->_internal_init_sound            = _SDL_init_sound;
  36.     me->_internal_sound_set_callback    = _SDL_sound_set_callback;
  37.     me->_internal_set_caption           = _SDL_set_caption;
  38.  
  39.  
  40.  
  41.     me->EventInfo.Mouse.Button.Left          = SDL_BUTTON_LEFT;
  42.     me->EventInfo.Mouse.Button.Middle        = SDL_BUTTON_MIDDLE;
  43.     me->EventInfo.Mouse.Button.Right         = SDL_BUTTON_RIGHT;
  44.     me->EventInfo.Mouse.Button.X1            = SDL_BUTTON_X1;
  45.     me->EventInfo.Mouse.Button.X2            = SDL_BUTTON_X2;
  46.     me->EventInfo.Mouse.Button.WheelUp       = SDL_BUTTON_WHEELUP;
  47.     me->EventInfo.Mouse.Button.WheelDown     = SDL_BUTTON_WHEELDOWN;
  48.  
  49.  
  50.     me->EventInfo.Keyboard.Key.Escape        = SDLK_ESCAPE;
  51.     me->EventInfo.Keyboard.Key.F1            = SDLK_F1;
  52.     me->EventInfo.Keyboard.Key.F2            = SDLK_F2;
  53.     me->EventInfo.Keyboard.Key.F3            = SDLK_F3;
  54.     me->EventInfo.Keyboard.Key.F4            = SDLK_F4;
  55.     me->EventInfo.Keyboard.Key.F5            = SDLK_F5;
  56.     me->EventInfo.Keyboard.Key.F6            = SDLK_F6;
  57.     me->EventInfo.Keyboard.Key.F7            = SDLK_F7;
  58.     me->EventInfo.Keyboard.Key.F8            = SDLK_F8;
  59.     me->EventInfo.Keyboard.Key.F9            = SDLK_F9;
  60.     me->EventInfo.Keyboard.Key.F10           = SDLK_F10;
  61.     me->EventInfo.Keyboard.Key.F11           = SDLK_F11;
  62.     me->EventInfo.Keyboard.Key.F12           = SDLK_F12;
  63.     me->EventInfo.Keyboard.Key.F13           = SDLK_F13;
  64.     me->EventInfo.Keyboard.Key.F14           = SDLK_F14;
  65.     me->EventInfo.Keyboard.Key.F15           = SDLK_F15;
  66.     me->EventInfo.Keyboard.Key.F16           = SDLK_F15;
  67.     me->EventInfo.Keyboard.Key.F17           = SDLK_F15;
  68.     me->EventInfo.Keyboard.Key.F18           = SDLK_F15;
  69.     me->EventInfo.Keyboard.Key.F19           = SDLK_F15;
  70.     me->EventInfo.Keyboard.Key.F20           = SDLK_F15;
  71.     me->EventInfo.Keyboard.Key.PrintScreen   = SDLK_PRINT;
  72.     me->EventInfo.Keyboard.Key.ScrollLock    = SDLK_SCROLLOCK;
  73.     me->EventInfo.Keyboard.Key.Pause         = SDLK_PAUSE;
  74.     me->EventInfo.Keyboard.Key.Tilde         = SDLK_BACKQUOTE;
  75.     me->EventInfo.Keyboard.Key.K_1           = SDLK_1;
  76.     me->EventInfo.Keyboard.Key.K_2           = SDLK_2;
  77.     me->EventInfo.Keyboard.Key.K_3           = SDLK_3;
  78.     me->EventInfo.Keyboard.Key.K_4           = SDLK_4;
  79.     me->EventInfo.Keyboard.Key.K_5           = SDLK_5;
  80.     me->EventInfo.Keyboard.Key.K_6           = SDLK_6;
  81.     me->EventInfo.Keyboard.Key.K_7           = SDLK_7;
  82.     me->EventInfo.Keyboard.Key.K_8           = SDLK_8;
  83.     me->EventInfo.Keyboard.Key.K_9           = SDLK_9;
  84.     me->EventInfo.Keyboard.Key.K_0           = SDLK_0;
  85.     me->EventInfo.Keyboard.Key.Dash          = SDLK_MINUS;
  86.     me->EventInfo.Keyboard.Key.Equals        = SDLK_EQUALS;
  87.     me->EventInfo.Keyboard.Key.Backspace     = SDLK_BACKSPACE;
  88.     me->EventInfo.Keyboard.Key.Tab           = SDLK_TAB;
  89.     me->EventInfo.Keyboard.Key.Q             = SDLK_q;
  90.     me->EventInfo.Keyboard.Key.W             = SDLK_w;
  91.     me->EventInfo.Keyboard.Key.E             = SDLK_e;
  92.     me->EventInfo.Keyboard.Key.R             = SDLK_r;
  93.     me->EventInfo.Keyboard.Key.T             = SDLK_t;
  94.     me->EventInfo.Keyboard.Key.Y             = SDLK_y;
  95.     me->EventInfo.Keyboard.Key.U             = SDLK_u;
  96.     me->EventInfo.Keyboard.Key.I             = SDLK_i;
  97.     me->EventInfo.Keyboard.Key.O             = SDLK_o;
  98.     me->EventInfo.Keyboard.Key.P             = SDLK_p;
  99.     me->EventInfo.Keyboard.Key.BracketOpen   = SDLK_LEFTBRACKET;
  100.     me->EventInfo.Keyboard.Key.BracketClosed = SDLK_RIGHTBRACKET;
  101.     me->EventInfo.Keyboard.Key.FSlash        = SDLK_BACKSLASH;
  102.     me->EventInfo.Keyboard.Key.ForwardSlash  = SDLK_BACKSLASH;
  103.     me->EventInfo.Keyboard.Key.CapsLock      = SDLK_CAPSLOCK;
  104.     me->EventInfo.Keyboard.Key.A             = SDLK_a;
  105.     me->EventInfo.Keyboard.Key.S             = SDLK_s;
  106.     me->EventInfo.Keyboard.Key.D             = SDLK_d;
  107.     me->EventInfo.Keyboard.Key.F             = SDLK_f;
  108.     me->EventInfo.Keyboard.Key.G             = SDLK_g;
  109.     me->EventInfo.Keyboard.Key.H             = SDLK_h;
  110.     me->EventInfo.Keyboard.Key.J             = SDLK_j;
  111.     me->EventInfo.Keyboard.Key.K             = SDLK_k;
  112.     me->EventInfo.Keyboard.Key.L             = SDLK_l;
  113.     me->EventInfo.Keyboard.Key.Colon         = SDLK_COLON;
  114.     me->EventInfo.Keyboard.Key.Quote         = SDLK_QUOTE;
  115.     me->EventInfo.Keyboard.Key.Enter         = SDLK_RETURN;
  116.     me->EventInfo.Keyboard.Key.LShift        = SDLK_LSHIFT;
  117.     me->EventInfo.Keyboard.Key.Z             = SDLK_z;
  118.     me->EventInfo.Keyboard.Key.X             = SDLK_x;
  119.     me->EventInfo.Keyboard.Key.C             = SDLK_c;
  120.     me->EventInfo.Keyboard.Key.V             = SDLK_v;
  121.     me->EventInfo.Keyboard.Key.B             = SDLK_b;
  122.     me->EventInfo.Keyboard.Key.N             = SDLK_n;
  123.     me->EventInfo.Keyboard.Key.M             = SDLK_m;
  124.     me->EventInfo.Keyboard.Key.Comma         = SDLK_COMMA;
  125.     me->EventInfo.Keyboard.Key.Period        = SDLK_PERIOD;
  126.     me->EventInfo.Keyboard.Key.Slash         = SDLK_SLASH;
  127.     me->EventInfo.Keyboard.Key.RShift        = SDLK_RSHIFT;
  128.     me->EventInfo.Keyboard.Key.LControl      = SDLK_LCTRL;
  129.     me->EventInfo.Keyboard.Key.LAlt          = SDLK_LALT;
  130.     me->EventInfo.Keyboard.Key.Space         = SDLK_SPACE;
  131.     me->EventInfo.Keyboard.Key.RAlt          = SDLK_RALT;
  132.     me->EventInfo.Keyboard.Key.RControl      = SDLK_RCTRL;
  133.     me->EventInfo.Keyboard.Key.Insert        = SDLK_INSERT;
  134.     me->EventInfo.Keyboard.Key.Home          = SDLK_HOME;
  135.     me->EventInfo.Keyboard.Key.PageUp        = SDLK_PAGEUP;
  136.     me->EventInfo.Keyboard.Key.Delete        = SDLK_DELETE;
  137.     me->EventInfo.Keyboard.Key.End           = SDLK_END;
  138.     me->EventInfo.Keyboard.Key.PageDown      = SDLK_PAGEDOWN;
  139.     me->EventInfo.Keyboard.Key.NumLock       = SDLK_NUMLOCK;
  140.     me->EventInfo.Keyboard.Key.NumSlash      = SDLK_KP_DIVIDE;
  141.     me->EventInfo.Keyboard.Key.NumStar       = SDLK_KP_MULTIPLY;
  142.     me->EventInfo.Keyboard.Key.NumMinus      = SDLK_KP_MINUS;
  143.     me->EventInfo.Keyboard.Key.Num7          = SDLK_KP7;
  144.     me->EventInfo.Keyboard.Key.Num8          = SDLK_KP8;
  145.     me->EventInfo.Keyboard.Key.Num9          = SDLK_KP9;
  146.     me->EventInfo.Keyboard.Key.NumPlus       = SDLK_KP_PLUS;
  147.     me->EventInfo.Keyboard.Key.Num4          = SDLK_KP4;
  148.     me->EventInfo.Keyboard.Key.Num5          = SDLK_KP5;
  149.     me->EventInfo.Keyboard.Key.Num6          = SDLK_KP6;
  150.     me->EventInfo.Keyboard.Key.Num1          = SDLK_KP1;
  151.     me->EventInfo.Keyboard.Key.Num2          = SDLK_KP2;
  152.     me->EventInfo.Keyboard.Key.Num3          = SDLK_KP3;
  153.     me->EventInfo.Keyboard.Key.NumEnter      = SDLK_KP_ENTER;
  154.     me->EventInfo.Keyboard.Key.Num0          = SDLK_KP0;
  155.     me->EventInfo.Keyboard.Key.NumPeriod     = SDLK_KP_PERIOD;
  156.     me->EventInfo.Keyboard.Key.Left          = SDLK_LEFT;
  157.     me->EventInfo.Keyboard.Key.Right         = SDLK_RIGHT;
  158.     me->EventInfo.Keyboard.Key.Up            = SDLK_UP;
  159.     me->EventInfo.Keyboard.Key.Down          = SDLK_DOWN;
  160.  
  161.  
  162.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  163.         return;
  164.  
  165.  
  166.     me->EngineBase = me->EngineBasis.SDL;
  167.  
  168.  
  169. }
  170.  
  171.  
  172.  
  173. void _SDL_handle_io ( Context* me )
  174. {
  175.     SDL_Event event;
  176.     while( SDL_PollEvent( &event ) )
  177.     {
  178.         switch( event.type )
  179.         {
  180.  
  181.             case SDL_VIDEORESIZE:{ me->listener_push_event( me->EventInfo.Type.System, me->EventInfo.System.Type.Resize, 2, event.resize.w , event.resize.h ); } break;
  182.             case SDL_QUIT:{ me->listener_push_event( me->EventInfo.Type.System, me->EventInfo.System.Type.Quit, 0 ); } break;
  183.             case SDL_KEYDOWN:{me->listener_push_event( me->EventInfo.Type.Keyboard, me->EventInfo.Keyboard.Type.KeyDown, 2, event.key.keysym.sym, event.key.keysym.unicode );break;}
  184.             case SDL_KEYUP:{me->listener_push_event( me->EventInfo.Type.Keyboard, me->EventInfo.Keyboard.Type.KeyUp, 2, event.key.keysym.sym, event.key.keysym.unicode );break;}
  185.             case SDL_MOUSEMOTION:{me->listener_push_event( me->EventInfo.Type.Mouse, me->EventInfo.Mouse.Type.Move, 2, event.motion.x, event.motion.y );break;}
  186.             case SDL_MOUSEBUTTONDOWN:{me->listener_push_event( me->EventInfo.Type.Mouse, me->EventInfo.Mouse.Type.ClickDown, 3, event.button.button, event.button.x, event.button.y );break;}
  187.             case SDL_MOUSEBUTTONUP:{me->listener_push_event( me->EventInfo.Type.Mouse, me->EventInfo.Mouse.Type.ClickUp, 3, event.button.button, event.button.x, event.button.y );break;}
  188.         }
  189.     }
  190. }
  191.  
  192. void _SDL_create_screen ( Context* me, int width, int height, bool fullscreen, bool border, bool resizable )
  193. {
  194.     _SDL_SCREEN(me) = SDL_SetVideoMode( width, height, 32, SDL_SWSURFACE | ( SDL_FULLSCREEN * fullscreen ) | ( SDL_RESIZABLE * resizable ) | ( SDL_NOFRAME * border ) );
  195.     me->main = me->push_surface( _SDL_SCREEN(me) );
  196.     me->mainalt = me->main;
  197. }
  198.  
  199. Context::Image* _SDL_image_load ( Context* me, const char* fname )
  200. {
  201.     return me->push_image( (void*)
  202.                           //#ifdef ALIB_SDL_IMAGE
  203.                           IMG_Load( fname )
  204.                           //#else
  205.                           //SDL_LoadBMP( fname )
  206.                           //#endif
  207.                           );
  208. }
  209.  
  210. Context::Image* _SDL_image_load_mem ( Context* me, void* mem, int len )
  211. {
  212.     SDL_RWops* tmp = SDL_RWFromMem( mem, len );
  213.     return me->push_image( (void*)
  214.                           //#ifdef ALIB_SDL_IMAGE
  215.                           IMG_Load_RW( tmp, 1 )
  216.                           //#else
  217.                           //SDL_LoadBMP_RW( tmp, 1 )
  218.                           //#endif
  219.                            );
  220. }
  221.  
  222. Context::Surface* _SDL_surface_create ( Context* me, int w, int h)
  223. {
  224.     return me->push_surface( (void*) SDL_CreateRGBSurface( SDL_SWSURFACE | SDL_SRCALPHA,
  225.                                  w, h,
  226.                                  _SDL_SCREEN(me)->format->BitsPerPixel,
  227.                                  _SDL_SCREEN(me)->format->Rmask,
  228.                                  _SDL_SCREEN(me)->format->Gmask,
  229.                                  _SDL_SCREEN(me)->format->Bmask,
  230.                                  _SDL_SCREEN(me)->format->Amask ) );
  231. }
  232.  
  233. Context::Surface* _SDL_surface_from_image ( Context* me, Context::Image* in)
  234. {
  235.  
  236. }
  237.  
  238. Context::Image* _SDL_image_from_surface ( Context* me, Context::Image* in)
  239. {
  240.  
  241. }
  242.  
  243. Context::Sound* _SDL_sound_load ( Context* me, const char* fname )
  244. {
  245.  
  246. }
  247.  
  248. Context::Sound* _SDL_sound_load_mem ( Context* me, void*, int len )
  249. {
  250.  
  251. }
  252.  
  253. int _SDL_sound_unload ( Context* me, Context::Sound*)
  254. {
  255.  
  256. }
  257.  
  258. int _SDL_image_unload ( Context* me, Context::Image* img)
  259. {
  260.     SDL_FreeSurface( (SDL_Surface*)img->getdata() );
  261.     return true;
  262. }
  263.  
  264. int _SDL_surface_free ( Context* me, Context::Surface* img)
  265. {
  266.     SDL_FreeSurface( (SDL_Surface*)img->getdata() );
  267.     return true;
  268. }
  269.  
  270. void _SDL_draw_rectangle ( Context* me, int x, int y, int w, int h, Context::Color color, bool filled )
  271. {
  272.     SDL_Rect a;
  273.     a.x = x;
  274.     a.y = y;
  275.     a.w = w;
  276.     a.h = h;
  277.     SDL_FillRect( _SDL_SCREEN(me), &a, SDL_MapRGBA( _SDL_SCREEN(me)->format, color.r, color.g, color.b, color.a ) );
  278. }
  279.  
  280. void _SDL_draw_line ( Context* me, int x1, int y1, int x2, int y2, Context::Color color )
  281. {
  282.  
  283. }
  284.  
  285. void _SDL_draw_circle ( Context* me, int x, int y, int r, Context::Color color, bool filled )
  286. {
  287.  
  288. }
  289.  
  290. void _SDL_draw_ellipse ( Context* me, int x1, int y1, int x2, int y2, Context::Color color, bool filled )
  291. {
  292.  
  293. }
  294.  
  295. void _SDL_draw_image ( Context* me, int x, int y, Context::Image* img )
  296. {
  297.     SDL_Rect a = { 0, 0, x - img->origin.x, y - img->origin.y };
  298.     SDL_BlitSurface( (SDL_Surface*)img->getdata(), 0, (SDL_Surface*)me->main->getdata(), &a );
  299. }
  300.  
  301. void _SDL_draw_image_part ( Context* me, int x, int y, int left, int top, int w, int h, Context::Image* img )
  302. {
  303.     SDL_Rect a = { x - img->origin.x, y - img->origin.y, 0, 0 };
  304.     SDL_Rect b = { left, top, w, h };
  305.     SDL_BlitSurface( (SDL_Surface*)img->getdata(), &b, (SDL_Surface*)me->main->getdata(), &a );
  306. }
  307.  
  308. void _SDL_draw_surface ( Context* me, int x, int y, Context::Surface* img )
  309. {
  310.     SDL_Rect a = { 0, 0, x, y };
  311.     SDL_BlitSurface( (SDL_Surface*)img->getdata(), 0, (SDL_Surface*)me->main->getdata(), &a );
  312. }
  313.  
  314. void _SDL_draw_surface_part ( Context* me, int x, int y, int left, int top, int w, int h, Context::Surface* img )
  315. {
  316.     SDL_Rect a = { x, y, 0, 0 };
  317.     SDL_Rect b = { left, top, w, h };
  318.  
  319.     SDL_BlitSurface( (SDL_Surface*)img->getdata(), &b, (SDL_Surface*)me->main->getdata(), &a );
  320. }
  321.  
  322. int _SDL_sound_play ( Context* me, Context::Sound* )
  323. {
  324.  
  325. }
  326.  
  327. void _SDL_repaint ( Context* me, int x, int y, int w, int h )
  328. {
  329.     if( x < 0 || y < 0 || w < 0 || h < 0 )
  330.         SDL_Flip( _SDL_SCREEN(me) );
  331.     else
  332.         SDL_UpdateRect( _SDL_SCREEN(me), x, y, w, h );
  333. }
  334.  
  335. int _SDL_image_width ( Context::Image* me )
  336. {
  337.     return ((SDL_Surface*)me->getdata())->w;
  338. }
  339.  
  340. int _SDL_image_height ( Context::Image* me )
  341. {
  342.     return ((SDL_Surface*)me->getdata())->h;
  343. }
  344.  
  345. int _SDL_sound_getlength ( Context::Sound* me )
  346. {
  347.  
  348. }
  349.  
  350. int _SDL_sound_getsamplerate ( Context::Sound* me )
  351. {
  352.  
  353. }
  354.  
  355. int _SDL_surface_width ( Context::Surface* me )
  356. {
  357.     return ((SDL_Surface*)me->getdata())->w;
  358. }
  359.  
  360. int _SDL_surface_height ( Context::Surface* me )
  361. {
  362.     return ((SDL_Surface*)me->getdata())->h;
  363. }
  364.  
  365. void _SDL_cleanup ( Context* me )
  366. {
  367.     if( me->audioinited != 0 )
  368.         SDL_CloseAudio();
  369.     SDL_Quit();
  370. }
  371.  
  372. void _SDL_audiomixercallback(void* userdata, unsigned char *stream, int len)
  373. {
  374.     Context* me = (Context*) userdata;
  375.     if( me->mixaudio == 0 ){
  376.         if( me->audiomixercallback != 0 )
  377.         {
  378.             me->audiomixercallback( me->audiouserdata, stream, len );
  379.         }
  380.     } else {
  381.         void* exstream = malloc( len );
  382.         if( me->audiomixercallback != 0 )
  383.         {
  384.             me->audiomixercallback( me->audiouserdata, (unsigned char*)exstream, len );
  385.         }
  386.         SDL_MixAudio(stream, (Uint8*)exstream, len, SDL_MIX_MAXVOLUME);
  387.         free( exstream );
  388.     }
  389. }
  390.  
  391. int _SDL_init_sound ( Context* me, int freq, int rate, int bitrate ) //bitrate & 0x80 equals whether or not it's a signed value
  392. {
  393.     SDL_AudioSpec fmt;
  394.     fmt.freq = freq;
  395.     switch( bitrate ){
  396.         case 8: fmt.format = AUDIO_U8; break;
  397.         case 8+0x80: fmt.format = AUDIO_S8; break;
  398.         case 16: fmt.format = AUDIO_U16SYS; break;
  399.         case 16+0x80: fmt.format = AUDIO_S16SYS; break;
  400.     }
  401.     fmt.channels = 1;
  402.     fmt.samples = rate; /* A good value for games */
  403.     fmt.callback = _SDL_audiomixercallback;
  404.     fmt.userdata = (void*)me;
  405.  
  406.     /* Open the audio device and start playing sound! */
  407.     if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
  408.         fprintf(stderr, "Unable to open audio: %sn",
  409.         SDL_GetError());
  410.         return -1;
  411.     }
  412.     SDL_PauseAudio(0);
  413.     SDL_LockAudio();
  414.     SDL_UnlockAudio();
  415.     me->audioinited = 1;
  416.     return 0;
  417. }
  418. void _SDL_sound_set_callback ( Context* me, void* userdata, void (*audiomixercallbacka)(void*,unsigned char *stream, int len) )
  419. {
  420.     me->audiouserdata = userdata;
  421.     me->audiomixercallback = audiomixercallbacka;
  422. }
  423. void _SDL_set_caption ( Context* me, char* title, char* icon )
  424. {
  425.     SDL_WM_SetCaption(title, title);
  426.     SDL_WM_SetIcon( IMG_Load(icon) , 0 );// IMG_LoadICO(icon);
  427.  
  428. }
Advertisement
Add Comment
Please, Sign In to add comment