Page 1 of 1
[SOLVED] No image displayed at blit? (SDL, C++)
Posted: Mon Jun 17, 2013 11:41 am
by MadPumpkin
All of my code compiles and runs fine, I've been struggling to get this working for a long while now. It seems as though there's something wrong with the method by which I'm blitting to the screen, as there is simply no image displayed. If it can be explained what is wrong with my implementation, I would like to fully understand what makes this not work if any particular details can be given. Any help would be much appreciated, been tackling this for a few hours trying different implementations altogether!
EDIT: Earlier, I was passing an SDLTexture by reference to w.Draw([here],0,0 )), and had SDLWindow's Draw function simply invoke SDLTexture's Draw function onto SDLWindow's mScreen (SDLSetVideo) which would indeed be my preferred method, though neither works (not that it's REALLY different here)
The main implementation making use of the code below, is simply: ( w.Render contains only SDL_Flip right now. )
Code: Select all
SDLTexture eye = SDLTexture( "Lucid_Eye_Auras.jpg" );
w.Draw( eye.Surface(), 0, 0 );
w.Render();
SDLWindow class Init and Draw functions:
Code: Select all
bool SDLWindow::Init()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
return false;
/* AS YOU CAN SEE, mScreen IS BEING PROPERLY SET UP */
if( ( mScreen = SDL_SetVideoMode( mWidth, mHeight, mBPP, mFlags ) ) == NULL )
return false;
SDL_EnableUNICODE( SDL_TRUE );
SDL_WM_SetCaption( mTitle, mIcon );
return true;
}
/* draw function */
void SDLWindow::Draw( SDL_Surface* surface, int x, int y, SDL_Rect *clip )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
if( ( surface != NULL ) && ( mScreen != NULL ) )
SDL_BlitSurface( surface, clip, mScreen, &offset );
}
Then I have the relevant SDLTexture functions here:
Code: Select all
void SDLTexture::Load( std::string filename, Uint32 trans ) // trans = 0xff00ff by default
{
SDL_Surface* loadedSurface = NULL, *optimizedSurface = NULL;
loadedSurface = IMG_Load( filename.c_str() );
if( loadedSurface != NULL )
{
optimizedSurface = SDL_DisplayFormat( loadedSurface );
SDL_FreeSurface( loadedSurface );
SDL_SetColorKey( optimizedSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, trans );
}
mSurface = optimizedSurface;
}
/* This one is used in other functions, but in particular it's used to get the surface pointer from within the class, in the main implementation */
inline SDL_Surface* Surface(){ return mSurface; }
Re: No image displayed at blit? (SDL, C++)
Posted: Mon Jun 17, 2013 3:08 pm
by dandymcgee
Is the SDL_OPENGL flag set? If so, you won't be able to do any software rendering (blits / flips) on top of the OGL context.
You have to either use OGL, or not use OGL, it seems like you're trying to mix hardware rendering with software rendering.
Re: No image displayed at blit? (SDL, C++)
Posted: Mon Jun 17, 2013 3:30 pm
by Nokurn
Code: Select all
if( GetGL() ) {
if( mFlags & ~SDL_OPENGL ) {
mFlags |= SDL_OPENGL;
}
} else {
if( mFlags & SDL_OPENGL ) {
SetGL( true );
}
}
// ... snip ...
if( GetGL() ) {
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
}
These bits look highly suspicious. Unless you're writing your
entire renderer to be capable of switching between OpenGL rendering or software rendering, you should only be using one. Do not mix hardware and software rendering in SDL.
I don't see why you would even need to care about situations were OpenGL isn't available, because virtually all consumer graphics cards going back to the late nineties are capable of some version of OpenGL. Unless you're targeting some very specific hardware that only supports software rendering, I would recommend switching entirely to OpenGL and targeting a commonly-available version, such as 1.5, which has been available since mid-2003 and includes vertex buffer objects. If you go for 2.0 instead, which was released in 2004, you'll get shaders as well and you will be able to forego the fixed pipeline entirely.
Re: No image displayed at blit? (SDL, C++)
Posted: Mon Jun 17, 2013 9:12 pm
by MadPumpkin
I have that in there because normally I use this for OpenGL rendering, but I would like to get SDL rendering functional since I haven't yet for this project. Commenting out those parts does not solve my problem, they function as intended (though I understand your point fully)
EDIT: in fact I've already implemented shaders and what have you on the OpenGL rendering side, I'm just not proficient with the non-fixed rendering pipeline in OpenGL and would like to write a small prototype using the code that I AM familiar with, raw SDL.
EDIT: I've removed that bit from the original post to avoid confusion about it being a possible reason.
Re: No image displayed at blit? (SDL, C++)
Posted: Tue Jun 18, 2013 7:21 am
by dandymcgee
You still have stuff like this:
Code: Select all
if( ( mScreen = SDL_SetVideoMode( mWidth, mHeight, mBPP, mFlags ) ) == NULL )
Which makes it impossible for someone who can't see those variable declarations to have any idea what that line of code does. For all I know mWidth is set to -1.
Perhaps you should make a copy of the project and slowly remove things until it works. That should help you find the culprit.
Re: EDITED: No image displayed at blit? (SDL, C++)
Posted: Tue Jun 18, 2013 1:11 pm
by Nokurn
After combing through it, I could only find a few questionable things, nothing that I can be certain is causing your problem.
Code: Select all
SDLTexture eye = SDLTexture( "Lucid_Eye_Auras.jpg" );
You use the SDLTexture constructor yet the loading logic is in SDLTexture::Load. Does the constructor call Load? Do you have a copy constructor or copy assignment operator? If so, check them, or initialize eye like so:
Code: Select all
SDLTexture eye( "Lucid_Eye_Auras.jpg" );
Debug to ensure that eye.Surface() is not null.
Code: Select all
SDL_SetColorKey( optimizedSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, trans );
When SDL accepts colors, it expects them to be mapped using SDL_MapRGB or SDL_MapRGBA, so that the bits are ordered correctly for the surface's format. I would suggest replacing the trans parameter with individual red, green, and blue parameters and passing SDL_MapRGB( optimizedSurface->format, r, g, b ) to SDL_SetColorKey. Just for the sake of debugging I would also try removing SDL_RLEACCEL.
If none of these are the problem, you will need to post more code. As dandymcgee has said, the definitions of the parameters for SDL_SetVideoMode are of particular interest.
Re: EDITED: No image displayed at blit? (SDL, C++)
Posted: Tue Jun 18, 2013 2:06 pm
by dandymcgee
Nokurn wrote:
Code: Select all
SDL_SetColorKey( optimizedSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, trans );
[...] Just for the sake of debugging I would also try removing SDL_RLEACCEL.
Alternatively, you could comment this line entirely. A color key isn't required to get
something to render.
I generally step through with the debugger looking suspect values first. If that doesn't work, I start commenting / removing large chunks of code until I figure out what's causing the problem.
Re: [SOLVED] No image displayed at blit? (SDL, C++)
Posted: Wed Jun 19, 2013 4:25 pm
by MadPumpkin
2 issues: I had forgotten to include the required jpeg libraries .dll in my project's folder. I had been loading an image into SDLTexture eye, before initializing SDL. I now know that this one is a no no and it makes sense. Thanks for the help, I managed to get it to show after commenting out all sorts of other shit, so now I'm dropping those things in and seeing if it can still be functional. At least this gave me a reason to finally put a bunch of error checking in :P. Thanks dandymcgee, nokurn.
Re: [SOLVED] No image displayed at blit? (SDL, C++)
Posted: Wed Jun 19, 2013 5:02 pm
by dandymcgee
MadPumpkin wrote:I had forgotten to include the required jpeg libraries .dll in my project's folder.
Damn, I haven't used SDL in a while and completely forgot about that issue. Extremely annoying that it doesn't throw a "missing .dll" error.
Re: [SOLVED] No image displayed at blit? (SDL, C++)
Posted: Wed Jun 19, 2013 10:33 pm
by MadPumpkin
Exactly my thoughts :P. Though it is my fault for failing to include that, in my defense I'm not actually using .jpg's in my project (as of now or near future) I only was using an offhand one for testing. I had included the libpng dll I needed at least!
Note to self: Test within actual boundaries of future use xP.