[Help?] C++ SDL unsuccesful screen blitting

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

[Help?] C++ SDL unsuccesful screen blitting

Post 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 );
}
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post by dandymcgee »

1) Did you call SDL_Init()?
2) Do you have libpng DLL in the bin directory?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

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

Post 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
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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*.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

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

Post 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.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

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

Post 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.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
Post Reply