SDL User-made Surface Class

Hello,

I want to make an encapsulated surface class that keeps track of important aspects about a surface. I have it started, except when I test the .onDraw() function, nothing appears on the screen. My Code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "NSurface.h"

NSurface::NSurface()
{
	surface = NULL;

}

NSurface::~NSurface()
{
	SDL_FreeSurface(surface);
}

bool NSurface::onLoad(const char* filename)
{
	SDL_Surface* temp = SDL_LoadBMP(filename);

	if (temp == NULL)
		return false;

	surface = SDL_DisplayFormat(temp);

	SDL_FreeSurface(temp);

	height = surface->h;
	width = surface->w;

	return true;
}

bool NSurface::onClick(int mX, int mY)
{
	if (mX >= getXCoordinate() && mX <= getXCoordinate() + getWidth())
	{
		if (mY >= getYCoordinate() && mY <= getYCoordinate() + getHeight())
		{
			return true;
		}
	}

	return false;
}

void NSurface::onDraw(SDL_Surface* destination, int xCoord, int yCoord)
{
	setXCoordinate(xCoord);
	setYCoordinate(yCoord);

	SDL_Rect offset;

	offset.w = xCoord;
	offset.h = yCoord;

	SDL_BlitSurface(this->surface, NULL, destination, &offset);

}


-----------------------------Main Program-------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main(int argc, char* args[])
{
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		return 1;
	}

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

	NSurface surfaceOne;
	
	surfaceOne.onLoad("hi.bmp");

	surfaceOne.onDraw(screen, 100, 100);

	SDL_Flip(screen);

	SDL_Delay(2000);


	SDL_FreeSurface(screen);
	
	SDL_Quit();

	return 0;
	

}


Can you tell me why nothing is blitting to the screen when I test that class.onDraw() and SDL_Flip(screen)?
I'm almost certain that you're using an older version of SDL. I'd highly recommend you get the latest version from the website.

Try changing the 'onDraw' function to this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void NSurface::onDraw(SDL_Surface* destination, int xCoord, int yCoord)
{
	setXCoordinate(xCoord);
	setYCoordinate(yCoord);

	SDL_Rect offset;

	offset.x = xCoord;
	offset.y = yCoord;
	offset.w = this->surface->w;
	offset.h = this->surface->h;

	SDL_BlitSurface(this->surface, NULL, destination, &offset);

}
Last edited on
Topic archived. No new replies allowed.