Page 1 of 1

[Help?] C++ SDL unsuccesful screen blitting

Posted: Sun Nov 04, 2012 6:40 pm
by MadPumpkin
My code compiles and runs just fine, but it will not draw my loaded surface "definition.png". I have a class SDLRenderer and a class SDLScene. The scene has surface drawing functions, to it's member surface then the whole scene is passed to the renderer... The renderer contains a function: drawScene( SDLScene* scene ) which should take the scenes member SDL_Surface and draw it to "screenSurface_" which is set up as the window surface after init().

EDIT:: Basically the "Scene" is just for drawing to, as a sorta buffer region, which can then be passed to the renderer, and the renderer will draw whatever scene is passed to it, to the screen buffer.

SDLScene.h

Code: Select all

#ifndef SDLSCENE_H
#define SDLSCENE_H

#include <vector>
#include "SDL/SDL.h"
#include "SDLImage.h"
#include "SDLFont.h"

#include "Math2D.h"


class SDLScene
{
public:
	SDLScene();
	~SDLScene();

	void drawSurface( SDL_Surface *surface, Vec2i point );
	void drawSurface( SDLImage *image );
	void drawSurface( SDLFont *font );

	inline SDL_Surface* sceneSurface () { return sceneSurface_; }
	inline void sceneSurface ( SDL_Surface* surface ) { sceneSurface_ = surface; }

private:
	SDL_Surface* sceneSurface_;

};

#endif
SDLScene.cpp

Code: Select all

#include "SDLScene.h"

SDLScene::SDLScene ()
{
	sceneSurface_ == NULL;
}

SDLScene::~SDLScene ()
{
}

void SDLScene::drawSurface( SDL_Surface *surface, Vec2i point )
{
	SDL_Rect offset;
	offset.x = point.x_;
	offset.y = point.y_;

	SDL_BlitSurface( surface, NULL, sceneSurface_, &offset );
}

void SDLScene::drawSurface( SDLImage *image )
{
	drawSurface( image->surface(), image->point() );
}

void SDLScene::drawSurface( SDLFont *font )
{
	drawSurface( font->surface(), font->point() );
}
SDLRenderer.h

Code: Select all

#ifndef SDLRENDERER_H
#define SDLRENDERER_H

#include <string>
#include "SDL/SDL.h"
#include "SDLScene.h"

#include "Math2D.h"


class SDLRenderer
{
public:
	SDLRenderer ();
	~SDLRenderer ();

	virtual bool init ( int width, int height, int bpp, int flags);
	virtual void cleanUp ();
	virtual void tick ();

	void drawScene( SDLScene *scene );

	inline Vec2i point( Vec2i p ) { return point_; }
	inline void point( int x, int y ) { point_.x_ = x; point_.y_ = y; }

private:
	SDL_Surface *screenSurface_; // SDL < 2.0

	Vec2i point_;
};

#endif
SDLRenderer.cpp

Code: Select all

#include <iostream>

#include "SDL/SDL.h"
#include "SDLImage.h"
#include "SDLFont.h"
#include "SDLRenderer.h"

#include "Math2D.h"


SDLRenderer::SDLRenderer ()
{
	screenSurface_ = NULL;
	point( 0, 0 );
}

SDLRenderer::~SDLRenderer ()
{
}

bool SDLRenderer::init ( int width, int height, int bpp, int flags)
{
	screenSurface_ = SDL_SetVideoMode( width, height, bpp, flags );
	if(screenSurface_ == NULL)
		return false;
	return true;
}

void SDLRenderer::cleanUp ()
{
}

void SDLRenderer::tick ()
{
	SDL_Flip( screenSurface_ );
}

void SDLRenderer::drawScene( SDLScene *scene )
{
	SDL_Rect offset;
	offset.x = point_.x_;
	offset.y = point_.y_;

	SDL_BlitSurface( scene->sceneSurface(), NULL, screenSurface_, &offset );
}

Re: [Help?] C++ SDL unsuccesful screen blitting

Posted: Mon Nov 05, 2012 6:36 pm
by dandymcgee
1) Did you call SDL_Init()?
2) Do you have libpng DLL in the bin directory?

Re: [Help?] C++ SDL unsuccesful screen blitting

Posted: Mon Nov 05, 2012 8:02 pm
by MadPumpkin
Yes, and no but it doesn't need to be, I installed it using the repos (Debian). If I remove SDLScene from the project altogether, and place the "drawSurface( whatever )" functions directly into SDLRenderer then it draws the images flawlessly because they're going directly to the screen buffer... But drawing a surface to the surface in SDLScene, then passing the SDLScene to SDLRenderer doesn't seem to work at all

Re: [Help?] C++ SDL unsuccesful screen blitting

Posted: Mon Nov 05, 2012 9:30 pm
by MarauderIIC
Prototype

Code: Select all

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

Code: Select all

class SDLScene {
	inline >>>void<<< sceneSurface ( SDL_Surface* surface ) { sceneSurface_ = surface; }

Code: Select all

 (...SDLScene* scene) {
	SDL_BlitSurface( >>>scene->sceneSurface()<<<, NULL, screenSurface_, &offset );
}
I don't understand why this compiles. sceneSurface() returns void but SDL_BlitSurface wants a SDL_Surface*.

Re: [Help?] C++ SDL unsuccesful screen blitting

Posted: Mon Nov 05, 2012 10:10 pm
by MadPumpkin
There's both a

Code: Select all

SDL_Surface* sceneSurface()
which returns sceneSurface_; and a

Code: Select all

void sceneSurface ( SDL_Surface* surface)
which sets sceneSurface_ to the SDL_Surface given. I already know they're not causing the issue, because I use patterns like this all the time.

Re: [Help?] C++ SDL unsuccesful screen blitting

Posted: Mon Nov 05, 2012 11:34 pm
by MarauderIIC
Doh, missed that second function entirely.
If I remove SDLScene from the project altogether, and place the "drawSurface( whatever )" functions directly into SDLRenderer
So something is likely wrong inside SDLScene. I don't think you made a pointer error, which is what I was looking for initially.

Is it something silly like your destination rect is wrong? Hard to tell because we don't see where you set point_'s x and y.
I'm thinking that you're doing offsets twice. Once in SDLScene and once in SDLRenderer.

Been a long time since I used SDL. Do you not need to initialize (SetVideoMode or GetVideoSurface) the sceneSurface in SDLScene? In general with this code post there's not enough data to be able to trace the problem correctly (I can't see where all fns are called)

Re: [Help?] C++ SDL unsuccesful screen blitting

Posted: Tue Nov 06, 2012 5:38 pm
by MadPumpkin
Alright, well thanks for your help. I've abandoned the SDLScene pattern for a bit till I can rewrite it more efficiently anyway. Right now I've replaced it altogether with an SDLImage class, which the other SDLImage's draw to, then drawing to the screen. Which is essentially what I wanted anyway, but with slightly less super powers. I think I'm going to leave this unsolved just in case anyone else stumbles here and notices any problem though.