Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* As a brief intro, this is my thoughts on binding SDL's C interface to QB64
- * Ideas presented were formed from the SDL mailing list. Here, I will make
- * a sample of best practices for porting the library. This is using SDL 1.2,
- * as SDL 1.3 is still rapidly changing to be usable on all systems. Integrating
- * with SDL 1.3 later should be fairly simple, still.
- * Anyway, enough chat, let's get down to business
- */
- // --------------------------------------------------
- // QB64SDL_video.h
- #include <SDL/SDL.h>
- #ifndef _QB64SDL_Video
- #define _QB64SDL_Video
- #define QB64SDL_VideoIndex 0
- #define QB64SDL_ERROR -1
- int QB64SDL_SetVideoMode( int x, int y, int depth, int flags ); // Create a Video Mode (A graphics window)
- int QB64SDL_CreateRGBSurface( int x, int y, int depth, int flags ); // Create a Video Surface
- //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.
- void QB64SDL_FreeSurface( int vindex ); // Free a surface
- SDL_Rect QB64SDL_Rectangle( int x, int y, int w, int h ); // Helper function to create SDL rects
- int QB64SDL_CreateRectList( void ); // Create a list of rectangles
- void QB64SDL_PushRectList( int rindex, SDL_Rect rectangle ); // Add a rectangle to the list
- void QB64SDL_FreeRectList( int rindex ); // Clean up a rectangle list
- int QB64SDL_IsSurfaceLocked( int vindex ); // Check to see if a surface is locked
- int QB64SDL_SurfaceLock( int vindex ); // Lock a surface
- int QB64SDL_SurfaceUnlock( int vindex ); // Unlock a surface
- SDL_Rect QB64SDL_SurfaceGetRect( int vindex ); // Helper function to get size of a surface (since we can't pass a NULL)
- void QB64SDL_FillRect( int vindex, SDL_Rect rectangle, Uint32 c ); // Fill a rectangle
- void QB64SDL_FillRectRGB( int vindex, SDL_Rect rectangle, SDL_Color c ); // Same as above, but using struct SDL_Color
- void QB64SDL_SetPixel( int vindex, int x, int y, Uint32 c ); // Set a pixel's colour
- void QB64SDL_SetPixelRGB( int vindex, int x, int y, SDL_Color c ); // Same as above, but using struct SDL_Color
- void QB64SDL_BlitSurface( int vsource, SDL_Rect srect, int vdestination, SDL_Rect drect ); // Blit a surface to another (or to the video display)
- #endif /* _QB64SDL_Video */
- // ----------------------------------
- // QB64SDL_video.c
- #include "QB64SDL_video.h"
- int num_surfaces = 0;
- int num_rectlists = 0;
- SDL_Surface **surfaces = NULL;
- SDL_Rect **rectlists = NULL;
- int QB64SDL_SetVideoMode( int x, int y, int depth, int flags ) {
- if( surfaces != NULL )
- return QB64SDL_ERROR; // Will SDL allow you to call this after the program begins?
- num_surfaces = 1;
- surfaces = (SDL_Surface **)alloc( sizeof( SDL_Surface *), num_surfaces );
- surfaces[QB64SDL_VideoIndex] = SDL_SetVideoMode( x, y, depth, flags );
- return QB64SDL_VideoIndex;
- }
- int QB64SDL_CreateRGBSurface( int x, int y, int depth, int flags ) {
- if( surfaces == NULL ) return QB64SDL_ERROR; // This means that the main video window hasn't been created :(
- num_surfaces++;
- surfaces = (SDL_Surface **)realloc( surfaces, sizeof( SDL_Surface *), num_surfaces );
- surfaces[num_surfaces-1] = SDL_CreateRGBSurface( x, y, depth, flags );
- return num_surfaces-1;
- }
- //int QB64SDL_CreateRGBSurfaceFrom( int vindex ); // Not sure of implementation...
- void QB64SDL_FreeSurface( int vindex ) {
- if( ( surfaces == NULL ) || ( vindex >= num_surfaces ) ) return; //Video hasn't been created, or index doesn't exist
- if( vindex == QB64SDL_VideoIndex ) return; // Can't free video surface like this!
- SDL_FreeSurface( surfaces[vindex] );
- surfaces[vindex] = NULL;
- SDL_Surface *tmp;
- for(int i = num_surfaces-2; i > vindex; i-- ) {
- tmp = surfaces[i+1];
- surfaces[i+1] = surfaces[i];
- surfaces[i] = tmp;
- }
- num_surfaces--;
- surfaces = (SDL_Surface **)realloc( surfaces, sizeof( SDL_Surface * ), num_surfaces );
- }
- // And so on....
- // This isn't complete, but it's a start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement