View Full Version : SDL Clipping
John Spikowski
16-02-2014, 19:06
I was wondering if any of the graphics pros here might offer some guidance with a strange issue I having with SDL. I'm trying to combine the functionally of both the BBC and SDL_draw extension libraries and I have this strange issue. I was hoping I could take advantage of the BBC clipping features of the library that SDL_draw doesn't offer. Clipping works great on the vertical axis but wraps the clipped image horizontal. Searching the net I have seen OpenGL settings to control edge clipping and so on. Is there a SDL option on window creation to prevent the wrapping effect and clip like the vertical axis?
Charles Pegge
16-02-2014, 19:38
I don't know much about SDL but, the docs say you can set a clipping rectangle (prior to drawing something)
http://search.cpan.org/~wkeenan/SDL-sdlpl-1.08/lib/SDL/Surface.pm#Clipping
John Spikowski
16-02-2014, 19:59
Thanks Charles for the tip but my last attempt didn't work either. (see attached image) I used the SDL_SetClipRect (http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlsetcliprect.html) hoping that would work but no luck.
screen = SDL_SetVideoMode(width, height, 32, videoflags);
SDL_SetClipRect(screen, NULL);
John Spikowski
16-02-2014, 23:23
Well, it looks like this isn't a BBC issue as it does the same thing when using SDL_draw alone. The SDL_SetClipRect did seem to prevent the seg fault I was getting when rendering off screen without the call. If I can get by the horizontal wrapping SDL is imposing, I won't need BBC for the clipping support. Any ideas are welcome and appreciated.
With a bit more testing it looks like it does seg fault if I render vertically off screen using SDL_draw alone. If I can solve this wrapping issue I can make SDL_draw clipping work in conjunction with the BBC extension module.
I noticed that passing a NULL for the RECT structure disabled clipping which is the opposite effect of what I was looking for. I changed my SDL_draw window create function to pass a valid RECT structure. Here is my code that still doesn't work and wraps the image.
SB Draw extension module source (https://bitbucket.org/ScriptBasic/c-basic/src/fdbe857f4c7542f4670e2bb92222e53a1e3a464a/SBext/SDL_draw/?at=master)
besFUNCTION(SDL_Draw_Window)
DIM AS int width, height;
DIM AS Uint32 videoflags;
DIM AS const char PTR title;
DIM AS SDL_Rect screct;
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
videoflags = SDL_SWSURFACE | SDL_ANYFORMAT;
besARGUMENTS("ii[z]")
AT width, AT height, AT title
besARGEND
IF (title EQ NULL) THEN_DO title = "";
screen = SDL_SetVideoMode(width, height, 32, videoflags);
screct.x = 0;
screct.y = 0;
screct.w = 800;
screct.h = 600;
SDL_SetClipRect(screen, AT screct);
SDL_WM_SetCaption(title, 0);
besRETURNVALUE = NULL;
besEND
John Spikowski
20-02-2014, 07:48
I solved my SDL clipping issues.
http://files.allbasic.info/ScriptBasic/gfx/sbgfxu64_alpha_circles.png
' ScriptBasic GFX - Alpha Circles
IMPORT gfx.inc
gfx::Window 640, 480, "ScriptBasic GFX - Alpha Circles"
' Random Value Arrays
RANDOMIZE(gfx::Time())
FOR i = 0 TO 512
rx[i] = RND() % 640
ry[i] = 60 + RND() % 480 - 80
rz[i] = RND() % 64
rr[i] = RND() AND 255
rg[i] = RND() AND 255
rb[i] = RND() AND 255
af = rx[i] / 640
ra[i] = INT(255 * af)
NEXT
ts = gfx::Time()
gfx::SDL_SetClipRect 0, 0, 640, 480
FOR i = 0 TO 512
gfx::filledCircleRGBA rx[i], ry[i], rz[i], rr[i], rg[i], rb[i], ra[i]
NEXT
te = gfx::Time()
gfx::stringColor 20, 15,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
gfx::Update
WHILE gfx::KeyName(1) <> "-escape"
WEND
gfx::Close