Advertisement
Guest User

QB64 SDL Wrapper/Binding Library Ideology

a guest
Dec 7th, 2010
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.97 KB | None | 0 0
  1. /* As a brief intro, this is my thoughts on binding SDL's C interface to QB64
  2.  * Ideas presented were formed from the SDL mailing list.  Here, I will make
  3.  * a sample of best practices for porting the library.  This is using SDL 1.2,
  4.  * as SDL 1.3 is still rapidly changing to be usable on all systems.  Integrating
  5.  * with SDL 1.3 later should be fairly simple, still.
  6.  * Anyway, enough chat, let's get down to business
  7.  */
  8.  
  9. // --------------------------------------------------
  10. // QB64SDL_video.h
  11. #include <SDL/SDL.h>
  12.  
  13. #ifndef _QB64SDL_Video
  14. #define _QB64SDL_Video
  15.  
  16. #define QB64SDL_VideoIndex  0
  17. #define QB64SDL_ERROR       -1
  18.  
  19. int QB64SDL_SetVideoMode( int x, int y, int depth, int flags );         // Create a Video Mode (A graphics window)
  20. int QB64SDL_CreateRGBSurface( int x, int y, int depth, int flags );     // Create a Video Surface
  21. //int QB64SDL_CreateRGBSurfaceFrom( int vindex );               // Not sure exactly how this would be implemented yet.  You may need some sort of QB64 interface to exchange void *pixel data.
  22. void QB64SDL_FreeSurface( int vindex );                     // Free a surface
  23.  
  24. SDL_Rect QB64SDL_Rectangle( int x, int y, int w, int h );           // Helper function to create SDL rects
  25. int QB64SDL_CreateRectList( void );                     // Create a list of rectangles
  26. void QB64SDL_PushRectList( int rindex, SDL_Rect rectangle );            // Add a rectangle to the list
  27. void QB64SDL_FreeRectList( int rindex );                    // Clean up a rectangle list
  28.  
  29. int QB64SDL_IsSurfaceLocked( int vindex );                  // Check to see if a surface is locked
  30. int QB64SDL_SurfaceLock( int vindex );                      // Lock a surface
  31. int QB64SDL_SurfaceUnlock( int vindex );                    // Unlock a surface
  32.  
  33. SDL_Rect QB64SDL_SurfaceGetRect( int vindex );                  // Helper function to get size of a surface (since we can't pass a NULL)
  34. void QB64SDL_FillRect( int vindex, SDL_Rect rectangle, Uint32 c );      // Fill a rectangle
  35. void QB64SDL_FillRectRGB( int vindex, SDL_Rect rectangle, SDL_Color c );    // Same as above, but using struct SDL_Color
  36. void QB64SDL_SetPixel( int vindex, int x, int y, Uint32 c );            // Set a pixel's colour
  37. void QB64SDL_SetPixelRGB( int vindex, int x, int y, SDL_Color c );      // Same as above, but using struct SDL_Color
  38.  
  39. void QB64SDL_BlitSurface( int vsource, SDL_Rect srect, int vdestination, SDL_Rect drect );  // Blit a surface to another (or to the video display)
  40.  
  41. #endif /* _QB64SDL_Video */
  42.  
  43. // ----------------------------------
  44. // QB64SDL_video.c
  45. #include "QB64SDL_video.h"
  46.  
  47. int num_surfaces = 0;
  48. int num_rectlists = 0;
  49. SDL_Surface **surfaces = NULL;
  50. SDL_Rect **rectlists = NULL;
  51.  
  52. int QB64SDL_SetVideoMode( int x, int y, int depth, int flags ) {
  53.     if( surfaces != NULL )
  54.         return QB64SDL_ERROR; // Will SDL allow you to call this after the program begins?
  55.     num_surfaces = 1;
  56.     surfaces = (SDL_Surface **)alloc( sizeof( SDL_Surface *), num_surfaces );
  57.     surfaces[QB64SDL_VideoIndex] = SDL_SetVideoMode( x, y, depth, flags );
  58.     return QB64SDL_VideoIndex;
  59. }
  60.  
  61. int QB64SDL_CreateRGBSurface( int x, int y, int depth, int flags ) {
  62.     if( surfaces == NULL ) return QB64SDL_ERROR;    // This means that the main video window hasn't been created :(
  63.     num_surfaces++;
  64.     surfaces = (SDL_Surface **)realloc( surfaces, sizeof( SDL_Surface *), num_surfaces );
  65.     surfaces[num_surfaces-1] = SDL_CreateRGBSurface( x, y, depth, flags );
  66.     return num_surfaces-1;
  67. }
  68.  
  69. //int QB64SDL_CreateRGBSurfaceFrom( int vindex ); // Not sure of implementation...
  70.  
  71. void QB64SDL_FreeSurface( int vindex ) {
  72.     if( ( surfaces == NULL ) || ( vindex >= num_surfaces ) ) return;    //Video hasn't been created, or index doesn't exist
  73.     if( vindex == QB64SDL_VideoIndex ) return; // Can't free video surface like this!
  74.     SDL_FreeSurface( surfaces[vindex] );
  75.     surfaces[vindex] = NULL;
  76.     SDL_Surface *tmp;
  77.     for(int i = num_surfaces-2; i > vindex; i-- ) {
  78.         tmp = surfaces[i+1];
  79.         surfaces[i+1] = surfaces[i];
  80.         surfaces[i] = tmp;
  81.     }
  82.     num_surfaces--;
  83.     surfaces = (SDL_Surface **)realloc( surfaces, sizeof( SDL_Surface * ), num_surfaces );
  84. }
  85.  
  86. // And so on....
  87.  
  88. // This isn't complete, but it's a start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement